Skip to content

Commit b20c15b

Browse files
committed
tests: drivers: uart: async_dual: Add test cases with runtime PM
Add configurations in which runtime PM is enabled. There are 2 configurations: - Test does not call PM API for getting and putting the device. In that configuration UART device shall be suspended whenever not active. - Test is calling PM API which should keep device active as long as requested (e.g. during packet reception which consists of multiple chunks). Signed-off-by: Krzysztof Chruściński <[email protected]>
1 parent 7287ee3 commit b20c15b

File tree

6 files changed

+123
-0
lines changed

6 files changed

+123
-0
lines changed

tests/drivers/uart/uart_async_dual/Kconfig

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,5 +11,10 @@ config UART_ASYNC_DUAL_TEST_TIMEOUT
1111
For how many loops will the stress test run. The higher this number the longer the
1212
test and therefore the higher likelihood an unlikely race/event will be triggered.
1313

14+
config PM_RUNTIME_IN_TEST
15+
bool "Use runtime PM in the test"
16+
select PM_DEVICE
17+
select PM_DEVICE_RUNTIME
18+
1419
# Include Zephyr's Kconfig
1520
source "Kconfig"

tests/drivers/uart/uart_async_dual/boards/nrf52_bsim.overlay

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ dut: &uart0 {
2828
pinctrl-1 = <&uart0_sleep>;
2929
pinctrl-names = "default", "sleep";
3030
hw-flow-control;
31+
zephyr,pm-device-runtime-auto;
3132
};
3233

