Skip to content

Commit

Permalink
[nrf fromtree] tests: boards: nrf: qdec: Add device runtime PM config…
Browse files Browse the repository at this point in the history
…uration

Add configuration to the test which is using runtime PM on qdec.

Signed-off-by: Krzysztof Chruściński <[email protected]>
(cherry picked from commit 5371eff)
  • Loading branch information
nordic-krch committed Oct 18, 2024
1 parent 21118d1 commit 2f149ee
Show file tree
Hide file tree
Showing 8 changed files with 170 additions and 19 deletions.
17 changes: 16 additions & 1 deletion tests/boards/nrf/qdec/boards/nrf52840dk_nrf52840.overlay
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,27 @@
<NRF_PSEL(QDEC_B, 1, 3)>; /* Arduino D2 */
};
};

qdec_sleep_pinctrl: qdec_sleep_pinctrl {
group1 {
psels = <NRF_PSEL(QDEC_A, 1, 1)>, /* Ardiuno D0 */
<NRF_PSEL(QDEC_B, 1, 3)>; /* Arduino D2 */
low-power-enable;
};
};
};

&qdec0 {
status = "okay";
pinctrl-0 = <&qdec_pinctrl>;
pinctrl-names = "default";
pinctrl-1 = <&qdec_sleep_pinctrl>;
pinctrl-names = "default", "sleep";
steps = < 127 >;
led-pre = < 500 >;
zephyr,pm-device-runtime-auto;
};

/* To prevent enabling console receiver. */
&uart0 {
disable-rx;
};
17 changes: 16 additions & 1 deletion tests/boards/nrf/qdec/boards/nrf5340dk_nrf5340_cpuapp.overlay
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,27 @@
<NRF_PSEL(QDEC_B, 0, 6)>; /* Arduino A2 */
};
};

qdec_sleep_pinctrl: qdec_sleep_pinctrl {
group1 {
psels = <NRF_PSEL(QDEC_A, 0, 4)>,
<NRF_PSEL(QDEC_B, 0, 6)>;
low-power-enable;
};
};
};

&qdec1 {
status = "okay";
pinctrl-0 = <&qdec_pinctrl>;
pinctrl-names = "default";
pinctrl-1 = <&qdec_sleep_pinctrl>;
pinctrl-names = "default", "sleep";
steps = < 127 >;
led-pre = < 500 >;
zephyr,pm-device-runtime-auto;
};

/* To prevent enabling console receiver. */
&uart0 {
disable-rx;
};
17 changes: 16 additions & 1 deletion tests/boards/nrf/qdec/boards/nrf54h20dk_nrf54h20_cpuapp.overlay
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,14 @@
<NRF_PSEL(QDEC_B, 1, 2)>;
};
};

qdec_sleep_pinctrl: qdec_sleep_pinctrl {
group1 {
psels = <NRF_PSEL(QDEC_A, 1, 0)>,
<NRF_PSEL(QDEC_B, 1, 2)>;
low-power-enable;
};
};
};

&gpio1 {
Expand All @@ -41,7 +49,14 @@
&qdec130 {
status = "okay";
pinctrl-0 = <&qdec_pinctrl>;
pinctrl-names = "default";
pinctrl-1 = <&qdec_sleep_pinctrl>;
pinctrl-names = "default", "sleep";
steps = <127>;
led-pre = <500>;
zephyr,pm-device-runtime-auto;
};

/* To prevent enabling console receiver. */
&uart136 {
disable-rx;
};
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,8 @@
*/

#include "nrf54l15pdk_nrf54l15_common.dtsi"

/* To prevent enabling console receiver. */
&uart20 {
disable-rx;
};
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,8 @@
*/

#include "nrf54l15pdk_nrf54l15_common.dtsi"

/* To prevent enabling console receiver. */
&uart30 {
disable-rx;
};
12 changes: 11 additions & 1 deletion tests/boards/nrf/qdec/boards/nrf54l15pdk_nrf54l15_common.dtsi
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,14 @@
<NRF_PSEL(QDEC_B, 1, 10)>;
};
};

