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