Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Fork of mbed-src 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 ((int)(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 ((int)(timestamp - p->timestamp) < 0) { 00074 break; 00075 } 00076 /* go to the next element */ 00077 prev = p; 00078 p = p->next; 00079 } 00080 00081 /* if we're at the end p will be NULL, which is correct */ 00082 obj->next = p; 00083 00084 /* if prev is NULL we're at the head */ 00085 if (prev == NULL) { 00086 head = obj; 00087 us_ticker_set_interrupt(timestamp); 00088 } else { 00089 prev->next = obj; 00090 } 00091 00092 __enable_irq(); 00093 } 00094 00095 void us_ticker_remove_event(ticker_event_t *obj) { 00096 __disable_irq(); 00097 00098 // remove this object from the list 00099 if (head == obj) { 00100 // first in the list, so just drop me 00101 head = obj->next; 00102 if (head == NULL) { 00103 us_ticker_disable_interrupt(); 00104 } else { 00105 us_ticker_set_interrupt(head->timestamp); 00106 } 00107 } else { 00108 // find the object before me, then drop me 00109 ticker_event_t* p = head; 00110 while (p != NULL) { 00111 if (p->next == obj) { 00112 p->next = obj->next; 00113 break; 00114 } 00115 p = p->next; 00116 } 00117 } 00118 00119 __enable_irq(); 00120 } 00121 00122 int us_ticker_get_next_timestamp(timestamp_t *timestamp) { 00123 int ret = 0; 00124 00125 /* if head is NULL, there are no pending events */ 00126 __disable_irq(); 00127 if (head != NULL) { 00128 *timestamp = head->timestamp; 00129 ret = 1; 00130 } 00131 __enable_irq(); 00132 00133 return ret; 00134 }
Generated on Tue Jul 12 2022 17:55:52 by
1.7.2
