Statistics
| Branch: | Tag: | Revision:

amiro-lld / source / alld_dw1000.c @ f125ae07

History | View | Annotate | Download (150.737 KB)

1
/*
2
AMiRo-LLD is a compilation of low-level hardware drivers for the Autonomous Mini Robot (AMiRo) platform.
3
Copyright (C) 2016..2019  Thomas Schöpping et al.
4

5
This program is free software: you can redistribute it and/or modify
6
it under the terms of the GNU Lesser General Public License as published by
7
the Free Software Foundation, either version 3 of the License, or
8
(at your option) any later version.
9

10
This program is distributed in the hope that it will be useful,
11
but WITHOUT ANY WARRANTY; without even the implied warranty of
12
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
GNU Lesser General Public License for more details.
14

15
You should have received a copy of the GNU Lesser General Public License
16
along with this program.  If not, see <http://www.gnu.org/licenses/>.
17
*/
18

    
19
/*! ------------------------------------------------------------------------------------------------------------------
20
 * @file    deca_device.c
21
 * @brief   Decawave device configuration and control functions
22
 *
23
 * @attention
24
 *
25
 * Copyright 2013 (c) Decawave Ltd, Dublin, Ireland.
26
 *
27
 * All rights reserved.
28
 *
29
 */
30

    
31

    
32
#include <alld_dw1000.h>
33
#if defined(AMIROLLD_CFG_USE_DW1000) || defined(__DOXYGEN__)
34

    
35
#include <alld_dw1000_regs.h>
36
#include <aos_thread.h>
37
#include <assert.h>
38
#include <string.h>
39
#include <stdlib.h>
40
#include <math.h>
41

    
42

    
43
// HW dependent implementation (see bottom of file)
44
static int _alld_dw1000_writespi(uint16_t headerLength,
45
               const        uint8_t *headerBuffer,
46
               uint32_t bodyLength,
47
               const        uint8_t *bodyBuffer);
48

    
49
static int _alld_dw1000_readspi(uint16_t headerLength,
50
                const uint8_t *headerBuffer,
51
                uint32_t readlength,
52
                uint8_t *readBuffer);
