Preliminary main mbed library for nexpaq development

Committer:
nexpaq
Date:
Fri Nov 04 20:54:50 2016 +0000
Revision:
1:d96dbedaebdb
Parent:
0:6c56fb4bc5f0
Removed extra directories for other platforms

Who changed what in which revision?

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