Skip to content

Commit aa007b9

Browse files
committed
tests: Update tests/samples affected by pm_notifier API change
Update pm tests and sample that make use of pm_notifier functionality, or test system power state transitions. Signed-off-by: Kenneth J. Miller <[email protected]>
1 parent 337d483 commit aa007b9

File tree

6 files changed

+178
-121
lines changed

6 files changed

+178
-121
lines changed

samples/boards/mec15xxevb_assy6853/power_management/src/power_mgmt.c

Lines changed: 56 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -90,44 +90,43 @@ static void pm_latency_check(void)
9090
}
9191

9292
/* Hooks to count entry/exit */
93-
static void notify_pm_state_entry(enum pm_state state)
93+
static void notify_pm_state(uint8_t direction, void *ctx)
9494
{
95-
if (!checks_enabled) {
96-
return;
97-
}
95+
ARG_UNUSED(ctx);
9896

99-
switch (state) {
100-
case PM_STATE_SUSPEND_TO_IDLE:
101-
GPIO_CTRL_REGS->CTRL_0012 = 0x240ul;
102-
pm_counters[0].entry_cnt++;
103-
break;
104-
case PM_STATE_SUSPEND_TO_RAM:
105-
GPIO_CTRL_REGS->CTRL_0013 = 0x240ul;
106-
pm_counters[1].entry_cnt++;
107-
pm_latency_check();
108-
break;
109-
default:
110-
break;
111-
}
112-
}
97+
const struct pm_state_info *pm_state = pm_state_next_get(0u);
11398

114-
static void notify_pm_state_exit(enum pm_state state)
115-
{
11699
if (!checks_enabled) {
117100
return;
118101
}
119102

120-
switch (state) {
121-
case PM_STATE_SUSPEND_TO_IDLE:
122-
GPIO_CTRL_REGS->CTRL_0012 = 0x10240ul;
123-
pm_counters[0].exit_cnt++;
124-
break;
125-
case PM_STATE_SUSPEND_TO_RAM:
126-
GPIO_CTRL_REGS->CTRL_0013 = 0x10240ul;
127-
pm_counters[1].exit_cnt++;
128-
break;
129-
default:
130-
break;
103+
if (direction & PM_STATE_ENTRY) {
104+
pm_counters[pm_state->state].entry_cnt++;
105+
106+
switch (pm_state->state) {
107+
case PM_STATE_SUSPEND_TO_IDLE:
108+
GPIO_CTRL_REGS->CTRL_0012 = 0x240ul;
109+
break;
110+
case PM_STATE_SUSPEND_TO_RAM:
111+
GPIO_CTRL_REGS->CTRL_0013 = 0x240ul;
112+
pm_latency_check();
113+
break;
114+
default:
115+
break;
116+
}
117+
} else {
118+
pm_counters[pm_state->state].exit_cnt++;
119+
120+
switch (pm_state->state) {
121+
case PM_STATE_SUSPEND_TO_IDLE:
122+
GPIO_CTRL_REGS->CTRL_0012 = 0x10240ul;
123+
break;
124+
case PM_STATE_SUSPEND_TO_RAM:
125+
GPIO_CTRL_REGS->CTRL_0013 = 0x10240ul;
126+
break;
127+
default:
128+
break;
129+
}
131130
}
132131
}
133132

@@ -256,21 +255,24 @@ static void resume_all_tasks(void)
256255
k_thread_resume(&thread_b_id);
257256
}
258257

259-
static struct pm_notifier notifier = {
260-
.state_entry = notify_pm_state_entry,
261-
.state_exit = notify_pm_state_exit,
262-
};
258+
PM_NOTIFIER_DEFINE(test_light_sleep, (PM_STATE_ENTRY|PM_STATE_EXIT), notify_pm_state, NULL);
259+
PM_NOTIFIER_DEFINE(test_deep_sleep, (PM_STATE_ENTRY|PM_STATE_EXIT), notify_pm_state, NULL);
263260