53

    
54
// Defines for enable_clocks function
55
#define FORCE_SYS_XTI  0
56
#define ENABLE_ALL_SEQ 1
57
#define FORCE_SYS_PLL  2
58
#define READ_ACC_ON    7
59
#define READ_ACC_OFF   8
60
#define FORCE_OTP_ON   11
61
#define FORCE_OTP_OFF  12
62
#define FORCE_TX_PLL   13
63
#define FORCE_LDE      14
64

    
65
// Defines for ACK request bitmask in DATA and MAC COMMAND frame control (first byte) - Used to detect AAT bit wrongly set.
66
#define FCTRL_ACK_REQ_MASK 0x20
67
// Frame control maximum length in bytes.
68
#define FCTRL_LEN_MAX 2
69

    
70

    
71
#define NUM_BR 3
72
#define NUM_PRF 2
73
#define NUM_PACS 4
74
#define NUM_BW 2            //2 bandwidths are supported
75
#define NUM_SFD 2           //supported number of SFDs - standard = 0, non-standard = 1
76
#define NUM_CH 6            //supported channels are 1, 2, 3, 4, 5, 7
77
#define NUM_CH_SUPPORTED 8  //supported channels are '0', 1, 2, 3, 4, 5, '6', 7
78
#define PCODES 25           //supported preamble codes
79

    
80

    
81
typedef struct {
82
    uint32_t lo32;
83
    uint16_t target[NUM_PRF];
84
} agc_cfg_struct ;
85

    
86
extern const agc_cfg_struct agc_config ;
87

    
88
//SFD threshold settings for 110k, 850k, 6.8Mb standard and non-standard
89
extern const uint16_t sftsh[NUM_BR][NUM_SFD];
90

    
91
extern const uint16_t dtune1[NUM_PRF];
92

    
93
#define XMLPARAMS_VERSION   (1.17f)
94

    
95
extern const uint32_t fs_pll_cfg[NUM_CH];
96
extern const uint8_t fs_pll_tune[NUM_CH];
97
extern const uint8_t rx_config[NUM_BW];
98
extern const uint32_t tx_config[NUM_CH];
99
extern const uint8_t dwnsSFDlen[NUM_BR]; //length of SFD for each of the bitrates
100
extern const uint32_t digital_bb_config[NUM_PRF][NUM_PACS];
101
extern const uint8_t chan_idx[NUM_CH_SUPPORTED];
102
extern const double txpwr_compensation[NUM_CH];
103

    
104
#define PEAK_MULTPLIER  (0x60) //3 -> (0x3 * 32) & 0x00E0
105
#define N_STD_FACTOR    (13)
106
#define LDE_PARAM1      (PEAK_MULTPLIER | N_STD_FACTOR)
107

    
108
#define LDE_PARAM3_16 (0x1607)
109
#define LDE_PARAM3_64 (0x0607)
110

    
111
#define MIXER_GAIN_STEP (0.5)
112
#define DA_ATTN_STEP    (2.5)
113

    
114
// #define DWT_API_ERROR_CHECK     // define so API checks config input parameters
115

    
116
//-----------------------------------------
117
// map the channel number to the index in the configuration arrays below
118
// 0th element is chan 1, 1st is chan 2, 2nd is chan 3, 3rd is chan 4, 4th is chan 5, 5th is chan 7
119
const uint8_t chan_idx[NUM_CH_SUPPORTED] = {0, 0, 1, 2, 3, 4, 0, 5};
120

    
121
//-----------------------------------------
122
const uint32_t tx_config[NUM_CH] =
123
{
124
    RF_TXCTRL_CH1,
125
    RF_TXCTRL_CH2,
126
    RF_TXCTRL_CH3,
127
    RF_TXCTRL_CH4,
128
    RF_TXCTRL_CH5,
129
    RF_TXCTRL_CH7,
130
};
131

    
132
//Frequency Synthesiser - PLL configuration
133
const uint32_t fs_pll_cfg[NUM_CH] =
134
{
135
    FS_PLLCFG_CH1,
136
    FS_PLLCFG_CH2,
137
    FS_PLLCFG_CH3,
138
    FS_PLLCFG_CH4,
139
    FS_PLLCFG_CH5,
140
    FS_PLLCFG_CH7
141
};
142

    
143
//Frequency Synthesiser - PLL tuning
144
const uint8_t fs_pll_tune[NUM_CH] =
145
{
146
    FS_PLLTUNE_CH1,
147
    FS_PLLTUNE_CH2,
148
    FS_PLLTUNE_CH3,
149
    FS_PLLTUNE_CH4,
150
    FS_PLLTUNE_CH5,
151
    FS_PLLTUNE_CH7
152
};
153

    
154
//bandwidth configuration
155
const uint8_t rx_config[NUM_BW] =
156
{
157
    RF_RXCTRLH_NBW,
158
    RF_RXCTRLH_WBW
159
};
160

    
161

    
162
const agc_cfg_struct agc_config =
163
{
164
    AGC_TUNE2_VAL,
165
    { AGC_TUNE1_16M , AGC_TUNE1_64M }  //adc target
166
};
167

    
168
//DW non-standard SFD length for 110k, 850k and 6.81M
169
const uint8_t dwnsSFDlen[NUM_BR] =
170
{
171
    DW_NS_SFD_LEN_110K,
172
    DW_NS_SFD_LEN_850K,
173
    DW_NS_SFD_LEN_6M8
174
};
175

    
176
// SFD Threshold
177
const uint16_t sftsh[NUM_BR][NUM_SFD] =
178
{
179
    {
180
        DRX_TUNE0b_110K_STD,
181
        DRX_TUNE0b_110K_NSTD
182
    },
183
    {
184
        DRX_TUNE0b_850K_STD,
185
        DRX_TUNE0b_850K_NSTD
186
    },
187
    {
188
        DRX_TUNE0b_6M8_STD,
189
        DRX_TUNE0b_6M8_NSTD
190
    }
191
};
192

    
193
const uint16_t dtune1[NUM_PRF] =
194
{
195
    DRX_TUNE1a_PRF16,
196
    DRX_TUNE1a_PRF64
197
};
198

    
199
const uint32_t digital_bb_config[NUM_PRF][NUM_PACS] =
200
{
201
    {
202
        DRX_TUNE2_PRF16_PAC8,
203
        DRX_TUNE2_PRF16_PAC16,
204
        DRX_TUNE2_PRF16_PAC32,
205
        DRX_TUNE2_PRF16_PAC64
206
    },
207
    {
208
        DRX_TUNE2_PRF64_PAC8,
209
        DRX_TUNE2_PRF64_PAC16,
210
        DRX_TUNE2_PRF64_PAC32,
211
        DRX_TUNE2_PRF64_PAC64
212
    }
213
};
214

    
215
const uint16_t lde_replicaCoeff[PCODES] =
216
{
217
    0, // No preamble code 0
218
    LDE_REPC_PCODE_1,
219
    LDE_REPC_PCODE_2,
220
    LDE_REPC_PCODE_3,
221
    LDE_REPC_PCODE_4,
222
    LDE_REPC_PCODE_5,
223
    LDE_REPC_PCODE_6,
224
    LDE_REPC_PCODE_7,
225
    LDE_REPC_PCODE_8,
226
    LDE_REPC_PCODE_9,
227
    LDE_REPC_PCODE_10,
228
    LDE_REPC_PCODE_11,
229
    LDE_REPC_PCODE_12,
230
    LDE_REPC_PCODE_13,
231
    LDE_REPC_PCODE_14,
232
    LDE_REPC_PCODE_15,
233
    LDE_REPC_PCODE_16,
234
    LDE_REPC_PCODE_17,
235
    LDE_REPC_PCODE_18,
236
    LDE_REPC_PCODE_19,
237
    LDE_REPC_PCODE_20,
238
    LDE_REPC_PCODE_21,
239
    LDE_REPC_PCODE_22,
240
    LDE_REPC_PCODE_23,
241
    LDE_REPC_PCODE_24
242
};
243

    
244
const double txpwr_compensation[NUM_CH] = {
245
    0.0,
246
    0.035,
247
    0.0,
248
    0.0,
249
    0.065,
250
    0.0
251
};
252

    
253

    
254
#define NUM_16M_OFFSET  (37)
255
#define NUM_16M_OFFSETWB  (68)
256
#define NUM_64M_OFFSET  (26)
257
#define NUM_64M_OFFSETWB  (59)
258

    
259
const uint8_t chan_idxnb[NUM_CH_SUPPORTED] = {0, 0, 1, 2, 0, 3, 0, 0}; //only channels 1,2,3 and 5 are in the narrow band tables
260
const uint8_t chan_idxwb[NUM_CH_SUPPORTED] = {0, 0, 0, 0, 0, 0, 0, 1}; //only channels 4 and 7 are in in the wide band tables
261

    
262
//---------------------------------------------------------------------------------------------------------------------------
263
// Range Bias Correction TABLES of range values in integer units of 25 CM, for 8-bit unsigned storage, MUST END IN 255 !!!!!!
264
//---------------------------------------------------------------------------------------------------------------------------
265

    
266
// offsets to nearest centimeter for index 0, all rest are +1 cm per value
267

    
268
#define CM_OFFSET_16M_NB    (-23)   // for normal band channels at 16 MHz PRF
269
#define CM_OFFSET_16M_WB    (-28)   // for wider  band channels at 16 MHz PRF
270
#define CM_OFFSET_64M_NB    (-17)   // for normal band channels at 64 MHz PRF
271
#define CM_OFFSET_64M_WB    (-30)   // for wider  band channels at 64 MHz PRF
272

    
273

    
274
//---------------------------------------------------------------------------------------------------------------------------
275
// range25cm16PRFnb: Range Bias Correction table for narrow band channels at 16 MHz PRF, NB: !!!! each MUST END IN 255 !!!!
276
//---------------------------------------------------------------------------------------------------------------------------
277

    
278
const uint8_t range25cm16PRFnb[4][NUM_16M_OFFSET] =
279
{
280
    // ch 1 - range25cm16PRFnb
281
    {
282
           1,
283
           3,
284
           4,
285
           5,
286
           7,
287
           9,
288
          11,
289
          12,
290
          13,
291
          15,
292
          18,
293
          20,
294
          23,
295
          25,
296
          28,
297
          30,
298
          33,
299
          36,
300
          40,
301
          43,
302
          47,
303
          50,
304
          54,
305
          58,
306
          63,
307
          66,
308
          71,
309
          76,
310
          82,
311
          89,
312
          98,
313
         109,
314
         127,
315
         155,
316
         222,
317
         255,
318
         255
319
    },
320

    
321
    // ch 2 - range25cm16PRFnb
322
    {
323
           1,
324
           2,
325
           4,
326
           5,
327
           6,
328
           8,
329
           9,
330
          10,
331
          12,
332
          13,
333
          15,
334
          18,
335
          20,
336
          22,
337
          24,
338
          27,
339
          29,
340
          32,
341
          35,
342
          38,
343
          41,
344
          44,
345
          47,
346
          51,
347
          55,
348
          58,
349
          62,
350
          66,
351
          71,
352
          78,
353
          85,
354
          96,
355
         111,
356
         135,
357
         194,
358
         240,
359
         255
360
    },
361

    
362
    // ch 3 - range25cm16PRFnb
363
    {
364
           1,
365
           2,
366
           3,
367
           4,
368
           5,
369
           7,
370
           8,
371
           9,
372
          10,
373
          12,
374
          14,
375
          16,
376
          18,
377
          20,
378
          22,
379
          24,
380
          26,
381
          28,
382
          31,
383
          33,
384
          36,
385
          39,
386
          42,
387
          45,
388
          49,
389
          52,
390
          55,
391
          59,
392
          63,
393
          69,
394
          76,
395
          85,
396
          98,
397
         120,
398
         173,
399
         213,
400
         255
401
    },
402

    
403
    // ch 5 - range25cm16PRFnb
404
    {
405
           1,
406
           1,
407
           2,
408
           3,
409
           4,
410
           5,
411
           6,
412
           6,
413
           7,
414
           8,
415
           9,
416
          11,
417
          12,
418
          14,
419
          15,
420
          16,
421
          18,
422
          20,
423
          21,
424
          23,
425
          25,
426
          27,
427
          29,
428
          31,
429
          34,
430
          36,
431
          38,
432
          41,
433
          44,
434
          48,
435
          53,
436
          59,
437
          68,
438
          83,
439
         120,
440
         148,
441
         255
442
    }
443
}; // end range25cm16PRFnb
444

    
445

    
446
//---------------------------------------------------------------------------------------------------------------------------
447
// range25cm16PRFwb: Range Bias Correction table for wide band channels at 16 MHz PRF, NB: !!!! each MUST END IN 255 !!!!
448
//---------------------------------------------------------------------------------------------------------------------------
449

    
450
const uint8_t range25cm16PRFwb[2][NUM_16M_OFFSETWB] =
451
{
452
    // ch 4 - range25cm16PRFwb
453
    {
454
           7,
455
           7,
456
           8,
457
           9,
458
           9,
459
          10,
460
          11,
461
          11,
462
          12,
463
          13,
464
          14,
465
          15,
466
          16,
467
          17,
468
          18,
469
          19,
470
          20,
471
          21,
472
          22,
473
          23,
474
          24,
475
          26,
476
          27,
477
          28,
478
          30,
479
          31,
480
          32,
481
          34,
482
          36,
483
          38,
484
          40,
485
          42,
486
          44,
487
          46,
488
          48,
489
          50,
490
          52,
491
          55,
492
          57,
493
          59,
494
          61,
495
          63,
496
          66,
497
          68,
498
          71,
499
          74,
500
          78,
501
          81,
502
          85,
503
          89,
504
          94,
505
          99,
506
         104,
507
         110,
508
         116,
509
         123,
510
         130,
511
         139,
512
         150,
513
         164,
514
         182,
515
         207,
516
         238,
517
         255,
518
         255,
519
         255,
520
         255,
521
         255
522
    },
523

    
524
    // ch 7 - range25cm16PRFwb
525
    {
526
           4,
527
           5,
528
           5,
529
           5,
530
           6,
531
           6,
532
           7,
533
           7,
534
           7,
535
           8,
536
           9,
537
           9,
538
          10,
539
          10,
540
          11,
541
          11,
542
          12,
543
          13,
544
          13,
545
          14,
546
          15,
547
          16,
548
          17,
549
          17,
550
          18,
551
          19,
552
          20,
553
          21,
554
          22,
555
          23,
556
          25,
557
          26,
558
          27,
559
          29,
560
          30,
561
          31,
562
          32,
563
          34,
564
          35,
565
          36,
566
          38,
567
          39,
568
          40,
569
          42,
570
          44,
571
          46,
572
          48,
573
          50,
574
          52,
575
          55,
576
          58,
577
          61,
578
          64,
579
          68,
580
          72,
581
          75,
582
          80,
583
          85,
584
          92,
585
         101,
586
         112,
587
         127,
588
         147,
589
         168,
590
         182,
591
         194,
592
         205,
593
         255
594
    }
595
}; // end range25cm16PRFwb
596

    
597
//---------------------------------------------------------------------------------------------------------------------------
598
// range25cm64PRFnb: Range Bias Correction table for narrow band channels at 64 MHz PRF, NB: !!!! each MUST END IN 255 !!!!
599
//---------------------------------------------------------------------------------------------------------------------------
600

    
601
const uint8_t range25cm64PRFnb[4][NUM_64M_OFFSET] =
602
{
603
    // ch 1 - range25cm64PRFnb
604
    {
605
           1,
606
           2,
607
           2,
608
           3,
609
           4,
610
           5,
611
           7,
612
          10,
613
          13,
614
          16,
615
          19,
616
          22,
617
          24,
618
          27,
619
          30,
620
          32,
621
          35,
622
          38,
623
          43,
624
          48,
625
          56,
626
          78,
627
         101,
628
         120,
629
         157,
630
         255
631
    },
632

    
633
    // ch 2 - range25cm64PRFnb
634
    {
635
           1,
636
           2,
637
           2,
638
           3,
639
           4,
640
           4,
641
           6,
642
           9,
643
          12,
644
          14,
645
          17,
646
          19,
647
          21,
648
          24,
649
          26,
650
          28,
651
          31,
652
          33,
653
          37,
654
          42,
655
          49,
656
          68,
657
          89,
658
         105,
659
         138,
660
         255
661
    },
662

    
663
    // ch 3 - range25cm64PRFnb
664
    {
665
           1,
666
           1,
667
           2,
668
           3,
669
           3,
670
           4,
671
           5,
672
           8,
673
          10,
674
          13,
675
          15,
676
          17,
677
          19,
678
          21,
679
          23,
680
          25,
681
          27,
682
          30,
683
          33,
684
          37,
685
          44,
686
          60,
687
          79,
688
          93,
689
         122,
690
         255
691
    },
692

    
693
    // ch 5 - range25cm64PRFnb
694
    {
695
           1,
696
           1,
697
           1,
698
           2,
699
           2,
700
           3,
701
           4,
702
           6,
703
           7,
704
           9,
705
          10,
706
          12,
707
          13,
708
          15,
709
          16,
710
          17,
711
          19,
712
          21,
713
          23,
714
          26,
715
          30,
716
          42,
717
          55,
718
          65,
719
          85,
720
         255
721
    }
722
}; // end range25cm64PRFnb
723

    
724
//---------------------------------------------------------------------------------------------------------------------------
725
// range25cm64PRFwb: Range Bias Correction table for wide band channels at 64 MHz PRF, NB: !!!! each MUST END IN 255 !!!!
726
//---------------------------------------------------------------------------------------------------------------------------
727

    
728
const uint8_t range25cm64PRFwb[2][NUM_64M_OFFSETWB] =
729
{
730
    // ch 4 - range25cm64PRFwb
731
    {
732
           7,
733
           8,
734
           8,
735
           9,
736
           9,
737
          10,
738
          11,
739
          12,
740
          13,
741
          13,
742
          14,
743
          15,
744
          16,
745
          16,
746
          17,
747
          18,
748
          19,
749
          19,
750
          20,
751
          21,
752
          22,
753
          24,
754
          25,
755
          27,
756
          28,
757
          29,
758
          30,
759
          32,
760
          33,
761
          34,
762
          35,
763
          37,
764
          39,
765
          41,
766
          43,
767
          45,
768
          48,
769
          50,
770
          53,
771
          56,
772
          60,
773
          64,
774
          68,
775
          74,
776
          81,
777
          89,
778
          98,
779
         109,
780
         122,
781
         136,
782
         146,
783
         154,
784
         162,
785
         178,
786
         220,
787
         249,
788
         255,
789
         255,
790
         255
791
    },
792

    
793
    // ch 7 - range25cm64PRFwb
794
    {
795
           4,
796
           5,
797
           5,
798
           5,
799
           6,
800
           6,
801
           7,
802
           7,
803
           8,
804
           8,
805
           9,
806
           9,
807
          10,
808
          10,
809
          10,
810
          11,
811
          11,
812
          12,
813
          13,
814
          13,
815
          14,
816
          15,
817
          16,
818
          16,
819
          17,
820
          18,
821
          19,
822
          19,
823
          20,
824
          21,
825
          22,
826
          23,
827
          24,
828
          25,
829
          26,
830
          28,
831
          29,
832
          31,
833
          33,
834
          35,
835
          37,
836
          39,
837
          42,
838
          46,
839
          50,
840
          54,
841
          60,
842
          67,
843
          75,
844
          83,
845
          90,
846
          95,
847
         100,
848
         110,
849
         135,
850
         153,
851
         172,
852
         192,
853
         255
854
    }
855
}; // end range25cm64PRFwb
856

    
857

    
858

    
859

    
860

    
861
/*! ------------------------------------------------------------------------------------------------------------------
862
 * Function: dwt_getrangebias()
863
 *
864
 * Description: This function is used to return the range bias correction need for TWR with DW1000 units.
865
 *
866
 * input parameters:        
867
 * @param chan  - specifies the operating channel (e.g. 1, 2, 3, 4, 5, 6 or 7) 
868
 * @param range - the calculated distance before correction
869
 * @param prf        - this is the PRF e.g. DWT_PRF_16M or DWT_PRF_64M
870
 *
871
 * output parameters
872
 *
873
 * returns correction needed in meters
874
 */
