From a98136628cab7dd063495e626bdb94e347c3a633 Mon Sep 17 00:00:00 2001 From: graham sanderson Date: Thu, 28 Jan 2021 10:01:12 -0600 Subject: [PATCH] uart_set_baudrate should return actual rate set even in case of out of range parameters --- src/common/pico_base/include/pico/error.h | 1 + src/rp2_common/hardware_uart/uart.c | 18 ++++++++++-------- 2 files changed, 11 insertions(+), 8 deletions(-) diff --git a/src/common/pico_base/include/pico/error.h b/src/common/pico_base/include/pico/error.h index 722a696..5c530ad 100644 --- a/src/common/pico_base/include/pico/error.h +++ b/src/common/pico_base/include/pico/error.h @@ -16,6 +16,7 @@ enum { PICO_ERROR_TIMEOUT = -1, PICO_ERROR_GENERIC = -2, PICO_ERROR_NO_DATA = -3, + PICO_ERROR_OUT_OF_RANGE = -4, }; #endif \ No newline at end of file diff --git a/src/rp2_common/hardware_uart/uart.c b/src/rp2_common/hardware_uart/uart.c index dbd4c2d..51d8d74 100644 --- a/src/rp2_common/hardware_uart/uart.c +++ b/src/rp2_common/hardware_uart/uart.c @@ -75,23 +75,25 @@ uint uart_set_baudrate(uart_inst_t *uart, uint baudrate) { uint32_t baud_rate_div = (8 * clock_get_hz(clk_peri) / baudrate); uint32_t baud_ibrd = baud_rate_div >> 7; uint32_t baud_fbrd = ((baud_rate_div & 0x7f) + 1) / 2; - invalid_params_if(UART, (baud_ibrd > 65535) || (baud_ibrd == 0)); + + if (baud_ibrd == 0) { + baud_ibrd = 1; + baud_fbrd = 0; + } else if (baud_ibrd >= 65535) { + baud_ibrd = 65535; + baud_fbrd = 0; + } // Load PL011's baud divisor registers uart_get_hw(uart)->ibrd = baud_ibrd; - if (baud_ibrd == 65535) { - uart_get_hw(uart)->fbrd = 0; - } else { - uart_get_hw(uart)->fbrd = baud_fbrd; - } + uart_get_hw(uart)->fbrd = baud_fbrd; // PL011 needs a (dummy) line control register write to latch in the // divisors. We don't want to actually change LCR contents here. hw_set_bits(&uart_get_hw(uart)->lcr_h, 0); // See datasheet - uint baud = (4 * clock_get_hz(clk_peri)) / (64 * baud_ibrd + baud_fbrd); - return baud; + return (4 * clock_get_hz(clk_peri)) / (64 * baud_ibrd + baud_fbrd); } /// \end::uart_set_baudrate[]