Serial Peripheral Interface (SPI) interface.
More...
|
hw_spi_t | hw_spi_init_default (bool cs_active_low, uint32_t baudrate) |
| Initialize a SPI interface using default pins and adapter. More...
|
|
hw_spi_t | hw_spi_init (uint8_t adapter, uint8_t sck, uint8_t tx, uint8_t rx, uint8_t cs, bool cs_active_low, uint32_t baudrate) |
| Initialize a SPI interface with specific adapter and pins. More...
|
|
void | hw_spi_finalize (hw_spi_t *spi) |
| Finalize and release a SPI interface. More...
|
|
uint8_t | hw_spi_count (void) |
| Get the total number of available SPI adapters. More...
|
|
static bool | hw_spi_valid (hw_spi_t *spi) |
| Get true if the SPI interface is valid. More...
|
|
size_t | hw_spi_xfr (hw_spi_t *spi, void *data, size_t tx, size_t rx) |
| Perform a SPI transfer operation (read, write, or combined). More...
|
|
size_t | hw_spi_read (hw_spi_t *spi, uint8_t reg, void *data, size_t len) |
| Read data from a specific register of a SPI device. More...
|
|
size_t | hw_spi_write (hw_spi_t *spi, uint8_t reg, const void *data, size_t len) |
| Write data to a specific register of a SPI device. More...
|
|
Serial Peripheral Interface (SPI) interface.
This module provides functions to initialize SPI peripherals, and bi-directional data transfer with SPI devices in master mode.
◆ hw_spi_count()
uint8_t hw_spi_count |
( |
void |
| ) |
|
Get the total number of available SPI adapters.
- Returns
- The number of SPI adapters available on the hardware platform.
Returns the number of logical SPI adapters that can be used in the system. These are usually numbered from 0 to hw_spi_count() - 1. If zero is returned, it indicates that SPI functionality is not available.
◆ hw_spi_finalize()
Finalize and release a SPI interface.
- Parameters
-
spi | Pointer to the SPI structure to finalize. |
This function releases the SPI interface and frees any associated resources. After calling this function, the SPI interface should not be used for further operations.
◆ hw_spi_init()
hw_spi_t hw_spi_init |
( |
uint8_t |
adapter, |
|
|
uint8_t |
sck, |
|
|
uint8_t |
tx, |
|
|
uint8_t |
rx, |
|
|
uint8_t |
cs, |
|
|
bool |
cs_active_low, |
|
|
uint32_t |
baudrate |
|
) |
| |
Initialize a SPI interface with specific adapter and pins.
- Parameters
-
adapter | The SPI adapter number to use (0 to hw_spi_count()-1). |
sck | The GPIO pin number to use for SPI Clock (SCK). |
tx | The GPIO pin number to use for SPI Master Out Slave In (MOSI). |
rx | The GPIO pin number to use for SPI Master In Slave Out (MISO). |
cs | The GPIO pin number to use for SPI Chip Select (CS). |
cs_active_low | True if the Chip Select (CS) pin is active low, false otherwise. |
baudrate | The desired SPI baud rate (e.g., 100000 for 100kHz). |
- Returns
- A SPI structure representing the initialized interface.
This function initializes a SPI interface using the specified adapter and GPIO pins for MOSI, MISO, and SCK lines. The adapter number should be valid and the specified pins should be capable of SPI functionality.
If the CS value is set to 0, it will not be used for reading or writing.
- Note
- The specified pins must support SPI functionality.
-
Pin assignments are platform-dependent.
◆ hw_spi_init_default()
hw_spi_t hw_spi_init_default |
( |
bool |
cs_active_low, |
|
|
uint32_t |
baudrate |
|
) |
| |
Initialize a SPI interface using default pins and adapter.
- Parameters
-
cs_active_low | True if the Chip Select (CS) pin is active low, false otherwise. |
baudrate | The desired SPI baud rate (e.g., 100000 for 100kHz). |
- Returns
- A SPI structure representing the initialized interface.
This function initializes the default SPI interface using platform-specific default pins and adapter settings. This is the simplest way to get SPI functionality without needing to specify pin assignments.
◆ hw_spi_read()
size_t hw_spi_read |
( |
hw_spi_t * |
spi, |
|
|
uint8_t |
reg, |
|
|
void * |
data, |
|
|
size_t |
len |
|
) |
| |
Read data from a specific register of a SPI device.
- Parameters
-
spi | Pointer to the SPI structure. |
reg | Register address to read from. |
data | Pointer to the buffer where read data will be stored. |
len | Number of bytes to read. |
- Returns
- Number of bytes successfully read, or 0 on failure.
This function writes the register address and then reads the specified number of bytes with automatic CS control.
◆ hw_spi_valid()
static bool hw_spi_valid |
( |
hw_spi_t * |
spi | ) |
|
|
inlinestatic |
Get true if the SPI interface is valid.
- Returns
- True if the SPI interface is valid, false otherwise.
The result of hw_spi_init can return an empty SPI structure if the initialization fails. This function checks if the SPI interface is valid.
Definition at line 111 of file spi.h.
uint32_t baudrate
SPI baud rate in Hz.
◆ hw_spi_write()
size_t hw_spi_write |
( |
hw_spi_t * |
spi, |
|
|
uint8_t |
reg, |
|
|
const void * |
data, |
|
|
size_t |
len |
|
) |
| |
Write data to a specific register of a SPI device.
- Parameters
-
spi | Pointer to the SPI structure. |
reg | Register address to write to. |
data | Pointer to the data to write (can be NULL if len is 0). |
len | Number of bytes to write. |
- Returns
- Number of bytes successfully written, or 0 on failure.
This function writes the register address followed by the data with automatic CS control.
◆ hw_spi_xfr()
size_t hw_spi_xfr |
( |
hw_spi_t * |
spi, |
|
|
void * |
data, |
|
|
size_t |
tx, |
|
|
size_t |
rx |
|
) |
| |
Perform a SPI transfer operation (read, write, or combined).
- Parameters
-
spi | Pointer to the SPI structure. |
data | Pointer to the data buffer for read/write operations. |
tx | Number of bytes to transmit from the data buffer. |
rx | Number of bytes to receive into the data buffer (after tx bytes). |
- Returns
- Number of bytes successfully transferred, or 0 on failure.
This function performs a generic SPI transfer with automatic CS control. For combined operations, transmit data is sent first, then receive data is read into the buffer starting at offset tx.
- Note
- The CS pin is automatically asserted before and deasserted after the transfer.
-
For combined transfers, ensure the data buffer is large enough for tx + rx bytes.