Skip to content

Commit ab09a17

Browse files
committed
ticker api: Schedule immediately event in the past.
1 parent aeffd73 commit ab09a17

File tree

2 files changed

+18
-17
lines changed

2 files changed

+18
-17
lines changed

TESTS/mbed_hal/ticker/main.cpp

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -905,9 +905,9 @@ static void test_insert_event_us_in_overflow_range() {
905905
* When an event is inserted with ticker_insert_event_us and a timestamp less
906906
* than timestamp value in the ticker interface.
907907
* Then
908-
* - The event should not be in the queue
909-
* - The interrupt timestamp should be set to
910-
* interface_stub.timestamp + TIMESTAMP_MAX_DELTA.
908+
* - The event should be in the queue
909+
* - The interrupt timestamp should be set to interface_stub.timestamp so it
910+
* is scheduled immediately.
911911
*/
912912
static void test_insert_event_us_underflow() {
913913
ticker_set_handler(&ticker_stub, NULL);
@@ -922,15 +922,16 @@ static void test_insert_event_us_underflow() {
922922
const uint32_t expected_id = 0xDEADDEAF;
923923

924924
ticker_insert_event_us(
925-
&ticker_stub,
925+
&ticker_stub,
926926
&event, expected_timestamp, expected_id
927927
);
928928

929-
TEST_ASSERT_EQUAL_PTR(NULL, queue_stub.head);
929+
TEST_ASSERT_EQUAL_PTR(&event, queue_stub.head);
930930
TEST_ASSERT_EQUAL_UINT32(
931-
interface_stub.timestamp + TIMESTAMP_MAX_DELTA,
931+
interface_stub.timestamp,
932932
interface_stub.interrupt_timestamp
933933
);
934+
TEST_ASSERT_EQUAL(1, interface_stub.set_interrupt_call);
934935

935936
TEST_ASSERT_EQUAL(0, interface_stub.disable_interrupt_call);
936937
}

hal/mbed_ticker_api.c

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -109,16 +109,22 @@ static void update_present_time(const ticker_data_t *const ticker)
109109
static void schedule_interrupt(const ticker_data_t *const ticker)
110110
{
111111
update_present_time(ticker);
112-
uint32_t duration = MBED_TICKER_INTERRUPT_TIMESTAMP_MAX_DELTA;
112+
uint32_t relative_timeout = MBED_TICKER_INTERRUPT_TIMESTAMP_MAX_DELTA;
113113

114114
if (ticker->queue->head) {
115-
us_timestamp_t event_interval = (ticker->queue->head->timestamp - ticker->queue->present_time);
116-
if (event_interval < MBED_TICKER_INTERRUPT_TIMESTAMP_MAX_DELTA) {
117-
duration = event_interval;
115+
us_timestamp_t present = ticker->queue->present_time;
116+
us_timestamp_t next_event_timestamp = ticker->queue->head->timestamp;
117+
118+
// if the event at the head of the queue is in the past then schedule
119+
// it immediately.
120+
if (next_event_timestamp < present) {
121+
relative_timeout = 0;
122+
} else if ((next_event_timestamp - present) < MBED_TICKER_INTERRUPT_TIMESTAMP_MAX_DELTA) {
123+
relative_timeout = next_event_timestamp - present;
118124
}
119125
}
120126

121-
ticker->interface->set_interrupt(ticker->queue->present_time + duration);
127+
ticker->interface->set_interrupt(ticker->queue->present_time + relative_timeout);
122128
}
123129

124130
void ticker_set_handler(const ticker_data_t *const ticker, ticker_event_handler handler)
@@ -184,12 +190,6 @@ void ticker_insert_event_us(const ticker_data_t *const ticker, ticker_event_t *o
184190
// update the current timestamp
185191
update_present_time(ticker);
186192

187-
// filter out timestamp in the past
188-
if (timestamp < ticker->queue->present_time) {
189-
schedule_interrupt(ticker);
190-
return;
191-
}
192-
193193
// initialise our data
194194
obj->timestamp = timestamp;
195195
obj->id = id;

0 commit comments

Comments
 (0)