Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DMA SPI support for STM32 devices #162

Merged
merged 21 commits into from
Nov 3, 2023
Merged

Conversation

multiplemonomials
Copy link
Collaborator

@multiplemonomials multiplemonomials commented May 23, 2023

Summary of changes

This MR adds a feature that many have been requesting for years: the ability to run SPI transfers in the background via DMA on STM32 devices. I based it on a test implementation for STM32L4 by @ABOSTM (also credit @wdx04's BurstSPI library). Luckily, these branches include most of the actual DMA code needed to make SPI work. The hard part is that the ABOSTM code only works for one specific device family, STM32L4. There are also some major bugs with how the interrupts are set up, so I don't think it actually worked.

So, to adapt that code for other STM devices, I added a new "stm_dma_utils.c" layer which abstracts the SPI code from the processor-to-processor differences. I also moved configuration into a new "stm_dma_info.h" header which is different for each device family and contains the DMA channel mappings.

Now, with this code, you can enable DMA with SPI::setDMAUsage(), and the next SPI transfer after the DMA usage hint is set will allocate the needed DMA channels and structures.

Lastly, I added a new SPI::transfer_and_wait() function to match the I2C::transfer_and_wait() function added a while ago.

** Still TODOs:**

  • Add stm_dma_info.h for most remaining processor targets
  • Run more tests

Impact of changes

STM32 devices will now support DMA.

Migration actions required

None

Documentation

None

Pull request type

[] Patch update (Bug fix / Target update / Docs update / Test update / Refactor)
[X] Feature update (New feature / Functionality change / New API)
[] Major update (Breaking change E.g. Return code change / API behaviour change)

Test results

[] No Tests required for this change (E.g docs only update)
[] Covered by existing mbed-os tests (Greentea or Unittest)
[X] Tests / results supplied as part of this PR

Currently I have tested this locally on STM32L452 and STM32H743ZI. Pending testing on other boards.


Reviewers


JohnK1987
JohnK1987 previously approved these changes Jul 15, 2023
drivers/source/SPI.cpp Outdated Show resolved Hide resolved
@multiplemonomials
Copy link
Collaborator Author

This is now soooooo close to being ready to merge. I have tested it on STM32F4, STM32L4, and STM32H7, and it works for basic transfers and to initialize an SD card!

There is just one significant issue, which is that on STM32H7, calling abort_transfer() causes every subsequent transfer to be corrupt. I shall look into that. Also I should prob test on STM32U5. But still, never been closer!

@JohnK1987
Copy link
Member

JohnK1987 commented Sep 1, 2023

Is it ready or still in ongoing?

@multiplemonomials
Copy link
Collaborator Author

ok @JohnK1987 I think I'm good to merge this as an initial attempt now. I just pushes a commit which fixes hang issues on STM32F2, F4, and F7. It's hard to tell if it's actually perfect or not (because my bus pirate is broken now) but I think it's mostly working on most devices and I don't want to squeeze any more changes into one MR. I will open tickets to investigate the abort_transfer() issue and also investigate issues on STM32U5!

@multiplemonomials multiplemonomials changed the title [draft] DMA SPI support for STM32 devices DMA SPI support for STM32 devices Nov 3, 2023
@multiplemonomials multiplemonomials merged commit 28815b1 into master Nov 3, 2023
9 checks passed
@multiplemonomials multiplemonomials deleted the dev/stm32-dma-spi branch November 3, 2023 03:16
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants