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