Skip to content

platform: stm32: fix SPI DMA timeout underflow#3116

Open
morleman1 wants to merge 2 commits into
analogdevicesinc:mainfrom
morleman1:fix-stm32-spi-dma-timeout
Open

platform: stm32: fix SPI DMA timeout underflow#3116
morleman1 wants to merge 2 commits into
analogdevicesinc:mainfrom
morleman1:fix-stm32-spi-dma-timeout

Conversation

@morleman1
Copy link
Copy Markdown

This fixes an issue where an integer underflow in the timeout loop masks DMA failures and creates a race condition that reports false timeouts.

Resolves #3115

Pull Request Description

Problem

The loop while (timeout--) in stm32_spi_transfer_dma() causes two issues:

  1. Integer Underflow: If DMA stalls, timeout decrements past 0 to 0xFFFFFFFF. The final if (timeout == 0) check fails, and the function incorrectly returns 0 (Success).
  2. Race Condition: If DMA completes during the very last iteration, the loop breaks but timeout is already 0, causing a false -ETIME return.

Solution

  • Changed loop condition to while (timeout > 0) to prevent underflow.
  • Verified completion using the actual hardware flag !sdesc->stm32_spi_dma_done instead of the counter.

Testing

Verified via hardware debugging on STM32 with an ADIN1110 by simulating a DMA stall. The driver now correctly catches the stall and returns -ETIME.

PR Type

  • Bug fix (change that fixes an issue)
  • New feature (change that adds new functionality)
  • Breaking change (has dependencies in other repos or will cause CI to fail)

PR Checklist

  • I have followed the Coding style guidelines
  • I have complied with the Submission Checklist
  • I have performed a self-review of the changes
  • I have commented my code, at least hard-to-understand parts
  • I have build all projects affected by the changes in this PR
  • I have tested in hardware affected projects, at the relevant boards
  • I have signed off all commits from this PR
  • I have updated the documentation (wiki pages, ReadMe etc), if applies

This fixes an issue where an integer underflow in the timeout loop
masks DMA failures and creates a race condition that reports false
timeouts.

Resolves analogdevicesinc#3115

Signed-off-by: Max Orlemann <maxorlemann@gmail.com>
@CLAassistant
Copy link
Copy Markdown

CLAassistant commented Jun 1, 2026

CLA assistant check
All committers have signed the CLA.

amiclaus
amiclaus previously approved these changes Jun 2, 2026
@buha
Copy link
Copy Markdown
Contributor

buha commented Jun 2, 2026

Thanks for catching it and submitting the fix. While you are at it, and because they seem identical, could you fix the same issue in xspi implementation ? You can put the xspi change in the same commit as they are tightly related.
https://github.com/analogdevicesinc/no-OS/blob/main/drivers/platform/stm32/stm32_xspi.c#L696

Comment thread drivers/platform/stm32/stm32_spi.c Outdated
Comment thread drivers/platform/stm32/stm32_spi.c Outdated
if (timeout == 0)
/* Fix: Check the actual completion flag instead of the counter */
if (!sdesc->stm32_spi_dma_done) {
/* Optional: Add HAL_DMA_Abort_IT() or similar cleanup here */
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

remove this comment as well

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i reverted to the original comment /* need some cleanup here? */, as a cleanup still should be implemented similar to the xspi implementation. here it is more complex, because of seperate handles for rx and txdma_ch

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You are right, just squash the commits for now into a single commit and leave the original question there. We can deal with that separately in another PR and merge this one.

Remove comments

fix same issues in stm32_xspi.c
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Bug in stm32 SPI DMA driver: Integer underflow in timeout masks DMA fault

4 participants