875
double dwt_getrangebias(uint8_t chan, float range, uint8_t prf)
876
{
877
    //first get the lookup index that corresponds to given range for a particular channel at 16M PRF
878
    int i = 0 ;
879
    int chanIdx ;
880
    int cmoffseti ;                                 // integer number of CM offset
881

    
882
    double mOffset ;                                // final offset result in metres
883

    
884
    // NB: note we may get some small negitive values e.g. up to -50 cm.
885

    
886
    int rangeint25cm = (int) (range * 4.00) ;       // convert range to integer number of 25cm values.
887

    
888
    if (rangeint25cm > 255) rangeint25cm = 255 ;    // make sure it matches largest value in table (all tables end in 255 !!!!)
889

    
890
    if (prf == DWT_PRF_16M)
891
    {
892
        switch(chan)
893
        {
894
            case 4:
895
            case 7:
896
            {
897
                chanIdx = chan_idxwb[chan];
898
                while (rangeint25cm > range25cm16PRFwb[chanIdx][i]) i++ ;       // find index in table corresponding to range
899
                cmoffseti = i + CM_OFFSET_16M_WB ;                              // nearest centimeter correction
900
            }
901
            break;
902
            default:
903
            {
904
                chanIdx = chan_idxnb[chan];
905
                while (rangeint25cm > range25cm16PRFnb[chanIdx][i]) i++ ;       // find index in table corresponding to range
906
                cmoffseti = i + CM_OFFSET_16M_NB ;                              // nearest centimeter correction
907
            }
908
        }//end of switch
909
    }
910
    else // 64M PRF
911
    {
912
        switch(chan)
913
        {
914
            case 4:
915
            case 7:
916
            {
917
                chanIdx = chan_idxwb[chan];
918
                while (rangeint25cm > range25cm64PRFwb[chanIdx][i]) i++ ;       // find index in table corresponding to range
919
                cmoffseti = i + CM_OFFSET_64M_WB ;                              // nearest centimeter correction
920
            }
921
            break;
922
            default:
923
            {
924
                chanIdx = chan_idxnb[chan];
925
                while (rangeint25cm > range25cm64PRFnb[chanIdx][i]) i++ ;       // find index in table corresponding to range
926
                cmoffseti = i + CM_OFFSET_64M_NB ;                              // nearest centimeter correction
927
            }
928
        }//end of switch
929
    } // end else
930

    
931

    
932
    mOffset = (float) cmoffseti ;                                       // offset result in centimmetres
933

    
934
    mOffset *= 0.01 ;                                                   // convert to metres
935

    
936
    return (mOffset) ;
937
}
938

    
939

    
940

    
941
// -------------------------------------------------------------------------------------------------------------------
942
//
943
// Internal functions for controlling and configuring the device
944
//
945
// -------------------------------------------------------------------------------------------------------------------
946

    
947
// Enable and Configure specified clocks
948
void _dwt_enableclocks(int clocks) ;
949
// Configure the ucode (FP algorithm) parameters
950
void _dwt_configlde(int prf);
951
// Load ucode from OTP/ROM
952
void _dwt_loaducodefromrom(void);
953
// Read non-volatile memory
954
uint32_t _dwt_otpread(uint32_t address);
955
// Program the non-volatile memory
956
uint32_t _dwt_otpprogword32(uint32_t data, uint16_t address);
957
// Upload the device configuration into always on memory
958
void _dwt_aonarrayupload(void);
959
// -------------------------------------------------------------------------------------------------------------------
960

    
961
/*!
962
 * Static data for DW1000 DecaWave Transceiver control
963
 */
964

    
965
static dwt_local_data_t dw1000local[DWT_NUM_DW_DEV] ; // Static local device data, can be an array to support multiple DW1000 testing applications/platforms
966
static dwt_local_data_t *pdw1000local = dw1000local ; // Static local data structure pointer
967

    
968

    
969
/*! ------------------------------------------------------------------------------------------------------------------
970
 * @fn dwt_setdevicedataptr()
971
 *
972
 * @brief This function sets the local data structure pointer to point to the structure in the local array as given by the index.
973
 *
974
 * input parameters
975
 * @param index    - selects the array object to point to. Must be within the array bounds, i.e. < DWT_NUM_DW_DEV
976
 *
977
 * output parameters
978
 *
979
 * returns DWT_SUCCESS for success, or DWT_ERROR for error
980
 */
981
int dwt_setdevicedataptr(unsigned int index)
982
{
983
    // Check the index is within the array bounds
984
    if (DWT_NUM_DW_DEV > index) // return error if index outside the array bounds
985
    {
986
        return DWT_ERROR ;
987
    }
988

    
989
    pdw1000local = &dw1000local[index];
990

    
991
    return DWT_SUCCESS ;
992
}
993

    
994
/*! ------------------------------------------------------------------------------------------------------------------
995
 * @fn dwt_initialise()
996
 *
997
 * @brief This function initiates communications with the DW1000 transceiver
998
 * and reads its DEV_ID register (address 0x00) to verify the IC is one supported
999
 * by this software (e.g. DW1000 32-bit device ID value is 0xDECA0130).  Then it
1000
 * does any initial once only device configurations needed for use and initialises
1001
 * as necessary any static data items belonging to this low-level driver.
1002
 *
1003
 * NOTES:
1004
 * 1.this function needs to be run before dwt_configuresleep, also the SPI frequency has to be < 3MHz
1005
 * 2.it also reads and applies LDO tune and crystal trim values from OTP memory
1006
 *
1007
 * input parameters
1008
 * @param config    -   specifies what configuration to load
1009
 *                  DWT_LOADUCODE     0x1 - load the LDE microcode from ROM - enabled accurate RX timestamp
1010
 *                  DWT_LOADNONE      0x0 - do not load any values from OTP memory
1011
 *
1012
 * output parameters
1013
 *
1014
 * returns DWT_SUCCESS for success, or DWT_ERROR for error
1015
 */
1016
// OTP addresses definitions
1017
#define LDOTUNE_ADDRESS (0x04)
1018
#define PARTID_ADDRESS (0x06)
1019
#define LOTID_ADDRESS  (0x07)
1020
#define VBAT_ADDRESS   (0x08)
1021
#define VTEMP_ADDRESS  (0x09)
1022
#define XTRIM_ADDRESS  (0x1E)
1023

    
1024
int dwt_initialise(const uint16_t config, DW1000Driver* drv)
1025
{
1026
    uint16_t otp_addr = 0;
1027
    uint32_t ldo_tune = 0;
1028

    
1029
    pdw1000local->dblbuffon = 0; // Double buffer mode off by default
1030
    pdw1000local->wait4resp = 0;
1031
    pdw1000local->sleep_mode = 0;
1032

    
1033
    pdw1000local->cbTxDone = NULL;
1034
    pdw1000local->cbRxOk = NULL;
1035
    pdw1000local->cbRxTo = NULL;
1036
    pdw1000local->cbRxErr = NULL;
1037

    
1038
    pdw1000local->driver = drv;
1039

    
1040
    // Read and validate device ID return -1 if not recognised
1041
    if (DWT_DEVICE_ID != dwt_readdevid()) // MP IC ONLY (i.e. DW1000) FOR THIS CODE
1042
    {
1043
        return DWT_ERROR ;
1044
    }
1045

    
1046
    // Make sure the device is completely reset before starting initialisation
1047
    dwt_softreset();
1048

    
1049
    _dwt_enableclocks(FORCE_SYS_XTI); // NOTE: set system clock to XTI - this is necessary to make sure the values read by _dwt_otpread are reliable
1050

    
1051
    // Configure the CPLL lock detect
1052
    dwt_write8bitoffsetreg(EXT_SYNC_ID, EC_CTRL_OFFSET, EC_CTRL_PLLLCK);
1053

    
1054
    // Read OTP revision number
1055
    otp_addr = _dwt_otpread(XTRIM_ADDRESS) & 0xffff;        // Read 32 bit value, XTAL trim val is in low octet-0 (5 bits)
1056
    pdw1000local->otprev = (otp_addr >> 8) & 0xff;            // OTP revision is next byte
1057

    
1058
    // Load LDO tune from OTP and kick it if there is a value actually programmed.
1059
    ldo_tune = _dwt_otpread(LDOTUNE_ADDRESS);
1060
    if((ldo_tune & 0xFF) != 0)
1061
    {
1062
        // Kick LDO tune
1063
        dwt_write8bitoffsetreg(OTP_IF_ID, OTP_SF, OTP_SF_LDO_KICK); // Set load LDE kick bit
1064
        pdw1000local->sleep_mode |= AON_WCFG_ONW_LLDO; // LDO tune must be kicked at wake-up
1065
    }
1066

    
1067
    // Load Part and Lot ID from OTP
1068
    pdw1000local->partID = _dwt_otpread(PARTID_ADDRESS);
1069
    pdw1000local->lotID = _dwt_otpread(LOTID_ADDRESS);
1070

    
1071
    // XTAL trim value is set in OTP for DW1000 module and EVK/TREK boards but that might not be the case in a custom design
1072
    pdw1000local->init_xtrim = otp_addr & 0x1F;
1073
    if (!pdw1000local->init_xtrim) // A value of 0 means that the crystal has not been trimmed
1074
    {
1075
        pdw1000local->init_xtrim = FS_XTALT_MIDRANGE ; // Set to mid-range if no calibration value inside
1076
    }
1077
    // Configure XTAL trim
1078
    dwt_setxtaltrim(pdw1000local->init_xtrim);
1079

    
1080
    // Load leading edge detect code
1081
    if(config & DWT_LOADUCODE)
1082
    {
1083
        _dwt_loaducodefromrom();
1084
        pdw1000local->sleep_mode |= AON_WCFG_ONW_LLDE; // microcode must be loaded at wake-up
1085
    }
1086
    else // Should disable the LDERUN enable bit in 0x36, 0x4
1087
    {
1088
        uint16_t rega = dwt_read16bitoffsetreg(PMSC_ID, PMSC_CTRL1_OFFSET+1) ;
1089
        rega &= 0xFDFF ; // Clear LDERUN bit
1090
        dwt_write16bitoffsetreg(PMSC_ID, PMSC_CTRL1_OFFSET+1, rega) ;
1091
    }
1092

    
1093
    _dwt_enableclocks(ENABLE_ALL_SEQ); // Enable clocks for sequencing
1094

    
1095
    // The 3 bits in AON CFG1 register must be cleared to ensure proper operation of the DW1000 in DEEPSLEEP mode.
1096
    dwt_write8bitoffsetreg(AON_ID, AON_CFG1_OFFSET, 0x00);
1097

    
1098
    // Read system register / store local copy
1099
    pdw1000local->sysCFGreg = dwt_read32bitreg(SYS_CFG_ID) ; // Read sysconfig register
1100

    
1101
    return DWT_SUCCESS ;
1102

    
1103
} // end dwt_initialise()
1104

    
1105
/*! ------------------------------------------------------------------------------------------------------------------
1106
 * @fn dwt_otprevision()
1107
 *
1108
 * @brief This is used to return the read OTP revision
1109
 *
1110
 * NOTE: dwt_initialise() must be called prior to this function so that it can return a relevant value.
1111
 *
1112
 * input parameters
1113
 *
1114
 * output parameters
1115
 *
1116
 * returns the read OTP revision value
1117
 */
1118
uint8_t dwt_otprevision(void)
1119
{
1120
    return pdw1000local->otprev ;
1121
}
1122

    
1123
/*! ------------------------------------------------------------------------------------------------------------------
1124
 * @fn dwt_setfinegraintxseq()
1125
 *
1126
 * @brief This function enables/disables the fine grain TX sequencing (enabled by default).
1127
 *
1128
 * input parameters
1129
 * @param enable - 1 to enable fine grain TX sequencing, 0 to disable it.
1130
 *
1131
 * output parameters none
1132
 *
1133
 * no return value
1134
 */
1135
void dwt_setfinegraintxseq(int enable)
1136
{
1137
    if (enable)
1138
    {
1139
        dwt_write16bitoffsetreg(PMSC_ID, PMSC_TXFINESEQ_OFFSET, PMSC_TXFINESEQ_ENABLE);
1140
    }
1141
    else
1142
    {
1143
        dwt_write16bitoffsetreg(PMSC_ID, PMSC_TXFINESEQ_OFFSET, PMSC_TXFINESEQ_DISABLE);
1144
    }
1145
}
1146

    
1147
/*! ------------------------------------------------------------------------------------------------------------------
1148
 * @fn dwt_setlnapamode()
1149
 *
1150
 * @brief This is used to enable GPIO for external LNA or PA functionality - HW dependent, consult the DW1000 User Manual.
1151
 *        This can also be used for debug as enabling TX and RX GPIOs is quite handy to monitor DW1000's activity.
1152
 *
1153
 * NOTE: Enabling PA functionality requires that fine grain TX sequencing is deactivated. This can be done using
1154
 *       dwt_setfinegraintxseq().
1155
 *
1156
 * input parameters
1157
 * @param lna - 1 to enable LNA functionality, 0 to disable it
1158
 * @param pa - 1 to enable PA functionality, 0 to disable it
1159
 *
1160
 * output parameters
1161
 *
1162
 * no return value
1163
 */
