mbed library for NZ32-SC151

Committer:
modtronix-com
Date:
Fri Aug 19 15:46:42 2016 +1000
Revision:
17:639ed60ce759
Parent:
1:71204b8406f2
Added tag v1.1 for changeset 076cbe3e55be

Who changed what in which revision?

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