Home | Projects | Notes > Direct Memory Access (DMA) > DMA Programming - Generic Steps to Follow
Identify the DMAx controller suitable for your application (Capability of each DMAx supported by your MCU may vary)
Initialize the DMA
Trigger the DMA (Manual or automatic trigger)
Manual trigger - Kick starting the DMA engine with the help of a processor core
Automatic trigger (Desirable) - Kick starting the DMA engine with help of a peripheral but not the processor core
Wait for Transmission Complete (TC) flag (i.e., polling) or get callback from DMA driver (i.e., interrupt)
DMA Handle
The DMA handle is a data structure provided by the DMA HAL driver, used by the application to manage the DMA controller of the MCU.
xxxxxxxxxx
41/* main.c */
2
3/* Private variables ---------------------------------------------------------*/
4DMA_HandleTypeDef hdma_memtomem_dma2_stream0;
xxxxxxxxxx
221/* stm32f4xx_hal_dma.h */
2
3/**
4 * @brief DMA handle Structure definition
5 */
6typedef struct __DMA_HandleTypeDef
7{
8 DMA_Stream_TypeDef *Instance; /*!< Register base address */
9 DMA_InitTypeDef Init; /*!< DMA communication parameters */
10 HAL_LockTypeDef Lock; /*!< DMA locking object */
11 __IO HAL_DMA_StateTypeDef State; /*!< DMA transfer state */
12 void *Parent; /*!< Parent object state */
13 void (* XferCpltCallback)( struct __DMA_HandleTypeDef * hdma); /*!< DMA transfer complete callback */
14 void (* XferHalfCpltCallback)( struct __DMA_HandleTypeDef * hdma); /*!< DMA Half transfer complete callback */
15 void (* XferM1CpltCallback)( struct __DMA_HandleTypeDef * hdma); /*!< DMA transfer complete Memory1 callback */
16 void (* XferM1HalfCpltCallback)( struct __DMA_HandleTypeDef * hdma); /*!< DMA transfer Half complete Memory1 callback */
17 void (* XferErrorCallback)( struct __DMA_HandleTypeDef * hdma); /*!< DMA transfer error callback */
18 void (* XferAbortCallback)( struct __DMA_HandleTypeDef * hdma); /*!< DMA transfer Abort callback */
19 __IO uint32_t ErrorCode; /*!< DMA Error code */
20 uint32_t StreamBaseAddress; /*!< DMA Stream Base Address */
21 uint32_t StreamIndex; /*!< DMA Stream Index */
22}DMA_HandleTypeDef;
DMA Initialization
MX_DMA_Init()
function is auto-generated by the STM32CubeMX based on our configuration.
xxxxxxxxxx
31/* main.c */
2
3static void MX_DMA_Init(void);
xxxxxxxxxx
321/* main.c */
2
3/**
4 * Enable DMA controller clock
5 * Configure DMA for memory to memory transfers
6 * hdma_memtomem_dma2_stream0
7 */
8static void MX_DMA_Init(void)
9{
10
11 /* DMA controller clock enable */
12 __HAL_RCC_DMA2_CLK_ENABLE();
13
14 /* Configure DMA request hdma_memtomem_dma2_stream0 on DMA2_Stream0 */
15 hdma_memtomem_dma2_stream0.Instance = DMA2_Stream0;
16 hdma_memtomem_dma2_stream0.Init.Channel = DMA_CHANNEL_0;
17 hdma_memtomem_dma2_stream0.Init.Direction = DMA_MEMORY_TO_MEMORY;
18 hdma_memtomem_dma2_stream0.Init.PeriphInc = DMA_PINC_DISABLE;
19 hdma_memtomem_dma2_stream0.Init.MemInc = DMA_MINC_DISABLE;
20 hdma_memtomem_dma2_stream0.Init.PeriphDataAlignment = DMA_PDATAALIGN_BYTE;
21 hdma_memtomem_dma2_stream0.Init.MemDataAlignment = DMA_MDATAALIGN_BYTE;
22 hdma_memtomem_dma2_stream0.Init.Mode = DMA_NORMAL;
23 hdma_memtomem_dma2_stream0.Init.Priority = DMA_PRIORITY_LOW;
24 hdma_memtomem_dma2_stream0.Init.FIFOMode = DMA_FIFOMODE_ENABLE;
25 hdma_memtomem_dma2_stream0.Init.FIFOThreshold = DMA_FIFO_THRESHOLD_FULL;
26 hdma_memtomem_dma2_stream0.Init.MemBurst = DMA_MBURST_SINGLE;
27 hdma_memtomem_dma2_stream0.Init.PeriphBurst = DMA_PBURST_SINGLE;
28 if (HAL_DMA_Init(&hdma_memtomem_dma2_stream0) != HAL_OK)
29 {
30 Error_Handler( );
31 }
32}