mbed library sources. Supersedes mbed-src.

Fork of mbed-dev by Umar Naeem

Committer:
ranaumarnaeem
Date:
Tue May 23 12:54:50 2017 +0000
Revision:
165:2dd56e6daeec
Parent:
162:e13f6fdb2ac4
jhjg

Who changed what in which revision?

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