Skip to main content
Associate
October 30, 2023
Solved

UART Receive issue

  • October 30, 2023
  • 31 replies
  • 10180 views

Hello,

I'm struggling with a really strange issue. I'm trying to make UART RX working on a STM32G4 custom board.

I've followed the UART Interrupt tutorial. (https://wiki.st.com/stm32mcu/wiki/Getting_started_with_UART)

I've tested it with Nucleo-G431RB and it works fine.

Now I'm running the exact same code on my custom board and I have nothing, when using the debugger I can see that the interrupt is never fired. I've check hardware connections and everything seems OK.

I've also tried to implement UART_Receive by polling mode => I can see in the debugger that characters are received, so Hardware is OK.

Can someone help me with this issue ?

Thanks

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

I found a solution by adding "#define USER_VECT_TAB_ADDRESS" to the beginning of system_stm32g4xx.c. The interrupt vector table was not set correctly.

So what I did wrong when I've generate the code with MXCube ?

And why my old code was working on the Nucleo and not on my custom board ?

31 replies

Tesla DeLorean
Guru
October 30, 2023

Perhaps there is a speed mismatch. Perhaps send a repetitive 'U' pattern and scope the signal.

Then try looping back the TX to RX.

What and how are you connecting to your current data source. The MCU is not designed to take RS232 level signals directly. 

Tips, Buy me a coffee, or three.. PayPal Venmo (See Profile) Up vote any posts that you find helpful, it shows what's working..
Tesla DeLorean
Guru
October 30, 2023

On Custom board is clock source (HSE) different? Have you changed HSE_VALUE define to reflect this? Baud rate computation depends on this being correct.

Tips, Buy me a coffee, or three.. PayPal Venmo (See Profile) Up vote any posts that you find helpful, it shows what's working..
EtienneLJAuthor
Associate
October 30, 2023

I use HSI 16MHz as clock source for LPUART1.

waclawek.jan
Super User
October 30, 2023

What's the transmitter?

Prepare a *minimum* but complete compilable example exhibiting the problem (i.e. works on Nucleo, doesn't work on your board) and post.

JW

EtienneLJAuthor
Associate
October 30, 2023

I use my PC through a UART to USB chip.

/* USER CODE BEGIN Header */
/**
 ******************************************************************************
 * @file : main.c
 * @brief : Main program body
 ******************************************************************************
 * @attention
 *
 * Copyright (c) 2023 STMicroelectronics.
 * All rights reserved.
 *
 * This software is licensed under terms that can be found in the LICENSE file
 * in the root directory of this software component.
 * If no LICENSE file comes with this software, it is provided AS-IS.
 *
 ******************************************************************************
 */
/* USER CODE END Header */
/* Includes ------------------------------------------------------------------*/
#include "main.h"

/* Private includes ----------------------------------------------------------*/
/* USER CODE BEGIN Includes */
#include <string.h>

/* USER CODE END Includes */

/* Private typedef -----------------------------------------------------------*/
/* USER CODE BEGIN PTD */

/* USER CODE END PTD */

/* Private define ------------------------------------------------------------*/
/* USER CODE BEGIN PD */

/* USER CODE END PD */

/* Private macro -------------------------------------------------------------*/
/* USER CODE BEGIN PM */

/* USER CODE END PM */

/* Private variables ---------------------------------------------------------*/
UART_HandleTypeDef hlpuart1;

/* USER CODE BEGIN PV */

char rcv_msg[20];
unsigned char msg_ptr;
uint8_t rcv_char;
const char delimiter = '\n';
unsigned char msg_available = 0;

/* USER CODE END PV */

/* Private function prototypes -----------------------------------------------*/
void SystemClock_Config(void);
static void MX_GPIO_Init(void);
static void MX_LPUART1_UART_Init(void);
/* USER CODE BEGIN PFP */

/* USER CODE END PFP */

/* Private user code ---------------------------------------------------------*/
/* USER CODE BEGIN 0 */

void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart)
{
	msg_available = 1;
	if(rcv_char == '\n')
	{
		msg_available = 1;
		msg_ptr = 0;
	}
	else
	{
		rcv_msg[msg_ptr] = rcv_char;
		msg_ptr++;
	}
	HAL_UART_Receive_IT(&hlpuart1, (uint8_t*)&rcv_char, 1);
}