1164
void dwt_setlnapamode(int lna, int pa)
1165
{
1166
    uint32_t gpio_mode = dwt_read32bitoffsetreg(GPIO_CTRL_ID, GPIO_MODE_OFFSET);
1167
    gpio_mode &= ~(GPIO_MSGP4_MASK | GPIO_MSGP5_MASK | GPIO_MSGP6_MASK);
1168
    if (lna)
1169
    {
1170
        gpio_mode |= GPIO_PIN6_EXTRXE;
1171
    }
1172
    if (pa)
1173
    {
1174
        gpio_mode |= (GPIO_PIN5_EXTTXE | GPIO_PIN4_EXTPA);
1175
    }
1176
    dwt_write32bitoffsetreg(GPIO_CTRL_ID, GPIO_MODE_OFFSET, gpio_mode);
1177
}
1178

    
1179
/*! ------------------------------------------------------------------------------------------------------------------
1180
 * @fn dwt_setgpiodirection()
1181
 *
1182
 * @brief This is used to set GPIO direction as an input (1) or output (0)
1183
 *
1184
 * input parameters
1185
 * @param gpioNum    -   this is the GPIO to configure - see GxM0... GxM8 in the deca_regs.h file
1186
 * @param direction  -   this sets the GPIO direction - see GxP0... GxP8 in the deca_regs.h file
1187
 *
1188
 * output parameters
1189
 *
1190
 * no return value
1191
 */
1192
void dwt_setgpiodirection(uint32_t gpioNum, uint32_t direction)
1193
{
1194
    uint8_t buf[GPIO_DIR_LEN];
1195
    uint32_t command = direction | gpioNum;
1196

    
1197
    buf[0] = command & 0xff;
1198
    buf[1] = (command >> 8) & 0xff;
1199
    buf[2] = (command >> 16) & 0xff;
1200

    
1201
    dwt_writetodevice(GPIO_CTRL_ID, GPIO_DIR_OFFSET, GPIO_DIR_LEN, buf);
1202
}
1203

    
1204
/*! ------------------------------------------------------------------------------------------------------------------
1205
 * @fn dwt_setgpiovalue()
1206
 *
1207
 * @brief This is used to set GPIO value as (1) or (0) only applies if the GPIO is configured as output
1208
 *
1209
 * input parameters
1210
 * @param gpioNum    -   this is the GPIO to configure - see GxM0... GxM8 in the deca_regs.h file
1211
 * @param value  -   this sets the GPIO value - see GDP0... GDP8 in the deca_regs.h file
1212
 *
1213
 * output parameters
1214
 *
1215
 * no return value
1216
 */
1217
void dwt_setgpiovalue(uint32_t gpioNum, uint32_t value)
1218
{
1219
    uint8_t buf[GPIO_DOUT_LEN];
1220
    uint32_t command = value | gpioNum;
1221

    
1222
    buf[0] = command & 0xff;
1223
    buf[1] = (command >> 8) & 0xff;
1224
    buf[2] = (command >> 16) & 0xff;
1225

    
1226
    dwt_writetodevice(GPIO_CTRL_ID, GPIO_DOUT_OFFSET, GPIO_DOUT_LEN, buf);
1227
}
1228

    
1229
/*! ------------------------------------------------------------------------------------------------------------------
1230
 * @fn dwt_getpartid()
1231
 *
1232
 * @brief This is used to return the read part ID of the device
1233
 *
1234
 * NOTE: dwt_initialise() must be called prior to this function so that it can return a relevant value.
1235
 *
1236
 * input parameters
1237
 *
1238
 * output parameters
1239
 *
1240
 * returns the 32 bit part ID value as programmed in the factory
1241
 */
1242
uint32_t dwt_getpartid(void)
1243
{
1244
    return pdw1000local->partID;
1245
}
1246

    
1247
/*! ------------------------------------------------------------------------------------------------------------------
1248
 * @fn dwt_getlotid()
1249
 *
1250
 * @brief This is used to return the read lot ID of the device
1251
 *
1252
 * NOTE: dwt_initialise() must be called prior to this function so that it can return a relevant value.
1253
 *
1254
 * input parameters
1255
 *
1256
 * output parameters
1257
 *
1258
 * returns the 32 bit lot ID value as programmed in the factory
1259
 */
1260
uint32_t dwt_getlotid(void)
1261
{
1262
    return pdw1000local->lotID;
1263
}
1264

    
1265
/*! ------------------------------------------------------------------------------------------------------------------
1266
 * @fn dwt_readdevid()
1267
 *
1268
 * @brief This is used to return the read device type and revision information of the DW1000 device (MP part is 0xDECA0130)
1269
 *
1270
 * input parameters
1271
 *
1272
 * output parameters
1273
 *
1274
 * returns the read value which for DW1000 is 0xDECA0130
1275
 */
1276
uint32_t dwt_readdevid(void)
1277
{
1278
    return dwt_read32bitoffsetreg(DEV_ID_ID,0);
1279
}
1280

    
1281
/*! ------------------------------------------------------------------------------------------------------------------
1282
 * @fn dwt_configuretxrf()
1283
 *
1284
 * @brief This function provides the API for the configuration of the TX spectrum
1285
 * including the power and pulse generator delay. The input is a pointer to the data structure
1286
 * of type dwt_txconfig_t that holds all the configurable items.
1287
 *
1288
 * input parameters
1289
 * @param config    -   pointer to the txrf configuration structure, which contains the tx rf config data
1290
 *
1291
 * output parameters
1292
 *
1293
 * no return value
1294
 */
1295
void dwt_configuretxrf(dwt_txconfig_t* config)
1296
{
1297

    
1298
    // Configure RF TX PG_DELAY
1299
    dwt_write8bitoffsetreg(TX_CAL_ID, TC_PGDELAY_OFFSET, config->PGdly);
1300

    
1301
    // Configure TX power
1302
    dwt_write32bitreg(TX_POWER_ID, config->power);
1303

    
1304
}
1305

    
1306
/*! ------------------------------------------------------------------------------------------------------------------
1307
 * @fn dwt_configure()
1308
 *
1309
 * @brief This function provides the main API for the configuration of the
1310
 * DW1000 and this low-level driver.  The input is a pointer to the data structure
1311
 * of type dwt_config_t that holds all the configurable items.
1312
 * The dwt_config_t structure shows which ones are supported
1313
 *
1314
 * input parameters
1315
 * @param config    -   pointer to the configuration structure, which contains the device configuration data.
1316
 *
1317
 * output parameters
1318
 *
1319
 * no return value
1320
 */
1321
void dwt_configure(dwt_config_t *config)
1322
{
1323
    uint8_t nsSfd_result  = 0;
1324
    uint8_t useDWnsSFD = 0;
1325
    uint8_t chan = config->chan ;
1326
    uint32_t regval ;
1327
    uint16_t reg16 = lde_replicaCoeff[config->rxCode];
1328
    uint8_t prfIndex = config->prf - DWT_PRF_16M;
1329
    uint8_t bw = ((chan == 4) || (chan == 7)) ? 1 : 0 ; // Select wide or narrow band
1330

    
1331
#ifdef DWT_API_ERROR_CHECK
1332
    assert(config->dataRate <= DWT_BR_6M8);
1333
    assert(config->rxPAC <= DWT_PAC64);
1334
    assert((chan >= 1) && (chan <= 7) && (chan != 6));
1335
    assert(((config->prf == DWT_PRF_64M) && (config->txCode >= 9) && (config->txCode <= 24))
1336
           || ((config->prf == DWT_PRF_16M) && (config->txCode >= 1) && (config->txCode <= 8)));
1337
    assert(((config->prf == DWT_PRF_64M) && (config->rxCode >= 9) && (config->rxCode <= 24))
1338
           || ((config->prf == DWT_PRF_16M) && (config->rxCode >= 1) && (config->rxCode <= 8)));
1339
    assert((config->txPreambLength == DWT_PLEN_64) || (config->txPreambLength == DWT_PLEN_128) || (config->txPreambLength == DWT_PLEN_256)
1340
           || (config->txPreambLength == DWT_PLEN_512) || (config->txPreambLength == DWT_PLEN_1024) || (config->txPreambLength == DWT_PLEN_1536)
1341
           || (config->txPreambLength == DWT_PLEN_2048) || (config->txPreambLength == DWT_PLEN_4096));
1342
    assert((config->phrMode == DWT_PHRMODE_STD) || (config->phrMode == DWT_PHRMODE_EXT));
1343
#endif
1344

    
1345
    // For 110 kbps we need a special setup
1346
    if(DWT_BR_110K == config->dataRate)
1347
    {
1348
        pdw1000local->sysCFGreg |= SYS_CFG_RXM110K ;
1349
        reg16 >>= 3; // lde_replicaCoeff must be divided by 8
1350
    }
1351
    else
1352
    {
1353
        pdw1000local->sysCFGreg &= (~SYS_CFG_RXM110K) ;
1354
    }
1355

    
1356
    pdw1000local->longFrames = config->phrMode ;
1357

    
1358
    pdw1000local->sysCFGreg &= ~SYS_CFG_PHR_MODE_11;
1359
    pdw1000local->sysCFGreg |= (SYS_CFG_PHR_MODE_11 & (config->phrMode << SYS_CFG_PHR_MODE_SHFT));
1360

    
1361
    dwt_write32bitreg(SYS_CFG_ID,pdw1000local->sysCFGreg) ;
1362
    // Set the lde_replicaCoeff
1363
    dwt_write16bitoffsetreg(LDE_IF_ID, LDE_REPC_OFFSET, reg16) ;
1364

    
1365
    _dwt_configlde(prfIndex);
1366

    
1367
    // Configure PLL2/RF PLL block CFG/TUNE (for a given channel)
1368
    dwt_write32bitoffsetreg(FS_CTRL_ID, FS_PLLCFG_OFFSET, fs_pll_cfg[chan_idx[chan]]);
1369
    dwt_write8bitoffsetreg(FS_CTRL_ID, FS_PLLTUNE_OFFSET, fs_pll_tune[chan_idx[chan]]);
1370

    
1371
    // Configure RF RX blocks (for specified channel/bandwidth)
1372
    dwt_write8bitoffsetreg(RF_CONF_ID, RF_RXCTRLH_OFFSET, rx_config[bw]);
1373

    
1374
    // Configure RF TX blocks (for specified channel and PRF)
1375
    // Configure RF TX control
1376
    dwt_write32bitoffsetreg(RF_CONF_ID, RF_TXCTRL_OFFSET, tx_config[chan_idx[chan]]);
1377

    
1378
    // Configure the baseband parameters (for specified PRF, bit rate, PAC, and SFD settings)
1379
    // DTUNE0
1380
    dwt_write16bitoffsetreg(DRX_CONF_ID, DRX_TUNE0b_OFFSET, sftsh[config->dataRate][config->nsSFD]);
1381

    
1382
    // DTUNE1
1383
    dwt_write16bitoffsetreg(DRX_CONF_ID, DRX_TUNE1a_OFFSET, dtune1[prfIndex]);
1384

    
1385
    if(config->dataRate == DWT_BR_110K)
1386
    {
1387
        dwt_write16bitoffsetreg(DRX_CONF_ID, DRX_TUNE1b_OFFSET, DRX_TUNE1b_110K);
1388
    }
1389
    else
1390
    {
1391
        if(config->txPreambLength == DWT_PLEN_64)
1392
        {
1393
            dwt_write16bitoffsetreg(DRX_CONF_ID, DRX_TUNE1b_OFFSET, DRX_TUNE1b_6M8_PRE64);
1394
            dwt_write8bitoffsetreg(DRX_CONF_ID, DRX_TUNE4H_OFFSET, DRX_TUNE4H_PRE64);
1395
        }
1396
        else
1397
        {
1398
            dwt_write16bitoffsetreg(DRX_CONF_ID, DRX_TUNE1b_OFFSET, DRX_TUNE1b_850K_6M8);
1399
            dwt_write8bitoffsetreg(DRX_CONF_ID, DRX_TUNE4H_OFFSET, DRX_TUNE4H_PRE128PLUS);
1400
        }
1401
    }
1402

    
1403
    // DTUNE2
1404
    dwt_write32bitoffsetreg(DRX_CONF_ID, DRX_TUNE2_OFFSET, digital_bb_config[prfIndex][config->rxPAC]);
1405

    
1406
    // DTUNE3 (SFD timeout)
1407
    // Don't allow 0 - SFD timeout will always be enabled
1408
    if(config->sfdTO == 0)
1409
    {
1410
        config->sfdTO = DWT_SFDTOC_DEF;
1411
    }
1412
    dwt_write16bitoffsetreg(DRX_CONF_ID, DRX_SFDTOC_OFFSET, config->sfdTO);
1413

    
1414
    // Configure AGC parameters
1415
    dwt_write32bitoffsetreg( AGC_CFG_STS_ID, 0xC, agc_config.lo32);
1416
    dwt_write16bitoffsetreg( AGC_CFG_STS_ID, 0x4, agc_config.target[prfIndex]);
1417

    
1418
    // Set (non-standard) user SFD for improved performance,
1419
    if(config->nsSFD)
1420
    {
1421
        // Write non standard (DW) SFD length
1422
        dwt_write8bitoffsetreg(USR_SFD_ID, 0x00, dwnsSFDlen[config->dataRate]);
1423
        nsSfd_result = 3 ;
1424
        useDWnsSFD = 1 ;
1425
    }
1426
    regval =  (CHAN_CTRL_TX_CHAN_MASK & (chan << CHAN_CTRL_TX_CHAN_SHIFT)) | // Transmit Channel
1427
              (CHAN_CTRL_RX_CHAN_MASK & (chan << CHAN_CTRL_RX_CHAN_SHIFT)) | // Receive Channel
1428
              (CHAN_CTRL_RXFPRF_MASK & (config->prf << CHAN_CTRL_RXFPRF_SHIFT)) | // RX PRF
1429
              ((CHAN_CTRL_TNSSFD|CHAN_CTRL_RNSSFD) & (nsSfd_result << CHAN_CTRL_TNSSFD_SHIFT)) | // nsSFD enable RX&TX
1430
              (CHAN_CTRL_DWSFD & (useDWnsSFD << CHAN_CTRL_DWSFD_SHIFT)) | // Use DW nsSFD
1431
              (CHAN_CTRL_TX_PCOD_MASK & (config->txCode << CHAN_CTRL_TX_PCOD_SHIFT)) | // TX Preamble Code
1432
              (CHAN_CTRL_RX_PCOD_MASK & (config->rxCode << CHAN_CTRL_RX_PCOD_SHIFT)) ; // RX Preamble Code
1433

    
1434
    dwt_write32bitreg(CHAN_CTRL_ID,regval) ;
1435

    
1436
    // Set up TX Preamble Size, PRF and Data Rate
1437
    pdw1000local->txFCTRL = ((config->txPreambLength | config->prf) << TX_FCTRL_TXPRF_SHFT) | (config->dataRate << TX_FCTRL_TXBR_SHFT);
1438
    dwt_write32bitreg(TX_FCTRL_ID, pdw1000local->txFCTRL);
1439

    
1440
    // The SFD transmit pattern is initialised by the DW1000 upon a user TX request, but (due to an IC issue) it is not done for an auto-ACK TX. The
1441
    // SYS_CTRL write below works around this issue, by simultaneously initiating and aborting a transmission, which correctly initialises the SFD
1442
    // after its configuration or reconfiguration.
1443
    // This issue is not documented at the time of writing this code. It should be in next release of DW1000 User Manual (v2.09, from July 2016).
1444
    dwt_write8bitoffsetreg(SYS_CTRL_ID, SYS_CTRL_OFFSET, SYS_CTRL_TXSTRT | SYS_CTRL_TRXOFF); // Request TX start and TRX off at the same time
1445
} // end dwt_configure()
1446

    
1447
/*! ------------------------------------------------------------------------------------------------------------------
1448
 * @fn dwt_setrxantennadelay()
1449
 *
1450
 * @brief This API function writes the antenna delay (in time units) to RX registers
1451
 *
1452
 * input parameters:
1453
 * @param rxDelay - this is the total (RX) antenna delay value, which
1454
 *                          will be programmed into the RX register
1455
 *
1456
 * output parameters
1457
 *
1458
 * no return value
1459
 */
