Skip to content

Commit

Permalink
drivers: stm32: spi: Add implementation for SPI Abort
Browse files Browse the repository at this point in the history
Add SPI abort transaction implementation for STM32 platform.

Signed-off-by: Naga Himanshu Indraganti <[email protected]>
  • Loading branch information
NagaHimanshu authored and buha committed Feb 26, 2025
1 parent d3b08e2 commit bdd7f1c
Showing 1 changed file with 57 additions and 23 deletions.
80 changes: 57 additions & 23 deletions drivers/platform/stm32/stm32_spi.c
Original file line number Diff line number Diff line change
Expand Up @@ -615,22 +615,10 @@ void stm32_spi_dma_callback(struct no_os_dma_xfer_desc *old_xfer,
no_os_pwm_disable(sdesc->tx_pwm_desc);
#endif

#if defined (STM32H5)
CLEAR_BIT(sdesc->hspi.Instance->CFG1, SPI_CFG1_TXDMAEN);
#else
CLEAR_BIT(sdesc->hspi.Instance->CR2, SPI_CR2_TXDMAEN);
#endif

#if defined (STM32H5)
CLEAR_BIT(sdesc->hspi.Instance->CFG1, SPI_CFG1_RXDMAEN);
#else
CLEAR_BIT(sdesc->hspi.Instance->CR2, SPI_CR2_RXDMAEN);
#endif

no_os_dma_xfer_abort(sdesc->dma_desc, sdesc->txdma_ch);

no_os_dma_xfer_abort(sdesc->dma_desc, sdesc->rxdma_ch);
/* Perform abort SPI transfers */
no_os_spi_transfer_abort(desc);

/* Free the allocated memory for tx and rx transfers */
no_os_free(sdesc->tx_ch_xfer);
no_os_free(sdesc->rx_ch_xfer);

Expand All @@ -639,13 +627,6 @@ void stm32_spi_dma_callback(struct no_os_dma_xfer_desc *old_xfer,

sdesc->stm32_spi_dma_done = true;

/* Dummy read to clear any pending read on SPI */
#ifndef SPI_SR_RXNE
*(volatile uint8_t *)&SPIx->RXDR;
#else
*(volatile uint8_t *)&SPIx->DR;
#endif

if (sdesc->stm32_spi_dma_user_cb)
sdesc->stm32_spi_dma_user_cb(sdesc->stm32_spi_dma_user_ctx);
}
Expand Down Expand Up @@ -705,6 +686,58 @@ int32_t stm32_spi_dma_transfer_sync(struct no_os_spi_desc* desc,
return 0;
}

/**
* @brief Abort SPI transfers.
* @param desc - The SPI descriptor.
* @return 0 in case of success, errno codes otherwise.
*/
int32_t stm32_spi_transfer_abort(struct no_os_spi_desc* desc)
{
int32_t ret;
struct stm32_spi_desc* sdesc;

if (!desc->extra)
return -EINVAL;

sdesc = desc->extra;
SPI_TypeDef *SPIx = sdesc->hspi.Instance;

if (sdesc->rxdma_ch) {
ret = no_os_dma_xfer_abort(sdesc->dma_desc, sdesc->rxdma_ch);
if (ret) {
return ret;
}

#if defined (STM32H5)
CLEAR_BIT(SPIx->CFG1, SPI_CFG1_RXDMAEN);
#else
CLEAR_BIT(SPIx->CR2, SPI_CR2_RXDMAEN);
#endif
}

if (sdesc->txdma_ch) {
ret = no_os_dma_xfer_abort(sdesc->dma_desc, sdesc->txdma_ch);
if (ret) {
return ret;
}

#if defined (STM32H5)
CLEAR_BIT(SPIx->CFG1, SPI_CFG1_TXDMAEN);
#else
CLEAR_BIT(SPIx->CR2, SPI_CR2_TXDMAEN);
#endif
}

/* Dummy read to clear any pending read on SPI */
#ifndef SPI_SR_RXNE
*(volatile uint8_t *)&SPIx->RXDR;
#else
*(volatile uint8_t *)&SPIx->DR;
#endif

return 0;
}

/**
* @brief stm32 platform specific SPI platform ops structure
*/
Expand All @@ -714,5 +747,6 @@ const struct no_os_spi_platform_ops stm32_spi_ops = {
.remove = &stm32_spi_remove,
.transfer = &stm32_spi_transfer,
.dma_transfer_async = &stm32_spi_dma_transfer_async,
.dma_transfer_sync = &stm32_spi_dma_transfer_sync
.dma_transfer_sync = &stm32_spi_dma_transfer_sync,
.transfer_abort = &stm32_spi_transfer_abort
};

0 comments on commit bdd7f1c

Please sign in to comment.