Statistics
| Branch: | Tag: | Revision:

amiro-lld / drivers / DW1000 / v1 / deca_instance_common.c @ 9466e34d

History | View | Annotate | Download (71.11 KB)

1
/*! ----------------------------------------------------------------------------
2
 *  @file    instance_common.c
3
 *  @brief   DecaWave application level common instance functions
4
 *
5
 * @attention
6
 *
7
 * Copyright 2015 (c) DecaWave Ltd, Dublin, Ireland.
8
 *
9
 * All rights reserved.
10
 *
11
 * @author DecaWave
12
 */
13

    
14
#include <alld_DW1000.h>
15
#include <module.h>
16
#include <string.h>
17
#include <math.h>
18
#include <deca_instance.h>
19

    
20

    
21
/*! @brief Software defined configuration settings for RTLS applications */
22
/*! Configuration for DecaRangeRTLS TREK Modes (4 default use cases selected by the switch S1 [2,3] on EVB1000, indexed 0 to 3 )*/
23
instanceConfig_t chConfig[4] ={
24
                               //mode 1 - S1: 2 off, 3 off
25
                               {
26
                                .channelNumber = 2,             // channel
27
                                .preambleCode = 4,              // preambleCode
28
                                .pulseRepFreq = DWT_PRF_16M,    // prf
29
                                .dataRate = DWT_BR_110K,        // datarate
30
                                .preambleLen = DWT_PLEN_1024,   // preambleLength
31
                                .pacSize = DWT_PAC32,           // pacSize
32
                                .nsSFD = 1,                     // non-standard SFD
33
                                .sfdTO = (1025 + 64 - 32)       // SFD timeout
34
                               },
35
                               //mode 2 - S1: 2 on, 3 off
36
                               {
37
                                .channelNumber = 2,            // channel
38
                                .preambleCode = 4,             // preambleCode
39
                                .pulseRepFreq = DWT_PRF_16M,   // prf
40
                                .dataRate = DWT_BR_6M8,        // datarate
41
                                .preambleLen = DWT_PLEN_128,   // preambleLength
42
                                .pacSize = DWT_PAC8,           // pacSize
43
                                .nsSFD = 0,                    // non-standard SFD
44
                                .sfdTO = (129 + 8 - 8)        // SFD timeout
45
                               },
46
                               //mode 3 - S1: 2 off, 3 on
47
                               {
48
                                .channelNumber = 5,             // channel
49
                                .preambleCode = 3,              // preambleCode
50
                                .pulseRepFreq = DWT_PRF_16M,    // prf
51
                                .dataRate = DWT_BR_110K,        // datarate
52
                                .preambleLen = DWT_PLEN_1024,   // preambleLength
53
                                .pacSize = DWT_PAC32,           // pacSize
54
                                .nsSFD = 1,                     // non-standard SFD
55
                                .sfdTO = (1025 + 64 - 32)       // SFD timeout
56
                               },
57
                               //mode 4 - S1: 2 on, 3 on
58
                               {
59
                                .channelNumber = 5,            // channel
60
                                .preambleCode = 3,             // preambleCode
61
                                .pulseRepFreq = DWT_PRF_16M,   // prf
62
                                .dataRate = DWT_BR_6M8,        // datarate
63
                                .preambleLen = DWT_PLEN_128,   // preambleLength
64
                                .pacSize = DWT_PAC8,           // pacSize
65
                                .nsSFD = 0,                    // non-standard SFD
66
                                .sfdTO = (129 + 8 - 8)        // SFD timeout
67
                               }
68
};
69

    
70

    
71
/*! Slot and Superframe Configuration for DecaRangeRTLS TREK Modes (4 default use cases selected by the switch S1 [2,3] on EVB1000, indexed 0 to 3 )*/
72
sfConfig_t sfConfig[4] ={
73
                         //mode 1 - S1: 2 off, 3 off
74
                         {
75
                          .slotPeriod = (28), //slot duration in milliseconds (NOTE: the ranging exchange must be able to complete in this time
76
                          //e.g. tag sends a poll, 4 anchors send responses and tag sends the final + processing time
77
                          .numSlots = (10),        //number of slots in the superframe (8 tag slots and 2 used for anchor to anchor ranging),
78
                          .sfPeriod = (10*28),  //in ms => 280ms frame means 3.57 Hz location rate
79
                          .pollSleepDly = (10*28), //tag period in ms (sleep time + ranging time)
80
                          .replyDly = (25000) //poll to final delay in microseconds (needs to be adjusted according to lengths of ranging frames)
81
                         },
82
#if (DISCOVERY == 1)
83
                         //mode 2 - S1: 2 on, 3 off
84
                         {
85
                          .slotPeriod = (10), //slot duration in milliseconds (NOTE: the ranging exchange must be able to complete in this time
86
                          //e.g. tag sends a poll, 4 anchors send responses and tag sends the final + processing time
87
                          .numSlots = (100),        //number of slots in the superframe (98 tag slots and 2 used for anchor to anchor ranging),
88
                          .sfPeriod = (10*100),  //in ms => 1000 ms frame means 1 Hz location rate
89
                          .pollSleepDly = (10*100), //tag period in ms (sleep time + ranging time)
90
                          .replyDly = (2500) //poll to final delay in microseconds (needs to be adjusted according to lengths of ranging frames)
91

    
92
                         },
93
#else
94
                         //mode 2 - S1: 2 on, 3 off
95
                         {
96
                          .slotPeriod = (10), //slot duration in milliseconds (NOTE: the ranging exchange must be able to complete in this time
97
                          //e.g. tag sends a poll, 4 anchors send responses and tag sends the final + processing time
98
                          .numSlots = (10),        //number of slots in the superframe (8 tag slots and 2 used for anchor to anchor ranging),
99
                          .sfPeriod = (10*10),  //in ms => 100 ms frame means 10 Hz location rate
100
                          .pollSleepDly = (10*10), //tag period in ms (sleep time + ranging time)
101
                          .replyDly = (2500) //poll to final delay in microseconds (needs to be adjusted according to lengths of ranging frames)
102
                         },
103
#endif
104
                         //mode 3 - S1: 2 off, 3 on
105
                         {
106
                          .slotPeriod = (28), //slot duration in milliseconds (NOTE: the ranging exchange must be able to complete in this time
107
                          //e.g. tag sends a poll, 4 anchors send responses and tag sends the final + processing time
108
                          .numSlots = (10),        //number of slots in the superframe (8 tag slots and 2 used for anchor to anchor ranging),
109
                          .sfPeriod = (10*28),  //in ms => 280ms frame means 3.57 Hz location rate
110
                          .pollSleepDly = (10*28), //tag period in ms (sleep time + ranging time)
111
                          .replyDly = (20000) //poll to final delay in microseconds (needs to be adjusted according to lengths of ranging frames)
112

    
113
                         },
114
                         //mode 4 - S1: 2 on, 3 on
115
                         {
116
                          .slotPeriod = (10), //slot duration in milliseconds (NOTE: the ranging exchange must be able to complete in this time
117
                          //e.g. tag sends a poll, 4 anchors send responses and tag sends the final + processing time
118
                          .numSlots = (10),        //number of slots in the superframe (8 tag slots and 2 used for anchor to anchor ranging),
119
                          .sfPeriod = (10*10),  //in ms => 100 ms frame means 10 Hz location rate
120
                          .pollSleepDly = (10*10), //tag period in ms (sleep time + ranging time)
121
                          .replyDly = (2500) //poll to final delay in microseconds (needs to be adjusted according to lengths of ranging frames)
122
                         }
123
};
124

    
125

    
126
// -------------------------------------------------------------------------------------------------------------------
127

    
128
//The table below specifies the default TX spectrum configuration parameters... this has been tuned for DW EVK hardware units
129
//the table is set for smart power - see below in the instance_config function how this is used when not using smart power
130
const tx_struct txSpectrumConfig[8] =
131
{
132
    //Channel 0 ----- this is just a place holder so the next array element is channel 1
133
    {
134
            0x0,   //0
135
            {
136
                    0x0, //0
137
                    0x0 //0
138
            }
139
    },
140
    //Channel 1
141
    {
142
            0xc9,   //PG_DELAY
143
            {
144
                    0x15355575, //16M prf power
145
                    0x07274767 //64M prf power
146
            }
147

    
148
    },
149
    //Channel 2
150
    {
151
            0xc2,   //PG_DELAY
152
            {
153
                    0x15355575, //16M prf power
154
                    0x07274767 //64M prf power
155
            }
156
    },
157
    //Channel 3
158
    {
159
            0xc5,   //PG_DELAY
160
            {
161
                    0x0f2f4f6f, //16M prf power
162
                    0x2b4b6b8b //64M prf power
163
            }
164
    },
165
    //Channel 4
166
    {
167
            0x95,   //PG_DELAY
168
            {
169
                    0x1f1f3f5f, //16M prf power
170
                    0x3a5a7a9a //64M prf power
171
            }
172
    },
173
    //Channel 5
174
    {
175
            0xc0,   //PG_DELAY
176
            {
177
                    0x0E082848, //16M prf power
178
                    0x25456585 //64M prf power
179
            }
180
    },
181
    //Channel 6 ----- this is just a place holder so the next array element is channel 7
182
    {
183
            0x0,   //0
184
            {
185
                    0x0, //0
186
                    0x0 //0
187
            }
188
    },
189
    //Channel 7
190
    {
191
            0x93,   //PG_DELAY
192
            {
193
                    0x32527292, //16M prf power
194
                    0x5171B1d1 //64M prf power
195
            }
196
    }
197
};
198

    
199
//these are default antenna delays for EVB1000, these can be used if there is no calibration data in the DW1000,
200
//or instead of the calibration data
201
const uint16_t rfDelays[2] = {
202
        (uint16_t) (((double)DWT_PRF_16M_RFDLY/ 2.0) * 1e-9 / DWT_TIME_UNITS),//PRF 16
203
        (uint16_t) (((double)DWT_PRF_64M_RFDLY/ 2.0) * 1e-9 / DWT_TIME_UNITS)
204
};
205

    
206
//these are default TREK Tag/Anchor antenna delays
207
const uint16_t rfDelaysTREK[2] = {
208
        (uint16_t) (((double)514.83f/ 2.0) * 1e-9 / DWT_TIME_UNITS),//channel 2
209
        (uint16_t) (((double)514.65f/ 2.0) * 1e-9 / DWT_TIME_UNITS) //channel 5
210
};
211

    
212

    
213
// -------------------------------------------------------------------------------------------------------------------
214
//      Data Definitions
215
// -------------------------------------------------------------------------------------------------------------------
216

    
217
instance_data_t instance_data[NUM_INST] ;
218

    
219
static double inst_tdist[MAX_TAG_LIST_SIZE] ;
220
static double inst_idist[MAX_ANCHOR_LIST_SIZE] ;
221
static double inst_idistraw[MAX_ANCHOR_LIST_SIZE] ;
222

    
223
instance_data_t instance_data[NUM_INST] ;
224

    
225
typedef struct __attribute__((packed))
226
{
227
    uint32_t      deviceID ;
228
    uint8_t       chan;               // added chan here - used in the reading of acc
229
    uint16_t      rfrxDly;            // rf delay (delay though the RF blocks before the signal comes out of the antenna i.e. "antenna delay")
230
    uint16_t      rftxDly;            // rf delay (delay though the RF blocks before the signal comes out of the antenna i.e. "antenna delay")
231
    uint32_t      antennaDly;         // antenna delay read from OTP 64 PRF value is in high 16 bits and 16M PRF in low 16 bits
232
    uint32_t      antennaCals[4];     // antenna delays for the TREKs (Anchor high 16-bits, Tag low 16-bits)
233
    uint32_t      txPowCfg[12];       // stores the Tx power configuration read from OTP (6 channels consecutively with PRF16 then 64, e.g. Ch 1 PRF16 is index 0 and 64 index 1)
234
    uint32_t      states[3] ;         //MP workaround debug states register
235
    uint8_t       statescount ;
236
    int           prfIndex ;
237
    uint32_t      ldoTune ;           //low 32 bits of LDO tune value
238
} platform_local_data_t ;
239

    
240
static platform_local_data_t platformLocalData ; // Static local device data
241

    
242
// -------------------------------------------------------------------------------------------------------------------
243
// Functions
244
// -------------------------------------------------------------------------------------------------------------------
245

    
246
// -------------------------------------------------------------------------------------------------------------------
247
// convert microseconds to device time
248
uint64_t convertmicrosectodevicetimeu (double microsecu){
249
  uint64_t dt;
250
  long double dtime;
251

    
252
  dtime = (long double)((microsecu / (double) DWT_TIME_UNITS) / 1e6);
253

    
254
  dt =  (uint64_t) (dtime) ;
255

    
256
  return dt;
257
}
258

    
259
double convertdevicetimetosec(int32_t dt){
260
  double f = 0;
261

    
262
  f =  dt * DWT_TIME_UNITS ;  // seconds #define TIME_UNITS          (1.0/499.2e6/128.0) = 15.65e-12
263

    
264
  return f ;
265
}
266

    
267
#pragma GCC optimize ("O3")
268
int reportTOF(int idx, uint32_t tofx){
269
  double distance ;
270
  double distance_to_correct;
271
  double tof ;
272
  int64_t tofi ;
273

    
274
  // check for negative results and accept them making them proper negative integers
275
  tofi = (int32_t) tofx ; // make it signed
276
  if (tofi > 0x7FFFFFFF){  // close up TOF may be negative
277
    tofi -= 0x80000000 ;  //
278
  }
279

    
280
  // convert to seconds (as floating point)
281
  tof = convertdevicetimetosec((int32_t)tofi) ;          //this is divided by 4 to get single time of flight
282
  inst_idistraw[idx] = distance = tof * SPEED_OF_LIGHT;
283

    
284
#if (CORRECT_RANGE_BIAS == 1)
285
  //for the 6.81Mb data rate we assume gating gain of 6dB is used,
286
  //thus a different range bias needs to be applied
287
  //if(inst->configData.dataRate == DWT_BR_6M8)
288
  if(instance_data[0].configData.smartPowerEn){
289
    //1.31 for channel 2 and 1.51 for channel 5
290
    if(instance_data[0].configData.chan == 5){
291
      distance_to_correct = distance/1.51;
292
    }
293
    else{   //channel 2
294
      distance_to_correct = distance/1.31;
295
    }
296
  }
297
  else{
298
    distance_to_correct = distance;
299
  }
300

    
301
  distance = distance - dwt_getrangebias(instance_data[0].configData.chan, (float) distance_to_correct, instance_data[0].configData.prf);
302
#endif
303

    
304
  if ((distance < 0) || (distance > 20000.000))    // discard any results less than <0 cm or >20 km
305
    return 0;
306

    
307
  inst_idist[idx] = distance;
308

    
309
  instance_data[0].longTermRangeCount++ ;                          // for computing a long term average
310

    
311
  return 1;
312
}// end of reportTOF
313

    
314
void setTagDist(int tidx, int aidx){
315
  inst_tdist[tidx] = inst_idist[aidx];
316
}
317

    
318
double getTagDist(int idx){
319
  return inst_tdist[idx];
320
}
321

    
322
void clearDistTable(int idx){
323
  inst_idistraw[idx] = 0;
324
  inst_idist[idx] = 0;
325
}
326

    
327
void instancecleardisttableall(void){
328
  int i;
329

    
330
  for(i=0; i<MAX_ANCHOR_LIST_SIZE; i++)  {
331
    inst_idistraw[i] = 0xffff;
332
    inst_idist[i] = 0xffff;
333
  }
334
}
335