diff --git a/os/xhal/codegen/hal_spi.xml b/os/xhal/codegen/hal_spi.xml
index 8f48a86e0..92055e429 100644
--- a/os/xhal/codegen/hal_spi.xml
+++ b/os/xhal/codegen/hal_spi.xml
@@ -48,13 +48,19 @@
+ Return a pointer to the configuration structure.
+ Pointer to the @p hal_sio_driver_c object.
+ A pointer to the configuration structure.
+
+ config))]]>
+
+
Retrieves a configuration field.
Pointer to the @p hal_sio_driver_c object.
Configuration field to be retrieved.
The field value.
- config))->field]]>
+ field)]]>
@@ -184,7 +190,7 @@ osalDbgCheckClassI();
osalDbgCheck((self != NULL) && (n > 0U));
#if SPI_SUPPORTS_CIRCULAR
-osalDbgCheck((__spi_getconf(self, circular) == false) || ((n & 1U) == 0U));
+osalDbgCheck((__spi_getfield(self, circular) == false) || ((n & 1U) == 0U));
#endif
osalDbgAssert(self->state == HAL_DRV_STATE_READY, "not ready");
@@ -253,7 +259,7 @@ osalDbgCheckClassI();
osalDbgCheck((self != NULL) && (n > 0U) &&
(rxbuf != NULL) && (txbuf != NULL));
#if SPI_SUPPORTS_CIRCULAR
-osalDbgCheck((__spi_getconf(self, circular) == false) || ((n & 1U) == 0U));
+osalDbgCheck((__spi_getfield(self, circular) == false) || ((n & 1U) == 0U));
#endif
osalDbgAssert(self->state == HAL_DRV_STATE_READY, "not ready");
@@ -326,7 +332,7 @@ osalDbgCheckClassI();
osalDbgCheck((self != NULL) && (n > 0U) && (txbuf != NULL));
#if SPI_SUPPORTS_CIRCULAR
-osalDbgCheck((__spi_getconf(self, circular) == false) || ((n & 1U) == 0U));
+osalDbgCheck((__spi_getfield(self, circular) == false) || ((n & 1U) == 0U));
#endif
osalDbgAssert(self->state == HAL_DRV_STATE_READY, "not ready");
@@ -395,7 +401,7 @@ osalDbgCheckClassI();
osalDbgCheck((self != NULL) && (n > 0U) && (rxbuf != NULL));
#if SPI_SUPPORTS_CIRCULAR
-osalDbgCheck((__spi_getconf(self, circular) == false) || ((n & 1U) == 0U));
+osalDbgCheck((__spi_getfield(self, circular) == false) || ((n & 1U) == 0U));
#endif
osalDbgAssert(self->state == HAL_DRV_STATE_READY, "not ready");
@@ -722,34 +728,34 @@ spi_lld_unselect(self);]]>
+palClearLine(__spi_getfield(self, ssline));]]>
+palSetLine(__spi_getfield(self, ssline));]]>
+palClearPort(__spi_getfield(self, ssport), __spi_getfield(self, ssmask));]]>
+palSetPort(__spi_getfield(self, ssport), __spi_getfield(self, ssmask));]]>
+palClearPad(__spi_getfield(self, ssport), __spi_getfield(self, sspad));]]>
+palSetPad(__spi_getfield(self, ssport), __spi_getfield(self, sspad));]]>
diff --git a/os/xhal/include/hal_spi.h b/os/xhal/include/hal_spi.h
index 5a849d678..2507c45b0 100644
--- a/os/xhal/include/hal_spi.h
+++ b/os/xhal/include/hal_spi.h
@@ -113,6 +113,17 @@
/* Module macros. */
/*===========================================================================*/
+/**
+ * @brief Return a pointer to the configuration structure.
+ *
+ * @param ip Pointer to the @p hal_sio_driver_c object.
+ * @return A pointer to the configuration structure.
+ *
+ * @notapi
+ */
+#define __spi_getconf(ip) \
+ ((const hal_spi_config_t *)((ip)->config))
+
/**
* @brief Retrieves a configuration field.
*
@@ -122,8 +133,8 @@
*
* @notapi
*/
-#define __spi_getconf(ip, field) \
- ((const hal_spi_config_t *)((ip)->config))->field
+#define __spi_getfield(ip, field) \
+ (__spi_getconf(ip)->field)
/*===========================================================================*/
/* Module data structures and types. */
@@ -408,14 +419,14 @@ CC_FORCE_INLINE
static inline void spiSelectX(void *ip) {
hal_spi_driver_c *self = (hal_spi_driver_c *)ip;
- palClearLine(__spi_getconf(self, ssline));
+ palClearLine(__spi_getfield(self, ssline));
}
CC_FORCE_INLINE
static inline void spiUnselectX(void *ip) {
hal_spi_driver_c *self = (hal_spi_driver_c *)ip;
- palSetLine(__spi_getconf(self, ssline));
+ palSetLine(__spi_getfield(self, ssline));
}
#elif SPI_SELECT_MODE == SPI_SELECT_MODE_PORT
@@ -423,14 +434,14 @@ CC_FORCE_INLINE
static inline void spiSelectX(void *ip) {
hal_spi_driver_c *self = (hal_spi_driver_c *)ip;
- palClearPort(__spi_getconf(self, ssport), __spi_getconf(self, ssmask));
+ palClearPort(__spi_getfield(self, ssport), __spi_getfield(self, ssmask));
}
CC_FORCE_INLINE
static inline void spiUnselectX(void *ip) {
hal_spi_driver_c *self = (hal_spi_driver_c *)ip;
- palSetPort(__spi_getconf(self, ssport), __spi_getconf(self, ssmask));
+ palSetPort(__spi_getfield(self, ssport), __spi_getfield(self, ssmask));
}
#elif SPI_SELECT_MODE == SPI_SELECT_MODE_PAD
@@ -438,14 +449,14 @@ CC_FORCE_INLINE
static inline void spiSelectX(void *ip) {
hal_spi_driver_c *self = (hal_spi_driver_c *)ip;
- palClearPad(__spi_getconf(self, ssport), __spi_getconf(self, sspad));
+ palClearPad(__spi_getfield(self, ssport), __spi_getfield(self, sspad));
}
CC_FORCE_INLINE
static inline void spiUnselectX(void *ip) {
hal_spi_driver_c *self = (hal_spi_driver_c *)ip;
- palSetPad(__spi_getconf(self, ssport), __spi_getconf(self, sspad));
+ palSetPad(__spi_getfield(self, ssport), __spi_getfield(self, sspad));
}
#endif /* SPI_SELECT_MODE == SPI_SELECT_MODE_LLD */
diff --git a/os/xhal/ports/STM32/LLD/SPIv2/hal_spi_lld.c b/os/xhal/ports/STM32/LLD/SPIv2/hal_spi_lld.c
index ef6787c99..20c0953f3 100644
--- a/os/xhal/ports/STM32/LLD/SPIv2/hal_spi_lld.c
+++ b/os/xhal/ports/STM32/LLD/SPIv2/hal_spi_lld.c
@@ -126,16 +126,17 @@ SPIDriver SPID6;
* @param[in] spip pointer to the @p SPIDriver object
*/
static void spi_lld_enable(SPIDriver *spip) {
+ const hal_spi_config_t *config = __spi_getconf(spip);
uint32_t cr1, cr2;
/* SPI setup.*/
- if (spip->config->slave) {
- cr1 = spip->config->cr1 & ~(SPI_CR1_MSTR | SPI_CR1_SPE);
- cr2 = spip->config->cr2 | SPI_CR2_FRXTH | SPI_CR2_RXDMAEN | SPI_CR2_TXDMAEN;
+ if (config->slave) {
+ cr1 = config->cr1 & ~(SPI_CR1_MSTR | SPI_CR1_SPE);
+ cr2 = config->cr2 | SPI_CR2_FRXTH | SPI_CR2_RXDMAEN | SPI_CR2_TXDMAEN;
}
else {
- cr1 = (spip->config->cr1 | SPI_CR1_MSTR) & ~SPI_CR1_SPE;
- cr2 = spip->config->cr2 | SPI_CR2_FRXTH | SPI_CR2_SSOE | SPI_CR2_RXDMAEN | SPI_CR2_TXDMAEN;
+ cr1 = (config->cr1 | SPI_CR1_MSTR) & ~SPI_CR1_SPE;
+ cr2 = config->cr2 | SPI_CR2_FRXTH | SPI_CR2_SSOE | SPI_CR2_RXDMAEN | SPI_CR2_TXDMAEN;
}
spip->spi->CR1 = cr1;
@@ -154,7 +155,7 @@ static void spi_lld_enable(SPIDriver *spip) {
*/
static void spi_lld_disable(SPIDriver *spip) {
- if (!spip->config->slave) {
+ if (!__spi_getfield(spip, slave)) {
/* Master mode, stopping gracefully.*/
/* Stopping TX DMA channel.*/
@@ -246,7 +247,7 @@ static void spi_lld_serve_rx_interrupt(SPIDriver *spip, uint32_t flags) {
/* Reporting the failure.*/
__spi_isr_error_code(spip, HAL_RET_HW_FAILURE);
}
- else if (spip->config->circular) {
+ else if (__spi_getfield(spip, circular)) {
if ((flags & STM32_DMA_ISR_HTIF) != 0U) {
/* Half buffer interrupt.*/
__spi_isr_half_code(spip);
@@ -460,7 +461,7 @@ msg_t spi_lld_start(SPIDriver *spip) {
spip->txsource = (uint32_t)STM32_SPI_FILLER_PATTERN;
/* If in stopped state then enables the SPI and DMA clocks.*/
- if (spip->state == SPI_STOP) {
+ if (spip->state == DRV_STATE_STOP) {
if (false) {
}
@@ -586,7 +587,7 @@ msg_t spi_lld_start(SPIDriver *spip) {
}
/* Configuration-specific DMA setup.*/
- ds = spip->config->cr2 & SPI_CR2_DS;
+ ds = __spi_getfield(spip, cr2) & SPI_CR2_DS;
if (!ds || (ds <= (SPI_CR2_DS_2 | SPI_CR2_DS_1 | SPI_CR2_DS_0))) {
/* Frame width is 8 bits or smaller.*/
spip->rxdmamode = (spip->rxdmamode & ~STM32_DMA_CR_SIZE_MASK) |
@@ -602,7 +603,7 @@ msg_t spi_lld_start(SPIDriver *spip) {
STM32_DMA_CR_PSIZE_HWORD | STM32_DMA_CR_MSIZE_HWORD;
}
- if (spip->config->circular) {
+ if (__spi_getfield(spip, circular)) {
spip->rxdmamode |= (STM32_DMA_CR_CIRC | STM32_DMA_CR_HTIE);
spip->txdmamode |= (STM32_DMA_CR_CIRC | STM32_DMA_CR_HTIE);
}
@@ -895,9 +896,9 @@ uint16_t spi_lld_polled_exchange(SPIDriver *spip, uint16_t frame) {
* Byte size access (uint8_t *) for transactions that are <= 8-bit.
* Halfword size access (uint16_t) for transactions that are <= 8-bit.
*/
- if ((spip->config->cr2 & SPI_CR2_DS) <= (SPI_CR2_DS_2 |
- SPI_CR2_DS_1 |
- SPI_CR2_DS_0)) {
+ if ((__spi_getfield(spip, cr2) & SPI_CR2_DS) <= (SPI_CR2_DS_2 |
+ SPI_CR2_DS_1 |
+ SPI_CR2_DS_0)) {
volatile uint8_t *dr8p = (volatile uint8_t *)&spip->spi->DR;
*dr8p = (uint8_t)frame;
while ((spip->spi->SR & SPI_SR_RXNE) == 0U) {
diff --git a/os/xhal/src/hal_spi.c b/os/xhal/src/hal_spi.c
index c6c8368a4..2eb528c99 100644
--- a/os/xhal/src/hal_spi.c
+++ b/os/xhal/src/hal_spi.c
@@ -210,7 +210,7 @@ msg_t spiStartIgnoreI(void *ip, size_t n) {
osalDbgCheck((self != NULL) && (n > 0U));
#if SPI_SUPPORTS_CIRCULAR
- osalDbgCheck((__spi_getconf(self, circular) == false) || ((n & 1U) == 0U));
+ osalDbgCheck((__spi_getfield(self, circular) == false) || ((n & 1U) == 0U));
#endif
osalDbgAssert(self->state == HAL_DRV_STATE_READY, "not ready");
@@ -283,7 +283,7 @@ msg_t spiStartExchangeI(void *ip, size_t n, const void *txbuf, void *rxbuf) {
osalDbgCheck((self != NULL) && (n > 0U) &&
(rxbuf != NULL) && (txbuf != NULL));
#if SPI_SUPPORTS_CIRCULAR
- osalDbgCheck((__spi_getconf(self, circular) == false) || ((n & 1U) == 0U));
+ osalDbgCheck((__spi_getfield(self, circular) == false) || ((n & 1U) == 0U));
#endif
osalDbgAssert(self->state == HAL_DRV_STATE_READY, "not ready");
@@ -357,7 +357,7 @@ msg_t spiStartSendI(void *ip, size_t n, const void *txbuf) {
osalDbgCheck((self != NULL) && (n > 0U) && (txbuf != NULL));
#if SPI_SUPPORTS_CIRCULAR
- osalDbgCheck((__spi_getconf(self, circular) == false) || ((n & 1U) == 0U));
+ osalDbgCheck((__spi_getfield(self, circular) == false) || ((n & 1U) == 0U));
#endif
osalDbgAssert(self->state == HAL_DRV_STATE_READY, "not ready");
@@ -429,7 +429,7 @@ msg_t spiStartReceiveI(void *ip, size_t n, void *rxbuf) {
osalDbgCheck((self != NULL) && (n > 0U) && (rxbuf != NULL));
#if SPI_SUPPORTS_CIRCULAR
- osalDbgCheck((__spi_getconf(self, circular) == false) || ((n & 1U) == 0U));
+ osalDbgCheck((__spi_getfield(self, circular) == false) || ((n & 1U) == 0U));
#endif
osalDbgAssert(self->state == HAL_DRV_STATE_READY, "not ready");