Statistics
| Branch: | Tag: | Revision:

amiro-lld / drivers / DW1000 / v1 / deca_instance.h @ 9466e34d

History | View | Annotate | Download (28.3 KB)

1
/*! ----------------------------------------------------------------------------
2
 *  @file    instance.h
3
 *  @brief   DecaWave header for application level instance
4
 *
5
 * @attention
6
 *
7
 * Copyright 2015 (c) DecaWave Ltd, Dublin, Ireland.
8
 *
9
 * All rights reserved.
10
 *
11
 * @author DecaWave
12
 */
13
#ifndef DECAINSTANCE_H
14
#define DECAINSTANCE_H
15

    
16
#ifdef __cplusplus
17
extern "C" {
18
#endif
19

    
20
#include <amiro-lld.h>
21
#include <alld_DW1000.h>
22

    
23
  /******************************************************************************************************************
24
   ********************* NOTES on DW (MP) features/options ***********************************************************
25
   *******************************************************************************************************************/
26
#define DEEP_SLEEP (0) //To enable deep-sleep set this to 1
27
  //DEEP_SLEEP mode can be used, for example, by a Tag instance to put the DW1000 into low-power deep-sleep mode:
28
  // when the Anchor is sending the range report back to the Tag, the Tag will enter sleep after a ranging exchange is finished
29
  // once it receives a report or times out, before the next poll message is sent (before next ranging exchange is started).
30

    
31
#define CORRECT_RANGE_BIAS  (1)     // Compensate for small bias due to uneven accumulator growth at close up high power
32

    
33

    
34
#define ANCTOANCTWR (1) //if set to 1 then anchor to anchor TWR will be done in the last slot
35

    
36
#define DISCOVERY (0)     //set to 1 to enable tag discovery - tags starts by sending blinks (with own ID) and then
37
                            //anchor assigns a slot to it and gives it short address
38
  /******************************************************************************************************************
39
   *******************************************************************************************************************
40
   *******************************************************************************************************************/
41

    
42
#define NUM_INST            1
43
#define SPEED_OF_LIGHT      (299702547.0)     // in m/s in air
44
#define MASK_40BIT                        (0x00FFFFFFFFFF)  // DW1000 counter is 40 bits
45
#define MASK_TXDTS                        (0x00FFFFFFFE00)  //The TX timestamp will snap to 8 ns resolution - mask lower 9 bits.
46

    
47
//!  callback events
48
#define DWT_SIG_RX_NOERR            0
49
#define DWT_SIG_TX_DONE             1       // Frame has been sent
50
#define DWT_SIG_RX_OKAY             2       // Frame Received with Good CRC
51
#define DWT_SIG_RX_ERROR            3       // Frame Received but CRC is wrong
52
#define DWT_SIG_RX_TIMEOUT          4       // Timeout on receive has elapsed
53
#define DWT_SIG_TX_AA_DONE          6       // ACK frame has been sent (as a result of auto-ACK)
54
#define DWT_SIG_RX_BLINK            7       // Received ISO EUI 64 blink message
55
#define DWT_SIG_RX_PHR_ERROR        8       // Error found in PHY Header
56
#define DWT_SIG_RX_SYNCLOSS         9       // Un-recoverable error in Reed Solomon Decoder
57
#define DWT_SIG_RX_SFDTIMEOUT       10      // Saw preamble but got no SFD within configured time
58
#define DWT_SIG_RX_PTOTIMEOUT       11      // Got preamble detection timeout (no preamble detected)
59

    
60
#define DWT_SIG_TX_PENDING          12      // TX is pending
61
#define DWT_SIG_TX_ERROR            13      // TX failed
62
#define DWT_SIG_RX_PENDING          14      // RX has been re-enabled
63
#define DWT_SIG_DW_IDLE             15      // DW radio is in IDLE (no TX or RX pending)
64

    
65
#define SIG_RX_UNKNOWN                        99                // Received an unknown frame
66

    
67
  //DecaRTLS frame function codes
68
#define RTLS_DEMO_MSG_TAG_POLL              (0x81)          // Tag poll message
69
#define RTLS_DEMO_MSG_ANCH_RESP             (0x70)          // Anchor response to poll
70
#define RTLS_DEMO_MSG_ANCH_POLL                                (0x71)                        // Anchor to anchor poll message
71
#define RTLS_DEMO_MSG_ANCH_RESP2            (0x72)          // Anchor response to poll from anchor
72
#define RTLS_DEMO_MSG_ANCH_FINAL            (0x73)          // Anchor final massage back to Anchor
73
#define RTLS_DEMO_MSG_TAG_FINAL             (0x82)          // Tag final massage back to Anchor
74

    
75

    
76
  //lengths including the Decaranging Message Function Code byte
77
#define TAG_POLL_MSG_LEN                    2                                // FunctionCode(1), Range Num (1)
78
#define ANCH_RESPONSE_MSG_LEN               8               // FunctionCode(1), Sleep Correction Time (2), Measured_TOF_Time(4), Range Num (1) (previous)
79
#define TAG_FINAL_MSG_LEN                   33              // FunctionCode(1), Range Num (1), Poll_TxTime(5),
80
  // Resp0_RxTime(5), Resp1_RxTime(5), Resp2_RxTime(5), Resp3_RxTime(5), Final_TxTime(5), Valid Response Mask (1)
81

    
82
#define MAX_MAC_MSG_DATA_LEN                (TAG_FINAL_MSG_LEN) //max message len of the above
83

    
84
#define STANDARD_FRAME_SIZE         127
85
  //#define STANDARD_FRAME_SIZE         80
86

    
87
#define ADDR_BYTE_SIZE_L            (8)
88
#define ADDR_BYTE_SIZE_S            (2)
89

    
90
#define FRAME_CONTROL_BYTES         2
91
#define FRAME_SEQ_NUM_BYTES         1
92
#define FRAME_PANID                 2
93
#define FRAME_CRC                                        2
94
#define FRAME_SOURCE_ADDRESS_S        (ADDR_BYTE_SIZE_S)
95
#define FRAME_DEST_ADDRESS_S          (ADDR_BYTE_SIZE_S)
96
#define FRAME_SOURCE_ADDRESS_L        (ADDR_BYTE_SIZE_L)
97
#define FRAME_DEST_ADDRESS_L          (ADDR_BYTE_SIZE_L)
98
#define FRAME_CTRLP                                        (FRAME_CONTROL_BYTES + FRAME_SEQ_NUM_BYTES + FRAME_PANID) //5
99
#define FRAME_CRTL_AND_ADDRESS_L    (FRAME_DEST_ADDRESS_L + FRAME_SOURCE_ADDRESS_L + FRAME_CTRLP) //21 bytes for 64-bit addresses)
100
#define FRAME_CRTL_AND_ADDRESS_S    (FRAME_DEST_ADDRESS_S + FRAME_SOURCE_ADDRESS_S + FRAME_CTRLP) //9 bytes for 16-bit addresses)
101
#define FRAME_CRTL_AND_ADDRESS_LS        (FRAME_DEST_ADDRESS_L + FRAME_SOURCE_ADDRESS_S + FRAME_CTRLP) //15 bytes for 1 16-bit address and 1 64-bit address)
102
#define MAX_USER_PAYLOAD_STRING_LL     (STANDARD_FRAME_SIZE-FRAME_CRTL_AND_ADDRESS_L-TAG_FINAL_MSG_LEN-FRAME_CRC) //127 - 21 - 16 - 2 = 88
103
#define MAX_USER_PAYLOAD_STRING_SS     (STANDARD_FRAME_SIZE-FRAME_CRTL_AND_ADDRESS_S-TAG_FINAL_MSG_LEN-FRAME_CRC) //127 - 9 - 16 - 2 = 100
104
#define MAX_USER_PAYLOAD_STRING_LS     (STANDARD_FRAME_SIZE-FRAME_CRTL_AND_ADDRESS_LS-TAG_FINAL_MSG_LEN-FRAME_CRC) //127 - 15 - 16 - 2 = 94
105

    
106
  //NOTE: the user payload assumes that there are only 88 "free" bytes to be used for the user message (it does not scale according to the addressing modes)
107
#define MAX_USER_PAYLOAD_STRING        MAX_USER_PAYLOAD_STRING_LL
108

    
109
#define MAX_TAG_LIST_SIZE                                (8)
110
#define MAX_ANCHOR_LIST_SIZE                        (4) //this is limited to 4 in this application
111
#define NUM_EXPECTED_RESPONSES                        (3) //e.g. MAX_ANCHOR_LIST_SIZE - 1
112
#define NUM_EXPECTED_RESPONSES_ANC                (1) //anchors A0, A1 and A2 are involved in anchor to anchor ranging
113
#define NUM_EXPECTED_RESPONSES_ANC0                (2) //anchor A0 expects response from A1 and A2
114

    
115
#define GATEWAY_ANCHOR_ADDR                                (0x8000)
116
#define A1_ANCHOR_ADDR                                        (0x8001)
117
#define A2_ANCHOR_ADDR                                        (0x8002)
118
#define A3_ANCHOR_ADDR                                        (0x8003)
119

    
120

    
121
  // !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
122
  // NOTE: the maximum RX timeout is ~ 65ms
123
  // !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
124

    
125
#define INST_DONE_WAIT_FOR_NEXT_EVENT           1   //this signifies that the current event has been processed and instance is ready for next one
126
#define INST_DONE_WAIT_FOR_NEXT_EVENT_TO    2   //this signifies that the current event has been processed and that instance is waiting for next one with a timeout
127
  //which will trigger if no event coming in specified time
128
#define INST_NOT_DONE_YET                       0   //this signifies that the instance is still processing the current event
129

    
130
  //application data message byte offsets
131
#define FCODE                               0               // Function code is 1st byte of messageData
132
#define PTXT                                2                                // Poll TX time
133
#define RRXT0                               7                                // A0 Response RX time
134
#define RRXT1                               12                                // A1 Response RX time
135
#define RRXT2                               17                                // A2 Response RX time
136
#define RRXT3                               22                                // A3 Response RX time
137
#define FTXT                                27                                // Final TX time
138
#define VRESP                               32                                // Mask of valid response times (e.g. if bit 1 = A0's response time is valid)
139
#define RES_TAG_SLP0                        1               // Response tag sleep correction LSB
140
#define RES_TAG_SLP1                        2               // Response tag sleep correction MSB
141
#define TOFR                                3                                // ToF (n-1) 4 bytes
142
#define TOFRN                                                                7                                // range number 1 byte
143
#define POLL_RNUM                           1               // Poll message range number
144

    
145
  //this it the delay used for configuring the receiver on delay (wait for response delay)
146
  //NOTE: this RX_RESPONSE_TURNAROUND is dependent on the microprocessor and code optimisations
147
#define RX_RESPONSEX_TURNAROUND (50) //takes about 100 us for response to come back
148
#define RX_RESPONSE1_TURNAROUND (200) //takes about 200 us for the 1st response to come back (from A0)
149
#define RX_RESPONSE1_TURNAROUND_6M81 (300) //takes about 100 us for response to come back
150
#define RX_RESPONSE1_TURNAROUND_110K (300) //takes about 100 us for response to come back
151

    
152
  //Tag will range to 3 or 4 anchors
153
  //Each ranging exchange will consist of minimum of 3 messages (Poll, Response, Final)
154
  //and a maximum of 6 messages (Poll, Response x 4, Final)
155
  //Thus the ranging exchange will take either 28 ms for 110 kbps and 5 ms for 6.81 Mbps.
156
  //NOTE: the above times are for 110k rate with 64 symb non-standard SFD and 1024 preamble length
157

    
158
  typedef enum instanceModes{LISTENER, TAG, ANCHOR, ANCHOR_RNG, NUM_MODES} INST_MODE;
159

    
160
  //Listener = in this mode, the instance only receives frames, does not respond
161
  //Tag = Exchanges DecaRanging messages (Poll-Response-Final) with Anchor and enabling Anchor to calculate the range between the two instances
162
  //Anchor = see above
163
  //Anchor_Rng = the anchor (assumes a tag function) and ranges to another anchor - used in Anchor to Anchor TWR for auto positioning function
164

    
165
#define TOF_REPORT_NUL 0
166
#define TOF_REPORT_T2A 1
167
#define TOF_REPORT_A2A 2
168

    
169
#define INVALID_TOF (0xABCDFFFF)
170

    
171
  typedef enum inst_states
172
  {
173
    TA_INIT, //0
174

    
175
    TA_TXE_WAIT,                //1 - state in which the instance will enter sleep (if ranging finished) or proceed to transmit a message
176
    TA_TXPOLL_WAIT_SEND,        //2 - configuration and sending of Poll message
177
    TA_TXFINAL_WAIT_SEND,       //3 - configuration and sending of Final message
178
    TA_TXRESPONSE_WAIT_SEND,    //4 - a place holder - response is sent from call back
179
    TA_TX_WAIT_CONF,            //6 - confirmation of TX done message
180

    
181
    TA_RXE_WAIT,                //7
182
    TA_RX_WAIT_DATA,            //8
183

    
184
    TA_SLEEP_DONE,               //9
185
    TA_TXRESPONSE_SENT_POLLRX,    //10
186
    TA_TXRESPONSE_SENT_RESPRX,    //11
187
    TA_TXRESPONSE_SENT_TORX                  //12
188

    
189
  } INST_STATES;
190

    
191

    
192
  // This file defines data and functions for access to Parameters in the Device
193
  //message structure for Poll, Response and Final message
194

    
195
  typedef struct
196
  {
197
    uint8_t frameCtrl[2];                                 //  frame control bytes 00-01
198
    uint8_t seqNum;                                       //  sequence_number 02
199
    uint8_t panID[2];                                     //  PAN ID 03-04
200
    uint8_t destAddr[ADDR_BYTE_SIZE_L];                     //  05-12 using 64 bit addresses
201
    uint8_t sourceAddr[ADDR_BYTE_SIZE_L];                   //  13-20 using 64 bit addresses
202
    uint8_t messageData[MAX_USER_PAYLOAD_STRING_LL] ; //  22-124 (application data and any user payload)
203
    uint8_t fcs[2] ;                                      //  125-126  we allow space for the CRC as it is logically part of the message. However ScenSor TX calculates and adds these bytes.
204
  } srd_msg_dlsl ;
205

    
206
  typedef struct
207
  {
208
    uint8_t frameCtrl[2];                                 //  frame control bytes 00-01
209
    uint8_t seqNum;                                       //  sequence_number 02
210
    uint8_t panID[2];                                     //  PAN ID 03-04
211
    uint8_t destAddr[ADDR_BYTE_SIZE_S];                     //  05-06
212
    uint8_t sourceAddr[ADDR_BYTE_SIZE_S];                   //  07-08
213
    uint8_t messageData[MAX_USER_PAYLOAD_STRING_SS] ; //  09-124 (application data and any user payload)
214
    uint8_t fcs[2] ;                                      //  125-126  we allow space for the CRC as it is logically part of the message. However ScenSor TX calculates and adds these bytes.
215
  } srd_msg_dsss ;
216

    
217
  typedef struct
218
  {
219
    uint8_t frameCtrl[2];                                 //  frame control bytes 00-01
220
    uint8_t seqNum;                                       //  sequence_number 02
221
    uint8_t panID[2];                                     //  PAN ID 03-04
222
    uint8_t destAddr[ADDR_BYTE_SIZE_L];                     //  05-12 using 64 bit addresses
223
    uint8_t sourceAddr[ADDR_BYTE_SIZE_S];                   //  13-14
224
    uint8_t messageData[MAX_USER_PAYLOAD_STRING_LS] ; //  15-124 (application data and any user payload)
225
    uint8_t fcs[2] ;                                      //  125-126  we allow space for the CRC as it is logically part of the message. However ScenSor TX calculates and adds these bytes.
226
  } srd_msg_dlss ;
227

    
228
  typedef struct
229
  {
230
    uint8_t frameCtrl[2];                                 //  frame control bytes 00-01
231
    uint8_t seqNum;                                       //  sequence_number 02
232
    uint8_t panID[2];                                     //  PAN ID 03-04
233
    uint8_t destAddr[ADDR_BYTE_SIZE_S];                     //  05-06
234
    uint8_t sourceAddr[ADDR_BYTE_SIZE_L];                   //  07-14 using 64 bit addresses
235
    uint8_t messageData[MAX_USER_PAYLOAD_STRING_LS] ; //  15-124 (application data and any user payload)
236
    uint8_t fcs[2] ;                                      //  125-126  we allow space for the CRC as it is logically part of the message. However ScenSor TX calculates and adds these bytes.
237
  } srd_msg_dssl ;
238

    
239
  typedef struct  __attribute__((packed))
240
  {
241
    uint8_t channelNumber ;       // valid range is 1 to 11
242
    uint8_t preambleCode ;        // 00 = use NS code, 1 to 24 selects code
243
    uint8_t pulseRepFreq ;        // NOMINAL_4M, NOMINAL_16M, or NOMINAL_64M
244
    uint8_t dataRate ;            // DATA_RATE_1 (110K), DATA_RATE_2 (850K), DATA_RATE_3 (6M81)
245
    uint8_t preambleLen ;         // values expected are 64, (128), (256), (512), 1024, (2048), and 4096
246
    uint8_t pacSize ;
247
    uint8_t nsSFD ;
248
    uint16_t sfdTO;  //!< SFD timeout value (in symbols) e.g. preamble length (128) + SFD(8) - PAC + some margin ~ 135us... DWT_SFDTOC_DEF; //default value
249
  } instanceConfig_t ;
250

    
251
  typedef struct
252
  {
253
    uint16_t slotPeriod ; //slot period (time for 1 tag to range to 4 anchors)
254
    uint16_t numSlots ; // number of slots in one superframe (number of tags supported)
255
    uint16_t sfPeriod ; // superframe period in ms
256

    
257
    uint16_t pollSleepDly ; // the minimum SLEEP time should be FRAME PERIOD so that tags don't interfere
258
    uint16_t replyDly ; //response delay time (Tag or Anchor when sending Final/Response messages respectively)
259
  } sfConfig_t ;
260

    
261
  /******************************************************************************************************************
262
   *******************************************************************************************************************
263
   *******************************************************************************************************************/
264

    
265
  //size of the event queue, in this application there should be at most 2 unprocessed events,
266
  //i.e. if there is a transmission with wait for response then the TX callback followed by RX callback could be executed
267
  //in turn and the event queued up before the instance processed the TX event.
268
#define MAX_EVENT_NUMBER (6)
269

    
270
  typedef struct  __attribute__((packed))
271
  {
272
    uint8_t  type;                        // event type - if 0 there is no event in the queue
273
    uint8_t  type_save;                // holds the event type - does not clear (used to show what event has been processed)
274
    uint8_t  type_pend;            // set if there is a pending event
275
    uint16_t rxLength ;                // length of RX data (does not apply to TX events)
276

    
277
    uint64_t timeStamp ;                // last timestamp (Tx or Rx) - 40 bit DW1000 time
278

    
279
    uint32_t timeStamp32l ;                   // last tx/rx timestamp - low 32 bits of the 40 bit DW1000 time
280
    uint32_t timeStamp32h ;                   // last tx/rx timestamp - high 32 bits of the 40 bit DW1000 time
281

    
282
    uint32_t uTimeStamp ;                          //32 bit system counter (ms) - STM32 tick time (at time of IRQ)
283

    
284
    union {
285
      //holds received frame (after a good RX frame event)
286
      uint8_t   frame[STANDARD_FRAME_SIZE];
287
      srd_msg_dlsl rxmsg_ll ; //64 bit addresses
288
      srd_msg_dssl rxmsg_sl ;
289
      srd_msg_dlss rxmsg_ls ;
290
      srd_msg_dsss rxmsg_ss ; //16 bit addresses
291
    }msgu;
292

    
293
    //uint32_t eventtime ;
294
    //uint32_t eventtimeclr ;
295
    uint8_t gotit;                        //stores the instance function which processed the event (used for debug)
296
  }event_data_t ;
297

    
298
extern event_data_t dw_event_g;
299

    
300
  // TX power and PG delay configuration structure
301
  typedef struct  __attribute__((packed)) {
302
    uint8_t PGdelay;
303

    
304
    //TX POWER
305
    //31:24     BOOST_0.125ms_PWR
306
    //23:16     BOOST_0.25ms_PWR-TX_SHR_PWR
307
    //15:8      BOOST_0.5ms_PWR-TX_PHR_PWR
308
    //7:0       DEFAULT_PWR-TX_DATA_PWR
309
    uint32_t txPwr[2]; //
310
  }tx_struct;
311

    
312
  typedef struct __attribute__((packed))
313
  {
314
    INST_MODE mode;                                //instance mode (tag or anchor)
315

    
316
    INST_STATES testAppState ;                        //state machine - current state
317
    INST_STATES nextState ;                                //state machine - next state
318
    INST_STATES previousState ;                        //state machine - previous state
319
    int done ;                                        //done with the current event/wait for next event to arrive
320

    
321
    //configuration structures
322
    dwt_config_t    configData ;        //DW1000 channel configuration
323
    dwt_txconfig_t  configTX ;                //DW1000 TX power configuration
324
    uint16_t                        txAntennaDelay ; //DW1000 TX antenna delay
325
    uint16_t                        rxAntennaDelay ; //DW1000 RX antenna delay
326
    uint32_t                         txPower ;                 //DW1000 TX power
327
    uint8_t txPowerChanged ;                        //power has been changed - update the register on next TWR exchange
328
    uint8_t antennaDelayChanged;                //antenna delay has been changed - update the register on next TWR exchange
329

    
330
    uint16_t instanceAddress16; //contains tag/anchor address
331

    
332
    //timeouts and delays
333
    int32_t tagSleepTime_ms; //in milliseconds - defines the nominal Tag sleep time period
334
    int32_t tagSleepRnd; //add an extra slot duration to sleep time to avoid collision before getting synced by anchor 0
335

    
336
    //this is the delay used for the delayed transmit
337
    uint64_t pollTx2FinalTxDelay ; //this is delay from Poll Tx time to Final Tx time in DW1000 units (40-bit)
338
    uint64_t pollTx2FinalTxDelayAnc ; //this is delay from Poll Tx time to Final Tx time in DW1000 units (40-bit) for Anchor to Anchor ranging
339
    uint64_t fixedReplyDelayAnc ;
340
    uint32_t fixedReplyDelayAncP ;
341
    int ancRespRxDelay ;
342

    
343
    int fwtoTime_sy ;        //this is final message duration (longest out of ranging messages)
344
    int fwtoTimeAnc_sy ;
345
    uint32_t delayedReplyTime;                // delayed reply time of ranging-init/response/final message
346

    
347
    uint32_t rxTimeouts ;
348

    
349
    //message structure used for holding the data of the frame to transmit before it is written to the DW1000
350
    srd_msg_dsss msg_f ; // ranging message frame with 16-bit addresses
351

    
352
    //Tag function address/message configuration
353
    uint8_t   shortAdd_idx ;                                // device's 16-bit address low byte (used as index into arrays [0 - 3])
354
    uint8_t   eui64[8];                                // device's EUI 64-bit address
355
    uint16_t  psduLength ;                        // used for storing the TX frame length
356
    uint8_t   frameSN;                                // modulo 256 frame sequence number - it is incremented for each new frame transmission
357
    uint16_t  panID ;                                        // panid used in the frames
358

    
359
    //64 bit timestamps
360
    //union of TX timestamps
361
    union {
362
      uint64_t txTimeStamp ;                   // last tx timestamp
363
      uint64_t tagPollTxTime ;                   // tag's poll tx timestamp
364
      uint64_t anchorRespTxTime ;           // anchor's reponse tx timestamp
365
    }txu;
366
    uint64_t tagPollRxTime ;          // receive time of poll message
367

    
368

    
369
    //application control parameters
370
    uint8_t        wait4ack ;                                // if this is set to DWT_RESPONSE_EXPECTED, then the receiver will turn on automatically after TX completion
371

    
372
    int8_t   responseTO ;
373
    uint8_t   instToSleep;                        // if set the instance will go to sleep before sending the blink/poll message
374
    uint8_t        stopTimer;                                // stop/disable an active timer
375
    uint8_t        instanceTimerEn;                // enable/start a timer
376
    uint32_t        instanceWakeTime;                        //
377
    uint32_t  nextSleepPeriod;
378

    
379
    uint8_t        gotTO;                                        // got timeout event
380

    
381
    uint8_t   rxResponseMaskAnc;
382
    uint8_t   rxResponseMask;                        // bit mask - bit 0 = received response from anchor ID = 0, bit 1 from anchor ID = 1 etc...
383
    uint8_t   rxResponseMaskReport;
384
    uint8_t        rangeNum;                                // incremented for each sequence of ranges (each slot)
385
    uint8_t        rangeNumA[MAX_TAG_LIST_SIZE];                                // array which holds last range number from each tag
386
    uint8_t        rangeNumAnc;                        // incremented for each sequence of ranges (each slot) - anchor to anchor ranging
387
    uint8_t        rangeNumAAnc[MAX_ANCHOR_LIST_SIZE]; //array which holds last range number for each anchor
388
    uint16_t  sframePeriod;
389
    uint16_t  slotPeriod;
390
    uint16_t  numSlots;
391
    uint32_t  a0SlotTime;
392
    uint32_t  a1SlotTime;
393
    int32_t   tagSleepCorrection;
394
    int32_t   tagSleepCorrection2;
395

    
396
    //diagnostic counters/data, results and logging
397
    uint32_t tof[MAX_TAG_LIST_SIZE]; //this is an array which holds last ToF from particular tag (ID 0-7)
398

    
399
    //this is an array which holds last ToF to each anchor it should
400
    uint32_t tofArray[MAX_ANCHOR_LIST_SIZE]; //contain 4 ToF to 4 anchors all relating to same range number sequence
401

    
402
    uint32_t tofAnc[MAX_ANCHOR_LIST_SIZE]; //this is an array which holds last ToFs from particular anchors (0, 0-1, 0-2, 1-2)
403

    
404
    //this is an array which holds last ToFs of the Anchor to Anchor ranging
405
    uint32_t tofArrayAnc[MAX_ANCHOR_LIST_SIZE]; //it contains 3 ToFs relating to same range number sequence (0, 0-1, 0-2, 1-2)
406

    
407
    int txMsgCount; //number of transmitted messages
408
    int        rxMsgCount; //number of received messages
409
    int lateTX; //number of "LATE" TX events
410
    int lateRX; //number of "LATE" RX events
411

    
412
    int longTermRangeCount ; //total number of ranges
413

    
414
    int newRange;                        //flag set when there is a new range to report TOF_REPORT_A2A or TOF_REPORT_T2A
415
    int newRangeAncAddress; //last 4 bytes of anchor address - used for printing/range output display
416
    int newRangeTagAddress; //last 4 bytes of tag address - used for printing/range output display
417
    uint32_t newRangeTime;
418

    
419
    uint8_t gatewayAnchor ; //set to TRUE = 1 if anchor address == GATEWAY_ANCHOR_ADDR
420

    
421
    //event queue - used to store DW1000 events as they are processed by the dw_isr/callback functions
422
    event_data_t dwevent[MAX_EVENT_NUMBER]; //this holds any TX/RX events and associated message data
423
    event_data_t saved_dwevent; //holds an RX event while the ACK is being sent
424
    uint8_t dweventIdxOut;
425
    uint8_t dweventIdxIn;
426
    uint8_t dweventPeek;
427
    uint8_t monitor;
428
    uint32_t timeofTx ;
429

    
430
    uint8_t rxRespsIdx; //index into the array below (current tag (4bits)/seq number(4bits))
431
    int8_t rxResps[256];
432

    
433
    int dwIDLE; //set to 1 when the RST goes high after wake up (it is set in process_dwRSTn_irq)
434

    
435
  } instance_data_t ;
436

    
437
  //-------------------------------------------------------------------------------------------------------------
438
  //
439
  //        Functions used in logging/displaying range and status data
440
  //
441
  //-------------------------------------------------------------------------------------------------------------
442

    
443
  // function to calculate and report the Time of Flight to the GUI/display
444
  int reportTOF(int idx, uint32_t tofx);
445
  void clearDistTable(int idx);
446
  void setTagDist(int tidx, int aidx);
447
  double getTagDist(int idx);
448

    
449
  // clear the status/ranging data
450
  void instanceclearcounts(void) ;
451

    
452
  //-------------------------------------------------------------------------------------------------------------
453
  //
454
  //        Functions used in driving/controlling the ranging application
455
  //
456
  //-------------------------------------------------------------------------------------------------------------
457

    
458
  void instance_close(void);
459
  // Call init, then call config, then call run. call close when finished
460
  // initialise the instance (application) structures and DW1000 device
461
  int instance_init(DW1000Driver* drv);
462

    
463
  // configure the instance and DW1000 device
464
  void instance_config(instanceConfig_t *config, sfConfig_t *sfconfig, DW1000Driver* drv) ;
465

    
466
  // configure the MAC address
467
  void instancesetaddresses(uint16_t address) ;
468

    
469
  void inst_processrxtimeout(instance_data_t *inst);
470

    
471
  // called (periodically or from and interrupt) to process any outstanding TX/RX events and to drive the ranging application
472
  int instance_run(void) ;       // returns indication of status report change
473
  int testapprun(instance_data_t *inst, int message);
474

    
475
  // calls the DW1000 interrupt handler
476
#define instance_process_irq(x)         dwt_isr()  //call device interrupt handler
477
  // configure TX/RX callback functions that are called from DW1000 ISR
478
  void instance_rxcallback(const dwt_cb_data_t *rxd);
479
  void instance_txcallback(const dwt_cb_data_t *txd);
480
  void instance_rxtimeoutcallback(const dwt_cb_data_t *rxd);
481
  void instance_rxerrorcallback(const dwt_cb_data_t *rxd);
482

    
483

    
484

    
485
  // sets the Tag sleep delay time (the time Tag "sleeps" between each ranging attempt)
486
  void instancesettagsleepdelay(int rangingsleep);
487
  void instancesetreplydelay(int delayms);
488

    
489
  // set/get the instance roles e.g. Tag/Anchor/Listener
490
  void instancesetrole(int mode) ;                //
491
  int instancegetrole(void) ;
492
  // get the DW1000 device ID (e.g. 0xDECA0130 for DW1000)
493
  uint32_t instancereaddeviceid(void) ;                                 // Return Device ID reg, enables validation of physical device presence
494

    
495
  double instancegetidist(int idx);
496
  double instancegetidistraw(int idx);
497
  int instancegetidist_mm(int idx);
498
  int instancegetidistraw_mm(int idx);
499
  uint8_t instancevalidranges(void);
500
  void instancecleardisttableall(void);
501

    
502
  void instance_backtoanchor(instance_data_t *inst);
503
  int instancesenddlypacket(instance_data_t *inst, int delayedTx);
504

    
505
  int instancegetrnum(void);
506
  int instancegetrnuma(int idx);
507
  int instancegetrnumanc(int idx);
508
  int instancegetlcount(void);
509

    
510
  int instancenewrangeancadd(void);
511
  int instancenewrangetagadd(void);
512
  int instancenewrangepolltim(void);
513
  int instancenewrange(void);
514
  uint32_t instancenewrangetim(void);
515

    
516
  uint64_t convertmicrosectodevicetimeu (double microsecu);
517
  double convertdevicetimetosec(int32_t dt);
518

    
519

    
520
  // -------------------------------------------------------------------------------------------------------------------
521
  // Deca Calibration Values
522
  // -------------------------------------------------------------------------------------------------------------------
523

    
524
  #define DWT_PRF_64M_RFDLY   (514.462f)
525
  #define DWT_PRF_16M_RFDLY   (513.9067f)
526

    
527
  /* Extern declaration while defining them in .c file */
528
  extern const uint16_t rfDelays[2];
529
  extern const uint16_t rfDelaysTREK[2];
530
  extern const tx_struct txSpectrumConfig[8];
531
  extern instance_data_t instance_data[NUM_INST] ;
532

    
533
  extern sfConfig_t sfConfig[4];
534
  extern instanceConfig_t chConfig[4];
535

    
536

    
537
  int instance_peekevent(void);
538

    
539
  void instance_saveevent(event_data_t newevent, uint8_t etype);
540

    
541
  event_data_t instance_getsavedevent(void);
542

    
543
  void instance_putevent(event_data_t newevent, uint8_t etype);
544

    
545
  event_data_t* instance_getevent(int x);
546

    
547
  void instance_clearevents(void);
548

    
549
  void instance_notify_DW1000_inIDLE(int idle);
550

    
551
  double dwt_getrangebias(uint8_t chan, float range, uint8_t prf);
552

    
553
  uint32_t dwt_getotptxpower(uint8_t prf, uint8_t chan);
554

    
555
  uint16_t dwt_getTREKOTPantennadelay(uint8_t anchor, uint8_t chan, uint8_t datarate);
556

    
557
  uint16_t dwt_readantennadelay(uint8_t prf);
558

    
559
  // configure TX power
560
  void instanceconfigtxpower(uint32_t txpower);
561
  void instancesettxpower(void);
562

    
563
  // configure the antenna delays
564
  void instanceconfigantennadelays(uint16_t tx, uint16_t rx);
565
  void instancesetantennadelays(void);
566
  uint16_t instancetxantdly(void);
567
  uint16_t instancerxantdly(void);
568

    
569
  int instance_starttxtest(int framePeriod);
570

    
571
#ifdef __cplusplus
572
}
573
#endif
574

    
575
#endif   /* DECAINSTANCE_H */
576