-
Notifications
You must be signed in to change notification settings - Fork 28
/
hw41_D41.c
1822 lines (1633 loc) · 56.9 KB
/
hw41_D41.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
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* See license.txt for license information. */
//******************************************************************************
// WISP 4.1
// Device: MSP430-2132
//
// Version HW4.1_SW6.0
//
// Pinout:
// P1.1 = Data out signal
// P1.2 = Data in signal
// P1.3 = Data in enable
//
// P2.4 = Supervisor In
// P2.5 = Supervisor In
//
// Tested against Impinj v 3.0.2 reader, Miller-4 encodings
// Alien reader code is out of date
//
// This is a partial implementation of the RFID EPCGlobal C1G2 protocol
// for the WISP 4.1 hardware platform.
//
// What's missing:
// - SECURED and KILLED states.
// - No support for WRITE, KILL, LOCK, ACCESS, BLOCKWRITE and BLOCKERASE
// commands.
// - Commands with EBVs always assume that the field is 8 bits long.
// - SELECTS don't support truncation.
// - READs ignore membank, wordptr, and wordcount fields. (What READs do
// return is dependent on what application you have configured in step
// 1.)
// - I sometimes get erroneous-looking session values in QUERYREP
// commands. For the time being, I just parse the command as if the
// session value is the same as my previous_session value.
// - QUERYs use a pretty aggressive slotting algorithm to preserve power.
// See the comments in that function for details.
// - Session timeouts work differently than what's in table 6.15 of the
// spec. Here's what we do:
// SL comes up as not asserted, and persists as long as it's in ram
// retention mode or better.
// All inventory flags come up as 'A'. S0's inventory flag persists
// as long as it's in ram retention mode or better. S1's inventory
// flag persists as long as it's in an inventory round and in ram
// retention mode or better; otherwise it is reset to 'A' with every
// reset at the top of the while loop. S2's and S3's inventory flag
// is reset to 'A' with every reset at the top of the while loop.
//******************************************************************************
#if(WISP_VERSION != BLUE_WISP)
#error "WISP Version not supported"
#endif
#include "dlwisp41.h"
#include "rfid.h"
/*******************************************************************************
***************** Edit mywisp.h to configure this WISP **********************
******************************************************************************/
#include "mywisp.h"
// as per mapping in monitor code
#define wisp_debug_1 DEBUG_1_4 // P1.4
#define wisp_debug_2 DEBUG_2_3 // P2.3
#define wisp_debug_3 CLK_A // P3.0
#define wisp_debug_4 TX_A // P3.4
#define wisp_debug_5 RX_A // P3.5
#define MONITOR_DEBUG_ON 0
volatile unsigned char* destorig = &cmd[0]; // pointer to beginning of cmd
// #pragma data_alignment=2 is important in sendResponse() when the words are
// copied into arrays. Sometimes the compiler puts reply[0] on an odd address,
// which cannot be copied as a word and thus screws everything up.
#pragma data_alignment=2
// compiler uses working register 4 as a global variable
// Pointer to &cmd[bits]
volatile __no_init __regvar unsigned char* dest @ 4;
// compiler uses working register 5 as a global variable
// count of bits received from reader
volatile __no_init __regvar unsigned short bits @ 5;
unsigned short TRcal=0;
#if ENABLE_SESSIONS
// selected and session inventory flags
#define S0_INDEX 0x00
#define S1_INDEX 0x01
#define S2_INDEX 0x02
#define S3_INDEX 0x03
#define SL_ASSERTED 1
#define SL_NOT_ASSERTED 0
#define SESSION_STATE_A 0
#define SESSION_STATE_B 1
unsigned char SL;
unsigned char previous_session = 0x00;
unsigned char session_table[] = {
SESSION_STATE_A, SESSION_STATE_A,
SESSION_STATE_A, SESSION_STATE_A
};
#endif // ENABLE_SESSIONS
int i;
int main(void)
{
//*******************************Timer setup**********************************
WDTCTL = WDTPW + WDTHOLD; // Stop Watchdog Timer
P1SEL = 0;
P2SEL = 0;
P1IE = 0;
P1IFG = 0;
P2IFG = 0;
DRIVE_ALL_PINS // set pin directions correctly and outputs to low.
// Check power on bootup, decide to receive or sleep.
if(!is_power_good())
sleep();
RECEIVE_CLOCK;
/*
// already set.
#if MONITOR_DEBUG_ON
// for monitor - pin direction
P1DIR |= wisp_debug_1;
P2DIR |= wisp_debug_2;
P3DIR |= wisp_debug_3;
P3DIR |= wisp_debug_4;
P3DIR |= wisp_debug_5;
#endif
*/
#if DEBUG_PINS_ENABLED
#if USE_2132
DEBUG_PIN5_LOW;
// P3DIR |= (BIT5); // already set.
#else
P4DIR |= (BIT7+BIT5+BIT3);
#endif
#endif
#if ENABLE_SLOTS
// setup int epc
epc = ackReply[2]<<8;
epc |= ackReply[3];
// calculate RN16_1 table
for (Q = 0; Q < 16; Q++)
{
rn16 = epc^Q;
lfsr();
if (Q > 8)
{
RN16[(Q<<1)-9] = __swap_bytes(rn16);
RN16[(Q<<1)-8] = rn16;
}
else
{
RN16[Q] = rn16;
}
}
#endif
TACTL = 0;
// P1IES &= ~BIT2; // initial state is POS edge to find start of CW
// P1IFG = 0x00; // clear interrupt flag after changing edge trigger
asm("MOV #0000h, R9");
// dest = destorig;
#if READ_SENSOR
init_sensor();
#endif
#if !(ENABLE_SLOTS)
queryReplyCRC = crc16_ccitt(&queryReply[0],2);
queryReply[3] = (unsigned char)queryReplyCRC;
queryReply[2] = (unsigned char)__swap_bytes(queryReplyCRC);
#endif
#if SENSOR_DATA_IN_ID
// this branch is for sensor data in the id
ackReply[2] = SENSOR_DATA_TYPE_ID;
state = STATE_READ_SENSOR;
timeToSample++;
#else
ackReplyCRC = crc16_ccitt(&ackReply[0], 14);
ackReply[15] = (unsigned char)ackReplyCRC;
ackReply[14] = (unsigned char)__swap_bytes(ackReplyCRC);
#endif
#if ENABLE_SESSIONS
initialize_sessions();
#endif
//state = STATE_ARBITRATE;
state = STATE_READY;
#if MONITOR_DEBUG_ON
// for monitor - set STATE READY debug line - 00000 - 0
P1OUT |= wisp_debug_1;
P1OUT &= ~wisp_debug_1;
P2OUT &= ~wisp_debug_2;
#endif
setup_to_receive();
while (1)
{
// TIMEOUT! reset timer
if (TAR > 0x256 || delimiterNotFound) // was 0x1000
{
if(!is_power_good()) {
sleep();
}
#if MONITOR_DEBUG_ON
// for monitor - set TAR OVERFLOW debug line - 00111 - 7
if (!delimiterNotFound)
{
P1OUT |= wisp_debug_1;
P1OUT &= ~wisp_debug_1;
P2OUT &= ~wisp_debug_2;
P3OUT = 0x31;
}
#endif
#if SENSOR_DATA_IN_ID
// this branch is for sensor data in the id
if ( timeToSample++ == 10 ) {
state = STATE_READ_SENSOR;
timeToSample = 0;
}
#elif SENSOR_DATA_IN_READ_COMMAND
if ( timeToSample++ == 10 ) {
state = STATE_READ_SENSOR;
timeToSample = 0;
}
#else
#if !(ENABLE_READS)
if(!is_power_good())
sleep();
#endif
inInventoryRound = 0;
state = STATE_READY;
#if MONITOR_DEBUG_ON
// for monitor - set STATE READY debug line - 00000 - 0
P1OUT |= wisp_debug_1;
P1OUT &= ~wisp_debug_1;
P2OUT &= ~wisp_debug_2;
#endif
#endif
#if ENABLE_SESSIONS
handle_session_timeout();
#endif
#if ENABLE_SLOTS
if (shift < 4)
shift += 1;
else
shift = 0;
#endif
setup_to_receive();
}
switch (state)
{
case STATE_READY:
{
inInventoryRound = 0;
//////////////////////////////////////////////////////////////////////
// process the QUERY command
//////////////////////////////////////////////////////////////////////
if ( bits == NUM_QUERY_BITS && ( ( cmd[0] & 0xF0 ) == 0x80 ) )
{
#if MONITOR_DEBUG_ON
// for monitor - set QUERY PKT debug line - 01001 - 9
P1OUT |= wisp_debug_1;
P1OUT &= ~wisp_debug_1;
P2OUT |= wisp_debug_2;
P3OUT = 0x01;
#endif
//DEBUG_PIN5_HIGH;
handle_query(STATE_REPLY);
#if MONITOR_DEBUG_ON
// for monitor - set STATE REPLY debug line - 00010 - 2
P1OUT |= wisp_debug_1;
P1OUT &= ~wisp_debug_1;
P2OUT &= ~wisp_debug_2;
P3OUT = 0x10;
#endif
setup_to_receive();
}
//////////////////////////////////////////////////////////////////////
// process the SELECT command
//////////////////////////////////////////////////////////////////////
// @ short distance has slight impact on performance
else if ( bits >= 44 && ( ( cmd[0] & 0xF0 ) == 0xA0 ) )
{
#if MONITOR_DEBUG_ON
// for monitor - set QUERY_ADJ debug line - 01101 - 13
P1OUT |= wisp_debug_1;
P1OUT &= ~wisp_debug_1;
P2OUT |= wisp_debug_2;
P3OUT = 0x30;
#endif
//DEBUG_PIN5_HIGH;
handle_select(STATE_READY);
//DEBUG_PIN5_LOW;
delimiterNotFound = 1;
} // select command
//////////////////////////////////////////////////////////////////////
// got >= 22 bits, and it's not the beginning of a select. just reset.
//////////////////////////////////////////////////////////////////////
else if ( bits >= MAX_NUM_QUERY_BITS && ( ( cmd[0] & 0xF0 ) != 0xA0 ) )
{
do_nothing();
state = STATE_READY;
delimiterNotFound = 1;
#if MONITOR_DEBUG_ON
// for monitor - set debug line - 01110 - 14
P1OUT |= wisp_debug_1;
P1OUT &= ~wisp_debug_1;
P2OUT &= ~wisp_debug_2;
P3OUT = 0x30;
#endif
}
break;
}
case STATE_ARBITRATE:
{
//////////////////////////////////////////////////////////////////////
// process the QUERY command
//////////////////////////////////////////////////////////////////////
if ( bits == NUM_QUERY_BITS && ( ( cmd[0] & 0xF0 ) == 0x80 ) )
{
#if MONITOR_DEBUG_ON
// for monitor - set QUERY PKT debug line - 01001 - 9
P1OUT |= wisp_debug_1;
P1OUT &= ~wisp_debug_1;
P2OUT |= wisp_debug_2;
P3OUT = 0x01;
#endif
//DEBUG_PIN5_HIGH;
handle_query(STATE_REPLY);
#if MONITOR_DEBUG_ON
// for monitor - set STATE REPLY debug line - 00010 - 2
P1OUT |= wisp_debug_1;
P1OUT &= ~wisp_debug_1;
P2OUT &= ~wisp_debug_2;
P3OUT = 0x10;
#endif
setup_to_receive();
}
//////////////////////////////////////////////////////////////////////
// got >= 22 bits, and it's not the beginning of a select. just reset.
//////////////////////////////////////////////////////////////////////
//else if ( bits >= NUM_QUERY_BITS )
else if ( bits >= MAX_NUM_QUERY_BITS && ( ( cmd[0] & 0xF0 ) != 0xA0 ) )
{
//DEBUG_PIN5_HIGH;
do_nothing();
state = STATE_READY;
delimiterNotFound = 1;
#if MONITOR_DEBUG_ON
// for monitor - set DELIMITER NOT FOUND debug line - 00110 - 6
P1OUT |= wisp_debug_1;
P1OUT &= ~wisp_debug_1;
P2OUT &= ~wisp_debug_2;
P3OUT = 0x30;
#endif
//DEBUG_PIN5_LOW;
}
// this state handles query, queryrep, queryadjust, and select commands.
//////////////////////////////////////////////////////////////////////
// process the QUERYREP command
//////////////////////////////////////////////////////////////////////
else if ( bits == NUM_QUERYREP_BITS && ( ( cmd[0] & 0x06 ) == 0x00 ) )
{
#if MONITOR_DEBUG_ON
// for monitor - set QUERY_REP debug line - 01100 - 12
P1OUT |= wisp_debug_1;
P1OUT &= ~wisp_debug_1;
P2OUT |= wisp_debug_2;
P3OUT = 0x20;
#endif
//DEBUG_PIN5_HIGH;
handle_queryrep(STATE_REPLY);
//DEBUG_PIN5_LOW;
//setup_to_receive();
delimiterNotFound = 1;
#if MONITOR_DEBUG_ON
// for monitor - set DELIMITER NOT FOUND debug line - 00110 - 6
P1OUT |= wisp_debug_1;
P1OUT &= ~wisp_debug_1;
P2OUT &= ~wisp_debug_2;
P3OUT = 0x30;
#endif
} // queryrep command
//////////////////////////////////////////////////////////////////////
// process the QUERYADJUST command
//////////////////////////////////////////////////////////////////////
else if ( bits == NUM_QUERYADJ_BITS && ( ( cmd[0] & 0xF8 ) == 0x48 ) )
{
// at short distance, you get better performance (~52 t/s) if you
// do setup_to_receive() rather than dnf =1. not sure that this holds
// true at distance though - need to recheck @ 2-3 ms.
//DEBUG_PIN5_HIGH;
#if MONITOR_DEBUG_ON
// for monitor - set QUERY_ADJ debug line - 01101 - 13
P1OUT |= wisp_debug_1;
P1OUT &= ~wisp_debug_1;
P2OUT |= wisp_debug_2;
P3OUT = 0x21;
#endif
handle_queryadjust(STATE_REPLY);
//DEBUG_PIN5_LOW;
setup_to_receive();
//delimiterNotFound = 1;
} // queryadjust command
//////////////////////////////////////////////////////////////////////
// process the SELECT command
//////////////////////////////////////////////////////////////////////
// @ short distance has slight impact on performance
else if ( bits >= 44 && ( ( cmd[0] & 0xF0 ) == 0xA0 ) )
{
#if MONITOR_DEBUG_ON
// for monitor - set SELECT debug line - 01110 - 14
P1OUT |= wisp_debug_1;
P1OUT &= ~wisp_debug_1;
P2OUT |= wisp_debug_2;
P3OUT = 0x30;
#endif
//DEBUG_PIN5_HIGH;
handle_select(STATE_READY);
//DEBUG_PIN5_LOW;
delimiterNotFound = 1;
#if MONITOR_DEBUG_ON
// for monitor - set DELIMITER NOT FOUND debug line - 00110 - 6
P1OUT |= wisp_debug_1;
P1OUT &= ~wisp_debug_1;
P2OUT &= ~wisp_debug_2;
P3OUT = 0x30;
#endif
} // select command
break;
}
case STATE_REPLY:
{
// this state handles query, query adjust, ack, and select commands
///////////////////////////////////////////////////////////////////////
// process the ACK command
///////////////////////////////////////////////////////////////////////
if ( bits == NUM_ACK_BITS && ( ( cmd[0] & 0xC0 ) == 0x40 ) )
{
#if MONITOR_DEBUG_ON
// for monitor - set ACK PKT debug line - 01010 - 10
P1OUT |= wisp_debug_1;
P1OUT &= ~wisp_debug_1;
P2OUT |= wisp_debug_2;
P3OUT = 0x10;
#endif
#if ENABLE_READS
handle_ack(STATE_ACKNOWLEDGED);
setup_to_receive();
#elif SENSOR_DATA_IN_ID
handle_ack(STATE_ACKNOWLEDGED);
delimiterNotFound = 1; // reset
#else
// this branch for hardcoded query/acks
handle_ack(STATE_ACKNOWLEDGED);
//delimiterNotFound = 1; // reset
setup_to_receive();
#endif
#if MONITOR_DEBUG_ON
// for monitor - set STATE ACK debug line - 00011 - 3
P1OUT |= wisp_debug_1;
P1OUT &= ~wisp_debug_1;
P2OUT &= ~wisp_debug_2;
P3OUT = 0x11;
#endif
}
//////////////////////////////////////////////////////////////////////
// process the QUERY command
//////////////////////////////////////////////////////////////////////
if ( bits == NUM_QUERY_BITS && ( ( cmd[0] & 0xF0 ) == 0x80 ) )
{
#if MONITOR_DEBUG_ON
// for monitor - set QUERY PKT debug line - 01001 - 9
P1OUT |= wisp_debug_1;
P1OUT &= ~wisp_debug_1;
P2OUT |= wisp_debug_2;
P3OUT = 0x01;
#endif
// i'm supposed to stay in state_reply when I get this, but if I'm
// running close to 1.8v then I really need to reset and get in the
// sleep, which puts me back into state_arbitrate. this is complete
// a violation of the protocol, but it sure does make everything
// work better. - polly 8/9/2008
//DEBUG_PIN5_HIGH;
handle_query(STATE_REPLY);
//DEBUG_PIN5_LOW;
//delimiterNotFound = 1;
setup_to_receive();
}
//////////////////////////////////////////////////////////////////////
// process the QUERYREP command
//////////////////////////////////////////////////////////////////////
else if ( bits == NUM_QUERYREP_BITS && ( ( cmd[0] & 0x06 ) == 0x00 ) )
{
#if MONITOR_DEBUG_ON
// for monitor - set QUERY_REP debug line - 01100 - 12
P1OUT |= wisp_debug_1;
P1OUT &= ~wisp_debug_1;
P2OUT |= wisp_debug_2;
P3OUT = 0x20;
#endif
//DEBUG_PIN5_HIGH;
do_nothing();
state = STATE_ARBITRATE;
#if MONITOR_DEBUG_ON
// for monitor - set STATE ARBITRATE debug line - 00001 - 1
P1OUT |= wisp_debug_1;
P1OUT &= ~wisp_debug_1;
P2OUT &= ~wisp_debug_2;
P3OUT = 0x01;
#endif
//handle_queryrep(STATE_ARBITRATE);
//DEBUG_PIN5_LOW;
setup_to_receive();
//delimiterNotFound = 1; // reset
} // queryrep command
//////////////////////////////////////////////////////////////////////
// process the QUERYADJUST command
//////////////////////////////////////////////////////////////////////
else if (bits == NUM_QUERYADJ_BITS && ( ( cmd[0] & 0xF8 ) == 0x48 ))
{
#if MONITOR_DEBUG_ON
// for monitor - set QUERY_ADJ debug line - 01101 - 13
P1OUT |= wisp_debug_1;
P1OUT &= ~wisp_debug_1;
P2OUT |= wisp_debug_2;
P3OUT = 0x21;
#endif
//DEBUG_PIN5_HIGH;
handle_queryadjust(STATE_REPLY);
//DEBUG_PIN5_LOW;
//setup_to_receive();
delimiterNotFound = 1;
#if MONITOR_DEBUG_ON
// for monitor - set DELIMITER NOT FOUND debug line - 00110 - 6
P1OUT |= wisp_debug_1;
P1OUT &= ~wisp_debug_1;
P2OUT &= ~wisp_debug_2;
P3OUT = 0x30;
#endif
} // queryadjust command
//////////////////////////////////////////////////////////////////////
// process the SELECT command
//////////////////////////////////////////////////////////////////////
else if ( bits >= 44 && ( ( cmd[0] & 0xF0 ) == 0xA0 ) )
{
#if MONITOR_DEBUG_ON
// for monitor - set SELECT debug line - 01110 - 14
P1OUT |= wisp_debug_1;
P1OUT &= ~wisp_debug_1;
P2OUT |= wisp_debug_2;
P3OUT = 0x30;
#endif
//DEBUG_PIN5_HIGH;
handle_select(STATE_READY);
//DEBUG_PIN5_LOW;
delimiterNotFound = 1;
#if MONITOR_DEBUG_ON
// for monitor - set DELIMITER NOT FOUND debug line - 00110 - 6
P1OUT |= wisp_debug_1;
P1OUT &= ~wisp_debug_1;
P2OUT &= ~wisp_debug_2;
P3OUT = 0x30;
#endif
//setup_to_receive();
} // select command
else if ( bits >= MAX_NUM_QUERY_BITS && ( ( cmd[0] & 0xF0 ) != 0xA0 ) &&
( ( cmd[0] & 0xF0 ) != 0x80 ) )
{
//DEBUG_PIN5_HIGH;
do_nothing();
state = STATE_READY;
delimiterNotFound = 1;
//DEBUG_PIN5_LOW;
}
break;
}
case STATE_ACKNOWLEDGED:
{
// responds to query, ack, request_rn cmds
// takes action on queryrep, queryadjust, and select cmds
/////////////////////////////////////////////////////////////////////
// process the REQUEST_RN command
//////////////////////////////////////////////////////////////////////
if ( bits >= NUM_REQRN_BITS && ( cmd[0] == 0xC1 ) )
{
#if 1
#if MONITOR_DEBUG_ON
// for monitor - set REQ_RN debug line - 01011 - 11
P1OUT |= wisp_debug_1;
P1OUT &= ~wisp_debug_1;
P2OUT |= wisp_debug_2;
P3OUT = 0x11;
#endif
DEBUG_PIN5_HIGH;
handle_request_rn(STATE_OPEN);
DEBUG_PIN5_LOW;
setup_to_receive();
#else
handle_request_rn(STATE_READY);
delimiterNotFound = 1;
#if MONITOR_DEBUG_ON
// for monitor - set DELIMITER NOT FOUND debug line - 00110 - 6
P1OUT |= wisp_debug_1;
P1OUT &= ~wisp_debug_1;
P2OUT &= ~wisp_debug_2;
P3OUT = 0x30;
#endif
#endif
}
#if 1
//////////////////////////////////////////////////////////////////////
// process the QUERY command
//////////////////////////////////////////////////////////////////////
if ( bits == NUM_QUERY_BITS && ( ( cmd[0] & 0xF0 ) == 0x80 ) )
{
#if MONITOR_DEBUG_ON
// for monitor - set QUERY PKT debug line - 01001 - 9
P1OUT |= wisp_debug_1;
P1OUT &= ~wisp_debug_1;
P2OUT |= wisp_debug_2;
P3OUT = 0x01;
#endif
//DEBUG_PIN5_HIGH;
handle_query(STATE_REPLY);
//DEBUG_PIN5_LOW;
delimiterNotFound = 1;
#if MONITOR_DEBUG_ON
// for monitor - set DELIMITER NOT FOUND debug line - 00110 - 6
P1OUT |= wisp_debug_1;
P1OUT &= ~wisp_debug_1;
P2OUT &= ~wisp_debug_2;
P3OUT = 0x30;
#endif
//setup_to_receive();
}
///////////////////////////////////////////////////////////////////////
// process the ACK command
///////////////////////////////////////////////////////////////////////
// this code doesn't seem to get exercised in the real world. if i ever
// ran into a reader that generated an ack in an acknowledged state,
// this code might need some work.
//else if ( bits == 20 && ( ( cmd[0] & 0xC0 ) == 0x40 ) )
else if ( bits == NUM_ACK_BITS && ( ( cmd[0] & 0xC0 ) == 0x40 ) )
{
#if MONITOR_DEBUG_ON
// for monitor - set ACK PKT debug line - 01010 - 10
P1OUT |= wisp_debug_1;
P1OUT &= ~wisp_debug_1;
P2OUT |= wisp_debug_2;
P3OUT = 0x10;
#endif
//DEBUG_PIN5_HIGH;
handle_ack(STATE_ACKNOWLEDGED);
#if MONITOR_DEBUG_ON
// for monitor - set STATE ACK debug line - 00011 - 3
P1OUT |= wisp_debug_1;
P1OUT &= ~wisp_debug_1;
P2OUT &= ~wisp_debug_2;
P3OUT = 0x11;
#endif
//DEBUG_PIN5_LOW;
setup_to_receive();
}
//////////////////////////////////////////////////////////////////////
// process the QUERYREP command
//////////////////////////////////////////////////////////////////////
else if ( bits == NUM_QUERYREP_BITS && ( ( cmd[0] & 0x06 ) == 0x00 ) )
{
// in the acknowledged state, rfid chips don't respond to queryrep
// commands
do_nothing();
state = STATE_READY;
delimiterNotFound = 1;
} // queryrep command
//////////////////////////////////////////////////////////////////////
// process the QUERYADJUST command
//////////////////////////////////////////////////////////////////////
else if ( bits == NUM_QUERYADJ_BITS && ( ( cmd[0] & 0xF8 ) == 0x48 ) )
{
//DEBUG_PIN5_HIGH;
do_nothing();
state = STATE_READY;
delimiterNotFound = 1;
//DEBUG_PIN5_LOW;
} // queryadjust command
//////////////////////////////////////////////////////////////////////
// process the SELECT command
//////////////////////////////////////////////////////////////////////
else if ( bits >= 44 && ( ( cmd[0] & 0xF0 ) == 0xA0 ) )
{
//DEBUG_PIN5_HIGH;
handle_select(STATE_READY);
delimiterNotFound = 1;
//DEBUG_PIN5_LOW;
} // select command
//////////////////////////////////////////////////////////////////////
// process the NAK command
//////////////////////////////////////////////////////////////////////
else if ( bits >= 10 && ( cmd[0] == 0xC0 ) )
//else if ( bits >= NUM_NAK_BITS && ( cmd[0] == 0xC0 ) )
{
//DEBUG_PIN5_HIGH;
do_nothing();
state = STATE_ARBITRATE;
delimiterNotFound = 1;
//DEBUG_PIN5_LOW;
}
//////////////////////////////////////////////////////////////////////
// process the READ command
//////////////////////////////////////////////////////////////////////
// warning: won't work for read addrs > 127d
if ( bits == NUM_READ_BITS && ( cmd[0] == 0xC2 ) )
{
//DEBUG_PIN5_HIGH;
handle_read(STATE_ARBITRATE);
state = STATE_ARBITRATE;
delimiterNotFound = 1 ;
//DEBUG_PIN5_LOW;
}
// FIXME: need write, kill, lock, blockwrite, blockerase
//////////////////////////////////////////////////////////////////////
// process the ACCESS command
//////////////////////////////////////////////////////////////////////
if ( bits >= 56 && ( cmd[0] == 0xC6 ) )
{
//DEBUG_PIN5_HIGH;
do_nothing();
state = STATE_ARBITRATE;
delimiterNotFound = 1 ;
//DEBUG_PIN5_LOW;
}
#endif
else if ( bits >= MAX_NUM_READ_BITS )
{
//DEBUG_PIN5_HIGH;
//do_nothing();
state = STATE_ARBITRATE;
delimiterNotFound = 1 ;
//DEBUG_PIN5_LOW;
}
#if 0
// kills performance ...
else if ( bits >= 44 )
{
do_nothing();
state = STATE_ARBITRATE;
delimiterNotFound = 1;
}
#endif
break;
}
case STATE_OPEN:
{
// responds to query, ack, req_rn, read, write, kill, access,
// blockwrite, and blockerase cmds
// processes queryrep, queryadjust, select cmds
//////////////////////////////////////////////////////////////////////
// process the READ command
//////////////////////////////////////////////////////////////////////
// warning: won't work for read addrs > 127d
if ( bits == NUM_READ_BITS && ( cmd[0] == 0xC2 ) )
{
//DEBUG_PIN5_HIGH;
handle_read(STATE_OPEN);
//DEBUG_PIN5_LOW;
// note: setup_to_receive() et al handled in handle_read
}
//////////////////////////////////////////////////////////////////////
// process the REQUEST_RN command
//////////////////////////////////////////////////////////////////////
else if ( bits >= NUM_REQRN_BITS && ( cmd[0] == 0xC1 ) )
//else if ( bits >= 30 && ( cmd[0] == 0xC1 ) )
{
handle_request_rn(STATE_OPEN);
setup_to_receive();
//delimiterNotFound = 1; // more bad reads??
}
//////////////////////////////////////////////////////////////////////
// process the QUERY command
//////////////////////////////////////////////////////////////////////
if ( bits == NUM_QUERY_BITS && ( ( cmd[0] & 0xF0 ) == 0x80 ) )
{
handle_query(STATE_REPLY);
//setup_to_receive();
delimiterNotFound = 1;
}
//////////////////////////////////////////////////////////////////////
// process the QUERYREP command
//////////////////////////////////////////////////////////////////////
else if ( bits == NUM_QUERYREP_BITS && ( ( cmd[0] & 0x06 ) == 0x00 ) )
{
//DEBUG_PIN5_HIGH;
do_nothing();
state = STATE_READY;
//delimiterNotFound = 1;
setup_to_receive();
//DEBUG_PIN5_LOW;
} // queryrep command
//////////////////////////////////////////////////////////////////////
// process the QUERYADJUST command
//////////////////////////////////////////////////////////////////////
//else if ( bits == NUM_QUERYADJ_BITS && ( ( cmd[0] & 0xF0 ) == 0x90 ) )
else if ( bits == 9 && ( ( cmd[0] & 0xF8 ) == 0x48 ) )
{
//DEBUG_PIN5_HIGH;
do_nothing();
state = STATE_READY;
delimiterNotFound = 1;
//DEBUG_PIN5_LOW;
} // queryadjust command
///////////////////////////////////////////////////////////////////////
// process the ACK command
///////////////////////////////////////////////////////////////////////
else if ( bits == NUM_ACK_BITS && ( ( cmd[0] & 0xC0 ) == 0x40 ) )
{
//DEBUG_PIN5_HIGH;
handle_ack(STATE_OPEN);
//setup_to_receive();
delimiterNotFound = 1;
//DEBUG_PIN5_LOW;
}
//////////////////////////////////////////////////////////////////////
// process the SELECT command
//////////////////////////////////////////////////////////////////////
else if ( bits >= 44 && ( ( cmd[0] & 0xF0 ) == 0xA0 ) )
{
handle_select(STATE_READY);
delimiterNotFound = 1;
} // select command
//////////////////////////////////////////////////////////////////////
// process the NAK command
//////////////////////////////////////////////////////////////////////
//else if ( bits >= NUM_NAK_BITS && ( cmd[0] == 0xC0 ) )
else if ( bits >= 10 && ( cmd[0] == 0xC0 ) )
{
handle_nak(STATE_ARBITRATE);
delimiterNotFound = 1;
}
break;
}
case STATE_READ_SENSOR:
{
#if SENSOR_DATA_IN_READ_COMMAND
read_sensor(&readReply[0]);
// crc is computed in the read state
RECEIVE_CLOCK;
state = STATE_READY;
delimiterNotFound = 1; // reset
#elif SENSOR_DATA_IN_ID
read_sensor(&ackReply[3]);
RECEIVE_CLOCK;
ackReplyCRC = crc16_ccitt(&ackReply[0], 14);
ackReply[15] = (unsigned char)ackReplyCRC;
ackReply[14] = (unsigned char)__swap_bytes(ackReplyCRC);
state = STATE_READY;
delimiterNotFound = 1; // reset
#endif
break;
} // end case
} // end switch
} // while loop
}
//************************** SETUP TO RECEIVE *********************************
// note: port interrupt can also reset, but it doesn't call this function
// because function call causes PUSH instructions prior to bit read
// at beginning of interrupt, which screws up timing. so, remember
// to change things in both places.
inline void setup_to_receive()
{
//P4OUT &= ~BIT3;
_BIC_SR(GIE); // temporarily disable GIE so we can sleep and enable interrupts
// at the same time
P1OUT |= RX_EN_PIN;
delimiterNotFound = 0;
// setup port interrupt on pin 1.2
P1SEL &= ~BIT2; //Disable TimerA2, so port interrupt can be used
// Setup timer. It has to setup because there is no setup time after done with
// port1 interrupt.
TACTL = 0;
TAR = 0;
TACCR0 = 0xFFFF; // Set up TimerA0 register as Max
TACCTL0 = 0;
TACCTL1 = SCS + CAP; //Synchronize capture source and capture mode
TACTL = TASSEL1 + MC1 + TAIE; // SMCLK and continuous mode and Timer_A
// interrupt enabled.
// initialize bits
bits = 0;
// initialize dest
dest = destorig; // = &cmd[0]
// clear R6 bits of word counter from prior communications to prevent dest++
// on 1st port interrupt
asm("CLR R6");
P1IE = 0;
P1IES &= ~RX_PIN; // Make positive edge for port interrupt to detect start of
// delimiter
P1IFG = 0; // Clear interrupt flag
P1IE |= RX_PIN; // Enable Port1 interrupt
_BIS_SR(LPM4_bits | GIE);
return;
}
inline void sleep()
{
P1OUT &= ~RX_EN_PIN;
#if MONITOR_DEBUG_ON
// for monitor - set SLEEP debug line - 01000 - 8
P1OUT |= wisp_debug_1;
P1OUT &= ~wisp_debug_1;
P2OUT |= wisp_debug_2;
P3OUT = 0x00;
#endif
// enable port interrupt for voltage supervisor
P2IES = 0;
P2IFG = 0;
P2IE |= VOLTAGE_SV_PIN;
P1IE = 0;
P1IFG = 0;
TACTL = 0;
_BIC_SR(GIE); // temporarily disable GIE so we can sleep and enable interrupts
// at the same time
P2IE |= VOLTAGE_SV_PIN; // Enable Port2 interrupt
if (is_power_good())
P2IFG = VOLTAGE_SV_PIN;