Skip to content

Commit 20b0271

Browse files
knutel-nordicanangl
authored andcommitted
pm: Fix to use abs time with policy event funcs
Update pm policy event usage after API was changed from using relative time in cycles to absolute time in ticks. Signed-off-by: Knut Eldhuset <[email protected]>
1 parent 9e5ecae commit 20b0271

File tree

5 files changed

+3
-108
lines changed

5 files changed

+3
-108
lines changed

subsys/mpsl/pm/mpsl_pm_utils.c

Lines changed: 2 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
#include <zephyr/pm/policy.h>
1111
#include <zephyr/logging/log.h>
1212

13-
#include <mpsl/mpsl_work.h>
1413
#include <mpsl/mpsl_pm_utils.h>
1514

1615
LOG_MODULE_REGISTER(mpsl_pm_utils, CONFIG_MPSL_LOG_LEVEL);
@@ -23,9 +22,6 @@ LOG_MODULE_REGISTER(mpsl_pm_utils, CONFIG_MPSL_LOG_LEVEL);
2322
#define TIME_TO_REGISTER_EVENT_IN_ZEPHYR_US 1000
2423
#define PM_MAX_LATENCY_HCI_COMMANDS_US 499999
2524

26-
static void m_work_handler(struct k_work *work);
27-
static K_WORK_DELAYABLE_DEFINE(pm_work, m_work_handler);
28-
2925
static uint8_t m_pm_prev_flag_value;
3026
static bool m_pm_event_is_registered;
3127
static uint32_t m_prev_lat_value_us;
@@ -66,38 +62,13 @@ void m_register_event(void)
6662
}
6763
case MPSL_PM_EVENT_STATE_BEFORE_EVENT:
6864
{
69-
/* Note: Considering an overflow could only happen if the system runs many years,
70-
* it needen't be considered here.
71-
*/
72-
int64_t current_time_us = k_uptime_get() * 1000;
73-
uint64_t relative_time_us = params.event_time_abs_us - current_time_us;
74-
uint64_t max_cycles_until_event = k_us_to_cyc_floor64(relative_time_us);
75-
76-
if (max_cycles_until_event > UINT32_MAX) {
77-
/* The event is too far in the future and would
78-
* exceed the 32-bit cycle limit.
79-
*/
80-
uint64_t event_delay_us = params.event_time_abs_us - current_time_us -
81-
TIME_TO_REGISTER_EVENT_IN_ZEPHYR_US;
82-
#ifdef CONFIG_TIMEOUT_64BIT
83-
mpsl_work_schedule(&pm_work, K_USEC(event_delay_us));
84-
#else
85-
if (event_delay_us > UINT32_MAX) {
86-
mpsl_work_schedule(&pm_work, K_USEC(UINT32_MAX));
87-
} else {
88-
mpsl_work_schedule(&pm_work, K_USEC((uint32_t)event_delay_us));
89-
}
90-
#endif
91-
return;
92-
}
93-
9465
/* Event scheduled */
9566
if (m_pm_event_is_registered) {
9667
pm_policy_event_update(&m_evt,
97-
k_us_to_cyc_floor32(params.event_time_abs_us));
68+
k_us_to_ticks_floor64(params.event_time_abs_us));
9869
} else {
9970
pm_policy_event_register(&m_evt,
100-
k_us_to_cyc_floor32(params.event_time_abs_us));
71+
k_us_to_ticks_floor64(params.event_time_abs_us));
10172
m_pm_event_is_registered = true;
10273
}
10374
break;
@@ -136,12 +107,6 @@ void mpsl_pm_utils_work_handler(void)
136107
m_register_latency();
137108
}
138109

