Skip to content

Commit 0947214

Browse files
committed
Fix STM32 spi_abort_asynch function
- add RX cleanup after SPI re-initialization, as it isn't implemented in the `HAL_SPI_Init` - cancel SPI enabling for 3-wire mode
1 parent c3deed6 commit 0947214

File tree

1 file changed

+18
-2
lines changed

1 file changed

+18
-2
lines changed

targets/TARGET_STM/stm_spi_api.c

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,17 @@ extern HAL_StatusTypeDef HAL_SPIEx_FlushRxFifo(SPI_HandleTypeDef *hspi);
8181
#define HAS_32BIT_SPI_TRANSFERS 1
8282
#endif // SPI_DATASIZE_X
8383

84+
/**
85+
* Flush RX FIFO/input register of SPI interface and clear overrun flag.
86+
*/
87+
static inline void spi_flush_rx(spi_t *obj)
88+
{
89+
#if defined(SPI_FLAG_FRLVL)
90+
HAL_SPIEx_FlushRxFifo(&(SPI_S(obj)->handle));
91+
#endif
92+
LL_SPI_ClearFlag_OVR(SPI_INST(obj));
93+
}
94+
8495
void spi_get_capabilities(PinName ssel, bool slave, spi_capabilities_t *cap)
8596
{
8697
if (slave) {
@@ -1373,10 +1384,15 @@ void spi_abort_asynch(spi_t *obj)
13731384
NVIC_DisableIRQ(irq_n);
13741385

13751386
// clean-up
1376-
__HAL_SPI_DISABLE(handle);
1387+
LL_SPI_Disable(SPI_INST(obj));
13771388
HAL_SPI_DeInit(handle);
13781389
HAL_SPI_Init(handle);
1379-
__HAL_SPI_ENABLE(handle);
1390+
// cleanup input buffer
1391+
spi_flush_rx(obj);
1392+
// enable SPI back if it isn't 3-wire mode
1393+
if (handle->Init.Direction != SPI_DIRECTION_1LINE) {
1394+
LL_SPI_Enable(SPI_INST(obj));
1395+
}
13801396
}
13811397

13821398
#endif //DEVICE_SPI_ASYNCH

0 commit comments

Comments
 (0)