2023-01-05 06:23 AM
Hi everyone,
Like most of embedded work, our project aims to have the smallest size possible.
Looking at what I can optimize, I came across the LL_USART_Init function that is generated by CubeMx.
This function uses LL_USART-SetBaudRate which needs to use Division for unsigned long (_udivmoddi4). On a STM32F4 project, _udivmoddi4 takes 720 bytes of flash space!!
Does anyone have a simple trick to get around this apart from getting rid of CubeMX initialisation and never use LL_USART_SetBaudRate ?
2023-01-05 10:54 AM
If the target baud rate is constant - just define the clock frequency as a constant (forget the variable) and use constant expression to initialize BRR:
#define USART_CLK 48000000u
#define BAUD_RATE 115200u
USART1->BRR = (USART_CLK + BAUD_RATE / 2) / BAUD_RATE;
USART1->CR1 = USART_CR1_TE | USART_CR1_RE | USART_CR1_UE;
These two lines are enough for simple UART init, so you may also forget any fancy HAL/LL UART_Init routines.