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