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