1460
void dwt_setrxantennadelay(uint16_t rxDelay)
1461
{
1462
    // Set the RX antenna delay for auto TX timestamp adjustment
1463
    dwt_write16bitoffsetreg(LDE_IF_ID, LDE_RXANTD_OFFSET, rxDelay);
1464
}
1465

    
1466
/*! ------------------------------------------------------------------------------------------------------------------
1467
 * @fn dwt_settxantennadelay()
1468
 *
1469
 * @brief This API function writes the antenna delay (in time units) to TX registers
1470
 *
1471
 * input parameters:
1472
 * @param txDelay - this is the total (TX) antenna delay value, which
1473
 *                          will be programmed into the TX delay register
1474
 *
1475
 * output parameters
1476
 *
1477
 * no return value
1478
 */
1479
void dwt_settxantennadelay(uint16_t txDelay)
1480
{
1481
    // Set the TX antenna delay for auto TX timestamp adjustment
1482
    dwt_write16bitoffsetreg(TX_ANTD_ID, TX_ANTD_OFFSET, txDelay);
1483
}
1484

    
1485
/*! ------------------------------------------------------------------------------------------------------------------
1486
 * @fn dwt_writetxdata()
1487
 *
1488
 * @brief This API function writes the supplied TX data into the DW1000's
1489
 * TX buffer.  The input parameters are the data length in bytes and a pointer
1490
 * to those data bytes.
1491
 *
1492
 * input parameters
1493
 * @param txFrameLength  - This is the total frame length, including the two byte CRC.
1494
 *                         Note: this is the length of TX message (including the 2 byte CRC) - max is 1023
1495
 *                         standard PHR mode allows up to 127 bytes
1496
 *                         if > 127 is programmed, DWT_PHRMODE_EXT needs to be set in the phrMode configuration
1497
 *                         see dwt_configure function
1498
 * @param txFrameBytes   - Pointer to the user’s buffer containing the data to send.
1499
 * @param txBufferOffset - This specifies an offset in the DW1000’s TX Buffer at which to start writing data.
1500
 *
1501
 * output parameters
1502
 *
1503
 * returns DWT_SUCCESS for success, or DWT_ERROR for error
1504
 */
1505
int dwt_writetxdata(uint16_t txFrameLength, uint8_t *txFrameBytes, uint16_t txBufferOffset)
1506
{
1507
#ifdef DWT_API_ERROR_CHECK
1508
    assert(txFrameLength >= 2);
1509
    assert((pdw1000local->longFrames && (txFrameLength <= 1023)) || (txFrameLength <= 127));
1510
    assert((txBufferOffset + txFrameLength) <= 1024);
1511
#endif
1512

    
1513
    if ((txBufferOffset + txFrameLength) <= 1024)
1514
    {
1515
        // Write the data to the IC TX buffer, (-2 bytes for auto generated CRC)
1516
        dwt_writetodevice( TX_BUFFER_ID, txBufferOffset, txFrameLength-2, txFrameBytes);
1517
        return DWT_SUCCESS;
1518
    }
1519
    else
1520
    {
1521
        return DWT_ERROR;
1522
    }
1523
} // end dwt_writetxdata()
1524

    
1525
/*! ------------------------------------------------------------------------------------------------------------------
1526
 * @fn dwt_writetxfctrl()
1527
 *
1528
 * @brief This API function configures the TX frame control register before the transmission of a frame
1529
 *
1530
 * input parameters:
1531
 * @param txFrameLength - this is the length of TX message (including the 2 byte CRC) - max is 1023
1532
 *                              NOTE: standard PHR mode allows up to 127 bytes
1533
 *                              if > 127 is programmed, DWT_PHRMODE_EXT needs to be set in the phrMode configuration
1534
 *                              see dwt_configure function
1535
 * @param txBufferOffset - the offset in the tx buffer to start writing the data
1536
 * @param ranging - 1 if this is a ranging frame, else 0
1537
 *
1538
 * output parameters
1539
 *
1540
 * no return value
1541
 */
1542
void dwt_writetxfctrl(uint16_t txFrameLength, uint16_t txBufferOffset, int ranging)
1543
{
1544

    
1545
#ifdef DWT_API_ERROR_CHECK
1546
    assert((pdw1000local->longFrames && (txFrameLength <= 1023)) || (txFrameLength <= 127));
1547
#endif
1548

    
1549
    // Write the frame length to the TX frame control register
1550
    // pdw1000local->txFCTRL has kept configured bit rate information
1551
    uint32_t reg32 = pdw1000local->txFCTRL | txFrameLength | (txBufferOffset << TX_FCTRL_TXBOFFS_SHFT) | (ranging << TX_FCTRL_TR_SHFT);
1552
    dwt_write32bitreg(TX_FCTRL_ID, reg32);
1553
} // end dwt_writetxfctrl()
1554

    
1555

    
1556
/*! ------------------------------------------------------------------------------------------------------------------
1557
 * @fn dwt_readrxdata()
1558
 *
1559
 * @brief This is used to read the data from the RX buffer, from an offset location give by offset parameter
1560
 *
1561
 * input parameters
1562
 * @param buffer - the buffer into which the data will be read
1563
 * @param length - the length of data to read (in bytes)
1564
 * @param rxBufferOffset - the offset in the rx buffer from which to read the data
1565
 *
1566
 * output parameters
1567
 *
1568
 * no return value
1569
 */
1570
void dwt_readrxdata(uint8_t *buffer, uint16_t length, uint16_t rxBufferOffset)
1571
{
1572
    dwt_readfromdevice(RX_BUFFER_ID,rxBufferOffset,length,buffer) ;
1573
}
1574

    
1575
/*! ------------------------------------------------------------------------------------------------------------------
1576
 * @fn dwt_readaccdata()
1577
 *
1578
 * @brief This is used to read the data from the Accumulator buffer, from an offset location give by offset parameter
1579
 *
1580
 * NOTE: Because of an internal memory access delay when reading the accumulator the first octet output is a dummy octet
1581
 *       that should be discarded. This is true no matter what sub-index the read begins at.
1582
 *
1583
 * input parameters
1584
 * @param buffer - the buffer into which the data will be read
1585
 * @param length - the length of data to read (in bytes)
1586
 * @param accOffset - the offset in the acc buffer from which to read the data
1587
 *
1588
 * output parameters
1589
 *
1590
 * no return value
1591
 */
1592
void dwt_readaccdata(uint8_t *buffer, uint16_t len, uint16_t accOffset)
1593
{
1594
    // Force on the ACC clocks if we are sequenced
1595
    _dwt_enableclocks(READ_ACC_ON);
1596

    
1597
    dwt_readfromdevice(ACC_MEM_ID,accOffset,len,buffer) ;
1598

    
1599
    _dwt_enableclocks(READ_ACC_OFF); // Revert clocks back
1600
}
1601

    
1602
/*! ------------------------------------------------------------------------------------------------------------------
1603
 * @fn dwt_readdiagnostics()
1604
 *
1605
 * @brief this function reads the RX signal quality diagnostic data
1606
 *
1607
 * input parameters
1608
 * @param diagnostics - diagnostic structure pointer, this will contain the diagnostic data read from the DW1000
1609
 *
1610
 * output parameters
1611
 *
1612
 * no return value
1613
 */
1614
void dwt_readdiagnostics(dwt_rxdiag_t *diagnostics)
1615
{
1616
    // Read the HW FP index
1617
    diagnostics->firstPath = dwt_read16bitoffsetreg(RX_TIME_ID, RX_TIME_FP_INDEX_OFFSET);
1618

    
1619
    // LDE diagnostic data
1620
    diagnostics->maxNoise = dwt_read16bitoffsetreg(LDE_IF_ID, LDE_THRESH_OFFSET);
1621

    
1622
    // Read all 8 bytes in one SPI transaction
1623
    dwt_readfromdevice(RX_FQUAL_ID, 0x0, 8, (uint8_t*)&diagnostics->stdNoise);
1624

    
1625
    diagnostics->firstPathAmp1 = dwt_read16bitoffsetreg(RX_TIME_ID, RX_TIME_FP_AMPL1_OFFSET);
1626

    
1627
    diagnostics->rxPreamCount = (dwt_read32bitreg(RX_FINFO_ID) & RX_FINFO_RXPACC_MASK) >> RX_FINFO_RXPACC_SHIFT  ;
1628
}
1629

    
1630
/*! ------------------------------------------------------------------------------------------------------------------
1631
 * @fn dwt_readtxtimestamp()
1632
 *
1633
 * @brief This is used to read the TX timestamp (adjusted with the programmed antenna delay)
1634
 *
1635
 * input parameters
1636
 * @param timestamp - a pointer to a 5-byte buffer which will store the read TX timestamp time
1637
 *
1638
 * output parameters - the timestamp buffer will contain the value after the function call
1639
 *
1640
 * no return value
1641
 */
