Example #1
0
void ticker_irq_handler(const ticker_data_t *const ticker)
{
    ticker->interface->clear_interrupt();

    /* Go through all the pending TimerEvents */
    while (1) {
        if (ticker->queue->head == NULL) {
            break;
        }

        // update the current timestamp used by the queue 
        update_present_time(ticker);

        if (ticker->queue->head->timestamp <= ticker->queue->present_time) { 
            // This event was in the past:
            //      point to the following one and execute its handler
            ticker_event_t *p = ticker->queue->head;
            ticker->queue->head = ticker->queue->head->next;
            if (ticker->queue->event_handler != NULL) {
                (*ticker->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 {
            break;
        } 
    }

    schedule_interrupt(ticker);
}
Example #2
0
/*
 * Initialize a ticker instance.  
 */
static void initialize(const ticker_data_t *ticker)
{
    // return if the queue has already been initialized, in that case the 
    // interface used by the queue is already initialized.
    if (ticker->queue->initialized) { 
        return;
    }

    ticker->interface->init();

    const ticker_info_t *info = ticker->interface->get_info();
    uint32_t frequency = info->frequency;
    if (info->frequency == 0) {
        MBED_ASSERT(0);
        frequency = 1000000;
    }

    uint8_t frequency_shifts = 0;
    for (uint8_t i = 31; i > 0; --i) {
        if ((1 << i) == frequency) {
            frequency_shifts = i;
            break;
        }
    }

    uint32_t bits = info->bits;
    if ((info->bits > 32) || (info->bits < 4)) {
        MBED_ASSERT(0);
        bits = 32;
    }
    uint32_t max_delta = 0x7 << (bits - 4); // 7/16th
    uint64_t max_delta_us =
            ((uint64_t)max_delta * 1000000 + frequency - 1) / frequency;

    ticker->queue->event_handler = NULL;
    ticker->queue->head = NULL;
    ticker->queue->tick_last_read = ticker->interface->read();
    ticker->queue->tick_remainder = 0;
    ticker->queue->frequency = frequency;
    ticker->queue->frequency_shifts = frequency_shifts;
    ticker->queue->bitmask = ((uint64_t)1 << bits) - 1;
    ticker->queue->max_delta = max_delta;
    ticker->queue->max_delta_us = max_delta_us;
    ticker->queue->present_time = 0;
    ticker->queue->initialized = true;
    
    update_present_time(ticker);
    schedule_interrupt(ticker);
}
Example #3
0
/*
 * Initialize a ticker instance.  
 */
static void initialize(const ticker_data_t *ticker)
{
    // return if the queue has already been initialized, in that case the 
    // interface used by the queue is already initialized.
    if (ticker->queue->initialized) { 
        return;
    }

    ticker->interface->init();
    
    ticker->queue->event_handler = NULL;
    ticker->queue->head = NULL;
    ticker->queue->present_time = 0;
    ticker->queue->initialized = true;
    
    update_present_time(ticker);
    schedule_interrupt(ticker);
}
Example #4
0
void ticker_insert_event(const ticker_data_t *const ticker, ticker_event_t *obj, timestamp_t timestamp, uint32_t id)
{
    core_util_critical_section_enter();

    // update the current timestamp
    update_present_time(ticker);
    us_timestamp_t absolute_timestamp = convert_timestamp(
        ticker->queue->present_time, 
        timestamp
    );
    core_util_critical_section_exit();

    // defer to ticker_insert_event_us
    ticker_insert_event_us(
        ticker, 
        obj, absolute_timestamp, id
    );
}
Example #5
0
/**
 * Compute the time when the interrupt has to be triggered and schedule it.  
 * 
 * If there is no event in the queue or the next event to execute is in more 
 * than MBED_TICKER_INTERRUPT_TIMESTAMP_MAX_DELTA us from now then the ticker 
 * irq will be scheduled in MBED_TICKER_INTERRUPT_TIMESTAMP_MAX_DELTA us.
 * Otherwise the irq will be scheduled to happen when the running counter reach 
 * the timestamp of the first event in the queue.
 * 
 * @note If there is no event in the queue then the interrupt is scheduled to 
 * in MBED_TICKER_INTERRUPT_TIMESTAMP_MAX_DELTA. This is necessary to keep track 
 * of the timer overflow.
 */
static void schedule_interrupt(const ticker_data_t *const ticker)
{
    update_present_time(ticker);
    uint32_t relative_timeout = MBED_TICKER_INTERRUPT_TIMESTAMP_MAX_DELTA;

    if (ticker->queue->head) {
        us_timestamp_t present = ticker->queue->present_time;
        us_timestamp_t next_event_timestamp = ticker->queue->head->timestamp;

        // if the event at the head of the queue is in the past then schedule
        // it immediately.
        if (next_event_timestamp < present) {
            relative_timeout = 0;
        } else if ((next_event_timestamp - present) < MBED_TICKER_INTERRUPT_TIMESTAMP_MAX_DELTA) {
            relative_timeout = next_event_timestamp - present;
        }
    } 

    ticker->interface->set_interrupt(ticker->queue->present_time + relative_timeout);
}
Example #6
0
void ticker_insert_event_us(const ticker_data_t *const ticker, ticker_event_t *obj, us_timestamp_t timestamp, uint32_t id)
{
    core_util_critical_section_enter();

    // update the current timestamp
    update_present_time(ticker);

    // 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 = ticker->queue->head;
    while (p != NULL) {
        /* check if we come before p */
        if (timestamp < p->timestamp) {
            break;
        }
        /* go to the next element */
        prev = p;
        p = p->next;
    }
    
    /* 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) {
        ticker->queue->head = obj;
    } else {
        prev->next = obj;
    }

    schedule_interrupt(ticker);

    core_util_critical_section_exit();
}
Example #7
0
us_timestamp_t ticker_read_us(const ticker_data_t *const ticker)
{
    update_present_time(ticker);
    return ticker->queue->present_time;
}