git-svn-id: svn://svn.code.sf.net/p/chibios/svn/trunk@14917 27425a3e-05d8-49a3-a47f-9c15f0e5edd8

This commit is contained in:
Giovanni Di Sirio 2021-10-17 13:36:49 +00:00
parent f83396ec38
commit ff2b03c863
3 changed files with 150 additions and 98 deletions

View File

@ -48,8 +48,11 @@
* @name SPI-specific messages * @name SPI-specific messages
* @{ * @{
*/ */
#define MSG_SPI_BUFFER_FULL MSG_OK
#define MSG_SPI_BUFFER_HALF (msg_t)-3 #define MSG_SPI_BUFFER_HALF (msg_t)-3
#define MSG_SPI_BUFFER_FULL (msg_t)-4
#define MSG_SPI_COMPLETE MSG_OK
#define MSG_SPI_TIMEOUT MSG_TIMEOUT
#define MSG_SPI_STOPPED MSG_RESET
/** @} */ /** @} */
/*===========================================================================*/ /*===========================================================================*/
@ -152,8 +155,8 @@ typedef struct hal_spi_config SPIConfig;
/** /**
* @brief SPI notification callback type. * @brief SPI notification callback type.
* *
* @param[in] spip pointer to the @p SPIDriver object triggering the * @param[in] spip pointer to the @p SPIDriver object
* callback * triggering the callback
*/ */
typedef void (*spicb_t)(SPIDriver *spip); typedef void (*spicb_t)(SPIDriver *spip);
@ -269,12 +272,12 @@ struct hal_spi_driver {
* @brief Buffer state. * @brief Buffer state.
* @note This function is meant to be called from the SPI callback only. * @note This function is meant to be called from the SPI callback only.
* *
* @param[in] spip pointer to the @p SPIDriver object * @param[in] spip pointer to the @p SPIDriver object
* @return The buffer state. * @return The buffer state.
* @retval false if the driver filled/sent the first half of the * @retval false if the driver filled/sent the first half of
* buffer. * the buffer.
* @retval true if the driver filled/sent the second half of the * @retval true if the driver filled/sent the second half of
* buffer. * the buffer.
* *
* @special * @special
*/ */
@ -284,7 +287,7 @@ struct hal_spi_driver {
/** /**
* @brief Asserts the slave select signal and prepares for transfers. * @brief Asserts the slave select signal and prepares for transfers.
* *
* @param[in] spip pointer to the @p SPIDriver object * @param[in] spip pointer to the @p SPIDriver object
* *
* @iclass * @iclass
*/ */
@ -297,7 +300,7 @@ do { \
* @brief Deasserts the slave select signal. * @brief Deasserts the slave select signal.
* @details The previously selected peripheral is unselected. * @details The previously selected peripheral is unselected.
* *
* @param[in] spip pointer to the @p SPIDriver object * @param[in] spip pointer to the @p SPIDriver object
* *
* @iclass * @iclass
*/ */
@ -354,9 +357,9 @@ do { \
* polling than suspending the thread waiting for an interrupt. * polling than suspending the thread waiting for an interrupt.
* @note This API is implemented as a macro in order to minimize latency. * @note This API is implemented as a macro in order to minimize latency.
* *
* @param[in] spip pointer to the @p SPIDriver object * @param[in] spip pointer to the @p SPIDriver object
* @param[in] frame the data frame to send over the SPI bus * @param[in] frame the data frame to send over the SPI bus
* @return The received data frame from the SPI bus. * @return The received data frame from the SPI bus.
*/ */
#define spiPolledExchange(spip, frame) spi_lld_polled_exchange(spip, frame) #define spiPolledExchange(spip, frame) spi_lld_polled_exchange(spip, frame)
/** @} */ /** @} */
@ -382,6 +385,33 @@ do { \
#define __spi_wakeup_isr(spip) #define __spi_wakeup_isr(spip)
#endif /* !SPI_USE_SYNCHRONIZATION */ #endif /* !SPI_USE_SYNCHRONIZATION */
/**
* @brief Common ISR code in linear mode.
* @details This code handles the portable part of the ISR code:
* - Callback invocation.
* - Waiting thread wakeup, if any.
* - Driver state transitions.
* .
* @note This macro is meant to be used in the low level drivers
* implementation only.
*
* @param[in] spip pointer to the @p SPIDriver object
*
* @notapi
*/
#define __spi_isr_complete_code(spip) { \
if ((spip)->config->end_cb) { \
(spip)->state = SPI_COMPLETE; \
(spip)->config->end_cb(spip); \
if ((spip)->state == SPI_COMPLETE) \
(spip)->state = SPI_READY; \
} \
else { \
(spip)->state = SPI_READY; \
} \
__spi_wakeup_isr(spip, MSG_OK); \
}
/** /**
* @brief Half buffer filled ISR code in circular mode. * @brief Half buffer filled ISR code in circular mode.
* @details This code handles the portable part of the ISR code: * @details This code handles the portable part of the ISR code:
@ -390,7 +420,7 @@ do { \
* @note This macro is meant to be used in the low level drivers * @note This macro is meant to be used in the low level drivers
* implementation only. * implementation only.
* *
* @param[in] spip pointer to the @p SPIDriver object * @param[in] spip pointer to the @p SPIDriver object
* *
* @notapi * @notapi
*/ */
@ -410,7 +440,7 @@ do { \
* @note This macro is meant to be used in the low level drivers * @note This macro is meant to be used in the low level drivers
* implementation only. * implementation only.
* *
* @param[in] spip pointer to the @p SPIDriver object * @param[in] spip pointer to the @p SPIDriver object
* *
* @notapi * @notapi
*/ */
@ -430,8 +460,8 @@ do { \
* @note This macro is meant to be used in the low level drivers * @note This macro is meant to be used in the low level drivers
* implementation only. * implementation only.
* *
* @param[in] spip pointer to the @p SPIDriver object * @param[in] spip pointer to the @p SPIDriver object
* @param[in] msg error code * @param[in] msg error code
* *
* @notapi * @notapi
*/ */

View File

@ -155,9 +155,8 @@ static void spi_lld_serve_rx_interrupt(SPIDriver *spip, uint32_t flags) {
} }
} }
else { else {
/* Portable SPI ISR code defined in the high level driver, note, it is /* Operation finished interrupt.*/
a macro.*/ __spi_isr_complete_code(spip);
__spi_isr_full_code(spip);
} }
} }

View File

@ -59,7 +59,7 @@ void spiInit(void) {
/** /**
* @brief Initializes the standard part of a @p SPIDriver structure. * @brief Initializes the standard part of a @p SPIDriver structure.
* *
* @param[out] spip pointer to the @p SPIDriver object * @param[out] spip pointer to the @p SPIDriver object
* *
* @init * @init
*/ */
@ -81,9 +81,9 @@ void spiObjectInit(SPIDriver *spip) {
/** /**
* @brief Configures and activates the SPI peripheral. * @brief Configures and activates the SPI peripheral.
* *
* @param[in] spip pointer to the @p SPIDriver object * @param[in] spip pointer to the @p SPIDriver object
* @param[in] config pointer to the @p SPIConfig object * @param[in] config pointer to the @p SPIConfig object
* @return The operation status. * @return The operation status.
* *
* @api * @api
*/ */
@ -118,7 +118,7 @@ msg_t spiStart(SPIDriver *spip, const SPIConfig *config) {
/** /**
* @brief Deactivates the SPI peripheral. * @brief Deactivates the SPI peripheral.
* *
* @param[in] spip pointer to the @p SPIDriver object * @param[in] spip pointer to the @p SPIDriver object
* *
* @api * @api
*/ */
@ -141,7 +141,7 @@ void spiStop(SPIDriver *spip) {
/** /**
* @brief Asserts the slave select signal and prepares for transfers. * @brief Asserts the slave select signal and prepares for transfers.
* *
* @param[in] spip pointer to the @p SPIDriver object * @param[in] spip pointer to the @p SPIDriver object
* *
* @api * @api
*/ */
@ -159,7 +159,7 @@ void spiSelect(SPIDriver *spip) {
* @brief Deasserts the slave select signal. * @brief Deasserts the slave select signal.
* @details The previously selected peripheral is unselected. * @details The previously selected peripheral is unselected.
* *
* @param[in] spip pointer to the @p SPIDriver object * @param[in] spip pointer to the @p SPIDriver object
* *
* @api * @api
*/ */
@ -181,9 +181,9 @@ void spiUnselect(SPIDriver *spip) {
* @p spiSelectI(). * @p spiSelectI().
* @post At the end of the operation the configured callback is invoked. * @post At the end of the operation the configured callback is invoked.
* *
* @param[in] spip pointer to the @p SPIDriver object * @param[in] spip pointer to the @p SPIDriver object
* @param[in] n number of words to be ignored * @param[in] n number of words to be ignored
* @return The operation status. * @return The operation status.
* *
* @iclass * @iclass
*/ */
@ -217,9 +217,9 @@ msg_t spiStartIgnoreI(SPIDriver *spip, size_t n) {
* @p spiSelectI(). * @p spiSelectI().
* @post At the end of the operation the configured callback is invoked. * @post At the end of the operation the configured callback is invoked.
* *
* @param[in] spip pointer to the @p SPIDriver object * @param[in] spip pointer to the @p SPIDriver object
* @param[in] n number of words to be ignored * @param[in] n number of words to be ignored
* @return The operation status. * @return The operation status.
* *
* @api * @api
*/ */
@ -243,11 +243,11 @@ msg_t spiStartIgnore(SPIDriver *spip, size_t n) {
* @note The buffers are organized as uint8_t arrays for data sizes below * @note The buffers are organized as uint8_t arrays for data sizes below
* or equal to 8 bits else it is organized as uint16_t arrays. * or equal to 8 bits else it is organized as uint16_t arrays.
* *
* @param[in] spip pointer to the @p SPIDriver object * @param[in] spip pointer to the @p SPIDriver object
* @param[in] n number of words to be exchanged * @param[in] n number of words to be exchanged
* @param[in] txbuf the pointer to the transmit buffer * @param[in] txbuf the pointer to the transmit buffer
* @param[out] rxbuf the pointer to the receive buffer * @param[out] rxbuf the pointer to the receive buffer
* @return The operation status. * @return The operation status.
* *
* @iclass * @iclass
*/ */
@ -285,11 +285,11 @@ msg_t spiStartExchangeI(SPIDriver *spip, size_t n,
* @note The buffers are organized as uint8_t arrays for data sizes below * @note The buffers are organized as uint8_t arrays for data sizes below
* or equal to 8 bits else it is organized as uint16_t arrays. * or equal to 8 bits else it is organized as uint16_t arrays.
* *
* @param[in] spip pointer to the @p SPIDriver object * @param[in] spip pointer to the @p SPIDriver object
* @param[in] n number of words to be exchanged * @param[in] n number of words to be exchanged
* @param[in] txbuf the pointer to the transmit buffer * @param[in] txbuf the pointer to the transmit buffer
* @param[out] rxbuf the pointer to the receive buffer * @param[out] rxbuf the pointer to the receive buffer
* @return The operation status. * @return The operation status.
* *
* @api * @api
*/ */
@ -313,10 +313,10 @@ msg_t spiStartExchange(SPIDriver *spip, size_t n,
* @note The buffers are organized as uint8_t arrays for data sizes below * @note The buffers are organized as uint8_t arrays for data sizes below
* or equal to 8 bits else it is organized as uint16_t arrays. * or equal to 8 bits else it is organized as uint16_t arrays.
* *
* @param[in] spip pointer to the @p SPIDriver object * @param[in] spip pointer to the @p SPIDriver object
* @param[in] n number of words to send * @param[in] n number of words to send
* @param[in] txbuf the pointer to the transmit buffer * @param[in] txbuf the pointer to the transmit buffer
* @return The operation status. * @return The operation status.
* *
* @iclass * @iclass
*/ */
@ -351,10 +351,10 @@ msg_t spiStartSendI(SPIDriver *spip, size_t n, const void *txbuf) {
* @note The buffers are organized as uint8_t arrays for data sizes below * @note The buffers are organized as uint8_t arrays for data sizes below
* or equal to 8 bits else it is organized as uint16_t arrays. * or equal to 8 bits else it is organized as uint16_t arrays.
* *
* @param[in] spip pointer to the @p SPIDriver object * @param[in] spip pointer to the @p SPIDriver object
* @param[in] n number of words to send * @param[in] n number of words to send
* @param[in] txbuf the pointer to the transmit buffer * @param[in] txbuf the pointer to the transmit buffer
* @return The operation status. * @return The operation status.
* *
* @api * @api
*/ */
@ -377,10 +377,10 @@ msg_t spiStartSend(SPIDriver *spip, size_t n, const void *txbuf) {
* @note The buffers are organized as uint8_t arrays for data sizes below * @note The buffers are organized as uint8_t arrays for data sizes below
* or equal to 8 bits else it is organized as uint16_t arrays. * or equal to 8 bits else it is organized as uint16_t arrays.
* *
* @param[in] spip pointer to the @p SPIDriver object * @param[in] spip pointer to the @p SPIDriver object
* @param[in] n number of words to receive * @param[in] n number of words to receive
* @param[out] rxbuf the pointer to the receive buffer * @param[out] rxbuf the pointer to the receive buffer
* @return The operation status. * @return The operation status.
* *
* @iclass * @iclass
*/ */
@ -415,10 +415,10 @@ msg_t spiStartReceiveI(SPIDriver *spip, size_t n, void *rxbuf) {
* @note The buffers are organized as uint8_t arrays for data sizes below * @note The buffers are organized as uint8_t arrays for data sizes below
* or equal to 8 bits else it is organized as uint16_t arrays. * or equal to 8 bits else it is organized as uint16_t arrays.
* *
* @param[in] spip pointer to the @p SPIDriver object * @param[in] spip pointer to the @p SPIDriver object
* @param[in] n number of words to receive * @param[in] n number of words to receive
* @param[out] rxbuf the pointer to the receive buffer * @param[out] rxbuf the pointer to the receive buffer
* @return The operation status. * @return The operation status.
* *
* @api * @api
*/ */
@ -435,10 +435,10 @@ msg_t spiStartReceive(SPIDriver *spip, size_t n, void *rxbuf) {
/** /**
* @brief Stops the ongoing SPI operation. * @brief Stops the ongoing SPI operation.
* *
* @param[in] spip pointer to the @p SPIDriver object * @param[in] spip pointer to the @p SPIDriver object
* @param[out sizep pointer to the counter of frames not yet transferred * @param[out sizep pointer to the counter of frames not yet
* or @p NULL * transferred or @p NULL
* @return The operation status. * @return The operation status.
* *
* @iclass * @iclass
*/ */
@ -461,7 +461,7 @@ msg_t spiStopTranferI(SPIDriver *spip, size_t *sizep) {
spip->state = SPI_READY; spip->state = SPI_READY;
#if SPI_USE_SYNCHRONIZATION == TRUE #if SPI_USE_SYNCHRONIZATION == TRUE
osalThreadResumeI(&spip->sync_transfer, MSG_OK); osalThreadResumeI(&spip->sync_transfer, MSG_SPI_STOPPED);
#endif #endif
} }
else { else {
@ -474,10 +474,10 @@ msg_t spiStopTranferI(SPIDriver *spip, size_t *sizep) {
/** /**
* @brief Stops the ongoing SPI operation, if any. * @brief Stops the ongoing SPI operation, if any.
* *
* @param[in] spip pointer to the @p SPIDriver object * @param[in] spip pointer to the @p SPIDriver object
* @param[out sizep pointer to the counter of frames not yet transferred * @param[out sizep pointer to the counter of frames not yet
* or @p NULL * transferred or @p NULL
* @return The operation status. * @return The operation status.
* *
* @api * @api
*/ */
@ -499,11 +499,16 @@ msg_t spiStopTranfer(SPIDriver *spip, size_t *sizep) {
* @brief Synchronizes with current transfer completion. * @brief Synchronizes with current transfer completion.
* @note This function can only be called by a single thread at time. * @note This function can only be called by a single thread at time.
* *
* @param[in] spip pointer to the @p SPIDriver object * @param[in] spip pointer to the @p SPIDriver object
* @param[in] timeout synchronization timeout * @param[in] timeout synchronization timeout
* @return The synchronization result. * @return The synchronization result.
* @retval MSG_OK if TX operation finished. * @retval MSG_SPI_COMPLETE if operation completed without errors.
* @retval MSG_TIMEOUT if synchronization timed out. * @retval MSG_SPI_TIMEOUT if synchronization timed out.
* @retval MSG_SPI_STOPPED if the transfer has been stopped.
* @retval MSG_SPI_BUFFER_FULL if operation finished on buffer full (circular
* mode only).
* @retval MSG_SPI_BUFFER_HALF if operation finished on buffer half (circular
* mode only).
* *
* @sclass * @sclass
*/ */
@ -518,7 +523,7 @@ msg_t spiSynchronizeS(SPIDriver *spip, sysinterval_t timeout) {
msg = osalThreadSuspendTimeoutS(&spip->sync_transfer, timeout); msg = osalThreadSuspendTimeoutS(&spip->sync_transfer, timeout);
} }
else { else {
msg = MSG_OK; msg = MSG_SPI_COMPLETE;
} }
return msg; return msg;
@ -528,11 +533,16 @@ msg_t spiSynchronizeS(SPIDriver *spip, sysinterval_t timeout) {
* @brief Synchronizes with current transfer completion. * @brief Synchronizes with current transfer completion.
* @note This function can only be called by a single thread at time. * @note This function can only be called by a single thread at time.
* *
* @param[in] spip pointer to the @p SPIDriver object * @param[in] spip pointer to the @p SPIDriver object
* @param[in] timeout synchronization timeout * @param[in] timeout synchronization timeout
* @return The synchronization result. * @return The synchronization result.
* @retval MSG_OK if TX operation finished. * @retval MSG_SPI_COMPLETE if operation completed without errors.
* @retval MSG_TIMEOUT if synchronization timed out. * @retval MSG_SPI_TIMEOUT if synchronization timed out.
* @retval MSG_SPI_STOPPED if the transfer has been stopped.
* @retval MSG_SPI_BUFFER_FULL if operation finished on buffer full (circular
* mode only).
* @retval MSG_SPI_BUFFER_HALF if operation finished on buffer half (circular
* mode only).
* *
* @api * @api
*/ */
@ -553,9 +563,12 @@ msg_t spiSynchronize(SPIDriver *spip, sysinterval_t timeout) {
* @pre In order to use this function the option @p SPI_USE_SYNCHRONIZATION * @pre In order to use this function the option @p SPI_USE_SYNCHRONIZATION
* must be enabled. * must be enabled.
* *
* @param[in] spip pointer to the @p SPIDriver object * @param[in] spip pointer to the @p SPIDriver object
* @param[in] n number of words to be ignored * @param[in] n number of words to be ignored
* @return The operation status. * @return The operation status.
* @retval MSG_SPI_COMPLETE if operation completed without errors.
* @retval MSG_SPI_TIMEOUT if synchronization timed out.
* @retval MSG_SPI_STOPPED if the transfer has been stopped.
* *
* @api * @api
*/ */
@ -583,11 +596,15 @@ msg_t spiIgnore(SPIDriver *spip, size_t n) {
* @note The buffers are organized as uint8_t arrays for data sizes below * @note The buffers are organized as uint8_t arrays for data sizes below
* or equal to 8 bits else it is organized as uint16_t arrays. * or equal to 8 bits else it is organized as uint16_t arrays.
* *
* @param[in] spip pointer to the @p SPIDriver object * @param[in] spip pointer to the @p SPIDriver object
* @param[in] n number of words to be exchanged * @param[in] n number of words to be exchanged
* @param[in] txbuf the pointer to the transmit buffer * @param[in] txbuf the pointer to the transmit buffer
* @param[out] rxbuf the pointer to the receive buffer * @param[out] rxbuf the pointer to the receive buffer
* @return The operation status. * @return The operation status.
* @retval MSG_SPI_COMPLETE if operation completed without errors.
* mode only).
* @retval MSG_SPI_TIMEOUT if synchronization timed out.
* @retval MSG_SPI_STOPPED if the transfer has been stopped.
* *
* @api * @api
*/ */
@ -615,10 +632,13 @@ msg_t spiExchange(SPIDriver *spip, size_t n,
* @note The buffers are organized as uint8_t arrays for data sizes below * @note The buffers are organized as uint8_t arrays for data sizes below
* or equal to 8 bits else it is organized as uint16_t arrays. * or equal to 8 bits else it is organized as uint16_t arrays.
* *
* @param[in] spip pointer to the @p SPIDriver object * @param[in] spip pointer to the @p SPIDriver object
* @param[in] n number of words to send * @param[in] n number of words to send
* @param[in] txbuf the pointer to the transmit buffer * @param[in] txbuf the pointer to the transmit buffer
* @return The operation status. * @return The operation status.
* @retval MSG_SPI_COMPLETE if operation completed without errors.
* @retval MSG_SPI_TIMEOUT if synchronization timed out.
* @retval MSG_SPI_STOPPED if the transfer has been stopped.
* *
* @api * @api
*/ */
@ -645,10 +665,13 @@ msg_t spiSend(SPIDriver *spip, size_t n, const void *txbuf) {
* @note The buffers are organized as uint8_t arrays for data sizes below * @note The buffers are organized as uint8_t arrays for data sizes below
* or equal to 8 bits else it is organized as uint16_t arrays. * or equal to 8 bits else it is organized as uint16_t arrays.
* *
* @param[in] spip pointer to the @p SPIDriver object * @param[in] spip pointer to the @p SPIDriver object
* @param[in] n number of words to receive * @param[in] n number of words to receive
* @param[out] rxbuf the pointer to the receive buffer * @param[out] rxbuf the pointer to the receive buffer
* @return The operation status. * @return The operation status.
* @retval MSG_SPI_COMPLETE if operation completed without errors.
* @retval MSG_SPI_TIMEOUT if synchronization timed out.
* @retval MSG_SPI_STOPPED if the transfer has been stopped.
* *
* @api * @api
*/ */
@ -676,7 +699,7 @@ msg_t spiReceive(SPIDriver *spip, size_t n, void *rxbuf) {
* @pre In order to use this function the option @p SPI_USE_MUTUAL_EXCLUSION * @pre In order to use this function the option @p SPI_USE_MUTUAL_EXCLUSION
* must be enabled. * must be enabled.
* *
* @param[in] spip pointer to the @p SPIDriver object * @param[in] spip pointer to the @p SPIDriver object
* *
* @api * @api
*/ */
@ -692,7 +715,7 @@ void spiAcquireBus(SPIDriver *spip) {
* @pre In order to use this function the option @p SPI_USE_MUTUAL_EXCLUSION * @pre In order to use this function the option @p SPI_USE_MUTUAL_EXCLUSION
* must be enabled. * must be enabled.
* *
* @param[in] spip pointer to the @p SPIDriver object * @param[in] spip pointer to the @p SPIDriver object
* *
* @api * @api
*/ */