This fork captures the mbed lib v125 for ease of integration into older projects.

Fork of mbed-dev by mbed official

Committer:
apluscw
Date:
Fri Jul 20 21:24:42 2018 +0000
Revision:
187:92cbb9eec47b
Parent:
144:ef7eb2e8f9f7
Mbed library with source code from mbed lib v125. Posted to ease integration with some older projects.

Who changed what in which revision?

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