-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathll_ifc_no_mac.c
338 lines (279 loc) · 8.01 KB
/
ll_ifc_no_mac.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
#include "ll_ifc_no_mac.h"
#include "ll_ifc.h"
#include "ll_ifc_private.h"
#include <string.h> // memset
int32_t ll_rssi_scan_set(uint32_t u1, uint32_t u2, uint32_t u3, uint32_t u4)
{
uint8_t buf[16];
memset(buf, 0, sizeof(buf));
// Little Endian
buf[ 0] = (u1 >> 24) & 0xFF;
buf[ 1] = (u1 >> 16) & 0xFF;
buf[ 2] = (u1 >> 8) & 0xFF;
buf[ 3] = (u1 ) & 0xFF;
buf[ 4] = (u2 >> 24) & 0xFF;
buf[ 5] = (u2 >> 16) & 0xFF;
buf[ 6] = (u2 >> 8) & 0xFF;
buf[ 7] = (u2 ) & 0xFF;
buf[ 8] = (u3 >> 24) & 0xFF;
buf[ 9] = (u3 >> 16) & 0xFF;
buf[10] = (u3 >> 8) & 0xFF;
buf[11] = (u3 ) & 0xFF;
buf[12] = (u4 >> 24) & 0xFF;
buf[13] = (u4 >> 16) & 0xFF;
buf[14] = (u4 >> 8) & 0xFF;
buf[15] = (u4 ) & 0xFF;
return hal_read_write(OP_RSSI_SET, buf, 16, NULL, 0);
}
int32_t ll_rssi_scan_get(uint8_t buf[], uint16_t len, uint8_t *bytes_received)
{
int32_t rw_response;
if (buf == NULL || len <= 0 || bytes_received == NULL)
{
return LL_IFC_ERROR_INCORRECT_PARAMETER;
}
rw_response = hal_read_write(OP_RSSI_GET, NULL, 0, buf, len);
if (rw_response < 0)
{
*bytes_received = 0;
return(-1);
}
else
{
*bytes_received = (uint8_t) (rw_response & 0xFF);
return(0);
}
}
int32_t ll_radio_params_get(uint8_t *sf, uint8_t *cr, uint8_t *bw, uint32_t *freq,
uint16_t *preamble_syms, uint8_t *header_enabled, uint8_t *crc_enabled,
uint8_t *iq_inverted)
{
int32_t ret;
uint8_t buff[8];
ret = hal_read_write(OP_GET_RADIO_PARAMS, NULL, 0, buff, 8);
*sf = (buff[0] >> 4) + 6;
*cr = ((buff[0] >> 2) & 0x03) + 1;
*bw = buff[0] & 0x03;
*header_enabled = buff[1] & (1u << 0u);
*crc_enabled = buff[1] & (1u << 1u);
*iq_inverted = buff[1] & (1u << 2u);
*preamble_syms = ((uint16_t)buff[2] << 8) | (uint16_t)buff[3];
*freq = (uint32_t)(buff[4] << 24);
*freq |= (uint32_t)(buff[5] << 16);
*freq |= (uint32_t)(buff[6] << 8);
*freq |= (uint32_t)(buff[7] );
return ret;
}
int32_t ll_radio_params_set(uint8_t flags, uint8_t sf, uint8_t cr, uint8_t bw, uint32_t freq,
uint16_t preamble_syms, uint8_t enable_header, uint8_t enable_crc,
uint8_t enable_iq_inversion)
{
uint8_t buf[9];
memset(buf, 0, sizeof(buf));
buf[0] = flags;
if(flags & RADIO_PARAM_FLAGS_SF)
{
if (sf < 6 || sf > 12)
{
return LL_IFC_ERROR_INCORRECT_PARAMETER;
}
sf = sf - 6;
buf[1] |= ((sf&0x07) << 4);
}
if(flags & RADIO_PARAM_FLAGS_CR)
{
if (cr < 1 || cr > 4)
{
return LL_IFC_ERROR_INCORRECT_PARAMETER;
}
cr = cr - 1;
buf[1] |= ((cr&0x03) << 2);
}
if(flags & RADIO_PARAM_FLAGS_BW)
{
if (bw > 3)
{
return LL_IFC_ERROR_INCORRECT_PARAMETER;
}
buf[1] |= ((bw&0x03) );
}
if ((flags & RADIO_PARAM_FLAGS_HEADER) && enable_header)
{
buf[2] |= (1u << 0u);
}
if ((flags & RADIO_PARAM_FLAGS_CRC) && enable_crc)
{
buf[2] |= (1u << 1u);
}
if ((flags & RADIO_PARAM_FLAGS_IQ) && enable_iq_inversion)
{
buf[2] |= (1u << 2u);
}
if (flags & RADIO_PARAM_FLAGS_PREAMBLE)
{
buf[3] = (preamble_syms >> 8) & 0xFF;
buf[4] = (preamble_syms >> 0) & 0xFF;
}
if(flags & RADIO_PARAM_FLAGS_FREQ)
{
buf[5] = (freq >> 24) & 0xFF;
buf[6] = (freq >> 16) & 0xFF;
buf[7] = (freq >> 8) & 0xFF;
buf[8] = (freq ) & 0xFF;
}
return hal_read_write(OP_SET_RADIO_PARAMS, buf, 9, NULL, 0);
}
int32_t ll_bandwidth_set(uint8_t bandwidth)
{
return ll_radio_params_set(RADIO_PARAM_FLAGS_BW, 0, 0, bandwidth, 0, 0, 0, 0, 0);
}
int32_t ll_spreading_factor_set(uint8_t sf)
{
return ll_radio_params_set(RADIO_PARAM_FLAGS_SF, sf, 0, 0, 0, 0, 0, 0, 0);
}
int32_t ll_coding_rate_set(uint8_t coding_rate)
{
return ll_radio_params_set(RADIO_PARAM_FLAGS_CR, 0, coding_rate, 0, 0, 0, 0, 0, 0);
}
int32_t ll_tx_power_set(int8_t pwr)
{
if (pwr < -4 || pwr > 26)
{
return LL_IFC_ERROR_INCORRECT_PARAMETER;
}
return hal_read_write(OP_TX_POWER_SET, (uint8_t *)&pwr, 1, NULL, 0);
}
int32_t ll_tx_power_get(int8_t *pwr)
{
if (NULL == pwr)
{
return LL_IFC_ERROR_INCORRECT_PARAMETER;
}
return hal_read_write(OP_TX_POWER_GET, NULL, 0, (uint8_t *)pwr, 1);
}
int32_t ll_frequency_set(uint32_t freq)
{
return ll_radio_params_set(RADIO_PARAM_FLAGS_FREQ, 0, 0, 0, freq, 0, 0, 0, 0);
}
int32_t ll_preamble_syms_set(uint16_t num_syms)
{
return ll_radio_params_set(RADIO_PARAM_FLAGS_PREAMBLE, 0, 0, 0, 0, num_syms, 0, 0, 0);
}
int32_t ll_header_enabled_set(uint8_t enabled)
{
return ll_radio_params_set(RADIO_PARAM_FLAGS_HEADER, 0, 0, 0, 0, 0, enabled, 0, 0);
}
int32_t ll_crc_enabled_set(uint8_t enabled)
{
return ll_radio_params_set(RADIO_PARAM_FLAGS_CRC, 0, 0, 0, 0, 0, 0, enabled, 0);
}
int32_t ll_iq_inversion_set(uint8_t inverted)
{
return ll_radio_params_set(RADIO_PARAM_FLAGS_IQ, 0, 0, 0, 0, 0, 0, 0, inverted);
}
int32_t ll_sync_word_set(uint8_t sync_word)
{
return hal_read_write(OP_SYNC_WORD_SET, (uint8_t *)&sync_word, 1, NULL, 0);
}
int32_t ll_sync_word_get(uint8_t *sync_word)
{
if (NULL == sync_word)
{
return LL_IFC_ERROR_INCORRECT_PARAMETER;
}
return hal_read_write(OP_SYNC_WORD_GET, NULL, 0, sync_word, 1);
}
int32_t ll_echo_mode(void)
{
return hal_read_write(OP_PKT_ECHO, NULL, 0, NULL, 0);
}
int32_t ll_packet_send(uint8_t buf[], uint16_t len)
{
return ll_packet_send_queue(buf, len);
}
int32_t ll_packet_send_queue(uint8_t buf[], uint16_t len)
{
if (buf == NULL || len <= 0)
{
return LL_IFC_ERROR_INCORRECT_PARAMETER;
}
uint8_t cmd_response;
int32_t rw_response = hal_read_write(OP_PKT_SEND_QUEUE, buf, len, &cmd_response, 1);
if (rw_response < 0)
{
return((int8_t)rw_response);
}
else
{
return(cmd_response);
}
}
int32_t ll_transmit_cw(void)
{
return hal_read_write(OP_TX_CW, NULL, 0, NULL, 0);
}
int32_t ll_packet_recv_cont(uint8_t buf[], uint16_t len, uint8_t *bytes_received)
{
int32_t rw_response;
if (buf == NULL || len == 0 || bytes_received == NULL)
{
return LL_IFC_ERROR_INCORRECT_PARAMETER;
}
rw_response = hal_read_write(OP_PKT_RECV_CONT, NULL, 0, buf, len);
if (rw_response < 0)
{
*bytes_received = 0;
return(-1);
}
else
{
*bytes_received = (uint8_t) (rw_response & 0xFF);
return(0);
}
}
int32_t ll_packet_recv(uint16_t num_timeout_symbols, uint8_t buf[], uint16_t len, uint8_t *bytes_received)
{
uint8_t buff[2];
int32_t rw_response;
if (buf == NULL || len <= 0 || bytes_received == NULL)
{
return LL_IFC_ERROR_INCORRECT_PARAMETER;
}
// Make the uint16_t value big-endian
buff[0] = (num_timeout_symbols >> 8) & 0xFF;
buff[1] = (num_timeout_symbols >> 0) & 0xFF;
rw_response = hal_read_write(OP_PKT_RECV, buff, sizeof(num_timeout_symbols), buf, len);
if (rw_response < 0)
{
*bytes_received = 0;
return(-1);
}
else
{
*bytes_received = (uint8_t) (rw_response & 0xFF);
return(0);
}
}
int32_t ll_packet_recv_with_rssi(uint16_t num_timeout_symbols, uint8_t buf[], uint16_t len, uint8_t *bytes_received)
{
uint8_t buff[2];
int32_t rw_response;
if (buf == NULL || len <= 0 || bytes_received == NULL)
{
return LL_IFC_ERROR_INCORRECT_PARAMETER;
}
// Make the uint16_t value big-endian
buff[0] = (num_timeout_symbols >> 8) & 0xFF;
buff[1] = (num_timeout_symbols >> 0) & 0xFF;
rw_response = hal_read_write(OP_MSG_RECV_RSSI, buff, sizeof(num_timeout_symbols), buf, len);
if (rw_response < 0)
{
*bytes_received = 0;
return(-1);
}
else
{
*bytes_received = (uint8_t) (rw_response & 0xFF);
return(0);
}
}