Why does the UART initialization fail when the optimization is set to 0 or 1?
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));
}
}
