标签:
一、初始化
1.初始化串口,时钟
MX_USART1_UART_Init();
串口时钟初始化为内部时钟
PeriphClkInit.Usart1ClockSelection = RCC_USART1CLKSOURCE_HSI;
PeriphClkInit.Usart2ClockSelection = RCC_USART2CLKSOURCE_HSI;
2.初始化dma,端口复用
HAL_UART_MspInit()
打开空闲帧中断
__HAL_UART_ENABLE_IT(uartHandle, UART_IT_IDLE);
二、启动接收传送
1.启动数据接收
/*启动DMA接收传送*/ void Mx_Uart_DebugRxStart(UART_E uart) { if(uart>= UART_MAX){return ;} if(!g_stMyUart[uart].bDebugRxStop){return ;} for(uint8 i=0;i<UART_DEBUG_BUFF_LEVER;i++) { if(!g_stMyUart[uart].bDebugRxAvail[i]) { g_stMyUart[uart].bDebugRxStop=false; HAL_UART_Receive_DMA(g_stMyUart[uart].huart ,g_stMyUart[uart].cDebugBuffRx[i] ,UART_RX_BUFF_SIZE-1); g_stMyUart[uart].cCurDebugRxBuff=i; g_stMyUart[uart].uDebugRxCount[i]=0; break; } } }
2.如果接收完数据则触发DMA中断,触 发DMa中断后,调用传送完成中断,如果收到空闲帧,也会调用传送完成中断
uint32_t temp; temp = huart->Instance->ISR; temp = huart->Instance->RDR; if(HAL_OK !=HAL_UART_DMAStopRx(huart)) { Error_Handler(); } g_stMyUart[uart].bDebugRxStop=true; if(NULL != huart->hdmarx) { temp = huart->hdmarx->Instance->CNDTR; MX_UART_DebugGetData(uart,temp); Mx_Uart_DebugRxStart(uart); } else{Error_Handler();}
停止DMA传送,置接收停止标志,读取数据,重新开始数据接收
三、发送DMA
1.启动传送
if(HAL_UART_Transmit_DMA(g_stMyUart[UART_DEBUG].huart , g_stMyUart[UART_DEBUG].cDebugBuffTx[i] , g_stMyUart[UART_DEBUG].uDebugTxCount[i]) == HAL_OK) { g_stMyUart[UART_DEBUG].cCurDebugTxBuff=i; g_stMyUart[UART_DEBUG].uDebugTxCount[i]=0; }
2.DMA传送完成时,打开串口发送完成中断,
void HAL_UART_TxCpltCallback(UART_HandleTypeDef *huart) { UART_E uart; for(uart=UART_DEBUG;uart<UART_MAX;uart++) { if(g_stMyUart[uart].huart == huart){break;} } if(UART_MAX == uart){return ;} HAL_UART_DMAStopTx(huart); if(g_stMyUart[uart].cCurDebugTxBuff <UART_DEBUG_BUFF_LEVER) { g_stMyUart[uart].bDebugTxEn[g_stMyUart[uart].cCurDebugTxBuff]=false; } }
标签:
原文地址:http://www.cnblogs.com/jiangbo1980/p/5567850.html