This is an automated email from the ASF dual-hosted git repository. xiaoxiang781216 pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/nuttx.git
commit b417d346578a10d40f2909fbd1d2eb10e563832a Author: Parthiban Nallathambi <[email protected]> AuthorDate: Sat Nov 21 20:45:34 2026 +0530 arch/rh850/u2c: Reject RLIN3 baud rates that do not fit the clock. The baud divider was written without a check. A rate above clock / 16, a divider over 16 bits or a large rounding error gave a wrong line rate without any notice. Reject a divider outside 1 to 65536 or a rate more than 3 percent off. The console rate fails the build with an error and u2c_rlin3_setup returns -EINVAL for any other unit. Signed-off-by: Parthiban Nallathambi <[email protected]> --- arch/rh850/src/common/rh850_internal.h | 2 +- arch/rh850/src/u2c/u2c_lowputc.c | 41 ++++++++++++++++++++++++++++------ arch/rh850/src/u2c/u2c_serial.c | 2 +- 3 files changed, 36 insertions(+), 9 deletions(-) diff --git a/arch/rh850/src/common/rh850_internal.h b/arch/rh850/src/common/rh850_internal.h index cec185153b4..2d39dd115d6 100644 --- a/arch/rh850/src/common/rh850_internal.h +++ b/arch/rh850/src/common/rh850_internal.h @@ -100,7 +100,7 @@ void rh850_clockconfig(void); void rh850_lowsetup(void); void rh850_lowputc(int ch); void rh850_serialinit(void); -void u2c_rlin3_setup(uintptr_t base, uint32_t baud); +int u2c_rlin3_setup(uintptr_t base, uint32_t baud); void u2c_rlin3_putc(uintptr_t base, int ch); bool u2c_irq_pending(int irq); void u2c_irq_raise(int irq); diff --git a/arch/rh850/src/u2c/u2c_lowputc.c b/arch/rh850/src/u2c/u2c_lowputc.c index d1ca7f4ae41..e9e2488095d 100644 --- a/arch/rh850/src/u2c/u2c_lowputc.c +++ b/arch/rh850/src/u2c/u2c_lowputc.c @@ -26,6 +26,7 @@ #include <nuttx/config.h> +#include <errno.h> #include <stdint.h> #include <nuttx/arch.h> @@ -43,6 +44,28 @@ # define CONSOLE_BAUD CONFIG_UART0_BAUD #endif +/* LBRP01 = round(clock / (16 x baud)) - 1 with LWBR = 0. A rate whose + * divider is out of range, or that is more than RLIN3_BAUD_TOL percent off, + * is rejected. + */ + +#define RLIN3_CLK CONFIG_RH850_CLK_HSB_FREQUENCY +#define RLIN3_BAUD_TOL 3 +#define RLIN3_DIV(b) ((RLIN3_CLK + RLIN3_SAMPLES * (b) / 2) / \ + (RLIN3_SAMPLES * (b))) +#define RLIN3_REAL(b) (RLIN3_SAMPLES * (b) * RLIN3_DIV(b)) +#define RLIN3_DIFF(b) (RLIN3_REAL(b) > RLIN3_CLK ? \ + RLIN3_REAL(b) - RLIN3_CLK : \ + RLIN3_CLK - RLIN3_REAL(b)) +#define RLIN3_BAUD_OK(b) \ + ((b) > 0 && RLIN3_SAMPLES * (b) <= RLIN3_CLK && \ + RLIN3_DIV(b) <= 0x10000 && \ + RLIN3_DIFF(b) * 100 <= RLIN3_REAL(b) * RLIN3_BAUD_TOL) + +#if defined(CONSOLE_BAUD) && !RLIN3_BAUD_OK(CONSOLE_BAUD) +# error CONFIG_UART0_BAUD does not fit CONFIG_RH850_CLK_HSB_FREQUENCY +#endif + /**************************************************************************** * Public Functions ****************************************************************************/ @@ -52,24 +75,27 @@ * * Description: * RLIN3 UART mode, 8N1, LSB first, no error detection. The unit clock - * and module stop are set up by rh850_clockconfig(). + * and module stop are set up by rh850_clockconfig(). Returns -EINVAL + * for a rate that RLIN3_BAUD_OK() rejects. * ****************************************************************************/ -void u2c_rlin3_setup(uintptr_t base, uint32_t baud) +int u2c_rlin3_setup(uintptr_t base, uint32_t baud) { - uint32_t samples = RLIN3_SAMPLES * baud; + uint64_t b = baud; + + if (!RLIN3_BAUD_OK(b)) + { + return -EINVAL; + } putreg8(0, base + RLIN3_LCUC_OFFSET); while ((getreg8(base + RLIN3_LMST_OFFSET) & RLIN3_LMST_OMM0) != 0) { } - /* LBRP01 = round(clock / (16 x baud)) - 1 */ - putreg8(0, base + RLIN3_LWBR_OFFSET); - putreg16((CONFIG_RH850_CLK_HSB_FREQUENCY + samples / 2) / samples - 1, - base + RLIN3_LBRP01_OFFSET); + putreg16((uint16_t)(RLIN3_DIV(b) - 1), base + RLIN3_LBRP01_OFFSET); putreg8(RLIN3_LMD_UART, base + RLIN3_LMD_OFFSET); putreg8(0, base + RLIN3_LBFC_OFFSET); putreg8(0, base + RLIN3_LEDE_OFFSET); @@ -81,6 +107,7 @@ void u2c_rlin3_setup(uintptr_t base, uint32_t baud) } putreg8(RLIN3_LUOER_UTOE | RLIN3_LUOER_UROE, base + RLIN3_LUOER_OFFSET); + return OK; } /**************************************************************************** diff --git a/arch/rh850/src/u2c/u2c_serial.c b/arch/rh850/src/u2c/u2c_serial.c index 5b62cb1d1c3..e5c2de5c49d 100644 --- a/arch/rh850/src/u2c/u2c_serial.c +++ b/arch/rh850/src/u2c/u2c_serial.c @@ -148,7 +148,7 @@ static int u2c_setup(FAR struct uart_dev_s *dev) if (!dev->isconsole) { - u2c_rlin3_setup(priv->base, priv->baud); + return u2c_rlin3_setup(priv->base, priv->baud); } return OK;
