-
Notifications
You must be signed in to change notification settings - Fork 0
/
rfidr_state.c
2161 lines (1777 loc) · 141 KB
/
rfidr_state.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
//////////////////////////////////////////////////////////////////////////////////
// //
// Module : RFIDr Firmware State //
// //
// Filename: rfidr_state.c //
// Creation Date: circa 10/31/2016 //
// Author: Edward Keehr //
// //
// Copyright 2021 Superlative Semiconductor LLC //
// //
// Licensed under the Apache License, Version 2.0 (the "License"); //
// you may not use this file except in compliance with the License. //
// You may obtain a copy of the License at //
// //
// http://www.apache.org/licenses/LICENSE-2.0 //
// //
// Unless required by applicable law or agreed to in writing, software //
// distributed under the License is distributed on an "AS IS" BASIS, //
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. //
// See the License for the specific language governing permissions and //
// limitations under the License. //
// //
// Description: //
// //
// This file contains the main application-level behavior of the RFIDr MCU //
// firmware, including the state machine that gets executed whenever the //
// iDevice software prompts for a state transition. //
// //
// Revisions: //
// 061619 - Major commentary cleanup. //
// 121219 - Major code cleanup to remove repeated code in several "states". //
// Now each state core has its own function which is called in the "state". //
// Support for frequency hopping, PDOA ranging, and kill functionality are //
// added in. Have only one error handling function now that checks state. //
// Also, more robust programming and searching/programming last inventoried //
// flag functionalities are added in. //
// //
//////////////////////////////////////////////////////////////////////////////////
#include "app_error.h"
#include "app_util_platform.h"
#include "ble_rfidrs.h"
#include "nordic_common.h"
#include "nrf_adc.h"
#include "nrf_delay.h"
#include "nrf_error.h"
#include "rfidr_error.h"
#include "rfidr_gpio.h"
#include "rfidr_rxradio.h"
#include "rfidr_spi.h"
#include "rfidr_state.h"
#include "rfidr_sx1257.h"
#include "rfidr_txradio.h"
#include "rfidr_user.h"
#include "rfidr_waveform.h"
#include <math.h>
#include <string.h>
#include <stdio.h>
//Define state variables that need to persist between calls to functions in this file.
//Also, only allow these state variables to be accessible to functions in this file.
static rfidr_state_t m_rfidr_state = IDLE_UNCONFIGURED; //Current state
static rfidr_state_t m_rfidr_state_next = IDLE_UNCONFIGURED; //Transition to next state when we can
static bool m_received_irq_flag = false;
static bool m_received_hvc_read_state_flag = false;
static bool m_received_hvc_pckt_data1_flag = false;
static bool m_dtc_state_flag = false;
static bool m_track_tag_state_flag = false;
static bool m_adc_returned_flag = false; //To be set to true when adc returns data.
static uint16_t m_num_inv_tags_found = 0;
static uint8_t m_return_state_code = 0;
static uint8_t m_hopskip_nonce = 0; //A number to help the iDevice keep track of which hop/skip pairs are associated.
static uint16_t m_last_adc_sample = 0;
//This function is used for transferring local state information to the iDevice over BTLE.
//After reviewing the code, it seems as if m_return_state code doesn't really need to be a state variable.
//This can be cleaned up in the next major code overhaul.
static uint8_t * decode_rfidr_state(rfidr_state_t rfidr_state)
{
switch(rfidr_state)
{
case(IDLE_UNCONFIGURED): m_return_state_code=0; break;
case(IDLE_CONFIGURED): m_return_state_code=1; break;
case(INITIALIZING): m_return_state_code=2; break;
case(SEARCHING_APP_SPECD_TAG): m_return_state_code=3; break; //These were all renumbered on 11/21 so we need to also update iPhone app.
case(SEARCHING_LAST_INV_TAG): m_return_state_code=4; break;
case(INVENTORYING): m_return_state_code=5; break;
case(TESTING_DTC): m_return_state_code=6; break;
case(PROGRAMMING_APP_SPECD_TAG): m_return_state_code=7; break;
case(PROGRAMMING_LAST_INV_TAG): m_return_state_code=8; break;
case(RECOVERING_WAVEFORM_MEMORY): m_return_state_code=9; break;
case(RESET_SX1257_AND_FPGA): m_return_state_code=10; break;
case(KILL_TAG): m_return_state_code=11; break;
case(PROGRAMMING_KILL_PASSWD): m_return_state_code=12; break;
case(TRACK_APP_SPECD_TAG): m_return_state_code=13; break;
case(TRACK_LAST_INV_TAG): m_return_state_code=14; break;
default: m_return_state_code=99; break;
}
return &m_return_state_code;
}
//This function sends a generic log message to the iDevice as a notification.
//Such a notification may take a while to send over a number of packets.
//This notification also stretches out over 3 connection intervals.
static void send_log_message(ble_rfidrs_t *p_rfidrs, char * inputString)
{
char rfidr_log_message[256] = {0};
uint16_t cnt = 0;
uint32_t nrf_error_code = NRF_SUCCESS;
//Truncate the input string short of its maximum length, leaving room for some of the null characters at the end to guarantee string termination.
while (*(inputString+cnt) && cnt < 240)
{
rfidr_log_message[cnt]=*(inputString+cnt); cnt++;
}
nrf_error_code=rfidr_error_complete_message_send(p_rfidrs,(uint8_t *)rfidr_log_message);
if (nrf_error_code != NRF_ERROR_INVALID_STATE)
{
//APP_ERROR_CHECK(nrf_error_code);
}
}
//This function sends a generic log message to the iDevice as a notification.
//Such a notification will send quickly over one packet (20 bytes).
static void send_short_message(ble_rfidrs_t *p_rfidrs, char * inputString)
{
char rfidr_log_message[20] = {0};
uint16_t cnt = 0;
uint32_t nrf_error_code = NRF_SUCCESS;
while (*(inputString+cnt) && cnt < 20)
{
rfidr_log_message[cnt]=*(inputString+cnt); cnt++;
}
nrf_error_code=rfidr_error_complete_message_send(p_rfidrs,(uint8_t *)rfidr_log_message);
if (nrf_error_code != NRF_ERROR_INVALID_STATE)
{
//APP_ERROR_CHECK(nrf_error_code);
}
}
//This function is called during the startup sequence in main().
//Here, we set all of the state variables to default values.
void rfidr_state_init(void)
{
m_rfidr_state = IDLE_UNCONFIGURED; //State variable to hold the current state information.
m_rfidr_state_next = IDLE_UNCONFIGURED; //State variable to hold the state to transition to the next time run_rfidr_state_machine is called.
m_received_irq_flag = false; //Indicates when an IRQ has been received from the FPGA.
m_received_hvc_read_state_flag = false; //Indicates when an indication ACK has been received on the BTLE "Read State" characteristic.
m_received_hvc_pckt_data1_flag = false; //Indicates when an indication ACK has been received on the BTLE "Data 1" characteristic.
m_dtc_state_flag = false; //Indicates when we are being kept in the DTC state to operate the reader in testing mode.
m_track_tag_state_flag = false; //Indicates when we are being kept in a tag tracking state to continually track a set of tags.
m_adc_returned_flag = false; //Indicates whether the ADC has returned a value or not.
m_num_inv_tags_found = 0; //Keep track of how many tags are found in an inventory - to be used while tracking tags.
m_return_state_code = 0; //Not really used, we can delete this on the next major code overhaul.
m_hopskip_nonce = 0; //We will increment each time we hop frequencies but not skip frequencies.
m_last_adc_sample = 0; //Retain the ADC sample for separate processing by IRQ handler and TX offset calibration algorithm.
//We keep setting of the hopskip nonce in this file to minimize message passing and also to retain flexibility of implementation.
}
void update_adc_sample(int32_t adc_sample)
{
m_adc_returned_flag = true;
m_last_adc_sample = (uint16_t)(adc_sample & 0x0000FFFF); //Let's cast this 10-bit unsigned value to something appropriate
}
//This function sets the "received irq" state variable denoting when the MCU has received an IRQ from the FPGA.
//This function should be called from the IRQ handler assigned to the PIN connected to the FPGA IRQ output.
void rfidr_state_received_irq(void)
{
m_received_irq_flag = true;
}
//This function sets a state variable flag denoting that the NRF BTLE SoftDevice has received an indication ACK from the iDevice for a read state characteristic write.
void rfidr_state_received_read_state_confirmation(void)
{
m_received_hvc_read_state_flag = true;
}
//This function sets a state variable flag denoting that the NRF BTLE SoftDevice has received an indication ACK from the iDevice for a packet data 1 characteristic write.
void rfidr_state_received_pckt_data1_confirmation(void)
{
m_received_hvc_pckt_data1_flag = true;
}
//This function informs the iDevice of any state transition and hence serves as a "bookend" function in the run_rfidr_state_machine function
static void rfidr_state_bookend_function(ble_rfidrs_t *p_rfidrs)
{
uint32_t nrf_error_code = NRF_SUCCESS;
m_received_hvc_read_state_flag = false;
nrf_error_code=ble_rfidrs_read_state_send(p_rfidrs,decode_rfidr_state(m_rfidr_state),BLE_RFIDRS_READ_STATE_CHAR_LEN);
if (nrf_error_code != NRF_ERROR_INVALID_STATE){APP_ERROR_CHECK(nrf_error_code);}
while(m_received_hvc_read_state_flag == false){}
}
//We got an error - send a message to the iDevice, shut down the PA to avoid damage, and return to unconfigured state.
//The intent is to go back to the unconfigured state if we get an error during initialization.
static void handle_error(ble_rfidrs_t *p_rfidrs, char * input_string_outer, char * input_string_inner, rfidr_error_t rfidr_error_code)
{
char rfidr_outer_sanitize[128] = {0};
char rfidr_inner_sanitize[128] = {0};
char rfidr_error_message[256] = {0};
uint16_t cnt = 0;
uint32_t nrf_error_code = NRF_SUCCESS;
//Truncate the input string short of its maximum length, leaving room for some of the null characters at the end to guarantee string termination.
cnt=0;
while (*(input_string_outer+cnt) && cnt < 116)
{
rfidr_outer_sanitize[cnt]=*(input_string_outer+cnt); cnt++;
}
cnt=0;
while (*(input_string_inner+cnt) && cnt < 116)
{
rfidr_inner_sanitize[cnt]=*(input_string_inner+cnt); cnt++;
}
rfidr_disable_pa(); //Don't check for error, just do it. We already got an error!
sprintf(rfidr_error_message,"Error at %s: %s: %02d",rfidr_outer_sanitize,rfidr_inner_sanitize,(uint8_t)rfidr_error_code);
nrf_error_code=rfidr_error_complete_message_send(p_rfidrs,(uint8_t *)rfidr_error_message);
//If we got an error while in tracking or DTC modes, we are exiting said state and need
//to set the state flag accordingly.
m_track_tag_state_flag = false;
m_dtc_state_flag = false;
if (nrf_error_code != NRF_ERROR_INVALID_STATE)
{
//APP_ERROR_CHECK(nrf_error_code);
}
if(m_rfidr_state==IDLE_UNCONFIGURED || m_rfidr_state==INITIALIZING){
rfidr_disable_led1(); //Keep LED disabled to show that the reader not configured.
m_rfidr_state_next=IDLE_UNCONFIGURED;
m_rfidr_state=IDLE_UNCONFIGURED;
} else {
rfidr_enable_led1(); //Keep LED enabled so show that the reader is configured.
m_rfidr_state_next=IDLE_CONFIGURED;
m_rfidr_state=IDLE_CONFIGURED;
}
rfidr_state_bookend_function(p_rfidrs);
}
//This function is called by the BTLE write state characteristic event handler in main.c.
//This function is ensured to be called prior to running the state machine itself, which is picked up by the main loop
//as soon as this function has completed.
uint32_t write_rfidr_state_next(ble_rfidrs_t *p_rfidrs, rfidr_state_t l_rfidr_state_next)
{
//Modus operandi of this function is to check for an illegal state transaction request from the app and
//ignore it. This will also be enforced on the app side. App side should figure out the problem by reading the state.
//In other words, this function protects the hardware from a bad app.
uint32_t nrf_error_code = NRF_SUCCESS;
switch(l_rfidr_state_next){
case INITIALIZING:
if(m_rfidr_state==IDLE_UNCONFIGURED) //If the app tries to initialize the state machine while in the unconfigured state, great! Do it!
{
m_rfidr_state_next = INITIALIZING;
}
else if(m_rfidr_state==IDLE_CONFIGURED) //If the app tries to initialize the state machine while in the configured state, don't change the state.
{
m_rfidr_state_next = m_rfidr_state;
}
else
{
m_rfidr_state_next = m_rfidr_state; //If the app tries to initialize the state machine while in any other state, don't change the state.
}
break;
case SEARCHING_APP_SPECD_TAG:
case SEARCHING_LAST_INV_TAG:
case INVENTORYING:
case PROGRAMMING_APP_SPECD_TAG:
case PROGRAMMING_LAST_INV_TAG:
case KILL_TAG:
case PROGRAMMING_KILL_PASSWD:
case RECOVERING_WAVEFORM_MEMORY:
case TESTING_DTC:
if(m_rfidr_state==IDLE_CONFIGURED)
{
m_rfidr_state_next = l_rfidr_state_next; //Safely transition to another state if we are not in the middle of one.
} //Note that the TRACK and DTC states require the app to toggle them in
else //and out of the state. These are the only states that "stick" between app accesses.
{
m_rfidr_state_next = m_rfidr_state; //Otherwise (for example we are in the middle of a state), stay in the same state.
}
break;
case TRACK_APP_SPECD_TAG:
if(m_rfidr_state==IDLE_CONFIGURED) //If we are configured, toggle the tracking state true.
{ //and enter the tracking state when the state machine is run.
m_track_tag_state_flag = true;
m_rfidr_state_next = l_rfidr_state_next;
}
else if(m_rfidr_state==TRACK_APP_SPECD_TAG)
{
m_track_tag_state_flag = false; //If we get a state request from the same state while we are in the state,
m_rfidr_state_next = m_rfidr_state; //we need to toggle out of it. So keep the same state variable, but set
} //the toggle flag to false.
else
{
m_rfidr_state_next = m_rfidr_state; //Otherwise, stay in the same state.
}
break;
case TRACK_LAST_INV_TAG:
if(m_rfidr_state==IDLE_CONFIGURED) //If we are configured, toggle the tracking state true.
{ //and enter the tracking state when the state machine is run.
m_track_tag_state_flag = true;
m_rfidr_state_next = l_rfidr_state_next;
}
else if(m_rfidr_state==TRACK_LAST_INV_TAG)
{
m_track_tag_state_flag = false; //If we get a state request from the same state while we are in the state,
m_rfidr_state_next = m_rfidr_state; //we need to toggle out of it. So keep the same state variable, but set
} //the toggle flag to false.
else
{
m_rfidr_state_next = m_rfidr_state; //Otherwise, stay in the same state.
}
break;
case RESET_SX1257_AND_FPGA: //Want to be able to get reset if we're in an "IDLE" state.
if(m_rfidr_state==IDLE_CONFIGURED || m_rfidr_state==IDLE_UNCONFIGURED || m_rfidr_state==TESTING_DTC || m_rfidr_state==TRACK_APP_SPECD_TAG || TRACK_LAST_INV_TAG)
{
m_rfidr_state_next = l_rfidr_state_next;
}
else
{
m_rfidr_state_next = m_rfidr_state; //We do not want to reset if the MCU is in the middle of an operational state.
}
break;
default: break; //Do nothing
}
return nrf_error_code;
}
//This function is called by the BTLE read state characteristic.
uint32_t read_rfidr_state(rfidr_state_t * p_rfidr_state)
{
*p_rfidr_state = m_rfidr_state;
return NRF_SUCCESS;
}
//These functions below calibrate the transmit offset in the I/Q DACs of the SX1257.
//We do this by setting registers in the FPGA, which then alter the 1-bit waveforms being sent to the DACs.
//We run this routine when we are sure that the FPGA is putting all "zeros" through the DACs.
//In this fashion, minimizing the RF output power is tantamount to maximizing the modulation depth of
//of the reader when we are doing DSB-ASK modulation.
static rfidr_error_t tx_offset_calibration_core(ble_rfidrs_t *p_rfidrs,uint8_t loop_tx_cal_sdm,uint8_t loop_tx_cal_zgn)
{
rfidr_error_t rfidr_error_code = RFIDR_SUCCESS;
rfidr_error_code = set_tx_sdm_offset(loop_tx_cal_sdm);
if(rfidr_error_code != RFIDR_SUCCESS){handle_error(p_rfidrs,"tx cal, set sdm offset","",rfidr_error_code); return rfidr_error_code;}
rfidr_error_code = set_tx_zgn_offset(loop_tx_cal_zgn);
if(rfidr_error_code != RFIDR_SUCCESS){handle_error(p_rfidrs,"tx cal, set zgn offset","",rfidr_error_code); return rfidr_error_code;}
nrf_delay_us(100);
rfidr_enable_pa();
nrf_delay_us(800);
m_adc_returned_flag = false;
nrf_adc_start();
while(m_adc_returned_flag == false){}
rfidr_disable_pa();
return RFIDR_SUCCESS;
}
//Note that the MAX2204 power detector takes about 1ms to completely settle, which will dictate how fast we can
//actuate the algorithm.
//Also the FCC requirement to hop every 400ms means that we need to complete this operation within 400ms.
//Luckily it appears we have time to do this, but not with a lot of margin.
//We won't use a full gradient descent scheme since we really need to find the minimum value to ensure maximum modulation depth.
//We'll just brute force the approach since we should have just enough time to do this.
static rfidr_error_t tx_offset_calibration_brute_force(ble_rfidrs_t *p_rfidrs)
{
rfidr_error_t rfidr_error_code = RFIDR_SUCCESS;
uint8_t loop_tx_cal_sdm = 0;
uint8_t loop_tx_cal_zgn = 0;
uint8_t sdm_offset_best = 8;
uint8_t zgn_offset_best = 8;
uint16_t meas_power_best = 9999;
uint8_t recover_frequency_slot = 12;
char short_message[20] = {0};
rfidr_error_code=hop_sx1257_frequency(&recover_frequency_slot); m_hopskip_nonce++;
//rfidr_error_code=set_sx1257_frequency(12); m_hopskip_nonce++;
if(rfidr_error_code != RFIDR_SUCCESS){handle_error(p_rfidrs,"tx cal, freq. hop.","",rfidr_error_code); return rfidr_error_code;}
rfidr_disable_pa();
for (loop_tx_cal_sdm = 0; loop_tx_cal_sdm < 16; loop_tx_cal_sdm++)
{
for (loop_tx_cal_zgn = 0; loop_tx_cal_zgn < 16; loop_tx_cal_zgn++)
{
rfidr_error_code = tx_offset_calibration_core(p_rfidrs,loop_tx_cal_sdm,loop_tx_cal_zgn);
if(rfidr_error_code != RFIDR_SUCCESS){handle_error(p_rfidrs,"tx cal, tx cal core","",rfidr_error_code); return rfidr_error_code;}
//sprintf(short_message,"CAL S:%02d Z:%02d O:%04d",loop_tx_cal_sdm,loop_tx_cal_zgn,m_last_adc_sample);
//send_short_message(p_rfidrs, short_message);
if(m_last_adc_sample < meas_power_best)
{
meas_power_best = m_last_adc_sample;
sdm_offset_best = loop_tx_cal_sdm;
zgn_offset_best = loop_tx_cal_zgn;
}
}
}
rfidr_disable_pa();
rfidr_error_code = set_tx_sdm_offset(sdm_offset_best);
if(rfidr_error_code != RFIDR_SUCCESS){handle_error(p_rfidrs,"tx cal, final set sdm offset","",rfidr_error_code); return rfidr_error_code;}
rfidr_error_code = set_tx_zgn_offset(zgn_offset_best);
if(rfidr_error_code != RFIDR_SUCCESS){handle_error(p_rfidrs,"tx cal, final set zgn offset","",rfidr_error_code); return rfidr_error_code;}
sprintf(short_message,"CAL S:%02d Z:%02d O:%04d",sdm_offset_best,zgn_offset_best,meas_power_best);
send_short_message(p_rfidrs, short_message);
return rfidr_error_code;
}
static rfidr_error_t tx_offset_calibration_gradient(ble_rfidrs_t *p_rfidrs)
{
#define MAX_GRAD_CAL_ITERS 96
rfidr_error_t rfidr_error_code = RFIDR_SUCCESS;
int8_t sdm_val_curr = 10;
int8_t zgn_val_curr = 10;
int8_t sdm_val_dir = 1;
int8_t zgn_val_dir = 1;
uint16_t result_curr = 9999;
bool sdm_p_improve = false;
bool sdm_n_improve = false;
bool zgn_p_improve = false;
bool zgn_n_improve = false;
uint8_t recover_frequency_slot = 12;
uint8_t iter_curr = 0;
char short_message[20] = {0};
rfidr_error_code=hop_sx1257_frequency(&recover_frequency_slot); m_hopskip_nonce++;
//rfidr_error_code=set_sx1257_frequency(12); m_hopskip_nonce++;
if(rfidr_error_code != RFIDR_SUCCESS){handle_error(p_rfidrs,"tx cal, freq. hop.","",rfidr_error_code); return rfidr_error_code;}
while(iter_curr++ <= MAX_GRAD_CAL_ITERS)
{
sprintf(short_message,"S:%02d Z:%02d O:%03d I:%02d",(uint8_t)(sdm_val_curr%16),(uint8_t)(sdm_val_curr%16),result_curr,iter_curr);
send_short_message(p_rfidrs, short_message);
//Assuming "dir" is still the correct representation of the gradient, continue down the gradient.
rfidr_error_code = tx_offset_calibration_core(p_rfidrs,(uint8_t)((sdm_val_curr+sdm_val_dir)%16),(uint8_t)((zgn_val_curr+zgn_val_dir)%16));
if(rfidr_error_code != RFIDR_SUCCESS){handle_error(p_rfidrs,"tx cal, gradient step","",rfidr_error_code); return rfidr_error_code;}
//If indeed we are still going down the gradient, update values. Don't change "dir" (the representation of the gradient).
if(m_last_adc_sample < result_curr)
{
result_curr = m_last_adc_sample;
sdm_val_curr = (sdm_val_curr+sdm_val_dir)%16;
zgn_val_curr = (zgn_val_curr+zgn_val_dir)%16;
continue; //Move to the next iteration of the loop.
}
else //We need to reestablish the gradient.
{
rfidr_error_code = tx_offset_calibration_core(p_rfidrs,(uint8_t)((sdm_val_curr+1)%16),(uint8_t)((zgn_val_curr+0)%16));
if(rfidr_error_code != RFIDR_SUCCESS){handle_error(p_rfidrs,"tx cal, ip grad test","",rfidr_error_code); return rfidr_error_code;}
sdm_p_improve = m_last_adc_sample < result_curr;
rfidr_error_code = tx_offset_calibration_core(p_rfidrs,(uint8_t)((sdm_val_curr-1)%16),(uint8_t)((zgn_val_curr+0)%16));
if(rfidr_error_code != RFIDR_SUCCESS){handle_error(p_rfidrs,"tx cal, im grad test","",rfidr_error_code); return rfidr_error_code;}
sdm_n_improve = m_last_adc_sample < result_curr;
rfidr_error_code = tx_offset_calibration_core(p_rfidrs,(uint8_t)((sdm_val_curr+0)%16),(uint8_t)((zgn_val_curr+1)%16));
if(rfidr_error_code != RFIDR_SUCCESS){handle_error(p_rfidrs,"tx cal, qp grad test","",rfidr_error_code); return rfidr_error_code;}
zgn_p_improve = m_last_adc_sample < result_curr;
rfidr_error_code = tx_offset_calibration_core(p_rfidrs,(uint8_t)((sdm_val_curr+0)%16),(uint8_t)((zgn_val_curr-1)%16));
if(rfidr_error_code != RFIDR_SUCCESS){handle_error(p_rfidrs,"tx cal, qm grad test","",rfidr_error_code); return rfidr_error_code;}
zgn_n_improve = m_last_adc_sample < result_curr;
if(sdm_p_improve)
sdm_val_dir = 1;
else if (sdm_n_improve)
sdm_val_dir = -1;
else
sdm_val_dir = 0;
if(zgn_p_improve)
zgn_val_dir = 1;
else if (zgn_n_improve)
zgn_val_dir = -1;
else
zgn_val_dir = 0;
if(sdm_val_dir == 0 && zgn_val_dir == 0)
break; //Break out of the loop. We've found the local minimum.
else
{
rfidr_error_code = tx_offset_calibration_core(p_rfidrs,(uint8_t)((sdm_val_curr+sdm_val_dir)%16),(uint8_t)((zgn_val_curr+zgn_val_dir)%16));
if(rfidr_error_code != RFIDR_SUCCESS){handle_error(p_rfidrs,"tx cal, gradient reestablish","",rfidr_error_code); return rfidr_error_code;}
result_curr = m_last_adc_sample;
sdm_val_curr = (sdm_val_curr+sdm_val_dir)%16;
zgn_val_curr = (zgn_val_curr+zgn_val_dir)%16;
continue; //Move to the next iteration of the loop.
//We assume that this operation did in fact reduce the error.
}
}
}
rfidr_disable_pa();
rfidr_error_code = set_tx_sdm_offset((uint8_t)(sdm_val_curr%16));
if(rfidr_error_code != RFIDR_SUCCESS){handle_error(p_rfidrs,"tx cal, final set sdm offset","",rfidr_error_code); return rfidr_error_code;}
rfidr_error_code = set_tx_zgn_offset((uint8_t)(zgn_val_curr%16));
if(rfidr_error_code != RFIDR_SUCCESS){handle_error(p_rfidrs,"tx cal, final set zgn offset","",rfidr_error_code); return rfidr_error_code;}
sprintf(short_message,"S:%02d Z:%02d O:%03d I:%02d",(uint8_t)(sdm_val_curr%16),(uint8_t)(sdm_val_curr%16),result_curr,iter_curr);
send_short_message(p_rfidrs, short_message);
return rfidr_error_code;
}
//This function performs the core initialization routine as directed by the iDevice app.
static rfidr_error_t initialization_core(ble_rfidrs_t *p_rfidrs, char *error_info)
{
rfidr_error_t rfidr_error_code = RFIDR_SUCCESS;
uint8_t blank_epc[MAX_EPC_LENGTH_IN_BYTES] = {0};
//GPIOTE is not initialized here but is initialized as part of the top level main entry
//SPI MASTER is not initialized here but is initialized as part of the top level main entry
//Note to self - we will need some way to debug if we get an error and we are not plugged into
//Most of this code has always been part of the initialization routine.
//Here we ensure an orderly power on.
rfidr_disable_pa(); //Make sure the PA is off until everything else is up.
//rfidr_disable_xo();
rfidr_enable_xo();
nrf_delay_ms(100);
rfidr_reset_fpga();
rfidr_reset_radio(); //was just enable the radio but it should come up already
nrf_delay_ms(100);
//rfidr_enable_xo(); //Maybe we don't actually want to be enabling the XO after the FPGA is pulled out of reset
rfidr_txradio_init(); //This function sets up TX RADIO state variables within the MCU firmware.
set_app_specd_target_epc(p_rfidrs,blank_epc,MAX_EPC_LENGTH_IN_BYTES); //This function sets up a target EPC within the MCU firmware for SELECT-based tag SEARCH operations.
set_app_specd_program_epc(p_rfidrs,blank_epc); //This function sets up a new EPC for programming onto a tag during PROGRAM operations.
rfidr_error_code=load_sx1257_default(); //This function sets up the registers in the SX1257. We try to shake around the PLL a bit to help it converge properly.
if(rfidr_error_code != RFIDR_SUCCESS){handle_error(p_rfidrs,error_info,"load sx1257 default",rfidr_error_code); return RFIDR_ERROR_GENERAL;}
rfidr_sel_ant0(); //Once again we default to ant0 on 11/21/19
rfidr_error_code=load_rfidr_rxram_default(); //Load RX RAM. At this point, loading data consists of expected receive packet lengths.
if(rfidr_error_code != RFIDR_SUCCESS){handle_error(p_rfidrs,error_info,"load rfidr_rxram_default",rfidr_error_code); return RFIDR_ERROR_GENERAL;}
rfidr_error_code=load_rfidr_txram_default(); //Load TX RAM. This loads all of the default TX packet opcodes into the FPGA TX RAM.
if(rfidr_error_code != RFIDR_SUCCESS){handle_error(p_rfidrs,error_info,"load_rfidr_txram_default",rfidr_error_code); return RFIDR_ERROR_GENERAL;}
rfidr_error_code=set_sx1257_frequency(12);
if(rfidr_error_code != RFIDR_SUCCESS){handle_error(p_rfidrs,error_info,"set frequency",rfidr_error_code); return RFIDR_ERROR_GENERAL;}
//Check that clk36 from the SX1257 is valid. This may be currently disabled to save LUT, as it never really failed.
//If it is disabled, this may need to be revisited on account of seeming clk36-related initialization failures on the FPGA.
if(!is_clk_36_valid()){handle_error(p_rfidrs,error_info,"clk 36 not valid: ",rfidr_error_code); return RFIDR_ERROR_GENERAL;}
//Set clk36 inside the FPGA to run. This may be currently disabled to save LUT, as it never really failed.
//If it is disabled, this may need to be revisited on account of seeming clk36-related initialization failures on the FPGA.
rfidr_error_code=set_clk_36_oneshot();
if(rfidr_error_code != RFIDR_SUCCESS){handle_error(p_rfidrs,error_info,"set_clk_36_oneshot",rfidr_error_code); return RFIDR_ERROR_GENERAL;}
//check that clk36 is indeed running, wait a bit before we do this.
if(!is_clk_36_running()){handle_error(p_rfidrs,error_info,"clk 36 not running",rfidr_error_code); return RFIDR_ERROR_GENERAL;}
//enable the irq - I don't think that we need to do this explicitly
rfidr_error_code = tx_offset_calibration_brute_force(p_rfidrs);
//rfidr_error_code = tx_offset_calibration_gradient(p_rfidrs);
if(rfidr_error_code != RFIDR_SUCCESS){handle_error(p_rfidrs,error_info,"tx offset cal",rfidr_error_code); return RFIDR_ERROR_GENERAL;}
//set_tx_sdm_offset(5); //Set offsets for TX to maximize modulation depth of the reader TX waveforms.
//set_tx_zgn_offset(10);
rfidr_enable_led1(); //Enabled LED1 to show that we are entering a configured state.
send_log_message(p_rfidrs,"Initialization function complete!");
return rfidr_error_code;
}
static rfidr_error_t search_core(ble_rfidrs_t *p_rfidrs, char *error_info, rfidr_query_session_t session, rfidr_target_epc_t target_epc, return_epc_t return_epc, return_mag_t return_mag, return_lna_gain_t return_lna_gain, rfidr_return_t *return_struct)
{
//Mode types:
//SEARCH_TARGET: Use main antenna, use user-specified target epc.
//SEARCH_LAST: Use main antenna, use last epc obtained via inventorying (useful for searching on tag that hasn't been programmed).
//SEARCH_CAL: Use alternate antenna, use epc consistent with dummy tag for phase calibration.
//SEARCH_PLL: Use alternate antenna, use all-zeros epc.
//We offer a-la-carte return of data since querying FPGA memory over the SPI bus takes time and slows down the read rate.
rfidr_error_t rfidr_error_code = RFIDR_SUCCESS;
uint8_t loop_iq = 0;
uint8_t loop_load = 0;
char short_message[20] = {0};
//Set return variable to default values
return_struct->i_pass=false;
return_struct->q_pass=false;
for(loop_load=0; loop_load<MAX_EPC_LENGTH_IN_BYTES; loop_load++)
{
return_struct->i_epc[loop_load]=0;
return_struct->q_epc[loop_load]=0;
}
return_struct->i_lna_gain=0xD4;
return_struct->q_lna_gain=0xD4;
return_struct->i_main_mag=0;
return_struct->i_alt_mag=0;
return_struct->q_main_mag=0;
return_struct->q_alt_mag=0;
//Load TX RAM, getting ready for search of specific tag and read back.
//We assume that the iDevice has set the target epc via callback already
set_select_target(TARGET_SL); //No possible error, so don't check. Select packet will make changes to tags' SL flag.
set_select_action(ACTION_A0); //No possible error, so don't check. Tags matching/not matching select packet specification will assert/deassert SL flag.
set_query_sel(SEL_PSL); //No possible error, so don't check. Only tags with SL flag asserted will respond to query.
set_query_session(session); //No possible error, so don't check.
set_query_target(TARGET_A); //No possible error, so don't check. Only tags whose inventory flag is set to "A" will participate in the round.
set_query_q(0); //Targeted search requires immediate reply by the tag of interest, so set query Q=0
//If we are checking the SX1257 PLL behavior, we need to set a flag bit in the FPGA to reconfigure the data recovery to look for tonal behavior in an otherwise benign environment.
if(target_epc==TARGET_PLL_EPC)
rfidr_error_code=set_sx1257_pll_chk_mode();
else
rfidr_error_code=unset_sx1257_pll_chk_mode(); //Have this in here just in case somehow pll mode was set elsewhere.
if(rfidr_error_code != RFIDR_SUCCESS){handle_error(p_rfidrs,error_info,"setting PLL check mode",rfidr_error_code); return RFIDR_ERROR_GENERAL;}
//Depending on the argument provided in the function all, load the appropriate select packet into the FPGA.
switch(target_epc)
{
case TARGET_APP_SPECD_EPC: rfidr_error_code=load_select_packet_only(APP_SPECD_EPC,MAX_EPC_LENGTH_IN_BYTES,SEL_PACKET_NO_1); break;
//Why is the above calling for an epc length of max bytes? Because we are doing Q=0 so we need to uniquely specify the tag.
//If we have an incomplete EPC, the app specd epc should be appended with all zeros and probably won't find the correct tag.
case TARGET_LAST_INV_EPC: rfidr_error_code=load_select_packet_only(LAST_INV_EPC,MAX_EPC_LENGTH_IN_BYTES,SEL_PACKET_NO_1); break;
case TARGET_CAL_EPC: rfidr_error_code=load_select_packet_only(DUMMYTAG_EPC,MAX_EPC_LENGTH_IN_BYTES,SEL_PACKET_NO_1); break;
case TARGET_PLL_EPC: rfidr_error_code=load_select_packet_only(ZERO_EPC,MAX_EPC_LENGTH_IN_BYTES,SEL_PACKET_NO_1); break;
default: rfidr_error_code=load_select_packet_only(ZERO_EPC,MAX_EPC_LENGTH_IN_BYTES,SEL_PACKET_NO_1); break;
}
if(rfidr_error_code != RFIDR_SUCCESS){handle_error(p_rfidrs,error_info,"loading select packet 1",rfidr_error_code); return RFIDR_ERROR_GENERAL;}
//For the select #2 packet, just put in all zeros and make sure this gets added to the selection as part of a union
//Next we set the second select packet to set the session flag to "A" on all tags that also comply with EPC2
set_select_action(ACTION_A1); //No possible error, so don't check.
rfidr_error_code=load_select_packet_only(ZERO_EPC,MAX_EPC_LENGTH_IN_BYTES,SEL_PACKET_NO_2);
if(rfidr_error_code != RFIDR_SUCCESS){handle_error(p_rfidrs,error_info,"loading select packet 2",rfidr_error_code); return RFIDR_ERROR_GENERAL;}
//Load query packet into TX RAM. This had to be done each time one of query select, session, target, or Q are changed.
//Since we are doing query Q=0 we don't need to load the Query Rep or Query Adjust packets.
rfidr_error_code=load_query_packet_only(FLAGSWAP_NO);
if(rfidr_error_code != RFIDR_SUCCESS){handle_error(p_rfidrs,error_info,"loading query",rfidr_error_code); return RFIDR_ERROR_GENERAL;}
//Tell the FPGA to operate its radio state machine in search mode - that is, run the sequence of operation that looks for one particular tag.
rfidr_error_code=set_radio_mode_search();
if(rfidr_error_code != RFIDR_SUCCESS){handle_error(p_rfidrs,error_info,"set radio mode to search",rfidr_error_code); return RFIDR_ERROR_GENERAL;}
//We need to check both I and Q paths of the clock and data recovery circuit.
for(loop_iq=0;loop_iq<=1;loop_iq++)
{
//Enable PA.
rfidr_error_code=rfidr_enable_pa();
if(rfidr_error_code != RFIDR_SUCCESS){handle_error(p_rfidrs,error_info,"enabling pa",rfidr_error_code); return RFIDR_ERROR_GENERAL;}
//Set use_i or use_q by sending the appropriate command to the FPGA.
if(loop_iq % 2 == 0)
rfidr_error_code=set_use_i();
else
rfidr_error_code=set_use_q();
if(rfidr_error_code != RFIDR_SUCCESS){handle_error(p_rfidrs,error_info,"set_use_i/q",rfidr_error_code); return RFIDR_ERROR_GENERAL;}
//Run the radio. First ensure we specify a select packet and that the SX1257 LNA gain is reset.
//We also set the flag in the FPGA to use the select packet on the first packet to be sent out.
rfidr_error_code=set_use_select_pkt();
if(rfidr_error_code != RFIDR_SUCCESS){handle_error(p_rfidrs,error_info,"setting select packet",rfidr_error_code); return RFIDR_ERROR_GENERAL;}
set_sx1257_lna_gain((uint8_t)(0xD4)); //Reset the gain
//Set the FPGA IRQ flag to false so that we can wait for it.
m_received_irq_flag = false;
//Now that the FPGA commands have all been loaded up, tell the FPGA to execute a run of the state machine.
rfidr_error_code=set_go_radio_oneshot();
if(rfidr_error_code != RFIDR_SUCCESS){handle_error(p_rfidrs,error_info,"set go radio",rfidr_error_code); return RFIDR_ERROR_GENERAL;}
//Wait for IRQ back from FPGA.
while(m_received_irq_flag==false){}
//ACK the IRQ to permit the FPGA state machines to accept another input.
rfidr_error_code=set_irq_ack_oneshot();
if(rfidr_error_code != RFIDR_SUCCESS){handle_error(p_rfidrs,error_info,"acking irq",rfidr_error_code); return RFIDR_ERROR_GENERAL;}
//Disable the PA to reduce the amount of time we spend eating into our FCC on-time budget.
rfidr_error_code=rfidr_disable_pa();
if(rfidr_error_code != RFIDR_SUCCESS){handle_error(p_rfidrs,error_info,"disabling pa",rfidr_error_code); return RFIDR_ERROR_GENERAL;}
//Was the operation a success? If so, declare it and dump EPC and magnitude data back to the iDevice.
//If the operation was not a success, declare a fail, because any information that might be returned would be invalid.
if(target_epc==TARGET_PLL_EPC || read_radio_exit_code()==0) //We weren't checking the exit code before for PLL check. Order of execution should prevent check of exit code from occurring. If not, not a problem.
{
if(loop_iq==0)
{
send_short_message(p_rfidrs, "Search I Pass");
return_struct->i_pass=true;
if(return_epc==RETURN_EPC_YES)
{
rfidr_error_code=rfidr_read_epc(return_struct->i_epc,(target_epc==TARGET_PLL_EPC) ? READ_RXRAM_PLLCHECK : READ_RXRAM_REGULAR);
if(rfidr_error_code != RFIDR_SUCCESS){handle_error(p_rfidrs,error_info,"checking I EPC", rfidr_error_code); return RFIDR_ERROR_GENERAL;}
}
if(return_mag==RETURN_MAG_YES)
{
rfidr_error_code=rfidr_read_main_magnitude(&(return_struct->i_main_mag),(target_epc==TARGET_PLL_EPC) ? READ_RXRAM_PLLCHECK : READ_RXRAM_REGULAR);
if(rfidr_error_code != RFIDR_SUCCESS){handle_error(p_rfidrs,error_info,"checking I - Main Mag",rfidr_error_code); return RFIDR_ERROR_GENERAL;}
rfidr_error_code=rfidr_read_alt_magnitude(&(return_struct->i_alt_mag),(target_epc==TARGET_PLL_EPC) ? READ_RXRAM_PLLCHECK : READ_RXRAM_REGULAR);
if(rfidr_error_code != RFIDR_SUCCESS){handle_error(p_rfidrs,error_info,"checking I - Alt Mag",rfidr_error_code); return RFIDR_ERROR_GENERAL;}
sprintf(short_message,"MI(I): %10d",(int)return_struct->i_main_mag);
send_short_message(p_rfidrs, short_message);
sprintf(short_message,"MQ(I): %10d",(int)return_struct->i_alt_mag);
send_short_message(p_rfidrs, short_message);
}
if(return_lna_gain==RETURN_LNA_GAIN_YES)
{
rfidr_error_code=get_sx1257_lna_gain(&(return_struct->i_lna_gain)); //Check the gain
if(rfidr_error_code != RFIDR_SUCCESS){handle_error(p_rfidrs,error_info,"getting I LNA gain",rfidr_error_code); return RFIDR_ERROR_GENERAL;}
}
}
else
{
send_short_message(p_rfidrs, "Search Q Pass");
return_struct->q_pass=true;
if(return_epc==RETURN_EPC_YES)
{
rfidr_error_code=rfidr_read_epc(return_struct->q_epc,(target_epc==TARGET_PLL_EPC) ? READ_RXRAM_PLLCHECK : READ_RXRAM_REGULAR);
if(rfidr_error_code != RFIDR_SUCCESS){handle_error(p_rfidrs,error_info,"checking Q EPC", rfidr_error_code); return RFIDR_ERROR_GENERAL;}
}
if(return_mag==RETURN_MAG_YES)
{
rfidr_error_code=rfidr_read_main_magnitude(&(return_struct->q_main_mag),(target_epc==TARGET_PLL_EPC) ? READ_RXRAM_PLLCHECK : READ_RXRAM_REGULAR);
if(rfidr_error_code != RFIDR_SUCCESS){handle_error(p_rfidrs,error_info,"checking Q - Main Mag",rfidr_error_code); return RFIDR_ERROR_GENERAL;}
rfidr_error_code=rfidr_read_alt_magnitude(&(return_struct->q_alt_mag),(target_epc==TARGET_PLL_EPC) ? READ_RXRAM_PLLCHECK : READ_RXRAM_REGULAR);
if(rfidr_error_code != RFIDR_SUCCESS){handle_error(p_rfidrs,error_info,"checking Q - Alt Mag",rfidr_error_code); return RFIDR_ERROR_GENERAL;}
sprintf(short_message,"MI(Q): %10d",(int)return_struct->q_alt_mag);
send_short_message(p_rfidrs, short_message);
sprintf(short_message,"MQ(Q): %10d",(int)return_struct->q_main_mag);
send_short_message(p_rfidrs, short_message);
}
if(return_lna_gain==RETURN_LNA_GAIN_YES)
{
rfidr_error_code=get_sx1257_lna_gain(&(return_struct->q_lna_gain)); //Check the gain
if(rfidr_error_code != RFIDR_SUCCESS){handle_error(p_rfidrs,error_info,"getting Q LNA gain",rfidr_error_code); return RFIDR_ERROR_GENERAL;}
}
}
}
else
{
sprintf(short_message,"Search %c Fail",loop_iq==0 ? 'I' : 'Q');
send_short_message(p_rfidrs, short_message);
//nrf_delay_ms(60); //I think this is in place to space out the fails on the logic analyzer so we can see then as they were early in the project.
}
//Set use_q to 1 and repeat the search.
} //for loop_iq
if(target_epc==TARGET_PLL_EPC)
{
rfidr_error_code=unset_sx1257_pll_chk_mode();
if(rfidr_error_code != RFIDR_SUCCESS){handle_error(p_rfidrs,error_info,"unsetting PLL check mode",rfidr_error_code); return RFIDR_ERROR_GENERAL;}
}
return rfidr_error_code;
} // static void search_core
static rfidr_error_t end_inventory(ble_rfidrs_t *p_rfidrs, char *error_info)
{
rfidr_error_t rfidr_error_code = RFIDR_SUCCESS;
//If we bail on the inventory in the middle of it, we need to make sure that the PA is disabled.
rfidr_error_code = rfidr_disable_pa();
if(rfidr_error_code != RFIDR_SUCCESS){handle_error(p_rfidrs,error_info,"disabling PA",rfidr_error_code); return RFIDR_ERROR_GENERAL;}
//Also we need to stop LED toggling if that was going on.
rfidr_enable_led1();
//Since the FPGA radio state machine will keep issuing query reps forever, we need to tell it that it's time to end.
//Setting the "inventory end" bit will do this.
rfidr_error_code = set_end_radio_fsm_loop();
if(rfidr_error_code != RFIDR_SUCCESS){handle_error(p_rfidrs,error_info,"setting inventory end",rfidr_error_code); return RFIDR_ERROR_GENERAL;}
//Have the "radio go" in order for the FPGA and FPGA Radio FSMs to get back to their idle states.
m_received_irq_flag = false;
rfidr_error_code = set_go_radio_oneshot();
if(rfidr_error_code != RFIDR_SUCCESS){handle_error(p_rfidrs,error_info,"set go radio for inv. end",rfidr_error_code); return RFIDR_ERROR_GENERAL;}
while(m_received_irq_flag==false){}
rfidr_error_code=set_irq_ack_oneshot();
if(rfidr_error_code != RFIDR_SUCCESS){handle_error(p_rfidrs,error_info,"acking irq for inv. end",rfidr_error_code); return RFIDR_ERROR_GENERAL;}
return RFIDR_SUCCESS;
}
static rfidr_error_t inventory_core(ble_rfidrs_t *p_rfidrs, char *error_info, rfidr_query_session_t session, const char *query_q_vector, uint8_t max_tags, char *epc2, rfidr_return_t *return_struct)
{
//The way inventory is going to run here is that we will only target tags with their inventory tag set to "A".
//We will exclude tags from inventory by setting their flags to "B".
//There are two EPC inputs, one implicit (epc1) - from the app, one explicit (epc2) - from calling this function.
//The inventoried tags are the union of the two epc masks.
//If one wants to inventory all tags in an area, one can set either of the EPCs to a zero-length EPC.
//In general, we'll be using Session S2 or S3 so that we can power down the PA and frequency hop in between query rounds.
#define QUERY_ROUND_LIMIT 36 //Somewhat arbitrary
#define MAX_QUERY_Q 6 //We want to limit this to limit frequency dwell time so we don't have to hop in the middle of a query round.
rfidr_error_t rfidr_error_code = RFIDR_SUCCESS; //An output error code.
uint8_t loop_query_q = 0; //Loop iteration value between query rounds.
uint8_t loop_iq = 0; //Loop iteration value between I and Q sensing in the reader.
uint8_t loop_load = 0; //Loop iteration value for loading arrays.
uint16_t loop_q_iter = 0; //Loop iteration variable for within the query round.
uint8_t q_value = 0; //Create a variable to hold the current value of Query Q so that we can take clear steps to sanitize it.
uint8_t recover_frequency_slot = 0; //A variable to fish out what frequency slot we hopped to in rfidr_sx1257.c.
uint8_t epc1_length_in_bytes = 0; //Variable to fish out what the application specified EPC length is.
//uint8_t epc2_length_in_bytes = 0; //Variable to fish out what the software-specified EPC length is.
char short_message[20] = {0}; //An array to hold a short message to be sent back to the iDevice.
bool query_adj_burn_flag = false; //We demo the Query Adjacent packet here by using it once. This flag lets us just do it once.
rfidr_select_target_t target = TARGET_S2; //The session flag to be targeted by the select packet.
m_num_inv_tags_found = 0; //Use a state variable for this now, so that other functions can use the info.
//Utilize a string for epc2 here because they are easy to input while also having a native sentinel value (null).
//EPC values of less than or equal to 12 bytes can be used here.
//EPC values of greater than 12 bytes will be truncated by the called function.
//Load TX RAM, getting ready for inventory
//In this code, we select both tags that comply with both EPC1 and EPC2 (i.e. the union).
//By setting EPC1 to a zero-length EPC, one ends up inventorying all flags.
//In the future, these functions can be easily modified to support inventorying different groups of RFID tags
switch(session)
{
case SESSION_S0: target=TARGET_S0; break;
case SESSION_S1: target=TARGET_S1; break;
case SESSION_S2: target=TARGET_S2; break;
case SESSION_S3: target=TARGET_S3; break;
default: target=TARGET_S0; break;
}
set_select_target(target); //Target inventoried flag of selected session with the Query. //No possible error, so don't check.
set_select_action(ACTION_A0); //With the first select packet, set the session flag to A for tags complying with EPC1 and to B for those not complying. //No possible error, so don't check.
set_query_sel(SEL_PSL); //121320 - We'll check for select flag to ensure exclusivity of this inventory test.
set_query_session(session); //Query packet will target the session specified by the calling function. //No possible error, so don't check.
set_query_target(TARGET_A); //Query packet will target flags with their session flag set to A. //No possible error, so don't check.
//Load a select packet into the FPGA TX RAM with an EPC that we want to act as a select mask.
//In other words, all of the packets with this tag EPC value will have their session flags set to "A" and all others will have the flags set to "B".
rfidr_error_code=read_length_app_specd_target_epc(&epc1_length_in_bytes); //No real error, so don't check it.
rfidr_error_code=load_select_packet_only(APP_SPECD_EPC,epc1_length_in_bytes, SEL_PACKET_NO_1);
if(rfidr_error_code != RFIDR_SUCCESS){handle_error(p_rfidrs,error_info,"loading arbitrary select packet",rfidr_error_code); return RFIDR_ERROR_GENERAL;}
//Next we set the second select packet to set the session flag to "A" on all tags that also comply with EPC2
//set_select_action(ACTION_A1); //No possible error, so don't check.
//Load the second select packet into TX RAM.
//Why are we doing this? To inventory all tags in the area and to demonstrate that the system can accommodate 2 select packets for
//more precise control of tag selecting.
//Note that currently, the FPGA radio FSM code only supports using two select packets during inventory. The rest of the time, we are trying to find one specific tag of interest.
//rfidr_error_code=set_fmw_specd_target_epc(epc2); //No real error, so don't check it
//rfidr_error_code=read_length_fmw_specd_target_epc(&epc2_length_in_bytes); //No real error, so don't check it.
//121320 - We seem to have problems with inventory specificity, so we'll use the second packet to ensure that
//absolutely only packets with the correct EPC are included in the inventory round.
set_select_target(TARGET_SL);
//Don't need to read the epc length again
rfidr_error_code=load_select_packet_only(APP_SPECD_EPC,epc1_length_in_bytes, SEL_PACKET_NO_2);
if(rfidr_error_code != RFIDR_SUCCESS){handle_error(p_rfidrs,error_info,"loading arbitrary select packet no 2",rfidr_error_code); return RFIDR_ERROR_GENERAL;}
//Tell the FPGA to operate its state machine as per inventory rules.
rfidr_error_code=set_radio_mode_inventory();
if(rfidr_error_code != RFIDR_SUCCESS){handle_error(p_rfidrs,error_info,"set radio mode to inventory",rfidr_error_code); return RFIDR_ERROR_GENERAL;}
//With the information we have so far, we can load the Query Adjust packet into the QUERY REP slot of the FPGA TX RAM.
//We do this first to demonstrate usage of the Query Adjust packet.
rfidr_error_code=load_query_adj_packet(true);
query_adj_burn_flag=true;
if(rfidr_error_code != RFIDR_SUCCESS){handle_error(p_rfidrs,error_info,"loading query rep",rfidr_error_code); return RFIDR_ERROR_GENERAL;}
//We also set the flag in the FPGA to use the select packet on the first packet to be sent out.
rfidr_error_code=set_use_select_pkt();
if(rfidr_error_code != RFIDR_SUCCESS){handle_error(p_rfidrs,error_info,"setting select packet",rfidr_error_code); return RFIDR_ERROR_GENERAL;}
set_sx1257_lna_gain((uint8_t)(0xD4)); //Reset the LNA gain. Not sure if we want it here or within the I/Q loop below as of 120719.
//Set query q and load the appropriate query packet. This needs to be done each time Q is changed.
//We dynamically check the length of the Q vector instead of hard coding it by waiting for the NULL (0) character in the Q vector string.
//When we get an error in the loop, we need to end the inventory so that we don't get stuck in the inventory.
//Also we need to put a hard QUERY_ROUND_LIMIT on this loop so it doesn't get stuck.
for(loop_query_q=0;(*(query_q_vector+loop_query_q) != 0) || loop_query_q < QUERY_ROUND_LIMIT;loop_query_q++)
{
rfidr_toggle_led1(); //Toggle LED to show that the reader is doing something.
q_value=(uint8_t)(*(query_q_vector+loop_query_q)-'0'); //Supposedly we got an integer char input. Subtract '0' (48) to do a char to integer conversion.
q_value=(q_value > MAX_QUERY_Q) ? MAX_QUERY_Q : q_value; //Sanitize q_value. Make sure we don't send through anything smaller than 0 and bigger than MAX_QUERY_Q.
//The type of q_value should enforce minimum of 0.
//We need to hop frequencies on a regular basis to comply with FCC section 15.247.
//We can't transmit on a given frequency for greater than 0.4s in a 10 second period.
//Each query rep interval lasts about 2ms and during this time we have the PA on almost the whole time.
//Given that we should not hop between I and Q variants (we want to search both I and Q for a given frequency),
//we really need to set Q <= 6. In addition, we need to try having the PA on only when absolutely necessary for RFID traffic.
//In other words, turn off the PA for BTLE transfers and moving data off of the FPGA.
rfidr_error_code=set_query_q(q_value); //Make sure we convert Q to an integer only value less than 16.
rfidr_error_code=hop_sx1257_frequency(&recover_frequency_slot); m_hopskip_nonce++;
if(rfidr_error_code != RFIDR_SUCCESS){end_inventory(p_rfidrs,"End Inv."); handle_error(p_rfidrs,error_info,"hopping frequency",rfidr_error_code); return RFIDR_ERROR_GENERAL;}
rfidr_error_code=load_query_packet_only(FLAGSWAP_NO);
if(rfidr_error_code != RFIDR_SUCCESS){end_inventory(p_rfidrs,"End Inv."); handle_error(p_rfidrs,error_info,"loading query",rfidr_error_code); return RFIDR_ERROR_GENERAL;}
//Enable PA. - 060120 - We'll want to move this into the loop_q_iter to minimize the time the PA is on by turning it off during SPI and BTLE transfers.
rfidr_error_code=rfidr_enable_pa();
if(rfidr_error_code != RFIDR_SUCCESS){end_inventory(p_rfidrs,"End Inv."); handle_error(p_rfidrs,error_info,"enabling pa",rfidr_error_code); return RFIDR_ERROR_GENERAL;}
//Set check I or check Q
for(loop_iq=0;loop_iq<=1;loop_iq++)
{
if(loop_iq % 2 == 0)
rfidr_error_code=set_use_i();
else
rfidr_error_code=set_use_q();
if(rfidr_error_code != RFIDR_SUCCESS){end_inventory(p_rfidrs,"End Inv."); handle_error(p_rfidrs,error_info,"set_use_i/q",rfidr_error_code); return RFIDR_ERROR_GENERAL;}
//Set new query each time we do a round. This is an FPGA setting that alerts the state machine that a new inventory round is starting.
//In this case, the query packet is sent during the first set of TX commands, followed by query rep. packets in between tag reads on subsequent TX commands.
rfidr_error_code=set_alt_radio_fsm_loop();
if(rfidr_error_code != RFIDR_SUCCESS){end_inventory(p_rfidrs,"End Inv."); handle_error(p_rfidrs,error_info,"setting new query flag",rfidr_error_code); return RFIDR_ERROR_GENERAL;}
//Run through a query round
//We basically re-run the query round for Q after doing I.
//Why do we have the query as the inner loop instead of I/Q? Let's say I/Q was the inner loop and the tag was only accessible on Q channel.
//Then the "I" channel sense would always result in the tag replying but not getting read and then going back to the arbitrate state with the slot counter set to maximum.
//We'd never read that tag as a result!
for(loop_q_iter=0;loop_q_iter <= (1 << q_value);loop_q_iter++)
{ //The <= is to ensure no tag backscatters RN16 again at a query.
//set_sx1257_lna_gain((uint8_t)(0xD4)); //Don't reset the LNA gain. The idea is that convergence won't change a whole lot in between rounds.
//Set the FPGA IRQ flag to false so we can wait for the IRQ.
m_received_irq_flag = false;
//Now that the FPGA is loaded with settings and commands, tell it to execute those commands.
rfidr_error_code=set_go_radio_oneshot();
if(rfidr_error_code != RFIDR_SUCCESS){end_inventory(p_rfidrs,"End Inv."); handle_error(p_rfidrs,error_info,"set go radio",rfidr_error_code); return RFIDR_ERROR_GENERAL;}
//Wait for IRQ back from FPGA.
while(m_received_irq_flag==false){}
//ACK the FPGA when we receive the IRQ so the FPGA can transition its state.
rfidr_error_code=set_irq_ack_oneshot();
if(rfidr_error_code != RFIDR_SUCCESS){end_inventory(p_rfidrs,"End Inv."); handle_error(p_rfidrs,error_info,"acking irq",rfidr_error_code); return RFIDR_ERROR_GENERAL;}
//Was the operation a success? This time it's important because otherwise we don't store the epc.
//Also, the original plan was to store all of the data in RAM and _then_ send it out.
//What is the time required to execute one indication transaction?
//Also in this case we wouldn't care about the stats, so we'd avoid sending ble packet 2.
//On the other hand, we can't afford a missed notification
//Actually what we can do is use the indication handler to shut down transactions (through a different state) if we end up missing an indication.
//This way writing the data back to the phone becomes nonblocking.
//This part is a bit interesting because we are in the middle of the state machine when we get the IRQ.
//The state machine has stopped at this point and passed back control to the MCU.
//When the FPGA state machine gets another "go_radio" it will start executing again.
if(read_radio_exit_code()==0)
{
m_num_inv_tags_found++;
if(m_num_inv_tags_found >= max_tags){end_inventory(p_rfidrs,"End Inv."); handle_error(p_rfidrs,error_info,"inventoried more than max # tags",rfidr_error_code); return RFIDR_ERROR_GENERAL;}
return_struct->i_pass=false;
return_struct->q_pass=false;
for(loop_load=0; loop_load < MAX_EPC_LENGTH_IN_BYTES; loop_load++)
{
return_struct->i_epc[loop_load]=0;
return_struct->q_epc[loop_load]=0;
}
return_struct->i_lna_gain=0xD4;
return_struct->q_lna_gain=0xD4;
return_struct->i_main_mag=0;
return_struct->i_alt_mag=0;
return_struct->q_main_mag=0;
return_struct->q_alt_mag=0;