mbed library sources. Supersedes mbed-src.

Fork of mbed-dev by mbed official

Committer:
bogdanm
Date:
Thu Oct 01 15:25:22 2015 +0300
Revision:
0:9b334a45a8ff
Initial commit on mbed-dev

Replaces mbed-src (now inactive)

Who changed what in which revision?

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