Clive One

STM32L4 LPUART1 Observations

Discussion created by Clive One on Dec 12, 2017

I've run into a couple of situations where LPUART1 initialization has ploughed into the Error_Handler() unexpectedly and so I've dug a bit deeper.

 

With nominal CPU and bus settings this has been observed when 9600 baud is selected, vs say 115200 baud, on code copied from other USART initialization, and only the Instance field being different.

 

The UART_Init() code does not compute BRR and PRESC automatically in a way that makes sense, instead you have to add a new ClockPrescaler field. In an ideal situation the library should compute factors that fit based on it's knowledge of the source clocks, and not the user's command of the minutia that makes LPUART1 vs USART2 different in certain contexts. This might be problematic when the baud rate is a user configurable parameter on the target system. I'm not say this can't be worked around, but it requires a level of awareness.

 

This code fails:

 

void LPUART1_Init(void) // STM32L4R9-DISCO Arduino Shield USART

{

  GPIO_InitTypeDef GPIO_InitStruct = { 0 };

 

  __LPUART1_CLK_ENABLE();

    __GPIOC_CLK_ENABLE();

 

    /* UART RX/TX GPIO pin configuration  */

  GPIO_InitStruct.Pin       = GPIO_PIN_0 | GPIO_PIN_1;

  GPIO_InitStruct.Mode      = GPIO_MODE_AF_PP;

  GPIO_InitStruct.Pull      = GPIO_PULLUP;

  GPIO_InitStruct.Speed     = GPIO_SPEED_HIGH;

  GPIO_InitStruct.Alternate = GPIO_AF8_LPUART1;

 

  HAL_GPIO_Init(GPIOC, &GPIO_InitStruct);

 

  /*## Configure the UART peripheral ######################################*/

  /* Put the USART peripheral in the Asynchronous mode (UART Mode) */

  /* UART1 configured as follow:

      - Word Length = 8 Bits

      - Stop Bit = One Stop bit

      - Parity = NO parity

      - BaudRate = 9600 baud

      - Hardware flow control disabled (RTS and CTS signals) */

  UartHandle.Instance        = LPUART1;

 

  UartHandle.Init.BaudRate   = 9600;

  UartHandle.Init.WordLength = UART_WORDLENGTH_8B;

  UartHandle.Init.StopBits   = UART_STOPBITS_1;

  UartHandle.Init.Parity     = UART_PARITY_NONE;

  UartHandle.Init.HwFlowCtl  = UART_HWCONTROL_NONE;

  UartHandle.Init.Mode       = UART_MODE_TX_RX;

 

  UartHandle.Init.OverSampling   = UART_OVERSAMPLING_16;

#ifdef UART_ONE_BIT_SAMPLE_DISABLE

  UartHandle.Init.OneBitSampling = UART_ONE_BIT_SAMPLE_DISABLE;

#endif

 

  if (HAL_UART_Init(&UartHandle) != HAL_OK)

  {

    /* Initialization Error */

    Error_Handler();

  }

}

 

Need to add this to keep the clock sufficiently in scope for the baud rate to be workable

 

#ifdef USART_PRESC_PRESCALER

  UartHandle.Init.ClockPrescaler = UART_PRESCALER_DIV4; //1,2,4,6,8,10,12,16,32,64,128,256

#endif

Outcomes