cancel
Showing results for 
Search instead for 
Did you mean: 

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

Jerly
Associate

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));
}
}

1 ACCEPTED SOLUTION

Accepted Solutions
Sarra.S
ST Employee

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.

View solution in original post

4 REPLIES 4

Does what vs what?

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
Sarra.S
ST Employee

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.

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

Elaborate, please.

JW

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?