samples: uart: Add sample for single line uart
The sample uses st single-line uart mode on stm32 boards. Signed-off-by: Jonathan Hahn <Jonathan.Hahn@t-online.de>
This commit is contained in:
parent
32f9dcf328
commit
236aeadd4f
6 changed files with 122 additions and 0 deletions
9
samples/drivers/uart/stm32/single_wire/CMakeLists.txt
Normal file
9
samples/drivers/uart/stm32/single_wire/CMakeLists.txt
Normal file
|
@ -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
|
||||
)
|
33
samples/drivers/uart/stm32/single_wire/README.rst
Normal file
33
samples/drivers/uart/stm32/single_wire/README.rst
Normal file
|
@ -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
|
|
@ -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;
|
||||
};
|
1
samples/drivers/uart/stm32/single_wire/prj.conf
Normal file
1
samples/drivers/uart/stm32/single_wire/prj.conf
Normal file
|
@ -0,0 +1 @@
|
|||
# Nothing needed here
|
12
samples/drivers/uart/stm32/single_wire/sample.yaml
Normal file
12
samples/drivers/uart/stm32/single_wire/sample.yaml
Normal file
|
@ -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"
|
45
samples/drivers/uart/stm32/single_wire/src/main.c
Normal file
45
samples/drivers/uart/stm32/single_wire/src/main.c
Normal file
|
@ -0,0 +1,45 @@
|
|||
/*
|
||||
* Copyright (c) 2021 Jonathan Hahn
|
||||
*
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*/
|
||||
|
||||
#include <zephyr.h>
|
||||
#include "kernel.h"
|
||||
#include <device.h>
|
||||
#include <devicetree.h>
|
||||
#include <drivers/uart.h>
|
||||
|
||||
#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));
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue