|
| 1 | +/* |
| 2 | + * Copyright (c) 2018 ARM Limited. All rights reserved. |
| 3 | + * SPDX-License-Identifier: Apache-2.0 |
| 4 | + * Licensed under the Apache License, Version 2.0 (the License); you may |
| 5 | + * not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an AS IS BASIS, WITHOUT |
| 12 | + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | +#include "ns_types.h" |
| 17 | +#include "fhss_api.h" |
| 18 | +#include "fhss_config.h" |
| 19 | +#include "mbed.h" |
| 20 | +#include "mbed_trace.h" |
| 21 | +#include "platform/arm_hal_interrupt.h" |
| 22 | +#include <Timer.h> |
| 23 | + |
| 24 | +#define TRACE_GROUP "fhdr" |
| 25 | +#ifndef NUMBER_OF_SIMULTANEOUS_TIMEOUTS |
| 26 | +#define NUMBER_OF_SIMULTANEOUS_TIMEOUTS 2 |
| 27 | +#endif //NUMBER_OF_SIMULTANEOUS_TIMEOUTS |
| 28 | + |
| 29 | +static Timer timer; |
| 30 | +static bool timer_initialized = false; |
| 31 | +static const fhss_api_t *fhss_active_handle = NULL; |
| 32 | +#if !MBED_CONF_NANOSTACK_HAL_CRITICAL_SECTION_USABLE_FROM_INTERRUPT |
| 33 | +static EventQueue *equeue; |
| 34 | +#endif |
| 35 | + |
| 36 | +struct fhss_timeout_s |
| 37 | +{ |
| 38 | + void (*fhss_timer_callback)(const fhss_api_t *fhss_api, uint16_t); |
| 39 | + uint32_t start_time; |
| 40 | + uint32_t stop_time; |
| 41 | + bool active; |
| 42 | + Timeout timeout; |
| 43 | +}; |
| 44 | + |
| 45 | +fhss_timeout_s fhss_timeout[NUMBER_OF_SIMULTANEOUS_TIMEOUTS]; |
| 46 | + |
| 47 | +static uint32_t read_current_time(void) |
| 48 | +{ |
| 49 | + return timer.read_us(); |
| 50 | +} |
| 51 | + |
| 52 | +static fhss_timeout_s *find_timeout(void (*callback)(const fhss_api_t *api, uint16_t)) |
| 53 | +{ |
| 54 | + for (int i = 0; i < NUMBER_OF_SIMULTANEOUS_TIMEOUTS; i++) { |
| 55 | + if (fhss_timeout[i].fhss_timer_callback == callback) { |
| 56 | + return &fhss_timeout[i]; |
| 57 | + } |
| 58 | + } |
| 59 | + return NULL; |
| 60 | +} |
| 61 | + |
| 62 | +static fhss_timeout_s *allocate_timeout(void) |
| 63 | +{ |
| 64 | + for (int i = 0; i < NUMBER_OF_SIMULTANEOUS_TIMEOUTS; i++) { |
| 65 | + if (fhss_timeout[i].fhss_timer_callback == NULL) { |
| 66 | + memset(&fhss_timeout[i], sizeof(fhss_timeout_s), 0); |
| 67 | + return &fhss_timeout[i]; |
| 68 | + } |
| 69 | + } |
| 70 | + return NULL; |
| 71 | +} |
| 72 | + |
| 73 | +static void fhss_timeout_handler(void) |
| 74 | +{ |
| 75 | + for (int i = 0; i < NUMBER_OF_SIMULTANEOUS_TIMEOUTS; i++) { |
| 76 | + if (fhss_timeout[i].active && ((fhss_timeout[i].stop_time - fhss_timeout[i].start_time) <= (read_current_time() - fhss_timeout[i].start_time))) { |
| 77 | + fhss_timeout[i].active = false; |
| 78 | + fhss_timeout[i].fhss_timer_callback(fhss_active_handle, read_current_time() - fhss_timeout[i].stop_time); |
| 79 | + } |
| 80 | + } |
| 81 | +} |
| 82 | + |
| 83 | +static void timer_callback(void) |
| 84 | +{ |
| 85 | +#if MBED_CONF_NANOSTACK_HAL_CRITICAL_SECTION_USABLE_FROM_INTERRUPT |
| 86 | + fhss_timeout_handler(); |
| 87 | +#else |
| 88 | + equeue->call(fhss_timeout_handler); |
| 89 | +#endif |
| 90 | +} |
| 91 | + |
| 92 | +static int platform_fhss_timer_start(uint32_t slots, void (*callback)(const fhss_api_t *api, uint16_t), const fhss_api_t *callback_param) |
| 93 | +{ |
| 94 | + int ret_val = -1; |
| 95 | + platform_enter_critical(); |
| 96 | + if (timer_initialized == false) { |
| 97 | +#if !MBED_CONF_NANOSTACK_HAL_CRITICAL_SECTION_USABLE_FROM_INTERRUPT |
| 98 | + equeue = mbed_highprio_event_queue(); |
| 99 | + MBED_ASSERT(equeue != NULL); |
| 100 | +#endif |
| 101 | + timer.start(); |
| 102 | + timer_initialized = true; |
| 103 | + } |
| 104 | + fhss_timeout_s *fhss_tim = find_timeout(callback); |
| 105 | + if (!fhss_tim) { |
| 106 | + fhss_tim = allocate_timeout(); |
| 107 | + } |
| 108 | + if (!fhss_tim) { |
| 109 | + platform_exit_critical(); |
| 110 | + tr_error("Failed to allocate timeout"); |
| 111 | + return ret_val; |
| 112 | + } |
| 113 | + fhss_tim->fhss_timer_callback = callback; |
| 114 | + fhss_tim->start_time = read_current_time(); |
| 115 | + fhss_tim->stop_time = fhss_tim->start_time + slots; |
| 116 | + fhss_tim->active = true; |
| 117 | + fhss_tim->timeout.attach_us(timer_callback, slots); |
| 118 | + fhss_active_handle = callback_param; |
| 119 | + ret_val = 0; |
| 120 | + platform_exit_critical(); |
| 121 | + return ret_val; |
| 122 | +} |
| 123 | + |
| 124 | +static int platform_fhss_timer_stop(void (*callback)(const fhss_api_t *api, uint16_t), const fhss_api_t *api) |
| 125 | +{ |
| 126 | + (void)api; |
| 127 | + platform_enter_critical(); |
| 128 | + fhss_timeout_s *fhss_tim = find_timeout(callback); |
| 129 | + if (!fhss_tim) { |
| 130 | + platform_exit_critical(); |
| 131 | + return -1; |
| 132 | + } |
| 133 | + fhss_tim->timeout.detach(); |
| 134 | + fhss_tim->active = false; |
| 135 | + platform_exit_critical(); |
| 136 | + return 0; |
| 137 | +} |
| 138 | + |
| 139 | +static uint32_t platform_fhss_get_remaining_slots(void (*callback)(const fhss_api_t *api, uint16_t), const fhss_api_t *api) |
| 140 | +{ |
| 141 | + (void)api; |
| 142 | + platform_enter_critical(); |
| 143 | + fhss_timeout_s *fhss_tim = find_timeout(callback); |
| 144 | + if (!fhss_tim) { |
| 145 | + platform_exit_critical(); |
| 146 | + return 0; |
| 147 | + } |
| 148 | + uint32_t remaining_slots = fhss_tim->stop_time - read_current_time(); |
| 149 | + platform_exit_critical(); |
| 150 | + return remaining_slots; |
| 151 | +} |
| 152 | + |
| 153 | +static uint32_t platform_fhss_timestamp_read(const fhss_api_t *api) |
| 154 | +{ |
| 155 | + (void)api; |
| 156 | + return read_current_time(); |
| 157 | +} |
| 158 | + |
| 159 | +fhss_timer_t fhss_functions = { |
| 160 | + .fhss_timer_start = platform_fhss_timer_start, |
| 161 | + .fhss_timer_stop = platform_fhss_timer_stop, |
| 162 | + .fhss_get_remaining_slots = platform_fhss_get_remaining_slots, |
| 163 | + .fhss_get_timestamp = platform_fhss_timestamp_read, |
| 164 | + .fhss_resolution_divider = 1 |
| 165 | +}; |
0 commit comments