Skip to content

Commit 23e6d50

Browse files
committed
Add ticker suspend/resume API
Add an API to suspend and resume the ticker.
1 parent 3c25b96 commit 23e6d50

File tree

2 files changed

+60
-1
lines changed

2 files changed

+60
-1
lines changed

hal/mbed_ticker_api.c

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,9 @@ static void initialize(const ticker_data_t *ticker)
3232
if (ticker->queue->initialized) {
3333
return;
3434
}
35+
if (ticker->queue->suspended) {
36+
return;
37+
}
3538

3639
ticker->interface->init();
3740

@@ -70,6 +73,7 @@ static void initialize(const ticker_data_t *ticker)
7073
ticker->queue->max_delta_us = max_delta_us;
7174
ticker->queue->present_time = 0;
7275
ticker->queue->dispatching = false;
76+
ticker->queue->suspended = false;
7377
ticker->queue->initialized = true;
7478

7579
update_present_time(ticker);
@@ -121,6 +125,9 @@ static us_timestamp_t convert_timestamp(us_timestamp_t ref, timestamp_t timestam
121125
static void update_present_time(const ticker_data_t *const ticker)
122126
{
123127
ticker_event_queue_t *queue = ticker->queue;
128+
if (queue->suspended) {
129+
return;
130+
}
124131
uint32_t ticker_time = ticker->interface->read();
125132
if (ticker_time == ticker->queue->tick_last_read) {
126133
// No work to do
@@ -230,7 +237,7 @@ int _ticker_match_interval_passed(timestamp_t prev_tick, timestamp_t cur_tick, t
230237
static void schedule_interrupt(const ticker_data_t *const ticker)
231238
{
232239
ticker_event_queue_t *queue = ticker->queue;
233-
if (ticker->queue->dispatching) {
240+
if (queue->suspended || ticker->queue->dispatching) {
234241
// Don't schedule the next interrupt until dispatching is
235242
// finished. This prevents repeated calls to interface->set_interrupt
236243
return;
@@ -285,6 +292,10 @@ void ticker_irq_handler(const ticker_data_t *const ticker)
285292
core_util_critical_section_enter();
286293

287294
ticker->interface->clear_interrupt();
295+
if (ticker->queue->suspended) {
296+
core_util_critical_section_exit();
297+
return;
298+
}
288299

289300
/* Go through all the pending TimerEvents */
290301
ticker->queue->dispatching = true;
@@ -430,3 +441,29 @@ int ticker_get_next_timestamp(const ticker_data_t *const data, timestamp_t *time
430441

431442
return ret;
432443
}
444+
445+
void ticker_suspend(const ticker_data_t *const ticker)
446+
{
447+
core_util_critical_section_enter();
448+
449+
ticker->queue->suspended = true;
450+
451+
core_util_critical_section_exit();
452+
}
453+
454+
void ticker_resume(const ticker_data_t *const ticker)
455+
{
456+
core_util_critical_section_enter();
457+
458+
ticker->queue->suspended = false;
459+
if (ticker->queue->initialized) {
460+
ticker->queue->tick_last_read = ticker->interface->read();
461+
462+
update_present_time(ticker);
463+
schedule_interrupt(ticker);
464+
} else {
465+
initialize(ticker);
466+
}
467+
468+
core_util_critical_section_exit();
469+
}

hal/ticker_api.h

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@ typedef struct {
8282
us_timestamp_t present_time; /**< Store the timestamp used for present time */
8383
bool initialized; /**< Indicate if the instance is initialized */
8484
bool dispatching; /**< The function ticker_irq_handler is dispatching */
85+
bool suspended; /**< Indicate if the instance is suspended */
8586
uint8_t frequency_shifts; /**< If frequency is a value of 2^n, this is n, otherwise 0 */
8687
} ticker_event_queue_t;
8788

@@ -184,6 +185,27 @@ us_timestamp_t ticker_read_us(const ticker_data_t *const ticker);
184185
*/
185186
int ticker_get_next_timestamp(const ticker_data_t *const ticker, timestamp_t *timestamp);
186187

188+
/** Suspend this ticker
189+
*
190+
* When suspended reads will always return the same time and no
191+
* events will be dispatched. When suspended the common layer
192+
* will only ever call the interface function clear_interrupt()
193+
* and that is only if ticker_irq_handler is called.
194+
*
195+
*
196+
* @param ticker The ticker object.
197+
*/
198+
void ticker_suspend(const ticker_data_t *const ticker);
199+
200+
/** Resume this ticker
201+
*
202+
* When resumed the ticker will ignore any time that has passed
203+
* and continue counting up where it left off.
204+
*
205+
* @param ticker The ticker object.
206+
*/
207+
void ticker_resume(const ticker_data_t *const ticker);
208+
187209
/* Private functions
188210
*
189211
* @cond PRIVATE

0 commit comments

Comments
 (0)