SPI1 and SPI2 DMA conflict?

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

Moderators: RoccoMarco, barthess

Xela
Posts: 15
Joined: Thu Sep 07, 2017 12:05 pm
Has thanked: 3 times

SPI1 and SPI2 DMA conflict?

Postby Xela » Thu Jan 11, 2018 5:01 pm

I have a couple of threads reading from different devices, one is on SPI1 and the other is SPI2. My code is crashing and after doing some reading I am beginning to think the conflict is in the DMA.

There are no shared resources between threads and they won't work unless I reduce the priority of one of them. I also read through some code and it looks to me as though SPI1 and SPI2 are already configured to use different DMA channels so perhaps the entire DMA controller is being monopolized...?

The following is how I am reading via SPI (similar for writing):

Code: Select all

int mpu_readfromspi(SPIDriver *spip, uint8_t headerLength, const uint8_t *headerBuffer, uint8_t readlength, uint8_t *readBuffer){
  spiAcquireBus(spip);                  /* Acquire ownership of the bus.    */
  spiSelect(spip);                      /* Slave Select assertion.          */
  spiSend(spip, headerLength, headerBuffer);    /* Slave Register select      */
  spiReceive(spip, readlength, readBuffer);  /* Read operation.         */
  spiUnselect(spip);                    /* Slave Select de-assertion.       */
  spiReleaseBus(spip);                  /* Ownership release.               */
  return MPU_SUCCESS;
}


Code: Select all

int dw_readfromspi(SPIDriver *spip, uint16 headerLength, const uint8 *headerBuffer, uint32 readlength, uint8 *readBuffer){
    spiAcquireBus(spip);                    /* Acquire ownership of the bus.    */
    spiSelect(spip);                        /* Slave Select assertion.          */
    spiSend(spip, headerLength, headerBuffer);     /* Slave Register select        */
    spiReceive(spip, readlength, readBuffer);  /* Read operation.              */
    spiUnselect(spip);                      /* Slave Select de-assertion.       */
    spiReleaseBus(spip);                    /* Ownership release.               */
   return DWT_SUCCESS;
}


I have read some years old comments about also doing spiStart and spiStop within each SPI routine.
I can implement that if it is necessary but I have to rewrite a bunch of code to pass in the speed configuration. I have a different SPI speed configurations for device initialization and main loop. In both cases the same SPI routine is used and I just call an SPI init function to switch the speeds.

All I know about the DMA is that there are two controllers with 8 channels each (stm32f407).

So I am not entirely sure if I should be trying to configure each SPI to use a non-conflicting DMA controller or channel or be trying to make them play nice on the same channel/controller.

I will be moving on to integrating a third device that is on SPI3 so I assume that has to be considered as well.

Am I at least thinking in the right direction here? It is correct there will be DMA collisions if I do not do something about it? And that I need to call spiStop to free up the shared resources?

Also, is avoiding the use of DMA an option (some of the forum thread comments I have read in this regard are years old)?

Cheers
Alex

User avatar
Giovanni
Site Admin
Posts: 14455
Joined: Wed May 27, 2009 8:48 am
Location: Salerno, Italy
Has thanked: 1076 times
Been thanked: 922 times
Contact:

Re: SPI1 and SPI2 DMA conflict?

Postby Giovanni » Thu Jan 11, 2018 5:09 pm

Hi,

You have two things to verify:
1) DMA settings in mcuconf.h.
2) Compatible DMA channels in the STM32 Reference Manual.

Note that if assertions are enabled in chconf.h then DMA conflicts are immediately detected.

About that code, if SPIs are configured at very high speed and buffers are too small then you could be getting an IRQ saturation, the CPU is not able to serve all IRQs and the behavior become unpredictable. Threads at lower priority could never have a chance to run.

Giovanni


Return to “STM32 Support”

Who is online

Users browsing this forum: Google [Bot] and 14 guests