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-dev by
mbed_ticker_api.c
00001 /* mbed Microcontroller Library 00002 * Copyright (c) 2015 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 "hal/ticker_api.h" 00018 #include "platform/mbed_critical.h" 00019 00020 void ticker_set_handler(const ticker_data_t *const data, ticker_event_handler handler) { 00021 data->interface->init(); 00022 00023 data->queue->event_handler = handler; 00024 } 00025 00026 void ticker_irq_handler(const ticker_data_t *const data) { 00027 data->interface->clear_interrupt(); 00028 00029 /* Go through all the pending TimerEvents */ 00030 while (1) { 00031 if (data->queue->head == NULL) { 00032 // There are no more TimerEvents left, so disable matches. 00033 data->interface->disable_interrupt(); 00034 return; 00035 } 00036 00037 if ((int)(data->queue->head->timestamp - data->interface->read()) <= 0) { 00038 // This event was in the past: 00039 // point to the following one and execute its handler 00040 ticker_event_t *p = data->queue->head; 00041 data->queue->head = data->queue->head->next; 00042 if (data->queue->event_handler != NULL) { 00043 (*data->queue->event_handler)(p->id); // NOTE: the handler can set new events 00044 } 00045 /* Note: We continue back to examining the head because calling the 00046 * event handler may have altered the chain of pending events. */ 00047 } else { 00048 // This event and the following ones in the list are in the future: 00049 // set it as next interrupt and return 00050 data->interface->set_interrupt(data->queue->head->timestamp); 00051 return; 00052 } 00053 } 00054 } 00055 00056 void ticker_insert_event(const ticker_data_t *const data, ticker_event_t *obj, timestamp_t timestamp, uint32_t id) { 00057 /* disable interrupts for the duration of the function */ 00058 core_util_critical_section_enter(); 00059 00060 // initialise our data 00061 obj->timestamp = timestamp; 00062 obj->id = id; 00063 00064 /* Go through the list until we either reach the end, or find 00065 an element this should come before (which is possibly the 00066 head). */ 00067 ticker_event_t *prev = NULL, *p = data->queue->head; 00068 while (p != NULL) { 00069 /* check if we come before p */ 00070 if ((int)(timestamp - p->timestamp) < 0) { 00071 break; 00072 } 00073 /* go to the next element */ 00074 prev = p; 00075 p = p->next; 00076 } 00077 00078 /* if we're at the end p will be NULL, which is correct */ 00079 obj->next = p; 00080 00081 /* if prev is NULL we're at the head */ 00082 if (prev == NULL) { 00083 data->queue->head = obj; 00084 data->interface->set_interrupt(timestamp); 00085 } else { 00086 prev->next = obj; 00087 } 00088 00089 core_util_critical_section_exit(); 00090 } 00091 00092 void ticker_remove_event(const ticker_data_t *const data, ticker_event_t *obj) { 00093 core_util_critical_section_enter(); 00094 00095 // remove this object from the list 00096 if (data->queue->head == obj) { 00097 // first in the list, so just drop me 00098 data->queue->head = obj->next; 00099 if (data->queue->head == NULL) { 00100 data->interface->disable_interrupt(); 00101 } else { 00102 data->interface->set_interrupt(data->queue->head->timestamp); 00103 } 00104 } else { 00105 // find the object before me, then drop me 00106 ticker_event_t* p = data->queue->head; 00107 while (p != NULL) { 00108 if (p->next == obj) { 00109 p->next = obj->next; 00110 break; 00111 } 00112 p = p->next; 00113 } 00114 } 00115 00116 core_util_critical_section_exit(); 00117 } 00118 00119 timestamp_t ticker_read(const ticker_data_t *const data) 00120 { 00121 return data->interface->read(); 00122 } 00123 00124 int ticker_get_next_timestamp(const ticker_data_t *const data, timestamp_t *timestamp) 00125 { 00126 int ret = 0; 00127 00128 /* if head is NULL, there are no pending events */ 00129 core_util_critical_section_enter(); 00130 if (data->queue->head != NULL) { 00131 *timestamp = data->queue->head->timestamp; 00132 ret = 1; 00133 } 00134 core_util_critical_section_exit(); 00135 00136 return ret; 00137 }
Generated on Tue Jul 12 2022 20:05:48 by
1.7.2
