mbed library sources

Fork of mbed-src by mbed official

Committer:
mbed_official
Date:
Fri Mar 20 09:30:08 2015 +0000
Revision:
495:01cb89f68337
Parent:
484:b5ae48b573d3
Child:
525:c320967f86b9
Synchronized with git revision d1d900d30c656b1f35f4f5db49e47ea199b0b8d6

Full URL: https://github.com/mbedmicro/mbed/commit/d1d900d30c656b1f35f4f5db49e47ea199b0b8d6/

Who changed what in which revision?

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