From 0c47233f973444732e6b171d01147415a266f79e Mon Sep 17 00:00:00 2001 From: Bas Stottelaar Date: Mon, 14 May 2018 00:26:01 +0200 Subject: [PATCH] cpu: efm32: add support for non-standard UART modes. --- cpu/efm32/Makefile.features | 6 ++++++ cpu/efm32/efm32-features.mk | 4 ++++ cpu/efm32/include/periph_cpu.h | 19 ++++++++++++++++++- cpu/efm32/periph/uart.c | 12 ++++++++++++ 4 files changed, 40 insertions(+), 1 deletion(-) create mode 100644 cpu/efm32/efm32-features.mk diff --git a/cpu/efm32/Makefile.features b/cpu/efm32/Makefile.features index 22697274e6..8ea9c65488 100644 --- a/cpu/efm32/Makefile.features +++ b/cpu/efm32/Makefile.features @@ -1,3 +1,5 @@ +include $(RIOTCPU)/efm32/efm32-features.mk + FEATURES_PROVIDED += periph_cpuid FEATURES_PROVIDED += periph_flashpage @@ -8,4 +10,8 @@ ifeq (1,$(EFM32_TNRG)) FEATURES_PROVIDED += periph_hwrng endif +ifeq (1,$(EFM32_UART_MODES)) + export CFLAGS += -DEFM32_UART_MODES=1 +endif + include $(RIOTCPU)/cortexm_common/Makefile.features diff --git a/cpu/efm32/efm32-features.mk b/cpu/efm32/efm32-features.mk new file mode 100644 index 0000000000..d9cc3da6f0 --- /dev/null +++ b/cpu/efm32/efm32-features.mk @@ -0,0 +1,4 @@ +# This file provides defaults for additional EFM32-specific features. You +# should override them from the command line, or in your Makefile. Note that +# some features may not be applicable to all EFM32 boards or CPUs. +export EFM32_UART_MODES ?= 0 diff --git a/cpu/efm32/include/periph_cpu.h b/cpu/efm32/include/periph_cpu.h index 1fd63fd20c..5a868ad57b 100644 --- a/cpu/efm32/include/periph_cpu.h +++ b/cpu/efm32/include/periph_cpu.h @@ -359,6 +359,20 @@ typedef struct { } timer_conf_t; /** @} */ +/** + * @brief Internal macro for combining UART modes data bits (x), stop bits + * (y, in half bits) and parity (z). + */ +#define UART_MODE(x, y, z) ((z << 8) | ((y * 2) << 4) | x) + +/** + * @brief Internal, pre-defined UART modes. + * @{ + */ +#define UART_MODE_8N1 UART_MODE(8, 1, 0) +#define UART_MODE_8E1 UART_MODE(8, 1, 2) +/** @} */ + /** * @brief UART device configuration. */ @@ -366,7 +380,10 @@ typedef struct { void *dev; /**< UART, USART or LEUART device used */ gpio_t rx_pin; /**< pin used for RX */ gpio_t tx_pin; /**< pin used for TX */ - uint32_t loc; /**< location of USART pins */ + uint32_t loc; /**< location of UART pins */ +#if EFM32_UART_MODES + uint32_t mode; /**< UART mode of operation */ +#endif CMU_Clock_TypeDef cmu; /**< the device CMU channel */ IRQn_Type irq; /**< the devices base IRQ channel */ } uart_conf_t; diff --git a/cpu/efm32/periph/uart.c b/cpu/efm32/periph/uart.c index adaadf2142..f021833b0f 100644 --- a/cpu/efm32/periph/uart.c +++ b/cpu/efm32/periph/uart.c @@ -26,8 +26,10 @@ #include "periph/gpio.h" #include "em_usart.h" +#include "em_usart_utils.h" #if LOW_POWER_ENABLED && defined(LEUART_COUNT) && LEUART_COUNT > 0 #include "em_leuart.h" +#include "em_leuart_utils.h" #endif /** @@ -73,6 +75,11 @@ int uart_init(uart_t dev, uint32_t baudrate, uart_rx_cb_t rx_cb, void *arg) init.enable = usartDisable; init.baudrate = baudrate; +#if EFM32_UART_MODES + init.databits = USART_DataBits2Def((uart_config[dev].mode >> 0) & 0xf); + init.stopbits = USART_StopBits2Def((uart_config[dev].mode >> 4) & 0xf); + init.parity = USART_Parity2Def((uart_config[dev].mode >> 8) & 0xf); +#endif USART_InitAsync(uart, &init); @@ -104,6 +111,11 @@ int uart_init(uart_t dev, uint32_t baudrate, uart_rx_cb_t rx_cb, void *arg) init.enable = leuartDisable; init.baudrate = baudrate; +#if EFM32_UART_MODES + init.databits = LEUART_DataBits2Def((uart_config[dev].mode >> 0) & 0xf); + init.stopbits = LEUART_StopBits2Def((uart_config[dev].mode >> 4) & 0xf); + init.parity = LEUART_Parity2Def((uart_config[dev].mode >> 8) & 0xf); +#endif LEUART_Init(leuart, &init);