Skip to content

Commit 008bf27

Browse files
nordic-piksnordicjm
authored andcommitted
samples: peripheral: lpuart: verify sample output
Check both ASYNC and IRQ mode. Signed-off-by: Piotr Kosycarz <[email protected]>
1 parent 6a85282 commit 008bf27

File tree

2 files changed

+97
-21
lines changed

2 files changed

+97
-21
lines changed

samples/peripheral/lpuart/sample.yaml

Lines changed: 51 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
sample:
22
description: Low Power UART sample
33
name: Low Power UART
4+
5+
common:
6+
tags: ci_build sysbuild ci_samples_peripheral_lpuart
7+
48
tests:
59
sample.peripheral.lpuart:
610
sysbuild: true
@@ -22,10 +26,8 @@ tests:
2226
- nrf21540dk/nrf52840
2327
- nrf54l15dk/nrf54l15/cpuapp
2428
platform_exclude: native_posix
25-
tags: sysbuild ci_samples_peripheral_lpuart
2629
sample.peripheral.lpuart.debug:
2730
sysbuild: true
28-
build_only: true
2931
platform_allow:
3032
- nrf52833dk/nrf52833
3133
- nrf52840dk/nrf52840
@@ -41,7 +43,20 @@ tests:
4143
- CONFIG_UART_CONSOLE=y
4244
- CONFIG_LOG=y
4345
- CONFIG_NRF_SW_LPUART_LOG_LEVEL_DBG=y
44-
tags: ci_build sysbuild ci_samples_peripheral_lpuart
46+
harness: console
47+
harness_config:
48+
fixture: gpio_loopback
49+
type: multi_line
50+
ordered: true
51+
regex:
52+
- ".*app: ASYNC Tx sent 5 bytes"
53+
- ".*app: ASYNC Received data 5 bytes"
54+
- ".*app: ASYNC Received data 1 bytes"
55+
- ".*app: ASYNC Tx sent 5 bytes"
56+
- ".*app: ASYNC Received data 5 bytes"
57+
- ".*app: ASYNC Received data 1 bytes"
58+
- ".*app: ASYNC Tx sent 5 bytes"
59+
- ".*app: ASYNC Received data 5 bytes"
4560
sample.peripheral.lpuart_int_driven:
4661
sysbuild: true
4762
build_only: true
@@ -57,4 +72,36 @@ tests:
5772
- nrf54l15dk/nrf54l15/cpuapp
5873
platform_allow: nrf52dk/nrf52832 nrf52833dk/nrf52833 nrf52840dk/nrf52840 nrf9160dk/nrf9160/ns
5974
nrf5340dk/nrf5340/cpuapp nrf21540dk/nrf52840 nrf54l15dk/nrf54l15/cpuapp
60-
tags: ci_build sysbuild ci_samples_peripheral_lpuart
75+
sample.peripheral.lpuart_int_driven.debug:
76+
sysbuild: true
77+
extra_configs:
78+
- CONFIG_NRF_SW_LPUART_INT_DRIVEN=y
79+
- CONFIG_ASSERT=y
80+
- CONFIG_CONSOLE=y
81+
- CONFIG_UART_CONSOLE=y
82+
- CONFIG_LOG=y
83+
- CONFIG_NRF_SW_LPUART_LOG_LEVEL_DBG=y
84+
integration_platforms:
85+
- nrf52dk/nrf52832
86+
- nrf52833dk/nrf52833
87+
- nrf52840dk/nrf52840
88+
- nrf9160dk/nrf9160/ns
89+
- nrf5340dk/nrf5340/cpuapp
90+
- nrf21540dk/nrf52840
91+
- nrf54l15dk/nrf54l15/cpuapp
92+
platform_allow: nrf52dk/nrf52832 nrf52833dk/nrf52833 nrf52840dk/nrf52840 nrf9160dk/nrf9160/ns
93+
nrf5340dk/nrf5340/cpuapp nrf21540dk/nrf52840 nrf54l15dk/nrf54l15/cpuapp
94+
harness: console
95+
harness_config:
96+
fixture: gpio_loopback
97+
type: multi_line
98+
ordered: true
99+
regex:
100+
- ".*app: IRQ Tx sent 5 bytes"
101+
- ".*app: IRQ Received data 5 bytes"
102+
- ".*app: IRQ Received data 1 bytes"
103+
- ".*app: IRQ Tx sent 5 bytes"
104+
- ".*app: IRQ Received data 5 bytes"
105+
- ".*app: IRQ Received data 1 bytes"
106+
- ".*app: IRQ Tx sent 5 bytes"
107+
- ".*app: IRQ Received data 5 bytes"

samples/peripheral/lpuart/src/main.c

Lines changed: 46 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
* SPDX-License-Identifier: LicenseRef-Nordic-5-Clause
55
*/
66

7+
#include <string.h>
8+
#include <stdio.h>
79
#include <zephyr/kernel.h>
810
#include <zephyr/device.h>
911
#include <zephyr/devicetree.h>
@@ -15,21 +17,46 @@ LOG_MODULE_REGISTER(app);
1517
#define BUF_SIZE 64
1618
static K_MEM_SLAB_DEFINE(uart_slab, BUF_SIZE, 3, 4);
1719

