mbed library sources(for async_print)

Dependents:   AsyncPrint

Fork of mbed-src by mbed official

Committer:
emilmont
Date:
Wed Mar 13 15:04:05 2013 +0000
Revision:
4:c4bfb462ca53
Child:
6:6dfdb79ccc45
Add default LED patterns for error notification; Add module for common logic for sorted linked list of events; Move the PinName parsing to the RPC library;

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 4:c4bfb462ca53 18
emilmont 4:c4bfb462ca53 19 static ticker_event_handler event_handler;
emilmont 4:c4bfb462ca53 20 static ticker_event_t *head = NULL;
emilmont 4:c4bfb462ca53 21
emilmont 4:c4bfb462ca53 22 void us_ticker_set_handler(ticker_event_handler handler) {
emilmont 4:c4bfb462ca53 23 us_ticker_init();
emilmont 4:c4bfb462ca53 24
emilmont 4:c4bfb462ca53 25 event_handler = handler;
emilmont 4:c4bfb462ca53 26 }
emilmont 4:c4bfb462ca53 27
emilmont 4:c4bfb462ca53 28 void us_ticker_irq_handler(void) {
emilmont 4:c4bfb462ca53 29 us_ticker_clear_interrupt();
emilmont 4:c4bfb462ca53 30
emilmont 4:c4bfb462ca53 31 /* Go through all the pending TimerEvents */
emilmont 4:c4bfb462ca53 32 while (1) {
emilmont 4:c4bfb462ca53 33 if (head == NULL) {
emilmont 4:c4bfb462ca53 34 // There are no more TimerEvents left, so disable matches.
emilmont 4:c4bfb462ca53 35 us_ticker_disable_interrupt();
emilmont 4:c4bfb462ca53 36 return;
emilmont 4:c4bfb462ca53 37 }
emilmont 4:c4bfb462ca53 38
emilmont 4:c4bfb462ca53 39 if ((int)(head->timestamp - us_ticker_read()) <= 0) {
emilmont 4:c4bfb462ca53 40 // This event was in the past:
emilmont 4:c4bfb462ca53 41 // point to the following one and execute its handler
emilmont 4:c4bfb462ca53 42 ticker_event_t *p = head;
emilmont 4:c4bfb462ca53 43 head = head->next;
emilmont 4:c4bfb462ca53 44 if (event_handler != NULL) {
emilmont 4:c4bfb462ca53 45 event_handler(p->id); // NOTE: the handler can set new events
emilmont 4:c4bfb462ca53 46 }
emilmont 4:c4bfb462ca53 47 } else {
emilmont 4:c4bfb462ca53 48 // This event and the following ones in the list are in the future:
emilmont 4:c4bfb462ca53 49 // set it as next interrupt and return
emilmont 4:c4bfb462ca53 50 us_ticker_set_interrupt(head->timestamp);
emilmont 4:c4bfb462ca53 51 return;
emilmont 4:c4bfb462ca53 52 }
emilmont 4:c4bfb462ca53 53 }
emilmont 4:c4bfb462ca53 54 }
emilmont 4:c4bfb462ca53 55
emilmont 4:c4bfb462ca53 56 void us_ticker_insert_event(ticker_event_t *obj, unsigned int timestamp, uint32_t id) {
emilmont 4:c4bfb462ca53 57 /* disable interrupts for the duration of the function */
emilmont 4:c4bfb462ca53 58 __disable_irq();
emilmont 4:c4bfb462ca53 59
emilmont 4:c4bfb462ca53 60 // initialise our data
emilmont 4:c4bfb462ca53 61 obj->timestamp = timestamp;
emilmont 4:c4bfb462ca53 62 obj->id = id;
emilmont 4:c4bfb462ca53 63
emilmont 4:c4bfb462ca53 64 /* Go through the list until we either reach the end, or find
emilmont 4:c4bfb462ca53 65 an element this should come before (which is possibly the
emilmont 4:c4bfb462ca53 66 head). */
emilmont 4:c4bfb462ca53 67 ticker_event_t *prev = NULL, *p = head;
emilmont 4:c4bfb462ca53 68 while (p != NULL) {
emilmont 4:c4bfb462ca53 69 /* check if we come before p */
emilmont 4:c4bfb462ca53 70 if ((int)(timestamp - p->timestamp) <= 0) {
emilmont 4:c4bfb462ca53 71 break;
emilmont 4:c4bfb462ca53 72 }
emilmont 4:c4bfb462ca53 73 /* go to the next element */
emilmont 4:c4bfb462ca53 74 prev = p;
emilmont 4:c4bfb462ca53 75 p = p->next;
emilmont 4:c4bfb462ca53 76 }
emilmont 4:c4bfb462ca53 77 /* if prev is NULL we're at the head */
emilmont 4:c4bfb462ca53 78 if (prev == NULL) {
emilmont 4:c4bfb462ca53 79 head = obj;
emilmont 4:c4bfb462ca53 80 us_ticker_set_interrupt(timestamp);
emilmont 4:c4bfb462ca53 81 } else {
emilmont 4:c4bfb462ca53 82 prev->next = obj;
emilmont 4:c4bfb462ca53 83 }
emilmont 4:c4bfb462ca53 84 /* if we're at the end p will be NULL, which is correct */
emilmont 4:c4bfb462ca53 85 obj->next = p;
emilmont 4:c4bfb462ca53 86
emilmont 4:c4bfb462ca53 87 __enable_irq();
emilmont 4:c4bfb462ca53 88 }
emilmont 4:c4bfb462ca53 89
emilmont 4:c4bfb462ca53 90 void us_ticker_remove_event(ticker_event_t *obj) {
emilmont 4:c4bfb462ca53 91 __disable_irq();
emilmont 4:c4bfb462ca53 92
emilmont 4:c4bfb462ca53 93 // remove this object from the list
emilmont 4:c4bfb462ca53 94 if (head == obj) {
emilmont 4:c4bfb462ca53 95 // first in the list, so just drop me
emilmont 4:c4bfb462ca53 96 head = obj->next;
emilmont 4:c4bfb462ca53 97 if (obj->next != NULL) {
emilmont 4:c4bfb462ca53 98 us_ticker_set_interrupt(head->timestamp);
emilmont 4:c4bfb462ca53 99 }
emilmont 4:c4bfb462ca53 100 } else {
emilmont 4:c4bfb462ca53 101 // find the object before me, then drop me
emilmont 4:c4bfb462ca53 102 ticker_event_t* p = head;
emilmont 4:c4bfb462ca53 103 while (p != NULL) {
emilmont 4:c4bfb462ca53 104 if (p->next == obj) {
emilmont 4:c4bfb462ca53 105 p->next = obj->next;
emilmont 4:c4bfb462ca53 106 break;
emilmont 4:c4bfb462ca53 107 }
emilmont 4:c4bfb462ca53 108 p = p->next;
emilmont 4:c4bfb462ca53 109 }
emilmont 4:c4bfb462ca53 110 }
emilmont 4:c4bfb462ca53 111
emilmont 4:c4bfb462ca53 112 __enable_irq();
emilmont 4:c4bfb462ca53 113 }