qdec_sleep_pinctrl: qdec_sleep_pinctrl {
group1 {
psels = <NRF_PSEL(QDEC_A, 1, 8)>,
<NRF_PSEL(QDEC_B, 1, 10)>;
low-power-enable;
};
};
};

&gpio1 {
Expand All @@ -37,7 +45,9 @@
&qdec20 {
status = "okay";
pinctrl-0 = <&qdec_pinctrl>;
pinctrl-names = "default";
pinctrl-1 = <&qdec_sleep_pinctrl>;
pinctrl-names = "default", "sleep";
steps = <127>;
led-pre = <500>;
zephyr,pm-device-runtime-auto;
};
76 changes: 76 additions & 0 deletions tests/boards/nrf/qdec/src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#include <zephyr/device.h>
#include <zephyr/drivers/sensor.h>
#include <zephyr/drivers/gpio.h>
#include <zephyr/pm/device_runtime.h>

static K_SEM_DEFINE(sem, 0, 1);
static const struct gpio_dt_spec phase_a = GPIO_DT_SPEC_GET(DT_ALIAS(qenca), gpios);
Expand Down Expand Up @@ -140,6 +141,10 @@ ZTEST(qdec_sensor, test_sensor_trigger_set_and_disable)
{
int rc;

if (IS_ENABLED(CONFIG_PM_DEVICE_RUNTIME)) {
pm_device_runtime_get(qdec_dev);
}

qdec_trigger.type = SENSOR_TRIG_DATA_READY;
qdec_trigger.chan = SENSOR_CHAN_ALL;
rc = sensor_trigger_set(qdec_dev, &qdec_trigger, qdec_trigger_handler);
Expand All @@ -158,10 +163,18 @@ ZTEST(qdec_sensor, test_sensor_trigger_set_and_disable)
/* emulation not working, but there maybe old trigger, ignore */
rc = k_sem_take(&sem, K_MSEC(200));

if (IS_ENABLED(CONFIG_PM_DEVICE_RUNTIME)) {
pm_device_runtime_put(qdec_dev);
}

/* there should be no triggers now*/
rc = k_sem_take(&sem, K_MSEC(200));
zassert_true(rc == -EAGAIN, "qdec handler should not be triggered (%d)", rc);

if (IS_ENABLED(CONFIG_PM_DEVICE_RUNTIME)) {
pm_device_runtime_get(qdec_dev);
}

/* register empty trigger - disable trigger */
rc = sensor_trigger_set(qdec_dev, &qdec_trigger, NULL);
zassert_true(rc == 0, "sensor_trigger_set failed: %d", rc);
Expand All @@ -171,6 +184,10 @@ ZTEST(qdec_sensor, test_sensor_trigger_set_and_disable)
/* emulation working, but handler not set, thus should not be called */
rc = k_sem_take(&sem, K_MSEC(200));
zassert_true(rc == -EAGAIN, "qdec handler should not be triggered (%d)", rc);

if (IS_ENABLED(CONFIG_PM_DEVICE_RUNTIME)) {
pm_device_runtime_put(qdec_dev);
}
}

/**
Expand All @@ -184,6 +201,10 @@ ZTEST(qdec_sensor, test_sensor_trigger_set)
int rc;
struct sensor_value val = {0};

if (IS_ENABLED(CONFIG_PM_DEVICE_RUNTIME)) {
pm_device_runtime_get(qdec_dev);
}

qdec_trigger.type = SENSOR_TRIG_DATA_READY;
qdec_trigger.chan = SENSOR_CHAN_ROTATION;
rc = sensor_trigger_set(qdec_dev, &qdec_trigger, qdec_trigger_handler);
Expand All @@ -202,6 +223,10 @@ ZTEST(qdec_sensor, test_sensor_trigger_set)

TC_PRINT("QDEC reading: %d\n", val.val1);
zassert_true(val.val1 != 0, "No readings from QDEC");

if (IS_ENABLED(CONFIG_PM_DEVICE_RUNTIME)) {
pm_device_runtime_put(qdec_dev);
}
}

/**
Expand All @@ -214,6 +239,10 @@ ZTEST(qdec_sensor, test_sensor_trigger_set_negative)
{
int rc;

if (IS_ENABLED(CONFIG_PM_DEVICE_RUNTIME)) {
pm_device_runtime_get(qdec_dev);
}

rc = sensor_trigger_set(qdec_dev, &qdec_trigger, qdec_trigger_handler);
zassume_true(rc != -ENOSYS, "sensor_trigger_set not supported");

Expand All @@ -228,6 +257,10 @@ ZTEST(qdec_sensor, test_sensor_trigger_set_negative)

rc = sensor_trigger_set(qdec_dev, &qdec_trigger, qdec_trigger_handler);
zassume_true(rc < 0, "sensor_trigger_set should fail due to invalid channel");

if (IS_ENABLED(CONFIG_PM_DEVICE_RUNTIME)) {
pm_device_runtime_put(qdec_dev);
}
}

/**
Expand All @@ -238,12 +271,20 @@ ZTEST(qdec_sensor, test_sensor_trigger_set_negative)
*/
ZTEST(qdec_sensor, test_qdec_readings)
{
if (IS_ENABLED(CONFIG_PM_DEVICE_RUNTIME)) {
pm_device_runtime_get(qdec_dev);
}

qenc_emulate_verify_reading(10, 100, true, false);
qenc_emulate_verify_reading(2, 500, true, false);
qenc_emulate_verify_reading(10, 200, false, false);
/* may lead to overflows but currently driver does not detects that */
qenc_emulate_verify_reading(1, 1000, false, true);
qenc_emulate_verify_reading(1, 1000, true, true);

if (IS_ENABLED(CONFIG_PM_DEVICE_RUNTIME)) {
pm_device_runtime_put(qdec_dev);
}
}

/**
Expand All @@ -257,9 +298,16 @@ ZTEST(qdec_sensor, test_sensor_channel_get_empty)
int rc;
struct sensor_value val = {0};

if (IS_ENABLED(CONFIG_PM_DEVICE_RUNTIME)) {
pm_device_runtime_get(qdec_dev);
}

rc = sensor_sample_fetch(qdec_dev);
zassert_true(rc == 0, "Failed to fetch sample (%d)", rc);

/* wait for potential new readings */
k_msleep(100);

/* get readings but ignore them, as they may include reading from time
* when emulation was still working (i.e. during previous test)
*/
Expand All @@ -277,6 +325,10 @@ ZTEST(qdec_sensor, test_sensor_channel_get_empty)
zassert_true(rc == 0, "Failed to get sample (%d)", rc);
zassert_true(val.val1 == 0, "Expected no readings but got: %d", val.val1);
zassert_true(val.val2 == 0, "Expected no readings but got: %d", val.val2);

if (IS_ENABLED(CONFIG_PM_DEVICE_RUNTIME)) {
pm_device_runtime_put(qdec_dev);
}
}

