typedef struct { uint8_t TimeOut; uint16_t Cnt; uint8_t ReceiveBuff[RECEIVELEN]; }UartType; /* usart1_tx ch4 usart1_rx ch5 usart2_tx ch7 usart2_rx ch6 */ UartType uart1Type; UartType uart2Type; UartType *uartTypeBuf[2]={&uart1Type,&uart2Type}; uint8_t usart1_rxBuf_dma[RECEIVELEN]={0}; uint8_t usart2_rxBuf_dma[RECEIVELEN]={0}; uint8_t Usart1_Dma_SendFlag = 1,Usart2_Dma_SendFlag = 1;; //uint8_t USART1_DMATX_Flish=0,USART2_DMATX_Flish=0; //================================================================= //name:uart1_config //descrip:uart1 config //param: in :buad //retval:none //================================================================= void uart1_Init(uint32_t buad) { USART_InitTypeDef USART_InitStruct; GPIO_InitTypeDef GPIO_InitStructure; NVIC_InitTypeDef NVIC_InitStructure; DMA_InitTypeDef DMA_InitStructure; //open clk RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE); RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE); RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA1, ENABLE); /* Configure USART Tx and Rx as alternate function push-pull */ GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_10MHz; GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9; GPIO_Init(GPIOA, &GPIO_InitStructure); GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING; GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10; GPIO_Init(GPIOA, &GPIO_InitStructure); // Enable the USARTx Interrupt NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn; NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0; NVIC_InitStructure.NVIC_IRQChannelSubPriority = UART1NVIC; NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; NVIC_Init(&NVIC_InitStructure); USART_InitStruct.USART_BaudRate = buad; USART_InitStruct.USART_WordLength = USART_WordLength_8b; USART_InitStruct.USART_StopBits = USART_StopBits_1; USART_InitStruct.USART_Parity = USART_Parity_No ; USART_InitStruct.USART_Mode = USART_Mode_Rx | USART_Mode_Tx; USART_InitStruct.USART_HardwareFlowControl = USART_HardwareFlowControl_None; USART_InitStruct.USART_Mode = USART_Mode_Rx | USART_Mode_Tx; USART_ITConfig(USART1, USART_IT_IDLE, ENABLE);//开启串口1的RX中断 USART_Init(USART1, &USART_InitStruct); DMA_DeInit(DMA1_Channel5); DMA_InitStructure.DMA_PeripheralBaseAddr = (uint32_t)(&USART1->DR); //???? DMA_InitStructure.DMA_MemoryBaseAddr = (uint32_t)usart1_rxBuf_dma; //???? DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralSRC; //dma?????? DMA_InitStructure.DMA_BufferSize = RECEIVELEN; DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable; //??DMA???????,???? DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable; //??DMA??????? DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_Byte; //?????? DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_Byte; //?????? DMA_InitStructure.DMA_Mode = DMA_Mode_Normal; //??DMA????? DMA_InitStructure.DMA_Priority = DMA_Priority_VeryHigh; //??DMA????? DMA_InitStructure.DMA_M2M = DMA_M2M_Disable; //??DMA?2?memory???????? DMA_Init(DMA1_Channel5,&DMA_InitStructure); DMA_Cmd(DMA1_Channel5,ENABLE); //????5 USART_DMACmd(USART1,USART_DMAReq_Rx,ENABLE); //??DMA???? /* DMA1 Channel4 (triggered by USART1 Tx event) Config */ DMA_DeInit(DMA1_Channel4); DMA_InitStructure.DMA_PeripheralBaseAddr = (uint32_t)(&USART1->DR); DMA_InitStructure.DMA_MemoryBaseAddr = (uint32_t)uart1Type.ReceiveBuff; DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralDST; DMA_InitStructure.DMA_BufferSize = RECEIVELEN; DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable; DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable; DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_Byte; DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_Byte; DMA_InitStructure.DMA_Mode = DMA_Mode_Circular; DMA_InitStructure.DMA_Priority = DMA_Priority_High; DMA_InitStructure.DMA_M2M = DMA_M2M_Disable; DMA_Init(DMA1_Channel4, &DMA_InitStructure); DMA_ITConfig(DMA1_Channel4, DMA_IT_TC, ENABLE); DMA_ITConfig(DMA1_Channel4, DMA_IT_TE, ENABLE); USART_DMACmd(USART1, USART_DMAReq_Tx, ENABLE); DMA_Cmd(DMA1_Channel4, DISABLE); NVIC_InitStructure.NVIC_IRQChannel = DMA1_Channel4_IRQn; NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0; NVIC_InitStructure.NVIC_IRQChannelSubPriority = DMAUART1TXNVIC; NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; NVIC_Init(&NVIC_InitStructure); USART_Cmd(USART1, ENABLE); USART_ClearFlag(USART1,USART_FLAG_TC); GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_2MHz; GPIO_Init(GPIOA, &GPIO_InitStructure); OPEN232POWER(); //打开 232芯片电源 } //================================================================= //name:uart2_config //descrip:uart2 config //param: in :buad //retval:none //================================================================= void uart2_Init(uint32_t buad) { USART_InitTypeDef USART_InitStruct; GPIO_InitTypeDef GPIO_InitStructure; NVIC_InitTypeDef NVIC_InitStructure; DMA_InitTypeDef DMA_InitStructure; //open clk RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE); RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2, ENABLE); RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA1, ENABLE);//??DMA?? /* Configure USART Tx and Rx as alternate function push-pull */ GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_10MHz; GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2; GPIO_Init(GPIOA, &GPIO_InitStructure); GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING; GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3; GPIO_Init(GPIOA, &GPIO_InitStructure); /* Enable the USARTx Interrupt */ NVIC_InitStructure.NVIC_IRQChannel = USART2_IRQn; NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0; NVIC_InitStructure.NVIC_IRQChannelSubPriority = UART2NVIC; NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; NVIC_Init(&NVIC_InitStructure); USART_InitStruct.USART_BaudRate = buad; USART_InitStruct.USART_WordLength = USART_WordLength_8b; USART_InitStruct.USART_StopBits = USART_StopBits_1; USART_InitStruct.USART_Parity = USART_Parity_No ; USART_InitStruct.USART_Mode = USART_Mode_Rx | USART_Mode_Tx; USART_InitStruct.USART_HardwareFlowControl = USART_HardwareFlowControl_None; USART_InitStruct.USART_Mode = USART_Mode_Rx | USART_Mode_Tx; USART_ITConfig(USART2, USART_IT_IDLE, ENABLE);//开启串口2的RX中断 USART_Init(USART2, &USART_InitStruct); DMA_DeInit(DMA1_Channel6); DMA_InitStructure.DMA_PeripheralBaseAddr = (uint32_t)(&USART2->DR); DMA_InitStructure.DMA_MemoryBaseAddr = (uint32_t)usart2_rxBuf_dma; DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralSRC; DMA_InitStructure.DMA_BufferSize = RECEIVELEN; DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable; DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable; //??DMA??????? DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_Byte; //?????? DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_Byte; //?????? DMA_InitStructure.DMA_Mode = DMA_Mode_Normal; //??DMA????? DMA_InitStructure.DMA_Priority = DMA_Priority_VeryHigh; //??DMA????? DMA_InitStructure.DMA_M2M = DMA_M2M_Disable; //??DMA?2?memory???????? DMA_Init(DMA1_Channel6,&DMA_InitStructure); DMA_Cmd(DMA1_Channel6,ENABLE); USART_DMACmd(USART2,USART_DMAReq_Rx,ENABLE); //??DMA???? DMA_DeInit(DMA1_Channel7); DMA_InitStructure.DMA_PeripheralBaseAddr = (uint32_t)(&USART2->DR); DMA_InitStructure.DMA_MemoryBaseAddr = (uint32_t)uart2Type.ReceiveBuff; DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralDST; DMA_InitStructure.DMA_BufferSize = RECEIVELEN; DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable; DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable; DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_Byte; DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_Byte; DMA_InitStructure.DMA_Mode = DMA_Mode_Circular; DMA_InitStructure.DMA_Priority = DMA_Priority_High; DMA_InitStructure.DMA_M2M = DMA_M2M_Disable; DMA_Init(DMA1_Channel7, &DMA_InitStructure); DMA_ITConfig(DMA1_Channel7, DMA_IT_TC, ENABLE); DMA_ITConfig(DMA1_Channel7, DMA_IT_TE, ENABLE); USART_DMACmd(USART2, USART_DMAReq_Tx, ENABLE); DMA_Cmd(DMA1_Channel7, DISABLE); NVIC_InitStructure.NVIC_IRQChannel = DMA1_Channel7_IRQn; NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0; NVIC_InitStructure.NVIC_IRQChannelSubPriority = DMAUART2TXNVIC; NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; NVIC_Init(&NVIC_InitStructure); USART_Cmd(USART2, ENABLE); USART_ClearFlag(USART2,USART_FLAG_TC); } void USART1_IRQHandler(void) { uint32_t temp; if(USART_GetITStatus(USART1, USART_IT_IDLE) != RESET) //??????????? { //USART_ClearFlag(USART1,USART_IT_IDLE); temp = USART1->SR; //??SR,???DR???? temp = USART1->DR; //?USART_IT_IDLE?? DMA_Cmd(DMA1_Channel5,DISABLE); //??DMA??,?????????? temp = RECEIVELEN - DMA_GetCurrDataCounter(DMA1_Channel5); //???????? memcpy(uart1Type.ReceiveBuff+uart1Type.Cnt,usart1_rxBuf_dma,temp); uart1Type.Cnt+=temp; uart1Type.ReceiveBuff[uart1Type.Cnt] = NULL; uart1Type.TimeOut=1; DMA_SetCurrDataCounter(DMA1_Channel5,RECEIVELEN); //????????,???,?????????0?? DMA_Cmd(DMA1_Channel5,ENABLE); //??DMA?? } } void USART2_IRQHandler(void) { uint32_t temp; if(USART_GetITStatus(USART2, USART_IT_IDLE) != RESET) //??????????? { //USART_ClearFlag(USART1,USART_IT_IDLE); temp = USART2->SR; //??SR,???DR???? temp = USART2->DR; //?USART_IT_IDLE?? DMA_Cmd(DMA1_Channel6,DISABLE); //??DMA??,?????????? temp = RECEIVELEN - DMA_GetCurrDataCounter(DMA1_Channel6); //???????? memcpy(uart2Type.ReceiveBuff+uart2Type.Cnt,usart2_rxBuf_dma,temp); uart2Type.Cnt+=temp; uart2Type.ReceiveBuff[uart2Type.Cnt] = NULL; uart2Type.TimeOut=1; DMA_SetCurrDataCounter(DMA1_Channel6,RECEIVELEN); //????????,???,?????????0?? DMA_Cmd(DMA1_Channel6,ENABLE); //??DMA?? } } void DMA1_Channel4_IRQHandler(void) { DMA_Cmd(DMA1_Channel4, DISABLE);//关闭DMA1通道4 USART_DMACmd(USART1, USART_DMAReq_Tx, DISABLE);//关闭 DMA1_Channel4->CNDTR = RECEIVELEN; DMA_ClearITPendingBit(DMA1_IT_GL4);//清除中断标志 Usart1_Dma_SendFlag = 1;//发送完成标记 } void DMA1_Channel7_IRQHandler(void) { DMA_Cmd(DMA1_Channel7, DISABLE);//关闭DMA1通道4 USART_DMACmd(USART2, USART_DMAReq_Tx, DISABLE);//关闭 DMA1_Channel7->CNDTR = RECEIVELEN; DMA_ClearITPendingBit(DMA1_IT_GL7);//清除中断标志 Usart2_Dma_SendFlag = 1;//发送完成标记 } void Usart1SendData_DMA(uint8_t *pdata, uint16_t Length) { // DMA_Cmd(DMA1_Channel4, DISABLE); IWDG_ReloadCounter();//喂狗 while(Usart1_Dma_SendFlag == 0); Usart1_Dma_SendFlag = 0; DMA1_Channel4->CMAR = (uint32_t)pdata; DMA1_Channel4->CNDTR = Length; DMA_Cmd(DMA1_Channel4, ENABLE); USART_DMACmd(USART1, USART_DMAReq_Tx, ENABLE); } void Usart2SendData_DMA(uint8_t *pdata, uint16_t Length) { // DMA_Cmd(DMA1_Channel4, DISABLE); IWDG_ReloadCounter();//喂狗 while(Usart2_Dma_SendFlag == 0); Usart2_Dma_SendFlag = 0; DMA1_Channel7->CMAR = (uint32_t)pdata; DMA1_Channel7->CNDTR = Length; DMA_Cmd(DMA1_Channel7, ENABLE); USART_DMACmd(USART2, USART_DMAReq_Tx, ENABLE); }
STM32F103 USART DMA收发不定长数据
猜你喜欢
转载自blog.csdn.net/mjfmjj/article/details/51315017
今日推荐
周排行