Skip to content
Permalink
Browse files

drivers/serial: stm32: Optimize LPUART code

On STM32 series currently available in Zephyr,
LPUART and U(S)ART IPs are similar and share most of their
registers. As a consequence LL API defined for U(S)ART also
applies to LPUART.
This allows to remove specific LPUART code.

Restrict use of LL LPUART API when it diverges from UART one.


Signed-off-by: Erwan Gouriou <erwan.gouriou@linaro.org>
  • Loading branch information...
erwango authored and galak committed May 20, 2019
1 parent 63a93e8 commit 69a2896f3e9fcf60793fc7d49cc6e460ecc0e861
Showing with 23 additions and 86 deletions.
  1. +23 −86 drivers/serial/uart_stm32.c
@@ -34,150 +34,87 @@

#define TIMEOUT 1000

static void uart_stm32_usart_set_baud_rate(struct device *dev,
u32_t clock_rate, u32_t baud_rate)
{
USART_TypeDef *UartInstance = UART_STRUCT(dev);

LL_USART_SetBaudRate(UartInstance,
clock_rate,
#ifdef USART_PRESC_PRESCALER
LL_USART_PRESCALER_DIV1,
#endif
#ifdef USART_CR1_OVER8
LL_USART_OVERSAMPLING_16,
#endif
baud_rate);
}

#ifdef CONFIG_LPUART_1
static void uart_stm32_lpuart_set_baud_rate(struct device *dev,
u32_t clock_rate, u32_t baud_rate)
{
USART_TypeDef *UartInstance = UART_STRUCT(dev);

LL_LPUART_SetBaudRate(UartInstance,
clock_rate,
#ifdef USART_PRESC_PRESCALER
LL_USART_PRESCALER_DIV1,
#endif
baud_rate);
}
#endif /* CONFIG_LPUART_1 */

static inline void uart_stm32_set_baudrate(struct device *dev, u32_t baud_rate)
{
const struct uart_stm32_config *config = DEV_CFG(dev);
struct uart_stm32_data *data = DEV_DATA(dev);
#ifdef CONFIG_LPUART_1
USART_TypeDef *UartInstance = UART_STRUCT(dev);
#endif

u32_t clock_rate;

/* Get clock rate */
clock_control_get_rate(data->clock,
(clock_control_subsys_t *)&config->pclken,
&clock_rate);



#ifdef CONFIG_LPUART_1
if (IS_LPUART_INSTANCE(UartInstance)) {
uart_stm32_lpuart_set_baud_rate(dev, clock_rate, baud_rate);
LL_LPUART_SetBaudRate(UartInstance,
clock_rate,
#ifdef USART_PRESC_PRESCALER
LL_USART_PRESCALER_DIV1,
#endif
baud_rate);
} else {
uart_stm32_usart_set_baud_rate(dev, clock_rate, baud_rate);
}
#else
uart_stm32_usart_set_baud_rate(dev, clock_rate, baud_rate);
#endif /* CONFIG_LPUART_1 */

LL_USART_SetBaudRate(UartInstance,
clock_rate,
#ifdef USART_PRESC_PRESCALER
LL_USART_PRESCALER_DIV1,
#endif
#ifdef USART_CR1_OVER8
LL_USART_OVERSAMPLING_16,
#endif
baud_rate);

#ifdef CONFIG_LPUART_1
}
#endif /* CONFIG_LPUART_1 */
}

static inline void uart_stm32_set_parity(struct device *dev, u32_t parity)
{
USART_TypeDef *UartInstance = UART_STRUCT(dev);

#ifdef CONFIG_LPUART_1
if (IS_LPUART_INSTANCE(UartInstance)) {
LL_LPUART_SetParity(UartInstance, parity);
} else {
LL_USART_SetParity(UartInstance, parity);
}
#else
LL_USART_SetParity(UartInstance, parity);
#endif /* CONFIG_LPUART_1 */
}

static inline u32_t uart_stm32_get_parity(struct device *dev)
{
USART_TypeDef *UartInstance = UART_STRUCT(dev);

#ifdef CONFIG_LPUART_1
if (IS_LPUART_INSTANCE(UartInstance)) {
return LL_LPUART_GetParity(UartInstance);
} else {
return LL_USART_GetParity(UartInstance);
}
#else
return LL_USART_GetParity(UartInstance);
#endif /* CONFIG_LPUART_1 */
}

static inline void uart_stm32_set_stopbits(struct device *dev, u32_t stopbits)
{
USART_TypeDef *UartInstance = UART_STRUCT(dev);

#ifdef CONFIG_LPUART_1
if (IS_LPUART_INSTANCE(UartInstance)) {
LL_LPUART_SetStopBitsLength(UartInstance, stopbits);
} else {
LL_USART_SetStopBitsLength(UartInstance, stopbits);
}
#else
LL_USART_SetStopBitsLength(UartInstance, stopbits);
#endif /* CONFIG_LPUART_1 */
}

static inline u32_t uart_stm32_get_stopbits(struct device *dev)
{
USART_TypeDef *UartInstance = UART_STRUCT(dev);

#ifdef CONFIG_LPUART_1
if (IS_LPUART_INSTANCE(UartInstance)) {
return LL_LPUART_GetStopBitsLength(UartInstance);
} else {
return LL_USART_GetStopBitsLength(UartInstance);
}
#else
return LL_USART_GetStopBitsLength(UartInstance);
#endif /* CONFIG_LPUART_1 */
}

static inline void uart_stm32_set_databits(struct device *dev, u32_t databits)
{
USART_TypeDef *UartInstance = UART_STRUCT(dev);

#ifdef CONFIG_LPUART_1
if (IS_LPUART_INSTANCE(UartInstance)) {
LL_LPUART_SetDataWidth(UartInstance, databits);
} else {
LL_USART_SetDataWidth(UartInstance, databits);
}
#else
LL_USART_SetDataWidth(UartInstance, databits);
#endif /* CONFIG_LPUART_1 */
}

static inline u32_t uart_stm32_get_databits(struct device *dev)
{
USART_TypeDef *UartInstance = UART_STRUCT(dev);

#ifdef CONFIG_LPUART_1
if (IS_LPUART_INSTANCE(UartInstance)) {
return LL_LPUART_GetDataWidth(UartInstance);
} else {
return LL_USART_GetDataWidth(UartInstance);
}
#else
return LL_USART_GetDataWidth(UartInstance);
#endif /* CONFIG_LPUART_1 */
}

static inline void uart_stm32_set_hwctrl(struct device *dev, u32_t hwctrl)

0 comments on commit 69a2896

Please sign in to comment.
You can’t perform that action at this time.