MCU driver/HAL for the Picocell Gateway concentrator board. The firmware implements either a USB CDC protocol or a UART protocol to bridge commands coming from host to the SX1308 SPI interface.

Committer:
dgabino
Date:
Wed Apr 11 14:42:47 2018 +0000
Revision:
0:c76361bd82e8
Initial commit

Who changed what in which revision?

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