diff --git a/subsys/testsuite/busy_sim/busy_sim.c b/subsys/testsuite/busy_sim/busy_sim.c index 11f94bcc37d..a63b21d13fa 100644 --- a/subsys/testsuite/busy_sim/busy_sim.c +++ b/subsys/testsuite/busy_sim/busy_sim.c @@ -21,6 +21,7 @@ struct busy_sim_data { struct k_work work; struct ring_buf rnd_rbuf; uint8_t rnd_buf[BUFFER_SIZE]; + busy_sim_cb_t cb; }; struct busy_sim_config { @@ -110,6 +111,10 @@ static void counter_alarm_callback(const struct device *dev, } /* Busy loop */ + if (data->cb) { + data->cb(); + } + k_busy_wait(get_timeout(false) / data->us_tick); if (config->pin_spec.port) { @@ -123,12 +128,13 @@ static void counter_alarm_callback(const struct device *dev, } void busy_sim_start(uint32_t active_avg, uint32_t active_delta, - uint32_t idle_avg, uint32_t idle_delta) + uint32_t idle_avg, uint32_t idle_delta, busy_sim_cb_t cb) { int err; const struct busy_sim_config *config = get_dev_config(busy_sim_dev); struct busy_sim_data *data = get_dev_data(busy_sim_dev); + data->cb = cb; data->active_avg = active_avg; data->active_delta = active_delta; data->idle_avg = idle_avg; diff --git a/subsys/testsuite/include/busy_sim.h b/subsys/testsuite/include/busy_sim.h index 3226a2b1712..a2e77b5a2fd 100644 --- a/subsys/testsuite/include/busy_sim.h +++ b/subsys/testsuite/include/busy_sim.h @@ -6,6 +6,8 @@ #ifndef BUSY_SIM_H__ #define BUSY_SIM_H__ +typedef void (*busy_sim_cb_t)(void); + /** * @brief Start busy simulator. * @@ -16,12 +18,19 @@ * values and keep them in a ring buffer. * * @param active_avg Avarage time of busy looping in the counter callback (in microseconds). + * * @param active_delta Specifies deviation from avarage time of busy looping (in microseconds). + * * @param idle_avg Avarage time of counter alarm timeout (in microseconds). + * * @param idle_delta Specifies deviation from avarage time of counter alarm (in microseconds). + * + * @param cb Callback called from the context of the busy simulator timeout. If ZLI interrupt + * is used for busy simulator counter then kernel API cannot be used from that callback. + * Callback is called before busy waiting. */ void busy_sim_start(uint32_t active_avg, uint32_t active_delta, - uint32_t idle_avg, uint32_t idle_delta); + uint32_t idle_avg, uint32_t idle_delta, busy_sim_cb_t cb); /** @brief Stop busy simulator. */ void busy_sim_stop(void); diff --git a/tests/ztest/busy_sim/src/main.c b/tests/ztest/busy_sim/src/main.c index 2ecbc345982..7a09a4a6076 100644 --- a/tests/ztest/busy_sim/src/main.c +++ b/tests/ztest/busy_sim/src/main.c @@ -21,7 +21,7 @@ static void test_busy_sim(void) /* Start busy simulator and check that k_busy_wait last longer */ t = k_uptime_get_32(); - busy_sim_start(500, 200, 1000, 400); + busy_sim_start(500, 200, 1000, 400, NULL); k_busy_wait(1000 * ms); t = k_uptime_get_32() - t; busy_ms = (3 * ms) / 2;