customized mbed library sources for nrf51822

Dependents:   Grove_Node Potentiometer BLE_Beacon I2C_Scanner

Committer:
yihui
Date:
Tue Nov 04 07:38:53 2014 +0000
Revision:
0:700cadd8b708
customized mbed-src library for nrf51822

Who changed what in which revision?

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