Includes library modifications to allow access to AIN_4 (AIN_0 / 5)

Committer:
bryantaylor
Date:
Tue Sep 20 21:26:12 2016 +0000
Revision:
0:eafc3fd41f75
hackathon

Who changed what in which revision?

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