|
px-fwlib 0.10.0
Cross-platform embedded library and documentation for 8/16/32-bit microcontrollers generated with Doxygen 1.9.2
|
Driver for the SPI peripheral to communicate with SPI slaves.
File(s):
The driver must be configured by supplying a project specific "px_spi_cfg.h". "px_spi_cfg_template.h" can be copied, renamed and modified to supply compile time options.
Data Structures | |
| struct | px_spi_handle_t |
| Define SPI handle. More... | |
Macros | |
| #define | PX_SPI_CFG_PER_COUNT (PX_SPI_CFG_SPI1_EN + PX_SPI_CFG_SPI2_EN ) |
| Number of enabled peripherals. More... | |
| #define | PX_SPI_CFG_SPI1_EN 1 |
| Enable/disable support for SPI1 peripheral. More... | |
| #define | PX_SPI_CFG_SPI2_EN 1 |
| Enable/disable support for SPI2 peripheral. More... | |
| #define | PX_SPI_CFG_DEFAULT_BAUD PX_SPI_BAUD_CLK_DIV_32 |
| Specify default baud rate. More... | |
| #define | PX_SPI_CFG_DEFAULT_MODE PX_SPI_MODE0 |
| Specify default SPI mode (clock phase and polarity) More... | |
| #define | PX_SPI_CFG_DEFAULT_DATA_ORDER PX_SPI_DATA_ORDER_MSB |
| Specify default SPI data order. More... | |
| #define | PX_SPI_CFG_CS_LO(cs_id) px_board_spi_cs_lo(cs_id) |
| Map PX_SPI_CFG_CS_LO() macro to px_board_spi_cs_lo() function. More... | |
| #define | PX_SPI_CFG_CS_HI(cs_id) px_board_spi_cs_hi(cs_id) |
| Map PX_SPI_CFG_CS_HI() macro to px_board_spi_cs_hi() function. More... | |
Enumerations | |
| enum | px_spi_nr_t |
| Specify SPI peripheral number. More... | |
| enum | px_spi_mode_t { PX_SPI_MODE0 = 0 , PX_SPI_MODE1 = 1 , PX_SPI_MODE2 = 2 , PX_SPI_MODE3 = 3 , PX_SPI_MODE0 = 0 , PX_SPI_MODE1 = 1 , PX_SPI_MODE2 = 2 , PX_SPI_MODE3 = 3 } |
| Specify SPI Clock polarity / Clock phase. More... | |
| enum | px_spi_dord_t { PX_SPI_DATA_ORDER_MSB = 0 , PX_SPI_DATA_ORDER_LSB = 1 , PX_SPI_DATA_ORDER_MSB = 0 , PX_SPI_DATA_ORDER_LSB = 1 } |
| Specify SPI Data order. More... | |
| enum | px_spi_baud_t { PX_SPI_BAUD_CLK_DIV_2 = 0 , PX_SPI_BAUD_CLK_DIV_4 , PX_SPI_BAUD_CLK_DIV_8 , PX_SPI_BAUD_CLK_DIV_16 , PX_SPI_BAUD_CLK_DIV_32 , PX_SPI_BAUD_CLK_DIV_64 , PX_SPI_BAUD_CLK_DIV_128 , PX_SPI_BAUD_CLK_DIV_256 , PX_SPI_BAUD_CLK_DIV_2 = 0 , PX_SPI_BAUD_CLK_DIV_4 , PX_SPI_BAUD_CLK_DIV_8 , PX_SPI_BAUD_CLK_DIV_16 , PX_SPI_BAUD_CLK_DIV_32 , PX_SPI_BAUD_CLK_DIV_64 , PX_SPI_BAUD_CLK_DIV_128 } |
| Specify SPI baud rate as a ratio of the peripheral clock. More... | |
Functions | |
| void | px_spi_init (void) |
| Initialise SPI driver. More... | |
| bool | px_spi_open (px_spi_handle_t *handle, px_spi_nr_t spi_nr, uint8_t cs_id) |
| Open SPI peripheral using predefined (default) parameters. More... | |
| bool | px_spi_open2 (px_spi_handle_t *handle, px_spi_nr_t spi_nr, uint8_t cs_id, px_spi_baud_t baud, px_spi_mode_t mode, px_spi_dord_t data_order, uint8_t mo_dummy_byte) |
| Open SPI peripheral using specified parameters. More... | |
| bool | px_spi_close (px_spi_handle_t *handle) |
| Close specified device. More... | |
| void | px_spi_wr (px_spi_handle_t *handle, const void *data, size_t nr_of_bytes, uint8_t flags) |
| Perform an SPI write transaction with an SPI slave. More... | |
| void | px_spi_rd (px_spi_handle_t *handle, void *data, size_t nr_of_bytes, uint8_t flags) |
| Perform an SPI read transaction with an SPI slave. More... | |
| void | px_spi_xc (px_spi_handle_t *handle, const void *data_wr, void *data_rd, size_t nr_of_bytes, uint8_t flags) |
| Perform an SPI exchange (write and read) transaction with an SPI slave. More... | |
| void | px_spi_change_baud (px_spi_handle_t *handle, px_spi_baud_t baud) |
| Change SPI peripheral baud. More... | |
| px_spi_baud_t | px_spi_util_baud_hz_to_clk_div (uint32_t baud_hz) |
| Calculate clock divisor that will yield closest frequency equal to or less than specified baud rate (in Hz). More... | |
| uint32_t | px_spi_util_clk_div_to_baud_hz (px_spi_baud_t baud) |
| Calculate the actual BAUD (in Hz) from the specified division. More... | |
SPI bit flags to demarcate the start and end of a transaction | |
| #define | PX_SPI_FLAG_START (1 << 0) |
| Begin SPI transaction (take SPI slave's Chip Select line low) More... | |
| #define | PX_SPI_FLAG_STOP (1 << 1) |
| Finish SPI transaction (take SPI slave's Chip Select line high) More... | |
| #define | PX_SPI_FLAG_START_AND_STOP (PX_SPI_FLAG_START + PX_SPI_FLAG_STOP) |
| Begin and finish SPI transaction. More... | |
| struct px_spi_handle_t |
| #define PX_SPI_CFG_PER_COUNT (PX_SPI_CFG_SPI1_EN + PX_SPI_CFG_SPI2_EN ) |
| #define PX_SPI_FLAG_START (1 << 0) |
| #define PX_SPI_FLAG_STOP (1 << 1) |
| #define PX_SPI_FLAG_START_AND_STOP (PX_SPI_FLAG_START + PX_SPI_FLAG_STOP) |
| #define PX_SPI_CFG_SPI1_EN 1 |
Enable/disable support for SPI1 peripheral.
Definition at line 35 of file px_spi_cfg_template.h.
| #define PX_SPI_CFG_SPI2_EN 1 |
Enable/disable support for SPI2 peripheral.
Definition at line 38 of file px_spi_cfg_template.h.
| #define PX_SPI_CFG_DEFAULT_BAUD PX_SPI_BAUD_CLK_DIV_32 |
Specify default baud rate.
Definition at line 41 of file px_spi_cfg_template.h.
| #define PX_SPI_CFG_DEFAULT_MODE PX_SPI_MODE0 |
Specify default SPI mode (clock phase and polarity)
Definition at line 44 of file px_spi_cfg_template.h.
| #define PX_SPI_CFG_DEFAULT_DATA_ORDER PX_SPI_DATA_ORDER_MSB |
Specify default SPI data order.
Definition at line 47 of file px_spi_cfg_template.h.
| #define PX_SPI_CFG_CS_LO | ( | cs_id | ) | px_board_spi_cs_lo(cs_id) |
Map PX_SPI_CFG_CS_LO() macro to px_board_spi_cs_lo() function.
A manual Chip Select function must be implemented, e.g.
void px_board_spi_cs_lo(uint8_t cs_id)
{
switch(cs_id)
{
case BOARD_SPI_CS:
PX_GPIO_PIN_SET_LO(PX_GPIO_SPI_CS);
break;
default:
break;
}
}
Definition at line 66 of file px_spi_cfg_template.h.
| #define PX_SPI_CFG_CS_HI | ( | cs_id | ) | px_board_spi_cs_hi(cs_id) |
Map PX_SPI_CFG_CS_HI() macro to px_board_spi_cs_hi() function.
A manual Chip Select function must be implemented, e.g.
void px_board_spi_cs_hi(uint8_t cs_id)
{
switch(cs_id)
{
case BOARD_SPI_CS:
PX_GPIO_OUT_SET_HI(PX_GPIO_SPI_CS);
break;
default:
break;
}
}
Definition at line 86 of file px_spi_cfg_template.h.
| enum px_spi_nr_t |
| enum px_spi_mode_t |
Specify SPI Clock polarity / Clock phase.
| enum px_spi_dord_t |
Specify SPI Data order.
| enum px_spi_baud_t |
Specify SPI baud rate as a ratio of the peripheral clock.
| bool px_spi_open | ( | px_spi_handle_t * | handle, |
| px_spi_nr_t | spi_nr, | ||
| uint8_t | cs_id | ||
| ) |
Open SPI peripheral using predefined (default) parameters.
| handle | Pointer to handle data structure |
| spi_nr | SPI peripheral number |
| cs_id | ID to use when calling PX_SPI_CS_xx() macro |
| false | Error: peripheral was not opened |
| true | Success: peripheral was opened |
| bool px_spi_open2 | ( | px_spi_handle_t * | handle, |
| px_spi_nr_t | spi_nr, | ||
| uint8_t | cs_id, | ||
| px_spi_baud_t | baud, | ||
| px_spi_mode_t | mode, | ||
| px_spi_dord_t | data_order, | ||
| uint8_t | mo_dummy_byte | ||
| ) |
Open SPI peripheral using specified parameters.
| handle | Pointer to handle data structure |
| spi_nr | SPI peripheral number |
| cs_id | ID to use when calling PX_SPI_CS() macro |
| baud | Desired BAUD rate as a division of peripheral clock |
| mode | Clock mode 0,1,2 or 3 |
| data_order | Data order (MSB first of LSB first) |
| mo_dummy_byte | Master Out dummy byte used when reading data from SPI slave |
| false | Error: peripheral was not opened |
| true | Success: peripheral was opened |
| bool px_spi_close | ( | px_spi_handle_t * | handle | ) |
| void px_spi_wr | ( | px_spi_handle_t * | handle, |
| const void * | data, | ||
| size_t | nr_of_bytes, | ||
| uint8_t | flags | ||
| ) |
Perform an SPI write transaction with an SPI slave.
With the use of flags this function allows concatenation of one or more writes / reads / exchanges with an SPI slave to form one big transaction.
If the PX_SPI_FLAG_START flag is specified, the SPI slave is first selected by taking it's Chip Select line low.
The specified number of bytes is then written to the SPI slave.
Finally, if the PX_SPI_FLAG_STOP flag is specified, the SPI slave's Chip Select line is taken high to complete the transaction.
| handle | Pointer to handle data structure |
| data | Buffer containing data to write |
| nr_of_bytes | Number of bytes to write to the slave |
| flags | Bit combination of PX_SPI_FLAG_START and PX_SPI_FLAG_STOP or nothing (0) |
| void px_spi_rd | ( | px_spi_handle_t * | handle, |
| void * | data, | ||
| size_t | nr_of_bytes, | ||
| uint8_t | flags | ||
| ) |
Perform an SPI read transaction with an SPI slave.
With the use of flags this function allows concatenation of one or more writes / reads / exchanges with an SPI slave to form one big transaction.
If the PX_SPI_FLAG_START flag is specified, the SPI slave is first selected by taking it's Chip Select line low.
The specified number of bytes is then read from the SPI slave.
Finally, if the PX_SPI_FLAG_STOP flag is specified, the SPI slave's Chip Select line is taken high to complete the transaction.
| handle | Pointer to handle data structure |
| data | Pointer to a buffer where the received data must be stored |
| nr_of_bytes | Number of bytes to read from slave |
| flags | Bit combination of PX_SPI_FLAG_START and PX_SPI_FLAG_STOP or nothing (0) |
| void px_spi_xc | ( | px_spi_handle_t * | handle, |
| const void * | data_wr, | ||
| void * | data_rd, | ||
| size_t | nr_of_bytes, | ||
| uint8_t | flags | ||
| ) |
Perform an SPI exchange (write and read) transaction with an SPI slave.
With the use of flags this function allows concatenation of one or more writes / reads / exchanges with an SPI slave to form one big transaction.
If the PX_SPI_FLAG_START flag is specified, the SPI slave is first selected by taking it's Chip Select line low.
The specified number of bytes is then written to the slave, while simultaneously reading the same number of bytes from the slave.
The write buffer and read buffer must both be at least "nr_of_bytes" bytes in size. It is acceptable to have the write buffer and read buffer point to the same location in which case the data to write will be overwritten by the received data.
Finally, if the PX_SPI_FLAG_STOP flag is specified, the SPI slave's Chip Select line is taken high to complete the transaction.
| handle | Pointer to handle data structure |
| data_wr | Pointer to a buffer containing data to be written |
| data_rd | Pointer to a buffer where the read data must be stored |
| nr_of_bytes | Number of bytes to exchange |
| flags | Bit combination of PX_SPI_FLAG_START and PX_SPI_FLAG_STOP or nothing (0) |
| void px_spi_change_baud | ( | px_spi_handle_t * | handle, |
| px_spi_baud_t | baud | ||
| ) |
| px_spi_baud_t px_spi_util_baud_hz_to_clk_div | ( | uint32_t | baud_hz | ) |
| uint32_t px_spi_util_clk_div_to_baud_hz | ( | px_spi_baud_t | baud | ) |