ChibiOS/RT Architecture - Reference Manual - Guides |
Generic SPI Driver. More...
Generic SPI Driver.
This module implements a generic SPI driver. The driver implements a state machine internally:
The driver is not thread safe for performance reasons, if you need to access the SPI bus from multiple thread then use the spiAcquireBus()
and spiReleaseBus()
APIs in order to gain exclusive access.
Modules | |
SPI Low Level Driver | |
SPI Driver low level driver template. | |
Defines | |
#define | SPI_USE_MUTUAL_EXCLUSION TRUE |
Enables the mutual exclusion APIs on the SPI bus. | |
Enumerations | |
enum | spistate_t { SPI_UNINIT = 0, SPI_STOP = 1, SPI_READY = 2, SPI_ACTIVE = 3 } |
Driver state machine possible states. More... | |
Functions | |
void | spiInit (void) |
SPI Driver initialization. | |
void | spiObjectInit (SPIDriver *spip) |
Initializes the standard part of a SPIDriver structure. | |
void | spiStart (SPIDriver *spip, const SPIConfig *config) |
Configures and activates the SPI peripheral. | |
void | spiStop (SPIDriver *spip) |
Deactivates the SPI peripheral. | |
void | spiSelect (SPIDriver *spip) |
Asserts the slave select signal and prepares for transfers. | |
void | spiUnselect (SPIDriver *spip) |
Deasserts the slave select signal. | |
void | spiIgnore (SPIDriver *spip, size_t n) |
Ignores data on the SPI bus. | |
void | spiExchange (SPIDriver *spip, size_t n, const void *txbuf, void *rxbuf) |
Exchanges data on the SPI bus. | |
void | spiSend (SPIDriver *spip, size_t n, const void *txbuf) |
Sends data ever the SPI bus. | |
void | spiReceive (SPIDriver *spip, size_t n, void *rxbuf) |
Receives data from the SPI bus. | |
void | spiAcquireBus (SPIDriver *spip) |
Gains exclusive access to the SPI bus. | |
void | spiReleaseBus (SPIDriver *spip) |
Releases exclusive access to the SPI bus. |
#define SPI_USE_MUTUAL_EXCLUSION TRUE |
enum spistate_t |
void spiInit | ( | void | ) |
SPI Driver initialization.
Definition at line 59 of file spi.c.
References spi_lld_init().
Referenced by halInit().
void spiObjectInit | ( | SPIDriver * | spip | ) |
Initializes the standard part of a SPIDriver
structure.
[in] | spip | pointer to the SPIDriver object |
Definition at line 69 of file spi.c.
References chMtxInit(), chSemInit(), SPIDriver::spd_config, SPIDriver::spd_mutex, SPIDriver::spd_state, and SPI_STOP.
Configures and activates the SPI peripheral.
Definition at line 86 of file spi.c.
References chDbgAssert, chDbgCheck, chSysLock, chSysUnlock, SPIDriver::spd_config, SPIDriver::spd_state, spi_lld_start(), SPI_READY, and SPI_STOP.
Referenced by mmcConnect(), mmcStartSequentialRead(), and mmcStartSequentialWrite().
void spiStop | ( | SPIDriver * | spip | ) |
Deactivates the SPI peripheral.
[in] | spip | pointer to the SPIDriver object |
Definition at line 105 of file spi.c.
References chDbgAssert, chDbgCheck, chSysLock, chSysUnlock, SPIDriver::spd_state, spi_lld_stop(), SPI_READY, and SPI_STOP.
Referenced by mmcDisconnect(), and mmcStop().
void spiSelect | ( | SPIDriver * | spip | ) |
Asserts the slave select signal and prepares for transfers.
[in] | spip | pointer to the SPIDriver object |
Definition at line 123 of file spi.c.
References chDbgAssert, chDbgCheck, chSysLock, chSysUnlock, SPIDriver::spd_state, SPI_ACTIVE, spi_lld_select(), and SPI_READY.
Referenced by mmcStartSequentialRead(), and mmcStartSequentialWrite().
void spiUnselect | ( | SPIDriver * | spip | ) |
Deasserts the slave select signal.
The previously selected peripheral is unselected.
[in] | spip | pointer to the SPIDriver object |
Definition at line 143 of file spi.c.
References chDbgAssert, chDbgCheck, chSysLock, chSysUnlock, SPIDriver::spd_state, SPI_ACTIVE, spi_lld_unselect(), and SPI_READY.
Referenced by mmcSequentialRead(), mmcSequentialWrite(), mmcStartSequentialRead(), mmcStartSequentialWrite(), mmcStopSequentialRead(), and mmcStopSequentialWrite().
void spiIgnore | ( | SPIDriver * | spip, | |
size_t | n | |||
) |
Ignores data on the SPI bus.
This function transmits a series of idle words on the SPI bus and ignores the received data. This function can be invoked even when a slave select signal has not been yet asserted.
[in] | spip | pointer to the SPIDriver object |
[in] | n | number of words to be ignored |
Definition at line 166 of file spi.c.
References chDbgAssert, chDbgCheck, SPIDriver::spd_state, SPI_ACTIVE, spi_lld_ignore(), and SPI_READY.
Referenced by mmcConnect(), mmcSequentialRead(), and mmcSequentialWrite().
void spiExchange | ( | SPIDriver * | spip, | |
size_t | n, | |||
const void * | txbuf, | |||
void * | rxbuf | |||
) |
Exchanges data on the SPI bus.
This function performs a simultaneous transmit/receive operation.
[in] | spip | pointer to the SPIDriver object |
[in] | n | number of words to be exchanged |
[in] | txbuf | the pointer to the transmit buffer |
[out] | rxbuf | the pointer to the receive buffer |
Definition at line 187 of file spi.c.
References chDbgAssert, chDbgCheck, SPIDriver::spd_state, SPI_ACTIVE, and spi_lld_exchange().
void spiSend | ( | SPIDriver * | spip, | |
size_t | n, | |||
const void * | txbuf | |||
) |
Sends data ever the SPI bus.
[in] | spip | pointer to the SPIDriver object |
[in] | n | number of words to send |
[in] | txbuf | the pointer to the transmit buffer |
Definition at line 207 of file spi.c.
References chDbgAssert, chDbgCheck, SPIDriver::spd_state, SPI_ACTIVE, and spi_lld_send().
Referenced by mmcSequentialWrite(), mmcStopSequentialRead(), and mmcStopSequentialWrite().
void spiReceive | ( | SPIDriver * | spip, | |
size_t | n, | |||
void * | rxbuf | |||
) |
Receives data from the SPI bus.
[in] | spip | pointer to the SPIDriver object |
[in] | n | number of words to receive |
[out] | rxbuf | the pointer to the receive buffer |
Definition at line 227 of file spi.c.
References chDbgAssert, chDbgCheck, SPIDriver::spd_state, SPI_ACTIVE, and spi_lld_receive().
Referenced by mmcSequentialRead(), and mmcSequentialWrite().
void spiAcquireBus | ( | SPIDriver * | spip | ) |
Gains exclusive access to the SPI bus.
This function tries to gain ownership to the SPI bus, if the bus is already being used then the invoking thread is queued.
SPI_USE_MUTUAL_EXCLUSION
option is set to TRUE
.[in] | spip | pointer to the SPIDriver object |
Definition at line 249 of file spi.c.
References chDbgCheck, chMtxLock(), chSemWait(), and SPIDriver::spd_mutex.
void spiReleaseBus | ( | SPIDriver * | spip | ) |
Releases exclusive access to the SPI bus.
SPI_USE_MUTUAL_EXCLUSION
option is set to TRUE
.[in] | spip | pointer to the SPIDriver object |
Definition at line 267 of file spi.c.
References chDbgCheck, chMtxUnlock(), and chSemSignal().