STM32 HAL UART发送DMA问题

2019-07-14 17:20发布

在为包含FT2232H UART < - > USB转换器的自定义STM32F7板设置项目后,我在发送(和接收数据)时遇到了多个问题。我使用的代码主要由CubeMX生成,位于帖子的末尾。首先,根据数据表,FT2232H和STM32F7的波特率至少应为12M波特,我无法让波特率高于标准115200。对于FT2232H,我正在工作,因为我从终端(USB端)发送一些字符,并在我短接FT2232H输出端的RX和TX引脚时收回了字符。其次是我不能连续多次调用sendUART()函数,为什么DMA Fifo不用于存储我想要发送的东西?还有什么方法可以回显所有收到的数据,但是利用了fifo,这样当没有及时轮询数据时,没有数据丢失?
  1. void MX_UART4_Init(void)
  2. {

  3.   huart4.Instance = UART4;
  4.   huart4.Init.BaudRate = 115200;
  5.   huart4.Init.WordLength = UART_WORDLENGTH_8B;
  6.   huart4.Init.StopBits = UART_STOPBITS_1;
  7.   huart4.Init.Parity = UART_PARITY_NONE;
  8.   huart4.Init.Mode = UART_MODE_TX_RX;
  9.   huart4.Init.HwFlowCtl = UART_HWCONTROL_NONE;
  10.   huart4.Init.OverSampling = UART_OVERSAMPLING_16;
  11.   huart4.Init.OneBitSampling = UART_ONE_BIT_SAMPLE_DISABLE;
  12.   huart4.AdvancedInit.AdvFeatureInit = UART_ADVFEATURE_NO_INIT;
  13.   if (HAL_UART_Init(&huart4) != HAL_OK)
  14.   {
  15.     _Error_Handler(__FILE__, __LINE__);
  16.   }

  17. }

  18. void HAL_UART_MspInit(UART_HandleTypeDef* uartHandle)
  19. {

  20.   GPIO_InitTypeDef GPIO_InitStruct;
  21.   if(uartHandle->Instance==UART4)
  22.   {
  23.   /* USER CODE BEGIN UART4_MspInit 0 */

  24.   /* USER CODE END UART4_MspInit 0 */
  25.     /* UART4 clock enable */
  26.     __HAL_RCC_UART4_CLK_ENABLE();

  27.     /**UART4 GPIO Configuration   
  28.     PA0/WKUP     ------> UART4_TX
  29.     PA1     ------> UART4_RX
  30.     */
  31.     GPIO_InitStruct.Pin = GPIO_PIN_0|GPIO_PIN_1;
  32.     GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
  33.     GPIO_InitStruct.Pull = GPIO_PULLUP;
  34.     GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
  35.     GPIO_InitStruct.Alternate = GPIO_AF8_UART4;
  36.     HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);

  37.     /* UART4 DMA Init */
  38.     /* UART4_TX Init */
  39.     hdma_uart4_tx.Instance = DMA1_Stream4;
  40.     hdma_uart4_tx.Init.Channel = DMA_CHANNEL_4;
  41.     hdma_uart4_tx.Init.Direction = DMA_MEMORY_TO_PERIPH;
  42.     hdma_uart4_tx.Init.PeriphInc = DMA_PINC_DISABLE;
  43.     hdma_uart4_tx.Init.MemInc = DMA_MINC_ENABLE;
  44.     hdma_uart4_tx.Init.PeriphDataAlignment = DMA_PDATAALIGN_BYTE;
  45.     hdma_uart4_tx.Init.MemDataAlignment = DMA_MDATAALIGN_BYTE;
  46.     hdma_uart4_tx.Init.Mode = DMA_NORMAL;
  47.     hdma_uart4_tx.Init.Priority = DMA_PRIORITY_MEDIUM;
  48.     hdma_uart4_tx.Init.FIFOMode = DMA_FIFOMODE_ENABLE;
  49.     hdma_uart4_tx.Init.FIFOThreshold = DMA_FIFO_THRESHOLD_FULL;
  50.     hdma_uart4_tx.Init.MemBurst = DMA_MBURST_SINGLE;
  51.     hdma_uart4_tx.Init.PeriphBurst = DMA_PBURST_SINGLE;
  52.     if (HAL_DMA_Init(&hdma_uart4_tx) != HAL_OK)
  53.     {
  54.       _Error_Handler(__FILE__, __LINE__);
  55.     }

  56.     __HAL_LINKDMA(uartHandle,hdmatx,hdma_uart4_tx);

  57.     /* UART4_RX Init */
  58.     hdma_uart4_rx.Instance = DMA1_Stream2;
  59.     hdma_uart4_rx.Init.Channel = DMA_CHANNEL_4;
  60.     hdma_uart4_rx.Init.Direction = DMA_PERIPH_TO_MEMORY;
  61.     hdma_uart4_rx.Init.PeriphInc = DMA_PINC_DISABLE;
  62.     hdma_uart4_rx.Init.MemInc = DMA_MINC_ENABLE;
  63.     hdma_uart4_rx.Init.PeriphDataAlignment = DMA_PDATAALIGN_BYTE;
  64.     hdma_uart4_rx.Init.MemDataAlignment = DMA_MDATAALIGN_BYTE;
  65.     hdma_uart4_rx.Init.Mode = DMA_NORMAL;
  66.     hdma_uart4_rx.Init.Priority = DMA_PRIORITY_MEDIUM;
  67.     hdma_uart4_rx.Init.FIFOMode = DMA_FIFOMODE_ENABLE;
  68.     hdma_uart4_rx.Init.FIFOThreshold = DMA_FIFO_THRESHOLD_FULL;
  69.     hdma_uart4_rx.Init.MemBurst = DMA_MBURST_SINGLE;
  70.     hdma_uart4_rx.Init.PeriphBurst = DMA_PBURST_SINGLE;
  71.     if (HAL_DMA_Init(&hdma_uart4_rx) != HAL_OK)
  72.     {
  73.       _Error_Handler(__FILE__, __LINE__);
  74.     }

  75.     __HAL_LINKDMA(uartHandle,hdmarx,hdma_uart4_rx);

  76.   /* USER CODE BEGIN UART4_MspInit 1 */

  77.   /* USER CODE END UART4_MspInit 1 */
  78.   }
  79. }

  80. void HAL_UART_MspDeInit(UART_HandleTypeDef* uartHandle)
  81. {

  82.   if(uartHandle->Instance==UART4)
  83.   {
  84.   /* USER CODE BEGIN UART4_MspDeInit 0 */

  85.   /* USER CODE END UART4_MspDeInit 0 */
  86.     /* Peripheral clock disable */
  87.     __HAL_RCC_UART4_CLK_DISABLE();

  88.     /**UART4 GPIO Configuration   
  89.     PA0/WKUP     ------> UART4_TX
  90.     PA1     ------> UART4_RX
  91.     */
  92.     HAL_GPIO_DeInit(GPIOA, GPIO_PIN_0|GPIO_PIN_1);

  93.     /* UART4 DMA DeInit */
  94.     HAL_DMA_DeInit(uartHandle->hdmatx);
  95.     HAL_DMA_DeInit(uartHandle->hdmarx);
  96.   /* USER CODE BEGIN UART4_MspDeInit 1 */

  97.   /* USER CODE END UART4_MspDeInit 1 */
  98.   }
  99. }

  100. /* USER CODE BEGIN 1 */
  101. void sendUART(char msg[]){
  102.     //HAL_UART_Transmit(&huart4,(uint8_t *) msg, strlen(msg),10000);
  103.     HAL_UART_Transmit_DMA(&huart4,(uint8_t *) msg, strlen(msg));
  104. }

  105. void echo(){
  106.     if(HAL_UART_Receive_DMA(&huart4, (uint8_t *)aRxBuffer, RXBUFFERSIZE) != HAL_OK){}
  107.     else if(HAL_UART_Transmit_DMA(&huart4, (uint8_t*)aRxBuffer, RXBUFFERSIZE)!= HAL_OK){
  108.     }
  109. }
复制代码

友情提示: 此问题已得到解决,问题已经关闭,关闭后问题禁止继续编辑,回答。
该问题目前已经被作者或者管理员关闭, 无法添加新回复
2条回答
罕井雅直
1楼-- · 2019-07-14 19:42
 精彩回答 2  元偷偷看……
zym123456
2楼-- · 2019-07-14 21:24
如果你将UART时钟源设置为200MHz SYSCLK(你在哪里做?它不在上面的代码中),那么你得到12.5M,对,但这不是12M,FTDI部分正在运行。当其他一切都很完美时(控制器不是太热,信号很清晰,没有噪声),它有4.167%的差异,再加上它可以工作的HSI振荡器的不准确性,但是我试用192 MHz以获得更好的匹配。

一周热门 更多>