mbed-os/hal/mbed_ticker_api.c

138 lines
4.4 KiB
C
Raw Normal View History

/* mbed Microcontroller Library
* Copyright (c) 2015 ARM Limited
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <stddef.h>
#include "hal/ticker_api.h"
#include "platform/mbed_critical.h"
void ticker_set_handler(const ticker_data_t *const data, ticker_event_handler handler) {
data->interface->init();
data->queue->event_handler = handler;
}
void ticker_irq_handler(const ticker_data_t *const data) {
data->interface->clear_interrupt();
/* Go through all the pending TimerEvents */
while (1) {
if (data->queue->head == NULL) {
// There are no more TimerEvents left, so disable matches.
data->interface->disable_interrupt();
return;
}
if ((int)(data->queue->head->timestamp - data->interface->read()) <= 0) {
// This event was in the past:
// point to the following one and execute its handler
ticker_event_t *p = data->queue->head;
data->queue->head = data->queue->head->next;
if (data->queue->event_handler != NULL) {
(*data->queue->event_handler)(p->id); // NOTE: the handler can set new events
}
/* Note: We continue back to examining the head because calling the
* event handler may have altered the chain of pending events. */
} else {
// This event and the following ones in the list are in the future:
// set it as next interrupt and return
data->interface->set_interrupt(data->queue->head->timestamp);
return;
}
}
}
void ticker_insert_event(const ticker_data_t *const data, ticker_event_t *obj, timestamp_t timestamp, uint32_t id) {
/* disable interrupts for the duration of the function */
core_util_critical_section_enter();
// initialise our data
obj->timestamp = timestamp;
obj->id = id;
/* Go through the list until we either reach the end, or find
an element this should come before (which is possibly the
head). */
ticker_event_t *prev = NULL, *p = data->queue->head;
while (p != NULL) {
/* check if we come before p */
if ((int)(timestamp - p->timestamp) < 0) {
break;
}
/* go to the next element */
prev = p;
p = p->next;
}
Fix crash with events in the past ticker_insert_event() can crash on KLXX (and probably other platforms) if an event is inserted with a timestamp before the current real time. The problem is easy to trigger: you just need to set up a Ticker object, and then disable interrupts for slightly longer than the Ticker object's interval. It's generally bad practice to disable interrupts for too long, but there are some cases where it's unavoidable, and anyway it would be better for the core library function not to crash. The case where I had an unavoidably long interrupts-off interval was writing flash with the FTFA. The FTFA hardware prohibits flash reads while an FTFA command is in progress, so interrupts must be disabled for the whole duration of each command to ensure that there are no instruction fetches from flash-resident ISRs in the course of the execution. An FTFA "erase sector" command takes a fairly long time (milliseconds), and I have a fairly high frequency Ticker (1ms). The problem and the fix are pretty straightforward. ticker_insert_event() searches the linked list to figure out where to insert the new event, looking for a spot earlier than any event currently queued. If the event is in the past, it'll usually end up at the head of the list. When the routine sees that the new event belongs at the head of the list, it calls data->interface->set_interrupt() to schedule the interrupt for the event, since it's the new soonest event. The KLXX version of us_ticker_set_interrupt() then looks to see if the event is in the past, which we've stipulated that it is, so rather than actually setting the interrupt, it simply calls the handler directly. The first thing the Ticker interrupt handler does is re-schedule itself, so we re-enter ticker_insert_event() at this point. This is where the problem comes in: we didn't finish updating the linked list before we called set_interrupt() and thus before we recursed back into ticker_insert_event(). We set the head of the list to the new event but we didn't set the new event's 'next' pointer. The fix is simply to finish updating the list before we call set_interrupt(), which we can do by moving the obj->next initialization ahead of the head pointer update.
2017-03-21 19:02:33 +00:00
/* if we're at the end p will be NULL, which is correct */
obj->next = p;
/* if prev is NULL we're at the head */
if (prev == NULL) {
data->queue->head = obj;
data->interface->set_interrupt(timestamp);
} else {
prev->next = obj;
}
core_util_critical_section_exit();
}
void ticker_remove_event(const ticker_data_t *const data, ticker_event_t *obj) {
core_util_critical_section_enter();
// remove this object from the list
if (data->queue->head == obj) {
// first in the list, so just drop me
data->queue->head = obj->next;
if (data->queue->head == NULL) {
data->interface->disable_interrupt();
} else {
data->interface->set_interrupt(data->queue->head->timestamp);
}
} else {
// find the object before me, then drop me
ticker_event_t* p = data->queue->head;
while (p != NULL) {
if (p->next == obj) {
p->next = obj->next;
break;
}
p = p->next;
}
}
core_util_critical_section_exit();
}
timestamp_t ticker_read(const ticker_data_t *const data)
{
return data->interface->read();
}
int ticker_get_next_timestamp(const ticker_data_t *const data, timestamp_t *timestamp)
{
int ret = 0;
/* if head is NULL, there are no pending events */
core_util_critical_section_enter();
if (data->queue->head != NULL) {
*timestamp = data->queue->head->timestamp;
ret = 1;
}
core_util_critical_section_exit();
return ret;
}