From ca7c12643c646bbf6fd2ec8e936d74059d33f5c4 Mon Sep 17 00:00:00 2001 From: Marian Buschsieweke Date: Tue, 13 Jul 2021 16:51:00 +0200 Subject: [PATCH] tests/periph_uart_mode: Drop dep to periph_timer Use xtimer only optionally (when periph_timer is available) for the test. This makes this test applicable to freshly ported boards that do not yet have a peripheral timer driver. --- tests/periph_uart_mode/Makefile | 3 +-- tests/periph_uart_mode/Makefile.board.dep | 3 +++ tests/periph_uart_mode/main.c | 23 ++++++++++++++++++++++- 3 files changed, 26 insertions(+), 3 deletions(-) create mode 100644 tests/periph_uart_mode/Makefile.board.dep diff --git a/tests/periph_uart_mode/Makefile b/tests/periph_uart_mode/Makefile index 04882515c0..329a1ac637 100644 --- a/tests/periph_uart_mode/Makefile +++ b/tests/periph_uart_mode/Makefile @@ -2,8 +2,7 @@ include ../Makefile.tests_common FEATURES_REQUIRED += periph_uart FEATURES_REQUIRED += periph_uart_modecfg - -USEMODULE += xtimer +FEATURES_OPTIONAL += periph_timer # Set this to prevent welcome message from printing and confusing output CFLAGS+=-DLOG_LEVEL=LOG_NONE diff --git a/tests/periph_uart_mode/Makefile.board.dep b/tests/periph_uart_mode/Makefile.board.dep new file mode 100644 index 0000000000..1da3893d4d --- /dev/null +++ b/tests/periph_uart_mode/Makefile.board.dep @@ -0,0 +1,3 @@ +ifneq (,$(filter periph_timer,$(FEATURES_USED))) + USEMODULE += xtimer +endif diff --git a/tests/periph_uart_mode/main.c b/tests/periph_uart_mode/main.c index d15df67ba0..b37d44796b 100644 --- a/tests/periph_uart_mode/main.c +++ b/tests/periph_uart_mode/main.c @@ -22,7 +22,9 @@ #include #include +#include "board.h" #include "periph/uart.h" +#include "periph_conf.h" #include "stdio_uart.h" #include "xtimer.h" @@ -52,6 +54,25 @@ /* Stores each mode string for printing at the end of the test */ static char mode_strings[TOTAL_OPTIONS][MODE_STR_LEN]; +static void _delay(void) +{ + if (IS_USED(MODULE_XTIMER)) { + xtimer_usleep(DELAY_US); + } + else { + /* + * As fallback for freshly ported boards with no timer drivers written + * yet, we just use the CPU to delay execution and assume that roughly + * 20 CPU cycles are spend per loop iteration. + * + * Note that the volatile qualifier disables compiler optimizations for + * all accesses to the counter variable. Without volatile, modern + * compilers would detect that the loop is only wasting CPU cycles and + * optimize it out - but here the wasting of CPU cycles is desired. + */ + for (volatile uint32_t i = 0; i < CLOCK_CORECLOCK / 20; i++) { } + } +} static void _get_mode(const uart_data_bits_t data_bits, const uart_parity_t parity, const uart_stop_bits_t stop_bits, char* mode_str) { @@ -153,7 +174,7 @@ int main(void) if (status == UART_OK) { results[ridx] = true; printf("%s\n", mode_strings[ridx]); - xtimer_usleep(DELAY_US); + _delay(); } else { results[ridx] = false;