/* USER CODE END 0 */

/**
 * @brief The application entry point.
 * @retval int
 */
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_LPUART1_UART_Init();
 /* USER CODE BEGIN 2 */


 //printf("Hello World %d\r\n", test);
 HAL_UART_Transmit(&hlpuart1, (uint8_t*)"Hello World\r\n", 13, 1000u);

 HAL_UART_Receive_IT(&hlpuart1, (uint8_t*)&rcv_char, 1);

 /* USER CODE END 2 */

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

 /* USER CODE BEGIN 3 */
 	if(msg_available)
 	{
 		msg_available = 0;
 		HAL_UART_Transmit(&hlpuart1, (uint8_t*)"msg_available\r\n", 15, 1000u);
 		if(strstr(rcv_msg ,"MOTOR"))
 		{
 			HAL_UART_Transmit(&hlpuart1, (uint8_t*)"MOTOR test has been selected\r\n", 31, 1000u);
 		}
 		else if(strstr(rcv_msg ,"TEMP"))
 		{
 			HAL_UART_Transmit(&hlpuart1, (uint8_t*)"TEMP test has been selected\r\n", 31, 1000u);
 		}
 		else if(strstr(rcv_msg ,"SONAR"))
 			{
 			HAL_UART_Transmit(&hlpuart1, (uint8_t*)"SONAR test has been selected\r\n", 31, 1000u);
 			}
 		else if(strstr(rcv_msg ,"CONTACT_SENS"))
 			{
 			HAL_UART_Transmit(&hlpuart1, (uint8_t*)"CONTACT_SENS test has been selected\r\n", 37, 1000u);
 			}
 		else if(strstr(rcv_msg ,"WEIGHT"))
 			{
 			HAL_UART_Transmit(&hlpuart1, (uint8_t*)"WEIGHT test has been selected\r\n", 31, 1000u);
 			}
 		else if(strstr(rcv_msg ,"ENCODER"))
 			{
 			HAL_UART_Transmit(&hlpuart1, (uint8_t*)"ENCODER test has been selected\r\n", 32, 1000u);
 			}
 		else if(strstr(rcv_msg ,"STEPPER"))
 			{
 			HAL_UART_Transmit(&hlpuart1, (uint8_t*)"STEPPER test has been selected\r\n", 32, 1000u);
 			}
 		else if(strstr(rcv_msg ,"T_HUM"))
 			{
 			HAL_UART_Transmit(&hlpuart1, (uint8_t*)"TEMP_HUM test has been selected\r\n", 33, 1000u);
 			}
 		rcv_msg[0] = '\0';
 	}
 }
 /* USER CODE END 3 */
}

/**
 * @brief System Clock Configuration
 * @retval None
 */
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_HSI;
 RCC_OscInitStruct.HSIState = RCC_HSI_ON;
 RCC_OscInitStruct.HSICalibrationValue = RCC_HSICALIBRATION_DEFAULT;
 RCC_OscInitStruct.PLL.PLLState = RCC_PLL_NONE;
 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_HSI;
 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_0) != 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 GPIO Initialization Function
 * @PAram None
 * @retval None
 */
