Skip to main content
Associate
November 14, 2023
Solved

Why does the UART initialization fail when the optimization is set to 0 or 1?

  • November 14, 2023
  • 3 replies
  • 1567 views

Hi:

Now, I am using STM32C031. When debugging UART, it works normally when the optimization setting is set to -O2 or -O3, but fails to work when set to -O0 or -O1. Finally, it was found through checking the registers that the UART baud rate was not set effectively when optimization was set to -O0 or -O1, and the UART baud rate setting used an inline function. Can it be explained whether there is any correlation between optimization and inline, and why setting optimization would cause UART initialization to fail? The code as follow:

__STATIC_INLINE void LL_USART_SetBaudRate(USART_TypeDef *USARTx, uint32_t PeriphClk, uint32_t PrescalerValue,
uint32_t OverSampling,
uint32_t BaudRate)
{
uint32_t usartdiv;
uint32_t brrtemp;

if (PrescalerValue > LL_USART_PRESCALER_DIV256)
{
/* Do not overstep the size of USART_PRESCALER_TAB */
}
else if (BaudRate == 0U)
{
/* Can Not divide per 0 */
}
else if (OverSampling == LL_USART_OVERSAMPLING_8)
{
usartdiv = (uint16_t)(__LL_USART_DIV_SAMPLING8(PeriphClk, (uint8_t)PrescalerValue, BaudRate));
brrtemp = usartdiv & 0xFFF0U;
brrtemp |= (uint16_t)((usartdiv & (uint16_t)0x000FU) >> 1U);
USARTx->BRR = brrtemp;
}
else
{
USARTx->BRR = (uint16_t)(__LL_USART_DIV_SAMPLING16(PeriphClk, (uint8_t)PrescalerValue, BaudRate));
}
}

This topic has been closed for replies.
Best answer by Sarra.S

Hello @Jerly

>> Whether there is any correlation between optimization and inline

yes, inlining of functions is an optimization and it really “works” only in optimizing compilation. If you don't use -O, no function is really inline.

 

3 replies

Tesla DeLorean
Guru
November 14, 2023

Does what vs what?

Tips, Buy me a coffee, or three.. PayPal VenmoUp vote any posts that you find helpful, it shows what's working..
Sarra.SBest answer
ST Employee
November 14, 2023

Hello @Jerly

>> Whether there is any correlation between optimization and inline

yes, inlining of functions is an optimization and it really “works” only in optimizing compilation. If you don't use -O, no function is really inline.

 

To give better visibility on the answered topics, please click on Accept as Solution on the reply which solved your issue or answered your question.
JerlyAuthor
Associate
November 15, 2023

Hi Sarra.S:

         Thank you for replying. Can you explain the relationship between inlining of functions and optimization? In normal circumstances, how should it be configured and are there any relevant resources explaining this?

       

waclawek.jan
Super User
November 14, 2023

> Finally, it was found through checking the registers that the UART baud rate was not set effectively

Elaborate, please.

JW