1642
void dwt_readtxtimestamp(uint8_t * timestamp)
1643
{
1644
    dwt_readfromdevice(TX_TIME_ID, TX_TIME_TX_STAMP_OFFSET, TX_TIME_TX_STAMP_LEN, timestamp) ; // Read bytes directly into buffer
1645
}
1646

    
1647
/*! ------------------------------------------------------------------------------------------------------------------
1648
 * @fn dwt_readtxtimestamphi32()
1649
 *
1650
 * @brief This is used to read the high 32-bits of the TX timestamp (adjusted with the programmed antenna delay)
1651
 *
1652
 * input parameters
1653
 *
1654
 * output parameters
1655
 *
1656
 * returns high 32-bits of TX timestamp
1657
 */
1658
uint32_t dwt_readtxtimestamphi32(void)
1659
{
1660
    return dwt_read32bitoffsetreg(TX_TIME_ID, 1); // Offset is 1 to get the 4 upper bytes out of 5
1661
}
1662

    
1663
/*! ------------------------------------------------------------------------------------------------------------------
1664
 * @fn dwt_readtxtimestamplo32()
1665
 *
1666
 * @brief This is used to read the low 32-bits of the TX timestamp (adjusted with the programmed antenna delay)
1667
 *
1668
 * input parameters
1669
 *
1670
 * output parameters
1671
 *
1672
 * returns low 32-bits of TX timestamp
1673
 */
1674
uint32_t dwt_readtxtimestamplo32(void)
1675
{
1676
    return dwt_read32bitreg(TX_TIME_ID); // Read TX TIME as a 32-bit register to get the 4 lower bytes out of 5
1677
}
1678

    
1679
/*! ------------------------------------------------------------------------------------------------------------------
1680
 * @fn dwt_readrxtimestamp()
1681
 *
1682
 * @brief This is used to read the RX timestamp (adjusted time of arrival)
1683
 *
1684
 * input parameters
1685
 * @param timestamp - a pointer to a 5-byte buffer which will store the read RX timestamp time
1686
 *
1687
 * output parameters - the timestamp buffer will contain the value after the function call
1688
 *
1689
 * no return value
1690
 */
1691
void dwt_readrxtimestamp(uint8_t * timestamp)
1692
{
1693
    dwt_readfromdevice(RX_TIME_ID, RX_TIME_RX_STAMP_OFFSET, RX_TIME_RX_STAMP_LEN, timestamp) ; // Get the adjusted time of arrival
1694
}
1695

    
1696
/*! ------------------------------------------------------------------------------------------------------------------
1697
 * @fn dwt_readrxtimestamphi32()
1698
 *
1699
 * @brief This is used to read the high 32-bits of the RX timestamp (adjusted with the programmed antenna delay)
1700
 *
1701
 * input parameters
1702
 *
1703
 * output parameters
1704
 *
1705
 * returns high 32-bits of RX timestamp
1706
 */
1707
uint32_t dwt_readrxtimestamphi32(void)
1708
{
1709
    return dwt_read32bitoffsetreg(RX_TIME_ID, 1); // Offset is 1 to get the 4 upper bytes out of 5
1710
}
1711

    
1712
/*! ------------------------------------------------------------------------------------------------------------------
1713
 * @fn dwt_readrxtimestamplo32()
1714
 *
1715
 * @brief This is used to read the low 32-bits of the RX timestamp (adjusted with the programmed antenna delay)
1716
 *
1717
 * input parameters
1718
 *
1719
 * output parameters
1720
 *
1721
 * returns low 32-bits of RX timestamp
1722
 */
1723
uint32_t dwt_readrxtimestamplo32(void)
1724
{
1725
    return dwt_read32bitreg(RX_TIME_ID); // Read RX TIME as a 32-bit register to get the 4 lower bytes out of 5
1726
}
1727

    
1728
/*! ------------------------------------------------------------------------------------------------------------------
1729
 * @fn dwt_readsystimestamphi32()
1730
 *
1731
 * @brief This is used to read the high 32-bits of the system time
1732
 *
1733
 * input parameters
1734
 *
1735
 * output parameters
1736
 *
1737
 * returns high 32-bits of system time timestamp
1738
 */
1739
uint32_t dwt_readsystimestamphi32(void)
1740
{
1741
    return dwt_read32bitoffsetreg(SYS_TIME_ID, 1); // Offset is 1 to get the 4 upper bytes out of 5
1742
}
1743

    
1744
/*! ------------------------------------------------------------------------------------------------------------------
1745
 * @fn dwt_readsystime()
1746
 *
1747
 * @brief This is used to read the system time
1748
 *
1749
 * input parameters
1750
 * @param timestamp - a pointer to a 5-byte buffer which will store the read system time
1751
 *
1752
 * output parameters
1753
 * @param timestamp - the timestamp buffer will contain the value after the function call
1754
 *
1755
 * no return value
1756
 */
1757
void dwt_readsystime(uint8_t * timestamp)
1758
{
1759
    dwt_readfromdevice(SYS_TIME_ID, SYS_TIME_OFFSET, SYS_TIME_LEN, timestamp) ;
1760
}
1761

    
1762
/*! ------------------------------------------------------------------------------------------------------------------
1763
 * @fn dwt_writetodevice()
1764
 *
1765
 * @brief  this function is used to write to the DW1000 device registers
1766
 * Notes:
1767
 *        1. Firstly we create a header (the first byte is a header byte)
1768
 *        a. check if sub index is used, if subindexing is used - set bit-6 to 1 to signify that the sub-index address follows the register index byte
1769
 *        b. set bit-7 (or with 0x80) for write operation
1770
 *        c. if extended sub address index is used (i.e. if index > 127) set bit-7 of the first sub-index byte following the first header byte
1771
 *
1772
 *        2. Write the header followed by the data bytes to the DW1000 device
1773
 *
1774
 *
1775
 * input parameters:
1776
 * @param recordNumber  - ID of register file or buffer being accessed
1777
 * @param index         - byte index into register file or buffer being accessed
1778
 * @param length        - number of bytes being written
1779
 * @param buffer        - pointer to buffer containing the 'length' bytes to be written
1780
 *
1781
 * output parameters
1782
 *
1783
 * no return value
1784
 */
1785
void dwt_writetodevice
1786
(
1787
    uint16_t      recordNumber,
1788
    uint16_t      index,
1789
    uint32_t      length,
1790
    const uint8_t *buffer
1791
)
1792
{
1793
    uint8_t header[3] ; // Buffer to compose header in
1794
    int   cnt = 0; // Counter for length of header
1795
#ifdef DWT_API_ERROR_CHECK
1796
    assert(recordNumber <= 0x3F); // Record number is limited to 6-bits.
1797
#endif
1798

    
1799
    // Write message header selecting WRITE operation and addresses as appropriate (this is one to three bytes long)
1800
    if (index == 0) // For index of 0, no sub-index is required
1801
    {
1802
        header[cnt++] = 0x80 | recordNumber ; // Bit-7 is WRITE operation, bit-6 zero=NO sub-addressing, bits 5-0 is reg file id
1803
    }
1804
    else
1805
    {
1806
#ifdef DWT_API_ERROR_CHECK
1807
        assert((index <= 0x7FFF) && ((index + length) <= 0x7FFF)); // Index and sub-addressable area are limited to 15-bits.
1808
#endif
1809
        header[cnt++] = 0xC0 | recordNumber ; // Bit-7 is WRITE operation, bit-6 one=sub-address follows, bits 5-0 is reg file id
1810

    
1811
        if (index <= 127) // For non-zero index < 127, just a single sub-index byte is required
1812
        {
1813
            header[cnt++] = (uint8_t)index ; // Bit-7 zero means no extension, bits 6-0 is index.
1814
        }
1815
        else
1816
        {
1817
            header[cnt++] = 0x80 | (uint8_t)(index) ; // Bit-7 one means extended index, bits 6-0 is low seven bits of index.
1818
            header[cnt++] =  (uint8_t) (index >> 7) ; // 8-bit value = high eight bits of index.
1819
        }
1820
    }
1821

    
1822
    // Write it to the SPI
1823
    _alld_dw1000_writespi(cnt,header,length,buffer);
1824
} // end dwt_writetodevice()
1825

    
1826
/*! ------------------------------------------------------------------------------------------------------------------
1827
 * @fn dwt_readfromdevice()
1828
 *
1829
 * @brief  this function is used to read from the DW1000 device registers
1830
 * Notes:
1831
 *        1. Firstly we create a header (the first byte is a header byte)
1832
 *        a. check if sub index is used, if subindexing is used - set bit-6 to 1 to signify that the sub-index address follows the register index byte
1833
 *        b. set bit-7 (or with 0x80) for write operation
1834
 *        c. if extended sub address index is used (i.e. if index > 127) set bit-7 of the first sub-index byte following the first header byte
1835
 *
1836
 *        2. Write the header followed by the data bytes to the DW1000 device
1837
 *        3. Store the read data in the input buffer
1838
 *
1839
 * input parameters:
1840
 * @param recordNumber  - ID of register file or buffer being accessed
1841
 * @param index         - byte index into register file or buffer being accessed
1842
 * @param length        - number of bytes being read
1843
 * @param buffer        - pointer to buffer in which to return the read data.
1844
 *
1845
 * output parameters
1846
 *
1847
 * no return value
1848
 */
1849
void dwt_readfromdevice
1850
(
1851
    uint16_t  recordNumber,
1852
    uint16_t  index,
1853
    uint32_t  length,
1854
    uint8_t   *buffer
1855
)
1856
{
1857
    uint8_t header[3] ; // Buffer to compose header in
1858
    int   cnt = 0; // Counter for length of header
1859
#ifdef DWT_API_ERROR_CHECK
1860
    assert(recordNumber <= 0x3F); // Record number is limited to 6-bits.
1861
#endif
1862

    
1863
    // Write message header selecting READ operation and addresses as appropriate (this is one to three bytes long)
1864
    if (index == 0) // For index of 0, no sub-index is required
1865
    {
1866
        header[cnt++] = (uint8_t) recordNumber ; // Bit-7 zero is READ operation, bit-6 zero=NO sub-addressing, bits 5-0 is reg file id
1867
    }
1868
    else
1869
    {
1870
#ifdef DWT_API_ERROR_CHECK
1871
        assert((index <= 0x7FFF) && ((index + length) <= 0x7FFF)); // Index and sub-addressable area are limited to 15-bits.
1872
#endif
1873
        header[cnt++] = (uint8_t)(0x40 | recordNumber) ; // Bit-7 zero is READ operation, bit-6 one=sub-address follows, bits 5-0 is reg file id
1874

    
1875
        if (index <= 127) // For non-zero index < 127, just a single sub-index byte is required
1876
        {
1877
            header[cnt++] = (uint8_t) index ; // Bit-7 zero means no extension, bits 6-0 is index.
1878
        }
1879
        else
1880
        {
1881
            header[cnt++] = 0x80 | (uint8_t)(index) ; // Bit-7 one means extended index, bits 6-0 is low seven bits of index.
1882
            header[cnt++] =  (uint8_t) (index >> 7) ; // 8-bit value = high eight bits of index.
1883
        }
1884
    }
1885

    
1886
    // Do the read from the SPI
1887
    _alld_dw1000_readspi(cnt, header, length, buffer);  // result is stored in the buffer
1888
} // end dwt_readfromdevice()
1889

    
1890

    
1891

    
1892
/*! ------------------------------------------------------------------------------------------------------------------
1893
 * @fn dwt_read32bitoffsetreg()
1894
 *
1895
 * @brief  this function is used to read 32-bit value from the DW1000 device registers
1896
 *
1897
 * input parameters:
1898
 * @param regFileID - ID of register file or buffer being accessed
1899
 * @param regOffset - the index into register file or buffer being accessed
1900
 *
1901
 * output parameters
1902
 *
1903
 * returns 32 bit register value
1904
 */
