forked
hal/mbed_ticker_api.c@149:156823d33999, 2016-10-28 (annotated)
- Committer:
- <>
- Date:
- Fri Oct 28 11:17:30 2016 +0100
- Revision:
- 149:156823d33999
- Child:
- 160:d5399cc887bb
This updates the lib to the mbed lib v128
NOTE: This release includes a restructuring of the file and directory locations and thus some
include paths in your code may need updating accordingly.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
<> | 149:156823d33999 | 1 | /* mbed Microcontroller Library |
<> | 149:156823d33999 | 2 | * Copyright (c) 2015 ARM Limited |
<> | 149:156823d33999 | 3 | * |
<> | 149:156823d33999 | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
<> | 149:156823d33999 | 5 | * you may not use this file except in compliance with the License. |
<> | 149:156823d33999 | 6 | * You may obtain a copy of the License at |
<> | 149:156823d33999 | 7 | * |
<> | 149:156823d33999 | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
<> | 149:156823d33999 | 9 | * |
<> | 149:156823d33999 | 10 | * Unless required by applicable law or agreed to in writing, software |
<> | 149:156823d33999 | 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
<> | 149:156823d33999 | 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
<> | 149:156823d33999 | 13 | * See the License for the specific language governing permissions and |
<> | 149:156823d33999 | 14 | * limitations under the License. |
<> | 149:156823d33999 | 15 | */ |
<> | 149:156823d33999 | 16 | #include <stddef.h> |
<> | 149:156823d33999 | 17 | #include "hal/ticker_api.h" |
<> | 149:156823d33999 | 18 | #include "platform/critical.h" |
<> | 149:156823d33999 | 19 | |
<> | 149:156823d33999 | 20 | void ticker_set_handler(const ticker_data_t *const data, ticker_event_handler handler) { |
<> | 149:156823d33999 | 21 | data->interface->init(); |
<> | 149:156823d33999 | 22 | |
<> | 149:156823d33999 | 23 | data->queue->event_handler = handler; |
<> | 149:156823d33999 | 24 | } |
<> | 149:156823d33999 | 25 | |
<> | 149:156823d33999 | 26 | void ticker_irq_handler(const ticker_data_t *const data) { |
<> | 149:156823d33999 | 27 | data->interface->clear_interrupt(); |
<> | 149:156823d33999 | 28 | |
<> | 149:156823d33999 | 29 | /* Go through all the pending TimerEvents */ |
<> | 149:156823d33999 | 30 | while (1) { |
<> | 149:156823d33999 | 31 | if (data->queue->head == NULL) { |
<> | 149:156823d33999 | 32 | // There are no more TimerEvents left, so disable matches. |
<> | 149:156823d33999 | 33 | data->interface->disable_interrupt(); |
<> | 149:156823d33999 | 34 | return; |
<> | 149:156823d33999 | 35 | } |
<> | 149:156823d33999 | 36 | |
<> | 149:156823d33999 | 37 | if ((int)(data->queue->head->timestamp - data->interface->read()) <= 0) { |
<> | 149:156823d33999 | 38 | // This event was in the past: |
<> | 149:156823d33999 | 39 | // point to the following one and execute its handler |
<> | 149:156823d33999 | 40 | ticker_event_t *p = data->queue->head; |
<> | 149:156823d33999 | 41 | data->queue->head = data->queue->head->next; |
<> | 149:156823d33999 | 42 | if (data->queue->event_handler != NULL) { |
<> | 149:156823d33999 | 43 | (*data->queue->event_handler)(p->id); // NOTE: the handler can set new events |
<> | 149:156823d33999 | 44 | } |
<> | 149:156823d33999 | 45 | /* Note: We continue back to examining the head because calling the |
<> | 149:156823d33999 | 46 | * event handler may have altered the chain of pending events. */ |
<> | 149:156823d33999 | 47 | } else { |
<> | 149:156823d33999 | 48 | // This event and the following ones in the list are in the future: |
<> | 149:156823d33999 | 49 | // set it as next interrupt and return |
<> | 149:156823d33999 | 50 | data->interface->set_interrupt(data->queue->head->timestamp); |
<> | 149:156823d33999 | 51 | return; |
<> | 149:156823d33999 | 52 | } |
<> | 149:156823d33999 | 53 | } |
<> | 149:156823d33999 | 54 | } |
<> | 149:156823d33999 | 55 | |
<> | 149:156823d33999 | 56 | void ticker_insert_event(const ticker_data_t *const data, ticker_event_t *obj, timestamp_t timestamp, uint32_t id) { |
<> | 149:156823d33999 | 57 | /* disable interrupts for the duration of the function */ |
<> | 149:156823d33999 | 58 | core_util_critical_section_enter(); |
<> | 149:156823d33999 | 59 | |
<> | 149:156823d33999 | 60 | // initialise our data |
<> | 149:156823d33999 | 61 | obj->timestamp = timestamp; |
<> | 149:156823d33999 | 62 | obj->id = id; |
<> | 149:156823d33999 | 63 | |
<> | 149:156823d33999 | 64 | /* Go through the list until we either reach the end, or find |
<> | 149:156823d33999 | 65 | an element this should come before (which is possibly the |
<> | 149:156823d33999 | 66 | head). */ |
<> | 149:156823d33999 | 67 | ticker_event_t *prev = NULL, *p = data->queue->head; |
<> | 149:156823d33999 | 68 | while (p != NULL) { |
<> | 149:156823d33999 | 69 | /* check if we come before p */ |
<> | 149:156823d33999 | 70 | if ((int)(timestamp - p->timestamp) < 0) { |
<> | 149:156823d33999 | 71 | break; |
<> | 149:156823d33999 | 72 | } |
<> | 149:156823d33999 | 73 | /* go to the next element */ |
<> | 149:156823d33999 | 74 | prev = p; |
<> | 149:156823d33999 | 75 | p = p->next; |
<> | 149:156823d33999 | 76 | } |
<> | 149:156823d33999 | 77 | /* if prev is NULL we're at the head */ |
<> | 149:156823d33999 | 78 | if (prev == NULL) { |
<> | 149:156823d33999 | 79 | data->queue->head = obj; |
<> | 149:156823d33999 | 80 | data->interface->set_interrupt(timestamp); |
<> | 149:156823d33999 | 81 | } else { |
<> | 149:156823d33999 | 82 | prev->next = obj; |
<> | 149:156823d33999 | 83 | } |
<> | 149:156823d33999 | 84 | /* if we're at the end p will be NULL, which is correct */ |
<> | 149:156823d33999 | 85 | obj->next = p; |
<> | 149:156823d33999 | 86 | |
<> | 149:156823d33999 | 87 | core_util_critical_section_exit(); |
<> | 149:156823d33999 | 88 | } |
<> | 149:156823d33999 | 89 | |
<> | 149:156823d33999 | 90 | void ticker_remove_event(const ticker_data_t *const data, ticker_event_t *obj) { |
<> | 149:156823d33999 | 91 | core_util_critical_section_enter(); |
<> | 149:156823d33999 | 92 | |
<> | 149:156823d33999 | 93 | // remove this object from the list |
<> | 149:156823d33999 | 94 | if (data->queue->head == obj) { |
<> | 149:156823d33999 | 95 | // first in the list, so just drop me |
<> | 149:156823d33999 | 96 | data->queue->head = obj->next; |
<> | 149:156823d33999 | 97 | if (data->queue->head == NULL) { |
<> | 149:156823d33999 | 98 | data->interface->disable_interrupt(); |
<> | 149:156823d33999 | 99 | } else { |
<> | 149:156823d33999 | 100 | data->interface->set_interrupt(data->queue->head->timestamp); |
<> | 149:156823d33999 | 101 | } |
<> | 149:156823d33999 | 102 | } else { |
<> | 149:156823d33999 | 103 | // find the object before me, then drop me |
<> | 149:156823d33999 | 104 | ticker_event_t* p = data->queue->head; |
<> | 149:156823d33999 | 105 | while (p != NULL) { |
<> | 149:156823d33999 | 106 | if (p->next == obj) { |
<> | 149:156823d33999 | 107 | p->next = obj->next; |
<> | 149:156823d33999 | 108 | break; |
<> | 149:156823d33999 | 109 | } |
<> | 149:156823d33999 | 110 | p = p->next; |
<> | 149:156823d33999 | 111 | } |
<> | 149:156823d33999 | 112 | } |
<> | 149:156823d33999 | 113 | |
<> | 149:156823d33999 | 114 | core_util_critical_section_exit(); |
<> | 149:156823d33999 | 115 | } |
<> | 149:156823d33999 | 116 | |
<> | 149:156823d33999 | 117 | timestamp_t ticker_read(const ticker_data_t *const data) |
<> | 149:156823d33999 | 118 | { |
<> | 149:156823d33999 | 119 | return data->interface->read(); |
<> | 149:156823d33999 | 120 | } |
<> | 149:156823d33999 | 121 | |
<> | 149:156823d33999 | 122 | int ticker_get_next_timestamp(const ticker_data_t *const data, timestamp_t *timestamp) |
<> | 149:156823d33999 | 123 | { |
<> | 149:156823d33999 | 124 | int ret = 0; |
<> | 149:156823d33999 | 125 | |
<> | 149:156823d33999 | 126 | /* if head is NULL, there are no pending events */ |
<> | 149:156823d33999 | 127 | core_util_critical_section_enter(); |
<> | 149:156823d33999 | 128 | if (data->queue->head != NULL) { |
<> | 149:156823d33999 | 129 | *timestamp = data->queue->head->timestamp; |
<> | 149:156823d33999 | 130 | ret = 1; |
<> | 149:156823d33999 | 131 | } |
<> | 149:156823d33999 | 132 | core_util_critical_section_exit(); |
<> | 149:156823d33999 | 133 | |
<> | 149:156823d33999 | 134 | return ret; |
<> | 149:156823d33999 | 135 | } |