static void MX_GPIO_Init(void)
{
/* USER CODE BEGIN MX_GPIO_Init_1 */
/* USER CODE END MX_GPIO_Init_1 */

 /* GPIO Ports Clock Enable */
 __HAL_RCC_GPIOA_CLK_ENABLE();

/* 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)
 {
 }
 /* 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 */

 

Here's my complete main.c code 

EtienneLJAuthor
Associate
October 30, 2023

I've tried to communicate from my laptop to the Nucleo via the FTDI chip by using an other UART port on nucleo and it is working fine.

Tesla DeLorean
Guru
October 30, 2023

msg_available and anything else changed and depended upon under interrupt/callback context should be volatile.

Check for error/status issues that might block reception, ie noise, parity, framing, etc as these need to be explicitly cleared. Would recommend a pull-up on RX pin

Check for failure returns from HAL_UART_xxx() functions.

Tips, Buy me a coffee, or three.. PayPal Venmo (See Profile) Up vote any posts that you find helpful, it shows what's working..
EtienneLJAuthor
Associate
October 30, 2023

How do I check parity / framing ?

Tesla DeLorean
Guru
October 30, 2023

Would suggest inspecting LPUART1->ISR

Tips, Buy me a coffee, or three.. PayPal Venmo (See Profile) Up vote any posts that you find helpful, it shows what's working..
EtienneLJAuthor
Associate
October 30, 2023

So ISR register goes from 0x6000d0 to 0x6000f8 after sending on RX and debbug stop.

Then after rerun and stop debbug I get 0x6000d8.

Tesla DeLorean
Guru
October 31, 2023

Here's the Reference Manual, explains the bit flags. The 8 (bit 3) is the Overrun Error

https://www.st.com/resource/en/reference_manual/rm0440-stm32g4-series-advanced-armbased-32bit-mcus-stmicroelectronics.pdf

 

g4_uart_isr.jpg

Tips, Buy me a coffee, or three.. PayPal Venmo (See Profile) Up vote any posts that you find helpful, it shows what's working..
EtienneLJAuthor
Associate
October 31, 2023

Ok but what can be the root cause ? I seems to be a software, as I uderstand it. (SO it does not explain why it is working on the nucleo board)

When I send only one char the ORE is not set but interrupt is still not triggered.

waclawek.jan
Super User
October 31, 2023

> Ok but what can be the root cause ?

ORE is set when two or more bytes arrive to the receiver without the program reading them out from Rx data register.

There may be noise, e.g. if you connect connectors, or there may be bad ground connection... Oscilloscope is your friend.

> When I send only one char the ORE is not set but interrupt is still not triggered.

If you observe the UART Rx data register in debugger, the RXNE flag gets cleared... debugging is intrusive.

JW

EtienneLJAuthor
Associate
October 31, 2023

There may be noise, e.g. if you connect connectors, or there may be bad ground connection... Oscilloscope is your friend.

Here's a picture of the signal received on RX pin.

NewFile1.png

Signal seems OK.

If you observe the UART Rx data register in debugger, the RXNE flag gets cleared...

I don't get into the interrupt handler with only one char and without debugger 

EtienneLJAuthor
Associate
October 31, 2023

So I've changed from LPUART1 to USART3 and now I don't get the ORE error when sending a char. But still no interrupt fired...

EDIT : ORE error with multiple chars as with LPUART1

waclawek.jan
Super User
October 31, 2023

The waveform you've posted above is 2 bytes (0x4D 0x0A), that would explain the overrun.

Here is a generic checklist to troubleshoot "interrupt does not fire" problems.

JW

EtienneLJAuthor
Associate
October 31, 2023

I've setup an interrupt on a GPIO and it doesn't fire also. 

waclawek.jan
Super User
October 31, 2023

Then you have disabled interrupts globally, or your program entirely runs in some other interrupt of same or higher priority, or your program never reached the code enabling the interrupts.

If you pause execution, where does the PC (Program Counter) point to?

JW

EtienneLJAuthor
Associate
October 31, 2023

It points to the if statement of the while(1) loop in main.c.

The program never reaches HAL_EXTI_IRQHandler. (I tried to toggle a GPIO in it)

Tesla DeLorean
Guru
October 31, 2023

EXTI ?

Start by establishing the EXTIxx_IRQHandler() that's called by the NVIC, you are then responsible for calling into the HAL which then calls your function.

Tips, Buy me a coffee, or three.. PayPal Venmo (See Profile) Up vote any posts that you find helpful, it shows what's working..
EtienneLJAuthor
Associate
October 31, 2023

EXTI9_5_IRQHandler what should points to this function ? 

EtienneLJAuthor
Associate
October 31, 2023

Looking at EXTI registers. the bit 5 of EXTI_FTSR1 is well set and when applying a 0 level on this pin the bit 5 of EXTI_PR1 is set to 1.