Skip to main content
Visitor II
July 1, 2024
Question

Is This Safe !! DMA??

  • July 1, 2024
  • 2 replies
  • 872 views

Hello all

 

I am interfacing various peripherals on the STM42F446RE, SPI, I2C, UART etc,  and in the process i'm choosing to use the DMA controller.

when configuring various 3rd party ICs some may have many registers to setup,

 

Rather than use global array variables eating up resorces of the ARM, is it safe to use local variables within the peripherals DMA setup when calling HAL_SPI_DMA functions ??

 

void setup_spi (void)

{

#define array 24

uint8_t    spi_tx[array] = { 10, 20, 30, 40, 50, 60, 70 };

 

 HAL_SPI_Transmit_DMA( &hspi1, spi_tx, array );

 

}

 

my concern with the above is..., does the data get lost when exiting the function before it is written to the DMA ??

 

is this ok to do this ??? or should I use global variables when writing to the DMA

 

Thank in advance for any info

 

 

 

 

 

 

 

 

 

 

 

    This topic has been closed for replies.

    2 replies

    Graduate II
    July 3, 2024

    As you rightly suspected, it's prone to  failure at some point in time, since its local variable.  In case your RAM is fully utilised,  you can keep couple of fixed buffers and switch them as you do the coding for different purposes.  It all depends on your logic design. 

    Graduate II
    July 3, 2024

    If you allow the context to collapse it's not going to be safe/usable.

    If this is a constant pattern use FLASH, and DMA out of that.

    If the routine doesn't re-enter you can define the array as static within the function/scope and it will make it global. This is safer, and you can't have multiple DMA transactions in-flight any way.