STM32F207串口DMA模式(串口中斷bug穩(wěn)定性)
使用DMA操作UART4:
一、DMA映射表
見下表:
UART4的發(fā)送 UART4_TX 在Stream4。
UART4的接收 UART4_RX在Stream2。
他們在Channel_4。
二、程序代碼
unsigned char btDMARecbuf[255];// DMA接收數(shù)據(jù)地址
unsigned char btDMASendbuf[255];//DMA發(fā)送數(shù)據(jù)地址
// 初始化
void UartDMA()
{
//定義中斷結(jié)構(gòu)體
NVIC_InitTypeDef NVIC_InitStructure ;
//定義IO初始化結(jié)構(gòu)體
GPIO_InitTypeDef GPIO_InitStructure;
//定義串口結(jié)構(gòu)體
USART_InitTypeDef USART_InitStructure;
//定義DMA結(jié)構(gòu)體
DMA_InitTypeDef DMA_InitStructure;
USART_DeInit(UART4); // 串口4
DMA_DeInit(DMA1_Stream2); // 接收
DMA_DeInit(DMA1_Stream4); // 發(fā)送
//設(shè)置IO口時鐘
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOC, ENABLE);
GPIO_PinAFConfig(GPIOC,GPIO_PinSource10,GPIO_AF_UART4);
GPIO_PinAFConfig(GPIOC,GPIO_PinSource11,GPIO_AF_UART4);
//管腳模式:輸出口
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
//類型:推挽模式
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
//上拉下拉設(shè)置
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
//IO口速度
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
//管腳指定
GPIO_InitStructure.GPIO_Pin =GPIO_Pin_10;
//初始化
GPIO_Init(GPIOC, &GPIO_InitStructure);
//管腳模式:輸入口
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
//上拉下拉設(shè)置
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
//管腳指定
GPIO_InitStructure.GPIO_Pin =GPIO_Pin_11;
//初始化
GPIO_Init(GPIOC, &GPIO_InitStructure);
//打開串口對應(yīng)的外設(shè)時鐘
RCC_APB1PeriphClockCmd(RCC_APB1Periph_UART4, ENABLE);
//串口發(fā) - DMA配置
//啟動DMA時鐘
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_DMA1, ENABLE);
//DMA發(fā)送中斷設(shè)置
NVIC_InitStructure.NVIC_IRQChannel = DMA1_Stream4_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 2;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
//DMA通道配置
DMA_InitStructure.DMA_Channel = DMA_Channel_4;
//外設(shè)地址
DMA_InitStructure.DMA_PeripheralBaseAddr = (uint32_t)(&UART4->DR);
//內(nèi)存地址
DMA_InitStructure.DMA_Memory0BaseAddr = (uint32_t)btDMASendbuf;
//dma傳輸方向
DMA_InitStructure.DMA_DIR = DMA_DIR_MemoryToPeripheral;
//設(shè)置DMA在傳輸時緩沖區(qū)的長度
DMA_InitStructure.DMA_BufferSize = 255;
//設(shè)置DMA的外設(shè)遞增模式,一個外設(shè)
DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable;
//設(shè)置DMA的內(nèi)存遞增模式
DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable;
//外設(shè)數(shù)據(jù)字長
DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_Byte;
//內(nèi)存數(shù)據(jù)字長
DMA_InitStructure.DMA_MemoryDataSize = DMA_PeripheralDataSize_Byte;
//設(shè)置DMA的傳輸模式
DMA_InitStructure.DMA_Mode = DMA_Mode_Normal;
//設(shè)置DMA的優(yōu)先級別
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轉(zhuǎn)移配置內(nèi)存?zhèn)鬏?/p>
DMA_InitStructure.DMA_MemoryBurst = DMA_MemoryBurst_Single;
//指定的Burst轉(zhuǎn)移配置外圍轉(zhuǎn)移 */
DMA_InitStructure.DMA_PeripheralBurst = DMA_PeripheralBurst_Single;
//配置DMA1的通道
DMA_Init(DMA1_Stream4, &DMA_InitStructure);
//使能中斷
DMA_ITConfig(DMA1_Stream4, DMA_IT_TC, ENABLE);
//串口收 - DMA配置
DMA_InitStructure.DMA_Channel = DMA_Channel_4;
//外設(shè)地址
DMA_InitStructure.DMA_PeripheralBaseAddr = (uint32_t)(&UART4->DR);
//內(nèi)存地址
DMA_InitStructure.DMA_Memory0BaseAddr = (uint32_t)btDMARecbuf;
//dma傳輸方向
DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralToMemory;
//設(shè)置DMA在傳輸時緩沖區(qū)的長度
DMA_InitStructure.DMA_BufferSize = 255;
//設(shè)置DMA的外設(shè)遞增模式,一個外設(shè)
DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable;
//設(shè)置DMA的內(nèi)存遞增模式
DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable;
//外設(shè)數(shù)據(jù)字長
DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_Byte;
//內(nèi)存數(shù)據(jù)字長
DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_Byte;
//設(shè)置DMA的傳輸模式
DMA_InitStructure.DMA_Mode = DMA_Mode_Normal;
//設(shè)置DMA的優(yōu)先級別
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轉(zhuǎn)移配置內(nèi)存?zhèn)鬏?/p>
DMA_InitStructure.DMA_MemoryBurst = DMA_MemoryBurst_Single;
//指定的Burst轉(zhuǎn)移配置外圍轉(zhuǎn)移 */
DMA_InitStructure.DMA_PeripheralBurst = DMA_PeripheralBurst_Single;
//配置DMA1的通道
DMA_Init(DMA1_Stream2, &DMA_InitStructure);
//使能通道
DMA_Cmd(DMA1_Stream2, ENABLE);
//初始化串口參數(shù)
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 = 115200;
//初始化串口
USART_Init(UART4, &USART_InitStructure);
//配置中斷
NVIC_InitStructure.NVIC_IRQChannel = UART4_IRQn; //通道設(shè)置為串口中斷
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0; //中斷占先等級
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0; //中斷響應(yīng)優(yōu)先級
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //打開中斷
NVIC_Init(&NVIC_InitStructure);
//采用DMA方式發(fā)送
USART_DMACmd(UART4, USART_DMAReq_Tx, ENABLE);
//采用DMA方式接收
USART_DMACmd(UART4, USART_DMAReq_Rx, ENABLE);
//中斷配置
USART_ITConfig(UART4, USART_IT_TC, DISABLE);
USART_ITConfig(UART4, USART_IT_RXNE, DISABLE);
USART_ITConfig(UART4, USART_IT_TXE, DISABLE);
USART_ITConfig(UART4, USART_IT_IDLE, ENABLE);
USART_ITConfig(USART1, USART_IT_RXNE, ENABLE);
USART_ITConfig(UART4, USART_IT_PE, ENABLE); //開啟PE錯誤接收中斷Bit 8PEIE: PE interrupt enable
USART_ITConfig(UART4, USART_IT_ERR, ENABLE);//CR2 開啟ERR中斷
//啟動串口
USART_Cmd(UART4, ENABLE);
while(!DMA_GetCmdStatus(DMA1_Stream4))
{
DMA_Cmd(DMA1_Stream4, ENABLE);
}
while(!DMA_GetCmdStatus(DMA1_Stream2))
{
DMA_Cmd(DMA1_Stream2, ENABLE);
}
}
// 發(fā)送
void UartSend(uint8_t *data, uint8_t size)
{
unsigned char i;
//復(fù)制數(shù)據(jù)
for (i=0; i { btDMASendbuf[i] = data[i]; } //設(shè)置傳輸數(shù)據(jù)長度 DMA_SetCurrDataCounter(DMA1_Stream4, size); //打開DMA, 開始發(fā)送 DMA_Cmd(DMA1_Stream4, ENABLE); } // DMA發(fā)送中斷處理函數(shù) void DMA1_Stream4_IRQHandler(void) { if(DMA_GetITStatus(DMA1_Stream4, DMA_IT_TCIF4) != RESET) { //清除標(biāo)志位 DMA_ClearFlag(DMA1_Stream4, DMA_FLAG_TCIF4); //關(guān)閉DMA DMA_Cmd(DMA1_Stream4, DISABLE); //打開發(fā)送完成中斷, 發(fā)送最后兩個字節(jié) USART_ITConfig(UART4, USART_IT_TC, ENABLE); } } // 串口中斷處理函數(shù) void UART4_IRQHandler(void) { unsigned char i; unsigned char len = 0; //發(fā)送完成中斷處理 if(USART_GetITStatus(UART4, USART_IT_TC) != RESET) { //關(guān)閉發(fā)送完成中斷 USART_ITConfig(UART4, USART_IT_TC, DISABLE); } //接