Skip to content

Commit 4e88698

Browse files
nordic-krchfabiobaltieri
authored andcommitted
drivers: serial: nrfx_uarte: Avoid 64 bit division
When converting timeout (in microseconds) to bauds use by the HW frame timeout use less accurate conversion but one that does not use 64 bit division. 64 bit division takes a lot of code and time and shoud be avoided. Signed-off-by: Krzysztof Chruściński <krzysztof.chruscinski@nordicsemi.no>
1 parent 69604fa commit 4e88698

1 file changed

Lines changed: 6 additions & 2 deletions

File tree

drivers/serial/uart_nrfx_uarte.c

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1846,9 +1846,13 @@ static void notify_uart_rx_rdy(const struct device *dev, size_t len)
18461846
#ifdef UARTE_HAS_FRAME_TIMEOUT
18471847
static uint32_t us_to_bauds(uint32_t baudrate, int32_t timeout)
18481848
{
1849-
uint64_t bauds = (uint64_t)baudrate * timeout / 1000000;
1849+
/* Divide baudrate by 1000 to avoid 64 bit division. This approach is not 100% accurate
1850+
* but error is insignificant (within few bauds). Precise timeout my be more important
1851+
* for higher baudrates but inaccuracy diminishes with higher baudrate.
1852+
*/
1853+
uint32_t bauds = ((baudrate / 1000) * timeout) / 1000;
18501854

1851-
return MIN((uint32_t)bauds, UARTE_FRAMETIMEOUT_COUNTERTOP_Msk);
1855+
return MIN(bauds, UARTE_FRAMETIMEOUT_COUNTERTOP_Msk);
18521856
}
18531857
#endif
18541858

0 commit comments

Comments
 (0)