3334
&timer0 {

tests/drivers/uart/uart_async_dual/boards/nrf54h20dk_nrf54h20_common.dtsi

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ dut: &uart134 {
6767
pinctrl-1 = <&uart134_alt_sleep>;
6868
pinctrl-names = "default", "sleep";
6969
hw-flow-control;
70+
zephyr,pm-device-runtime-auto;
7071
};
7172

7273
dut_aux: &uart137 {
@@ -76,6 +77,7 @@ dut_aux: &uart137 {
7677
pinctrl-1 = <&uart137_alt_sleep>;
7778
pinctrl-names = "default", "sleep";
7879
hw-flow-control;
80+
zephyr,pm-device-runtime-auto;
7981
};
8082

8183
dut2: &uart120 {
@@ -85,6 +87,7 @@ dut2: &uart120 {
8587
pinctrl-names = "default", "sleep";
8688
current-speed = <115200>;
8789
hw-flow-control;
90+
zephyr,pm-device-runtime-auto;
8891
};
8992

9093
&timer137 {

tests/drivers/uart/uart_async_dual/boards/nrf54l15pdk_nrf54l15_cpuapp.overlay

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ dut: &uart21 {
4545
pinctrl-1 = <&uart21_sleep>;
4646
pinctrl-names = "default", "sleep";
4747
hw-flow-control;
48+
zephyr,pm-device-runtime-auto;
4849
};
4950

5051
dut_aux: &uart22 {
@@ -54,6 +55,7 @@ dut_aux: &uart22 {
5455
pinctrl-1 = <&uart22_sleep>;
5556
pinctrl-names = "default", "sleep";
5657
hw-flow-control;
58+
zephyr,pm-device-runtime-auto;
5759
};
5860

5961
&timer20 {

tests/drivers/uart/uart_async_dual/src/main.c

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
*/
66

77
#include <zephyr/drivers/uart.h>
8+
#include <zephyr/pm/device.h>
9+
#include <zephyr/pm/device_runtime.h>
810
#include <zephyr/random/random.h>
911
#include <zephyr/sys/ring_buffer.h>
1012
#include <zephyr/ztest.h>
@@ -57,6 +59,40 @@ ZTEST_DMEM struct dut_data duts[] = {
5759
#endif
5860
};
5961

62+
static void pm_check(const struct device *dev, const struct device *second_dev, bool exp_on,
63+
int line)
64+
{
65+
if (!IS_ENABLED(CONFIG_PM_DEVICE_RUNTIME)) {
66+
return;
67+
}
68+
69+
if (dev == second_dev) {
70+
return;
71+
}
72+
73+
enum pm_device_state state;
74+
int err;
75+
int cnt;
76+
77+
cnt = pm_device_runtime_usage(dev);
78+
err = pm_device_state_get(dev, &state);
79+
zassert_equal(err, 0);
80+
81+
if (exp_on) {
82+
zassert_not_equal(cnt, 0, "Wrong PM cnt:%d, line:%d", cnt, line);
83+
zassert_equal(state, PM_DEVICE_STATE_ACTIVE,
84+
"Wrong PM state %s, line:%d", pm_device_state_str(state), line);
85+
return;
86+
}
87+
88+
/* Expect device to be off. */
89+
zassert_equal(cnt, 0, "Wrong PM count:%d, line:%d", cnt, line);
90+
zassert_equal(state, PM_DEVICE_STATE_SUSPENDED,
91+
"Wrong PM state %s, line:%d", pm_device_state_str(state), line);
92+
}
93+
94+
#define PM_CHECK(dev, second_dev, exp_on) pm_check(dev, second_dev, exp_on, __LINE__)
95+
6096
static const struct device *rx_dev;
6197
static const struct device *tx_dev;
6298

@@ -452,11 +488,20 @@ static void hci_like_callback(const struct device *dev, struct uart_event *evt,
452488
{
453489
switch (evt->type) {
454490
case UART_TX_DONE:
491+
455492
zassert_true(dev == tx_dev);
493+
if (IS_ENABLED(CONFIG_PM_RUNTIME_IN_TEST)) {
494+
PM_CHECK(tx_dev, rx_dev, true);
495+
pm_device_runtime_put(tx_dev);
496+
}
497+
PM_CHECK(tx_dev, rx_dev, false);
456498
k_sem_give(&tx_data.sem);
457499
break;
458500
case UART_TX_ABORTED:
459501
zassert_true(dev == tx_dev);
502+
if (IS_ENABLED(CONFIG_PM_RUNTIME_IN_TEST)) {
503+
pm_device_runtime_put(tx_dev);
504+
}
460505
zassert_false(tx_data.cont,
461506
"Unexpected TX abort, receiver not reading data on time");
462507
break;
@@ -568,6 +613,10 @@ static void hci_like_tx(struct test_tx_data *tx_data)
568613
uint8_t len = buf[4] + 5;
569614
int err;
570615

616+
if (IS_ENABLED(CONFIG_PM_RUNTIME_IN_TEST)) {
617+
pm_device_runtime_get(tx_dev);
618+
}
619+
571620
err = uart_tx(tx_dev, buf, len, TX_TIMEOUT);
572621
zassert_equal(err, 0, "Unexpected err:%d", err);
573622
}
@@ -595,25 +644,57 @@ static void hci_like_rx(void)
595644
uint8_t last_hdr = 0xff;
596645
uint8_t len;
597646
bool cont;
647+
bool explicit_pm = IS_ENABLED(CONFIG_PM_RUNTIME_IN_TEST);
598648

599649
while (1) {
650+
if (explicit_pm) {
651+
pm_device_runtime_get(rx_dev);
652+
}
653+
600654
cont = rx(rx_data.buf, 1);
601655
if (!cont) {
656+
if (explicit_pm) {
657+
pm_device_runtime_put(rx_dev);
658+
}
602659
break;
603660
}
604661
check_pre_hdr(rx_data.buf, last_hdr);
605662
last_hdr = rx_data.buf[0];
606663

664+
/* If explicitly requested device to stay on in should be on.
665+
* If application did not touch PM, device should be off.
666+
*/
667+
PM_CHECK(rx_dev, tx_dev, explicit_pm);
668+
607669
cont = rx(rx_data.buf, 4);
608670
if (!cont) {
671+
if (explicit_pm) {
672+
pm_device_runtime_put(rx_dev);
673+
}
609674
break;
610675
}
611676
len = get_len(rx_data.buf);
612677

678+
/* If explicitly requested device to stay on in should be on.
679+
* If application did not touch PM, device should be off.
680+
*/
681+
PM_CHECK(rx_dev, tx_dev, explicit_pm);
682+
613683
cont = rx(rx_data.buf, len);
614684
if (!cont) {
685+
if (explicit_pm) {
686+
pm_device_runtime_put(rx_dev);
687+
}
615688
break;
616689
}
690+
691+
if (explicit_pm) {
692+
pm_device_runtime_put(rx_dev);
693+
}
694+
695+
/* Device shall be released and off. */
696+
PM_CHECK(rx_dev, tx_dev, false);
697+
617698
check_payload(rx_data.buf, len);
618699
}
619700
}
@@ -677,6 +758,8 @@ static void hci_like_test(uint32_t baudrate)
677758
/* Flush data. */
678759
(void)uart_tx_abort(tx_dev);
679760
k_msleep(10);
761+
PM_CHECK(tx_dev, rx_dev, false);
762+
680763
(void)uart_rx_enable(rx_dev, rx_data.buf, sizeof(rx_data.buf), RX_TIMEOUT);
681764
k_msleep(1);
682765
(void)uart_rx_disable(rx_dev);

tests/drivers/uart/uart_async_dual/testcase.yaml

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,3 +29,32 @@ tests:
2929
- nrf52_bsim
3030
extra_configs:
3131
- CONFIG_TEST_BUSY_SIM=y
32+
drivers.uart.async_dual.pm_runtime:
33+
harness: ztest
34+
harness_config:
35+
fixture: uart_loopback
36+
depends_on: gpio
37+
platform_allow:
38+
- nrf54l15pdk/nrf54l15/cpuapp
39+
- nrf54h20dk/nrf54h20/cpuapp
40+
- nrf54h20dk/nrf54h20/cpurad
41+
- nrf54h20dk/nrf54h20/cpuppr
42+
- nrf9160dk/nrf9160
43+
- nrf52_bsim
44+
extra_configs:
45+
- CONFIG_PM_DEVICE_RUNTIME=y
46+
- CONFIG_PM_DEVICE=y
47+
drivers.uart.async_dual.pm_runtime.explicit:
48+
harness: ztest
49+
harness_config:
50+
fixture: uart_loopback
51+
depends_on: gpio
52+
platform_allow:
53+
- nrf54l15pdk/nrf54l15/cpuapp
54+
- nrf54h20dk/nrf54h20/cpuapp
55+
- nrf54h20dk/nrf54h20/cpurad
56+
- nrf54h20dk/nrf54h20/cpuppr
57+
- nrf9160dk/nrf9160
58+
- nrf52_bsim
59+
extra_configs:
60+
- CONFIG_PM_RUNTIME_IN_TEST=y

0 commit comments

Comments
 (0)