/**
Expand All @@ -291,6 +343,10 @@ ZTEST(qdec_sensor, test_sensor_channel_get)
struct sensor_value val_first = {0};
struct sensor_value val_second = {0};

if (IS_ENABLED(CONFIG_PM_DEVICE_RUNTIME)) {
pm_device_runtime_get(qdec_dev);
}

qenc_emulate_start(K_MSEC(10), true);

/* wait for some readings*/
Expand Down Expand Up @@ -325,6 +381,10 @@ ZTEST(qdec_sensor, test_sensor_channel_get)
*/
TC_PRINT("Expected the same readings: %d vs %d - ignore!\n", val_first.val2,
val_second.val2);

if (IS_ENABLED(CONFIG_PM_DEVICE_RUNTIME)) {
pm_device_runtime_put(qdec_dev);
}
}

/**
Expand All @@ -338,6 +398,10 @@ ZTEST(qdec_sensor, test_sensor_channel_get_negative)
int rc;
struct sensor_value val = {0};

if (IS_ENABLED(CONFIG_PM_DEVICE_RUNTIME)) {
pm_device_runtime_get(qdec_dev);
}

qenc_emulate_start(K_MSEC(10), true);

/* wait for some readings*/
Expand All @@ -350,6 +414,10 @@ ZTEST(qdec_sensor, test_sensor_channel_get_negative)
zassert_true(rc < 0, "Should failed to get sample (%d)", rc);
zassert_true(val.val1 == 0, "Some readings from QDEC: %d", val.val1);
zassert_true(val.val2 == 0, "Some readings from QDEC: %d", val.val2);

