forked from vedderb/nunchuk_mod
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrfhelp.c
380 lines (318 loc) · 7.99 KB
/
rfhelp.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
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
/*
Copyright 2015 Benjamin Vedder [email protected]
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "rfhelp.h"
#include "rf.h"
#include "ch.h"
#include "hal.h"
#include "crc.h"
#include <string.h>
// Variables
static mutex_t rf_mutex;
static char rx_addr[6][5];
static char tx_addr[5];
static bool rx_addr_set[6];
static int address_length;
static bool tx_pipe0_addr_eq;
void rfhelp_init(void) {
chMtxObjectInit(&rf_mutex);
// address_length = rf_get_address_width();
address_length = 3; // We assume length 3
// This should not happen
if (address_length > 5 || address_length < 3) {
address_length = 5;
}
for (int i = 0;i < 6;i++) {
rf_read_reg(NRF_REG_RX_ADDR_P0, rx_addr[i], address_length);
rx_addr_set[i] = false;
}
rf_read_reg(NRF_REG_TX_ADDR, tx_addr, address_length);
tx_pipe0_addr_eq = memcmp(rx_addr[0], tx_addr, address_length) == 0;
}
/**
* Re-init the rf chip
*/
void rfhelp_restart(void) {
chMtxLock(&rf_mutex);
rf_init();
rf_set_tx_addr(tx_addr, address_length);
for (int i = 0;i < 6;i++) {
if (rx_addr_set[i]) {
rf_set_rx_addr(i, rx_addr[i], address_length);
}
}
chMtxUnlock(&rf_mutex);
}
/**
* Set TX mode, send data, wait for result, set RX mode.
*
* @param data
* The data to be sent.
*
* @param len
* Length of the data.
*
* @return
* 0: Send OK.
* -1: Max RT.
* -2: Timeout
*/
int rfhelp_send_data(char *data, int len, bool ack) {
int timeout = 60;
int retval = -1;
chMtxLock(&rf_mutex);
rf_mode_tx();
rf_clear_irq();
rf_flush_all();
// Pipe0-address and tx-address must be equal for ack to work.
if (!tx_pipe0_addr_eq && ack) {
rf_set_rx_addr(0, tx_addr, address_length);
}
if (ack) {
rf_write_tx_payload(data, len);
} else {
rf_write_tx_payload_no_ack(data, len);
}
for(;;) {
int s = rf_status();
chThdSleepMilliseconds(1);
timeout--;
if (NRF_STATUS_GET_TX_DS(s)) {
rf_clear_tx_irq();
retval = 0;
break;
} else if (NRF_STATUS_GET_MAX_RT(s)) {
rf_clear_maxrt_irq();
retval = -1;
break;
} else if (timeout == 0) {
retval = -2;
break;
}
}
// Restore pipe0 address
if (!tx_pipe0_addr_eq && ack) {
rf_set_rx_addr(0, rx_addr[0], address_length);
}
rf_mode_rx();
chMtxUnlock(&rf_mutex);
return retval;
}
/**
* Same as rfhelp_send_data, but will add a crc checksum to the end. This is
* useful for protecting against corruption between the NRF and the MCU in case
* there are errors on the SPI bus.
*
* @param data
* The data to be sent.
*
* @param len
* Length of the data. Should be no more than 30 bytes.
*
* @return
* 0: Send OK.
* -1: Max RT.
* -2: Timeout
*/
int rfhelp_send_data_crc(char *data, int len) {
char buffer[len + 2];
unsigned short crc = crc16((unsigned char*)data, len);
memcpy(buffer, data, len);
buffer[len] = (char)(crc >> 8);
buffer[len + 1] = (char)(crc & 0xFF);
return rfhelp_send_data(buffer, len + 2, true);
}
/**
* Same as rfhelp_send_data_crc, but without ack. A counter is included for
* duplicate packets and a number of resends can be specified.
*
* @param data
* The data to be sent.
*
* @param len
* Length of the data. Should be no more than 29 bytes.
*
* @param resends
* The amount of resends for this packet. Should be at least 1.
*
* @return
* 0: Send OK.
* -1: Max RT. (Should not happen)
* -2: Timeout
*/
int rfhelp_send_data_crc_noack(char *data, int len, int resends) {
char buffer[len + 3];
static unsigned char counter = 0;
memcpy(buffer, data, len);
buffer[len] = counter++;
unsigned short crc = crc16((unsigned char*)buffer, len + 1);
buffer[len + 1] = (char)(crc >> 8);
buffer[len + 2] = (char)(crc & 0xFF);
int res = 0;
for (int i = 0;i < resends;i++) {
res = rfhelp_send_data(buffer, len + 3, false);
if (res != 0) {
break;
}
}
return res;
}
/**
* Read data from the RX fifo
*
* @param data
* Pointer to the array in which to store the data.
*
* @param len
* Pointer to variable storing the data length.
*
* @param pipe
* Pointer to the pipe on which the data was received. Can be 0.
*
* @return
* 1: Read OK, more data to read.
* 0: Read OK
* -1: No RX data
* -2: Wrong length read. Something is likely wrong.
*/
int rfhelp_read_rx_data(char *data, int *len, int *pipe) {
int retval = -1;
chMtxLock(&rf_mutex);
int s = rf_status();
int pipe_n = NRF_STATUS_GET_RX_P_NO(s);
if (pipe_n != 7) {
*len = rf_get_payload_width();
if (pipe) {
*pipe = pipe_n;
}
if (*len <= 32 && *len >= 0) {
rf_read_rx_payload(data, *len);
rf_clear_rx_irq();
// rf_flush_rx();
s = rf_status();
if (NRF_STATUS_GET_RX_P_NO(s) == 7) {
retval = 0;
} else {
retval = 1;
}
} else {
*len = 0;
retval = -2;
}
}
chMtxUnlock(&rf_mutex);
return retval;
}
/**
* Same as rfhelp_read_rx_data, but will check if there is a valid CRC in the
* end of the payload.
*
* @param data
* Pointer to the array in which to store the data.
*
* @param len
* Pointer to variable storing the data length.
*
* @param pipe
* Pointer to the pipe on which the data was received. Can be 0.
*
* @return
* 1: Read OK, more data to read.
* 0: Read OK
* -1: No RX data
* -2: Wrong length read. Something is likely wrong.
* -3: Data read, but CRC does not match.
*/
int rfhelp_read_rx_data_crc(char *data, int *len, int *pipe) {
int res = rfhelp_read_rx_data(data, len, pipe);
if (res >= 0 && *len > 2) {
unsigned short crc = crc16((unsigned char*)data, *len - 2);
if (crc != ((unsigned short) data[*len - 2] << 8 | (unsigned short) data[*len - 1])) {
res = -3;
}
}
*len -= 2;
return res;
}
/**
* Same as rfhelp_read_rx_data_crc, but for use with the corresponding
* nocak send function.
*
* @param data
* Pointer to the array in which to store the data.
*
* @param len
* Pointer to variable storing the data length.
*
* @param pipe
* Pointer to the pipe on which the data was received. Can be 0.
*
* @return
* 1: Read OK, more data to read.
* 0: Read OK
* -1: No RX data
* -2: Wrong length read. Something is likely wrong.
* -3: Data read, but CRC does not match.
* -4: Duplicate packet received.
*/
int rfhelp_read_rx_data_crc_noack(char *data, int *len, int *pipe) {
int res = rfhelp_read_rx_data(data, len, pipe);
static unsigned char counter = 0;
if (res >= 0 && *len > 3) {
unsigned short crc = crc16((unsigned char*)data, *len - 2);
if (crc != ((unsigned short) data[*len - 2] << 8 | (unsigned short) data[*len - 1])) {
res = -3;
} else {
unsigned char cnt = data[*len - 3];
if (cnt == counter) {
res = -4;
}
counter = cnt;
}
}
*len -= 3;
return res;
}
int rfhelp_rf_status(void) {
chMtxLock(&rf_mutex);
int s = rf_status();
chMtxUnlock(&rf_mutex);
return s;
}
void rfhelp_set_tx_addr(const char *addr, int addr_len) {
chMtxLock(&rf_mutex);
memcpy(tx_addr, addr, addr_len);
address_length = addr_len;
tx_pipe0_addr_eq = memcmp(rx_addr[0], tx_addr, address_length) == 0;
rf_set_tx_addr(tx_addr, address_length);
chMtxUnlock(&rf_mutex);
}
void rfhelp_set_rx_addr(int pipe, const char *addr, int addr_len) {
chMtxLock(&rf_mutex);
memcpy(rx_addr[pipe], addr, addr_len);
address_length = addr_len;
tx_pipe0_addr_eq = memcmp(rx_addr[0], tx_addr, address_length) == 0;
rx_addr_set[pipe] = true;
rf_set_rx_addr(pipe, addr, address_length);
chMtxUnlock(&rf_mutex);
}
void rfhelp_power_down(void) {
chMtxLock(&rf_mutex);
rf_power_down();
chMtxUnlock(&rf_mutex);
}
void rfhelp_power_up(void) {
chMtxLock(&rf_mutex);
rf_power_up();
chMtxUnlock(&rf_mutex);
}