1905
uint32_t dwt_read32bitoffsetreg(int regFileID,int regOffset)
1906
{
1907
    uint32_t  regval = 0 ;
1908
    int     j ;
1909
    uint8_t   buffer[4] ;
1910

    
1911
    dwt_readfromdevice(regFileID,regOffset,4,buffer); // Read 4 bytes (32-bits) register into buffer
1912

    
1913
    for (j = 3 ; j >= 0 ; j --)
1914
    {
1915
        regval = (regval << 8) + buffer[j] ;
1916
    }
1917
    return regval ;
1918

    
1919
} // end dwt_read32bitoffsetreg()
1920

    
1921
/*! ------------------------------------------------------------------------------------------------------------------
1922
 * @fn dwt_read16bitoffsetreg()
1923
 *
1924
 * @brief  this function is used to read 16-bit value from the DW1000 device registers
1925
 *
1926
 * input parameters:
1927
 * @param regFileID - ID of register file or buffer being accessed
1928
 * @param regOffset - the index into register file or buffer being accessed
1929
 *
1930
 * output parameters
1931
 *
1932
 * returns 16 bit register value
1933
 */
1934
uint16_t dwt_read16bitoffsetreg(int regFileID,int regOffset)
1935
{
1936
    uint16_t  regval = 0 ;
1937
    uint8_t   buffer[2] ;
1938

    
1939
    dwt_readfromdevice(regFileID,regOffset,2,buffer); // Read 2 bytes (16-bits) register into buffer
1940

    
1941
    regval = (buffer[1] << 8) + buffer[0] ;
1942
    return regval ;
1943

    
1944
} // end dwt_read16bitoffsetreg()
1945

    
1946
/*! ------------------------------------------------------------------------------------------------------------------
1947
 * @fn dwt_read8bitoffsetreg()
1948
 *
1949
 * @brief  this function is used to read an 8-bit value from the DW1000 device registers
1950
 *
1951
 * input parameters:
1952
 * @param regFileID - ID of register file or buffer being accessed
1953
 * @param regOffset - the index into register file or buffer being accessed
1954
 *
1955
 * output parameters
1956
 *
1957
 * returns 8-bit register value
1958
 */
1959
uint8_t dwt_read8bitoffsetreg(int regFileID, int regOffset)
1960
{
1961
    uint8_t regval;
1962

    
1963
    dwt_readfromdevice(regFileID, regOffset, 1, &regval);
1964

    
1965
    return regval ;
1966
}
1967

    
1968
/*! ------------------------------------------------------------------------------------------------------------------
1969
 * @fn dwt_write8bitoffsetreg()
1970
 *
1971
 * @brief  this function is used to write an 8-bit value to the DW1000 device registers
1972
 *
1973
 * input parameters:
1974
 * @param regFileID - ID of register file or buffer being accessed
1975
 * @param regOffset - the index into register file or buffer being accessed
1976
 * @param regval    - the value to write
1977
 *
1978
 * output parameters
1979
 *
1980
 * no return value
1981
 */
1982
void dwt_write8bitoffsetreg(int regFileID, int regOffset, uint8_t regval)
1983
{
1984
    dwt_writetodevice(regFileID, regOffset, 1, &regval);
1985
}
1986

    
1987
/*! ------------------------------------------------------------------------------------------------------------------
1988
 * @fn dwt_write16bitoffsetreg()
1989
 *
1990
 * @brief  this function is used to write 16-bit value to the DW1000 device registers
1991
 *
1992
 * input parameters:
1993
 * @param regFileID - ID of register file or buffer being accessed
1994
 * @param regOffset - the index into register file or buffer being accessed
1995
 * @param regval    - the value to write
1996
 *
1997
 * output parameters
1998
 *
1999
 * no return value
2000
 */
2001
void dwt_write16bitoffsetreg(int regFileID,int regOffset,uint16_t regval)
2002
{
2003
    uint8_t   buffer[2] ;
2004

    
2005
    buffer[0] = regval & 0xFF;
2006
    buffer[1] = regval >> 8 ;
2007

    
2008
    dwt_writetodevice(regFileID,regOffset,2,buffer);
2009
} // end dwt_write16bitoffsetreg()
2010

    
2011
/*! ------------------------------------------------------------------------------------------------------------------
2012
 * @fn dwt_write32bitoffsetreg()
2013
 *
2014
 * @brief  this function is used to write 32-bit value to the DW1000 device registers
2015
 *
2016
 * input parameters:
2017
 * @param regFileID - ID of register file or buffer being accessed
2018
 * @param regOffset - the index into register file or buffer being accessed
2019
 * @param regval    - the value to write
2020
 *
2021
 * output parameters
2022
 *
2023
 * no return value
2024
 */
2025
void dwt_write32bitoffsetreg(int regFileID,int regOffset,uint32_t regval)
2026
{
2027
    int     j ;
2028
    uint8_t   buffer[4] ;
2029

    
2030
    for ( j = 0 ; j < 4 ; j++ )
2031
    {
2032
        buffer[j] = regval & 0xff ;
2033
        regval >>= 8 ;
2034
    }
2035

    
2036
    dwt_writetodevice(regFileID,regOffset,4,buffer);
2037
} // end dwt_write32bitoffsetreg()
2038

    
2039
/*! ------------------------------------------------------------------------------------------------------------------
2040
 * @fn dwt_enableframefilter()
2041
 *
2042
 * @brief This is used to enable the frame filtering - (the default option is to
2043
 * accept any data and ACK frames with correct destination address
2044
 *
2045
 * input parameters
2046
 * @param - bitmask - enables/disables the frame filtering options according to
2047
 *      DWT_FF_NOTYPE_EN        0x000   no frame types allowed
2048
 *      DWT_FF_COORD_EN         0x002   behave as coordinator (can receive frames with no destination address (PAN ID has to match))
2049
 *      DWT_FF_BEACON_EN        0x004   beacon frames allowed
2050
 *      DWT_FF_DATA_EN          0x008   data frames allowed
2051
 *      DWT_FF_ACK_EN           0x010   ack frames allowed
2052
 *      DWT_FF_MAC_EN           0x020   mac control frames allowed
2053
 *      DWT_FF_RSVD_EN          0x040   reserved frame types allowed
2054
 *
2055
 * output parameters
2056
 *
2057
 * no return value
2058
 */
2059
void dwt_enableframefilter(uint16_t enable)
2060
{
2061
    uint32_t sysconfig = SYS_CFG_MASK & dwt_read32bitreg(SYS_CFG_ID) ; // Read sysconfig register
2062

    
2063
    if(enable)
2064
    {
2065
        // Enable frame filtering and configure frame types
2066
        sysconfig &= ~(SYS_CFG_FF_ALL_EN); // Clear all
2067
        sysconfig |= (enable & SYS_CFG_FF_ALL_EN) | SYS_CFG_FFE;
2068
    }
2069
    else
2070
    {
2071
        sysconfig &= ~(SYS_CFG_FFE);
2072
    }
2073

    
2074
    pdw1000local->sysCFGreg = sysconfig ;
2075
    dwt_write32bitreg(SYS_CFG_ID,sysconfig) ;
2076
}
2077

    
2078
/*! ------------------------------------------------------------------------------------------------------------------
2079
 * @fn dwt_setpanid()
2080
 *
2081
 * @brief This is used to set the PAN ID
2082
 *
2083
 * input parameters
2084
 * @param panID - this is the PAN ID
2085
 *
2086
 * output parameters
2087
 *
2088
 * no return value
2089
 */
2090
void dwt_setpanid(uint16_t panID)
2091
{
2092
    // PAN ID is high 16 bits of register
2093
    dwt_write16bitoffsetreg(PANADR_ID, PANADR_PAN_ID_OFFSET, panID);
2094
}
2095

    
2096
/*! ------------------------------------------------------------------------------------------------------------------
2097
 * @fn dwt_setaddress16()
2098
 *
2099
 * @brief This is used to set 16-bit (short) address
2100
 *
2101
 * input parameters
2102
 * @param shortAddress - this sets the 16 bit short address
2103
 *
2104
 * output parameters
2105
 *
2106
 * no return value
2107
 */
2108
void dwt_setaddress16(uint16_t shortAddress)
2109
{
2110
    // Short address into low 16 bits
2111
    dwt_write16bitoffsetreg(PANADR_ID, PANADR_SHORT_ADDR_OFFSET, shortAddress);
2112
}
2113

    
2114
/*! ------------------------------------------------------------------------------------------------------------------
2115
 * @fn dwt_seteui()
2116
 *
2117
 * @brief This is used to set the EUI 64-bit (long) address
2118
 *
2119
 * input parameters
2120
 * @param eui64 - this is the pointer to a buffer that contains the 64bit address
2121
 *
2122
 * output parameters
2123
 *
2124
 * no return value
2125
 */
2126
void dwt_seteui(uint8_t *eui64)
2127
{
2128
    dwt_writetodevice(EUI_64_ID, EUI_64_OFFSET, EUI_64_LEN, eui64);
2129
}
2130

    
2131
/*! ------------------------------------------------------------------------------------------------------------------
2132
 * @fn dwt_geteui()
2133
 *
2134
 * @brief This is used to get the EUI 64-bit from the DW1000
2135
 *
2136
 * input parameters
2137
 * @param eui64 - this is the pointer to a buffer that will contain the read 64-bit EUI value
2138
 *
2139
 * output parameters
2140
 *
2141
 * no return value
2142
 */
2143
void dwt_geteui(uint8_t *eui64)
2144
{
2145
    dwt_readfromdevice(EUI_64_ID, EUI_64_OFFSET, EUI_64_LEN, eui64);
2146
}
2147

    
2148
/*! ------------------------------------------------------------------------------------------------------------------
2149
 * @fn dwt_otpread()
2150
 *
2151
 * @brief This is used to read the OTP data from given address into provided array
2152
 *
2153
 * input parameters
2154
 * @param address - this is the OTP address to read from
2155
 * @param array - this is the pointer to the array into which to read the data
2156
 * @param length - this is the number of 32 bit words to read (array needs to be at least this length)
2157
 *
2158
 * output parameters
2159
 *
2160
 * no return value
2161
 */
2162
void dwt_otpread(uint32_t address, uint32_t *array, uint8_t length)
2163
{
2164
    int i;
2165

    
2166
    _dwt_enableclocks(FORCE_SYS_XTI); // NOTE: Set system clock to XTAL - this is necessary to make sure the values read by _dwt_otpread are reliable
2167

    
2168
    for(i=0; i<length; i++)
2169
    {
2170
        array[i] = _dwt_otpread(address + i) ;
2171
    }
2172

    
2173
    _dwt_enableclocks(ENABLE_ALL_SEQ); // Restore system clock to PLL
2174

    
2175
    return ;
2176
}
2177

    
2178
/*! ------------------------------------------------------------------------------------------------------------------
2179
 * @fn _dwt_otpread()
2180
 *
2181
 * @brief function to read the OTP memory. Ensure that MR,MRa,MRb are reset to 0.
2182
 *
2183
 * input parameters
2184
 * @param address - address to read at
2185
 *
2186
 * output parameters
2187
 *
2188
 * returns the 32bit of read data
2189
 */
