本文博客链接:http://blog.csdn.net/jdh99,作者:jdh,
环境: 主机:WIN8 开发环境:MDK5.13 mcu: stm32f407VGT6
说明: 之前用STM32F103实现DMA收发串口数据,现在项目中采用STM32F407,所以将此机制移植到F4上。 STM32F103上用DMA收发串口数据**: STM32的串口采用DMA方式发送数据测试 STM32的串口采用DMA方式接收数据测试
源代码: 串口初始化代码:
/*********************************************************************
* 初始化串口
**********************************************************************/
static void init_uart(void)
{
//定义中断结构体
NVIC_InitTypeDef NVIC_InitStructure ;
//定义IO初始化结构体
GPIO_InitTypeDef GPIO_InitStructure;
//定义串口结构体
USART_InitTypeDef USART_InitStructure;
//定义DMA结构体
DMA_InitTypeDef DMA_InitStructure;
//打开串口对应的外设时钟
RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2, ENABLE);
//串口发DMA配置
//启动DMA时钟
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_DMA1, ENABLE);
//DMA发送中断设置
NVIC_InitStructure.NVIC_IRQChannel = DMA1_Stream6_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 2;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
//DMA通道配置
DMA_DeInit(DMA1_Stream6);
DMA_InitStructure.DMA_Channel = DMA_Channel_4;
//外设地址
DMA_InitStructure.DMA_PeripheralBaseAddr = (uint32_t)(&USART2->DR);
//内存地址
DMA_InitStructure.DMA_Memory0BaseAddr = (uint32_t)Tx_Buf_Gsm;
//dma传输方向
DMA_InitStructure.DMA_DIR = DMA_DIR_MemoryToPeripheral;
//设置DMA在传输时缓冲区的长度
DMA_InitStructure.DMA_BufferSize = TX_LEN_GSM;
//设置DMA的外设递增模式,一个外设
DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable;
//设置DMA的内存递增模式
DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable;
//外设数据字长
DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_Byte;
//内存数据字长
DMA_InitStructure.DMA_MemoryDataSize = DMA_PeripheralDataSize_Byte;
//设置DMA的传输模式
DMA_InitStructure.DMA_Mode = DMA_Mode_Normal;
//设置DMA的优先级别
DMA_InitStructure.DMA_Priority = DMA_Priority_High;
//指定如果FIFO模式或直接模式将用于指定的流 : 不使能FIFO模式
DMA_InitStructure.DMA_FIFOMode = DMA_FIFOMode_Disable;
//指定了FIFO阈值水平
DMA_InitStructure.DMA_FIFOThreshold = DMA_FIFOThreshold_HalfFull;
//指定的Burst转移配置内存传输
DMA_InitStructure.DMA_MemoryBurst = DMA_MemoryBurst_Single;
//指定的Burst转移配置外围转移 */
DMA_InitStructure.DMA_PeripheralBurst = DMA_PeripheralBurst_Single;
//配置DMA1的通道
DMA_Init(DMA1_Stream6, &DMA_InitStructure);
//使能中断
DMA_ITConfig(DMA1_Stream6,DMA_IT_TC,ENABLE);
//串口收DMA配置
//启动DMA时钟
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_DMA1, ENABLE);
//DMA通道配置
DMA_DeInit(DMA1_Stream5);
DMA_InitStructure.DMA_Channel = DMA_Channel_4;
//外设地址
DMA_InitStructure.DMA_PeripheralBaseAddr = (uint32_t)(&USART2->DR);
//内存地址
DMA_InitStructure.DMA_Memory0BaseAddr = (uint32_t)Rx_Buf_Gsm;
//dma传输方向
DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralToMemory;
//设置DMA在传输时缓冲区的长度
DMA_InitStructure.DMA_BufferSize = RX_LEN_GSM;
//设置DMA的外设递增模式,一个外设
DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable;
//设置DMA的内存递增模式
DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable;
//外设数据字长
DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_Byte;
//内存数据字长
DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_Byte;
//设置DMA的传输模式
DMA_InitStructure.DMA_Mode = DMA_Mode_Normal;
//设置DMA的优先级别
DMA_InitStructure.DMA_Priority = DMA_Priority_VeryHigh;
//指定如果FIFO模式或直接模式将用于指定的流 : 不使能FIFO模式
DMA_InitStructure.DMA_FIFOMode = DMA_FIFOMode_Disable;
//指定了FIFO阈值水平
DMA_InitStructure.DMA_FIFOThreshold = DMA_FIFOThreshold_HalfFull;
//指定的Burst转移配置内存传输
DMA_InitStructure.DMA_MemoryBurst = DMA_MemoryBurst_Single;
//指定的Burst转移配置外围转移 */
DMA_InitStructure.DMA_PeripheralBurst = DMA_PeripheralBurst_Single;
//配置DMA1的通道
DMA_Init(DMA1_Stream5, &DMA_InitStructure);
//使能通道
DMA_Cmd(DMA1_Stream5,ENABLE);
//初始化串口参数
USART_InitStructure.USART_WordLength = USART_WordLength_8b;
USART_InitStructure.USART_StopBits = USART_StopBits_1;
USART_InitStructure.USART_Parity = USART_Parity_No;
USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
USART_InitStructure.USART_BaudRate = DEFAULT_BAUD_GSM;
//初始化串口
USART_Init(USART2,&USART_InitStructure);
//中断配置
USART_ITConfig(USART2,USART_IT_TC,DISABLE);
USART_ITConfig(USART2,USART_IT_RXNE,DISABLE);
USART_ITConfig(USART2,USART_IT_IDLE,ENABLE);
//配置中断
//NVIC_PriorityGroupConfig(NVIC_PriorityGroup_3);
NVIC_InitStructure.NVIC_IRQChannel = USART2_IRQn; //通道设置为串口中断
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0; //中断占先等级
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0; //中断响应优先级
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //打开中断
NVIC_Init(&NVIC_InitStructure);
//采用DMA方式发送
USART_DMACmd(USART2,USART_DMAReq_Tx,ENABLE);
//采用DMA方式接收
USART_DMACmd(USART2,USART_DMAReq_Rx,ENABLE);
//中断配置
USART_ITConfig(USART2,USART_IT_TC,DISABLE);
USART_ITConfig(USART2,USART_IT_RXNE,DISABLE);
USART_ITConfig(USART2,USART_IT_TXE,DISABLE);
USART_ITConfig(USART2,USART_IT_IDLE,ENABLE);
//启动串口
USART_Cmd(USART2, ENABLE);
//设置IO口时钟
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE);
GPIO_PinAFConfig(GPIOA,GPIO_PinSource2,GPIO_AF_USART2);
GPIO_PinAFConfig(GPIOA,GPIO_PinSource3,GPIO_AF_USART2);
//管脚模式:输出口
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
//类型:推挽模式
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
//上拉下拉设置
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
//IO口速度
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;
//管脚指定
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2;
//初始化
GPIO_Init(GPIOA, &GPIO_InitStructure);
//管脚模式:输入口
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
//上拉下拉设置
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
//管脚指定
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3;
//初始化
GPIO_Init(GPIOA, &GPIO_InitStructure);
}
|