if (IS_ENABLED(CONFIG_PM_DEVICE_RUNTIME)) {
pm_device_runtime_put(qdec_dev);
}
}

/**
Expand All @@ -362,6 +430,10 @@ ZTEST(qdec_sensor, test_sensor_sample_fetch)
{
int rc;

if (IS_ENABLED(CONFIG_PM_DEVICE_RUNTIME)) {
pm_device_runtime_get(qdec_dev);
}

rc = sensor_sample_fetch(qdec_dev);
zassert_true(rc == 0, "Failed to fetch sample (%d)", rc);

Expand All @@ -370,6 +442,10 @@ ZTEST(qdec_sensor, test_sensor_sample_fetch)

rc = sensor_sample_fetch_chan(qdec_dev, SENSOR_CHAN_MAX);
zassert_true(rc < 0, "Should fail to fetch sample from invalid channel (%d)", rc);

if (IS_ENABLED(CONFIG_PM_DEVICE_RUNTIME)) {
pm_device_runtime_put(qdec_dev);
}
}

static void *setup(void)
Expand Down
40 changes: 25 additions & 15 deletions tests/boards/nrf/qdec/testcase.yaml
Original file line number Diff line number Diff line change
@@ -1,21 +1,31 @@
common:
platform_allow:
- nrf52840dk/nrf52840
- nrf5340dk/nrf5340/cpuapp
- nrf54l15dk/nrf54l15/cpuapp
- nrf54l15dk/nrf54l15/cpuflpr
- nrf54h20dk/nrf54h20/cpuapp
integration_platforms:
- nrf52840dk/nrf52840
- nrf5340dk/nrf5340/cpuapp
- nrf54l15dk/nrf54l15/cpuapp
- nrf54l15dk/nrf54l15/cpuflpr
- nrf54h20dk/nrf54h20/cpuapp
harness: ztest
harness_config:
fixture: gpio_loopback
tests:
drivers.sensor.qdec:
tags:
- drivers
- sensors
- qdec
platform_allow:
- nrf52840dk/nrf52840
- nrf5340dk/nrf5340/cpuapp
- nrf54l15dk/nrf54l15/cpuapp
- nrf54l15dk/nrf54l15/cpuflpr
- nrf54h20dk/nrf54h20/cpuapp
integration_platforms:
- nrf52840dk/nrf52840
- nrf5340dk/nrf5340/cpuapp
- nrf54l15dk/nrf54l15/cpuapp
- nrf54l15dk/nrf54l15/cpuflpr
- nrf54h20dk/nrf54h20/cpuapp
harness: ztest
harness_config:
fixture: gpio_loopback
drivers.sensor.qdec.pm_runtime:
tags:
- drivers
- sensors
- qdec
- pm
extra_configs:
- CONFIG_PM_DEVICE=y
- CONFIG_PM_DEVICE_RUNTIME=y

0 comments on commit 2f149ee

Please sign in to comment.