Statistics
| Branch: | Tag: | Revision:

amiro-lld / include / DW1000 / v0 / deca_instance.h @ 5d4d14a3

History | View | Annotate | Download (33.587 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
#include <amiro-lld.h>
17

    
18
#if defined(AMIROLLD_CFG_USE_DW1000) || defined(__DOXYGEN__)
19

    
20
#include <alld_dw1000.h>
21

    
22
#ifdef __cplusplus
23
extern "C" {
24
#endif
25

    
26
/******************************************************************************************************************
27
********************* NOTES on TREK compile/build time features/options ***********************************************************
28
*******************************************************************************************************************/
29
#define DEEP_SLEEP (1) //To enable deep-sleep set this to 1
30
//DEEP_SLEEP mode can be used, for example, by a Tag instance to put the DW1000 into low-power deep-sleep mode while it is
31
//waiting for start of next ranging exchange
32

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

    
35
#define ANCTOANCTWR (1) //if set to 1 then anchor to anchor two-way ranging will be done in the last 2 slots, this
36
//is used for TREK demo, to aid in anchor installation,
37

    
38
#define TAG_HASTO_RANGETO_A0 (0) //if set to 1 then tag will only send the Final if the Response from A0 has been received
39

    
40
#define READ_EVENT_COUNTERS (0) //read event counters - can be used for debug to periodically output event counters
41

    
42
#define DISCOVERY (0)                //set to 1 to enable tag discovery - tags starts by sending blinks (with own ID) and then
43
                            //anchor assigns a slot to it and gives it short address
44

    
45
/******************************************************************************************************************
46
*******************************************************************************************************************
47
*******************************************************************************************************************/
48

    
49
#define NUM_INST            1                                  // one instance (tag or anchor - controlling one DW1000)
50
#define SPEED_OF_LIGHT      (299702547.0)     // in m/s in air
51
#define MASK_40BIT                        (0x00FFFFFFFFFF)  // DW1000 counter is 40 bits
52
#define MASK_TXDTS                        (0x00FFFFFFFE00)  // The TX timestamp will snap to 8 ns resolution - mask lower 9 bits.
53

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

    
67
#define DWT_SIG_TX_PENDING          12      // TX is pending
68
#define DWT_SIG_TX_ERROR            13      // TX failed
69
#define DWT_SIG_RX_PENDING          14      // RX has been re-enabled
70
#define DWT_SIG_DW_IDLE             15      // DW radio is in IDLE (no TX or RX pending)
71

    
72
#define SIG_RX_UNKNOWN                            99                // Received an unknown frame
73

    
74
//DecaRTLS frame function codes
75
#define RTLS_DEMO_MSG_RNG_INIT              (0x71)          // Ranging initiation message
76
#define RTLS_DEMO_MSG_TAG_POLL              (0x81)          // Tag poll message
77
#define RTLS_DEMO_MSG_ANCH_RESP             (0x70)          // Anchor response to poll
78
#define RTLS_DEMO_MSG_ANCH_POLL                                (0x7A)                        // Anchor to anchor poll message
79
#define RTLS_DEMO_MSG_ANCH_RESP2            (0x7B)          // Anchor response to poll from anchor
80
#define RTLS_DEMO_MSG_ANCH_FINAL            (0x7C)          // Anchor final massage back to Anchor
81
#define RTLS_DEMO_MSG_TAG_FINAL             (0x82)          // Tag final massage back to Anchor
82

    
83

    
84
//lengths including the Decaranging Message Function Code byte
85
//absolute length = 17 +
86
#define RANGINGINIT_MSG_LEN                                        5                                // FunctionCode(1), Sleep Correction Time (2), Tag Address (2)
87

    
88
//absolute length = 11 +
89
#define TAG_POLL_MSG_LEN                    2                                // FunctionCode(1), Range Num (1)
90
#define ANCH_RESPONSE_MSG_LEN               8               // FunctionCode(1), Sleep Correction Time (2), Measured_TOF_Time(4), Range Num (1) (previous)
91
#define TAG_FINAL_MSG_LEN                   33              // FunctionCode(1), Range Num (1), Poll_TxTime(5),
92
                                                                                                                        // Resp0_RxTime(5), Resp1_RxTime(5), Resp2_RxTime(5), Resp3_RxTime(5), Final_TxTime(5), Valid Response Mask (1)
93
#define ANCH_POLL_MSG_LEN_S                                        2                                // FunctionCode(1), Range Num (1),
94
#define ANCH_POLL_MSG_LEN                   4                                // FunctionCode(1), Range Num (1), Next Anchor (2)
95
#define ANCH_FINAL_MSG_LEN                  33              // FunctionCode(1), Range Num (1), Poll_TxTime(5),
96
                                                                                                                        // Resp0_RxTime(5), Resp1_RxTime(5), Resp2_RxTime(5), Resp3_RxTime(5), Final_TxTime(5), Valid Response Mask (1)
97
#define MAX_MAC_MSG_DATA_LEN                (TAG_FINAL_MSG_LEN) //max message len of the above
98

    
99
#define STANDARD_FRAME_SIZE         127
100

    
101
#define ADDR_BYTE_SIZE_L            (8)
102
#define ADDR_BYTE_SIZE_S            (2)
103

    
104
#define FRAME_CONTROL_BYTES         2
105
#define FRAME_SEQ_NUM_BYTES         1
106
#define FRAME_PANID                 2
107
#define FRAME_CRC                                        2
108
#define FRAME_SOURCE_ADDRESS_S        (ADDR_BYTE_SIZE_S)
109
#define FRAME_DEST_ADDRESS_S          (ADDR_BYTE_SIZE_S)
110
#define FRAME_SOURCE_ADDRESS_L        (ADDR_BYTE_SIZE_L)
111
#define FRAME_DEST_ADDRESS_L          (ADDR_BYTE_SIZE_L)
112
#define FRAME_CTRLP                                        (FRAME_CONTROL_BYTES + FRAME_SEQ_NUM_BYTES + FRAME_PANID) //5
113
#define FRAME_CRTL_AND_ADDRESS_L    (FRAME_DEST_ADDRESS_L + FRAME_SOURCE_ADDRESS_L + FRAME_CTRLP) //21 bytes for 64-bit addresses)
114
#define FRAME_CRTL_AND_ADDRESS_S    (FRAME_DEST_ADDRESS_S + FRAME_SOURCE_ADDRESS_S + FRAME_CTRLP) //9 bytes for 16-bit addresses)
115
#define FRAME_CRTL_AND_ADDRESS_LS        (FRAME_DEST_ADDRESS_L + FRAME_SOURCE_ADDRESS_S + FRAME_CTRLP) //15 bytes for one 16-bit address and one 64-bit address)
116
#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
117
#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
118
#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
119

    
120
//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)
121
#define MAX_USER_PAYLOAD_STRING        MAX_USER_PAYLOAD_STRING_LL
122

    
123
#define BLINK_FRAME_CONTROL_BYTES       (1)
124
#define BLINK_FRAME_SEQ_NUM_BYTES       (1)
125
#define BLINK_FRAME_CRC                                        (FRAME_CRC)
126
#define BLINK_FRAME_SOURCE_ADDRESS      (ADDR_BYTE_SIZE_L)
127
#define BLINK_FRAME_CTRLP                                (BLINK_FRAME_CONTROL_BYTES + BLINK_FRAME_SEQ_NUM_BYTES) //2
128
#define BLINK_FRAME_CRTL_AND_ADDRESS    (BLINK_FRAME_SOURCE_ADDRESS + BLINK_FRAME_CTRLP) //10 bytes
129
#define BLINK_FRAME_LEN_BYTES           (BLINK_FRAME_CRTL_AND_ADDRESS + BLINK_FRAME_CRC)
130

    
131

    
132
#if (DISCOVERY == 0)
133
#define MAX_TAG_LIST_SIZE                                (8)
134
#else
135
#define MAX_TAG_LIST_SIZE                                (100)
136
#endif
137

    
138

    
139
#define MAX_ANCHOR_LIST_SIZE                        (4) //this is limited to 4 in this application
140
#define NUM_EXPECTED_RESPONSES                        (3) //e.g. MAX_ANCHOR_LIST_SIZE - 1
141

    
142
#define NUM_EXPECTED_RESPONSES_ANC0                (2) //anchor A0 expects response from A1 and A2
143
#define NUM_EXPECTED_RESPONSES_ANC1                (1) //anchor A1 expects response from A2
144

    
145
#define GATEWAY_ANCHOR_ADDR                                (0x8000)
146
#define A1_ANCHOR_ADDR                                        (0x8001)
147
#define A2_ANCHOR_ADDR                                        (0x8002)
148
#define A3_ANCHOR_ADDR                                        (0x8003)
149

    
150
#define WAIT4TAGFINAL                                        2
151
#define WAIT4ANCFINAL                                        1
152

    
153
// !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
154
// NOTE: the maximum RX timeout is ~ 65ms
155
// !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
156

    
157
#define INST_DONE_WAIT_FOR_NEXT_EVENT           1   //this signifies that the current event has been processed and instance is ready for next one
158
#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
159
                                                        //which will trigger if no event coming in specified time
160
#define INST_NOT_DONE_YET                       0   //this signifies that the instance is still processing the current event
161

    
162
//application data message byte offsets
163
#define FCODE                               0               // Function code is 1st byte of messageData
164
#define PTXT                                2                                // Poll TX time
165
#define RRXT0                               7                                // A0 Response RX time
166
#define RRXT1                               12                                // A1 Response RX time
167
#define RRXT2                               17                                // A2 Response RX time
168
#define RRXT3                               22                                // A3 Response RX time
169
#define FTXT                                27                                // Final TX time
170
#define VRESP                               32                                // Mask of valid response times (e.g. if bit 1 = A0's response time is valid)
171
#define RES_TAG_SLP0                        1               // Response tag sleep correction LSB
172
#define RES_TAG_SLP1                        2               // Response tag sleep correction MSB
173
#define TOFR                                3                                // ToF (n-1) 4 bytes
174
#define TOFRN                                                                7                                // range number 1 byte
175
#define POLL_RNUM                           1               // Poll message range number
176
#define POLL_NANC                                                        2                                // Address of next anchor to send a poll message (e.g. A1)
177
#define RES_TAG_ADD0                        3               // Tag's short address (slot num) LSB
178
#define RES_TAG_ADD1                        4               // Tag's short address (slot num) MSB
179

    
180

    
181
#define BLINK_PERIOD                (2000) //ms (Blink at 2Hz initially)
182

    
183
#define DW_RX_ON_DELAY      (16) //us - the DW RX has 16 us RX on delay before it will receive any data
184

    
185
//this it the delay used for configuring the receiver on delay (wait for response delay)
186
//NOTE: this RX_RESPONSE_TURNAROUND is dependent on the microprocessor and code optimisations
187
#define RX_RESPONSE_TURNAROUND (300) //this takes into account any turnaround/processing time (reporting received poll and sending the response)
188

    
189
#define PTO_PACS                                (3)         //tag will use PTO to reduce power consumption (if no response coming stop RX)
190

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

    
197

    
198

    
199
//Anchor to Anchor Ranging
200
//1. A0 sends a Poll
201
//2. A1 responds (to A0's Poll) after RX_RESPONSE_TURNAROUND (300 us) + response frame length = fixedReplyDelayAnc1 ~= 480 us
202
//3. A0 turns its receiver on (after 300 us) expecting A1's response (this is done automatically in 1. by using WAIT4RESP)
203
//4. A2 responds (to A0's Poll) after 2*fixedReplyDelayAnc1 ~= 960 us.
204
//5. A0 turns its receiver on (last Rx on time + fixedReplyDelayAnc1) expecting A2's response (this is done after reception of A1's response using delayed RX)
205
//6. A0 sends a Final this is timed from the Poll TX time = pollTx2FinalTxDelayAnc ~= 1550 us (1250+300)
206
//7. A1 and A2 turn on their receivers to expect this Final frame (this is done automatically in 2. AND 4. by using WAIT4RESP)
207

    
208

    
209
//Tag to Anchor Ranging
210
//1. Tag sends a Poll
211
//2. A0 responds (delayed response) after fixedReplyDelayAnc1, A0 will re-enable its receiver automatically (by using WAIT4RESP)
212
//3. A1, A2, A3 re-enble the receiver to receive A0's response
213
//4. A1 responds and will re-enable its receiver automatically (by using WAIT4RESP)
214
//5. A2, A3 re-enble the receiver to receive A1's response
215
//6. A2 responds and will re-enable its receiver automatically (by using WAIT4RESP)
216
//7. A0, A3 re-enable the receiver to receive A2's response
217
//9. A3 responds and will re-enable its receiver automatically (by using WAIT4RESP)
218
//10. A0, A1, A2, A3 - all receive the Final from the tag
219

    
220

    
221
//Tag Discovery mode
222
//1. Tag sends a Blink
223
//2. A0 responds with Ranging Init giving it short address and slot time correction
224
//3. Tag sleeps until the next period and then starts ranging exchange
225

    
226

    
227
typedef enum instanceModes{TAG, ANCHOR, ANCHOR_RNG, NUM_MODES} INST_MODE;
228
//Tag = Exchanges DecaRanging messages (Poll-Response-Final) with Anchor and enabling Anchor to calculate the range between the two instances
229
//Anchor = see above
230
//Anchor_Rng = the anchor (assumes a tag function) and ranges to another anchor - used in Anchor to Anchor TWR for auto positioning function
231

    
232

    
233
// instance sending a poll (starting TWR) is INITIATOR
234
// instance which receives a poll (and will be involved in the TWR) is RESPONDER
235
// instance which does not receive a poll (default state) will be a LISTENER - will send no responses
236
typedef enum instanceTWRModes{INITIATOR, RESPONDER_A, RESPONDER_B, RESPONDER_T, LISTENER, GREETER, ATWR_MODES} ATWR_MODE;
237

    
238

    
239
#define TOF_REPORT_NUL 0
240
#define TOF_REPORT_T2A 1
241
#define TOF_REPORT_A2A 2
242

    
243
#define INVALID_TOF (0xABCDFFFF)
244

    
245
typedef enum inst_states
246
{
247
    TA_INIT, //0
248

    
249
    TA_TXE_WAIT,                //1 - state in which the instance will enter sleep (if ranging finished) or proceed to transmit a message
250
    TA_TXBLINK_WAIT_SEND,       //2 - configuration and sending of Blink message
251
    TA_TXPOLL_WAIT_SEND,        //2 - configuration and sending of Poll message
252
    TA_TXFINAL_WAIT_SEND,       //3 - configuration and sending of Final message
253
    TA_TXRESPONSE_WAIT_SEND,    //4 - a place holder - response is sent from call back
254
    TA_TX_WAIT_CONF,            //5 - confirmation of TX done message
255

    
256
    TA_RXE_WAIT,                //6
257
    TA_RX_WAIT_DATA,            //7
258

    
259
    TA_SLEEP_DONE,               //8
260
    TA_TXRESPONSE_SENT_POLLRX,   //9
261
    TA_TXRESPONSE_SENT_RESPRX,   //10
262
    TA_TXRESPONSE_SENT_TORX,     //11
263
    TA_TXRESPONSE_SENT_APOLLRX,  //12
264
    TA_TXRESPONSE_SENT_ARESPRX   //13
265

    
266
} INST_STATES;
267

    
268

    
269
// This file defines data and functions for access to Parameters in the Device
270
//message structure for Poll, Response and Final message
271

    
272
typedef struct
273
{
274
    uint8_t frameCtrl[2];                                 //  frame control bytes 00-01
275
    uint8_t seqNum;                                       //  sequence_number 02
276
    uint8_t panID[2];                                     //  PAN ID 03-04
277
    uint8_t destAddr[ADDR_BYTE_SIZE_L];                     //  05-12 using 64 bit addresses
278
    uint8_t sourceAddr[ADDR_BYTE_SIZE_L];                   //  13-20 using 64 bit addresses
279
    uint8_t messageData[MAX_USER_PAYLOAD_STRING_LL] ; //  22-124 (application data and any user payload)
280
    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.
281
} srd_msg_dlsl ;
282

    
283
typedef struct
284
{
285
    uint8_t frameCtrl[2];                                 //  frame control bytes 00-01
286
    uint8_t seqNum;                                       //  sequence_number 02
287
    uint8_t panID[2];                                     //  PAN ID 03-04
288
    uint8_t destAddr[ADDR_BYTE_SIZE_S];                     //  05-06
289
    uint8_t sourceAddr[ADDR_BYTE_SIZE_S];                   //  07-08
290
    uint8_t messageData[MAX_USER_PAYLOAD_STRING_SS] ; //  09-124 (application data and any user payload)
291
    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.
292
} srd_msg_dsss ;
293

    
294
typedef struct
295
{
296
    uint8_t frameCtrl[2];                                 //  frame control bytes 00-01
297
    uint8_t seqNum;                                       //  sequence_number 02
298
    uint8_t panID[2];                                     //  PAN ID 03-04
299
    uint8_t destAddr[ADDR_BYTE_SIZE_L];                     //  05-12 using 64 bit addresses
300
    uint8_t sourceAddr[ADDR_BYTE_SIZE_S];                   //  13-14
301
    uint8_t messageData[MAX_USER_PAYLOAD_STRING_LS] ; //  15-124 (application data and any user payload)
302
    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.
303
} srd_msg_dlss ;
304

    
305
typedef struct
306
{
307
    uint8_t frameCtrl[2];                                 //  frame control bytes 00-01
308
    uint8_t seqNum;                                       //  sequence_number 02
309
    uint8_t panID[2];                                     //  PAN ID 03-04
310
    uint8_t destAddr[ADDR_BYTE_SIZE_S];                     //  05-06
311
    uint8_t sourceAddr[ADDR_BYTE_SIZE_L];                   //  07-14 using 64 bit addresses
312
    uint8_t messageData[MAX_USER_PAYLOAD_STRING_LS] ; //  15-124 (application data and any user payload)
313
    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.
314
} srd_msg_dssl ;
315

    
316
//12 octets for Minimum IEEE ID blink
317
typedef struct
318
{
319
    uint8_t frameCtrl;                                         //  frame control bytes 00
320
    uint8_t seqNum;                                       //  sequence_number 01
321
    uint8_t tagID[BLINK_FRAME_SOURCE_ADDRESS];        //  02-09 64 bit address
322
    uint8_t fcs[2] ;                                      //  10-11  we allow space for the CRC as it is logically part of the message. However ScenSor TX calculates and adds these bytes.
323
} iso_IEEE_EUI64_blink_msg ;
324

    
325

    
326
typedef struct
327
{
328
    uint8_t channelNumber ;       // valid range is 1 to 11
329
    uint8_t preambleCode ;        // 00 = use NS code, 1 to 24 selects code
330
    uint8_t pulseRepFreq ;        // NOMINAL_4M, NOMINAL_16M, or NOMINAL_64M
331
    uint8_t dataRate ;            // DATA_RATE_1 (110K), DATA_RATE_2 (850K), DATA_RATE_3 (6M81)
332
    uint8_t preambleLen ;         // values expected are 64, (128), (256), (512), 1024, (2048), and 4096
333
    uint8_t pacSize ;
334
    uint8_t nsSFD ;
335
    uint16_t sfdTO;  //!< SFD timeout value (in symbols) e.g. preamble length (128) + SFD(8) - PAC + some margin ~ 135us... DWT_SFDTOC_DEF; //default value
336
} instanceConfig_t ;
337

    
338
typedef struct
339
{
340
    uint16_t slotDuration_ms ; //slot duration (time for 1 tag to range to 4 anchors)
341
    uint16_t numSlots ;                  // number of slots in one superframe (number of tags supported)
342
    uint16_t sfPeriod_ms ;         // superframe period in ms
343
    uint16_t tagPeriod_ms ;         // the time during which tag ranges to anchors and then sleeps, should be same as FRAME PERIOD so that tags don't interfere
344
    uint16_t pollTxToFinalTxDly_us ; //response delay time (Poll to Final delay)
345
} sfConfig_t ;
346

    
347
/******************************************************************************************************************
348
*******************************************************************************************************************
349
*******************************************************************************************************************/
350

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

    
356
typedef struct
357
{
358
    uint8_t  type;                        // event type - if 0 there is no event in the queue
359
    //uint8_t  typeSave;                // holds the event type - does not clear (used to show what event has been processed)
360
    uint8_t  typePend;            // set if there is a pending event (i.e. DW is not in IDLE (TX/RX pending)
361
    uint16_t rxLength ;                // length of RX data (does not apply to TX events)
362

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

    
365
    uint32_t timeStamp32l ;                   // last tx/rx timestamp - low 32 bits of the 40 bit DW1000 time
366
    uint32_t timeStamp32h ;                   // last tx/rx timestamp - high 32 bits of the 40 bit DW1000 time
367

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

    
370
        union {
371
                        //holds received frame (after a good RX frame event)
372
            uint8_t   frame[STANDARD_FRAME_SIZE];
373
                    srd_msg_dlsl rxmsg_ll ; //64 bit addresses
374
                        srd_msg_dssl rxmsg_sl ;
375
                        srd_msg_dlss rxmsg_ls ;
376
                        srd_msg_dsss rxmsg_ss ; //16 bit addresses
377
                        iso_IEEE_EUI64_blink_msg rxblinkmsg;
378
        }msgu;
379

    
380
    //uint8_t gotit;                        //stores the instance function which processed the event (used for debug)
381
}event_data_t ;
382

    
383
// TX power and PG delay configuration structure
384
typedef struct {
385
                uint8_t pgDelay;
386

    
387
                //TX POWER
388
                //31:24     BOOST_0.125ms_PWR
389
                //23:16     BOOST_0.25ms_PWR-TX_SHR_PWR
390
                //15:8      BOOST_0.5ms_PWR-TX_PHR_PWR
391
                //7:0       DEFAULT_PWR-TX_DATA_PWR
392
                uint32_t txPwr[2]; //
393
}tx_struct;
394

    
395
typedef struct
396
{
397
    INST_MODE mode;                                //instance mode (tag or anchor)
398
    ATWR_MODE twrMode;
399
    INST_STATES testAppState ;                        //state machine - current state
400
    INST_STATES nextState ;                                //state machine - next state
401
    INST_STATES previousState ;                        //state machine - previous state
402

    
403
        //configuration structures
404
        dwt_config_t    configData ;        //DW1000 channel configuration
405
    dwt_txconfig_t  configTX ;                 //DW1000 TX power configuration
406
    uint16_t                txAntennaDelay ; //DW1000 TX antenna delay
407
    uint16_t                rxAntennaDelay ; //DW1000 RX antenna delay
408
    uint32_t                txPower ;                 //DW1000 TX power
409
    uint8_t         txPowerChanged;  //power has been changed - update the register on next TWR exchange
410
    uint8_t         antennaDelayChanged;                //antenna delay has been changed - update the register on next TWR exchange
411

    
412
    uint16_t instanceAddress16; //contains tag/anchor 16 bit address
413

    
414
        //timeouts and delays
415
    int32_t tagPeriod_ms; // in ms, tag ranging + sleeping period
416
    int32_t tagSleepTime_ms; //in milliseconds - defines the nominal Tag sleep time period
417
    int32_t tagSleepRnd_ms; //add an extra slot duration to sleep time to avoid collision before getting synced by anchor 0
418

    
419
        //this is the delay used for the delayed transmit
420
    uint64_t pollTx2FinalTxDelay ; //this is delay from Poll Tx time to Final Tx time in DW1000 units (40-bit)
421
    uint64_t pollTx2FinalTxDelayAnc ; //this is delay from Poll Tx time to Final Tx time in DW1000 units (40-bit) for Anchor to Anchor ranging
422
    uint32_t fixedReplyDelayAnc32h ; //this is a delay used for calculating delayed TX/delayed RX on time (units: 32bit of 40bit DW time)
423
    uint16_t anc1RespTx2FinalRxDelay_sy ; //This is delay for RX on when A1 expecting Final form A0 or A2 expecting Final from A1
424
    uint16_t anc2RespTx2FinalRxDelay_sy ; //This is delay for RX on when A2 waiting for A0's final (anc to anc ranging)
425
    uint32_t preambleDuration32h ; //preamble duration in device time (32 MSBs of the 40 bit time)
426
    uint32_t tagRespRxDelay_sy ; //TX to RX delay time when tag is awaiting response message an another anchor
427
    uint32_t ancRespRxDelay_sy ; //TX to RX delay time when anchor is awaiting response message an another anchor
428

    
429
        int fwtoTime_sy ;        //this is FWTO for response message (used by initiating anchor in anchor to anchor ranging)
430
        int fwto4RespFrame_sy ; //this is a frame wait timeout used when awaiting reception of Response frames (used by both tag/anchor)
431
        int fwto4FinalFrame_sy ; //this is a frame wait timeout used when awaiting reception of Final frames
432
    uint32_t delayedTRXTime32h;                // time at which to do delayed TX or delayed RX (note TX time is time of SFD, RX time is RX on time)
433

    
434
    //message structure used for holding the data of the frame to transmit before it is written to the DW1000
435
        srd_msg_dsss msg_f ; // ranging message frame with 16-bit addresses
436
        iso_IEEE_EUI64_blink_msg blinkmsg ; // frame structure (used for tx blink message)
437
        srd_msg_dlss rng_initmsg ;        // ranging init message (destination long, source short)
438

    
439
        //Tag function address/message configuration
440
    uint8_t   shortAdd_idx ;                                // device's 16-bit address low byte (used as index into arrays [0 - 3])
441
    uint8_t   eui64[8];                                // device's EUI 64-bit address
442
    uint16_t  psduLength ;                        // used for storing the TX frame length
443
    uint8_t   frameSN;                                // modulo 256 frame sequence number - it is incremented for each new frame transmission
444
    uint16_t  panID ;                                        // panid used in the frames
445

    
446
        //64 bit timestamps
447
        //union of TX timestamps
448
        union {
449
        uint64_t txTimeStamp ;                   // last tx timestamp
450
        uint64_t tagPollTxTime ;                   // tag's poll tx timestamp
451
        uint64_t anchorRespTxTime ;           // anchor's reponse tx timestamp
452
        }txu;
453
    uint32_t tagPollTxTime32h ;
454
    uint64_t tagPollRxTime ;          // receive time of poll message
455

    
456

    
457
        //application control parameters
458
    uint8_t        wait4ack ;                                // if this is set to DWT_RESPONSE_EXPECTED, then the receiver will turn on automatically after TX completion
459
    uint8_t   wait4final ;
460

    
461
    uint8_t  instToSleep;                        // if set the instance will go to sleep before sending the blink/poll message
462
    uint8_t         instanceTimerEn;                // enable/start a timer
463
    uint32_t instanceWakeTime_ms;    // micro time at which the tag was waken up
464
    uint32_t nextWakeUpTime_ms;                // micro time at which to wake up tag
465

    
466
    uint8_t   rxResponseMaskAnc;                // bit mask - bit 0 not used;
467
                                                                        // bit 1 = received response from anchor ID = 1;
468
                                                                        // bit 2 from anchor ID = 2,
469
                                                                        // bit 3 set if two responses (from Anchor 1 and Anchor 2) received and A0 got third response (from A2)
470

    
471
    uint8_t   rxResponseMask;                        // bit mask - bit 0 = received response from anchor ID = 0, bit 1 from anchor ID = 1 etc...
472
    uint8_t   rxResponseMaskReport;   // this will be set before outputting range reports to signify which are valid
473
    uint8_t        rangeNum;                                // incremented for each sequence of ranges (each slot)
474
    uint8_t        rangeNumA[MAX_TAG_LIST_SIZE];                                // array which holds last range number from each tag
475
    uint8_t        rangeNumAnc;                        // incremented for each sequence of ranges (each slot) - anchor to anchor ranging
476
    uint8_t        rangeNumAAnc[MAX_ANCHOR_LIST_SIZE]; //array which holds last range number for each anchor
477

    
478
    int8_t   rxResps;                                // how many responses were received to a poll (in current ranging exchange)
479
    int8_t   remainingRespToRx ;                // how many responses remain to be received (in current ranging exchange)
480

    
481
    uint16_t  sframePeriod_ms;                // superframe period in ms
482
    uint16_t  slotDuration_ms;                // slot duration in ms
483
    uint16_t  numSlots;
484
    uint32_t  a0SlotTime_ms;                        // relative time in superframe at which A0 starts ranging (this is start of 2nd last slot)
485
    uint32_t  a1SlotTime_ms;                        // absolute time in superframe at which A1 starts ranging
486
    uint32_t  a2aStartTime_ms;                // absolute time in superframe at which A0 starts ranging
487
    int32_t   tagSleepCorrection_ms;  // tag's sleep correction to keep it in it's assigned slot
488

    
489
    //diagnostic counters/data, results and logging
490
    uint32_t tof[MAX_TAG_LIST_SIZE]; //this is an array which holds last ToF from particular tag (ID 0-(MAX_TAG_LIST_SIZE-1))
491

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

    
495
    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)
496

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

    
500
#if (DISCOVERY ==1)
501
    uint8_t tagListLen ;
502
    uint8_t tagList[MAX_TAG_LIST_SIZE][8];
503
#endif
504

    
505
    //debug counters
506
    //int txMsgCount; //number of transmitted messages
507
        //int rxMsgCount; //number of received messages
508
    //int rxTimeouts ; //number of received timeout events
509
        //int lateTX; //number of "LATE" TX events
510
        //int lateRX; //number of "LATE" RX events
511

    
512

    
513
        //ranging counters
514
    int longTermRangeCount ; //total number of ranges
515

    
516
    int newRange;                        //flag set when there is a new range to report TOF_REPORT_A2A or TOF_REPORT_T2A
517
    int newRangeAncAddress; //last 4 bytes of anchor address - used for printing/range output display
518
    int newRangeTagAddress; //last 4 bytes of tag address - used for printing/range output display
519
    int newRangeTime;
520

    
521
    uint8_t gatewayAnchor ; //set to TRUE = 1 if anchor address == GATEWAY_ANCHOR_ADDR
522

    
523
        //event queue - used to store DW1000 events as they are processed by the dw_isr/callback functions
524
    event_data_t dwevent[MAX_EVENT_NUMBER]; //this holds any TX/RX events and associated message data
525
    uint8_t dweventIdxOut;
526
    uint8_t dweventIdxIn;
527
    uint8_t dweventPeek;
528
    uint8_t monitor;
529
    uint32_t timeofTx ;
530

    
531
    uint8_t smartPowerEn;
532

    
533
#if (READ_EVENT_COUNTERS == 1)
534
        dwt_deviceentcnts_t ecounters;
535
#endif
536

    
537
} instance_data_t ;
538

    
539
//-------------------------------------------------------------------------------------------------------------
540
//
541
//        Functions used in logging/displaying range and status data
542
//
543
//-------------------------------------------------------------------------------------------------------------
544

    
545
// function to calculate and the range from given Time of Flight
546
int instance_calculate_rangefromTOF(int idx, uint32_t tofx);
547

    
548
void instance_cleardisttable(int idx);
549
void instance_set_tagdist(int tidx, int aidx);
550
double instance_get_tagdist(int idx);
551

    
552
double instance_get_idist(int idx);
553
double instance_get_idistraw(int idx);
554
int instance_get_idist_mm(int idx);
555
int instance_get_idistraw_mm(int idx);
556
uint8_t instance_validranges(void);
557

    
558
int instance_get_rnum(void);
559
int instance_get_rnuma(int idx);
560
int instance_get_rnumanc(int idx);
561
int instance_get_lcount(void);
562

    
563
int instance_newrangeancadd(void);
564
int instance_newrangetagadd(void);
565
int instance_newrangepolltim(void);
566
int instance_newrange(void);
567
int instance_newrangetim(void);
568

    
569
int instance_calc_ranges(uint32_t *array, uint16_t size, int reportRange, uint8_t* mask);
570

    
571
// clear the status/ranging data
572
void instance_clearcounts(void) ;
573

    
574
void instance_cleardisttableall(void);
575
//-------------------------------------------------------------------------------------------------------------
576
//
577
//        Functions used in driving/controlling the ranging application
578
//
579
//-------------------------------------------------------------------------------------------------------------
580

    
581
// Call init, then call config, then call run.
582
// initialise the instance (application) structures and DW1000 device
583
int instance_init(int role, DW1000Driver* drv);
584
// configure the instance and DW1000 device
585
void instance_config(instanceConfig_t *config, sfConfig_t *sfconfig) ;
586

    
587
// configure the MAC address
588
void instance_set_16bit_address(uint16_t address) ;
589
void instance_config_frameheader_16bit(instance_data_t *inst);
590

    
591
void tag_process_rx_timeout(instance_data_t *inst);
592

    
593
// called (periodically or from and interrupt) to process any outstanding TX/RX events and to drive the ranging application
594
int tag_run(void) ;
595
int anch_run(void) ;       // returns indication of status report change
596

    
597
// configure TX/RX callback functions that are called from DW1000 ISR
598
void rx_ok_cb_tag(const dwt_cb_data_t *cb_data);
599
void rx_to_cb_tag(const dwt_cb_data_t *cb_data);
600
void rx_err_cb_tag(const dwt_cb_data_t *cb_data);
601
//void tx_conf_cb_tag(const dwt_cb_data_t *cb_data);
602

    
603
void rx_ok_cb_anch(const dwt_cb_data_t *cb_data);
604
void rx_to_cb_anch(const dwt_cb_data_t *cb_data);
605
void rx_err_cb_anch(const dwt_cb_data_t *cb_data);
606
void tx_conf_cb(const dwt_cb_data_t *cb_data);
607

    
608
void instance_set_replydelay(int delayms);
609

    
610
// set/get the instance roles e.g. Tag/Anchor
611
// done though instance_init void instance_set_role(int mode) ;                //
612
int instance_get_role(void) ;
613
// get the DW1000 device ID (e.g. 0xDECA0130 for DW1000)
614
uint32_t instance_readdeviceid(void) ;                                 // Return Device ID reg, enables validation of physical device presence
615

    
616
void rnganch_change_back_to_anchor(instance_data_t *inst);
617
int instance_send_delayed_frame(instance_data_t *inst, int delayedTx);
618

    
619
uint64_t instance_convert_usec_to_devtimeu (double microsecu);
620

    
621

    
622
void instance_seteventtime(event_data_t *dw_event, uint8_t* timeStamp);
623

    
624
int instance_peekevent(void);
625

    
626
void instance_saveevent(event_data_t newevent, uint8_t etype);
627

    
628
event_data_t instance_getsavedevent(void);
629

    
630
void instance_putevent(event_data_t newevent, uint8_t etype);
631

    
632
event_data_t* instance_getevent(int x);
633

    
634
void instance_clearevents(void);
635

    
636
void instance_notify_DW1000_inIDLE(int idle);
637

    
638
// configure the antenna delays
639
void instance_config_antennadelays(uint16_t tx, uint16_t rx);
640
void instance_set_antennadelays(void);
641
uint16_t instance_get_txantdly(void);
642
uint16_t instance_get_rxantdly(void);
643

    
644
// configure the TX power
645
void instance_config_txpower(uint32_t txpower);
646
void instance_set_txpower(void);
647
int instance_starttxtest(int framePeriod);
648

    
649

    
650
instance_data_t* instance_get_local_structure_ptr(unsigned int x);
651

    
652
#ifdef __cplusplus
653
}
654
#endif
655

    
656
#endif /* defined(AMIROLLD_CFG_USE_DW1000) */
657

    
658
#endif /* DECAINSTANCE_H */