CHGame 0.3.0
Libraries for the CHGame handheld
Loading...
Searching...
No Matches
SPIClass Class Reference

An SPI master: the Arduino SPI API on one of the chip's SPI peripherals (the global SPI on this board). More...

#include <SPI.h>

Public Member Functions

 SPIClass ()
 An SPI on the variant's default pins: MOSI, MISO and SCK (PA7, PA6 and PA5 on this board), with no hardware chip select.
 
 SPIClass (uint32_t mosi, uint32_t miso, uint32_t sclk, uint32_t ssel=PNUM_NOT_DEFINED)
 An SPI on the given pins, which must all belong to the same SPI peripheral (on the CH32X035 only SPI1's PA7, PA6, PA5 and PA4 do).
 
virtual void begin (uint8_t _pin=CS_PIN_CONTROLLED_BY_USER)
 Starts the SPI with the pin's settings (the defaults the first time: 3 MHz, MSB first, SPI_MODE0).
 
void end (void)
 Stops the SPI: resets SPI1, switches its clock off and forgets every pin's settings.
 
virtual void beginTransaction (uint8_t pin, SPISettings settings)
 Stores a device's settings for a CS pin and programs SPI1 with them.
 
void beginTransaction (SPISettings settings)
 Programs SPI1 with a device's settings, for a device whose CS the sketch drives itself.
 
void endTransaction (uint8_t pin)
 Forgets the pin's settings: transfers for it do nothing until the next begin() or beginTransaction() for it.
 
void endTransaction (void)
 Forgets the settings of the sketch-driven CS: transfer() does nothing until the next begin() or beginTransaction().
 
void usingInterrupt (uint8_t interruptNumber)
 Does nothing (kept for compatibility): transfers are never guarded against interrupts.
 
void attachInterrupt (void)
 Does nothing (kept for compatibility).
 
void detachInterrupt (void)
 Does nothing (kept for compatibility).
 
SPI_HandleTypeDef * getHandle (void)
 The WCH peripheral library's view of this SPI: its instance and init structure, for mixing in WCH's own SPI functions.
 
Pins (call before begin())

On the CH32X035 SPI1 has one pin for each role, so these only matter for an SPIClass made with no pins at all.

void setMISO (uint32_t miso)
 Sets the MISO pin.
 
void setMOSI (uint32_t mosi)
 Sets the MOSI pin.
 
void setSCLK (uint32_t sclk)
 Sets the clock pin.
 
void setSSEL (uint32_t ssel)
 Sets a hardware chip select, driven by the peripheral itself.
 
void setMISO (PinName miso)
 Sets the MISO pin.
 
void setMOSI (PinName mosi)
 Sets the MOSI pin.
 
void setSCLK (PinName sclk)
 Sets the clock pin.
 
void setSSEL (PinName ssel)
 Sets a hardware chip select.
 
Transfers

Each one needs begin() or beginTransaction() for its pin first.

With a CS pin other than the last transfer's, SPI1 is reset and programmed with that pin's settings first. The class drives the pin low for the transfer and, with SPI_LAST, high after it.

virtual byte transfer (uint8_t pin, uint8_t _data, SPITransferMode _mode=SPI_LAST)
 Sends one byte and returns the byte received meanwhile.
 
virtual uint16_t transfer16 (uint8_t pin, uint16_t _data, SPITransferMode _mode=SPI_LAST)
 Sends 16 bits as two bytes and returns the 16 bits received.
 
virtual void transfer (uint8_t pin, void *_buf, size_t _count, SPITransferMode _mode=SPI_LAST)
 Sends a buffer, replacing its bytes with the bytes received.
 
virtual void transfer (byte _pin, void *_bufout, void *_bufin, size_t _count, SPITransferMode _mode=SPI_LAST)
 Sends one buffer and receives into another.
 
byte transfer (uint8_t _data, SPITransferMode _mode=SPI_LAST)
 Sends one byte and returns the byte received meanwhile (CS driven by the sketch).
 
uint16_t transfer16 (uint16_t _data, SPITransferMode _mode=SPI_LAST)
 Sends 16 bits and returns the 16 bits received (CS driven by the sketch).
 
void transfer (void *_buf, size_t _count, SPITransferMode _mode=SPI_LAST)
 Sends a buffer, replacing its bytes with the bytes received (CS driven by the sketch).
 
void transfer (void *_bufout, void *_bufin, size_t _count, SPITransferMode _mode=SPI_LAST)
 Sends one buffer and receives into another (CS driven by the sketch).
 
Deprecated settings

Kept for compatibility: use SPISettings with beginTransaction() instead.

Each changes one of the pin's settings and then resets and programs SPI1 with them; for a pin with no settings it does nothing.

void setBitOrder (uint8_t _pin, BitOrder)
 Sets the bit order for a CS pin.
 
void setBitOrder (BitOrder _order)
 Sets the bit order.
 
void setDataMode (uint8_t _pin, uint8_t)
 Sets the mode for a CS pin.
 
void setDataMode (uint8_t _mode)
 Sets the mode.
 
void setClockDivider (uint8_t _pin, uint8_t)
 Sets the clock for a CS pin as a divider of 48 MHz.
 
void setClockDivider (uint8_t _div)
 Sets the clock as a divider of 48 MHz.
 

Protected Attributes

spi_t _spi
 the instance's pins, peripheral and WCH init structure
 

Detailed Description

An SPI master: the Arduino SPI API on one of the chip's SPI peripherals (the global SPI on this board).

Every method takes an optional CS pin first. Without it (or with CS_PIN_CONTROLLED_BY_USER) the sketch drives its device's chip select itself; with it, the class drives that pin low for the transfer and high after it, and keeps settings for it apart from other pins'.

Constructor & Destructor Documentation

◆ SPIClass() [1/2]

SPIClass::SPIClass ( )

An SPI on the variant's default pins: MOSI, MISO and SCK (PA7, PA6 and PA5 on this board), with no hardware chip select.

◆ SPIClass() [2/2]

SPIClass::SPIClass ( uint32_t  mosi,
uint32_t  miso,
uint32_t  sclk,
uint32_t  ssel = PNUM_NOT_DEFINED 
)

An SPI on the given pins, which must all belong to the same SPI peripheral (on the CH32X035 only SPI1's PA7, PA6, PA5 and PA4 do).

Parameters
mosithe MOSI pin (PA7).
misothe MISO pin (PA6).
sclkthe clock pin (PA5).
ssela hardware chip select the peripheral drives itself (PA4, the panel's CS on this board), or PNUM_NOT_DEFINED (the default) for none. With one, do not pass CS pins to the other methods.

Member Function Documentation

◆ setMISO() [1/2]

void SPIClass::setMISO ( uint32_t  miso)
inline

Sets the MISO pin.

Parameters
misothe pin's Arduino number (PA6).

◆ setMOSI() [1/2]

void SPIClass::setMOSI ( uint32_t  mosi)
inline

Sets the MOSI pin.

Parameters
mosithe pin's Arduino number (PA7).

◆ setSCLK() [1/2]

void SPIClass::setSCLK ( uint32_t  sclk)
inline

Sets the clock pin.

Parameters
sclkthe pin's Arduino number (PA5).

◆ setSSEL() [1/2]

void SPIClass::setSSEL ( uint32_t  ssel)
inline

Sets a hardware chip select, driven by the peripheral itself.

Parameters
sselthe pin's Arduino number (PA4, the panel's CS on this board).

◆ setMISO() [2/2]

void SPIClass::setMISO ( PinName  miso)
inline

Sets the MISO pin.

Parameters
misothe pin's name (PA_6).

◆ setMOSI() [2/2]

void SPIClass::setMOSI ( PinName  mosi)
inline

Sets the MOSI pin.

Parameters
mosithe pin's name (PA_7).

◆ setSCLK() [2/2]

void SPIClass::setSCLK ( PinName  sclk)
inline

Sets the clock pin.

Parameters
sclkthe pin's name (PA_5).

◆ setSSEL() [2/2]

void SPIClass::setSSEL ( PinName  ssel)
inline

Sets a hardware chip select.

Parameters
sselthe pin's name (PA_4).

◆ begin()

virtual void SPIClass::begin ( uint8_t  _pin = CS_PIN_CONTROLLED_BY_USER)
virtual

Starts the SPI with the pin's settings (the defaults the first time: 3 MHz, MSB first, SPI_MODE0).

Resets SPI1 and programs it afresh, and sets the SPI pins up.

Parameters
_pina CS pin for the class to drive (made an output and set high here), or CS_PIN_CONTROLLED_BY_USER (the default). Nothing happens if NB_SPI_SETTINGS other pins already have settings.

◆ end()

void SPIClass::end ( void  )

Stops the SPI: resets SPI1, switches its clock off and forgets every pin's settings.

◆ beginTransaction() [1/2]

virtual void SPIClass::beginTransaction ( uint8_t  pin,
SPISettings  settings 
)
virtual

Stores a device's settings for a CS pin and programs SPI1 with them.

Resets SPI1 and programs it afresh. Unlike on AVR, nothing is locked and interrupts are left alone.

Parameters
pina CS pin for the class to drive (made an output and set high here), or CS_PIN_CONTROLLED_BY_USER. Nothing happens if NB_SPI_SETTINGS other pins already have settings.
settingsthe device's clock, bit order and mode.

◆ beginTransaction() [2/2]

void SPIClass::beginTransaction ( SPISettings  settings)
inline

Programs SPI1 with a device's settings, for a device whose CS the sketch drives itself.

Parameters
settingsthe device's clock, bit order and mode.

◆ endTransaction() [1/2]

void SPIClass::endTransaction ( uint8_t  pin)

Forgets the pin's settings: transfers for it do nothing until the next begin() or beginTransaction() for it.

SPI1 is left as it is.

Parameters
pinthe CS pin given to beginTransaction().

◆ endTransaction() [2/2]

void SPIClass::endTransaction ( void  )
inline

Forgets the settings of the sketch-driven CS: transfer() does nothing until the next begin() or beginTransaction().

◆ transfer() [1/6]

virtual byte SPIClass::transfer ( uint8_t  pin,
uint8_t  _data,
SPITransferMode  _mode = SPI_LAST 
)
virtual

Sends one byte and returns the byte received meanwhile.

Parameters
pinthe device's CS pin.
_datathe byte to send.
_modeSPI_LAST (the default) releases CS afterwards, SPI_CONTINUE keeps it low for the next transfer.
Returns
the byte received; 0 if the pin has no settings or the settings are SPI_TRANSMITONLY.

◆ transfer16() [1/2]

virtual uint16_t SPIClass::transfer16 ( uint8_t  pin,
uint16_t  _data,
SPITransferMode  _mode = SPI_LAST 
)
virtual

Sends 16 bits as two bytes and returns the 16 bits received.

With MSBFIRST the high byte goes first, with LSBFIRST the low byte, so the bits go out in the chosen order.

Parameters
pinthe device's CS pin.
_datathe 16 bits to send.
_modeSPI_LAST (the default) or SPI_CONTINUE.
Returns
the 16 bits received; 0 if the pin has no settings or the settings are SPI_TRANSMITONLY.

◆ transfer() [2/6]

virtual void SPIClass::transfer ( uint8_t  pin,
void *  _buf,
size_t  _count,
SPITransferMode  _mode = SPI_LAST 
)
virtual

Sends a buffer, replacing its bytes with the bytes received.

Parameters
pinthe device's CS pin.
_bufthe bytes to send, overwritten with those received (left as sent with SPI_TRANSMITONLY).
_counthow many bytes (up to 65,535); 0 does nothing.
_modeSPI_LAST (the default) or SPI_CONTINUE.

◆ transfer() [3/6]

virtual void SPIClass::transfer ( byte  _pin,
void *  _bufout,
void *  _bufin,
size_t  _count,
SPITransferMode  _mode = SPI_LAST 
)
virtual

Sends one buffer and receives into another.

Parameters
_pinthe device's CS pin.
_bufoutthe bytes to send.
_bufinreceives the bytes received (untouched with SPI_TRANSMITONLY); may be _bufout.
_counthow many bytes (up to 65,535); 0 does nothing.
_modeSPI_LAST (the default) or SPI_CONTINUE.

◆ transfer() [4/6]

byte SPIClass::transfer ( uint8_t  _data,
SPITransferMode  _mode = SPI_LAST 
)
inline

Sends one byte and returns the byte received meanwhile (CS driven by the sketch).

Parameters
_datathe byte to send.
_modeignored without a CS pin.
Returns
the byte received; 0 before begin() or with SPI_TRANSMITONLY.

◆ transfer16() [2/2]

uint16_t SPIClass::transfer16 ( uint16_t  _data,
SPITransferMode  _mode = SPI_LAST 
)
inline

Sends 16 bits and returns the 16 bits received (CS driven by the sketch).

Parameters
_datathe 16 bits to send, in the chosen bit order.
_modeignored without a CS pin.
Returns
the 16 bits received; 0 before begin() or with SPI_TRANSMITONLY.

◆ transfer() [5/6]

void SPIClass::transfer ( void *  _buf,
size_t  _count,
SPITransferMode  _mode = SPI_LAST 
)
inline

Sends a buffer, replacing its bytes with the bytes received (CS driven by the sketch).

Parameters
_bufthe bytes to send, overwritten with those received.
_counthow many bytes (up to 65,535).
_modeignored without a CS pin.

◆ transfer() [6/6]

void SPIClass::transfer ( void *  _bufout,
void *  _bufin,
size_t  _count,
SPITransferMode  _mode = SPI_LAST 
)
inline

Sends one buffer and receives into another (CS driven by the sketch).

Parameters
_bufoutthe bytes to send.
_bufinreceives the bytes received.
_counthow many bytes (up to 65,535).
_modeignored without a CS pin.

◆ setBitOrder() [1/2]

void SPIClass::setBitOrder ( uint8_t  _pin,
BitOrder   
)

Sets the bit order for a CS pin.

Parameters
_pinthe CS pin.
Parameters
  • _order: MSBFIRST or LSBFIRST.

◆ setBitOrder() [2/2]

void SPIClass::setBitOrder ( BitOrder  _order)
inline

Sets the bit order.

Parameters
_orderMSBFIRST or LSBFIRST.

◆ setDataMode() [1/2]

void SPIClass::setDataMode ( uint8_t  _pin,
uint8_t   
)

Sets the mode for a CS pin.

Parameters
_pinthe CS pin.
Parameters
  • _mode: SPI_MODE0 ... SPI_MODE3; any other value is ignored.

◆ setDataMode() [2/2]

void SPIClass::setDataMode ( uint8_t  _mode)
inline

Sets the mode.

Parameters
_modeSPI_MODE0 ... SPI_MODE3.

◆ setClockDivider() [1/2]

void SPIClass::setClockDivider ( uint8_t  _pin,
uint8_t   
)

Sets the clock for a CS pin as a divider of 48 MHz.

Parameters
_pinthe CS pin.
Parameters
  • _div: SPI_CLOCK_DIV2 ... SPI_CLOCK_DIV128 (any 1-255 works; the result is rounded down to 48 MHz / 2^n); 0 means the default clock (3 MHz).

◆ setClockDivider() [2/2]

void SPIClass::setClockDivider ( uint8_t  _div)
inline

Sets the clock as a divider of 48 MHz.

Parameters
_divSPI_CLOCK_DIV2 ... SPI_CLOCK_DIV128; 0 means the default clock (3 MHz).

◆ usingInterrupt()

void SPIClass::usingInterrupt ( uint8_t  interruptNumber)

Does nothing (kept for compatibility): transfers are never guarded against interrupts.

Parameters
interruptNumberignored.

◆ attachInterrupt()

void SPIClass::attachInterrupt ( void  )

Does nothing (kept for compatibility).

◆ detachInterrupt()

void SPIClass::detachInterrupt ( void  )

Does nothing (kept for compatibility).

◆ getHandle()

SPI_HandleTypeDef * SPIClass::getHandle ( void  )
inline

The WCH peripheral library's view of this SPI: its instance and init structure, for mixing in WCH's own SPI functions.

Use at your own risk.

Returns
the handle, valid for the object's lifetime.

Member Data Documentation

◆ _spi

spi_t SPIClass::_spi
protected

the instance's pins, peripheral and WCH init structure


The documentation for this class was generated from the following file: