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

This commit is contained in:
Giovanni Di Sirio 2023-08-24 09:52:13 +00:00
parent 18d026687f
commit 473728e4bd
4 changed files with 123 additions and 42 deletions

View File

@ -8,6 +8,26 @@
</imports>
<public>
<definitions_early>
<group description="SPI operation modes">
<define name="SPI_MODE_CIRCULAR" value="1U &lt;&lt; 0">
<brief>Enables the circular buffer mode.</brief>
</define>
<define name="SPI_MODE_SLAVE" value="1U &lt;&lt; 1">
<brief>Enables the slave mode.</brief>
</define>
<define name="SPI_MODE_FSIZE_MASK" value="3U &lt;&lt; 2">
<brief>Memory buffers frame size.</brief>
</define>
<define name="SPI_MODE_FSIZE_8" value="0U &lt;&lt; 2">
<brief>Memory frame size is 8 bits</brief>
</define>
<define name="SPI_MODE_FSIZE_16" value="1U &lt;&lt; 2">
<brief>Memory frame size is 16 bits</brief>
</define>
<define name="SPI_MODE_FSIZE_32" value="2U &lt;&lt; 2">
<brief>Memory frame size is 32 bits</brief>
</define>
</group>
<group description="SPI CS modes">
<define name="SPI_SELECT_MODE_NONE" value="0">
<brief>@p spiSelect() and @p spiUnselect() do nothing.</brief>
@ -69,6 +89,10 @@
</macro>
</macros>
<types>
<typedef name="spi_mode_t">
<brief>Type of SPI transfer mode options.</brief>
<basetype ctype="uint_fast16_t" />
</typedef>
<typedef name="hal_spi_driver_c">
<brief>Type of structure representing a SPI driver.</brief>
<basetype ctype="struct hal_spi_driver" />
@ -103,16 +127,9 @@
architecture dependent, fields.
</note>
<fields>
<condition check="SPI_SUPPORTS_CIRCULAR == TRUE">
<field name="circular" ctype="bool">
<brief>Enables the circular buffer mode.</brief>
</field>
</condition>
<condition check="SPI_SUPPORTS_SLAVE_MODE == TRUE">
<field name="slave" ctype="bool">
<brief>Enables the slave mode.</brief>
</field>
</condition>
<field name="mode" ctype="spi_mode_t">
<brief>SPI transfer mode options.</brief>
</field>
<condition check="SPI_SELECT_MODE == SPI_SELECT_MODE_LINE">
<field name="ssline" ctype="ioline_t">
<brief>The chip select line.</brief>
@ -146,6 +163,7 @@ spi_lld_config_fields;]]></verbatim>
<verbatim><![CDATA[
SPI_CONFIG_EXT_FIELDS]]></verbatim>
</condition>
</fields>
</struct>
<class type="regular" name="hal_spi_driver" namespace="spi"
@ -199,7 +217,8 @@ osalDbgCheckClassI();
osalDbgCheck((self != NULL) && (n > 0U));
#if SPI_SUPPORTS_CIRCULAR
osalDbgCheck((__spi_getfield(self, circular) == false) || ((n & 1U) == 0U));
osalDbgCheck(((__spi_getfield(self, mode) & SPI_MODE_CIRCULAR) == 0U) ||
((n & 1U) == 0U));
#endif
osalDbgAssert(self->state == HAL_DRV_STATE_READY, "not ready");
@ -268,7 +287,8 @@ osalDbgCheckClassI();
osalDbgCheck((self != NULL) && (n > 0U) &&
(rxbuf != NULL) && (txbuf != NULL));
#if SPI_SUPPORTS_CIRCULAR
osalDbgCheck((__spi_getfield(self, circular) == false) || ((n & 1U) == 0U));
osalDbgCheck(((__spi_getfield(self, mode) & SPI_MODE_CIRCULAR) == 0U) ||
((n & 1U) == 0U));
#endif
osalDbgAssert(self->state == HAL_DRV_STATE_READY, "not ready");
@ -341,7 +361,8 @@ osalDbgCheckClassI();
osalDbgCheck((self != NULL) && (n > 0U) && (txbuf != NULL));
#if SPI_SUPPORTS_CIRCULAR
osalDbgCheck((__spi_getfield(self, circular) == false) || ((n & 1U) == 0U));
osalDbgCheck(((__spi_getfield(self, mode) & SPI_MODE_CIRCULAR) == 0U) ||
((n & 1U) == 0U));
#endif
osalDbgAssert(self->state == HAL_DRV_STATE_READY, "not ready");
@ -410,7 +431,8 @@ osalDbgCheckClassI();
osalDbgCheck((self != NULL) && (n > 0U) && (rxbuf != NULL));
#if SPI_SUPPORTS_CIRCULAR
osalDbgCheck((__spi_getfield(self, circular) == false) || ((n & 1U) == 0U));
osalDbgCheck(((__spi_getfield(self, mode) & SPI_MODE_CIRCULAR) == 0U) ||
((n & 1U) == 0U));
#endif
osalDbgAssert(self->state == HAL_DRV_STATE_READY, "not ready");

