Skip to main content
Graduate
May 16, 2023
Question

Sending a large amount of data around 1024 bytes using UART at 1mbps baud

  • May 16, 2023
  • 4 replies
  • 2977 views

Hi, I am working on the NUCLEO-G491RE development board with 170MHz HSE and testing UART communication with 1mbps baud with HAL_UART_Transmit_DMA and HAL_UART_TxCpltCallback on STM32CubeIDE but only able to send around first 256 bytes and the remaining data were never sent.

Why?

    This topic has been closed for replies.

    4 replies

    Graduate II
    May 16, 2023

    Because you're doing something wrong...

    Without your source code I can only give such a useless answer.

    Or I can do some wild guesses: buffer pointer correct? Buffer size only 8 bit variable? ....

    Graduate
    May 16, 2023

    @Community member​ please find my project in the attachment

    Graduate II
    May 16, 2023

    I don't like downloading stuff, so please show the relevant code with the </> code snippet tool.

    Or maybe someone else will take a look.

    Graduate
    May 17, 2023

    Hi @Community member​, please find the code here.

    Note: I have removed the comment section in the main.c file because I was not able to post such a big file

    #include "main.h"
     
    UART_HandleTypeDef hlpuart1;
    UART_HandleTypeDef huart4;
    DMA_HandleTypeDef hdma_uart4_tx;
     
    void SystemClock_Config(void);
    static void MX_GPIO_Init(void);
    static void MX_DMA_Init(void);
    static void MX_LPUART1_UART_Init(void);
    static void MX_UART4_Init(void);
     
    volatile uint8_t flag = 0;
     
    uint8_t txBuffer[] = "The sun was shining brightly in the sky as the birds chirped a"
    		 "nd flew around. It was a beautiful day, perfect for a picnic i"
    		 "n the park. Sarah and her friends had been planning this picni"
    		 "c for weeks and they were finally able to make it happen. As t"
    		 "hey spread out the blanket on the grass, they realized they ha"
    		 "d forgotten the plates and utensils. It was a silly mistake, b"
    		 "ut luckily there was a convenience store nearby where they wer"
    		 "e able to buy everything they needed. After a delicious lunch "
    		 "of sandwiches and fruit, they played frisbee and threw a footb"
    		 "all around. They even had a water balloon fight, cooling off i"
    		 "n the warm sun. As the day came to an end, they packed up thei"
    		 "r things and headed home, tired but happy. It was a day they w"
    		 "ould always remember, filled with laughter, good food, and gre"
    		 "at friends. As the day came to an end, they packed up their th"
    		 "ings and headed home, tired but happy. It was a day they would"
    		 " always remember, filled with laughter, good food, and great f"
    		 "riends. As the day came to an\r\n\r\n";
     
    void HAL_UART_TxCpltCallback (UART_HandleTypeDef * huart)
    {
    	flag = 1;
    }
     
    int main(void)
    {
     HAL_Init();
     SystemClock_Config();
     MX_GPIO_Init();
     MX_DMA_Init();
     MX_LPUART1_UART_Init();
     MX_UART4_Init();
     HAL_StatusTypeDef status;
     while (1)
     {
    	 HAL_UART_Transmit_DMA(&huart4, txBuffer, sizeof(txBuffer) - 1);
    	 HAL_Delay(5000);
     }
    }
     
    void SystemClock_Config(void)
    {
     RCC_OscInitTypeDef RCC_OscInitStruct = {0};
     RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
     
     /** Configure the main internal regulator output voltage
     */
     HAL_PWREx_ControlVoltageScaling(PWR_REGULATOR_VOLTAGE_SCALE1);
     
     /** Initializes the RCC Oscillators according to the specified parameters
     * in the RCC_OscInitTypeDef structure.
     */
     RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE;
     RCC_OscInitStruct.HSEState = RCC_HSE_ON;
     RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
     RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;
     RCC_OscInitStruct.PLL.PLLM = RCC_PLLM_DIV2;
     RCC_OscInitStruct.PLL.PLLN = 25;
     RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV2;
     RCC_OscInitStruct.PLL.PLLQ = RCC_PLLQ_DIV2;
     RCC_OscInitStruct.PLL.PLLR = RCC_PLLR_DIV2;
     if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
     {
     Error_Handler();
     }
     
     /** Initializes the CPU, AHB and APB buses clocks
     */
     RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
     |RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;
     RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
     RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
     RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV1;
     RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;
     
     if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_4) != HAL_OK)
     {
     Error_Handler();
     }
    }
     
    /**
     * @brief LPUART1 Initialization Function
     * @param None
     * @retval None
     */
    static void MX_LPUART1_UART_Init(void)
    {
     
     /* USER CODE BEGIN LPUART1_Init 0 */
     
     /* USER CODE END LPUART1_Init 0 */
     
     /* USER CODE BEGIN LPUART1_Init 1 */
     
     /* USER CODE END LPUART1_Init 1 */
     hlpuart1.Instance = LPUART1;
     hlpuart1.Init.BaudRate = 115200;
     hlpuart1.Init.WordLength = UART_WORDLENGTH_8B;
     hlpuart1.Init.StopBits = UART_STOPBITS_1;
     hlpuart1.Init.Parity = UART_PARITY_NONE;
     hlpuart1.Init.Mode = UART_MODE_TX_RX;
     hlpuart1.Init.HwFlowCtl = UART_HWCONTROL_NONE;
     hlpuart1.Init.OneBitSampling = UART_ONE_BIT_SAMPLE_DISABLE;
     hlpuart1.Init.ClockPrescaler = UART_PRESCALER_DIV1;
     hlpuart1.AdvancedInit.AdvFeatureInit = UART_ADVFEATURE_NO_INIT;
     if (HAL_UART_Init(&hlpuart1) != HAL_OK)
     {
     Error_Handler();
     }
     if (HAL_UARTEx_SetTxFifoThreshold(&hlpuart1, UART_TXFIFO_THRESHOLD_1_8) != HAL_OK)
     {
     Error_Handler();
     }
     if (HAL_UARTEx_SetRxFifoThreshold(&hlpuart1, UART_RXFIFO_THRESHOLD_1_8) != HAL_OK)
     {
     Error_Handler();
     }
     if (HAL_UARTEx_DisableFifoMode(&hlpuart1) != HAL_OK)
     {
     Error_Handler();
     }
     /* USER CODE BEGIN LPUART1_Init 2 */
     
     /* USER CODE END LPUART1_Init 2 */
     
    }
     
    /**
     * @brief UART4 Initialization Function
     * @param None
     * @retval None
     */
    static void MX_UART4_Init(void)
    {
     
     /* USER CODE BEGIN UART4_Init 0 */
     
     /* USER CODE END UART4_Init 0 */
     
     /* USER CODE BEGIN UART4_Init 1 */
     
     /* USER CODE END UART4_Init 1 */
     huart4.Instance = UART4;
     huart4.Init.BaudRate = 1000000;
     huart4.Init.WordLength = UART_WORDLENGTH_8B;
     huart4.Init.StopBits = UART_STOPBITS_1;
     huart4.Init.Parity = UART_PARITY_NONE;
     huart4.Init.Mode = UART_MODE_TX_RX;
     huart4.Init.HwFlowCtl = UART_HWCONTROL_NONE;
     huart4.Init.OverSampling = UART_OVERSAMPLING_16;
     huart4.Init.OneBitSampling = UART_ONE_BIT_SAMPLE_DISABLE;
     huart4.Init.ClockPrescaler = UART_PRESCALER_DIV1;
     huart4.AdvancedInit.AdvFeatureInit = UART_ADVFEATURE_NO_INIT;
     if (HAL_UART_Init(&huart4) != HAL_OK)
     {
     Error_Handler();
     }
     if (HAL_UARTEx_SetTxFifoThreshold(&huart4, UART_TXFIFO_THRESHOLD_1_8) != HAL_OK)
     {
     Error_Handler();
     }
     if (HAL_UARTEx_SetRxFifoThreshold(&huart4, UART_RXFIFO_THRESHOLD_1_8) != HAL_OK)
     {
     Error_Handler();
     }
     if (HAL_UARTEx_DisableFifoMode(&huart4) != HAL_OK)
     {
     Error_Handler();
     }
     /* USER CODE BEGIN UART4_Init 2 */
     
     /* USER CODE END UART4_Init 2 */
     
    }
     
    /**
     * Enable DMA controller clock
     */
    static void MX_DMA_Init(void)
    {
     
     /* DMA controller clock enable */
     __HAL_RCC_DMAMUX1_CLK_ENABLE();
     __HAL_RCC_DMA1_CLK_ENABLE();
     
     /* DMA interrupt init */
     /* DMA1_Channel1_IRQn interrupt configuration */
     HAL_NVIC_SetPriority(DMA1_Channel1_IRQn, 0, 0);
     HAL_NVIC_EnableIRQ(DMA1_Channel1_IRQn);
     
    }
     
    /**
     * @brief GPIO Initialization Function
     * @param None
     * @retval None
     */
    static void MX_GPIO_Init(void)
    {
     GPIO_InitTypeDef GPIO_InitStruct = {0};
    /* USER CODE BEGIN MX_GPIO_Init_1 */
    /* USER CODE END MX_GPIO_Init_1 */
     
     /* GPIO Ports Clock Enable */
     __HAL_RCC_GPIOC_CLK_ENABLE();
     __HAL_RCC_GPIOF_CLK_ENABLE();
     __HAL_RCC_GPIOA_CLK_ENABLE();
     __HAL_RCC_GPIOB_CLK_ENABLE();
     
     /*Configure GPIO pin Output Level */
     HAL_GPIO_WritePin(LD2_GPIO_Port, LD2_Pin, GPIO_PIN_RESET);
     
     /*Configure GPIO pin : B1_Pin */
     GPIO_InitStruct.Pin = B1_Pin;
     GPIO_InitStruct.Mode = GPIO_MODE_IT_RISING;
     GPIO_InitStruct.Pull = GPIO_NOPULL;
     HAL_GPIO_Init(B1_GPIO_Port, &GPIO_InitStruct);
     
     /*Configure GPIO pin : LD2_Pin */
     GPIO_InitStruct.Pin = LD2_Pin;
     GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
     GPIO_InitStruct.Pull = GPIO_NOPULL;
     GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
     HAL_GPIO_Init(LD2_GPIO_Port, &GPIO_InitStruct);
     
     /* EXTI interrupt init*/
     HAL_NVIC_SetPriority(EXTI15_10_IRQn, 0, 0);
     HAL_NVIC_EnableIRQ(EXTI15_10_IRQn);
     
    /* USER CODE BEGIN MX_GPIO_Init_2 */
    /* USER CODE END MX_GPIO_Init_2 */
    }
     
    /* USER CODE BEGIN 4 */
     
    /* USER CODE END 4 */
     
    /**
     * @brief This function is executed in case of error occurrence.
     * @retval None
     */
    void Error_Handler(void)
    {
     /* USER CODE BEGIN Error_Handler_Debug */
     /* User can add his own implementation to report the HAL error return state */
     __disable_irq();
     while (1)
     {
    	 HAL_GPIO_TogglePin(GPIOA, GPIO_PIN_5);
    	 HAL_Delay(100);
     }
     /* USER CODE END Error_Handler_Debug */
    }
     
    #ifdef USE_FULL_ASSERT
    /**
     * @brief Reports the name of the source file and the source line number
     * where the assert_param error has occurred.
     * @param file: pointer to the source file name
     * @param line: assert_param error line source number
     * @retval None
     */
    void assert_failed(uint8_t *file, uint32_t line)
    {
     /* USER CODE BEGIN 6 */
     /* User can add his own implementation to report the file name and line number,
     ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
     /* USER CODE END 6 */
    }
    #endif /* USE_FULL_ASSERT */

    Graduate II
    May 17, 2023

    That looks good so far.

    What I don't see is the important DMA init.

    Another thing: you don't really use the flag set in callback, so you might want to check that flag before sending the buffer again.

    Here's mine for an H7 running at 921600 baud, mind that I had to enable the UART interrupts manually after DMA init (can't remember why or if that's "double"):

    /* +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ */
    /**
     * @brief Tx Transfer completed callback.
     * @param huart UART handle.
     * @retval None
     */
    void HAL_UART_TxCpltCallback(UART_HandleTypeDef *huart)
    {
    	u8Uart3TxActive = 0;
    	huart->gState = HAL_UART_STATE_READY;
    }
     
    /**
     * @brief This function handles DMA2 stream 7 global interrupt.
     */
    void DMA2_Stream7_IRQHandler(void)
    {
    	HAL_DMA_IRQHandler(&hdma_usart3_tx);
    }
     
     
     
     
    in MSP init:
    	/* USART3_TX DMA Init */
    	hdma_usart3_tx.Instance = DMA2_Stream7;
    	hdma_usart3_tx.Init.Request 	= DMA_REQUEST_USART3_TX;
     
    	hdma_usart3_tx.Init.Direction = DMA_MEMORY_TO_PERIPH;
    	hdma_usart3_tx.Init.PeriphInc = DMA_PINC_DISABLE;
    	hdma_usart3_tx.Init.MemInc 	= DMA_MINC_ENABLE;
    	hdma_usart3_tx.Init.PeriphDataAlignment = DMA_PDATAALIGN_BYTE;
    	hdma_usart3_tx.Init.MemDataAlignment = DMA_MDATAALIGN_BYTE;
    	hdma_usart3_tx.Init.Mode = DMA_NORMAL;
    	hdma_usart3_tx.Init.Priority = DMA_PRIORITY_LOW;
    	hdma_usart3_tx.Init.FIFOMode = DMA_FIFOMODE_ENABLE;
    	hdma_usart3_tx.Init.FIFOThreshold = DMA_FIFO_THRESHOLD_FULL;
    	hdma_usart3_tx.Init.MemBurst = DMA_MBURST_SINGLE;
    	hdma_usart3_tx.Init.PeriphBurst = DMA_PBURST_SINGLE;
     
    	if( HAL_DMA_Init(&hdma_usart3_tx) != HAL_OK ) Error_Handler();
     
    	__HAL_LINKDMA(huart, hdmatx, hdma_usart3_tx);
     
    	/* enable uart interrupts */
    	USART3->CR1 |= USART_CR1_RXNEIE;
    	USART3->CR1 |= USART_CR1_TCIE;
     

    Another thing you might want to check is the receiver side: buffers big enough, speed settings okay?

    Graduate
    May 17, 2023

    Hi @Community member​, thanks for the hint will check from my side and update here for any misses.

    Visitor II
    January 18, 2024

    Hi @newbie_stm32  are u successfully receiving 1024 bytes???