mbed library sources modified for open wear

Dependents:   openwear-lifelogger-example

Fork of mbed-src by mbed official

Committer:
emilmont
Date:
Mon Mar 18 10:36:46 2013 +0000
Revision:
6:6dfdb79ccc45
Parent:
4:c4bfb462ca53
Fix GCC build including define of ARMCC intrinsic __disable_irq

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();
emilmont 4:c4bfb462ca53 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();
emilmont 4:c4bfb462ca53 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 }
emilmont 4:c4bfb462ca53 39
emilmont 4:c4bfb462ca53 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 }
emilmont 4:c4bfb462ca53 48 } else {
emilmont 4:c4bfb462ca53 49 // This event and the following ones in the list are in the future:
emilmont 4:c4bfb462ca53 50 // set it as next interrupt and return
emilmont 4:c4bfb462ca53 51 us_ticker_set_interrupt(head->timestamp);
emilmont 4:c4bfb462ca53 52 return;
emilmont 4:c4bfb462ca53 53 }
emilmont 4:c4bfb462ca53 54 }
emilmont 4:c4bfb462ca53 55 }
emilmont 4:c4bfb462ca53 56
emilmont 4:c4bfb462ca53 57 void us_ticker_insert_event(ticker_event_t *obj, unsigned int timestamp, uint32_t id) {
emilmont 4:c4bfb462ca53 58 /* disable interrupts for the duration of the function */
emilmont 4:c4bfb462ca53 59 __disable_irq();
emilmont 4:c4bfb462ca53 60
emilmont 4:c4bfb462ca53 61 // initialise our data
emilmont 4:c4bfb462ca53 62 obj->timestamp = timestamp;
emilmont 4:c4bfb462ca53 63 obj->id = id;
emilmont 4:c4bfb462ca53 64
emilmont 4:c4bfb462ca53 65 /* Go through the list until we either reach the end, or find
emilmont 4:c4bfb462ca53 66 an element this should come before (which is possibly the
emilmont 4:c4bfb462ca53 67 head). */
emilmont 4:c4bfb462ca53 68 ticker_event_t *prev = NULL, *p = head;
emilmont 4:c4bfb462ca53 69 while (p != NULL) {
emilmont 4:c4bfb462ca53 70 /* check if we come before p */
emilmont 4:c4bfb462ca53 71 if ((int)(timestamp - p->timestamp) <= 0) {
emilmont 4:c4bfb462ca53 72 break;
emilmont 4:c4bfb462ca53 73 }
emilmont 4:c4bfb462ca53 74 /* go to the next element */
emilmont 4:c4bfb462ca53 75 prev = p;
emilmont 4:c4bfb462ca53 76 p = p->next;
emilmont 4:c4bfb462ca53 77 }
emilmont 4:c4bfb462ca53 78 /* if prev is NULL we're at the head */
emilmont 4:c4bfb462ca53 79 if (prev == NULL) {
emilmont 4:c4bfb462ca53 80 head = obj;
emilmont 4:c4bfb462ca53 81 us_ticker_set_interrupt(timestamp);
emilmont 4:c4bfb462ca53 82 } else {
emilmont 4:c4bfb462ca53 83 prev->next = obj;
emilmont 4:c4bfb462ca53 84 }
emilmont 4:c4bfb462ca53 85 /* if we're at the end p will be NULL, which is correct */
emilmont 4:c4bfb462ca53 86 obj->next = p;
emilmont 4:c4bfb462ca53 87
emilmont 4:c4bfb462ca53 88 __enable_irq();
emilmont 4:c4bfb462ca53 89 }
emilmont 4:c4bfb462ca53 90
emilmont 4:c4bfb462ca53 91 void us_ticker_remove_event(ticker_event_t *obj) {
emilmont 4:c4bfb462ca53 92 __disable_irq();
emilmont 4:c4bfb462ca53 93
emilmont 4:c4bfb462ca53 94 // remove this object from the list
emilmont 4:c4bfb462ca53 95 if (head == obj) {
emilmont 4:c4bfb462ca53 96 // first in the list, so just drop me
emilmont 4:c4bfb462ca53 97 head = obj->next;
emilmont 4:c4bfb462ca53 98 if (obj->next != NULL) {
emilmont 4:c4bfb462ca53 99 us_ticker_set_interrupt(head->timestamp);
emilmont 4:c4bfb462ca53 100 }
emilmont 4:c4bfb462ca53 101 } else {
emilmont 4:c4bfb462ca53 102 // find the object before me, then drop me
emilmont 4:c4bfb462ca53 103 ticker_event_t* p = head;
emilmont 4:c4bfb462ca53 104 while (p != NULL) {
emilmont 4:c4bfb462ca53 105 if (p->next == obj) {
emilmont 4:c4bfb462ca53 106 p->next = obj->next;
emilmont 4:c4bfb462ca53 107 break;
emilmont 4:c4bfb462ca53 108 }
emilmont 4:c4bfb462ca53 109 p = p->next;
emilmont 4:c4bfb462ca53 110 }
emilmont 4:c4bfb462ca53 111 }
emilmont 4:c4bfb462ca53 112
emilmont 4:c4bfb462ca53 113 __enable_irq();
emilmont 4:c4bfb462ca53 114 }