-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathMRF24J40.c
701 lines (564 loc) · 13.8 KB
/
MRF24J40.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
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
/*
* Copyright (C) 2011, Alex Hornung
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
* DEALINGS IN THE SOFTWARE.
*/
#include "hal_pic18.h"
#include "MRF24J40.h"
#include "ieee802154.h"
static unsigned char seq_no = 0;
static int internal_state = 0;
static unsigned char
SPI_READ_LONG(int addr)
{
unsigned char a_msb, a_lsb, d;
a_msb = a_lsb = 0;
a_msb = (((addr >> 3) & 0x7F) | 0x80);
a_lsb = (((addr & 0x07) << 5));
CS_LOW();
spi_write(a_msb);
spi_write(a_lsb);
d = spi_read();
CS_HIGH();
return d;
}
static unsigned char
SPI_READ_SHORT(unsigned char addr)
{
unsigned char d;
addr &= 0x3f; /* Trim address to 6 bits */
addr <<= 1; /* Shift into the right position */
addr &= ~0x01; /* Clear write bit */
CS_LOW();
spi_write(addr);
d = spi_read();
CS_HIGH();
return d;
}
static void
SPI_WRITE_LONG(int addr, unsigned char d)
{
unsigned char a_msb, a_lsb;
a_msb = a_lsb = 0;
a_msb = (((addr >> 3) & 0x7F) | 0x80);
a_lsb = (((addr & 0x07) << 5) | (1<<4));
CS_LOW();
spi_write(a_msb);
spi_write(a_lsb);
spi_write(d);
CS_HIGH();
}
static void
SPI_WRITE_SHORT(unsigned char addr, unsigned char d)
{
addr &= 0x3f; /* Trim address to 6 bits */
addr <<= 1; /* Shift into the right position */
addr |= 0x01; /* Set R/W bit to write */
CS_LOW();
spi_write(addr);
spi_write(d);
CS_HIGH();
}
static void
SPI_WRITE_FIFO(int addr, unsigned char *d, int len)
{
int i;
for (i = 0; i < len; i++) {
SPI_WRITE_LONG(addr++, d[i]);
}
}
static void
SPI_READ_FIFO(int addr, unsigned char *d, int len)
{
int i;
for (i = 0; i < len; i++) {
d[i] = SPI_READ_LONG(addr++);
}
}
void
mrf24j40_ie(void)
{
/*
* Very intuitively IE (interrupt enable) set to 1
* causes the interrupt to be disabled...
*/
SPI_WRITE_SHORT(INTCON, ~(TXNIE | RXIE | SECIE));
}
void
mrf24j40_pwr_reset(void)
{
SPI_WRITE_SHORT(SOFTRST, RSTPWR);
}
void
mrf24j40_bb_reset(void)
{
SPI_WRITE_SHORT(SOFTRST, RSTBB);
}
void
mrf24j40_mac_reset(void)
{
/* NOTE: All control registers are reset by this! */
internal_state = 0;
SPI_WRITE_SHORT(SOFTRST, RSTMAC);
}
void
mrf24j40_rf_reset(void)
{
unsigned char old = SPI_READ_SHORT(RFCTL);
SPI_WRITE_SHORT(RFCTL, old | RFRST);
SPI_WRITE_SHORT(RFCTL, old & ~RFRST);
DELAY_1MS(); /* Delay min 192us */
}
void
mrf24j40_rxfifo_flush(void)
{
SPI_WRITE_SHORT(RXFLUSH, _RXFLUSH);
}
void
mrf24j40_set_channel(int ch)
{
/* translate channel */
/* 0x00 -> Ch 11 */
/* 0x01 -> Ch 12 */
/* 0x0F -> Ch 26 */
if (ch >= 11)
ch -= 11;
SPI_WRITE_LONG(RFCON0, CHANNEL(ch) | RFOPT(0x03));
mrf24j40_rf_reset();
}
unsigned char
mrf24j40_get_channel(void)
{
return (11 + (SPI_READ_LONG(RFCON0) >> 4));
}
void
mrf24j40_set_promiscuous(int crc_check)
{
unsigned char w;
/*
* Set promiscuous mode, disable auto-ACK and, if requested,
* accept packets with a CRC error.
*/
w = NOACKRSP;
if (!crc_check)
w |= ERRPKT;
else
w |= PROMI;
SPI_WRITE_SHORT(RXMCR, w);
}
void
mrf24j40_set_coordinator(void)
{
SPI_WRITE_SHORT(RXMCR, SPI_READ_SHORT(RXMCR) | PANCOORD);
}
void
mrf24j40_clear_coordinator(void)
{
SPI_WRITE_SHORT(RXMCR, SPI_READ_SHORT(RXMCR) & ~PANCOORD);
}
void
mrf24j40_set_pan(int pan)
{
SPI_WRITE_SHORT(PANIDH, pan>>8);
SPI_WRITE_SHORT(PANIDL, pan & 0xFF);
}
void
mrf24j40_set_short_addr(int addr)
{
SPI_WRITE_SHORT(SADRH, addr>>8);
SPI_WRITE_SHORT(SADRL, addr & 0xFF);
}
void
mrf24j40_init(int ch)
{
RESET_LOW();
internal_state = 0;
DELAY_1MS();
RESET_HIGH();
DELAY_1MS();
SPI_WRITE_SHORT(SOFTRST, (RSTPWR | RSTBB | RSTMAC));
while ((SPI_READ_SHORT(SOFTRST) & (RSTPWR | RSTBB | RSTMAC)) != 0);
DELAY_1MS();
/* initialization sequence as suggested in datasheet */
SPI_WRITE_SHORT(PACON2, SPI_READ_SHORT(PACON2) | FIFOEN);
SPI_WRITE_SHORT(TXSTBL, RFSTBL(9) | MSIFS(5));
SPI_WRITE_LONG(RFCON0, CHANNEL(ch) | RFOPT(0x03));
SPI_WRITE_LONG(RFCON1, VCOOPT(0x02));
SPI_WRITE_LONG(RFCON2, PLLEN);
SPI_WRITE_LONG(RFCON6, TXFIL);
SPI_WRITE_LONG(RFCON7, SLPCLKSEL(0x02));
SPI_WRITE_LONG(RFCON8, RFVCO);
SPI_WRITE_LONG(SLPCON0, INTEDGE); /* Set Rising Edge INT Polarity */
SPI_WRITE_LONG(SLPCON1, SLPCLKDIV(1) | CLKOUTDIS);
/* Carrier Sense with energy above threshold */
SPI_WRITE_SHORT(BBREG2, CCAMODE(0x03) | CCASTH(0x02));
SPI_WRITE_SHORT(CCAEDTH, 0x60);
/* Flush RX FIFO */
SPI_WRITE_SHORT(RXFLUSH, _RXFLUSH);
/* Enable interrupts */
mrf24j40_ie();
mrf24j40_rf_reset();
}
void
mrf24j40_sleep(int spi_wake)
{
unsigned char r;
/* Enable immediate wakeup */
SPI_WRITE_SHORT(WAKECON, IMMWAKE);
r = SPI_READ_SHORT(SLPACK);
/*
* If we are using pin wakeup instead of spi wakeup, set up the
* wake pin
*/
if(!spi_wake) {
WAKE_LOW();
/* Enable WAKE pin, and set polarity to active high */
SPI_WRITE_SHORT(RXFLUSH, SPI_READ_SHORT(RXFLUSH) |
WAKEPAD | WAKEPOL);
}
mrf24j40_pwr_reset();
SPI_WRITE_SHORT(SLPACK, r | _SLPACK);
}
void
mrf24j40_wakeup(int spi_wake)
{
if (spi_wake) {
/* Wake up on register by setting and then clearing REGWAKE */
SPI_WRITE_SHORT(WAKECON, REGWAKE);
SPI_WRITE_SHORT(WAKECON, 0);
} else {
/* Wake up by asserting the wake pin */
WAKE_HIGH();
}
mrf24j40_rf_reset();
}
void
mrf24j40_set_encdec(int types, int mode, unsigned char *key, int klen)
{
unsigned char *keyp;
unsigned char w;
int addr;
int len;
w = SPI_READ_SHORT(SECCON0);
if (types & MRF24J40_TX_KEY) {
addr = SECKTXNFIFO;
len = klen;
keyp = key;
while (len-- > 0)
SPI_WRITE_LONG(addr++, *keyp++);
w |= TXNCIPHER(mode);
SPI_WRITE_SHORT(SECCON0, w);
}
if (types & MRF24J40_RX_KEY) {
addr = SECKRXFIFO;
len = klen;
keyp = key;
while (len-- > 0)
SPI_WRITE_LONG(addr++, *keyp++);
w |= RXCIPHER(mode);
SPI_WRITE_SHORT(SECCON0, w);
}
}
void
mrf24j40_txpkt_raw(unsigned char *frame, int hdr_len, int frame_len, int enc)
{
unsigned char w;
int addr = TXNFIFO;
internal_state = 0;
/* Request ACK */
SPI_WRITE_SHORT(TXNCON, SPI_READ_SHORT(TXNCON) | TXNACKREQ);
/* Write the header and total frame length into the TXNFIFO */
SPI_WRITE_LONG(addr++, hdr_len);
SPI_WRITE_LONG(addr++, frame_len);
/* Write the frame (header + payload) into the TXNFIFO */
while (frame_len-- > 0)
SPI_WRITE_LONG(addr++, *frame++);
w = SPI_READ_SHORT(TXNCON);
w &= ~(TXNSECEN);
if (enc)
w |= TXNSECEN;
/* Trigger transmission */
SPI_WRITE_SHORT(TXNCON, w | TXNTRIG);
}
void
mrf24j40_txpkt_trigger(void)
{
unsigned char w;
internal_state = 0;
w = SPI_READ_SHORT(TXNCON);
w &= ~(TXNSECEN);
SPI_WRITE_SHORT(TXNCON, w | TXNTRIG);
}
/*
* mrf24j40_txpkt sends a packet with the following frame header format:
*
* - src PAN == dst PAN (FCPANCOMP)
* - short src and dst addresses
* - ACK requested (FCREQACK)
* - type is set to data (FCFRTYP_DATA)
*
*/
void
mrf24j40_txpkt(unsigned short dest, unsigned char *pkt, int payload_len, int enc)
{
struct ieee802_15_4_MAChdr pkt_header;
unsigned char w;
int hlen = 0;
int flen = 0;
int addr = TXNFIFO;
int i;
internal_state = 0;
hlen = sizeof(pkt_header);
flen += hlen;
flen += len;
/* Request ACK */
SPI_WRITE_SHORT(TXNCON, SPI_READ_SHORT(TXNCON) | TXNACKREQ);
/* Populate the header as described in the comment above */
pkt_header.dest_addr = (dest << 8) | (dest >> 8);
pkt_header.src_addr = ((short)SPI_READ_SHORT(SADRH) << 8) |
SPI_READ_SHORT(SADRL);
pkt_header.dest_pan = ((short)SPI_READ_SHORT(PANIDH) << 8) |
SPI_READ_SHORT(PANIDL);
pkt_header.seq_no = seq_no++;
pkt_header.fc_low = FCFRTYP(FCFRTYP_DATA) | FCREQACK | FCPANCOMP;
pkt_header.fc_high = FCDADDRM(FCADDR_SHORT) | FCFRVER(0) |
FCSADDRM(FCADDR_SHORT);
/* Write the header and total frame length into the TXNFIFO */
SPI_WRITE_LONG(addr++, hlen);
SPI_WRITE_LONG(addr++, flen);
/* Write the header into the TXNFIFO */
for (i = 0; i < sizeof(pkt_header); i++) {
SPI_WRITE_LONG(addr++, ((unsigned char *)&pkt_header)[i]);
}
/* Write the payload into the TXNFIFO */
for (i = 0; i < len; i++) {
SPI_WRITE_LONG(addr++, pkt[i]);
}
w = SPI_READ_SHORT(TXNCON);
if (enc)
w |= TXNSECEN;
/* Trigger transmission */
SPI_WRITE_SHORT(TXNCON, w | TXNTRIG);
}
int
mrf24j40_txpkt_intcb(void)
{
unsigned char stat = SPI_READ_SHORT(TXSTAT);
if (stat & TXNSTAT) {
if (stat & CCAFAIL)
return EBUSY;
else
return EIO;
} else {
return 0;
}
}
int
mrf24j40_sec_intcb(int accept)
{
unsigned char w;
w = SPI_READ_SHORT(SECCON0);
w &= ~(SECSTART | SECIGNORE);
if (accept) {
SPI_WRITE_SHORT(SECCON0, w | SECSTART);
} else {
SPI_WRITE_SHORT(SECCON0, w | SECIGNORE);
SPI_WRITE_SHORT(RXFLUSH, _RXFLUSH);
}
}
int
mrf24j40_check_rx_dec(int no_err_flush)
{
int err;
err = (SPI_READ_SHORT(RXSR) & SECDECERR) ? EIO : 0;
if (err && !no_err_flush)
SPI_WRITE_SHORT(RXFLUSH, _RXFLUSH);
return err;
}
int
mrf24j40_rxpkt_intcb(unsigned char *d, int len, unsigned char *plqi,
unsigned char *prssi)
{
int flen;
int addr = RXFIFO;
unsigned char lqi, rssi;
/* Disable receiving more packets */
SPI_WRITE_SHORT(BBREG1, SPI_READ_SHORT(BBREG1) | RXDECINV);
/* Read frame length */
flen = SPI_READ_LONG(addr++);
*d++ = flen;
/* Check whether the provided buffer is large enough */
if (flen > len) {
/* Re-enable packet reception */
SPI_WRITE_SHORT(BBREG1, SPI_READ_SHORT(BBREG1) & ~RXDECINV);
return ENOMEM;
}
/* Read out frame */
while (flen-- > 0)
*d++ = SPI_READ_LONG(addr++);
lqi = SPI_READ_LONG(addr++);
rssi = SPI_READ_LONG(addr++);
if (plqi != (void *)0)
*plqi = lqi;
if (prssi != (void *)0)
*prssi = rssi;
/*
* Flush RX FIFO (silicon errata #1 workaround, strictly
* speaking only needed if using promiscuous mode).
*/
SPI_WRITE_SHORT(RXFLUSH, _RXFLUSH);
/* Re-enable packet reception */
SPI_WRITE_SHORT(BBREG1, SPI_READ_SHORT(BBREG1) & ~RXDECINV);
return 0;
}
int
mrf24j40_rxpkt_part_intcb(unsigned char *d, int len, int flags,
unsigned char *plqi, unsigned char *prssi)
{
static int flen;
static int addr;
unsigned char lqi, rssi;
/* Abort; flush and re-enable reception */
if (flags & MRF24J40_PART_RX_ABORT) {
/* Flush RX FIFO */
SPI_WRITE_SHORT(RXFLUSH, _RXFLUSH);
/* Re-enable packet reception */
SPI_WRITE_SHORT(BBREG1, SPI_READ_SHORT(BBREG1) & ~RXDECINV);
return -1;
}
/* First chunk; disable packet reception and read frame length */
if (flags & MRF24J40_PART_RX_FIRST) {
/* Disable receiving more packets */
SPI_WRITE_SHORT(BBREG1, SPI_READ_SHORT(BBREG1) | RXDECINV);
addr = RXFIFO;
flen = SPI_READ_LONG(addr++);
/* Account for frame len */
--len;
*d++ = flen;
}
if (flen < len)
len = flen;
/* Adjust remaining frame length */
flen -= len;
/* Read out frame */
while (len-- > 0)
*d++ = SPI_READ_LONG(addr++);
/* Have we finished reading the frame? */
if (flen == 0) {
lqi = SPI_READ_LONG(addr++);
rssi = SPI_READ_LONG(addr++);
if (plqi != (void *)0)
*plqi = lqi;
if (prssi != (void *)0)
*prssi = rssi;
/*
* Flush RX FIFO (silicon errata #1 workaround, strictly
* speaking only needed if using promiscuous mode).
*/
SPI_WRITE_SHORT(RXFLUSH, _RXFLUSH);
/* Re-enable packet reception */
SPI_WRITE_SHORT(BBREG1, SPI_READ_SHORT(BBREG1) & ~RXDECINV);
}
return flen;
}
int
mrf24j40_check_enc(void)
{
return mrf24j40_txpkt_intcb();
}
int
mrf24j40_check_dec(void)
{
unsigned char w;
int error;
if ((error = mrf24j40_txpkt_intcb()) != 0)
return error;
/* NOT REACHED */
w = SPI_READ_SHORT(RXSR);
return (w & UPSECERR) ? EIO : 0;
}
int
mrf24j40_int_tasks(void)
{
unsigned char stat;
int ret = 0;
/* Read INTSTAT register; this clears the interrupt flags */
stat = SPI_READ_SHORT(INTSTAT);
/* Check which interrupts occured and set return value accordingly */
if (stat & RXIF) {
ret |= MRF24J40_INT_RX;
}
if (stat & TXNIF) {
switch (internal_state) {
MRF24J40_STATE_UPENC:
ret |= MRF24J40_INT_ENC;
internal_state = 0;
break;
MRF24J40_STATE_UPDEC:
ret |= MRF24J40_INT_DEC;
internal_state = 0;
break;
default:
ret |= MRF24J40_INT_TX;
}
}
if (stat & SECIF) {
ret |= MRF24J40_INT_SEC;
}
return ret;
}
/*
* NOTE: header length can be a maximum of 31 bytes due to a hardware
* limitation.
*/
void
mrf24j40_encdec(unsigned char *nonce, int nonce_len, unsigned char *frame,
int hdr_len, int frame_len, int enc)
{
int addr;
/* Upper layer encryption / decryption */
if (enc)
internal_state = MRF24J40_STATE_UPENC;
else
internal_state = MRF24J40_STATE_UPDEC;
/*
* Load the 13-byte NONCE into UPNONCE...
*/
addr = UPNONCE0;
while (nonce_len-- > 0)
SPI_WRITE_LONG(addr++, *nonce++);
/*
* Enable upper layer encryption UPENC in SECCR2.
*/
if (enc)
SPI_WRITE_SHORT(SECCR2, UPENC);
else
SPI_WRITE_SHORT(SECCR2, UPDEC);
/*
* Encrypt frame by setting TXNTRIG and TXNSECEN
*/
mrf24j40_txpkt_raw(frame, hdr_len, frame_len, enc);
/*
* TXNIF interrupt issued when encryption or decryption complete.
* Check TXNSTAT for error; for decryption also check UPSECERR for
* MIC (Message Integrity Check).
*
* TXNFIFO contains encrypted/decrypted frame.
*/
}