source: EcnlProtoTool/trunk/asp3_dcre/mbed/common/ticker_api.c@ 321

Last change on this file since 321 was 321, checked in by coas-nagasima, 7 years ago

文字コードを設定

  • Property svn:eol-style set to native
  • Property svn:mime-type set to text/x-csrc;charset=UTF-8
File size: 4.2 KB
Line 
1/* mbed Microcontroller Library
2 * Copyright (c) 2015 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 "ticker_api.h"
18#include "cmsis.h"
19
20void ticker_set_handler(const ticker_data_t *const data, ticker_event_handler handler) {
21 data->interface->init();
22
23 data->queue->event_handler = handler;
24}
25
26void ticker_irq_handler(const ticker_data_t *const data) {
27 data->interface->clear_interrupt();
28
29 /* Go through all the pending TimerEvents */
30 while (1) {
31 if (data->queue->head == NULL) {
32 // There are no more TimerEvents left, so disable matches.
33 data->interface->disable_interrupt();
34 return;
35 }
36
37 if ((int)(data->queue->head->timestamp - data->interface->read()) <= 0) {
38 // This event was in the past:
39 // point to the following one and execute its handler
40 ticker_event_t *p = data->queue->head;
41 data->queue->head = data->queue->head->next;
42 if (data->queue->event_handler != NULL) {
43 (*data->queue->event_handler)(p->id); // NOTE: the handler can set new events
44 }
45 /* Note: We continue back to examining the head because calling the
46 * event handler may have altered the chain of pending events. */
47 } else {
48 // This event and the following ones in the list are in the future:
49 // set it as next interrupt and return
50 data->interface->set_interrupt(data->queue->head->timestamp);
51 return;
52 }
53 }
54}
55
56void ticker_insert_event(const ticker_data_t *const data, ticker_event_t *obj, timestamp_t timestamp, uint32_t id) {
57 /* disable interrupts for the duration of the function */
58 __disable_irq();
59
60 // initialise our data
61 obj->timestamp = timestamp;
62 obj->id = id;
63
64 /* Go through the list until we either reach the end, or find
65 an element this should come before (which is possibly the
66 head). */
67 ticker_event_t *prev = NULL, *p = data->queue->head;
68 while (p != NULL) {
69 /* check if we come before p */
70 if ((int)(timestamp - p->timestamp) < 0) {
71 break;
72 }
73 /* go to the next element */
74 prev = p;
75 p = p->next;
76 }
77 /* if prev is NULL we're at the head */
78 if (prev == NULL) {
79 data->queue->head = obj;
80 data->interface->set_interrupt(timestamp);
81 } else {
82 prev->next = obj;
83 }
84 /* if we're at the end p will be NULL, which is correct */
85 obj->next = p;
86
87 __enable_irq();
88}
89
90void ticker_remove_event(const ticker_data_t *const data, ticker_event_t *obj) {
91 __disable_irq();
92
93 // remove this object from the list
94 if (data->queue->head == obj) {
95 // first in the list, so just drop me
96 data->queue->head = obj->next;
97 if (data->queue->head == NULL) {
98 data->interface->disable_interrupt();
99 } else {
100 data->interface->set_interrupt(data->queue->head->timestamp);
101 }
102 } else {
103 // find the object before me, then drop me
104 ticker_event_t* p = data->queue->head;
105 while (p != NULL) {
106 if (p->next == obj) {
107 p->next = obj->next;
108 break;
109 }
110 p = p->next;
111 }
112 }
113
114 __enable_irq();
115}
116
117timestamp_t ticker_read(const ticker_data_t *const data)
118{
119 return data->interface->read();
120}
121
122int ticker_get_next_timestamp(const ticker_data_t *const data, timestamp_t *timestamp)
123{
124 int ret = 0;
125
126 /* if head is NULL, there are no pending events */
127 __disable_irq();
128 if (data->queue->head != NULL) {
129 *timestamp = data->queue->head->timestamp;
130 ret = 1;
131 }
132 __enable_irq();
133
134 return ret;
135}
Note: See TracBrowser for help on using the repository browser.