cancel
Showing results for 
Search instead for 
Did you mean: 

LPUART1 Issue

RrW1
Associate III

Hi,

I am learning how to use LPUART1 on the Nucleo-64 (STM32L433RCT6P) to generate messages on the COM4 port of my Windows 10 laptop. A "Hello World\r\n" message should be printed every time the blue User Button on the board gets pressed.

Here are the relevant variables used:

UART_HandleTypeDef hlpuart1;
 
/* Buffer used for transmission */
uint8_t aTxBuffer[13] = "Hello World\r\n";
 
/* Flag for push button */
__IO FlagStatus xPush = RESET;

The LPUART1 Initialization:

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 = 9600;
  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.AdvancedInit.AdvFeatureInit = UART_ADVFEATURE_NO_INIT;
  if (HAL_UART_Init(&hlpuart1) != HAL_OK)
  {
    Error_Handler();
  }
  /* USER CODE BEGIN LPUART1_Init 2 */
 
  /* USER CODE END LPUART1_Init 2 */
 
}

The main code:

int main(void)
{
  /* MCU Configuration--------------------------------------------------------*/
  /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
  HAL_Init();
 
  /* Configure the system clock */
  SystemClock_Config();
 
  /* Initialize all configured peripherals */
  MX_GPIO_Init();
  MX_LPUART1_UART_Init();
 
  /* Infinite loop */
  while (1)
  {
                /* xPush is a variable that is SET inside a GPIO IRQ Handler */
		if(xPush == SET)
		{
			if(HAL_UART_Transmit(&hlpuart1, (uint8_t*)aTxBuffer, 13, 5000)!= HAL_OK)
			{
				Error_Handler();   
			}
			
			HAL_Delay(100);
			
			xPush = RESET;
		}
  }
 
}

Here is the clock initialization (I am using HSI16 as the system clock, and the PCLK1 powers the LPUART1):

void SystemClock_Config(void)
{
  RCC_OscInitTypeDef RCC_OscInitStruct = {0};
  RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
  RCC_PeriphCLKInitTypeDef PeriphClkInit = {0};
 
  /** Initializes the RCC Oscillators according to the specified parameters
  * in the RCC_OscInitTypeDef structure.
  */
  RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI|RCC_OSCILLATORTYPE_LSI;
  RCC_OscInitStruct.HSIState = RCC_HSI_ON;
  RCC_OscInitStruct.HSICalibrationValue = RCC_HSICALIBRATION_DEFAULT;
  RCC_OscInitStruct.LSIState = RCC_LSI_ON;
  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_DIV4;				// LPUART1
  RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV8;				// USART1
 
  if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_0) != HAL_OK)
  {
    Error_Handler();
  }
  PeriphClkInit.PeriphClockSelection = RCC_PERIPHCLK_USART1|RCC_PERIPHCLK_LPTIM1|RCC_PERIPHCLK_LPUART1;
  PeriphClkInit.Usart1ClockSelection = RCC_USART1CLKSOURCE_PCLK2;
  PeriphClkInit.Lptim1ClockSelection = RCC_LPTIM1CLKSOURCE_LSI;
	PeriphClkInit.Lpuart1ClockSelection = RCC_LPUART1CLKSOURCE_PCLK1;
  if (HAL_RCCEx_PeriphCLKConfig(&PeriphClkInit) != HAL_OK)
  {
    Error_Handler();
  }
  /** Configure the main internal regulator output voltage
  */
  if (HAL_PWREx_ControlVoltageScaling(PWR_REGULATOR_VOLTAGE_SCALE1) != HAL_OK)
  {
    Error_Handler();
  }
}

And this is what I am seeing in the PuTTY Terminal with one button press:

0693W000005AGrpQAG.png 

I believe this is caused by a mismatch in the baud rate, even though the LPUART1's baud rate configuration is 9600 and the PuTTY's baud rate configuration is 9600. I also matched and verified that all the parameters are the same: 8 Data bits (in PuTTY) where LPUART1 has wordlength 8 bits (including parity, although parity is 0), 1 stop bits on both PuTTY and LPUART1, no parity on both, and no flow control on both. Here is the PuTTY configuration:

0693W000005AGruQAG.pngAnd here is a screenshot of the Device Manager showing that COM4 is connected to the board's ST-LINK. I also disconnected/reconnected the STM32 from my laptop to verify that COM4 is the port that I should be using:

0693W000005AGs4QAG.png 

I have also read the user manual here and confirmed that LPUART1 has a connection with the ST-LINK, and that SB66 and SB75 is soldered. Regarding the pins that I am using, I am using PA2 as LPUART1_TX and PA3 as LPUART1_RX, both configured to Alternate Function 8 (LPUART1) with no pull-up or pull-down resistors.

What could be the issue here? I tried varying the baud rate on PuTTY to initially make sure that the amount of characters I receive and I am sending are the same based on a suggestion here. I also tried increasing the APB1 clock peripheral's divider (PCLK1 clocks the LPUART1 here). Would using HSI also be fine in this application?

Any help would be appreciated. Thanks!

12 REPLIES 12

Hi,

I confirm the issue reported by @RrW​ on my STM32L433RCT6P board.

I've generated an empty project inside STM32CubeIDE with the default pin configuration.

I try to send a message through UART2 at 115200 and checking with logic analyzer I saw the same behavior.

In order to receive the message sent by NUCLEO I had to set up Putty to work with a baud rate of 130000 (about 14% higher than 115200)

Thanks

> If the baud rate in this case is off by 14%, does that mean that the clock might be off by 14% too (14% higher than 16MHz)?

It sure does. In fact, that's the only explanation that makes sense to me.

> Is there also a way to scope the HSI16 clock rate?

You're effectively already doing this, but you could route HSI to the MCO output and scope it there. Or use it as the clock source for an LPTIM in PWM mode and scope it there.

If you feel a post has answered your question, please click "Accept as Solution".
RrW1
Associate III

Thanks for the explanation, and for the suggestion on scoping out the HSI16 to the MCO output. I might try that to get a better sense of the exact clock speed of this microcontroller. I just hope it does not affect other peripherals badly (like I2C and SPI), and it does not affect/prevent the microcontroller going into Low Power Run Mode.

Thanks @SCons.1​ for confirming that this is not just a problem on my microcontroller.