fft

Dependencies:   BSP_DISCO_F746NG SDFileSystem_Warning_Fixed

Committer:
MikamiUitOpen
Date:
Sun Oct 09 10:11:14 2016 +0000
Revision:
0:3e46577dc273
1

Who changed what in which revision?

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