From 0241982fb4ad90d8f2a0146fc71eb01927663ea9 Mon Sep 17 00:00:00 2001 From: dragonmux Date: Mon, 15 Aug 2022 17:57:51 +0100 Subject: [PATCH] stm32/usart: Implemented a function to get the current databits setting for a USART --- .../stm32/common/usart_common_all.h | 1 + lib/stm32/common/usart_common_all.c | 21 +++++++++++++++++++ 2 files changed, 22 insertions(+) diff --git a/include/libopencm3/stm32/common/usart_common_all.h b/include/libopencm3/stm32/common/usart_common_all.h index 8c310a9c..a86a91fb 100644 --- a/include/libopencm3/stm32/common/usart_common_all.h +++ b/include/libopencm3/stm32/common/usart_common_all.h @@ -100,6 +100,7 @@ BEGIN_DECLS void usart_set_baudrate(uint32_t usart, uint32_t baud); void usart_set_databits(uint32_t usart, uint32_t bits); +uint32_t usart_get_databits(uint32_t usart); void usart_set_stopbits(uint32_t usart, uint32_t stopbits); void usart_set_parity(uint32_t usart, uint32_t parity); void usart_set_mode(uint32_t usart, uint32_t mode); diff --git a/lib/stm32/common/usart_common_all.c b/lib/stm32/common/usart_common_all.c index 8e676c07..6fcbf191 100644 --- a/lib/stm32/common/usart_common_all.c +++ b/lib/stm32/common/usart_common_all.c @@ -95,6 +95,27 @@ void usart_set_databits(uint32_t usart, uint32_t bits) } } +/*---------------------------------------------------------------------------*/ +/** @brief USART Get Word Length. + +The word length is set to 8 or 9 bits. Note that the last bit will be a parity +bit if parity is enabled, in which case the data length will be 7 or 8 bits +respectively. + +@param[in] usart unsigned 32 bit. USART block register address base @ref +usart_reg_base +@returns unsigned 32 bit Word length in bits 8 or 9. +*/ + +uint32_t usart_get_databits(uint32_t usart) +{ + const uint32_t reg32 = USART_CR1(usart) & USART_CR1_M; + if (reg32) + return 9; + else + return 8; +} + /*---------------------------------------------------------------------------*/ /** @brief USART Set Stop Bit(s).