From 236aeadd4ffe28a84faad92b7bfb33d67fb51cf2 Mon Sep 17 00:00:00 2001 From: Jonathan Hahn Date: Sat, 1 Jan 2022 23:43:07 +0100 Subject: [PATCH] samples: uart: Add sample for single line uart The sample uses st single-line uart mode on stm32 boards. Signed-off-by: Jonathan Hahn --- .../uart/stm32/single_wire/CMakeLists.txt | 9 ++++ .../drivers/uart/stm32/single_wire/README.rst | 33 ++++++++++++++ .../single_wire/boards/stm32f3_disco.overlay | 22 +++++++++ .../drivers/uart/stm32/single_wire/prj.conf | 1 + .../uart/stm32/single_wire/sample.yaml | 12 +++++ .../drivers/uart/stm32/single_wire/src/main.c | 45 +++++++++++++++++++ 6 files changed, 122 insertions(+) create mode 100644 samples/drivers/uart/stm32/single_wire/CMakeLists.txt create mode 100644 samples/drivers/uart/stm32/single_wire/README.rst create mode 100644 samples/drivers/uart/stm32/single_wire/boards/stm32f3_disco.overlay create mode 100644 samples/drivers/uart/stm32/single_wire/prj.conf create mode 100644 samples/drivers/uart/stm32/single_wire/sample.yaml create mode 100644 samples/drivers/uart/stm32/single_wire/src/main.c diff --git a/samples/drivers/uart/stm32/single_wire/CMakeLists.txt b/samples/drivers/uart/stm32/single_wire/CMakeLists.txt new file mode 100644 index 00000000000..8e04bab1c63 --- /dev/null +++ b/samples/drivers/uart/stm32/single_wire/CMakeLists.txt @@ -0,0 +1,9 @@ +# SPDX-License-Identifier: Apache-2.0 + +cmake_minimum_required(VERSION 3.20.0) +find_package(Zephyr REQUIRED HINTS $ENV{ZEPHYR_BASE}) +project(uart_stm32_single_wire) + +target_sources(app PRIVATE + src/main.c + ) diff --git a/samples/drivers/uart/stm32/single_wire/README.rst b/samples/drivers/uart/stm32/single_wire/README.rst new file mode 100644 index 00000000000..6016dcfccb0 --- /dev/null +++ b/samples/drivers/uart/stm32/single_wire/README.rst @@ -0,0 +1,33 @@ +.. _sample_uart_stm32_single_wire: + +STM32 Single Wire UART +###################### + +Overview +******** + +A simple application demonstrating how to use the single wire / half-duplex UART +functionality of STM32. Without adaptions this example runs on STM32F3 discovery +board. You need to establish a physical connection between pins PA2 (USART2_TX) and +PC10 (UART4_TX). + +Add a `single_wire_uart_loopback` fixture to your board in the hardware map to allow +twister to verify this sample's output automatically. + +Building and Running +******************** + +Build and flash as follows, replacig ``stm32f3_disco`` with your board: + + .. zephyr-app-commands:: + :zephyr-app: samples/drivers/uart/stm32/single_wire + :board: stm32f3_disco + :goals: build flash + :compact: + +After flashing the console output should not show any failure reports, +but the following message repeated every 2s: + +.. code-block:: none + + Received c diff --git a/samples/drivers/uart/stm32/single_wire/boards/stm32f3_disco.overlay b/samples/drivers/uart/stm32/single_wire/boards/stm32f3_disco.overlay new file mode 100644 index 00000000000..5d07b989a93 --- /dev/null +++ b/samples/drivers/uart/stm32/single_wire/boards/stm32f3_disco.overlay @@ -0,0 +1,22 @@ +/* + * Copyright (c) 2021 Jonathan Hahn + * + * SPDX-License-Identifier: Apache-2.0 + */ + +/ { + aliases { + single-line-uart1 = &usart2; + single-line-uart2 = &uart4; + }; +}; + +&usart2 { + pinctrl-0 = <&usart2_tx_pa2>; + single-wire; +}; + +&uart4 { + pinctrl-0 = <&uart4_tx_pc10>; + single-wire; +}; diff --git a/samples/drivers/uart/stm32/single_wire/prj.conf b/samples/drivers/uart/stm32/single_wire/prj.conf new file mode 100644 index 00000000000..d522ea5349b --- /dev/null +++ b/samples/drivers/uart/stm32/single_wire/prj.conf @@ -0,0 +1 @@ +# Nothing needed here diff --git a/samples/drivers/uart/stm32/single_wire/sample.yaml b/samples/drivers/uart/stm32/single_wire/sample.yaml new file mode 100644 index 00000000000..69dfcd3db2c --- /dev/null +++ b/samples/drivers/uart/stm32/single_wire/sample.yaml @@ -0,0 +1,12 @@ +sample: + name: STM32 Single Wire UART sample +tests: + sample.drivers.uart.stm32.single_wire: + platform_allow: stm32f3_disco + tags: drivers uart + harness: console + harness_config: + fixture: single_line_uart_loopback + type: one_line + regex: + - "Received c" diff --git a/samples/drivers/uart/stm32/single_wire/src/main.c b/samples/drivers/uart/stm32/single_wire/src/main.c new file mode 100644 index 00000000000..d6a34d17b90 --- /dev/null +++ b/samples/drivers/uart/stm32/single_wire/src/main.c @@ -0,0 +1,45 @@ +/* + * Copyright (c) 2021 Jonathan Hahn + * + * SPDX-License-Identifier: Apache-2.0 + */ + +#include +#include "kernel.h" +#include +#include +#include + +#define UART_NODE1 DT_ALIAS(single_line_uart1) +#define UART_NODE2 DT_ALIAS(single_line_uart2) + +const struct device *sl_uart1 = DEVICE_DT_GET(UART_NODE1); +const struct device *sl_uart2 = DEVICE_DT_GET(UART_NODE2); + +void main(void) +{ + unsigned char recv; + + if (!device_is_ready(sl_uart1) || !device_is_ready(sl_uart2)) { + printk("uart devices not ready\n"); + return; + } + + while (true) { + + uart_poll_out(sl_uart1, 'c'); + + /* give the uarts some time to get the data through */ + k_sleep(K_MSEC(50)); + + int ret = uart_poll_in(sl_uart2, &recv); + + if (ret < 0) { + printk("Receiving failed. Error: %d", ret); + } else { + printk("Received %c\n", recv); + } + + k_sleep(K_MSEC(2000)); + } +}