instance.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357
  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 _INSTANCE_H_
  14. #define _INSTANCE_H_
  15. #ifdef __cplusplus
  16. extern "C" {
  17. #endif
  18. #include "deca_types.h"
  19. #include "deca_device_api.h"
  20. #include "deca_param_types.h"
  21. #include "port.h"
  22. #include "config.h"
  23. #include "stateMachine.h"
  24. #include "message.h"
  25. #include "event.h"
  26. // TEST anc for peroid and sucess rate
  27. #define ANC_RESP_TEST 0
  28. #define REPORT2WIFI
  29. //#define REPORT2COM
  30. #if !defined(REPORT2COM) && !defined(REPORT2WIFI)
  31. #define REPORT2COM
  32. #endif
  33. /******************************************************************************************************************
  34. ********************* NOTES on DW (MP) features/options ***********************************************************
  35. *******************************************************************************************************************/
  36. //#define DEEP_SLEEP (1) //To enable deep-sleep set this to 1
  37. #define DEEP_SLEEP (0) //To enable deep-sleep set this to 1
  38. //DEEP_SLEEP mode can be used, for example, by a Tag instance to put the DW1000 into low-power deep-sleep mode:
  39. // when the Anchor is sending the range report back to the Tag, the Tag will enter sleep after a ranging exchange is finished
  40. // once it receives a report or times out, before the next poll message is sent (before next ranging exchange is started).
  41. #define CORRECT_RANGE_BIAS (1) // Compensate for small bias due to uneven accumulator growth at close up high power
  42. #define ANCTOANCTWR (0) //if set to 1 then anchor to anchor TWR will be done in the last slot
  43. /******************************************************************************************************************
  44. *******************************************************************************************************************
  45. *******************************************************************************************************************/
  46. #define NUM_INST 1
  47. #define SPEED_OF_LIGHT (299702547.0) // in m/s in air
  48. #define MASK_40BIT (0x00FFFFFFFFFF) // DW1000 counter is 40 bits
  49. #define MASK_TXDTS (0x00FFFFFFFE00) //The TX timestamp will snap to 8 ns resolution - mask lower 9 bits.
  50. #define SIG_RX_UNKNOWN 99 // Received an unknown frame
  51. // !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
  52. // NOTE: the maximum RX timeout is ~ 65ms
  53. // !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
  54. #define TOF_REPORT_NUL 0
  55. #define TOF_REPORT_T2A 1
  56. #define TOF_REPORT_A2A 2
  57. #define INVALID_TOF (0xABCDFFFF)
  58. /******************************************************************************************************************
  59. *******************************************************************************************************************
  60. *******************************************************************************************************************/
  61. typedef enum _rtx_stage_{
  62. RTX_IDLE=0,
  63. RTX_RX,
  64. RTX_TX_THEN_RX,
  65. RTX_TX,
  66. }eRxTxStage;
  67. // TX power and PG delay configuration structure
  68. typedef struct {
  69. uint8_t PGdelay;
  70. //TX POWER
  71. //31:24 BOOST_0.125ms_PWR
  72. //23:16 BOOST_0.25ms_PWR-TX_SHR_PWR
  73. //15:8 BOOST_0.5ms_PWR-TX_PHR_PWR
  74. //7:0 DEFAULT_PWR-TX_DATA_PWR
  75. uint32_t txPwr[2]; //
  76. }tx_struct;
  77. typedef struct _tof_inform_
  78. {
  79. uint16_t address;
  80. uint32_t value;
  81. }stTofInformation;
  82. typedef struct _tof_result_
  83. {
  84. uint16_t address;
  85. double distanceRaw;
  86. double distanceBias;
  87. }stFofResult;
  88. typedef struct
  89. {
  90. //diagnostic counters/data, results and logging
  91. //stTofInformation tofOfTags[MAX_TAG_LIST_SIZE]; //this is an array which holds last ToF from particular tag (ID 0-7)
  92. //this is an array which holds last ToF to each anchor it should
  93. stTofInformation tofArray[MAX_ANCHOR_LIST_SIZE]; //contain 4 ToF to 4 anchors all relating to same range number sequence
  94. //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)
  95. //this is an array which holds last ToFs of the Anchor to Anchor ranging
  96. //uint32_t tofArrayAnc[MAX_ANCHOR_LIST_SIZE]; //it contains 3 ToFs relating to same range number sequence (0, 0-1, 0-2, 1-2)
  97. //stFofResult tagdistance[MAX_ANCHOR_LIST_SIZE];
  98. stFofResult distance[MAX_ANCHOR_LIST_SIZE];
  99. }twr_result;
  100. typedef struct {
  101. //timeouts and delays
  102. int32_t tagSleepTime_ms; //in milliseconds - defines the nominal Tag sleep time period
  103. int32_t tagSleepRnd; //add an extra slot duration to sleep time to avoid collision before getting synced by anchor 0
  104. uint32_t delayedReplyTime; // delayed reply time of ranging-init/response/final message
  105. int32_t tagSleepCorrection;
  106. int32_t tagSleepCorrection2;
  107. uint32_t longTermRangeCount ; //total number of ranges
  108. //64 bit timestamps
  109. //union of TX timestamps
  110. uint64_t tagPollRxTime ; // receive time of poll message
  111. union {
  112. uint64_t txTimeStamp ; // last tx timestamp
  113. uint64_t tagPollTxTime ; // tag's poll tx timestamp
  114. uint64_t anchorRespTxTime ; // anchor's reponse tx timestamp
  115. }txu;
  116. uint64_t tagFinalRxTime ; // receive time of poll message
  117. }tm_and_delays;
  118. //Application data
  119. #pragma pack(1)
  120. typedef struct _tag_cmd_ {
  121. uint8_t control;
  122. uint8_t command;
  123. uint8_t reservered1;
  124. uint8_t reservered2;
  125. uint8_t reservered3;
  126. uint8_t reservered4;
  127. }stTagControlCommand;
  128. typedef struct _tag_status_ {
  129. uint8_t status;
  130. uint8_t power;
  131. uint8_t heartRate;
  132. uint8_t reserved1;
  133. uint8_t reserved2;
  134. float txRssi;
  135. float txRssiFinal;
  136. }stTagStatus;
  137. #pragma pack()
  138. typedef struct _tag_range_inform_
  139. {
  140. uint16_t address;
  141. uint8_t flag;
  142. uint8_t rangeNumA;
  143. int16_t slot;
  144. uint16_t rxRespsIdx; //index into the array below (current tag (4bits)/seq number(4bits))
  145. int8_t rxResps[RESP_RECORD_COUNT];
  146. uint32_t tickUpdate;
  147. uint32_t tofOfTags;
  148. stTagStatus tagStatus;
  149. stTagControlCommand tagCommand;
  150. }stTagRangeInformation;
  151. typedef struct
  152. {
  153. inst_setting setting; //setting
  154. event_queue evtQueue; //event queue - used to store DW1000 events as they are processed by the dw_isr/callback functions
  155. twr_result twrResult; //twr result record
  156. state_machine stateMachine; //state machine
  157. tm_and_delays tmAndDelays; //time and delays params
  158. message_frame msgFrame; //message frame
  159. stTagRangeInformation rangeInform[MAX_TAG_LIST_SIZE];
  160. } instance_data_t ;
  161. typedef struct _inst_debug
  162. {
  163. uint32_t errFrameCount;
  164. uint8_t toBeHandled;
  165. uint32_t type;
  166. uint32_t ancPollCount;
  167. uint32_t ancRespCounts[MAX_ANCHOR_LIST_SIZE];
  168. uint32_t andRespSentCount;
  169. uint32_t ancFinalCount;
  170. uint32_t ancRespFailsCounts[MAX_ANCHOR_LIST_SIZE];
  171. uint32_t ancRespLateCount;
  172. uint32_t tofSuccess;
  173. uint32_t calculateFails;
  174. uint32_t rxTimeoutCount;
  175. }stInstDebug;
  176. extern volatile eRxTxStage trxStage;
  177. extern volatile stExtandCommand g_stExtandCommand;
  178. //-------------------------------------------------------------------------------------------------------------
  179. //
  180. // Functions used in logging/displaying range and status data
  181. //
  182. //-------------------------------------------------------------------------------------------------------------
  183. // function to calculate and report the Time of Flight to the GUI/display
  184. int reportTOF(int idx, stTofInformation* tofx);
  185. void clearDistTable(int idx);
  186. void setTagDist(int tidx, int aidx);
  187. double getTagDist(int idx);
  188. // clear the status/ranging data
  189. void instClearCounts(void) ;
  190. //-------------------------------------------------------------------------------------------------------------
  191. //
  192. // Functions used in driving/controlling the ranging application
  193. //
  194. //-------------------------------------------------------------------------------------------------------------
  195. void instClose(void);
  196. // Call init, then call config, then call run. call close when finished
  197. // initialise the instance (application) structures and DW1000 device
  198. int instance_init(void);
  199. // configure the instance and DW1000 device
  200. void instance_config(const instanceConfig_t *config, const sfConfig_t *sfconfig);
  201. void instUpdateFramePeroid(uint16_t period);
  202. // configure the MAC address
  203. void instSetAddress(uint16_t address) ;
  204. void instSetPanID(uint16_t id);
  205. void instProcessRXtimeout(instance_data_t *inst);
  206. int appState_TA_INIT(instance_data_t *inst, uint32_t *status, int message);
  207. int appState_TA_TX_WAIT_CONF(instance_data_t *inst, uint32_t *status, int message);
  208. int appState_TA_RXE_WAIT(instance_data_t *inst, uint32_t *status, int message);
  209. int appState_TA_RX_WAIT_DATA(instance_data_t *inst, uint32_t *status, int message);
  210. // called (periodically or from and interrupt) to process any outstanding TX/RX events and to drive the ranging application
  211. int instance_run(void) ; // returns indication of status report change
  212. int testAppRun(instance_data_t *inst, int message);
  213. // calls the DW1000 interrupt handler
  214. #define instance_process_irq(x) dwt_isr() //call device interrupt handler
  215. // configure TX/RX callback functions that are called from DW1000 ISR
  216. void instRXcallback(const dwt_callback_data_t *rxd);
  217. void instTXcallback(const dwt_callback_data_t *txd);
  218. // sets the Tag sleep delay time (the time Tag "sleeps" between each ranging attempt)
  219. void instSetTagSleepDelay(int rangingsleep);
  220. void instSetReplyDelay(int delayms);
  221. // get the DW1000 device ID (e.g. 0xDECA0130 for DW1000)
  222. uint32_t instReadDeviceID(void) ; // Return Device ID reg, enables validation of physical device presence
  223. double instGetIdist(int idx);
  224. double instGetIdistRaw(int idx);
  225. uint16_t instGetIdistAddress(int idx);
  226. int instGetIdist_mm(int idx);
  227. int instGetIdistRaw_mm(int idx);
  228. uint8_t instValidRanges(void);
  229. void instClearDistTableAll();
  230. void instBack2Anchor(instance_data_t *inst);
  231. int instSendDlyPacket(instance_data_t *inst, int delayedTx);
  232. int instGetRNum(void);
  233. uint32_t instGetTagRangeNum(uint32_t address);
  234. int instGetLCount(void);
  235. int instNewRangeAncAdd(void);
  236. int instNewRangeTagAdd(void);
  237. int instWRangePollTim(void);
  238. int instNewRange(void);
  239. uint32_t instNewRangeTim(void);
  240. uint64_t convertMicroSec2DeviceTime (double microSec);
  241. double convertDeviceTime2Sec(int32_t dt);
  242. #define DWT_PRF_64M_RFDLY (514.462f)
  243. #define DWT_PRF_16M_RFDLY (513.9067f)
  244. extern const uint16_t rfDelays[2];
  245. extern const uint16_t rfDelaysTREK[2];
  246. extern const tx_struct txSpectrumConfig[8];
  247. extern const tx_struct txManualSpectrumConfig[8];
  248. extern const tx_struct maxSpectrumConfig[8];
  249. extern volatile instance_data_t instance_data[NUM_INST] ;
  250. extern volatile instance_data_t* gInst;
  251. int instPeekEvent(void);
  252. void instSaveEvent(event_data_t newEvent, uint8_t eType);
  253. event_data_t instGetSavedEvent(void);
  254. void instPutEvent(event_data_t newEvent, uint8_t eType);
  255. event_data_t* instGetEvent(int x);
  256. void instClearEvents(void);
  257. // configure TX power
  258. void instConfigTXPower(uint32_t txpower);
  259. void instSetTXPower(void);
  260. // configure the antenna delays
  261. void instConfigAntennaDelays(uint16_t tx, uint16_t rx);
  262. void instSetAntennaDelays(void);
  263. uint16_t instTXAntennaDly(void);
  264. uint16_t instRXAntennaDly(void);
  265. int instStartTXtest(int framePeriod);
  266. uint32_t GetTofResult(uint32_t address);
  267. void SetTofResult(uint32_t addr, uint32_t value);
  268. void ClearTagTofResult(uint32_t address);
  269. uint32_t GetTagTofResult(uint32_t address);
  270. void SetTagTofResult(uint32_t addr, uint32_t value);
  271. void ClearTofResult(uint32_t address);
  272. void SaveTagCommand(uint8_t* buf, uint32_t len);
  273. int GetTagCommand(uint16_t sourceAddress, stTagControlCommand* cmd);
  274. int GetTagStatus(uint32_t address, stTagStatus* status);
  275. void SaveTagStatus(uint16_t address, uint8_t* messageData);
  276. int instRangeSlotUpdate();
  277. #ifdef __cplusplus
  278. }
  279. #endif
  280. #endif