diff --git a/tests/periph_uart/Makefile b/tests/periph_uart/Makefile index 73e6acdbafab499dfc67ae2de720e62d524a2190..5ee830f3b0a6dfb90a700abfe863d0a04ea2a242 100644 --- a/tests/periph_uart/Makefile +++ b/tests/periph_uart/Makefile @@ -2,8 +2,9 @@ include ../Makefile.tests_common BOARD_INSUFFICIENT_MEMORY := arduino-duemilanove arduino-uno nucleo-f031k6 -FEATURES_REQUIRED = periph_uart -FEATURES_OPTIONAL = periph_lpuart # STM32 L0 and L4 provides lpuart support +FEATURES_REQUIRED += periph_uart +FEATURES_OPTIONAL += periph_lpuart # STM32 L0 and L4 provides lpuart support +FEATURES_OPTIONAL += periph_uart_modecfg USEMODULE += shell USEMODULE += xtimer diff --git a/tests/periph_uart/main.c b/tests/periph_uart/main.c index 850f6962d5e68331dfad4017f35085287d1eb3f0..0a2333365fe1fd424b6391d57d0aba242ceedbf1 100644 --- a/tests/periph_uart/main.c +++ b/tests/periph_uart/main.c @@ -53,6 +53,15 @@ static uart_ctx_t ctx[UART_NUMOF]; static kernel_pid_t printer_pid; static char printer_stack[THREAD_STACKSIZE_MAIN]; +#ifdef MODULE_PERIPH_UART_MODECFG +static uart_data_bits_t data_bits_lut[] = { UART_DATA_BITS_5, UART_DATA_BITS_6, + UART_DATA_BITS_7, UART_DATA_BITS_8 }; +static int data_bits_lut_len = sizeof(data_bits_lut)/sizeof(data_bits_lut[0]); + +static uart_stop_bits_t stop_bits_lut[] = { UART_STOP_BITS_1, UART_STOP_BITS_2 }; +static int stop_bits_lut_len = sizeof(stop_bits_lut)/sizeof(stop_bits_lut[0]); +#endif + static int parse_dev(char *arg) { unsigned dev = atoi(arg); @@ -142,7 +151,7 @@ static int cmd_init(int argc, char **argv) return 1; } else if (res != UART_OK) { - puts("Error: Unable to initialize UART device\n"); + puts("Error: Unable to initialize UART device"); return 1; } printf("Success: Initialized UART_DEV(%i) at BAUD %"PRIu32"\n", dev, baud); @@ -154,6 +163,74 @@ static int cmd_init(int argc, char **argv) return 0; } +#ifdef MODULE_PERIPH_UART_MODECFG +static int cmd_mode(int argc, char **argv) +{ + int dev, data_bits_arg, stop_bits_arg; + uart_data_bits_t data_bits; + uart_parity_t parity; + uart_stop_bits_t stop_bits; + + if (argc < 5) { + printf("usage: %s <dev> <data bits> <parity> <stop bits>\n", argv[0]); + return 1; + } + + dev = parse_dev(argv[1]); + if (dev < 0) { + return 1; + } + + data_bits_arg = atoi(argv[2]) - 5; + if (data_bits_arg >= 0 && data_bits_arg < data_bits_lut_len) { + data_bits = data_bits_lut[data_bits_arg]; + } + else { + printf("Error: Invalid number of data_bits (%i).\n", data_bits_arg + 5); + return 1; + } + + argv[3][0] &= ~0x20; + switch (argv[3][0]) { + case 'N': + parity = UART_PARITY_NONE; + break; + case 'E': + parity = UART_PARITY_EVEN; + break; + case 'O': + parity = UART_PARITY_ODD; + break; + case 'M': + parity = UART_PARITY_MARK; + break; + case 'S': + parity = UART_PARITY_SPACE; + break; + default: + printf("Error: Invalid parity (%c).\n", argv[3][0]); + return 1; + } + + stop_bits_arg = atoi(argv[4]) - 1; + if (stop_bits_arg >= 0 && stop_bits_arg < stop_bits_lut_len) { + stop_bits = stop_bits_lut[stop_bits_arg]; + } + else { + printf("Error: Invalid number of stop bits (%i).\n", stop_bits_arg + 1); + return 1; + } + + if (uart_mode(UART_DEV(dev), data_bits, parity, stop_bits) != UART_OK) { + puts("Error: Unable to apply UART settings"); + return 1; + } + printf("Success: Successfully applied UART_DEV(%i) settings\n", dev); + + return 0; +} +#endif /* MODULE_PERIPH_UART_MODECFG */ + static int cmd_send(int argc, char **argv) { int dev; @@ -177,6 +254,9 @@ static int cmd_send(int argc, char **argv) static const shell_command_t shell_commands[] = { { "init", "Initialize a UART device with a given baudrate", cmd_init }, +#ifdef MODULE_PERIPH_UART_MODECFG + { "mode", "Setup data bits, stop bits and parity for a given UART device", cmd_mode }, +#endif { "send", "Send a string through given UART device", cmd_send }, { NULL, NULL, NULL } };