一、前言
DMA会在不同的寄存器/ram/存储设备之间建立通道,自动传输数据,以达到解放CPU的目的。
比如你想用DAC模块去输出一段特定的波形,就要让CPU将预设的数值不断写入DAC的寄存器。这时CPU被DAC任务长期占用,系统处理其他任务和响应其他事件的能力被大幅降低。
在实际应用里,经常有一些繁重的读写操作。这些操作不需要经过计算,却依然占用了大量的CPU资源,遇到这种情况就要考虑使用DMA了。
我开发板上的stm芯片上共有7个dma通道,它可以建立7个DMA连接。但是DMA控制器只有一个,所以同时只能有一个DMA连接被相应。
二、DMA的初始化
针对每一个DMA频道,都要初始化它的控制寄存器,来看一下DMA的init结构体的原型:
/**
* @brief DMA Configuration Structure definition
*/
typedef struct
{
uint32_t Direction; /*!< Specifies if the data will be transferred from memory to peripheral,
from memory to memory or from peripheral to memory.
This parameter can be a value of @ref DMA_Data_transfer_direction */
uint32_t PeriphInc; /*!< Specifies whether the Peripheral address register should be incremented or not.
This parameter can be a value of @ref DMA_Peripheral_incremented_mode */
uint32_t MemInc; /*!< Specifies whether the memory address register should be incremented or not.
This parameter can be a value of @ref DMA_Memory_incremented_mode */
uint32_t PeriphDataAlignment; /*!< Specifies the Peripheral data width.
This parameter can be a value of @ref DMA_Peripheral_data_size */
uint32_t MemDataAlignment; /*!< Specifies the Memory data width.
This parameter can be a value of @ref DMA_Memory_data_size */
uint32_t Mode; /*!< Specifies the operatio