@@ -170,6 +170,29 @@ static int uart_esp32_err_check(const struct device *dev)
170170}
171171
172172#ifdef CONFIG_UART_USE_RUNTIME_CONFIGURE
173+
174+ static uint32_t uart_esp32_get_standard_baud (uint32_t calc_baud )
175+ {
176+ const uint32_t standard_bauds [] = {9600 , 14400 , 19200 , 38400 , 57600 ,
177+ 74880 , 115200 , 230400 , 460800 , 921600 };
178+ int num_bauds = ARRAY_SIZE (standard_bauds );
179+ uint32_t baud = calc_baud ;
180+
181+ /* Find the standard baudrate within 0.1% range. If no close
182+ * value is found, input is returned.
183+ */
184+ for (int i = 0 ; i < num_bauds ; i ++ ) {
185+ float range = (float )abs (calc_baud - standard_bauds [i ]) / standard_bauds [i ];
186+
187+ if (range < 0.001f ) {
188+ baud = standard_bauds [i ];
189+ break ;
190+ }
191+ }
192+
193+ return baud ;
194+ }
195+
173196static int uart_esp32_config_get (const struct device * dev , struct uart_config * cfg )
174197{
175198 struct uart_esp32_data * data = dev -> data ;
@@ -179,11 +202,14 @@ static int uart_esp32_config_get(const struct device *dev, struct uart_config *c
179202 uart_hw_flowcontrol_t hw_flow ;
180203 uart_sclk_t src_clk ;
181204 uint32_t sclk_freq ;
205+ uint32_t calc_baud ;
182206
183207 uart_hal_get_sclk (& data -> hal , & src_clk );
184208 esp_clk_tree_src_get_freq_hz ((soc_module_clk_t )src_clk ,
185209 ESP_CLK_TREE_SRC_FREQ_PRECISION_CACHED , & sclk_freq );
186- uart_hal_get_baudrate (& data -> hal , & cfg -> baudrate , sclk_freq );
210+
211+ uart_hal_get_baudrate (& data -> hal , & calc_baud , sclk_freq );
212+ cfg -> baudrate = uart_esp32_get_standard_baud (calc_baud );
187213
188214 uart_hal_get_parity (& data -> hal , & parity );
189215 switch (parity ) {
0 commit comments