@@ -98,6 +98,16 @@ LOG_MODULE_REGISTER(uart_nrfx_uarte, CONFIG_UART_LOG_LEVEL);
9898#define UARTE_ANY_CACHE 1
9999#endif
100100
101+ #ifdef UARTE_ANY_CACHE
102+ /* uart120 instance does not retain BAUDRATE register when ENABLE=0. When this instance
103+ * is used then baudrate must be set after enabling the peripheral and not before.
104+ * This approach works for all instances so can be generally applied when uart120 is used.
105+ * It is not default for all because it costs some resources. Since currently only uart120
106+ * needs cache, that is used to determine if workaround shall be applied.
107+ */
108+ #define UARTE_BAUDRATE_RETENTION_WORKAROUND 1
109+ #endif
110+
101111/*
102112 * RX timeout is divided into time slabs, this define tells how many divisions
103113 * should be made. More divisions - higher timeout accuracy and processor usage.
@@ -182,6 +192,9 @@ struct uarte_nrfx_int_driven {
182192struct uarte_nrfx_data {
183193#ifdef CONFIG_UART_USE_RUNTIME_CONFIGURE
184194 struct uart_config uart_config ;
195+ #ifdef UARTE_BAUDRATE_RETENTION_WORKAROUND
196+ nrf_uarte_baudrate_t nrf_baudrate ;
197+ #endif
185198#endif
186199#ifdef UARTE_INTERRUPT_DRIVEN
187200 struct uarte_nrfx_int_driven * int_driven ;
@@ -381,7 +394,6 @@ static int baudrate_set(const struct device *dev, uint32_t baudrate)
381394 const struct uarte_nrfx_config * config = dev -> config ;
382395 /* calculated baudrate divisor */
383396 nrf_uarte_baudrate_t nrf_baudrate = NRF_BAUDRATE (baudrate );
384- NRF_UARTE_Type * uarte = get_uarte_instance (dev );
385397
386398 if (nrf_baudrate == 0 ) {
387399 return - EINVAL ;
@@ -392,7 +404,13 @@ static int baudrate_set(const struct device *dev, uint32_t baudrate)
392404 nrf_baudrate /= config -> clock_freq / NRF_UARTE_BASE_FREQUENCY_16MHZ ;
393405 }
394406
395- nrf_uarte_baudrate_set (uarte , nrf_baudrate );
407+ #ifdef UARTE_BAUDRATE_RETENTION_WORKAROUND
408+ struct uarte_nrfx_data * data = dev -> data ;
409+
410+ data -> nrf_baudrate = nrf_baudrate ;
411+ #else
412+ nrf_uarte_baudrate_set (get_uarte_instance (dev ), nrf_baudrate );
413+ #endif
396414
397415 return 0 ;
398416}
@@ -572,6 +590,13 @@ static void uarte_enable(const struct device *dev, uint32_t act_mask, uint32_t s
572590 }
573591#endif
574592 nrf_uarte_enable (get_uarte_instance (dev ));
593+ #if UARTE_BAUDRATE_RETENTION_WORKAROUND
594+ nrf_uarte_baudrate_t baudrate = COND_CODE_1 (CONFIG_UART_USE_RUNTIME_CONFIGURE ,
595+ (data -> nrf_baudrate ),
596+ (((const struct uarte_nrfx_config * )dev -> config )-> nrf_baudrate ));
597+
598+ nrf_uarte_baudrate_set (get_uarte_instance (dev ), baudrate );
599+ #endif
575600}
576601
577602/* At this point we should have irq locked and any previous transfer completed.
@@ -2153,6 +2178,11 @@ static int uarte_nrfx_pm_action(const struct device *dev,
21532178 }
21542179
21552180 nrf_uarte_enable (uarte );
2181+ #if UARTE_BAUDRATE_RETENTION_WORKAROUND
2182+ nrf_uarte_baudrate_set (get_uarte_instance (dev ),
2183+ COND_CODE_1 (CONFIG_UART_USE_RUNTIME_CONFIGURE ,
2184+ (data -> nrf_baudrate ), (cfg -> nrf_baudrate ));
2185+ #endif
21562186
21572187#ifdef UARTE_ANY_ASYNC
21582188 if (data -> async ) {
0 commit comments