Skip to main content
Graduate
February 12, 2024
Solved

Unable to receive data over UART3

  • February 12, 2024
  • 3 replies
  • 2146 views

Hi,

I have the following board : 
NUCLEO-F429ZI

The datasheet says that UART3 is the STLINK UART that can be used over USB.
I configured this UART but when I try to receive anything from the terminal that is connected to the UART com port, I don't see the interrupt being triggered.

The objective is to receive some data over UART3, process it, and send the processed data over UART6.


UART3 init in main.c

/**
 * @brief USART3 Initialization Function
 * @PAram None
 * @retval None
 */
static void MX_USART3_UART_Init(void)
{

 /* USER CODE BEGIN USART3_Init 0 */

 /* USER CODE END USART3_Init 0 */

 /* USER CODE BEGIN USART3_Init 1 */

 /* USER CODE END USART3_Init 1 */
 huart3.Instance = USART3;
 huart3.Init.BaudRate = 115200;
 huart3.Init.WordLength = UART_WORDLENGTH_8B;
 huart3.Init.StopBits = UART_STOPBITS_1;
 huart3.Init.Parity = UART_PARITY_NONE;
 huart3.Init.Mode = UART_MODE_TX_RX;
 huart3.Init.HwFlowCtl = UART_HWCONTROL_NONE;
 huart3.Init.OverSampling = UART_OVERSAMPLING_16;
 if (HAL_UART_Init(&huart3) != HAL_OK)
 {
 Error_Handler();
 }
 /* USER CODE BEGIN USART3_Init 2 */

 /* USER CODE END USART3_Init 2 */

}

 hal_msp.c 

/**
* @brief UART MSP Initialization
* This function configures the hardware resources used in this example
* @PAram huart: UART handle pointer
* @retval None
*/
void HAL_UART_MspInit(UART_HandleTypeDef* huart)
{
 GPIO_InitTypeDef GPIO_InitStruct = {0};
 if(huart->Instance==USART3)
 {
 /* USER CODE BEGIN USART3_MspInit 0 */

 /* USER CODE END USART3_MspInit 0 */
 /* Peripheral clock enable */
 __HAL_RCC_USART3_CLK_ENABLE();

 __HAL_RCC_GPIOD_CLK_ENABLE();
 /**USART3 GPIO Configuration
 PD8 ------> USART3_TX
 PD9 ------> USART3_RX
 */
 GPIO_InitStruct.Pin = STLK_RX_Pin|STLK_TX_Pin;
 GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
 GPIO_InitStruct.Pull = GPIO_NOPULL;
 GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
 GPIO_InitStruct.Alternate = GPIO_AF7_USART3;
 HAL_GPIO_Init(GPIOD, &GPIO_InitStruct);

 /* USART3 interrupt Init */
 HAL_NVIC_SetPriority(USART3_IRQn, 0, 0);
 HAL_NVIC_EnableIRQ(USART3_IRQn);
 /* USER CODE BEGIN USART3_MspInit 1 */

 /* USER CODE END USART3_MspInit 1 */
 }
 else if(huart->Instance==USART6)
 {
 /* USER CODE BEGIN USART6_MspInit 0 */

 /* USER CODE END USART6_MspInit 0 */
 /* Peripheral clock enable */
 __HAL_RCC_USART6_CLK_ENABLE();

 __HAL_RCC_GPIOC_CLK_ENABLE();
 /**USART6 GPIO Configuration
 PC6 ------> USART6_TX
 PC7 ------> USART6_RX
 */
 GPIO_InitStruct.Pin = UART6_TX_Pin|UART6_RX_Pin;
 GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
 GPIO_InitStruct.Pull = GPIO_NOPULL;
 GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
 GPIO_InitStruct.Alternate = GPIO_AF8_USART6;
 HAL_GPIO_Init(GPIOC, &GPIO_InitStruct);

 /* USART6 interrupt Init */
 HAL_NVIC_SetPriority(USART6_IRQn, 0, 0);
 HAL_NVIC_EnableIRQ(USART6_IRQn);
 /* USER CODE BEGIN USART6_MspInit 1 */

 /* USER CODE END USART6_MspInit 1 */
 }

}

 

 

Application code : 

