24#include "boards/board.h"
67 if (frequency >= (F_CPU / 2))
68 return ClockRate::CLOCK_DIV_2;
69 else if (frequency >= (F_CPU / 4))
70 return ClockRate::CLOCK_DIV_4;
71 else if (frequency >= (F_CPU / 8))
72 return ClockRate::CLOCK_DIV_8;
73 else if (frequency >= (F_CPU / 16))
74 return ClockRate::CLOCK_DIV_16;
75 else if (frequency >= (F_CPU / 32))
76 return ClockRate::CLOCK_DIV_32;
77 else if (frequency >= (F_CPU / 64))
78 return ClockRate::CLOCK_DIV_64;
80 return ClockRate::CLOCK_DIV_128;
127 enum class Mode : uint8_t
172 SPSR_.loop_until_bit_set(SPIF);
209 uint8_t value = *data;
248 void transfer(uint8_t* data, uint16_t size, uint8_t sent)
250 while (size--) *data++ =
transfer(sent);
271 using REG8 = board_traits::REG8;
273 static constexpr const REG8 SPDR_{SPDR};
274 static constexpr const REG8 SPSR_{SPSR};
276 static constexpr const REG8 USIDR_{USIDR};
277 static constexpr const REG8 USISR_{USISR};
278 static constexpr const REG8 USICR_{USICR};
355 USICR_ = USICR_START_;
368 using REG8 = board_traits::REG8;
370 static constexpr const REG8 SPCR_{SPCR};
371 static constexpr const REG8 SPDR_{SPDR};
372 static constexpr const REG8 SPSR_{SPSR};
374 static const constexpr uint8_t SPCR_START_ =
375 bits::BV8(SPE, MSTR) | (uint8_t(RATE) & 0x03U) | uint8_t(ORDER) | uint8_t(MODE);
376 static const constexpr uint8_t SPSR_START_ = (uint8_t(RATE) & 0x10U) ?
bits::BV8(SPI2X) : 0;
378 static constexpr const REG8 USIDR_{USIDR};
379 static constexpr const REG8 USISR_{USISR};
380 static constexpr const REG8 USICR_{USICR};
381 static const constexpr uint8_t USICR_START_ = uint8_t(MODE);
Contain general payload transfer API for an SPI device.
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 da...
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...
uint8_t transfer(uint8_t data)
Transfer one byte to the currently selected SPI slave device through MOSI pin, and get the byte retur...
void transfer(uint8_t *data, uint16_t size)
Transfer an array of payload data to the currently selected SPI slave device through MOSI pin,...
void transfer(uint16_t size, uint8_t sent)
Transfer the provided byte sent several times to the currently selected SPI slave device through MOSI...
Base class for any SPI slave device.
void start_transfer()
Start an SPI transfer to this device.
SPIDevice() INLINE=default
Create a new SPIDevice; this sets up the CS pin for later use during transfers.
void end_transfer() INLINE
End the current SPI ransfer tot hsi device.
#define INLINE
Specific GCC attribute to force the compiler to always inline code of a given function.
General Purpose (digital) Input Output API.
static constexpr uint8_t BV8(uint8_t bit)
Create a uint8_t bitmask for the given bit number.
DigitalPin
Defines all available digital input/output pins of the target MCU.
typename FastPinType< DPIN_ >::TYPE FAST_PIN
Useful alias type to the FastPin type matching a given board::DigitalPin.
@ OUTPUT
Digital pin is configured as output.
Define API to define and manage SPI devices.
ClockRate
Define SPI clock rate as a divider of MCU clock frequency.
ChipSelect
Active polarity of slave selection pin.
@ ACTIVE_HIGH
Slave device is active when SS pin is high.
@ ACTIVE_LOW
Slave device is active when SS pin is low.
constexpr ClockRate compute_clockrate(uint32_t frequency)
Calculate ClockRate for the given frequency.
DataOrder
Bit ordering per byte.
@ MSB_FIRST
Most significant bit transferred first.
@ LSB_FIRST
Least significant bit transferred first.
void init()
This function must be called once in your program, before any use of an SPI device.
Mode
SPI transmission mode.
@ MODE_0
SPI mode 0: CPOL = 0 and CPHA = 0.
@ MODE_3
SPI mode 3: CPOL = 1 and CPHA = 1.
@ MODE_2
SPI mode 2: CPOL = 1 and CPHA = 0.
@ MODE_1
SPI mode 1: CPOL = 0 and CPHA = 1.