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