一.查看串口DMA通道
对应的代码片段如下
#define U3_DMA_SEND_CH DMA_CH5
#define U3_DMA_RECV_CH DMA_CH6
uint8_t u3_send_buff[20]={0};
uint8_t u3_recv_buff[20]={0};
DMA_InitType DMA_UART_TYPE;
DMA_DeInit(U3_DMA_SEND_CH);
DMA_UART_TYPE.BufSize=50;
DMA_UART_TYPE.CircularMode=DMA_MODE_NORMAL;
DMA_UART_TYPE.Direction=DMA_DIR_PERIPH_DST;
DMA_UART_TYPE.DMA_MemoryInc=DMA_MEM_INC_ENABLE;
DMA_UART_TYPE.Mem2Mem=DMA_M2M_DISABLE;
DMA_UART_TYPE.MemAddr=(uint32_t)u3_send_buff; //
DMA_UART_TYPE.MemDataSize=DMA_MemoryDataSize_Byte;
DMA_UART_TYPE.PeriphAddr=(u32)&(USART3->DAT);
DMA_UART_TYPE.PeriphDataSize=DMA_PERIPH_DATA_SIZE_BYTE;
DMA_UART_TYPE.PeriphInc=DMA_PERIPH_INC_DISABLE;
DMA_UART_TYPE.Priority=DMA_PRIORITY_VERY_HIGH;
DMA_Init(U3_DMA_SEND_CH,&DMA_UART_TYPE);
DMA_RequestRemap(DMA_REMAP_USART3_TX, DMA, U3_DMA_SEND_CH, ENABLE);
DMA_SetCurrDataCounter(U3_DMA_SEND_CH,20);
DMA_EnableChannel(U3_DMA_SEND_CH, ENABLE);
DMA_InitType DMA_UART_TYPE_1;
DMA_DeInit(U3_DMA_RECV_CH);
DMA_UART_TYPE_1.BufSize=50;
DMA_UART_TYPE_1.CircularMode=DMA_MODE_NORMAL;
DMA_UART_TYPE_1.Direction=DMA_DIR_PERIPH_SRC; //从外设到寄存器
DMA_UART_TYPE_1.DMA_MemoryInc=DMA_MEM_INC_ENABLE;
DMA_UART_TYPE_1.Mem2Mem=DMA_M2M_DISABLE;
DMA_UART_TYPE_1.MemAddr=(uint32_t)u3_recv_buff; //
DMA_UART_TYPE_1.MemDataSize=DMA_MemoryDataSize_Byte;
DMA_UART_TYPE_1.PeriphAddr=(u32)&(USART3->DAT);
DMA_UART_TYPE_1.PeriphDataSize=DMA_PERIPH_DATA_SIZE_BYTE;
DMA_UART_TYPE_1.PeriphInc=DMA_PERIPH_INC_DISABLE;
DMA_UART_TYPE_1.Priority=DMA_PRIORITY_VERY_HIGH;
DMA_Init(U3_DMA_RECV_CH,&DMA_UART_TYPE_1);
DMA_RequestRemap(DMA_REMAP_USART3_RX, DMA, U3_DMA_RECV_CH, ENABLE);
USART_EnableDMA(USART3, USART_DMAREQ_RX | USART_DMAREQ_TX, ENABLE);
DMA_SetCurrDataCounter(U3_DMA_RECV_CH,20);
DMA_EnableChannel(U3_DMA_RECV_CH, ENABLE);
二.配置UART中断
DMA中接收数据包采用空闲中断
//配置串口以及中断
RCC_EnableAPB2PeriphClk(RCC_APB2_PERIPH_GPIOB|RCC_APB2_PERIPH_AFIO, ENABLE);
RCC_EnableAPB1PeriphClk(RCC_APB1_PERIPH_USART3,ENABLE);
RCC_EnableAHBPeriphClk(RCC_AHB_PERIPH_DMA,ENABLE);
//gpio init
GPIO_InitStructure.Pin = UART3_TX_PIN;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_InitStructure.GPIO_Alternate = GPIO_AF0_USART3;
GPIO_InitPeripheral(UART3_PORT, &GPIO_InitStructure);
GPIO_InitStructure.Pin = UART3_RX_PIN;
GPIO_InitStructure.GPIO_Alternate = GPIO_AF5_USART3;
GPIO_InitStructure.GPIO_Pull = GPIO_Pull_Up;
GPIO_InitPeripheral(UART3_PORT, &GPIO_InitStructure);
//nvic init
NVIC_InitStructure.NVIC_IRQChannel = USART3_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0; //0级先占,0级次占
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
//uart init
USART_InitStructure.BaudRate = 115200;
USART_InitStructure.WordLength = USART_WL_8B;
USART_InitStructure.StopBits = USART_STPB_1;
USART_InitStructure.Parity = USART_PE_NO;
USART_InitStructure.HardwareFlowControl = USART_HFCTRL_NONE;
USART_InitStructure.Mode = USART_MODE_RX | USART_MODE_TX;
USART_Init(USART3, &USART_InitStructure);
USART_ConfigInt(USART3, USART_INT_IDLEF, ENABLE);
USART_Enable(USART3, ENABLE);
//重写中断函数
void USART3_IRQHandler(void)
{
u8 Res;
if (USART_GetIntStatus(USART3, USART_INT_IDLEF) != RESET) {
DMA_EnableChannel(DMA_CH6, DISABLE); //关闭接收通道
DMA_SetCurrDataCounter(DMA_CH6,20);
DMA_EnableChannel(DMA_CH6, ENABLE); //关闭接收通道
//清标志位
char c =USART3->DAT;
c=USART3->STS;
} else if(USART_GetIntStatus(USART3, USART_INT_TXDE) != RESET) {
} else {
USART_ReceiveData(USART3);
}
}