Why does the UART initialization fail when the optimization is set to 0 or 1?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content
2023-11-13 5:36 PM
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));
}
}
Solved! Go to Solution.
- Labels:
-
STM32Cube MCU Packages
Accepted Solutions
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content
2023-11-14 2:03 AM
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content
2023-11-13 6:55 PM
Does what vs what?
Up vote any posts that you find helpful, it shows what's working..
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content
2023-11-14 2:03 AM
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content
2023-11-14 8:17 AM
> Finally, it was found through checking the registers that the UART baud rate was not set effectively
Elaborate, please.
JW
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content
2023-11-14 5:14 PM
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?