18-
static void uart_irq_handler(const struct device *dev, void *context)
20+
#define TX_DATA_SIZE 5
21+
static const uint8_t tx_buf[TX_DATA_SIZE] = {1, 2, 3, 4, 5};
22+
static uint8_t buffer_print[TX_DATA_SIZE * 5 + 1];
23+
24+
static void to_display_format(const uint8_t *src, size_t size, char *dst)
25+
{
26+
size_t i;
27+
28+
for (i = 0; i < size; i++) {
29+
sprintf(dst + 5 * i, "0x%02x,", src[i]);
30+
}
31+
}
32+
33+
static void verify_buffers(const uint8_t *tx_data, char *rx_data, size_t size)
1934
{
20-
uint8_t buf[] = {1, 2, 3, 4, 5};
35+
if (memcmp(tx_data, rx_data, size)) {
36+
to_display_format(tx_data, size, buffer_print);
37+
LOG_ERR("Buffer contents are different: %s", buffer_print);
38+
to_display_format(rx_data, size, buffer_print);
39+
LOG_ERR(" vs: %s", buffer_print);
40+
__ASSERT(false, "Buffer contents are different");
41+
}
42+
}
2143

44+
static void uart_irq_handler(const struct device *dev, void *context)
45+
{
2246
if (uart_irq_tx_ready(dev)) {
23-
(void)uart_fifo_fill(dev, buf, sizeof(buf));
47+
(void)uart_fifo_fill(dev, tx_buf, TX_DATA_SIZE);
2448
uart_irq_tx_disable(dev);
49+
LOG_INF("IRQ Tx sent %d bytes", TX_DATA_SIZE);
2550
}
2651

2752
if (uart_irq_rx_ready(dev)) {
28-
uint8_t buf[10];
53+
uint8_t buf[TX_DATA_SIZE + 1];
2954
int len = uart_fifo_read(dev, buf, sizeof(buf));
3055

31-
if (len) {
32-
printk("read %d bytes\n", len);
56+
LOG_INF("IRQ Received data %d bytes", len);
57+
__ASSERT(len == 1 || len == TX_DATA_SIZE, "Received unexpected data");
58+
if (len == TX_DATA_SIZE) {
59+
verify_buffers(tx_buf, buf, TX_DATA_SIZE);
3360
}
3461
}
3562
}
@@ -49,28 +76,31 @@ static void interrupt_driven(const struct device *dev)
4976
}
5077
}
5178

52-
static void uart_callback(const struct device *dev,
53-
struct uart_event *evt,
54-
void *user_data)
79+
static void uart_callback(const struct device *dev, struct uart_event *evt, void *user_data)
5580
{
5681
struct device *uart = user_data;
5782
int err;
5883

5984
switch (evt->type) {
6085
case UART_TX_DONE:
61-
LOG_INF("Tx sent %d bytes", evt->data.tx.len);
86+
LOG_INF("ASYNC Tx sent %d bytes", evt->data.tx.len);
6287
break;
6388

6489
case UART_TX_ABORTED:
65-
LOG_ERR("Tx aborted");
90+
LOG_ERR("ASYNC Tx aborted");
6691
break;
6792

6893
case UART_RX_RDY:
69-
LOG_INF("Received data %d bytes", evt->data.rx.len);
94+
LOG_INF("ASYNC Received data %d bytes", evt->data.rx.len);
95+
__ASSERT(evt->data.rx.len == 1 || evt->data.rx.len == TX_DATA_SIZE,
96+
"Received unexpected data");
97+
if (evt->data.rx.len == TX_DATA_SIZE) {
98+
verify_buffers(tx_buf, evt->data.rx.buf + evt->data.rx.offset,
99+
TX_DATA_SIZE);
100+
}
70101
break;
71102

72-
case UART_RX_BUF_REQUEST:
73-
{
103+
case UART_RX_BUF_REQUEST: {
74104
uint8_t *buf;
75105

76106
err = k_mem_slab_alloc(&uart_slab, (void **)&buf, K_NO_WAIT);
@@ -95,7 +125,6 @@ static void uart_callback(const struct device *dev,
95125

96126
static void async(const struct device *lpuart)
97127
{
98-
uint8_t txbuf[5] = {1, 2, 3, 4, 5};
99128
int err;
100129
uint8_t *buf;
101130

@@ -109,12 +138,12 @@ static void async(const struct device *lpuart)
109138
__ASSERT(err == 0, "Failed to enable RX");
110139

111140
while (1) {
112-
err = uart_tx(lpuart, txbuf, sizeof(txbuf), 10000);
141+
err = uart_tx(lpuart, tx_buf, sizeof(tx_buf), 10000);
113142
__ASSERT(err == 0, "Failed to initiate transmission");
114143

115144
k_sleep(K_MSEC(500));
116145

117-
uart_poll_out(lpuart, txbuf[0]);
146+
uart_poll_out(lpuart, tx_buf[0]);
118147
k_sleep(K_MSEC(100));
119148
}
120149
}

0 commit comments

Comments
 (0)