139-
static void m_work_handler(struct k_work *work)
140-
{
141-
ARG_UNUSED(work);
142-
mpsl_pm_utils_work_handler();
143-
}
144-
145110
void mpsl_pm_utils_init(void)
146111
{
147112
mpsl_pm_params_t params = {0};

tests/subsys/mpsl/pm/CMakeLists.txt

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,9 @@ project(pm_test)
1313
test_runner_generate(pm_test.c)
1414

1515
# Create mocks for pm module.
16-
cmock_handle(${CMAKE_CURRENT_SOURCE_DIR}/kernel_minimal_mock.h)
1716
cmock_handle(${ZEPHYR_BASE}/include/zephyr/pm/policy.h)
1817
cmock_handle(${ZEPHYR_NRFXLIB_MODULE_DIR}/mpsl/include/mpsl_pm.h)
1918
cmock_handle(${ZEPHYR_NRFXLIB_MODULE_DIR}/mpsl/include/mpsl_pm_config.h)
20-
cmock_handle(${ZEPHYR_NRF_MODULE_DIR}/include/mpsl/mpsl_work.h)
2119

2220
# Add Unit Under Test source files
2321
target_sources(app PRIVATE ${ZEPHYR_NRF_MODULE_DIR}/subsys/mpsl/pm/mpsl_pm_utils.c)
@@ -28,10 +26,6 @@ target_sources(app PRIVATE pm_test.c)
2826
# Include paths
2927
target_include_directories(app PRIVATE src)
3028

31-
# Preinclude file to the UUT to redefine mpsl_work_schedule().
32-
set_property(SOURCE ${ZEPHYR_NRF_MODULE_DIR}/subsys/mpsl/pm/mpsl_pm_utils.c
33-
PROPERTY COMPILE_FLAGS "-include mocks/kernel_minimal_mock.h -include mocks/mpsl_work.h")
34-
3529
# Options that cannot be passed through Kconfig fragments.
3630
target_compile_options(app PRIVATE
3731
-DCONFIG_PM=y

tests/subsys/mpsl/pm/kernel_minimal_mock.h

Lines changed: 0 additions & 25 deletions
This file was deleted.

tests/subsys/mpsl/pm/pm_test.c

Lines changed: 0 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -13,18 +13,11 @@
1313
#include "cmock_policy.h"
1414
#include "cmock_mpsl_pm.h"
1515
#include "cmock_mpsl_pm_config.h"
16-
#include "cmock_mpsl_work.h"
17-
#include "cmock_kernel_minimal_mock.h"
1816

1917
#include <mpsl/mpsl_pm_utils.h>
2018

2119
#define PM_MAX_LATENCY_HCI_COMMANDS_US 499999
2220

23-
#define TIME_TO_REGISTER_EVENT_IN_ZEPHYR_US 1000
24-
25-
/* Mock implementation for mpsl_work_q*/
26-
struct k_work_q mpsl_work_q;
27-
2821
/* The unity_main is not declared in any header file. It is only defined in the generated test
2922
* runner because of ncs' unity configuration. It is therefore declared here to avoid a compiler
3023
* warning.
@@ -36,7 +29,6 @@ typedef enum {
3629
EVENT_FUNC_REGISTER,
3730
EVENT_FUNC_UPDATE,
3831
EVENT_FUNC_UNREGISTER,
39-
EVENT_FUNC_DELAY_SCHEDULING,
4032
} event_func_t;
4133

4234
typedef enum {
@@ -87,26 +79,16 @@ void run_test(test_vector_event_t *p_test_vectors, int num_test_vctr)
8779

8880
switch (v.event_func) {
8981
case EVENT_FUNC_REGISTER:
90-
__cmock_k_uptime_get_ExpectAndReturn(v.curr_time_ms);
9182
__cmock_pm_policy_event_register_Expect(0, v.event_time_us);
9283
__cmock_pm_policy_event_register_IgnoreArg_evt();
9384
break;
9485
case EVENT_FUNC_UPDATE:
95-
__cmock_k_uptime_get_ExpectAndReturn(v.curr_time_ms);
9686
__cmock_pm_policy_event_update_Expect(0, v.event_time_us);
9787
__cmock_pm_policy_event_update_IgnoreArg_evt();
9888
break;
9989
case EVENT_FUNC_UNREGISTER:
10090
__cmock_pm_policy_event_unregister_ExpectAnyArgs();
10191
break;
102-
case EVENT_FUNC_DELAY_SCHEDULING:
103-
__cmock_k_uptime_get_ExpectAndReturn(v.curr_time_ms);
104-
__cmock_K_USEC_ExpectAndReturn(
105-
v.event_time_us, (k_timeout_t){v.event_time_us + 100});
106-
__cmock_mpsl_work_schedule_Expect(
107-
0, (k_timeout_t){v.event_time_us + 100});
108-
__cmock_mpsl_work_schedule_IgnoreArg_dwork();
109-
break;
11092
case EVENT_FUNC_NONE:
11193
break;
11294
}
@@ -226,28 +208,6 @@ void test_register_update_and_deregister_event(void)
226208
run_test(&test_vectors[0], ARRAY_SIZE(test_vectors));
227209
}
228210

229-
void test_event_delayed_work(void)
230-
{
231-
uint64_t event_time_us = (uint64_t)UINT32_MAX + 50000;
232-
/* Make sure time until event will be more than UINT32_MAX cycles away. */
233-
TEST_ASSERT_GREATER_THAN_INT64(UINT32_MAX, k_us_to_cyc_floor64(event_time_us));
234-
235-
test_vector_event_t test_vectors[] = {
236-
/* Event time after 32 bit cycles have wrapped, so schedule retry. */
237-
{true, {event_time_us, MPSL_PM_EVENT_STATE_BEFORE_EVENT, 1},
238-
EVENT_FUNC_DELAY_SCHEDULING, event_time_us - 1000, 0},
239-
/* Time has progressed until cycles will no longer wrap,
240-
* so register latency normally.
241-
*/
242-
{true, {event_time_us, MPSL_PM_EVENT_STATE_BEFORE_EVENT, 2},
243-
EVENT_FUNC_REGISTER, event_time_us, 100},
244-
/* Deregister event. */
245-
{true, {0, MPSL_PM_EVENT_STATE_NO_EVENTS_LEFT, 3},
246-
EVENT_FUNC_UNREGISTER, 0, 0},
247-
};
248-
run_test(&test_vectors[0], ARRAY_SIZE(test_vectors));
249-
}
250-
251211
int main(void)
252212
{
253213
(void)unity_main();

tests/subsys/mpsl/pm/prj.conf

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,4 @@
55
#
66

77
CONFIG_UNITY=y
8+
CONFIG_SYS_CLOCK_TICKS_PER_SEC=1000000

0 commit comments

Comments
 (0)