Midnight Sun Firmware
Loading...
Searching...
No Matches
SPI

SPI library. More...

Classes

struct  SpiSettings
 SPI settings struct. More...
 

Macros

#define SPI_TIMEOUT_MS   100U
 Maximum time permitted for a SPI transaction.
 
#define SPI_MAX_NUM_DATA   32U
 None of our SPI transactions should need to be longer than 32 bytes.
 
#define spi_tx(spi, tx_data, tx_len)   spi_exchange(spi, tx_data, tx_len, NULL, 0U)
 Transmit data from the SPI port. More...
 
#define spi_rx(spi, rx_data, rx_len)   spi_exchange(spi, NULL, 0U, rx_data, rx_len)
 Receive data from the SPI port. More...
 

Enumerations

enum  SpiPort { SPI_PORT_1 = 0 , SPI_PORT_2 , SPI_PORT_3 , NUM_SPI_PORTS }
 SPI Port selection. More...
 
enum  SpiMode {
  SPI_MODE_0 = 0 , SPI_MODE_1 , SPI_MODE_2 , SPI_MODE_3 ,
  NUM_SPI_MODES
}
 SPI Mode selection. More...
 

Functions

StatusCode spi_init (SpiPort spi, const SpiSettings *settings)
 Initialize SPI instance. More...
 
StatusCode spi_exchange (SpiPort spi, uint8_t *tx_data, size_t tx_len, uint8_t *rx_data, size_t rx_len)
 Performs a SPI data exchange. More...
 

Detailed Description

SPI library.

Macro Definition Documentation

◆ spi_rx

#define spi_rx (   spi,
  rx_data,
  rx_len 
)    spi_exchange(spi, NULL, 0U, rx_data, rx_len)

Receive data from the SPI port.

This method will only receive data, and will not send any data

Parameters
spiSpecifies which SPI port performs the receive
rx_dataPointer to a buffer to receive data
rx_lenLength of the data to receive

◆ spi_tx

#define spi_tx (   spi,
  tx_data,
  tx_len 
)    spi_exchange(spi, tx_data, tx_len, NULL, 0U)

Transmit data from the SPI port.

This method will only send data, and will discard the response bytes

Parameters
spiSpecifies which SPI port performs the transmit
tx_dataPointer to a buffer to transmit data
tx_lenLength of the data to transmit

Enumeration Type Documentation

◆ SpiMode

enum SpiMode

SPI Mode selection.

Enumerator
SPI_MODE_0 

CPOL: 0 CPHA: 0. Sampled on rising edge. Idle low

SPI_MODE_1 

CPOL: 0 CPHA: 1. Sampled on falling edge. Idle low

SPI_MODE_2 

CPOL: 1 CPHA: 0. Sampled on falling edge. Idle high

SPI_MODE_3 

CPOL: 1 CPHA: 1. Sampled on rising edge. Idle high

NUM_SPI_MODES 

Number of SPI Modes

◆ SpiPort

enum SpiPort

SPI Port selection.

Enumerator
SPI_PORT_1 

CS: PA4, SCLK: PA5, SDI: PA6, and SDO: PA7

SPI_PORT_2 

CS: PB12, SCLK: PB13, SDI: PB14, and SDO: PB15

SPI_PORT_3 

CS: PA15, SCLK: PB3, SDI: PB4, and SDO: PB5

NUM_SPI_PORTS 

Number of SPI Ports

Function Documentation

◆ spi_exchange()

StatusCode spi_exchange ( SpiPort  spi,
uint8_t *  tx_data,
size_t  tx_len,
uint8_t *  rx_data,
size_t  rx_len 
)

Performs a SPI data exchange.

Half-duplex communication where it transmits first, then receives. It will first pull the CS low, transmit the data, then receive data, and finally pull CS high.

Parameters
spiSpecifies which SPI port performs the exchange
tx_dataPointer to a buffer to transmit data
tx_lenLength of the data to transmit
rx_dataPointer to a buffer to receive data
rx_lenLength of the data to receive
Returns
STATUS_CODE_OK if data exchange succeeded STATUS_CODE_INVALID_ARGS if one of the parameters are incorrect STATUS_CODE_INTERNAL_ERROR if HAL transmission/receiving fails STATUS_CODE_TIMEOUT if transmission/receiving takes too long

◆ spi_init()

StatusCode spi_init ( SpiPort  spi,
const SpiSettings settings 
)

Initialize SPI instance.

Prescalers on STM32 must be a power of 2, so the actual baudrate may not be as exactly as requested.

Parameters
spiSpecifies which SPI port to initialize
settingsPointer to SPI initialization settings
Returns
STATUS_CODE_OK if initialization succeeded STATUS_CODE_INVALID_ARGS if one of the parameters are incorrect STATUS_CODE_INTERNAL_ERROR if HAL initialization fails STATUS_CODE_RESOURCE_EXHAUSTED if already initialized