cancel
Showing results for 
Search instead for 
Did you mean: 

UART Receive issue

EtienneLJ
Associate III

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

1 ACCEPTED SOLUTION

Accepted Solutions
EtienneLJ
Associate III

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 ?

View solution in original post

31 REPLIES 31

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
Up vote any posts that you find helpful, it shows what's working..

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

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
Up vote any posts that you find helpful, it shows what's working..

Hi,

I'm connecting RX and TX to an UART to USB chip (this one https://www.digikey.nl/nl/products/detail/sparkfun-electronics/DEV-15096/9817166?utm_adgroup=&utm_source=google&utm_medium=cpc&utm_campaign=PMax%20Shopping_Product_High%20ROAS&utm_term=&productid=9817166&utm_content=&utm_id=go_cmp-19662899439_adg-_ad-__dev-c_ext-_prd-9817166_sig-Cj0KCQjwqP2pBhDMARIs...) to send data from my computer to my custom board.

I've checked the output signal from this chip with the scope and I have 0 to 3.3V levels.

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 

I use HSI 16MHz as clock source for LPUART1.

I'm using an FTDI chip to send data from my laptop. I've analysed levels on RX pin with a scope and I get 0 to 3.3V levels.

EtienneLJ
Associate III

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.

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
Up vote any posts that you find helpful, it shows what's working..