cancel
Showing results for 
Search instead for 
Did you mean: 

UART not receiving anything

Wleon.1
Associate II

Hi I have a code bellow:

static void MX_UART8_Init(void)
{
 
  /* USER CODE BEGIN UART8_Init 0 */
  GPIO_InitTypeDef GPIO_InitStruct = {0};
 
  /* USER CODE BEGIN UART8_MspInit 0 */
 
  /* USER CODE END UART8_MspInit 0 */
    /* Peripheral clock enable */
    __HAL_RCC_UART8_CLK_ENABLE();
  
    __HAL_RCC_GPIOE_CLK_ENABLE();
    /**UART8 GPIO Configuration    
    PE0     ------> UART8_RX
    PE1     ------> UART8_TX 
    */
    GPIO_InitStruct.Pin = GPIO_PIN_0|GPIO_PIN_1;
    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_UART8;
    HAL_GPIO_Init(GPIOE, &GPIO_InitStruct);
 
    /* UART8 interrupt Init */
    HAL_NVIC_SetPriority(UART8_IRQn, 0, 0);
    HAL_NVIC_EnableIRQ(UART8_IRQn);
  /* USER CODE BEGIN UART8_MspInit 1 */
 
  /* USER CODE END UART8_MspInit 1 */
 
  /* USER CODE END UART8_Init 0 */
 
  /* USER CODE BEGIN UART8_Init 1 */
 
  /* USER CODE END UART8_Init 1 */
  huart8.Instance = UART8;
  huart8.Init.BaudRate = 9600;
  huart8.Init.WordLength = UART_WORDLENGTH_8B;
  huart8.Init.StopBits = UART_STOPBITS_1;
  huart8.Init.Parity = UART_PARITY_NONE;
  huart8.Init.Mode = UART_MODE_TX_RX;
  huart8.Init.HwFlowCtl = UART_HWCONTROL_NONE;
  huart8.Init.OverSampling = UART_OVERSAMPLING_16;
  huart8.Init.OneBitSampling = UART_ONE_BIT_SAMPLE_DISABLE;
  huart8.AdvancedInit.AdvFeatureInit = UART_ADVFEATURE_NO_INIT;
  if (HAL_UART_Init(&huart8) != HAL_OK)
  {
    Error_Handler();
  }
  /* USER CODE BEGIN UART8_Init 2 */
 
  /* USER CODE END UART8_Init 2 */
 
}
 
static void InitPPSAndStart(void)
{
  /* EXTI interrupt init for PPS*/
  HAL_NVIC_SetPriority(EXTI9_5_IRQn, 0, 0);
  HAL_NVIC_EnableIRQ(EXTI9_5_IRQn);
  HAL_NVIC_DisableIRQ(EXTI9_5_IRQn);
  HAL_NVIC_ClearPendingIRQ(EXTI9_5_IRQn);
  HAL_NVIC_EnableIRQ(EXTI9_5_IRQn);
}
 
 
 
static void ctrlMgrLMLoop(void) {
  uint8_t statusFlag = 0;
  uint32_t frameRate = 1;//226.5;
  GPIO_PinState nx_arm_state;
  uint8_t bcam_stream = 0;
  int32_t i = 1;  
 
 
  /* stm trigger debug code start */
  ...
  InitPPSAndStart();
  MX_UART8_Init();
  HAL_TIM_PWM_Start(&htim4, TIM_CHANNEL_1);
 
 
  while(1)
  {
    Uartret = HAL_UART_Receive(&huart8, uartbuf, 1, HAL_MAX_DELAY);  <---- Stuck HERE 
    if(uartIntCnt > 0)
    {
      /*process buffer*/
 
      uartIntCnt--;
    }
 
    HAL_Delay(10);
     
  }
 
...
 
}

By running the code it got stuck at :

Uartret = HAL_UART_Receive(&huart8, uartbuf, 1, HAL_MAX_DELAY);

By tracing deep into the code I will eventually come and wait here:

/**
  * @brief  Handle UART Communication Timeout.
  * @param huart     UART handle.
  * @param Flag      Specifies the UART flag to check
  * @param Status    Flag status (SET or RESET)
  * @param Tickstart Tick start value
  * @param Timeout   Timeout duration
  * @retval HAL status
  */
HAL_StatusTypeDef UART_WaitOnFlagUntilTimeout(UART_HandleTypeDef *huart, uint32_t Flag, FlagStatus Status,
                                              uint32_t Tickstart, uint32_t Timeout)
{
  /* Wait until flag is set */
  while ((__HAL_UART_GET_FLAG(huart, Flag) ? SET : RESET) == Status) <--- loop here
  {
...

I have check the connection and the device, and I can see the device is giving out UART message, moreover the UART works on STM32F303-Nucleo-64 development board, my question is, is there any reason for the program to stall here ? and How can I debug futher ?

10 REPLIES 10

Which STM32?

Observe waveform directly on the given pin using oscilloscope/LA.

Read out and check/post the relevant GPIO and UART registers content.

JW

Wleon.1
Associate II

I am current testing on: STM32F733ZET

how do I read out the GPIO and UART register content?

I am using STM32CubeMX to generate code.

> how do I read out the GPIO and UART register content?

Usually in debugger.

Note, that observing the UART registers *while* UART is running might lead to data loss.

JW

Wleon.1
Associate II

Here is the Registers while running, the only thing change is the ISR Busy bit. the rest stays the same0693W000001qy8YQAQ.png

In UART_SR, ORE is set, whih means the UART already received some bytes which it did not "pick" from the data register. At the same time, RDR is 0 - do you receive all zeros?

It might also be that your baudrate is not what you think. UART_BRR is suspiciously low, indicating APB frequency of 21.35MHz - is this the case? Try to transmit and observe Tx pin using LA or oscilloscope.

Does it receive if you *don't* observe the UART registers in the debugger?

JW

Wleon.1
Associate II

I notice that the init is not properly done, after I do a fix on the following code:

  /* USER CODE END UART8_Init 0 */
 
  /* USER CODE BEGIN UART8_Init 1 */
  //static const UART_HandleTypeDef EmptyStruct;
  huart8 = (const UART_HandleTypeDef){0};  <-- FIX to flush out all garbage before init 
 
  /* USER CODE END UART8_Init 1 */
  huart8.Instance = UART8;
  huart8.Init.BaudRate = 9600;
  huart8.Init.WordLength = UART_WORDLENGTH_8B;
  huart8.Init.StopBits = UART_STOPBITS_1;
  huart8.Init.Parity = UART_PARITY_NONE;
  huart8.Init.Mode = UART_MODE_TX_RX;
  huart8.Init.HwFlowCtl = UART_HWCONTROL_NONE;
  huart8.Init.OverSampling = UART_OVERSAMPLING_16;
  huart8.Init.OneBitSampling = UART_ONE_BIT_SAMPLE_DISABLE;
  huart8.AdvancedInit.AdvFeatureInit = UART_ADVFEATURE_NO_INIT;
  if (HAL_UART_Init(&huart8) != HAL_OK)
  {
    Error_Handler();
  }

the register become:

0693W000001qyVMQAY.png

but still stall at the same place ...

What is the flag it is waiting for?

JW

Wleon.1
Associate II

waiting for data to arrive.

RXNE?

And are data arriving at the Rx pin? Did you observe it using oscilloscope/LA?

JW