2190
uint32_t _dwt_otpread(uint32_t address)
2191
{
2192
    uint32_t ret_data;
2193

    
2194
    // Write the address
2195
    dwt_write16bitoffsetreg(OTP_IF_ID, OTP_ADDR, address);
2196

    
2197
    // Perform OTP Read - Manual read mode has to be set
2198
    dwt_write8bitoffsetreg(OTP_IF_ID, OTP_CTRL, OTP_CTRL_OTPREAD | OTP_CTRL_OTPRDEN);
2199
    dwt_write8bitoffsetreg(OTP_IF_ID, OTP_CTRL, 0x00); // OTPREAD is self clearing but OTPRDEN is not
2200

    
2201
    // Read read data, available 40ns after rising edge of OTP_READ
2202
    ret_data = dwt_read32bitoffsetreg(OTP_IF_ID, OTP_RDAT);
2203

    
2204
    // Return the 32bit of read data
2205
    return ret_data;
2206
}
2207

    
2208
/*! ------------------------------------------------------------------------------------------------------------------
2209
 * @fn _dwt_otpsetmrregs()
2210
 *
2211
 * @brief Configure the MR registers for initial programming (enable charge pump).
2212
 * Read margin is used to stress the read back from the
2213
 * programmed bit. In normal operation this is relaxed.
2214
 *
2215
 * input parameters
2216
 * @param mode - "0" : Reset all to 0x0:           MRA=0x0000, MRB=0x0000, MR=0x0000
2217
 *               "1" : Set for inital programming: MRA=0x9220, MRB=0x000E, MR=0x1024
2218
 *               "2" : Set for soak programming:   MRA=0x9220, MRB=0x0003, MR=0x1824
2219
 *               "3" : High Vpp:                   MRA=0x9220, MRB=0x004E, MR=0x1824
2220
 *               "4" : Low Read Margin:            MRA=0x0000, MRB=0x0003, MR=0x0000
2221
 *               "5" : Array Clean:                MRA=0x0049, MRB=0x0003, MR=0x0024
2222
 *               "4" : Very Low Read Margin:       MRA=0x0000, MRB=0x0003, MR=0x0000
2223
 *
2224
 * output parameters
2225
 *
2226
 * returns DWT_SUCCESS for success, or DWT_ERROR for error
2227
 */
2228
uint32_t _dwt_otpsetmrregs(int mode)
2229
{
2230
    uint8_t rd_buf[4];
2231
    uint8_t wr_buf[4];
2232
    uint32_t mra=0,mrb=0,mr=0;
2233

    
2234
    // PROGRAMME MRA
2235
    // Set MRA, MODE_SEL
2236
    wr_buf[0] = 0x03;
2237
    dwt_writetodevice(OTP_IF_ID, OTP_CTRL+1,1,wr_buf);
2238

    
2239
    // Load data
2240
    switch(mode&0x0f) {
2241
    case 0x0 :
2242
        mr =0x0000;
2243
        mra=0x0000;
2244
        mrb=0x0000;
2245
        break;
2246
    case 0x1 :
2247
        mr =0x1024;
2248
        mra=0x9220; // Enable CPP mon
2249
        mrb=0x000e;
2250
        break;
2251
    case 0x2 :
2252
        mr =0x1824;
2253
        mra=0x9220;
2254
        mrb=0x0003;
2255
        break;
2256
    case 0x3 :
2257
        mr =0x1824;
2258
        mra=0x9220;
2259
        mrb=0x004e;
2260
        break;
2261
    case 0x4 :
2262
        mr =0x0000;
2263
        mra=0x0000;
2264
        mrb=0x0003;
2265
        break;
2266
    case 0x5 :
2267
        mr =0x0024;
2268
        mra=0x0000;
2269
        mrb=0x0003;
2270
        break;
2271
    default :
2272
        return DWT_ERROR;
2273
    }
2274

    
2275
    wr_buf[0] = mra & 0x00ff;
2276
    wr_buf[1] = (mra & 0xff00)>>8;
2277
    dwt_writetodevice(OTP_IF_ID, OTP_WDAT,2,wr_buf);
2278

    
2279

    
2280
    // Set WRITE_MR
2281
    wr_buf[0] = 0x08;
2282
    dwt_writetodevice(OTP_IF_ID, OTP_CTRL,1,wr_buf);
2283

    
2284
    // Wait?
2285

    
2286
    // Set Clear Mode sel
2287
    wr_buf[0] = 0x02;
2288
    dwt_writetodevice(OTP_IF_ID,OTP_CTRL+1,1,wr_buf);
2289

    
2290
    // Set AUX update, write MR
2291
    wr_buf[0] = 0x88;
2292
    dwt_writetodevice(OTP_IF_ID, OTP_CTRL,1,wr_buf);
2293
    // Clear write MR
2294
    wr_buf[0] = 0x80;
2295
    dwt_writetodevice(OTP_IF_ID, OTP_CTRL,1,wr_buf);
2296
    // Clear AUX update
2297
    wr_buf[0] = 0x00;
2298
    dwt_writetodevice(OTP_IF_ID, OTP_CTRL,1,wr_buf);
2299

    
2300
    ///////////////////////////////////////////
2301
    // PROGRAM MRB
2302
    // Set SLOW, MRB, MODE_SEL
2303
    wr_buf[0] = 0x05;
2304
    dwt_writetodevice(OTP_IF_ID,OTP_CTRL+1,1,wr_buf);
2305

    
2306
    wr_buf[0] = mrb & 0x00ff;
2307
    wr_buf[1] = (mrb & 0xff00)>>8;
2308
    dwt_writetodevice(OTP_IF_ID, OTP_WDAT,2,wr_buf);
2309

    
2310
    // Set WRITE_MR
2311
    wr_buf[0] = 0x08;
2312
    dwt_writetodevice(OTP_IF_ID, OTP_CTRL,1,wr_buf);
2313

    
2314
    // Wait?
2315

    
2316
    // Set Clear Mode sel
2317
    wr_buf[0] = 0x04;
2318
    dwt_writetodevice(OTP_IF_ID,OTP_CTRL+1,1,wr_buf);
2319

    
2320
    // Set AUX update, write MR
2321
    wr_buf[0] = 0x88;
2322
    dwt_writetodevice(OTP_IF_ID, OTP_CTRL,1,wr_buf);
2323
    // Clear write MR
2324
    wr_buf[0] = 0x80;
2325
    dwt_writetodevice(OTP_IF_ID, OTP_CTRL,1,wr_buf);
2326
    // Clear AUX update
2327
    wr_buf[0] = 0x00;
2328
    dwt_writetodevice(OTP_IF_ID, OTP_CTRL,1,wr_buf);
2329

    
2330
    ///////////////////////////////////////////
2331
    // PROGRAM MR
2332
    // Set SLOW, MODE_SEL
2333
    wr_buf[0] = 0x01;
2334
    dwt_writetodevice(OTP_IF_ID,OTP_CTRL+1,1,wr_buf);
2335
    // Load data
2336

    
2337
    wr_buf[0] = mr & 0x00ff;
2338
    wr_buf[1] = (mr & 0xff00)>>8;
2339
    dwt_writetodevice(OTP_IF_ID, OTP_WDAT,2,wr_buf);
2340

    
2341
    // Set WRITE_MR
2342
    wr_buf[0] = 0x08;
2343
    dwt_writetodevice(OTP_IF_ID, OTP_CTRL,1,wr_buf);
2344

    
2345
    // Wait?
2346
    deca_sleep(10);
2347
    // Set Clear Mode sel
2348
    wr_buf[0] = 0x00;
2349
    dwt_writetodevice(OTP_IF_ID,OTP_CTRL+1,1,wr_buf);
2350

    
2351
    // Read confirm mode writes.
2352
    // Set man override, MRA_SEL
2353
    wr_buf[0] = OTP_CTRL_OTPRDEN;
2354
    dwt_writetodevice(OTP_IF_ID, OTP_CTRL,1,wr_buf);
2355
    wr_buf[0] = 0x02;
2356
    dwt_writetodevice(OTP_IF_ID,OTP_CTRL+1,1,wr_buf);
2357
    // MRB_SEL
2358
    wr_buf[0] = 0x04;
2359
    dwt_writetodevice(OTP_IF_ID,OTP_CTRL+1,1,wr_buf);
2360
    deca_sleep(100);
2361

    
2362
    // Clear mode sel
2363
    wr_buf[0] = 0x00;
2364
    dwt_writetodevice(OTP_IF_ID,OTP_CTRL+1,1,wr_buf);
2365
    // Clear MAN_OVERRIDE
2366
    wr_buf[0] = 0x00;
2367
    dwt_writetodevice(OTP_IF_ID, OTP_CTRL,1,wr_buf);
2368

    
2369
    deca_sleep(10);
2370

    
2371
    if (((mode&0x0f) == 0x1)||((mode&0x0f) == 0x2))
2372
    {
2373
        // Read status register
2374
        dwt_readfromdevice(OTP_IF_ID, OTP_STAT,1,rd_buf);
2375
    }
2376

    
2377
    return DWT_SUCCESS;
2378
}
2379

    
2380
/*! ------------------------------------------------------------------------------------------------------------------
2381
 * @fn _dwt_otpprogword32()
2382
 *
2383
 * @brief function to program the OTP memory. Ensure that MR,MRa,MRb are reset to 0.
2384
 * VNM Charge pump needs to be enabled (see _dwt_otpsetmrregs)
2385
 * Note the address is only 11 bits long.
2386
 *
2387
 * input parameters
2388
 * @param address - address to read at
2389
 *
2390
 * output parameters
2391
 *
2392
 * returns DWT_SUCCESS for success, or DWT_ERROR for error
2393
 */
2394
uint32_t _dwt_otpprogword32(uint32_t data, uint16_t address)
2395
{
2396
    uint8_t rd_buf[1];
2397
    uint8_t wr_buf[4];
2398
    uint8_t otp_done;
2399

    
2400
    // Read status register
2401
    dwt_readfromdevice(OTP_IF_ID, OTP_STAT, 1, rd_buf);
2402

    
2403
    if((rd_buf[0] & 0x02) != 0x02)
2404
    {
2405
        return DWT_ERROR;
2406
    }
2407

    
2408
    // Write the data
2409
    wr_buf[3] = (data>>24) & 0xff;
2410
    wr_buf[2] = (data>>16) & 0xff;
2411
    wr_buf[1] = (data>>8) & 0xff;
2412
    wr_buf[0] = data & 0xff;
2413
    dwt_writetodevice(OTP_IF_ID, OTP_WDAT, 4, wr_buf);
2414

    
2415
    // Write the address [10:0]
2416
    wr_buf[1] = (address>>8) & 0x07;
2417
    wr_buf[0] = address & 0xff;
2418
    dwt_writetodevice(OTP_IF_ID, OTP_ADDR, 2, wr_buf);
2419

    
2420
    // Enable Sequenced programming
2421
    wr_buf[0] = OTP_CTRL_OTPPROG;
2422
    dwt_writetodevice(OTP_IF_ID, OTP_CTRL, 1, wr_buf);
2423
    wr_buf[0] = 0x00; // And clear
2424
    dwt_writetodevice(OTP_IF_ID, OTP_CTRL, 1, wr_buf);
2425

    
2426
    // WAIT for status to flag PRGM OK..
2427
    otp_done = 0;
2428
    while(otp_done == 0)
2429
    {
2430
        deca_sleep(1);
2431
        dwt_readfromdevice(OTP_IF_ID, OTP_STAT, 1, rd_buf);
2432

    
2433
        if((rd_buf[0] & 0x01) == 0x01)
2434
        {
2435
            otp_done = 1;
2436
        }
2437
    }
2438

    
2439
    return DWT_SUCCESS;
2440
}
2441

    
2442
/*! ------------------------------------------------------------------------------------------------------------------
2443
 * @fn dwt_otpwriteandverify()
2444
 *
2445
 * @brief This is used to program 32-bit value into the DW1000 OTP memory.
2446
 *
2447
 * input parameters
2448
 * @param value - this is the 32-bit value to be programmed into OTP
2449
 * @param address - this is the 16-bit OTP address into which the 32-bit value is programmed
2450
 *
2451
 * output parameters
2452
 *
2453
 * returns DWT_SUCCESS for success, or DWT_ERROR for error
2454
 */
2455
int dwt_otpwriteandverify(uint32_t value, uint16_t address)
2456
{
2457
    int prog_ok = DWT_SUCCESS;
2458
    int retry = 0;
2459
    // Firstly set the system clock to crystal
2460
    _dwt_enableclocks(FORCE_SYS_XTI); //set system clock to XTI
2461