Test code for Grove Node BLE
Dependencies: BLE_API nRF51822
Fork of BLE_LoopbackUART by
us_ticker_api.c
00001 /* mbed Microcontroller Library 00002 * Copyright (c) 2006-2013 ARM Limited 00003 * 00004 * Licensed under the Apache License, Version 2.0 (the "License"); 00005 * you may not use this file except in compliance with the License. 00006 * You may obtain a copy of the License at 00007 * 00008 * http://www.apache.org/licenses/LICENSE-2.0 00009 * 00010 * Unless required by applicable law or agreed to in writing, software 00011 * distributed under the License is distributed on an "AS IS" BASIS, 00012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 00013 * See the License for the specific language governing permissions and 00014 * limitations under the License. 00015 */ 00016 #include <stddef.h> 00017 #include "us_ticker_api.h" 00018 #include "cmsis.h" 00019 00020 static ticker_event_handler event_handler; 00021 static ticker_event_t *head = NULL; 00022 00023 void us_ticker_set_handler(ticker_event_handler handler) { 00024 us_ticker_init(); 00025 00026 event_handler = handler; 00027 } 00028 00029 void us_ticker_irq_handler(void) { 00030 us_ticker_clear_interrupt(); 00031 00032 /* Go through all the pending TimerEvents */ 00033 while (1) { 00034 if (head == NULL) { 00035 // There are no more TimerEvents left, so disable matches. 00036 us_ticker_disable_interrupt(); 00037 return; 00038 } 00039 00040 if ((int64_t)(head->timestamp - us_ticker_read()) <= 0) { 00041 // This event was in the past: 00042 // point to the following one and execute its handler 00043 ticker_event_t *p = head; 00044 head = head->next; 00045 if (event_handler != NULL) { 00046 event_handler(p->id); // NOTE: the handler can set new events 00047 } 00048 /* Note: We continue back to examining the head because calling the 00049 * event handler may have altered the chain of pending events. */ 00050 } else { 00051 // This event and the following ones in the list are in the future: 00052 // set it as next interrupt and return 00053 us_ticker_set_interrupt(head->timestamp); 00054 return; 00055 } 00056 } 00057 } 00058 00059 void us_ticker_insert_event(ticker_event_t *obj, timestamp_t timestamp, uint32_t id) { 00060 /* disable interrupts for the duration of the function */ 00061 __disable_irq(); 00062 00063 // initialise our data 00064 obj->timestamp = timestamp; 00065 obj->id = id; 00066 00067 /* Go through the list until we either reach the end, or find 00068 an element this should come before (which is possibly the 00069 head). */ 00070 ticker_event_t *prev = NULL, *p = head; 00071 while (p != NULL) { 00072 /* check if we come before p */ 00073 if ((int64_t)(timestamp - p->timestamp) < 0) { 00074 break; 00075 } 00076 /* go to the next element */ 00077 prev = p; 00078 p = p->next; 00079 } 00080 /* if prev is NULL we're at the head */ 00081 if (prev == NULL) { 00082 head = obj; 00083 us_ticker_set_interrupt(timestamp); 00084 } else { 00085 prev->next = obj; 00086 } 00087 /* if we're at the end p will be NULL, which is correct */ 00088 obj->next = p; 00089 00090 __enable_irq(); 00091 } 00092 00093 void us_ticker_remove_event(ticker_event_t *obj) { 00094 __disable_irq(); 00095 00096 // remove this object from the list 00097 if (head == obj) { 00098 // first in the list, so just drop me 00099 head = obj->next; 00100 if (head == NULL) { 00101 us_ticker_disable_interrupt(); 00102 } else { 00103 us_ticker_set_interrupt(head->timestamp); 00104 } 00105 } else { 00106 // find the object before me, then drop me 00107 ticker_event_t* p = head; 00108 while (p != NULL) { 00109 if (p->next == obj) { 00110 p->next = obj->next; 00111 break; 00112 } 00113 p = p->next; 00114 } 00115 } 00116 00117 __enable_irq(); 00118 }
Generated on Tue Jul 12 2022 21:43:26 by 1.7.2