-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.c
796 lines (672 loc) · 25.7 KB
/
main.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
/*! ----------------------------------------------------------------------------
* @file main.c
* @brief main loop for the DecaRanging application
*
* @attention
*
* Copyright 2013 (c) DecaWave Ltd, Dublin, Ireland.
*
* All rights reserved.
*
* @author DecaWave
*/
/* Includes */
#include "compiler/compiler.h"
#include "platform/port.h"
#include "application/instance.h"
#include "decadriver/deca_types.h"
#include "decadriver/deca_spi.h"
#include "stdio.h"
#define DWINTERRUPT_EN (1) //set to 1 when using DW interrupt, set to 0 to poll DW1000 IRQ line
#define SOFTWARE_VER_STRING "Version 3.05 " //
#define SWS1_SRM_MODE 0x80 //slow ranging mode with PC - response is configurable and > 150 ms
#define SWS1_ANC_MODE 0x08 //anchor mode
#define SWS1_SHF_MODE 0x10 //short frame mode (6.81M) (switch S1-5)
#define SWS1_64M_MODE 0x20 // 64M PRF mode (switch S1-6)
#define SWS1_CH5_MODE 0x40 //channel 5 mode (switch S1-7)
int instance_anchaddr = 0; //0 = 0xDECA020000000001; 1 = 0xDECA020000000002; 2 = 0xDECA020000000003
//NOTE: switches TA_SW1_7 and TA_SW1_8 are used to set tag/anchor address
int dr_mode = 0;
//if instance_mode = TAG_TDOA then the device cannot be selected as anchor
int instance_mode = ANCHOR;
//int instance_mode = TAG;
//int instance_mode = TAG_TDOA;
//int instance_mode = LISTENER;
uint8 s1switch = 0;
int chan, tagaddr, ancaddr;
#define LCD_BUFF_LEN (100)
uint8 dataseq[LCD_BUFF_LEN];
uint8 dataseq1[LCD_BUFF_LEN];
int ranging = 0;
typedef struct
{
uint8 channel ;
uint8 prf ;
uint8 datarate ;
uint8 preambleCode ;
uint8 preambleLength ;
uint8 pacSize ;
uint8 nsSFD ;
uint16 sfdTO ;
} chConfig_t ;
//Configuration for DecaRanging Modes (8 default use cases selectable by the switch S1 on EVK)
chConfig_t chConfig[8] ={
//mode 1 - S1: 7 off, 6 off, 5 off
{
2, // channel
DWT_PRF_16M, // prf
DWT_BR_110K, // datarate
3, // preambleCode
DWT_PLEN_1024, // preambleLength
DWT_PAC32, // pacSize
1, // non-standard SFD
(1025 + 64 - 32) //SFD timeout
},
//mode 2
{
2, // channel
DWT_PRF_16M, // prf
DWT_BR_6M8, // datarate
3, // preambleCode
DWT_PLEN_128, // preambleLength
DWT_PAC8, // pacSize
0, // non-standard SFD
(129 + 8 - 8) //SFD timeout
},
//mode 3
{
2, // channel
DWT_PRF_64M, // prf
DWT_BR_110K, // datarate
9, // preambleCode
DWT_PLEN_1024, // preambleLength
DWT_PAC32, // pacSize
1, // non-standard SFD
(1025 + 64 - 32) //SFD timeout
},
//mode 4
{
2, // channel
DWT_PRF_64M, // prf
DWT_BR_6M8, // datarate
9, // preambleCode
DWT_PLEN_128, // preambleLength
DWT_PAC8, // pacSize
0, // non-standard SFD
(129 + 8 - 8) //SFD timeout
},
//mode 5
{
5, // channel
DWT_PRF_16M, // prf
DWT_BR_110K, // datarate
3, // preambleCode
DWT_PLEN_1024, // preambleLength
DWT_PAC32, // pacSize
1, // non-standard SFD
(1025 + 64 - 32) //SFD timeout
},
//mode 6
{
5, // channel
DWT_PRF_16M, // prf
DWT_BR_6M8, // datarate
3, // preambleCode
DWT_PLEN_128, // preambleLength
DWT_PAC8, // pacSize
0, // non-standard SFD
(129 + 8 - 8) //SFD timeout
},
//mode 7
{
5, // channel
DWT_PRF_64M, // prf
DWT_BR_110K, // datarate
9, // preambleCode
DWT_PLEN_1024, // preambleLength
DWT_PAC32, // pacSize
1, // non-standard SFD
(1025 + 64 - 32) //SFD timeout
},
//mode 8
{
5, // channel
DWT_PRF_64M, // prf
DWT_BR_6M8, // datarate
9, // preambleCode
DWT_PLEN_128, // preambleLength
DWT_PAC8, // pacSize
0, // non-standard SFD
(129 + 8 - 8) //SFD timeout
}
};
uint32 inittestapplication(uint8 s1switch);
// Restart and re-configure
void restartinstance(void)
{
instance_close() ; //shut down instance, PHY, SPI close, etc.
spi_peripheral_init(); //re initialise SPI...
inittestapplication(s1switch) ; //re-initialise instance/device
} // end restartinstance()
//NOTE: Channel 5 is not supported for the non-discovery mode
int decarangingmode(uint8 s1switch)
{
int mode = 0;
if(s1switch & SWS1_SHF_MODE)
{
mode = 1;
}
if(s1switch & SWS1_64M_MODE)
{
mode = mode + 2;
}
if(s1switch & SWS1_CH5_MODE)
{
mode = mode + 4;
}
return mode;
}
uint32 inittestapplication(uint8 s1switch)
{
uint32 devID ;
instanceConfig_t instConfig;
int result;
SPI_ConfigFastRate(SPI_BaudRatePrescaler_32); //max SPI before PLLs configured is ~4M
//this is called here to wake up the device (i.e. if it was in sleep mode before the restart)
devID = instancereaddeviceid() ;
if(DWT_DEVICE_ID != devID) //if the read of device ID fails, the DW1000 could be asleep
{
port_SPIx_clear_chip_select(); //CS low
Sleep(1); //200 us to wake up then waits 5ms for DW1000 XTAL to stabilise
port_SPIx_set_chip_select(); //CS high
Sleep(7);
devID = instancereaddeviceid() ;
// SPI not working or Unsupported Device ID
if(DWT_DEVICE_ID != devID)
return(-1) ;
//clear the sleep bit - so that after the hard reset below the DW does not go into sleep
dwt_softreset();
}
//reset the DW1000 by driving the RSTn line low
reset_DW1000();
result = instance_init() ;
if (0 > result) return(-1) ; // Some failure has occurred
SPI_ConfigFastRate(SPI_BaudRatePrescaler_4); //increase SPI to max
devID = instancereaddeviceid() ;
if (DWT_DEVICE_ID != devID) // Means it is NOT MP device
{
// SPI not working or Unsupported Device ID
return(-1) ;
}
if(s1switch & SWS1_ANC_MODE)
{
instance_mode = ANCHOR;
led_on(LED_PC6);
}
else
{
instance_mode = TAG;
led_on(LED_PC7);
}
instancesetrole(instance_mode) ; // Set this instance role
instance_init_s(instance_mode);
dr_mode = decarangingmode(s1switch);
instConfig.channelNumber = chConfig[dr_mode].channel ;
instConfig.preambleCode = chConfig[dr_mode].preambleCode ;
instConfig.pulseRepFreq = chConfig[dr_mode].prf ;
instConfig.pacSize = chConfig[dr_mode].pacSize ;
instConfig.nsSFD = chConfig[dr_mode].nsSFD ;
instConfig.sfdTO = chConfig[dr_mode].sfdTO ;
instConfig.dataRate = chConfig[dr_mode].datarate ;
instConfig.preambleLen = chConfig[dr_mode].preambleLength ;
instance_config(&instConfig) ; // Set operating channel etc
instancesettagsleepdelay(POLL_SLEEP_DELAY, BLINK_SLEEP_DELAY); //set the Tag sleep time
instance_init_timings();
return devID;
}
/**
**===========================================================================
**
** Abstract: main program
**
**===========================================================================
*/
void process_dwRSTn_irq(void)
{
instance_notify_DW1000_inIDLE(1);
}
#if (DWINTERRUPT_EN == 1)
void process_deca_irq(void)
{
do{
instance_process_irq(0);
}while(port_CheckEXT_IRQ() == 1); //while IRQ line active (ARM can only do edge sensitive interrupts)
}
#else
void process_deca_irq(void)
{
while(dwt_checkIRQ() == 1)
{
instance_process_irq(0);
} //while IRQ line active
}
#endif
void initLCD(void)
{
uint8 initseq[9] = { 0x39, 0x14, 0x55, 0x6D, 0x78, 0x38 /*0x3C*/, 0x0C, 0x01, 0x06 };
uint8 command = 0x0;
int j = 100000;
writetoLCD( 9, 0, initseq); //init seq
while(j--);
command = 0x2 ; //return cursor home
writetoLCD( 1, 0, &command);
command = 0x1 ; //clear screen
writetoLCD( 1, 0, &command);
}
/*
* @brief switch_mask - bitmask of testing switches (currently 7 switches)
* switchbuff[] - switch name to test
* *switch_fn[]() - corresponded to switch test function
**/
#define switch_mask (0x7F)
const uint8 switchbuf[]={0, TA_SW1_3 , TA_SW1_4 , TA_SW1_5 , TA_SW1_6 , TA_SW1_7 , TA_SW1_8 };
typedef int (* switch_handler_t)(uint16) ;
const switch_handler_t switch_fn[] ={ is_button_low, \
is_switch_on, is_switch_on, is_switch_on,\
is_switch_on, is_switch_on, is_switch_on };
/*
* @fn test_application_run
* @brief test application for production pre-test procedure
**/
void test_application_run(void)
{
char dataseq[2][40];
uint8 j, switchStateOn, switchStateOff;
switchStateOn=0;
switchStateOff=0;
led_on(LED_ALL); // show all LED OK
Sleep(1000);
dataseq[0][0] = 0x1 ; //clear screen
writetoLCD( 1, 0, (const uint8 *) &dataseq);
dataseq[0][0] = 0x2 ; //return cursor home
writetoLCD( 1, 0, (const uint8 *) &dataseq);
/* testing SPI to DW1000*/
writetoLCD( 40, 1, (const uint8 *) "TESTING ");
writetoLCD( 40, 1, (const uint8 *) "SPI, U2, S2, S3 ");
Sleep(1000);
if(inittestapplication(s1switch) == (uint32)-1)
{
writetoLCD( 40, 1, (const uint8 *) "SPI, U2, S2, S3 ");
writetoLCD( 40, 1, (const uint8 *) "-- TEST FAILS --");
while(1); //stop
}
writetoLCD( 40, 1, (const uint8 *) "SPI, U2, S2, S3 ");
writetoLCD( 40, 1, (const uint8 *) " TEST OK ");
Sleep(1000);
/* testing of switch S2 */
dataseq[0][0] = 0x1 ; //clear screen
writetoLCD( 1, 0, (const uint8 *) &dataseq);
while( (switchStateOn & switchStateOff) != switch_mask )
{
memset(&dataseq, ' ', sizeof(dataseq));
strcpy(&dataseq[0][0], (const char *)"SWITCH");
strcpy(&dataseq[1][0], (const char *)"toggle");
//switch 7-1
for (j=0;j<sizeof(switchbuf);j++)
{
if( switch_fn[j](switchbuf[j]) ) //execute current switch switch_fn
{
dataseq[0][8+j]='O';
switchStateOn |= 0x01<<j;
switchStateOff &= ~(0x01<<j);//all switches finaly should be in off state
}else{
dataseq[1][8+j]='O';
switchStateOff |=0x01<<j;
}
}
writetoLCD(40, 1, (const uint8 *) &dataseq[0][0]);
writetoLCD(40, 1, (const uint8 *) &dataseq[1][0]);
Sleep(100);
}
led_off(LED_ALL);
writetoLCD( 40, 1, (const uint8 *) " Preliminary ");
writetoLCD( 40, 1, (const uint8 *) " TEST OKAY ");
while(1);
}
void setLCDline1(uint8 s1switch)
{
uint8 command = 0x2 ; //return cursor home
writetoLCD( 1, 0, &command);
sprintf((char*)&dataseq[0], "DecaRanging %02x", s1switch);
writetoLCD( 40, 1, dataseq); //send some data
sprintf((char*)&dataseq1[0], " ");
writetoLCD( 16, 1, dataseq1); //send some data
}
/*
* @fn main()
* @brief main entry point
**/
int main(void)
{
int i = 0;
int toggle = 1;
double range_result = 0;
double avg_result = 0;
led_off(LED_ALL); //turn off all the LEDs
peripherals_init();
spi_peripheral_init();
Sleep(1000); //wait for LCD to power on
initLCD();
memset(dataseq, 0, LCD_BUFF_LEN);
memcpy(dataseq, (const uint8 *) "DECAWAVE ", 16);
writetoLCD( 40, 1, dataseq); //send some data
memcpy(dataseq, (const uint8 *) SOFTWARE_VER_STRING, 16); // Also set at line #26 (Should make this from single value !!!)
writetoLCD( 16, 1, dataseq); //send some data
Sleep(1000);
#ifdef USB_SUPPORT
// enable the USB functionality
usb_init();
Sleep(1000);
#endif
s1switch = is_button_low(0) << 1 // is_switch_on(TA_SW1_2) << 2
| is_switch_on(TA_SW1_3) << 2
| is_switch_on(TA_SW1_4) << 3
| is_switch_on(TA_SW1_5) << 4
| is_switch_on(TA_SW1_6) << 5
| is_switch_on(TA_SW1_7) << 6
| is_switch_on(TA_SW1_8) << 7;
port_DisableEXT_IRQ(); //disable ScenSor IRQ until we configure the device
//test EVB1000 - used in EVK1000 production
if((is_button_low(0) == S1_SWITCH_ON) && (is_switch_on(TA_SW1_8) == S1_SWITCH_ON)) //using BOOT1 switch for test
{
test_application_run(); //does not return....
}
else
if(is_switch_on(TA_SW1_3) == S1_SWITCH_OFF)
{
int j = 1000000;
uint8 command;
memset(dataseq, 0, LCD_BUFF_LEN);
while(j--);
//command = 0x1 ; //clear screen
//writetoLCD( 1, 0, &command);
command = 0x2 ; //return cursor home
writetoLCD( 1, 0, &command);
memcpy(dataseq, (const uint8 *) "DECAWAVE ", 12);
writetoLCD( 40, 1, dataseq); //send some data
#ifdef USB_SUPPORT //this is set in the port.h file
memcpy(dataseq, (const uint8 *) "USB to SPI ", 12);
#else
#endif
writetoLCD( 16, 1, dataseq); //send some data
j = 1000000;
while(j--);
command = 0x2 ; //return cursor home
writetoLCD( 1, 0, &command);
#ifdef USB_SUPPORT //this is set in the port.h file
// enable the USB functionality
//usb_init();
NVIC_DisableDECAIRQ();
// Do nothing in foreground -- allow USB application to run, I guess on the basis of USB interrupts?
while (1) // loop forever
{
usb_run();
}
#endif
return 1;
}
else //run DecaRanging application
{
uint8 dataseq[LCD_BUFF_LEN];
uint8 command = 0x0;
command = 0x2 ; //return cursor home
writetoLCD( 1, 0, &command);
memset(dataseq, ' ', LCD_BUFF_LEN);
memcpy(dataseq, (const uint8 *) "DECAWAVE RANGE", 15);
writetoLCD( 15, 1, dataseq); //send some data
led_off(LED_ALL);
#ifdef USB_SUPPORT //this is set in the port.h file
usb_printconfig(16, (uint8 *)SOFTWARE_VER_STRING, s1switch);
#endif
if(inittestapplication(s1switch) == (uint32)-1)
{
led_on(LED_ALL); //to display error....
dataseq[0] = 0x2 ; //return cursor home
writetoLCD( 1, 0, &dataseq[0]);
memset(dataseq, ' ', LCD_BUFF_LEN);
memcpy(dataseq, (const uint8 *) "ERROR ", 12);
writetoLCD( 40, 1, dataseq); //send some data
memcpy(dataseq, (const uint8 *) " INIT FAIL ", 12);
writetoLCD( 40, 1, dataseq); //send some data
return 0; //error
}
//sleep for 5 seconds displaying "Decawave"
i=30;
while(i--)
{
if (i & 1) led_off(LED_ALL);
else led_on(LED_ALL);
Sleep(200);
}
i = 0;
led_off(LED_ALL);
command = 0x2 ; //return cursor home
writetoLCD( 1, 0, &command);
memset(dataseq, ' ', LCD_BUFF_LEN);
if(s1switch & SWS1_ANC_MODE)
{
instance_mode = ANCHOR;
led_on(LED_PC6);
}
else
{
instance_mode = TAG;
led_on(LED_PC7);
}
if(instance_mode == TAG)
{
//if TA_SW1_2 is on use fast ranging (fast 2wr)
if(is_button_low(0) == S1_SWITCH_ON)
{
memcpy(&dataseq[2], (const uint8 *) " Fast Tag ", 12);
writetoLCD( 40, 1, dataseq); //send some data
memcpy(&dataseq[2], (const uint8 *) " Ranging ", 12);
writetoLCD( 16, 1, dataseq); //send some data
}
else
{
memcpy(&dataseq[2], (const uint8 *) " TAG BLINK ", 12);
writetoLCD( 40, 1, dataseq); //send some data
sprintf((char*)&dataseq[0], "%llX", instance_get_addr());
writetoLCD( 16, 1, dataseq); //send some data
}
}
else
{
memcpy(&dataseq[2], (const uint8 *) " AWAITING ", 12);
writetoLCD( 40, 1, dataseq); //send some data
memcpy(&dataseq[2], (const uint8 *) " POLL ", 12);
writetoLCD( 16, 1, dataseq); //send some data
}
command = 0x2 ; //return cursor home
writetoLCD( 1, 0, &command);
}
#if (DWINTERRUPT_EN == 1)
port_EnableEXT_IRQ(); //enable ScenSor IRQ before starting
#endif
memset(dataseq, ' ', LCD_BUFF_LEN);
memset(dataseq1, ' ', LCD_BUFF_LEN);
#ifdef USART_SUPPORT
printf2(" %s\n", SOFTWARE_VER_STRING);
#endif
// main loop
while(1)
{
#if (DWINTERRUPT_EN == 0)
process_deca_irq(); //poll DW1000 IRQ line when using polling of interrupt line
#endif
instance_run();
//if delayed TX scheduled but did not happen after expected time then it has failed... (has to be < slot period)
//if anchor just go into RX and wait for next message from tags/anchors
//if tag handle as a timeout
if((instance_data[0].monitor == 1) && ((portGetTickCnt() - instance_data[0].timeofTx) > instance_data[0].finalReplyDelay_ms))
{
instance_data[0].wait4ack = 0;
if(instance_mode == TAG)
{
inst_processrxtimeout(&instance_data[0]);
}
else //if(instance_mode == ANCHOR)
{
dwt_forcetrxoff(); //this will clear all events
//enable the RX
instance_data[0].testAppState = TA_RXE_WAIT ;
}
instance_data[0].monitor = 0;
}
if(instancenewrange())
{
int n, l = 0, /*txl = 0, rxl = 0,*/ aaddr, taddr, txa, rxa, rng, rng_raw;
ranging = 1;
//send the new range information to LCD and/or USB
range_result = instance_get_idist();
avg_result = instance_get_adist();
//set_rangeresult(range_result);
dataseq[0] = 0x2 ; //return cursor home
writetoLCD( 1, 0, dataseq);
memset(dataseq, ' ', LCD_BUFF_LEN);
memset(dataseq1, ' ', LCD_BUFF_LEN);
sprintf((char*)&dataseq[1], "LAST: %4.2f m", range_result);
writetoLCD( 40, 1, dataseq); //send some data
sprintf((char*)&dataseq1[1], "AVG8: %4.2f m", avg_result);
writetoLCD( 16, 1, dataseq1); //send some data
l = instance_get_lcount();
//txl = instance_get_txl();
//rxl = instance_get_rxl();
aaddr = instancenewrangeancadd();
taddr = instancenewrangetagadd();
txa = instancetxantdly();
rxa = instancerxantdly();
rng = (int) (range_result*1000);
rng_raw = (int) (instance_get_idistraw()*1000);
if(instance_mode == TAG)
{
//n = sprintf((char*)&dataseq[0], "ia%04x t%04x %04x %04x %04x %04x %04x %02x %02x t", aaddr, taddr, rng, rng_raw, l, txa, rxa, txl, rxl);
n = sprintf((char*)&dataseq[0], "ia%04x t%04x %08x %08x %04x %04x %04x t", aaddr, taddr, rng, rng_raw, l, txa, rxa);
}
else
{
//n = sprintf((char*)&dataseq[0], "ia%04x t%04x %04x %04x %04x %04x %04x %02x %02x a", aaddr, taddr, rng, rng_raw, l, txa, rxa, txl, rxl);
//n = sprintf((char*)&dataseq[0], "ia%04x t%04x %08x %08x %04x %04x %04x %2.2f a", aaddr, taddr, rng, rng_raw, l, txa, rxa, instance_data[0].clockOffset);
n = sprintf((char*)&dataseq[0], "ia%04x t%04x %08x %08x %04x %04x %04x a", aaddr, taddr, rng, rng_raw, l, txa, rxa);
}
#ifdef USB_SUPPORT //this is set in the port.h file
send_usbmessage(&dataseq[0], n);
#endif
#ifdef USART_SUPPORT
{
//printf2("R= %i mm\r\n",rng);
printf2("R= %-3.2f m\r\n",range_result);
}
#endif
}
#ifdef USART_SUPPORT
{
int nrm = 0;
if(nrm = instancenorange())
{
if(nrm == 1)
{
printf2("I= No Response\r\n");
}
else if(nrm == 2)
{
printf2("I= No Report\r\n");
}
else if(nrm == 3)
{
printf2("I= No Final\r\n");
}
}
}
#endif
if(ranging == 0)
{
if(instance_mode != ANCHOR)
{
if(instancesleeping())
{
dataseq[0] = 0x2 ; //return cursor home
writetoLCD( 1, 0, dataseq);
if(toggle)
{
toggle = 0;
memcpy(&dataseq[0], (const uint8 *) " AWAITING ", 16);
writetoLCD( 40, 1, dataseq); //send some data
memcpy(&dataseq[0], (const uint8 *) " RESPONSE ", 16);
writetoLCD( 16, 1, dataseq); //send some data
}
else
{
toggle = 1;
memcpy(&dataseq[2], (const uint8 *) " TAG BLINK ", 16);
writetoLCD( 40, 1, dataseq); //send some data
sprintf((char*)&dataseq[0], "%llX", instance_get_addr());
writetoLCD( 16, 1, dataseq); //send some data
}
}
if(instanceanchorwaiting() == 2)
{
ranging = 1;
dataseq[0] = 0x2 ; //return cursor home
writetoLCD( 1, 0, dataseq);
memcpy(&dataseq[0], (const uint8 *) " RANGING WITH", 16);
writetoLCD( 40, 1, dataseq); //send some data
sprintf((char*)&dataseq[0], "%016llX", instance_get_anchaddr());
writetoLCD( 16, 1, dataseq); //send some data
}
}
else //if(instance_mode == ANCHOR)
{
if(instanceanchorwaiting())
{
toggle+=2;
if(toggle > 300000)
{
dataseq[0] = 0x2 ; //return cursor home
writetoLCD( 1, 0, dataseq);
if(toggle & 0x1)
{
toggle = 0;
memcpy(&dataseq[0], (const uint8 *) " AWAITING ", 16);
writetoLCD( 40, 1, dataseq); //send some data
memcpy(&dataseq[0], (const uint8 *) " POLL ", 16);
writetoLCD( 16, 1, dataseq); //send some data
}
else
{
toggle = 1;
memcpy(&dataseq[0], (const uint8 *) " DISCOVERY MODE ", 16);
writetoLCD( 40, 1, dataseq); //send some data
sprintf((char*)&dataseq[0], "%llX", instance_get_addr());
writetoLCD( 16, 1, dataseq); //send some data
}
}
}
else if(instanceanchorwaiting() == 2)
{
dataseq[0] = 0x2 ; //return cursor home
writetoLCD( 1, 0, dataseq);
memcpy(&dataseq[0], (const uint8 *) " RANGING WITH", 16);
writetoLCD( 40, 1, dataseq); //send some data
sprintf((char*)&dataseq[0], "%llX", instance_get_tagaddr());
writetoLCD( 16, 1, dataseq); //send some data
}
}
}
#ifdef USB_SUPPORT //this is set in the port.h file
usb_run();
#endif
}
return 0;
}