int main(void)
{
 /* USER CODE BEGIN 1 */

 /* USER CODE END 1 */

 /* MCU Configuration--------------------------------------------------------*/

 /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
 HAL_Init();

 /* USER CODE BEGIN Init */

 /* USER CODE END Init */

 /* Configure the system clock */
 SystemClock_Config();

 /* USER CODE BEGIN SysInit */

 /* USER CODE END SysInit */

 /* Initialize all configured peripherals */
 MX_GPIO_Init();
 MX_USART3_UART_Init();
 MX_USART6_UART_Init();
 /* USER CODE BEGIN 2 */

 // Start receiving data in interrupt mode
 // HAL_UART_Receive(&huart3, rx_buffer, sizeof(rx_buffer),HAL_MAX_DELAY);

 if(HAL_UART_Receive_IT(&huart3, rx_buffer, sizeof(rx_buffer)) != HAL_OK)
 {
 Error_Handler();
 }

 // Transmit some data
// char *message = "Hello World\n";

 /* USER CODE END 2 */

 /* Infinite loop */
 /* USER CODE BEGIN WHILE */
 while (1)
 {
 /* USER CODE END WHILE */

 /* USER CODE BEGIN 3 */
 }
 /* USER CODE END 3 */
}

 

Callback to process the received cmd : 

// This function is called when data is received
void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart)
{
 if (huart->Instance == USART3)
 {
 // If data is received from UART3, send it to UART6
// HAL_UART_Transmit(&huart6, rx_buffer, sizeof(rx_buffer), HAL_MAX_DELAY);
 if(HAL_UART_Transmit_IT(&huart6, rx_buffer, sizeof(rx_buffer)) != HAL_OK)
 {
 Error_Handler();
 }
 // Restart the interrupt reception on UART3
 HAL_UART_Receive_IT(&huart3, rx_buffer, sizeof(rx_buffer));
 }
}

 

I don't understand what is the problem.
Please help and ask for any other details.

    This topic has been closed for replies.
    Best answer by smaiti

    @Pavel A. @Bob S ,
    Thanks for the insights.
    I changed my strategy in the following ways : 

    #define BUFFER_SIZE 1
    
    //calling the following inside main()
     if(HAL_UART_Receive_IT(&huart3, &parentRx, BUFFER_SIZE) != HAL_OK)
     {
     Error_Handler();
     }
    
    /**
     * @brief Callback function called when UART receive is complete.
     * 
     * @param huart Pointer to the UART handle structure.
     */
    void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart)
    {
     if (huart->Instance == USART3)
     {
     parentMessageSts = Motor_Cmd_Recieve_IT(parentRx, parentRxBuf,sizeof(parentRxBuf),&parentRxPtr);
    
     if(HAL_UART_Receive_IT(&huart3, &parentRx, BUFFER_SIZE) != HAL_OK)
     {
     Error_Handler();
     }
     }
    }
    
    // Motor_Cmd_Receive processes the incoming commands and stores it in a cmd buffe to be processed by another function
    // This approach seems to be working.

    3 replies

    Super User
    February 12, 2024

    Please start from polling mode RX and TX first. Will it work?

    HAL_UART_Receive(&huart3,...)
    HAL_UART_Transmit(&huart6,...)
    smaitiAuthor
    Graduate
    February 12, 2024

    Hi @Pavel A.,

    Thanks for your reply.
    I tried it but it also does not seem to be working.
    I used HAL_DELAY_MAX in the time interval.
    I also tested just the transmission on UART3 using the HAL_Transmit function in the while(1) loop.
    I am getting the buffer on a terminal that is connected to this UART.
    So not sure why this is happening.
    Regards
    smaiti

    Super User
    February 12, 2024

    How big is rx_buffer?  You do realize that you won't see the interrupt until you have received sizeof(rx_buffer) number of bytes, right?

    Also, with your original code that uses interrupts, in the callback you start sending from rx_buffer, and then immediately start receiving new data into that same rx_buffer.  HAL_UART_Transmit_IT() returns immediately, before the data has been sent.

    smaitiAuthorAnswer
    Graduate
    February 14, 2024

    @Pavel A. @Bob S ,
    Thanks for the insights.
    I changed my strategy in the following ways : 

    #define BUFFER_SIZE 1
    
    //calling the following inside main()
     if(HAL_UART_Receive_IT(&huart3, &parentRx, BUFFER_SIZE) != HAL_OK)
     {
     Error_Handler();
     }
    
    /**
     * @brief Callback function called when UART receive is complete.
     * 
     * @param huart Pointer to the UART handle structure.
     */
    void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart)
    {
     if (huart->Instance == USART3)
     {
     parentMessageSts = Motor_Cmd_Recieve_IT(parentRx, parentRxBuf,sizeof(parentRxBuf),&parentRxPtr);
    
     if(HAL_UART_Receive_IT(&huart3, &parentRx, BUFFER_SIZE) != HAL_OK)
     {
     Error_Handler();
     }
     }
    }
    
    // Motor_Cmd_Receive processes the incoming commands and stores it in a cmd buffe to be processed by another function
    // This approach seems to be working.
    Visitor II
    February 14, 2024