View File

@ -33,6 +33,41 @@
/* Module constants. */
/*===========================================================================*/
/**
* @name SPI operation modes
* @{
*/
/**
* @brief Enables the circular buffer mode.
*/
#define SPI_MODE_CIRCULAR 1U << 0
/**
* @brief Enables the slave mode.
*/
#define SPI_MODE_SLAVE 1U << 1
/**
* @brief Memory buffers frame size.
*/
#define SPI_MODE_FSIZE_MASK 3U << 2
/**
* @brief Memory frame size is 8 bits.
*/
#define SPI_MODE_FSIZE_8 0U << 2
/**
* @brief Memory frame size is 16 bits.
*/
#define SPI_MODE_FSIZE_16 1U << 2
/**
* @brief Memory frame size is 32 bits.
*/
#define SPI_MODE_FSIZE_32 2U << 2
/** @} */
/**
* @name SPI CS modes
* @{
@ -150,6 +185,11 @@
/* Module data structures and types. */
/*===========================================================================*/
/**
* @brief Type of SPI transfer mode options.
*/
typedef uint_fast16_t spi_mode_t;
/**
* @brief Type of structure representing a SPI driver.
*/
@ -187,18 +227,10 @@ typedef struct hal_spi_driver SPIDriver;
* architecture dependent, fields.
*/
struct hal_spi_config {
#if (SPI_SUPPORTS_CIRCULAR == TRUE) || defined (__DOXYGEN__)
/**
* @brief Enables the circular buffer mode.
* @brief SPI transfer mode options.
*/
bool circular;
#endif /* SPI_SUPPORTS_CIRCULAR == TRUE */
#if (SPI_SUPPORTS_SLAVE_MODE == TRUE) || defined (__DOXYGEN__)
/**
* @brief Enables the slave mode.
*/
bool slave;
#endif /* SPI_SUPPORTS_SLAVE_MODE == TRUE */
spi_mode_t mode;
#if (SPI_SELECT_MODE == SPI_SELECT_MODE_LINE) || defined (__DOXYGEN__)
/**
* @brief The chip select line.

View File

@ -120,8 +120,7 @@ SPIDriver SPID6;
* Default SPI configuration.
*/
static const hal_spi_config_t spi_default_config = {
.circular = false,
.slave = false,
.mode = 0U,
#if (SPI_SELECT_MODE == SPI_SELECT_MODE_LINE) || defined (__DOXYGEN__)
.ssline = PAL_LINE(STM32_SPI_DEFAULT_PORT, STM32_SPI_DEFAULT_PAD);
#elif SPI_SELECT_MODE == SPI_SELECT_MODE_PORT
@ -149,7 +148,7 @@ static void spi_lld_enable(SPIDriver *spip) {
uint32_t cr1, cr2;
/* SPI setup.*/
if (config->slave) {
if ((config->mode & SPI_MODE_SLAVE) != 0U) {
cr1 = config->cr1 & ~(SPI_CR1_MSTR | SPI_CR1_SPE);
cr2 = config->cr2 | SPI_CR2_FRXTH | SPI_CR2_RXDMAEN | SPI_CR2_TXDMAEN;
}
@ -174,7 +173,7 @@ static void spi_lld_enable(SPIDriver *spip) {
*/
static void spi_lld_disable(SPIDriver *spip) {
if (!__spi_getfield(spip, slave)) {
if ((__spi_getfield(spip, mode) & SPI_MODE_SLAVE) == 0U) {
/* Master mode, stopping gracefully.*/
/* Stopping TX DMA channel.*/
@ -266,7 +265,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 (__spi_getfield(spip, circular)) {
else if ((__spi_getfield(spip, mode) & SPI_MODE_CIRCULAR) != 0U) {
if ((flags & STM32_DMA_ISR_HTIF) != 0U) {
/* Half buffer interrupt.*/
__spi_isr_half_code(spip);
@ -686,6 +685,7 @@ void spi_lld_stop(SPIDriver *spip) {
const hal_spi_config_t *spi_lld_configure(hal_spi_driver_c *spip,
const hal_spi_config_t *config) {
uint32_t ds;
spi_mode_t mode = __spi_getfield(spip, mode);
if (config == NULL) {
config = &spi_default_config;
@ -695,23 +695,46 @@ const hal_spi_config_t *spi_lld_configure(hal_spi_driver_c *spip,
spi_lld_disable(spip);
/* Configuration-specific DMA setup.*/
spip->rxdmamode &= ~STM32_DMA_CR_SIZE_MASK;
spip->txdmamode &= ~STM32_DMA_CR_SIZE_MASK;
/* Size of the peripheral port large enough to accommodate the physical
frame size.*/
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) |
STM32_DMA_CR_PSIZE_BYTE | STM32_DMA_CR_MSIZE_BYTE;
spip->txdmamode = (spip->txdmamode & ~STM32_DMA_CR_SIZE_MASK) |
STM32_DMA_CR_PSIZE_BYTE | STM32_DMA_CR_MSIZE_BYTE;
spip->rxdmamode |= STM32_DMA_CR_PSIZE_BYTE;
spip->txdmamode |= STM32_DMA_CR_PSIZE_BYTE;
}
else {
/* Frame width is larger than 8 bits.*/
spip->rxdmamode = (spip->rxdmamode & ~STM32_DMA_CR_SIZE_MASK) |
STM32_DMA_CR_PSIZE_HWORD | STM32_DMA_CR_MSIZE_HWORD;
spip->txdmamode = (spip->txdmamode & ~STM32_DMA_CR_SIZE_MASK) |
STM32_DMA_CR_PSIZE_HWORD | STM32_DMA_CR_MSIZE_HWORD;
spip->rxdmamode |= STM32_DMA_CR_PSIZE_HWORD;
spip->txdmamode |= STM32_DMA_CR_PSIZE_HWORD;
}
if (__spi_getfield(spip, circular)) {
/* Size of the memory port as specified in the configuration. This may
result in data truncation or zero-padding if the peripheral and memory
port sizes differ.
The behavior is also different between DMAv1 and DMAv2.*/
switch (mode & SPI_MODE_FSIZE_MASK) {
case SPI_MODE_FSIZE_8:
spip->rxdmamode |= STM32_DMA_CR_MSIZE_BYTE;
spip->txdmamode |= STM32_DMA_CR_MSIZE_BYTE;
break;
case SPI_MODE_FSIZE_16:
spip->rxdmamode |= STM32_DMA_CR_MSIZE_HWORD;
spip->txdmamode |= STM32_DMA_CR_MSIZE_HWORD;
break;
case SPI_MODE_FSIZE_32:
spip->rxdmamode |= STM32_DMA_CR_MSIZE_WORD;
spip->txdmamode |= STM32_DMA_CR_MSIZE_WORD;
break;
default:
/* Unsupported mode.*/
return NULL;
}
if ((mode & SPI_MODE_CIRCULAR) != 0U) {
spip->rxdmamode |= (STM32_DMA_CR_CIRC | STM32_DMA_CR_HTIE);
spip->txdmamode |= (STM32_DMA_CR_CIRC | STM32_DMA_CR_HTIE);
}

View File

@ -241,7 +241,8 @@ msg_t spiStartIgnoreI(void *ip, size_t n) {
osalDbgCheck((self != NULL) && (n > 0U));
#if SPI_SUPPORTS_CIRCULAR
osalDbgCheck((__spi_getfield(self, circular) == false) || ((n & 1U) == 0U));
osalDbgCheck(((__spi_getfield(self, mode) & SPI_MODE_CIRCULAR) == 0U) ||
((n & 1U) == 0U));
#endif
osalDbgAssert(self->state == HAL_DRV_STATE_READY, "not ready");
@ -314,7 +315,8 @@ 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_getfield(self, circular) == false) || ((n & 1U) == 0U));
osalDbgCheck(((__spi_getfield(self, mode) & SPI_MODE_CIRCULAR) == 0U) ||
((n & 1U) == 0U));
#endif
osalDbgAssert(self->state == HAL_DRV_STATE_READY, "not ready");
@ -388,7 +390,8 @@ msg_t spiStartSendI(void *ip, size_t n, const void *txbuf) {
osalDbgCheck((self != NULL) && (n > 0U) && (txbuf != NULL));
#if SPI_SUPPORTS_CIRCULAR
osalDbgCheck((__spi_getfield(self, circular) == false) || ((n & 1U) == 0U));
osalDbgCheck(((__spi_getfield(self, mode) & SPI_MODE_CIRCULAR) == 0U) ||
((n & 1U) == 0U));
#endif
osalDbgAssert(self->state == HAL_DRV_STATE_READY, "not ready");
@ -460,7 +463,8 @@ msg_t spiStartReceiveI(void *ip, size_t n, void *rxbuf) {
osalDbgCheck((self != NULL) && (n > 0U) && (rxbuf != NULL));
#if SPI_SUPPORTS_CIRCULAR
osalDbgCheck((__spi_getfield(self, circular) == false) || ((n & 1U) == 0U));
osalDbgCheck(((__spi_getfield(self, mode) & SPI_MODE_CIRCULAR) == 0U) ||
((n & 1U) == 0U));
#endif
osalDbgAssert(self->state == HAL_DRV_STATE_READY, "not ready");