调试通过,需要CLEAR_FLAG一下才行,代码如下图:★★★★经过验证,不是必须的。★★★★看来有机会要好好看看寄存器了。
需要注意的是,在使用UART_DMASTOP程序时,会关闭所有的接收和发送DMA,所以如果需要只关发送,需要自己写程序。
以下是HAL_UART_DMAStop(UART_HandleTypeDef *huart)程序源码:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 |
/** * @brief Stop the DMA Transfer. * @param huart UART handle. * @retval HAL status */ HAL_StatusTypeDef HAL_UART_DMAStop(UART_HandleTypeDef *huart) { /* The Lock is not implemented on this API to allow the user application to call the HAL UART API under callbacks HAL_UART_TxCpltCallback() / HAL_UART_RxCpltCallback() / HAL_UART_TxHalfCpltCallback / HAL_UART_RxHalfCpltCallback: indeed, when HAL_DMA_Abort() API is called, the DMA TX/RX Transfer or Half Transfer complete interrupt is generated if the DMA transfer interruption occurs at the middle or at the end of the stream and the corresponding call back is executed. */ /* Stop UART DMA Tx request if ongoing 停止TXDMA*/ if ((huart->gState == HAL_UART_STATE_BUSY_TX) && (HAL_IS_BIT_SET(huart->Instance->CR3, USART_CR3_DMAT))) { CLEAR_BIT(huart->Instance->CR3, USART_CR3_DMAT); /* Abort the UART DMA Tx channel */ if(huart->hdmatx != NULL) { HAL_DMA_Abort(huart->hdmatx); } UART_EndTxTransfer(huart); } /* Stop UART DMA Rx request if ongoing 停止RXDMA*/ if ((huart->RxState == HAL_UART_STATE_BUSY_RX) && (HAL_IS_BIT_SET(huart->Instance->CR3, USART_CR3_DMAR))) { CLEAR_BIT(huart->Instance->CR3, USART_CR3_DMAR); /* Abort the UART DMA Rx channel */ if(huart->hdmarx != NULL) { HAL_DMA_Abort(huart->hdmarx); } UART_EndRxTransfer(huart); } return HAL_OK; } |
最后程序改为如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 |
HAL_StatusTypeDef HAL_UART_TX_DMAStop(UART_HandleTypeDef *huart) { /* The Lock is not implemented on this API to allow the user application to call the HAL UART API under callbacks HAL_UART_TxCpltCallback() / HAL_UART_RxCpltCallback() / HAL_UART_TxHalfCpltCallback / HAL_UART_RxHalfCpltCallback: indeed, when HAL_DMA_Abort() API is called, the DMA TX/RX Transfer or Half Transfer complete interrupt is generated if the DMA transfer interruption occurs at the middle or at the end of the stream and the corresponding call back is executed. */ /* Stop UART DMA Tx request if ongoing */ if ((huart->gState == HAL_UART_STATE_BUSY_TX) && (HAL_IS_BIT_SET(huart->Instance->CR3, USART_CR3_DMAT))) { CLEAR_BIT(huart->Instance->CR3, USART_CR3_DMAT); /* Abort the UART DMA Tx channel */ if(huart->hdmatx != NULL) { HAL_DMA_Abort(huart->hdmatx); } //UART_EndTxTransfer(huart);//这句话找不到,所以直接找到然后清指令就行了。 /* Disable TXEIE and TCIE interrupts */ CLEAR_BIT(huart->Instance->CR1, (USART_CR1_TXEIE | USART_CR1_TCIE)); /* At end of Tx process, restore huart->gState to Ready */ huart->gState = HAL_UART_STATE_READY; } return HAL_OK; } /* USER CODE END 4 */ /* StartDefaultTask function */ void StartDefaultTask(void const * argument) { /* USER CODE BEGIN 5 */ /* Infinite loop */ for(;;) { HAL_GPIO_TogglePin(LD2_GPIO_Port,LD2_Pin); osDelay(30); } /* USER CODE END 5 */ } /* StartTask02 function */ void StartTask02(void const * argument) { /* USER CODE BEGIN StartTask02 */ //HAL_UART_Receive_DMA(&huart2,(uint8_t *)uart_dma_buf2 , size_of_rx_buffer2); /* Infinite loop */ for(;;) { HAL_UART_TX_DMAStop(&huart2); HAL_UART_Transmit_DMA(&huart2,(uint8_t *)uart_tx_buf,strlen((const char *)uart_tx_buf)); //__HAL_DMA_CLEAR_FLAG(&hdma_usart2_tx,DMA_FLAG_TC7); //HAL_UART_Transmit_DMA(&huart2,(uint8_t *)uart_tx_buf,5); //hdma_usart2_tx.Instance->CMAR = //printf("123\r\n"); osDelay(1000); } /* USER CODE END StartTask02 */ } |
转载请注明:徐自远的乱七八糟小站 » 【原创】STM32HAL库下DMA串口发送测试