mbed library for NZ32-SC151

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers ticker_api.c Source File

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 "ticker_api.h"
00018 #include "cmsis.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     __disable_irq();
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     /* if prev is NULL we're at the head */
00078     if (prev == NULL) {
00079         data->queue->head = obj;
00080         data->interface->set_interrupt(timestamp);
00081     } else {
00082         prev->next = obj;
00083     }
00084     /* if we're at the end p will be NULL, which is correct */
00085     obj->next = p;
00086 
00087     __enable_irq();
00088 }
00089 
00090 void ticker_remove_event(const ticker_data_t *const data, ticker_event_t *obj) {
00091     __disable_irq();
00092 
00093     // remove this object from the list
00094     if (data->queue->head == obj) {
00095         // first in the list, so just drop me
00096         data->queue->head = obj->next;
00097         if (data->queue->head == NULL) {
00098             data->interface->disable_interrupt();
00099         } else {
00100             data->interface->set_interrupt(data->queue->head->timestamp);
00101         }
00102     } else {
00103         // find the object before me, then drop me
00104         ticker_event_t* p = data->queue->head;
00105         while (p != NULL) {
00106             if (p->next == obj) {
00107                 p->next = obj->next;
00108                 break;
00109             }
00110             p = p->next;
00111         }
00112     }
00113 
00114     __enable_irq();
00115 }
00116 
00117 timestamp_t ticker_read(const ticker_data_t *const data)
00118 {
00119     return data->interface->read();
00120 }
00121 
00122 int ticker_get_next_timestamp(const ticker_data_t *const data, timestamp_t *timestamp)
00123 {
00124     int ret = 0;
00125 
00126     /* if head is NULL, there are no pending events */
00127     __disable_irq();
00128     if (data->queue->head != NULL) {
00129         *timestamp = data->queue->head->timestamp;
00130         ret = 1;
00131     }
00132     __enable_irq();
00133 
00134     return ret;
00135 }