ChibiOS/HAL
6.1.0
|
Generic SPI Driver. More...
Generic SPI Driver.
This module implements a generic SPI (Serial Peripheral Interface) driver allowing bidirectional and monodirectional transfers, complex atomic transactions are supported as well.
HAL_USE_SPI
option must be enabled in halconf.h
.The driver implements a state machine internally, not all the driver functionalities can be used in any moment, any transition not explicitly shown in the following diagram has to be considered an error and shall be captured by an assertion (if enabled).
The driver is not thread safe for performance reasons, if you need to access the SPI bus from multiple threads then use the spiAcquireBus()
and spiReleaseBus()
APIs in order to gain exclusive access.
Macros | |
#define | SPI_SUPPORTS_CIRCULAR TRUE |
Circular mode support flag. More... | |
SPI configuration options | |
#define | SPI_USE_WAIT TRUE |
Enables synchronous APIs. More... | |
#define | SPI_USE_CIRCULAR FALSE |
Enables circular transfers APIs. More... | |
#define | SPI_USE_MUTUAL_EXCLUSION TRUE |
Enables the spiAcquireBus() and spiReleaseBus() APIs. More... | |
#define | SPI_SELECT_MODE SPI_SELECT_MODE_PAD |
Handling method for SPI CS line. More... | |
Macro Functions | |
#define | spiSelectI(spip) |
Asserts the slave select signal and prepares for transfers. More... | |
#define | spiUnselectI(spip) |
Deasserts the slave select signal. More... | |
#define | spiStartIgnoreI(spip, n) |
Ignores data on the SPI bus. More... | |
#define | spiStartExchangeI(spip, n, txbuf, rxbuf) |
Exchanges data on the SPI bus. More... | |
#define | spiStartSendI(spip, n, txbuf) |
Sends data over the SPI bus. More... | |
#define | spiStartReceiveI(spip, n, rxbuf) |
Receives data from the SPI bus. More... | |
#define | spiPolledExchange(spip, frame) spi_lld_polled_exchange(spip, frame) |
Exchanges one frame using a polled wait. More... | |
Low level driver helper macros | |
#define | _spi_wakeup_isr(spip) |
Wakes up the waiting thread. More... | |
#define | _spi_isr_code(spip) |
Common ISR code. More... | |
#define | _spi_isr_code_half1(spip) |
Common ISR code in circular mode. More... | |
#define | _spi_isr_code_half2(spip) |
Common ISR code in circular mode. More... | |
PLATFORM configuration options | |
#define | PLATFORM_SPI_USE_SPI1 FALSE |
SPI1 driver enable switch. More... | |
Typedefs | |
typedef struct SPIDriver | SPIDriver |
Type of a structure representing an SPI driver. More... | |
typedef void(* | spicallback_t) (SPIDriver *spip) |
SPI notification callback type. More... | |
Data Structures | |
struct | SPIConfig |
Driver configuration structure. More... | |
struct | SPIDriver |
Structure representing an SPI driver. More... | |
Functions | |
void | spiInit (void) |
SPI Driver initialization. More... | |
void | spiObjectInit (SPIDriver *spip) |
Initializes the standard part of a SPIDriver structure. More... | |
void | spiStart (SPIDriver *spip, const SPIConfig *config) |
Configures and activates the SPI peripheral. More... | |
void | spiStop (SPIDriver *spip) |
Deactivates the SPI peripheral. More... | |
void | spiSelect (SPIDriver *spip) |
Asserts the slave select signal and prepares for transfers. More... | |
void | spiUnselect (SPIDriver *spip) |
Deasserts the slave select signal. More... | |
void | spiStartIgnore (SPIDriver *spip, size_t n) |
Ignores data on the SPI bus. More... | |
void | spiStartExchange (SPIDriver *spip, size_t n, const void *txbuf, void *rxbuf) |
Exchanges data on the SPI bus. More... | |
void | spiStartSend (SPIDriver *spip, size_t n, const void *txbuf) |
Sends data over the SPI bus. More... | |
void | spiStartReceive (SPIDriver *spip, size_t n, void *rxbuf) |
Receives data from the SPI bus. More... | |
void | spiAbortI (SPIDriver *spip) |
Aborts the ongoing SPI operation. More... | |
void | spiAbort (SPIDriver *spip) |
Aborts the ongoing SPI operation, if any. More... | |
void | spiIgnore (SPIDriver *spip, size_t n) |
Ignores data on the SPI bus. More... | |
void | spiExchange (SPIDriver *spip, size_t n, const void *txbuf, void *rxbuf) |
Exchanges data on the SPI bus. More... | |
void | spiSend (SPIDriver *spip, size_t n, const void *txbuf) |
Sends data over the SPI bus. More... | |
void | spiReceive (SPIDriver *spip, size_t n, void *rxbuf) |
Receives data from the SPI bus. More... | |
void | spiAcquireBus (SPIDriver *spip) |
Gains exclusive access to the SPI bus. More... | |
void | spiReleaseBus (SPIDriver *spip) |
Releases exclusive access to the SPI bus. More... | |
void | spi_lld_init (void) |
Low level SPI driver initialization. More... | |
void | spi_lld_start (SPIDriver *spip) |
Configures and activates the SPI peripheral. More... | |
void | spi_lld_stop (SPIDriver *spip) |
Deactivates the SPI peripheral. More... | |
void | spi_lld_select (SPIDriver *spip) |
Asserts the slave select signal and prepares for transfers. More... | |
void | spi_lld_unselect (SPIDriver *spip) |
Deasserts the slave select signal. More... | |
void | spi_lld_ignore (SPIDriver *spip, size_t n) |
Ignores data on the SPI bus. More... | |
void | spi_lld_exchange (SPIDriver *spip, size_t n, const void *txbuf, void *rxbuf) |
Exchanges data on the SPI bus. More... | |
void | spi_lld_send (SPIDriver *spip, size_t n, const void *txbuf) |
Sends data over the SPI bus. More... | |
void | spi_lld_receive (SPIDriver *spip, size_t n, void *rxbuf) |
Receives data from the SPI bus. More... | |
void | spi_lld_abort (SPIDriver *spip) |
Aborts the ongoing SPI operation, if any. More... | |
uint16_t | spi_lld_polled_exchange (SPIDriver *spip, uint16_t frame) |
Exchanges one frame using a polled wait. More... | |
Enumerations |
Variables | |
SPIDriver | SPID1 |
SPI1 driver identifier. More... | |
#define SPI_USE_WAIT TRUE |
#define SPI_USE_CIRCULAR FALSE |
#define SPI_USE_MUTUAL_EXCLUSION TRUE |
Enables the spiAcquireBus()
and spiReleaseBus()
APIs.
#define SPI_SELECT_MODE SPI_SELECT_MODE_PAD |
#define spiSelectI | ( | spip | ) |
Asserts the slave select signal and prepares for transfers.
[in] | spip | pointer to the SPIDriver object |
Definition at line 139 of file hal_spi.h.
Referenced by spiSelect().
#define spiUnselectI | ( | spip | ) |
Deasserts the slave select signal.
The previously selected peripheral is unselected.
[in] | spip | pointer to the SPIDriver object |
Definition at line 152 of file hal_spi.h.
Referenced by spiUnselect().
#define spiStartIgnoreI | ( | spip, | |
n | |||
) |
Ignores data on the SPI bus.
This asynchronous function starts the transmission of a series of idle words on the SPI bus and ignores the received data.
spiSelect()
or spiSelectI()
. [in] | spip | pointer to the SPIDriver object |
[in] | n | number of words to be ignored |
Definition at line 209 of file hal_spi.h.
Referenced by spiIgnore(), and spiStartIgnore().
#define spiStartExchangeI | ( | spip, | |
n, | |||
txbuf, | |||
rxbuf | |||
) |
Exchanges data on the SPI bus.
This asynchronous function starts a simultaneous transmit/receive operation.
spiSelect()
or spiSelectI()
. [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 231 of file hal_spi.h.
Referenced by spiExchange(), and spiStartExchange().
#define spiStartSendI | ( | spip, | |
n, | |||
txbuf | |||
) |
Sends data over the SPI bus.
This asynchronous function starts a transmit operation.
spiSelect()
or spiSelectI()
. [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 251 of file hal_spi.h.
Referenced by spiSend(), and spiStartSend().
#define spiStartReceiveI | ( | spip, | |
n, | |||
rxbuf | |||
) |
Receives data from the SPI bus.
This asynchronous function starts a receive operation.
spiSelect()
or spiSelectI()
. [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 271 of file hal_spi.h.
Referenced by spiReceive(), and spiStartReceive().
#define spiPolledExchange | ( | spip, | |
frame | |||
) | spi_lld_polled_exchange(spip, frame) |
Exchanges one frame using a polled wait.
This synchronous function exchanges one frame using a polled synchronization method. This function is useful when exchanging small amount of data on high speed channels, usually in this situation is much more efficient just wait for completion using polling than suspending the thread waiting for an interrupt.
[in] | spip | pointer to the SPIDriver object |
[in] | frame | the data frame to send over the SPI bus |
#define _spi_wakeup_isr | ( | spip | ) |
Wakes up the waiting thread.
[in] | spip | pointer to the SPIDriver object |
#define _spi_isr_code | ( | spip | ) |
Common ISR code.
This code handles the portable part of the ISR code:
[in] | spip | pointer to the SPIDriver object |
#define _spi_isr_code_half1 | ( | spip | ) |
Common ISR code in circular mode.
This code handles the portable part of the ISR code:
[in] | spip | pointer to the SPIDriver object |
#define _spi_isr_code_half2 | ( | spip | ) |
Common ISR code in circular mode.
This code handles the portable part of the ISR code:
[in] | spip | pointer to the SPIDriver object |
#define SPI_SUPPORTS_CIRCULAR TRUE |
Circular mode support flag.
Definition at line 37 of file hal_spi_lld.h.
#define PLATFORM_SPI_USE_SPI1 FALSE |
SPI1 driver enable switch.
If set to TRUE
the support for SPI1 is included.
FALSE
. Definition at line 53 of file hal_spi_lld.h.
Type of a structure representing an SPI driver.
Definition at line 68 of file hal_spi_lld.h.
typedef void(* spicallback_t) (SPIDriver *spip) |
SPI notification callback type.
[in] | spip | pointer to the SPIDriver object triggering the callback |
Definition at line 76 of file hal_spi_lld.h.
enum spistate_t |
void spiInit | ( | void | ) |
SPI Driver initialization.
halInit()
, there is no need to explicitly initialize the driver.Definition at line 56 of file hal_spi.c.
References spi_lld_init().
Referenced by halInit().
void spiObjectInit | ( | SPIDriver * | spip | ) |
Initializes the standard part of a SPIDriver
structure.
[out] | spip | pointer to the SPIDriver object |
Definition at line 68 of file hal_spi.c.
References SPIDriver::config, SPIDriver::mutex, osalMutexObjectInit(), SPI_STOP, SPIDriver::state, and SPIDriver::thread.
Referenced by spi_lld_init().
Configures and activates the SPI peripheral.
Definition at line 91 of file hal_spi.c.
References SPIDriver::config, osalDbgAssert, osalDbgCheck, osalSysLock(), osalSysUnlock(), spi_lld_start(), SPI_READY, SPI_STOP, and SPIDriver::state.
Referenced by mmcConnect(), mmcDisconnect(), mmcStartSequentialRead(), mmcStartSequentialWrite(), and mmcSync().
void spiStop | ( | SPIDriver * | spip | ) |
Deactivates the SPI peripheral.
[in] | spip | pointer to the SPIDriver object |
Definition at line 111 of file hal_spi.c.
References SPIDriver::config, osalDbgAssert, osalDbgCheck, osalSysLock(), osalSysUnlock(), spi_lld_stop(), SPI_READY, SPI_STOP, and SPIDriver::state.
Referenced by mmcConnect(), mmcDisconnect(), mmcErase(), mmcSequentialRead(), mmcSequentialWrite(), mmcStartSequentialRead(), mmcStartSequentialWrite(), 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 134 of file hal_spi.c.
References osalDbgAssert, osalDbgCheck, osalSysLock(), osalSysUnlock(), SPI_READY, spiSelectI, and SPIDriver::state.
Referenced by mmcStartSequentialRead(), mmcStartSequentialWrite(), read_CxD(), send_command_R1(), send_command_R3(), and sync().
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 152 of file hal_spi.c.
References osalDbgAssert, osalDbgCheck, osalSysLock(), osalSysUnlock(), SPI_READY, spiUnselectI, and SPIDriver::state.
Referenced by mmcSequentialRead(), mmcSequentialWrite(), mmcStopSequentialRead(), mmcStopSequentialWrite(), read_CxD(), send_command_R1(), send_command_R3(), and sync().
void spiStartIgnore | ( | SPIDriver * | spip, |
size_t | n | ||
) |
Ignores data on the SPI bus.
This asynchronous function starts the transmission of a series of idle words on the SPI bus and ignores the received data.
spiSelect()
or spiSelectI()
. [in] | spip | pointer to the SPIDriver object |
[in] | n | number of words to be ignored |
Definition at line 175 of file hal_spi.c.
References osalDbgAssert, osalDbgCheck, osalSysLock(), osalSysUnlock(), SPI_READY, spiStartIgnoreI, and SPIDriver::state.
void spiStartExchange | ( | SPIDriver * | spip, |
size_t | n, | ||
const void * | txbuf, | ||
void * | rxbuf | ||
) |
Exchanges data on the SPI bus.
This asynchronous function starts a simultaneous transmit/receive operation.
spiSelect()
or spiSelectI()
. [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 202 of file hal_spi.c.
References osalDbgAssert, osalDbgCheck, osalSysLock(), osalSysUnlock(), SPI_READY, spiStartExchangeI, and SPIDriver::state.
void spiStartSend | ( | SPIDriver * | spip, |
size_t | n, | ||
const void * | txbuf | ||
) |
Sends data over the SPI bus.
This asynchronous function starts a transmit operation.
spiSelect()
or spiSelectI()
. [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 229 of file hal_spi.c.
References osalDbgAssert, osalDbgCheck, osalSysLock(), osalSysUnlock(), SPI_READY, spiStartSendI, and SPIDriver::state.
void spiStartReceive | ( | SPIDriver * | spip, |
size_t | n, | ||
void * | rxbuf | ||
) |
Receives data from the SPI bus.
This asynchronous function starts a receive operation.
spiSelect()
or spiSelectI()
. [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 254 of file hal_spi.c.
References osalDbgAssert, osalDbgCheck, osalSysLock(), osalSysUnlock(), SPI_READY, spiStartReceiveI, and SPIDriver::state.
void spiAbortI | ( | SPIDriver * | spip | ) |
Aborts the ongoing SPI operation.
[in] | spip | pointer to the SPIDriver object |
Definition at line 272 of file hal_spi.c.
References osalDbgAssert, osalDbgCheck, osalDbgCheckClassI, osalThreadResumeI(), SPI_ACTIVE, SPI_COMPLETE, spi_lld_abort(), SPI_READY, SPIDriver::state, and SPIDriver::thread.
Referenced by spiAbort().
void spiAbort | ( | SPIDriver * | spip | ) |
Aborts the ongoing SPI operation, if any.
[in] | spip | pointer to the SPIDriver object |
Definition at line 294 of file hal_spi.c.
References osalDbgAssert, osalOsRescheduleS(), osalSysLock(), osalSysUnlock(), SPI_ACTIVE, SPI_READY, spiAbortI(), and SPIDriver::state.
void spiIgnore | ( | SPIDriver * | spip, |
size_t | n | ||
) |
Ignores data on the SPI bus.
This synchronous function performs the transmission of a series of idle words on the SPI bus and ignores the received data.
SPI_USE_WAIT
must be enabled. end_cb
= NULL
).[in] | spip | pointer to the SPIDriver object |
[in] | n | number of words to be ignored |
Definition at line 322 of file hal_spi.c.
References SPIConfig::circular, SPIDriver::config, osalDbgAssert, osalDbgCheck, osalSysLock(), osalSysUnlock(), osalThreadSuspendS(), SPI_READY, spiStartIgnoreI, SPIDriver::state, and SPIDriver::thread.
Referenced by mmcConnect(), mmcSequentialRead(), mmcSequentialWrite(), and read_CxD().
void spiExchange | ( | SPIDriver * | spip, |
size_t | n, | ||
const void * | txbuf, | ||
void * | rxbuf | ||
) |
Exchanges data on the SPI bus.
This synchronous function performs a simultaneous transmit/receive operation.
SPI_USE_WAIT
must be enabled. end_cb
= NULL
). [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 354 of file hal_spi.c.
References SPIConfig::circular, SPIDriver::config, osalDbgAssert, osalDbgCheck, osalSysLock(), osalSysUnlock(), osalThreadSuspendS(), SPI_READY, spiStartExchangeI, SPIDriver::state, and SPIDriver::thread.
void spiSend | ( | SPIDriver * | spip, |
size_t | n, | ||
const void * | txbuf | ||
) |
Sends data over the SPI bus.
This synchronous function performs a transmit operation.
SPI_USE_WAIT
must be enabled. end_cb
= NULL
). [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 386 of file hal_spi.c.
References SPIConfig::circular, SPIDriver::config, osalDbgAssert, osalDbgCheck, osalSysLock(), osalSysUnlock(), osalThreadSuspendS(), SPI_READY, spiStartSendI, SPIDriver::state, and SPIDriver::thread.
Referenced by mmcSequentialWrite(), mmcStopSequentialRead(), mmcStopSequentialWrite(), and send_hdr().
void spiReceive | ( | SPIDriver * | spip, |
size_t | n, | ||
void * | rxbuf | ||
) |
Receives data from the SPI bus.
This synchronous function performs a receive operation.
SPI_USE_WAIT
must be enabled. end_cb
= NULL
). [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 416 of file hal_spi.c.
References SPIConfig::circular, SPIDriver::config, osalDbgAssert, osalDbgCheck, osalSysLock(), osalSysUnlock(), osalThreadSuspendS(), SPI_READY, spiStartReceiveI, SPIDriver::state, and SPIDriver::thread.
Referenced by mmcSequentialRead(), mmcSequentialWrite(), read_CxD(), recvr1(), recvr3(), sync(), and wait().
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
must be enabled.[in] | spip | pointer to the SPIDriver object |
Definition at line 443 of file hal_spi.c.
References SPIDriver::mutex, osalDbgCheck, and osalMutexLock().
void spiReleaseBus | ( | SPIDriver * | spip | ) |
Releases exclusive access to the SPI bus.
SPI_USE_MUTUAL_EXCLUSION
must be enabled.[in] | spip | pointer to the SPIDriver object |
Definition at line 459 of file hal_spi.c.
References SPIDriver::mutex, osalDbgCheck, and osalMutexUnlock().
void spi_lld_init | ( | void | ) |
Low level SPI driver initialization.
Definition at line 65 of file hal_spi_lld.c.
References spiObjectInit().
Referenced by spiInit().
void spi_lld_start | ( | SPIDriver * | spip | ) |
Configures and activates the SPI peripheral.
[in] | spip | pointer to the SPIDriver object |
Definition at line 80 of file hal_spi_lld.c.
References SPI_STOP, and SPIDriver::state.
Referenced by spiStart().
void spi_lld_stop | ( | SPIDriver * | spip | ) |
Deactivates the SPI peripheral.
[in] | spip | pointer to the SPIDriver object |
Definition at line 101 of file hal_spi_lld.c.
References SPI_READY, and SPIDriver::state.
Referenced by spiStop().
void spi_lld_select | ( | SPIDriver * | spip | ) |
Asserts the slave select signal and prepares for transfers.
[in] | spip | pointer to the SPIDriver object |
Definition at line 120 of file hal_spi_lld.c.
void spi_lld_unselect | ( | SPIDriver * | spip | ) |
Deasserts the slave select signal.
The previously selected peripheral is unselected.
[in] | spip | pointer to the SPIDriver object |
Definition at line 134 of file hal_spi_lld.c.
void spi_lld_ignore | ( | SPIDriver * | spip, |
size_t | n | ||
) |
Ignores data on the SPI bus.
This asynchronous function starts the transmission of a series of idle words on the SPI bus and ignores the received data.
[in] | spip | pointer to the SPIDriver object |
[in] | n | number of words to be ignored |
Definition at line 151 of file hal_spi_lld.c.
void spi_lld_exchange | ( | SPIDriver * | spip, |
size_t | n, | ||
const void * | txbuf, | ||
void * | rxbuf | ||
) |
Exchanges data on the SPI bus.
This asynchronous function starts 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 173 of file hal_spi_lld.c.
void spi_lld_send | ( | SPIDriver * | spip, |
size_t | n, | ||
const void * | txbuf | ||
) |
Sends data over the SPI bus.
This asynchronous function starts a transmit operation.
[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 196 of file hal_spi_lld.c.
void spi_lld_receive | ( | SPIDriver * | spip, |
size_t | n, | ||
void * | rxbuf | ||
) |
Receives data from the SPI bus.
This asynchronous function starts a receive operation.
[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 217 of file hal_spi_lld.c.
void spi_lld_abort | ( | SPIDriver * | spip | ) |
Aborts the ongoing SPI operation, if any.
[in] | spip | pointer to the SPIDriver object |
Definition at line 233 of file hal_spi_lld.c.
Referenced by spiAbortI().
uint16_t spi_lld_polled_exchange | ( | SPIDriver * | spip, |
uint16_t | frame | ||
) |
Exchanges one frame using a polled wait.
This synchronous function exchanges one frame using a polled synchronization method. This function is useful when exchanging small amount of data on high speed channels, usually in this situation is much more efficient just wait for completion using polling than suspending the thread waiting for an interrupt.
[in] | spip | pointer to the SPIDriver object |
[in] | frame | the data frame to send over the SPI bus |
Definition at line 253 of file hal_spi_lld.c.
SPIDriver SPID1 |
SPI1 driver identifier.
Definition at line 41 of file hal_spi_lld.c.