@@ -125,7 +125,7 @@ FORCE_INLINE_ATTR void lp_uart_ll_get_sclk(uart_dev_t *hw, uart_sclk_t *source_c
125
125
* @param hw Address offset of the LP UART peripheral registers
126
126
* @param src_clk Source clock for the LP UART peripheral
127
127
*/
128
- static inline void lp_uart_ll_set_source_clk (uart_dev_t * hw , soc_periph_lp_uart_clk_src_t src_clk )
128
+ FORCE_INLINE_ATTR void lp_uart_ll_set_source_clk (uart_dev_t * hw , soc_periph_lp_uart_clk_src_t src_clk )
129
129
{
130
130
(void )hw ;
131
131
switch (src_clk ) {
@@ -141,9 +141,6 @@ static inline void lp_uart_ll_set_source_clk(uart_dev_t *hw, soc_periph_lp_uart_
141
141
}
142
142
}
143
143
144
- /// LP_CLKRST.lpperi is a shared register, so this function must be used in an atomic way
145
- #define lp_uart_ll_set_source_clk (...) (void)__DECLARE_RCC_ATOMIC_ENV; lp_uart_ll_set_source_clk(__VA_ARGS__)
146
-
147
144
/**
148
145
* @brief Configure the lp uart baud-rate.
149
146
*
@@ -155,18 +152,28 @@ static inline void lp_uart_ll_set_source_clk(uart_dev_t *hw, soc_periph_lp_uart_
155
152
*/
156
153
FORCE_INLINE_ATTR void lp_uart_ll_set_baudrate (uart_dev_t * hw , uint32_t baud , uint32_t sclk_freq )
157
154
{
158
- #define DIV_UP ( a , b ) (((a) + (b) - 1) / (b))
159
- const uint32_t max_div = BIT ( 12 ) - 1 ; // UART divider integer part only has 12 bits
160
- uint32_t sclk_div = DIV_UP ( sclk_freq , ( uint64_t ) max_div * baud );
155
+ // Constants
156
+ const uint32_t MAX_DIV_BITS = 12 ; // UART divider integer part bits
157
+ const uint32_t MAX_DIV = ( 1U << MAX_DIV_BITS ) - 1 ; // Maximum divider value
161
158
162
- if (sclk_div == 0 ) abort ();
159
+ // Ensure baud rate and sclk_freq are valid
160
+ if (baud == 0 || sclk_freq == 0 ) abort ();
163
161
164
- uint32_t clk_div = ((sclk_freq ) << 4 ) / (baud * sclk_div );
165
- // The baud rate configuration register is divided into
166
- // an integer part and a fractional part.
167
- hw -> clkdiv_sync .clkdiv_int = clk_div >> 4 ;
168
- hw -> clkdiv_sync .clkdiv_frag = clk_div & 0xf ;
162
+ // Calculate sclk divider
163
+ uint32_t sclk_div = (sclk_freq + MAX_DIV * baud - 1 ) / (MAX_DIV * baud );
164
+ if (sclk_div == 0 ) abort (); // Handle invalid sclk_div
165
+
166
+ // Calculate clk_div with integer and fractional parts
167
+ uint32_t clk_div = (sclk_freq * 16 ) / (baud * sclk_div );
168
+ uint32_t clk_div_int = clk_div >> 4 ; // Integer part
169
+ uint32_t clk_div_frac = clk_div & 0xF ; // Fractional part
170
+
171
+ // Configure hardware registers
172
+ hw -> clkdiv_sync .clkdiv_int = clk_div_int ;
173
+ hw -> clkdiv_sync .clkdiv_frag = clk_div_frac ;
169
174
HAL_FORCE_MODIFY_U32_REG_FIELD (hw -> clk_conf , sclk_div_num , sclk_div - 1 );
175
+
176
+ // Update UART hardware
170
177
uart_ll_update (hw );
171
178
}
172
179
@@ -176,15 +183,12 @@ FORCE_INLINE_ATTR void lp_uart_ll_set_baudrate(uart_dev_t *hw, uint32_t baud, ui
176
183
* @param hw_id LP UART instance ID
177
184
* @param enable True to enable, False to disable
178
185
*/
179
- static inline void _lp_uart_ll_enable_bus_clock (int hw_id , bool enable )
186
+ FORCE_INLINE_ATTR void lp_uart_ll_enable_bus_clock (int hw_id , bool enable )
180
187
{
181
188
(void )hw_id ;
182
189
LPPERI .clk_en .lp_uart_ck_en = enable ;
183
190
}
184
191
185
- /// LPPERI.clk_en is a shared register, so this function must be used in an atomic way
186
- #define lp_uart_ll_enable_bus_clock (...) (void)__DECLARE_RCC_ATOMIC_ENV; _lp_uart_ll_enable_bus_clock(__VA_ARGS__)
187
-
188
192
/**
189
193
* @brief Enable the UART clock.
190
194
*
0 commit comments