FastArduino  v1.7
C++ library to build fast but small Arduino/AVR projects
spi Namespace Reference

Define API to define and manage SPI devices. More...

Classes

class  AbstractSPIDevice
 Contain general payload transfer API for an SPI device. More...
 
class  SPIDevice
 Base class for any SPI slave device. More...
 

Enumerations

enum  ClockRate : uint8_t {
  CLOCK_DIV_4 = 0x00,
  CLOCK_DIV_16 = 0x01,
  CLOCK_DIV_64 = 0x02,
  CLOCK_DIV_128 = 0x03,
  CLOCK_DIV_2 = 0x10,
  CLOCK_DIV_8 = 0x11,
  CLOCK_DIV_32 = 0x12
}
 Define SPI clock rate as a divider of MCU clock frequency. More...
 
enum  DataOrder : uint8_t {
  DataOrder::MSB_FIRST = 0,
  DataOrder::LSB_FIRST = bits::BV8(DORD)
}
 Bit ordering per byte. More...
 
enum  Mode : uint8_t {
  Mode::MODE_0 = 0,
  Mode::MODE_1 = bits::BV8(CPHA),
  Mode::MODE_2 = bits::BV8(CPOL),
  Mode::MODE_3 = bits::BV8(CPHA, CPOL)
}
 SPI transmission mode. More...
 
enum  ChipSelect : uint8_t {
  ChipSelect::ACTIVE_LOW = 0,
  ChipSelect::ACTIVE_HIGH = 1
}
 Active polarity of slave selection pin. More...
 

Functions

void init ()
 This function must be called once in your program, before any use of an SPI device. More...
 
constexpr ClockRate compute_clockrate (uint32_t frequency)
 Calculate ClockRate for the given frequency. More...
 

Detailed Description

Define API to define and manage SPI devices.

SPI is available to all MCU supported by FastArduino, even in ATtiny MCU, for which SPI is implemented with Universal Serial Interface (USI).

Note
USI does not allow full SPI support, contrarily to native SPI support of ATmega MCU. When differences exist, they are documented in the API.

Enumeration Type Documentation

◆ ClockRate

enum spi::ClockRate : uint8_t
strong

Define SPI clock rate as a divider of MCU clock frequency.

Note
this is not used in ATtiny.
See also
compute_clockrate()

Definition at line 47 of file spi.h.

◆ DataOrder

enum spi::DataOrder : uint8_t
strong

Bit ordering per byte.

Enumerator
MSB_FIRST 

Most significant bit transferred first.

LSB_FIRST 

Least significant bit transferred first.

Note
this is not available on ATtiny MCU.

Definition at line 87 of file spi.h.

◆ Mode

enum spi::Mode : uint8_t
strong

SPI transmission mode.

See also
https://en.wikipedia.org/wiki/Serial_Peripheral_Interface_Bus#Mode_numbers
Enumerator
MODE_0 

SPI mode 0: CPOL = 0 and CPHA = 0.

MODE_1 

SPI mode 1: CPOL = 0 and CPHA = 1.

MODE_2 

SPI mode 2: CPOL = 1 and CPHA = 0.

Note
not available on ATtiny MCU.
MODE_3 

SPI mode 3: CPOL = 1 and CPHA = 1.

Note
not available on ATtiny MCU.

Definition at line 109 of file spi.h.

◆ ChipSelect

enum spi::ChipSelect : uint8_t
strong

Active polarity of slave selection pin.

Enumerator
ACTIVE_LOW 

Slave device is active when SS pin is low.

ACTIVE_HIGH 

Slave device is active when SS pin is high.

Definition at line 137 of file spi.h.

Function Documentation

◆ init()

void spi::init ( )

This function must be called once in your program, before any use of an SPI device.

It simply sets up the pins used by SPI interface: MOSI, MISO, SCK.

Definition at line 20 of file spi.cpp.

◆ compute_clockrate()

constexpr ClockRate spi::compute_clockrate ( uint32_t  frequency)
constexpr

Calculate ClockRate for the given frequency.

Computations done by this method will be performed at compile-time as long as all provided arguments are constants; this is important as this will help optimize code size and execution time.

See also
ClockRate

Definition at line 65 of file spi.h.