M to M编程过程
1 定义数据
/* Step1 定义数据目标源和数据接收源 *********************************************/
/* 定义aSRC_Const_Buffer数组作为DMA传输数据源
* const关键字将aSRC_Const_Buffer数组变量定义为常量类型
* 表示数据存储在内部的FLASH中
*/
const uint32_t aSRC_Const_Buffer[BUFFER_SIZE]= {
0x01020304,0x05060708,0x090A0B0C,0x0D0E0F10,
0x11121314,0x15161718,0x191A1B1C,0x1D1E1F20,
0x21222324,0x25262728,0x292A2B2C,0x2D2E2F30,
0x31323334,0x35363738,0x393A3B3C,0x3D3E3F40,
0x41424344,0x45464748,0x494A4B4C,0x4D4E4F50,
0x51525354,0x55565758,0x595A5B5C,0x5D5E5F60,
0x61626364,0x65666768,0x696A6B6C,0x6D6E6F70,
0x71727374,0x75767778,0x797A7B7C,0x7D7E7F80};
/* 定义DMA传输目标存储器
* 存储在内部的SRAM中
*/
uint32_t aDST_Buffer[BUFFER_SIZE];
2 初始化DMA初始化结构体
/* Step2 初始化DMA结构体 ******************************************************/
void MtM_DMA_Config(void)
{
DMA_InitTypeDef DMA_InitStruct;
// 开时钟,我经常会忘记这一步(DMA挂载在AHB总线)
RCC_AHBPeriphClockCmd(MTM_DMA_CLK, ENABLE);
// 配置初始化结构体
DMA_InitStruct.DMA_PeripheralBaseAddr = (uint32_t)aSRC_Const_Buffer;
DMA_InitStruct.DMA_MemoryBaseAddr = (uint32_t)aDST_Buffer;
/* DMA_DIR_PeripheralSRC:外设为源,DMA_DIR_PeripheralDST:外设为目的地 */
DMA_InitStruct.DMA_DIR = DMA_DIR_PeripheralSRC;
DMA_InitStruct.DMA_BufferSize = BUFFER_SIZE;
DMA_InitStruct.DMA_PeripheralInc = DMA_PeripheralInc_Enable;
DMA_InitStruct.DMA_MemoryInc = DMA_MemoryInc_Enable;
DMA_InitStruct.DMA_PeripheralDataSize = DMA_PeripheralDataSize_Word;
DMA_InitStruct.DMA_MemoryDataSize = DMA_MemoryDataSize_Word;
DMA_InitStruct.DMA_Mode = DMA_Mode_Normal;
DMA_InitStruct.DMA_Priority = DMA_Priority_Medium;
DMA_InitStruct.DMA_M2M = DMA_M2M_Enable;
DMA_Init(MTM_DMA_CHANNEL, &DMA_InitStruct);
DMA_Cmd(MTM_DMA_CHANNEL, ENABLE);
}
3 比较传输是否正确(比较两个数组是否相等)
/**
* @brief 用于判断两个数组是否相等(1 相等 0 不等)
*/
uint8_t Buffercmp(const uint32_t *buf1, uint32_t *buf2, uint16_t buf_len)
{
uint8_t result = 1;
while (buf_len--)
{
if (*buf1 != *buf2)
{
result = 0;
break;
}
buf1++;
buf2++;
}
return result;
}
4 在主函数中进行调用
main.c
/* main.c */
#include "stm32f10x.h"
#include "bsp_led.h"
#include "bsp_dma.h"
void delay(uint32_t count);
int main(void)
{
uint8_t status = 0;
LED_GPIO_Config();
LED_YELLOW;
delay(0XFFFFFF);
MtM_DMA_Config();
// 等待DMA传输完成
while (DMA_GetFlagStatus(DMA1_FLAG_TC6) != SET);
status = Buffercmp(aSRC_Const_Buffer, aDST_Buffer, BUFFER_SIZE);
if (status == 0)
{
LED_RED;
}
else
{
LED_GREEN;
}
while (1)
{
}
}
void delay(uint32_t count)
{
for (; count != 0; count--);
}
bsp_dma.c
#include "bsp_dma.h"
/* Step1 定义数据目标源和数据接收源 *********************************************/
/* 定义aSRC_Const_Buffer数组作为DMA传输数据源
* const关键字将aSRC_Const_Buffer数组变量定义为常量类型
* 表示数据存储在内部的FLASH中
*/
const uint32_t aSRC_Const_Buffer[BUFFER_SIZE]= {
0x01020304,0x05060708,0x090A0B0C,0x0D0E0F10,
0x11121314,0x15161718,0x191A1B1C,0x1D1E1F20,
0x21222324,0x25262728,0x292A2B2C,0x2D2E2F30,
0x31323334,0x35363738,0x393A3B3C,0x3D3E3F40,
0x41424344,0x45464748,0x494A4B4C,0x4D4E4F50,
0x51525354,0x55565758,0x595A5B5C,0x5D5E5F60,
0x61626364,0x65666768,0x696A6B6C,0x6D6E6F70,
0x71727374,0x75767778,0x797A7B7C,0x7D7E7F80};
/* 定义DMA传输目标存储器
* 存储在内部的SRAM中
*/
uint32_t aDST_Buffer[BUFFER_SIZE];
/* Step2 初始化DMA结构体 ******************************************************/
void MtM_DMA_Config(void)
{
DMA_InitTypeDef DMA_InitStruct;
// 开时钟,我经常会忘记这一步(DMA挂载在AHB总线)
RCC_AHBPeriphClockCmd(MTM_DMA_CLK, ENABLE);
// 配置初始化结构体
DMA_InitStruct.DMA_PeripheralBaseAddr = (uint32_t)aSRC_Const_Buffer;
DMA_InitStruct.DMA_MemoryBaseAddr = (uint32_t)aDST_Buffer;
/* DMA_DIR_PeripheralSRC:外设为源,DMA_DIR_PeripheralDST:外设为目的地 */
DMA_InitStruct.DMA_DIR = DMA_DIR_PeripheralSRC;
DMA_InitStruct.DMA_BufferSize = BUFFER_SIZE;
DMA_InitStruct.DMA_PeripheralInc = DMA_PeripheralInc_Enable;
DMA_InitStruct.DMA_MemoryInc = DMA_MemoryInc_Enable;
DMA_InitStruct.DMA_PeripheralDataSize = DMA_PeripheralDataSize_Word;
DMA_InitStruct.DMA_MemoryDataSize = DMA_MemoryDataSize_Word;
DMA_InitStruct.DMA_Mode = DMA_Mode_Normal;
DMA_InitStruct.DMA_Priority = DMA_Priority_Medium;
DMA_InitStruct.DMA_M2M = DMA_M2M_Enable;
DMA_Init(MTM_DMA_CHANNEL, &DMA_InitStruct);
DMA_Cmd(MTM_DMA_CHANNEL, ENABLE);
}
/**
* @brief 用于判断两个数组是否相等(1 相等 0 不等)
*/
uint8_t Buffercmp(const uint32_t *buf1, uint32_t *buf2, uint16_t buf_len)
{
uint8_t result = 1;
while (buf_len--)
{
if (*buf1 != *buf2)
{
result = 0;
break;
}
buf1++;
buf2++;
}
return result;
}
bsp_dma.h
#ifndef __BSP_DMA_H
#define __BSP_DMA_H
#include "stm32f10x.h"
// 要发送的数据大小
#define BUFFER_SIZE 32
#define MTM_DMA_CLK RCC_AHBPeriph_DMA1
#define MTM_DMA_CHANNEL DMA1_Channel6
extern const uint32_t aSRC_Const_Buffer[BUFFER_SIZE];
extern uint32_t aDST_Buffer[BUFFER_SIZE];
void MtM_DMA_Config(void);
uint8_t Buffercmp(const uint32_t *buf1, uint32_t *buf2, uint16_t buf_len);
#endif /* __BSP_DMA_H */