Skip to content

Commit 7e1609f

Browse files
committed
samples: uart_async: implement uart async test
For testing purpose, it could be removed after the PR is finished. Signed-off-by: Jun Li <[email protected]>
1 parent 22cd034 commit 7e1609f

File tree

4 files changed

+200
-0
lines changed

4 files changed

+200
-0
lines changed
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
# SPDX-License-Identifier: Apache-2.0
2+
3+
cmake_minimum_required(VERSION 3.13.1)
4+
include($ENV{ZEPHYR_BASE}/cmake/app/boilerplate.cmake NO_POLICY_SCOPE)
5+
project(uart_async)
6+
7+
FILE(GLOB app_sources src/*.c)
8+
target_sources(app PRIVATE ${app_sources})

samples/drivers/uart_async/prj.conf

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
#Enable async UART support
2+
CONFIG_UART_ASYNC_API=y
3+
4+
#Enable log on UART
5+
CONFIG_THREAD_NAME=y
6+
CONFIG_LOG=y
7+
CONFIG_LOG_BACKEND_UART=y
8+
CONFIG_LOG_BACKEND_SHOW_COLOR=y
9+
## Enable debug log level
10+
CONFIG_LOG_DEFAULT_LEVEL=4
11+
12+
## Enable shell
13+
CONFIG_SHELL=y
14+
CONFIG_PRINTK=y

samples/drivers/uart_async/src/main.c

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
/*
2+
* Copyright 2019 Intel Corporation
3+
* SPDX-License-Identifier: Apache-2.0
4+
*/
5+
6+
#include <zephyr.h>
7+
#include <sys/printk.h>
8+
#include <drivers/uart.h>
9+
#include <string.h>
10+
#include <kernel.h>
11+
12+
#define ASYNC_BUF_SZ 128
13+
14+
static struct main_data {
15+
struct device *uart_dev;
16+
uint8_t rx_buf[ASYNC_BUF_SZ];
17+
uint8_t tx_buf[ASYNC_BUF_SZ];
18+
struct k_sem tx_sem;
19+
} _main_data;
20+
21+
static void uart_async_callback(struct uart_event *evt, void *priv_data)
22+
{
23+
struct main_data *data = priv_data;
24+
25+
if (evt->type == UART_TX_DONE) {
26+
printk("TX done: %d\n", evt->data.tx.len);
27+
memset(data->tx_buf, 0, sizeof(data->tx_buf));
28+
29+
} else if (evt->type == UART_RX_RDY) {
30+
uint8_t *src_buf = evt->data.rx.buf
31+
+ evt->data.rx.offset;
32+
memcpy(data->tx_buf, src_buf, evt->data.rx.len);
33+
printk("RX done: %d - %s\n", evt->data.rx.len, data->tx_buf);
34+
if (evt->data.rx.len > 0) {
35+
k_sem_give(&data->tx_sem);
36+
}
37+
}
38+
}
39+
40+
int main(void)
41+
{
42+
uint32_t version = sys_kernel_version_get();
43+
44+
printk(
45+
"UART-Async API Echo ARCH: %s, BOARD: %s, version: %d.%d.%d\n",
46+
CONFIG_ARCH, CONFIG_BOARD,
47+
SYS_KERNEL_VER_MAJOR(version),
48+
SYS_KERNEL_VER_MINOR(version),
49+
SYS_KERNEL_VER_PATCHLEVEL(version));
50+
51+
struct device *uart = device_get_binding("UART_6");
52+
53+
if (uart == NULL) {
54+
printk("UART open failed!\n");
55+
return -1;
56+
}
57+
58+
struct main_data *data = &_main_data;
59+
60+
data->uart_dev = uart;
61+
62+
uint8_t *buf = data->rx_buf;
63+
64+
memset(buf, 0, sizeof(ASYNC_BUF_SZ));
65+
66+
67+
uart_callback_set(uart, uart_async_callback, data);
68+
k_sem_init(&data->tx_sem, 0, 1);
69+
70+
/* start rx */
71+
uart_rx_enable(uart, buf, ASYNC_BUF_SZ, K_FOREVER.ticks);
72+
while (1) {
73+
k_sem_take(&data->tx_sem, K_FOREVER);
74+
int len = strlen(data->tx_buf);
75+
76+
if (len > 0) {
77+
uart_tx(data->uart_dev, data->tx_buf,
78+
len, K_FOREVER.ticks);
79+
}
80+
}
81+
82+
return 0;
83+
}
Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
#!/usr/bin/env python3
2+
#
3+
# Copyright (c) 2019 Intel Corporation
4+
#
5+
# SPDX-License-Identifier: Apache-2.0
6+
7+
import sys, traceback
8+
import getopt
9+
import serial
10+
import os
11+
import time
12+
from threading import Thread
13+
from threading import Timer
14+
import click
15+
import signal
16+
17+
class uart_test(Thread):
18+
def __init__(self, tty_port, baud):
19+
signal.signal(signal.SIGTERM, self.handleSignal)
20+
signal.signal(signal.SIGINT, self.handleSignal)
21+
signal.signal(signal.SIGTSTP, self.handleSignal)
22+
23+
Thread.__init__(self)
24+
25+
self.console = serial.Serial(
26+
port=tty_port,
27+
baudrate=baud,
28+
parity=serial.PARITY_NONE,
29+
stopbits=serial.STOPBITS_ONE,
30+
bytesize=serial.EIGHTBITS,
31+
timeout = 0.01, # 10 ms
32+
)
33+
self.running = True
34+
if self.console.isOpen():
35+
self.console.flushInput()
36+
self.console.flushOutput()
37+
self.start()
38+
39+
def __del__(self):
40+
self.console.close()
41+
42+
def close(self):
43+
self.running = False
44+
time.sleep(1)
45+
self.join()
46+
if self.console.isOpen():
47+
self.console.close()
48+
49+
def packet_send(self, cmd):
50+
if self.console.isOpen():
51+
self.console.write(cmd.encode())
52+
53+
def handleSignal(self, signum, frame):
54+
self.close()
55+
56+
def tx_test(self):
57+
if self.running:
58+
my_packet = "This is a stm32 F429zi packet!\r\n"
59+
print("Write: " + my_packet)
60+
self.packet_send(my_packet)
61+
self.tx_timer = Timer(1.0, self.tx_test)
62+
self.tx_timer.start();
63+
64+
def run(self):
65+
66+
self.tx_test()
67+
while self.running:
68+
if self.console.inWaiting() > 0:
69+
input_str = self.console.readline()
70+
print("rcv: %s" % input_str)
71+
72+
self.tx_timer.cancel();
73+
self.console.close();
74+
75+
76+
@click.command("Start dma test")
77+
@click.option('-b', '--baud', default=115200, type=int, help='Baud rate')
78+
@click.argument("tty_port")
79+
def main(tty_port, baud):
80+
try:
81+
test_shell = uart_test(tty_port, baud)
82+
83+
except KeyboardInterrupt:
84+
test_shell.close()
85+
exit()
86+
except serial.SerialException:
87+
print("Open serial port %s failed!" % tty_port)
88+
except SystemExit:
89+
print('Exiting...')
90+
except:
91+
traceback.print_exc()
92+
sys.exit(3)
93+
94+
if __name__ == "__main__":
95+
main()

0 commit comments

Comments
 (0)