Skip to content

Commit 440fbfd

Browse files
author
Jarkko Paso
committed
NS hal: Implemented FHSS driver
1 parent a0a3dd6 commit 440fbfd

File tree

1 file changed

+157
-0
lines changed

1 file changed

+157
-0
lines changed
Lines changed: 157 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,157 @@
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+
#define NUMBER_OF_SIMULTANEOUS_TIMEOUTS 2
26+
27+
static Timer timer;
28+
static bool timer_initialized = false;
29+
static const fhss_api_t *fhss_active_handle = NULL;
30+
#if !MBED_CONF_NANOSTACK_HAL_CRITICAL_SECTION_USABLE_FROM_INTERRUPT
31+
static EventQueue *equeue;
32+
#endif
33+
34+
struct fhss_timeout_s
35+
{
36+
void (*fhss_timer_callback)(const fhss_api_t *fhss_api, uint16_t);
37+
uint32_t start_time;
38+
uint32_t stop_time;
39+
bool active;
40+
Timeout timeout;
41+
};
42+
43+
fhss_timeout_s fhss_timeout[NUMBER_OF_SIMULTANEOUS_TIMEOUTS];
44+
45+
static uint32_t read_current_time(void)
46+
{
47+
return timer.read_us();
48+
}
49+
50+
static fhss_timeout_s *find_timeout(void (*callback)(const fhss_api_t *api, uint16_t))
51+
{
52+
for (int i = 0; i < NUMBER_OF_SIMULTANEOUS_TIMEOUTS; i++) {
53+
if (fhss_timeout[i].fhss_timer_callback == callback) {
54+
return &fhss_timeout[i];
55+
}
56+
}
57+
return NULL;
58+
}
59+
60+
static fhss_timeout_s *allocate_timeout(void)
61+
{
62+
for (int i = 0; i < NUMBER_OF_SIMULTANEOUS_TIMEOUTS; i++) {
63+
if (fhss_timeout[i].fhss_timer_callback == NULL) {
64+
memset(&fhss_timeout[i], sizeof(fhss_timeout_s), 0);
65+
return &fhss_timeout[i];
66+
}
67+
}
68+
return NULL;
69+
}
70+
71+
static void fhss_timeout_handler(void)
72+
{
73+
for (int i = 0; i < NUMBER_OF_SIMULTANEOUS_TIMEOUTS; i++) {
74+
if (fhss_timeout[i].active && ((fhss_timeout[i].stop_time - fhss_timeout[i].start_time) <= (read_current_time() - fhss_timeout[i].start_time))) {
75+
fhss_timeout[i].active = false;
76+
fhss_timeout[i].fhss_timer_callback(fhss_active_handle, read_current_time() - fhss_timeout[i].stop_time);
77+
}
78+
}
79+
}
80+
81+
static void timer_callback(void)
82+
{
83+
#if MBED_CONF_NANOSTACK_HAL_CRITICAL_SECTION_USABLE_FROM_INTERRUPT
84+
fhss_timeout_handler();
85+
#else
86+
equeue->call(fhss_timeout_handler);
87+
#endif
88+
}
89+
90+
static int platform_fhss_timer_start(uint32_t slots, void (*callback)(const fhss_api_t *api, uint16_t), const fhss_api_t *callback_param)
91+
{
92+
int ret_val = -1;
93+
platform_enter_critical();
94+
if (timer_initialized == false) {
95+
#if !MBED_CONF_NANOSTACK_HAL_CRITICAL_SECTION_USABLE_FROM_INTERRUPT
96+
equeue = mbed_highprio_event_queue();
97+
MBED_ASSERT(equeue != NULL);
98+
#endif
99+
timer.start();
100+
timer_initialized = true;
101+
}
102+
fhss_timeout_s *fhss_tim = find_timeout(callback);
103+
if (!fhss_tim) {
104+
fhss_tim = allocate_timeout();
105+
}
106+
fhss_tim->fhss_timer_callback = callback;
107+
fhss_tim->start_time = read_current_time();
108+
fhss_tim->stop_time = fhss_tim->start_time + slots;
109+
fhss_tim->active = true;
110+
fhss_tim->timeout.attach_us(timer_callback, slots);
111+
fhss_active_handle = callback_param;
112+
ret_val = 0;
113+
platform_exit_critical();
114+
return ret_val;
115+
}
116+
117+
static int platform_fhss_timer_stop(void (*callback)(const fhss_api_t *api, uint16_t), const fhss_api_t *api)
118+
{
119+
(void)api;
120+
platform_enter_critical();
121+
fhss_timeout_s *fhss_tim = find_timeout(callback);
122+
if (!fhss_tim) {
123+
platform_exit_critical();
124+
return -1;
125+
}
126+
fhss_tim->timeout.detach();
127+
fhss_tim->active = false;
128+
platform_exit_critical();
129+
return 0;
130+
}
131+
132+
static uint32_t platform_fhss_get_remaining_slots(void (*callback)(const fhss_api_t *api, uint16_t), const fhss_api_t *api)
133+
{
134+
(void)api;
135+
platform_enter_critical();
136+
fhss_timeout_s *fhss_tim = find_timeout(callback);
137+
if (!fhss_tim) {
138+
platform_exit_critical();
139+
return 0;
140+
}
141+
platform_exit_critical();
142+
return fhss_tim->stop_time - read_current_time();
143+
}
144+
145+
static uint32_t platform_fhss_timestamp_read(const fhss_api_t *api)
146+
{
147+
(void)api;
148+
return read_current_time();
149+
}
150+
151+
fhss_timer_t fhss_functions = {
152+
.fhss_timer_start = platform_fhss_timer_start,
153+
.fhss_timer_stop = platform_fhss_timer_stop,
154+
.fhss_get_remaining_slots = platform_fhss_get_remaining_slots,
155+
.fhss_get_timestamp = platform_fhss_timestamp_read,
156+
.fhss_resolution_divider = 1
157+
};

0 commit comments

Comments
 (0)