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