FastArduino  v1.7
C++ library to build fast but small Arduino/AVR projects
spi::SPIDevice< CS, CS_MODE, RATE, MODE, ORDER > Class Template Reference

Base class for any SPI slave device. More...

#include <fastarduino/spi.h>

Inheritance diagram for spi::SPIDevice< CS, CS_MODE, RATE, MODE, ORDER >:
Collaboration diagram for spi::SPIDevice< CS, CS_MODE, RATE, MODE, ORDER >:

Protected Member Functions

 SPIDevice () INLINE=default
 Create a new SPIDevice; this sets up the CS pin for later use during transfers.
 
void start_transfer ()
 Start an SPI transfer to this device. More...
 
void end_transfer () INLINE
 End the current SPI ransfer tot hsi device. More...
 
- Protected Member Functions inherited from spi::AbstractSPIDevice
 AbstractSPIDevice (const AbstractSPIDevice &)=delete
 
AbstractSPIDeviceoperator= (const AbstractSPIDevice &)=delete
 
uint8_t transfer (uint8_t data)
 Transfer one byte to the currently selected SPI slave device through MOSI pin, and get the byte returned by the device through MISO pin. More...
 
void transfer (uint8_t *data, uint16_t size)
 Transfer an array of payload data to the currently selected SPI slave device through MOSI pin, and get all data bytes simultaneously received from that device through MISO pin. More...
 
void transfer (const uint8_t *data, uint16_t size)
 Transfer an array of payload data to the currently selected SPI slave device through MOSI pin; any data bytes simultaneously received from that device through MISO pin are lost. More...
 
void transfer (uint8_t *data, uint16_t size, uint8_t sent)
 Transfer the provided byte sent several times to the currently selected SPI slave device through MOSI pin, and get all data bytes simultaneously received from that device through MISO pin. More...
 
void transfer (uint16_t size, uint8_t sent)
 Transfer the provided byte sent several times to the currently selected SPI slave device through MOSI pin; any data bytes simultaneously received from that device through MISO pin are lost. More...
 

Detailed Description

template<board::DigitalPin CS, ChipSelect CS_MODE = ChipSelect::ACTIVE_LOW, ClockRate RATE = ClockRate::CLOCK_DIV_4, Mode MODE = Mode::MODE_0, DataOrder ORDER = DataOrder::MSB_FIRST>
class spi::SPIDevice< CS, CS_MODE, RATE, MODE, ORDER >

Base class for any SPI slave device.

It contains the barebones API for implementing a real device.

Implementing a new SPI device consists mainly in subclassing SPIDevice and add public device-specific methods that will use start_transfer(), transfer() and end_transfer() to communicate with the actual device.

The snippet below illustrates this and summarizes the definition of an SPI based device, the Winbond W25Q80BV flash-memory chip:

template<board::DigitalPin CS>
class WinBond : public spi::SPIDevice<CS, spi::ChipSelect::ACTIVE_LOW, spi::ClockRate::CLOCK_DIV_2>
{
public:
void write_page(uint32_t address, uint8_t* data, uint8_t size)
{
send(0x02, address, data, (size == 0 ? 256 : size));
}
uint8_t read_data(uint32_t address);
void read_data(uint32_t address, uint8_t* data, uint16_t size);
...
private:
void send(uint8_t code, uint32_t address, uint8_t* data, uint16_t size)
{
this->start_transfer();
this->transfer(code);
this->transfer(address >> 16);
this->transfer(address >> 8);
this->transfer(address);
this->transfer(data, size);
this->end_transfer();
}
...
};
Template Parameters
CSthe pin used to select the slave device
CS_MODEthe chip select active mode
RATEthe SPI clock rate for this device
MODEthe SPI mode used for this device
ORDERthe bit order for this device

Definition at line 327 of file spi.h.

Member Function Documentation

◆ start_transfer()

template<board::DigitalPin CS, ChipSelect CS_MODE = ChipSelect::ACTIVE_LOW, ClockRate RATE = ClockRate::CLOCK_DIV_4, Mode MODE = Mode::MODE_0, DataOrder ORDER = DataOrder::MSB_FIRST>
void spi::SPIDevice< CS, CS_MODE, RATE, MODE, ORDER >::start_transfer ( )
inlineprotected

Start an SPI transfer to this device.

Concretely this sets the active level on CS pin.

Definition at line 341 of file spi.h.

◆ end_transfer()

template<board::DigitalPin CS, ChipSelect CS_MODE = ChipSelect::ACTIVE_LOW, ClockRate RATE = ClockRate::CLOCK_DIV_4, Mode MODE = Mode::MODE_0, DataOrder ORDER = DataOrder::MSB_FIRST>
void spi::SPIDevice< CS, CS_MODE, RATE, MODE, ORDER >::end_transfer ( )
inlineprotected

End the current SPI ransfer tot hsi device.

Concretely this clears the active level on CS pin.

Definition at line 359 of file spi.h.


The documentation for this class was generated from the following file:
spi::SPIDevice::start_transfer
void start_transfer()
Start an SPI transfer to this device.
Definition: spi.h:341
spi::SPIDevice::end_transfer
void end_transfer() INLINE
End the current SPI ransfer tot hsi device.
Definition: spi.h:359
spi::SPIDevice
Base class for any SPI slave device.
Definition: spi.h:328
spi::AbstractSPIDevice::transfer
uint8_t transfer(uint8_t data)
Transfer one byte to the currently selected SPI slave device through MOSI pin, and get the byte retur...
Definition: spi.h:169