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