264261
int test_pwr_mgmt_multithread(bool use_logging, uint8_t cycles)
265262
{
266263
uint8_t iterations = cycles;
264+
const struct pm_state_info *light_sleep = &residency_info[0];
265+
const struct pm_state_info *deep_sleep = &residency_info[residency_info_len - 1];
267266
/* Ensure we can enter deep sleep when stopping threads
268267
* No UART output should occur when threads are suspended
269268
* Test to verify Zephyr RTOS issue #20033
270269
* https://github.com/zephyrproject-rtos/zephyr/issues/20033
271270
*/
272271

273-
pm_notifier_register(&notifier);
272+
pm_notifier_register(&PM_NOTIFIER(test_light_sleep), light_sleep->state,
273+
light_sleep->substate_id);
274+
pm_notifier_register(&PM_NOTIFIER(test_deep_sleep), deep_sleep->state,
275+
deep_sleep->substate_id);
274276
create_tasks();
275277

276278
LOG_WRN("PM multi-thread test started for cycles: %d, logging: %d",
@@ -283,7 +285,7 @@ int test_pwr_mgmt_multithread(bool use_logging, uint8_t cycles)
283285
LOG_INF("Suspend...");
284286
suspend_all_tasks();
285287
LOG_INF("About to enter light sleep");
286-
k_msleep((residency_info[0].min_residency_us / 1000U) +
288+
k_msleep((light_sleep->min_residency_us / 1000U) +
287289
LT_EXTRA_SLP_TIME);
288290
k_busy_wait(100);
289291

@@ -304,7 +306,7 @@ int test_pwr_mgmt_multithread(bool use_logging, uint8_t cycles)
304306
/* GPIO toggle to measure latency for deep sleep */
305307
pm_trigger_marker();
306308
k_msleep(
307-
(residency_info[residency_info_len - 1].min_residency_us /
309+
(deep_sleep->min_residency_us /
308310
1000U) + DP_EXTRA_SLP_TIME);
309311
k_busy_wait(100);
310312

@@ -324,25 +326,33 @@ int test_pwr_mgmt_multithread(bool use_logging, uint8_t cycles)
324326
LOG_INF("PM multi-thread completed");
325327
pm_check_counters(cycles);
326328
pm_reset_counters();
327-
pm_notifier_unregister(&notifier);
329+
pm_notifier_unregister(&PM_NOTIFIER(test_light_sleep), light_sleep->state,
330+
light_sleep->substate_id);
331+
pm_notifier_unregister(&PM_NOTIFIER(test_deep_sleep), deep_sleep->state,
332+
deep_sleep->substate_id);
328333

329334
return 0;
330335
}
331336

332337
int test_pwr_mgmt_singlethread(bool use_logging, uint8_t cycles)
333338
{
334339
uint8_t iterations = cycles;
340+
const struct pm_state_info *light_sleep = &residency_info[0];
341+
const struct pm_state_info *deep_sleep = &residency_info[residency_info_len - 1];
335342

336343
LOG_WRN("PM single-thread test started for cycles: %d, logging: %d",
337344
cycles, use_logging);
338345

339-
pm_notifier_register(&notifier);
346+
pm_notifier_register(&PM_NOTIFIER(test_light_sleep), light_sleep->state,
347+
light_sleep->substate_id);
348+
pm_notifier_register(&PM_NOTIFIER(test_deep_sleep), deep_sleep->state,
349+
deep_sleep->substate_id);
340350
checks_enabled = true;
341351
while (iterations-- > 0) {
342352

343353
/* Trigger Light Sleep 1 state. 48MHz PLL stays on */
344354
LOG_INF("About to enter light sleep");
345-
k_msleep((residency_info[0].min_residency_us / 1000U) +
355+
k_msleep((light_sleep->min_residency_us / 1000U) +
346356
LT_EXTRA_SLP_TIME);
347357
k_busy_wait(100);
348358

@@ -358,7 +368,7 @@ int test_pwr_mgmt_singlethread(bool use_logging, uint8_t cycles)
358368
/* GPIO toggle to measure latency */
359369
pm_trigger_marker();
360370
k_msleep(
361-
(residency_info[residency_info_len - 1].min_residency_us /
371+
(deep_sleep->min_residency_us /
362372
1000U) + DP_EXTRA_SLP_TIME);
363373
k_busy_wait(100);
364374

@@ -374,7 +384,10 @@ int test_pwr_mgmt_singlethread(bool use_logging, uint8_t cycles)
374384
LOG_INF("PM single-thread completed");
375385
pm_check_counters(cycles);
376386
pm_reset_counters();
377-
pm_notifier_unregister(&notifier);
387+
pm_notifier_unregister(&PM_NOTIFIER(test_light_sleep), light_sleep->state,
388+
light_sleep->substate_id);
389+
pm_notifier_unregister(&PM_NOTIFIER(test_deep_sleep), deep_sleep->state,
390+
deep_sleep->substate_id);
378391

379392
return 0;
380393
}

tests/subsys/pm/device_wakeup_api/boards/native_posix.overlay

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,18 @@
99
gpio-controller;
1010
wakeup-source;
1111
};
12+
13+
/ {
14+
cpus {
15+
power-states {
16+
s2ram: s2ram {
17+
compatible = "zephyr,power-state";
18+
power-state-name = "suspend-to-ram";
19+
};
20+
};
21+
};
22+
};
23+
24+
&cpu0 {
25+
cpu-power-states = <&s2ram>;
26+
};

tests/subsys/pm/power_mgmt/boards/native_posix.overlay

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

77
/ {
8+
cpus {
9+
power-states {
10+
suspend: suspend {
11+
compatible = "zephyr,power-state";
12+
power-state-name = "suspend-to-idle";
13+
};
14+
};
15+
};
16+
817
device_a: device_a {
918
compatible = "test-device-pm";
1019
};
@@ -21,3 +30,7 @@
2130
compatible = "test-device-pm";
2231
};
2332
};
33+
34+
&cpu0 {
35+
cpu-power-states = <&suspend>;
36+
};

tests/subsys/pm/power_mgmt/src/main.c

Lines changed: 41 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -180,8 +180,7 @@ void pm_state_set(enum pm_state state, uint8_t substate_id)
180180
return;
181181
}
182182

183-
184-
/* at this point, notify_pm_state_entry() implemented in
183+
/* at this point, notify_pm_state() implemented in
185184
* this file has been called and set_pm should have been set
186185
*/
187186
zassert_true(set_pm == true,
@@ -245,46 +244,42 @@ const struct pm_state_info *pm_policy_next_state(uint8_t cpu, int32_t ticks)
245244
}
246245

247246
/* implement in application, called by idle thread */
248-
static void notify_pm_state_entry(enum pm_state state)
247+
static void notify_pm_state(uint8_t direction, void *ctx)
249248
{
250249
enum pm_device_state device_power_state;
251-
252-
/* enter suspend */
253-
zassert_true(notify_app_entry == true,
254-
"Notification to enter suspend was not sent to the App");
255-
zassert_true(z_is_idle_thread_object(_current));
256-
zassert_equal(state, PM_STATE_SUSPEND_TO_IDLE);
257-
258-
pm_device_state_get(device_dummy, &device_power_state);
259-
if (testing_device_runtime) {
260-
/* If device runtime is enable, the device should still be
261-
* active
262-
*/
263-
zassert_true(device_power_state == PM_DEVICE_STATE_ACTIVE);
250+
const struct pm_state_info *pm_state = pm_state_next_get(0u);
251+
252+
if (direction & PM_STATE_ENTRY) {
253+
/* enter suspend */
254+
zassert_true(notify_app_entry == true,
255+
"Notification to enter suspend was not sent to the App");
256+
zassert_true(z_is_idle_thread_object(_current));
257+
zassert_equal(pm_state->state, PM_STATE_SUSPEND_TO_IDLE);
258+
259+
pm_device_state_get(device_dummy, &device_power_state);
260+
if (testing_device_runtime) {
261+
/* If device runtime is enable, the device should still be
262+
* active
263+
*/
264+
zassert_true(device_power_state == PM_DEVICE_STATE_ACTIVE);
265+
} else {
266+
/* at this point, devices should not be active */
267+
zassert_false(device_power_state == PM_DEVICE_STATE_ACTIVE);
268+
}
269+
set_pm = true;
270+
notify_app_exit = true;
264271
} else {
265-
/* at this point, devices should not be active */
266-
zassert_false(device_power_state == PM_DEVICE_STATE_ACTIVE);
272+
/* leave suspend */
273+
zassert_true(notify_app_exit == true,
274+
"Notification to leave suspend was not sent to the App");
275+
zassert_true(z_is_idle_thread_object(_current));
276+
zassert_equal(pm_state->state, PM_STATE_SUSPEND_TO_IDLE);
277+
278+
/* at this point, devices are active again*/
279+
pm_device_state_get(device_dummy, &device_power_state);
280+
zassert_equal(device_power_state, PM_DEVICE_STATE_ACTIVE);
281+
leave_idle = true;
267282
}
268-
set_pm = true;
269-
notify_app_exit = true;
270-
}
271-
272-
/* implement in application, called by idle thread */
273-
static void notify_pm_state_exit(enum pm_state state)
274-
{
275-
enum pm_device_state device_power_state;
276-
277-
/* leave suspend */
278-
zassert_true(notify_app_exit == true,
279-
"Notification to leave suspend was not sent to the App");
280-
zassert_true(z_is_idle_thread_object(_current));
281-
zassert_equal(state, PM_STATE_SUSPEND_TO_IDLE);
282-
283-
/* at this point, devices are active again*/
284-
pm_device_state_get(device_dummy, &device_power_state);
285-
zassert_equal(device_power_state, PM_DEVICE_STATE_ACTIVE);
286-
leave_idle = true;
287-
288283
}
289284

290285
/*
@@ -309,10 +304,7 @@ ZTEST(power_management_1cpu, test_power_idle)
309304
zassert_true(idle_entered, "Never entered idle thread");
310305
}
311306

312-
static struct pm_notifier notifier = {
313-
.state_entry = notify_pm_state_entry,
314-
.state_exit = notify_pm_state_exit,
315-
};
307+
PM_NOTIFIER_DEFINE(test, (PM_STATE_ENTRY|PM_STATE_EXIT), notify_pm_state, NULL);
316308

317309
/*
318310
* @brief test power state transition
@@ -321,17 +313,17 @@ static struct pm_notifier notifier = {
321313
* - The system support control of power state ordering between
322314
* subsystems and devices
323315
* - The application can control system power state transitions in idle thread
324-
* through pm_notify_pm_state_entry and pm_notify_pm_state_exit
316+
* through pm_notify_pm_state
325317
*
326-
* @see pm_notify_pm_state_entry(), pm_notify_pm_state_exit()
318+
* @see pm_notify_pm_state()
327319
*
328320
* @ingroup power_tests
329321
*/
330322
ZTEST(power_management_1cpu, test_power_state_trans)
331323
{
332324
int ret;
333325

334-
pm_notifier_register(&notifier);
326+
pm_notifier_register(&PM_NOTIFIER(test), PM_STATE_SUSPEND_TO_IDLE, 0);
335327
enter_low_power = true;
336328

337329
ret = pm_device_runtime_disable(device_dummy);
@@ -344,7 +336,7 @@ ZTEST(power_management_1cpu, test_power_state_trans)
344336
ret = pm_device_runtime_enable(device_dummy);
345337
zassert_true(ret == 0, "Failed to enable device runtime PM");
346338

347-
pm_notifier_unregister(&notifier);
339+
pm_notifier_unregister(&PM_NOTIFIER(test), PM_STATE_SUSPEND_TO_IDLE, 0);
348340
}
349341

350342
/*
@@ -366,7 +358,7 @@ ZTEST(power_management_1cpu, test_power_state_notification)
366358
int ret;
367359
enum pm_device_state device_power_state;
368360

369-
pm_notifier_register(&notifier);
361+
pm_notifier_register(&PM_NOTIFIER(test), PM_STATE_SUSPEND_TO_IDLE, 0);
370362
enter_low_power = true;
371363

372364
ret = api->open(device_dummy);
@@ -385,7 +377,7 @@ ZTEST(power_management_1cpu, test_power_state_notification)
385377
api->close(device_dummy);
386378
pm_device_state_get(device_dummy, &device_power_state);
387379
zassert_equal(device_power_state, PM_DEVICE_STATE_SUSPENDED);
388-
pm_notifier_unregister(&notifier);
380+
pm_notifier_unregister(&PM_NOTIFIER(test), PM_STATE_SUSPEND_TO_IDLE, 0);
389381
testing_device_runtime = false;
390382
}
391383

@@ -446,7 +438,7 @@ ZTEST(power_management_1cpu, test_device_state_lock)
446438

447439
void power_management_1cpu_teardown(void *data)
448440
{
449-
pm_notifier_unregister(&notifier);
441+
pm_notifier_unregister(&PM_NOTIFIER(test), PM_STATE_SUSPEND_TO_IDLE, 0);
450442
}
451443

452444
static void *power_management_1cpu_setup(void)

0 commit comments

Comments
 (0)