Maxim mbed development library

Dependents:   sensomed

Committer:
switches
Date:
Tue Nov 08 18:27:11 2016 +0000
Revision:
0:0e018d759a2a
Initial commit

Who changed what in which revision?

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