Nothing Special / mbed-STM32F103C8

Fork of mbed-STM32F103C8_org by Nothing Special

Committer:
mega64
Date:
Thu Mar 16 06:15:53 2017 +0000
Revision:
146:03e976389d16
fully rebuild, now based on mbed-dev v160

Who changed what in which revision?

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