Tim Barry / mbed_blinky_offset
Committer:
timbobazza
Date:
Sat Oct 04 09:07:08 2014 +0000
Revision:
0:2173789ea697
First version

Who changed what in which revision?

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