spi without dma

ChibiOS public support forum for topics related to the STMicroelectronics STM32 family of micro-controllers.

Moderator: RoccoMarco

Post Reply
User avatar
alex31
Posts: 445
Joined: Fri May 25, 2012 10:23 am
Has thanked: 57 times
Been thanked: 88 times

spi without dma

Post by alex31 »

Hello
I am using Chibios 2.6.9 on a STM43F4, i need to use both pwm on tim3/tim4 (both 4 channels), and SPI2.
The cards are already done and routing cannot be changed :-(, since there is
DMA conflict between SPI2_RX and TIM4_CH2 (dma(1,3)), and on SPI2_TX and TIM3_CH1 (dma(1,4)),
is it possible to use SPI2 without DMA ?

I know that i can bitbang spi if nothing else works, but if i could avoid that ...

Thanks.

Alexandre
User avatar
Giovanni
Site Admin
Posts: 14891
Joined: Wed May 27, 2009 8:48 am
Has thanked: 1202 times
Been thanked: 996 times

Re: spi without dma

Post by Giovanni »

Hi,

It is not implemented, it could be done, I already did that for the I2C driver. If you want to try look at how the I2Cv2 driver is done in 3.0, it is not much code.

Alternatively pull from the code the function spi_lld_polled_exchange() and use that directly. You need to initialize the SPI and clocks before that, take the code from spi_lld_start() omitting the DMA/IRQ initialization.

I will look into adding a no-DMA mode at some point but right now I am focused on the 3.0 release, it is already way late.

Giovanni
User avatar
alex31
Posts: 445
Joined: Fri May 25, 2012 10:23 am
Has thanked: 57 times
Been thanked: 88 times

Re: spi without dma

Post by alex31 »

Alternatively pull from the code the function spi_lld_polled_exchange() and use that directly. You need to initialize the SPI and clocks before that, take the code from spi_lld_start() omitting the DMA/IRQ initialization.


Thanks, i will try that way.

I will look into adding a no-DMA mode at some point but right now I am focused on the 3.0 release, it is already way late.


Yes, that will bring even more flexibility to chibios, which is already a powerful tool to make mcu programming easier.

Alexandre
User avatar
alex31
Posts: 445
Joined: Fri May 25, 2012 10:23 am
Has thanked: 57 times
Been thanked: 88 times

Re: spi without dma

Post by alex31 »

In final, i have modified in place os/hal/src/spi.c and os/hal/platforms/STM32/SPIv1/spi_lld.c and it works.

I have done rough mod, where a uniq macro in mcuconf.h enable or disable use of DMA for all spi peripherals.

Sure, it would better if it where fine grained by spi peripheral ...

Anyway, thanks for the advice

Alexandre
genosensor
Posts: 65
Joined: Thu Oct 03, 2013 1:06 am
Has thanked: 1 time
Been thanked: 1 time

Re: spi without dma

Post by genosensor »

I just ran into exactly this problem myself.
Needed access to SPI2 on a board that also used I2C2.
SPI2 traffic was light, so DMA not needed -- but Chibios paniced when it couldn't allocate a DMA channel for it.

My solution was to define more legal configuration values for the STM32_SPI_USE_SPIn #defines (in mcuconf.h)
In addition to TRUE and FALSE, STM32_SPI_USE_SPIn may be set to IRQ, DMA or POLL
The updated SPI driver compiles appropriate support for each channel depending on those #defines.
This approach allows each SPI channel to be handled differently.
In my case, it was important the SPI1 continue to be DMA driven, while SPI2 was POLLed.

This is really just a logical extension of the Chibios's existing configuration strategy.

For now, I've only implemented the POLL option in the STM32v1 SPI driver.

Any reason why approach shouldn't be generalized to other device drivers?

- brent
User avatar
Giovanni
Site Admin
Posts: 14891
Joined: Wed May 27, 2009 8:48 am
Has thanked: 1202 times
Been thanked: 996 times

Re: spi without dma

Post by Giovanni »

Hi,

A polled implementation is not possible for the SPI driver, it relies on callbacks called from ISR context, a polled driver cannot do that.

The I2C driver supports already a DMA-less mode.

Giovanni
genosensor
Posts: 65
Joined: Thu Oct 03, 2013 1:06 am
Has thanked: 1 time
Been thanked: 1 time

Re: spi without dma

Post by genosensor »

Giovanni,

The patch I implemented merely makes it possible to configure some SPI channels operate with DMA and full driver support, while others operate only in a polled mode, supporting a very limited API subset.
When operating in polled mode, the SPI driver only does initialization and supports (only):

spi_lld_polled_exchange()

to exchange each SPI frame.

(This may not be possible, but, it is tested and works here :-)

I made no changes to the I2C driver.
This is in v2.6
Henk Grobbelaar
Posts: 1
Joined: Fri Apr 12, 2024 7:35 am

Re: spi without dma

Post by Henk Grobbelaar »

This is an old thread, I could not find anything newer regarding this issue.
I have a problem where when I use SPI2, I cannot write to either USART3 and UART4, I suspect DMA conflict. The device on SPI2 is low data, maybe read once an hour. But UART3 and 4 will be used extensively and cannot be closed every time I need to use SPI2.
Is there a way to use SPI2 without DMA and SPI1 with DMA?

I see in the previous replies some people came up with solutions, is there any way that can be shared? I'd rather not reinvent the wheel if I don't have to.

For added reference, I'm using Chibios in nanoframework, custom board using STM32F405V
User avatar
Giovanni
Site Admin
Posts: 14891
Joined: Wed May 27, 2009 8:48 am
Has thanked: 1202 times
Been thanked: 996 times

Re: spi without dma

Post by Giovanni »

Hi,

The STM32F4 does not have a DMAMUX so peripherals use specific DMA channels, you need to check the Reference Manual and see if there are alternative channels that can be used in your case. There is a table with mapping in the DMA chapter.

Giovanni
User avatar
alex31
Posts: 445
Joined: Fri May 25, 2012 10:23 am
Has thanked: 57 times
Been thanked: 88 times

Re: spi without dma

Post by alex31 »

USART3_TX : [stream(1, 3) C4]
USART3_TX : [stream(1, 4) C7]
USART3_RX : [stream(1, 1) C4]

UART4_RX : [stream(1, 2) C4]
UART4_TX : [stream(1, 4) C4]

SPI2_RX : [stream(1, 3) C0]
SPI2_TX : [stream(1, 4) C0]

Unfortunately, there is no solution with the very limited F4 dma fixed stuff.
New hardware designs should use DMAMUX fitted models which help a lot.

In you case, if you have little traffic on SPI, you can use spiPolledExchange API in a loop.

Alexandre
Post Reply