mbed library sources
common/us_ticker_api.c@0:0a673c671a56, 2016-07-27 (annotated)
- Committer:
- ebrus
- Date:
- Wed Jul 27 18:35:32 2016 +0000
- Revision:
- 0:0a673c671a56
4
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
ebrus | 0:0a673c671a56 | 1 | /* mbed Microcontroller Library |
ebrus | 0:0a673c671a56 | 2 | * Copyright (c) 2006-2013 ARM Limited |
ebrus | 0:0a673c671a56 | 3 | * |
ebrus | 0:0a673c671a56 | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
ebrus | 0:0a673c671a56 | 5 | * you may not use this file except in compliance with the License. |
ebrus | 0:0a673c671a56 | 6 | * You may obtain a copy of the License at |
ebrus | 0:0a673c671a56 | 7 | * |
ebrus | 0:0a673c671a56 | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
ebrus | 0:0a673c671a56 | 9 | * |
ebrus | 0:0a673c671a56 | 10 | * Unless required by applicable law or agreed to in writing, software |
ebrus | 0:0a673c671a56 | 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
ebrus | 0:0a673c671a56 | 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
ebrus | 0:0a673c671a56 | 13 | * See the License for the specific language governing permissions and |
ebrus | 0:0a673c671a56 | 14 | * limitations under the License. |
ebrus | 0:0a673c671a56 | 15 | */ |
ebrus | 0:0a673c671a56 | 16 | #include <stddef.h> |
ebrus | 0:0a673c671a56 | 17 | #include "us_ticker_api.h" |
ebrus | 0:0a673c671a56 | 18 | #include "cmsis.h" |
ebrus | 0:0a673c671a56 | 19 | |
ebrus | 0:0a673c671a56 | 20 | static ticker_event_handler event_handler; |
ebrus | 0:0a673c671a56 | 21 | static ticker_event_t *head = NULL; |
ebrus | 0:0a673c671a56 | 22 | |
ebrus | 0:0a673c671a56 | 23 | void us_ticker_set_handler(ticker_event_handler handler) { |
ebrus | 0:0a673c671a56 | 24 | us_ticker_init(); |
ebrus | 0:0a673c671a56 | 25 | |
ebrus | 0:0a673c671a56 | 26 | event_handler = handler; |
ebrus | 0:0a673c671a56 | 27 | } |
ebrus | 0:0a673c671a56 | 28 | |
ebrus | 0:0a673c671a56 | 29 | void us_ticker_irq_handler(void) { |
ebrus | 0:0a673c671a56 | 30 | us_ticker_clear_interrupt(); |
ebrus | 0:0a673c671a56 | 31 | |
ebrus | 0:0a673c671a56 | 32 | /* Go through all the pending TimerEvents */ |
ebrus | 0:0a673c671a56 | 33 | while (1) { |
ebrus | 0:0a673c671a56 | 34 | if (head == NULL) { |
ebrus | 0:0a673c671a56 | 35 | // There are no more TimerEvents left, so disable matches. |
ebrus | 0:0a673c671a56 | 36 | us_ticker_disable_interrupt(); |
ebrus | 0:0a673c671a56 | 37 | return; |
ebrus | 0:0a673c671a56 | 38 | } |
ebrus | 0:0a673c671a56 | 39 | |
ebrus | 0:0a673c671a56 | 40 | if ((int)(head->timestamp - us_ticker_read()) <= 0) { |
ebrus | 0:0a673c671a56 | 41 | // This event was in the past: |
ebrus | 0:0a673c671a56 | 42 | // point to the following one and execute its handler |
ebrus | 0:0a673c671a56 | 43 | ticker_event_t *p = head; |
ebrus | 0:0a673c671a56 | 44 | head = head->next; |
ebrus | 0:0a673c671a56 | 45 | if (event_handler != NULL) { |
ebrus | 0:0a673c671a56 | 46 | event_handler(p->id); // NOTE: the handler can set new events |
ebrus | 0:0a673c671a56 | 47 | } |
ebrus | 0:0a673c671a56 | 48 | /* Note: We continue back to examining the head because calling the |
ebrus | 0:0a673c671a56 | 49 | * event handler may have altered the chain of pending events. */ |
ebrus | 0:0a673c671a56 | 50 | } else { |
ebrus | 0:0a673c671a56 | 51 | // This event and the following ones in the list are in the future: |
ebrus | 0:0a673c671a56 | 52 | // set it as next interrupt and return |
ebrus | 0:0a673c671a56 | 53 | us_ticker_set_interrupt(head->timestamp); |
ebrus | 0:0a673c671a56 | 54 | return; |
ebrus | 0:0a673c671a56 | 55 | } |
ebrus | 0:0a673c671a56 | 56 | } |
ebrus | 0:0a673c671a56 | 57 | } |
ebrus | 0:0a673c671a56 | 58 | |
ebrus | 0:0a673c671a56 | 59 | void us_ticker_insert_event(ticker_event_t *obj, timestamp_t timestamp, uint32_t id) { |
ebrus | 0:0a673c671a56 | 60 | /* disable interrupts for the duration of the function */ |
ebrus | 0:0a673c671a56 | 61 | __disable_irq(); |
ebrus | 0:0a673c671a56 | 62 | |
ebrus | 0:0a673c671a56 | 63 | // initialise our data |
ebrus | 0:0a673c671a56 | 64 | obj->timestamp = timestamp; |
ebrus | 0:0a673c671a56 | 65 | obj->id = id; |
ebrus | 0:0a673c671a56 | 66 | |
ebrus | 0:0a673c671a56 | 67 | /* Go through the list until we either reach the end, or find |
ebrus | 0:0a673c671a56 | 68 | an element this should come before (which is possibly the |
ebrus | 0:0a673c671a56 | 69 | head). */ |
ebrus | 0:0a673c671a56 | 70 | ticker_event_t *prev = NULL, *p = head; |
ebrus | 0:0a673c671a56 | 71 | while (p != NULL) { |
ebrus | 0:0a673c671a56 | 72 | /* check if we come before p */ |
ebrus | 0:0a673c671a56 | 73 | if ((int)(timestamp - p->timestamp) <= 0) { |
ebrus | 0:0a673c671a56 | 74 | break; |
ebrus | 0:0a673c671a56 | 75 | } |
ebrus | 0:0a673c671a56 | 76 | /* go to the next element */ |
ebrus | 0:0a673c671a56 | 77 | prev = p; |
ebrus | 0:0a673c671a56 | 78 | p = p->next; |
ebrus | 0:0a673c671a56 | 79 | } |
ebrus | 0:0a673c671a56 | 80 | /* if prev is NULL we're at the head */ |
ebrus | 0:0a673c671a56 | 81 | if (prev == NULL) { |
ebrus | 0:0a673c671a56 | 82 | head = obj; |
ebrus | 0:0a673c671a56 | 83 | us_ticker_set_interrupt(timestamp); |
ebrus | 0:0a673c671a56 | 84 | } else { |
ebrus | 0:0a673c671a56 | 85 | prev->next = obj; |
ebrus | 0:0a673c671a56 | 86 | } |
ebrus | 0:0a673c671a56 | 87 | /* if we're at the end p will be NULL, which is correct */ |
ebrus | 0:0a673c671a56 | 88 | obj->next = p; |
ebrus | 0:0a673c671a56 | 89 | |
ebrus | 0:0a673c671a56 | 90 | __enable_irq(); |
ebrus | 0:0a673c671a56 | 91 | } |
ebrus | 0:0a673c671a56 | 92 | |
ebrus | 0:0a673c671a56 | 93 | void us_ticker_remove_event(ticker_event_t *obj) { |
ebrus | 0:0a673c671a56 | 94 | __disable_irq(); |
ebrus | 0:0a673c671a56 | 95 | |
ebrus | 0:0a673c671a56 | 96 | // remove this object from the list |
ebrus | 0:0a673c671a56 | 97 | if (head == obj) { |
ebrus | 0:0a673c671a56 | 98 | // first in the list, so just drop me |
ebrus | 0:0a673c671a56 | 99 | head = obj->next; |
ebrus | 0:0a673c671a56 | 100 | if (head == NULL) { |
ebrus | 0:0a673c671a56 | 101 | us_ticker_disable_interrupt(); |
ebrus | 0:0a673c671a56 | 102 | } else { |
ebrus | 0:0a673c671a56 | 103 | us_ticker_set_interrupt(head->timestamp); |
ebrus | 0:0a673c671a56 | 104 | } |
ebrus | 0:0a673c671a56 | 105 | } else { |
ebrus | 0:0a673c671a56 | 106 | // find the object before me, then drop me |
ebrus | 0:0a673c671a56 | 107 | ticker_event_t* p = head; |
ebrus | 0:0a673c671a56 | 108 | while (p != NULL) { |
ebrus | 0:0a673c671a56 | 109 | if (p->next == obj) { |
ebrus | 0:0a673c671a56 | 110 | p->next = obj->next; |
ebrus | 0:0a673c671a56 | 111 | break; |
ebrus | 0:0a673c671a56 | 112 | } |
ebrus | 0:0a673c671a56 | 113 | p = p->next; |
ebrus | 0:0a673c671a56 | 114 | } |
ebrus | 0:0a673c671a56 | 115 | } |
ebrus | 0:0a673c671a56 | 116 | |
ebrus | 0:0a673c671a56 | 117 | __enable_irq(); |
ebrus | 0:0a673c671a56 | 118 | } |