instance_app.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664
  1. /*
  2. *
  3. *
  4. */
  5. /* Includes ------------------------------------------------------------------*/
  6. #include "main.h"
  7. #include "instance.h"
  8. /* Private typedef -----------------------------------------------------------*/
  9. #pragma pack(4)
  10. typedef struct
  11. {
  12. uint8_t channel ;
  13. uint8_t prf ;
  14. uint8_t datarate ;
  15. uint8_t preambleCode ;
  16. uint8_t preambleLength ;
  17. uint8_t pacSize ;
  18. uint8_t nsSFD ;
  19. uint8_t txPower;
  20. uint16_t sfdTO ;
  21. } chConfig_t ;
  22. #pragma pack()
  23. #define REPORTBUFFER_LENGTH 4096
  24. #define REPORTBUFFER_COUNT 4
  25. #define REPORTBUFFER_MASK (REPORTBUFFER_COUNT-1)
  26. #define REPORT_HEAD_LENGTH 7
  27. #define REPORT_TAGTOF_LENGTH 128
  28. #define REPORT_THRESHOLD (REPORT_HEAD_LENGTH+(REPORTBUFFER_LENGTH-REPORT_HEAD_LENGTH)/REPORT_TAGTOF_LENGTH*REPORT_TAGTOF_LENGTH)
  29. #define REPORT_PEROID 480
  30. typedef struct _report_buf_{
  31. uint8_t state;
  32. uint8_t count;
  33. uint16_t length;
  34. uint8_t buffer[REPORTBUFFER_LENGTH];
  35. }stReportBuffer;
  36. typedef struct _range_report_
  37. {
  38. uint16_t index;
  39. uint16_t state;
  40. uint32_t time;
  41. stReportBuffer reportBuffers[REPORTBUFFER_COUNT];
  42. }stRangeReport;
  43. typedef struct _anc_command_
  44. {
  45. uint8_t command1;
  46. uint8_t command2;
  47. uint8_t command3;
  48. uint8_t tagCount;
  49. }stAnchorCommand;
  50. /* Private define ------------------------------------------------------------*/
  51. stRangeReport rangeReport = {0};
  52. stAnchorCommand ancCommand = {0};
  53. volatile stExtandCommand g_stExtandCommand = {0};
  54. /* Private macro -------------------------------------------------------------*/
  55. #define DEFAULT_DWM_ADDRESS 1
  56. #define MASK_RANGEPEROID 0x0f
  57. #define DEFAULT_RANGEPEROID 0
  58. #define MIN_RANGEPEROID 2
  59. /* Crystal frequency, in hertz. */
  60. #define XTAL_FREQ_HZ 38400000
  61. ///command defination
  62. #define COMMAND_ITME_SIZE 8
  63. #define CMD_ANC_ADDRL 0
  64. #define CMD_ANC_ADDRH 1
  65. #define CMD_ANC_CMD1 2
  66. #define CMD_ANC_CMD2 3
  67. #define CMD_ANC_CMD3 4
  68. #define CMD_ANC_TAGCOUNT 5
  69. #define CMD_TAGx_ADDRL 0
  70. #define CMD_TAGx_ADDRH 1
  71. #define CMD_TAGx_CTRL 2
  72. #define CMD_TAGx_CMD 3
  73. #define CMD_TAGx_RESV1 4
  74. #define CMD_TAGx_RESV2 5
  75. /* Private variables ---------------------------------------------------------*/
  76. //
  77. // 0 1 2 ---- F
  78. // peroid (ms)
  79. const uint16_t PeroidValue[16] = {
  80. 1000, 10, 15, 30, 60, 125, 250, 500, 1000, 2000, 3000, 5000, 10000, 30000,60000, 1000
  81. };
  82. const chConfig_t defaultChConfig = {
  83. .channel = 2, // channel
  84. .prf = DWT_PRF_64M, // prf
  85. .datarate = DWT_BR_6M8, // datarate
  86. .preambleCode = 9, // preambleCode
  87. .preambleLength = DWT_PLEN_128, // preambleLength
  88. .pacSize = DWT_PAC8, // pacSize
  89. .nsSFD = 0, // non-standard SFD
  90. .txPower = 0x1f,
  91. .sfdTO = (129 + 8 - 4), //SFD timeout
  92. };
  93. const uint8_t prf16pCodes[4] = { 3, 4, 3, 4 };
  94. const uint8_t prf64pCodes[4] = { 9, 10, 11, 12 };
  95. //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 )
  96. const sfConfig_t sfConfig ={
  97. .slotPeriod = (TA_SLOT_TIME), // slot period ms
  98. .numSlots = (TA_SLOT_COUNT), // number of slots (only 10 are used) - thus 100 ms superframe means 10 Hz location rate
  99. .sfPeriod = (TA_SLOT_TIME * TA_SLOT_COUNT), // superframe period (100 ms - gives 10 Hz)
  100. .pollSleepDly = (TA_SLOT_TIME * TA_SLOT_COUNT), // poll sleep delay (tag sleep time, usually = superframe period)
  101. .replyDly = (TA_SLOT_TIME*TA_SLOT_FACTOR),
  102. };
  103. const stStationConfig defaultDwmConfig={
  104. .address = DEFAULT_DWM_ADDRESS,//
  105. .panID = 0xdeca,//
  106. .signal={
  107. .channel = 0, // channel2: 0; channel5: 1;
  108. .prfMode = 0, // 0:64M; 1:16M;
  109. .pCode = 0, // Preamble Code, ON PRF=64M: {0:9,1:10,2:11,3:12}, ON PRF=16M: {0:3,1:4,2:3,3:4}
  110. .encrypt = 1, // 0:None; 1:TEA key=ScenAuto1234!@#$
  111. .reserved1 = 0, // reserved1
  112. .reserved2 = 0, // reserved2
  113. .reserved3 = 0, // reserved3
  114. .temp = 0,
  115. .peroid = DEFAULT_RANGEPEROID,
  116. },
  117. .fixPower = 0x1f1f1f1f,
  118. };
  119. //
  120. stStationConfig dwsConfig = {
  121. .panID = 0xdeca,//
  122. .address = DEFAULT_DWM_ADDRESS,//
  123. .signal={
  124. .channel = 0, // channel2: 0; channel5: 1;
  125. .prfMode = 0, // 0:64M; 1:16M;
  126. .pCode = 0, // Preamble Code, ON PRF=64M: {0:9,1:10,2:11,3:12}, ON PRF=16M: {0:3,1:4,2:3,3:4}
  127. .encrypt = 1, // 0:None; 1:TEA key=ScenAuto1234!@#$
  128. .reserved1 = 0, // reserved1
  129. .reserved2 = 0, // reserved2
  130. .reserved3 = 0, // reserved3
  131. .temp = 0,
  132. .peroid = DEFAULT_RANGEPEROID,
  133. },
  134. .fixPower = 0x1f1f1f1f,
  135. };
  136. //
  137. uint32_t instance_anchaddr = 0;
  138. /* Private function prototypes -----------------------------------------------*/
  139. void addressconfigure(uint32_t addr);
  140. int decarangingmode(stStationConfig cfg);
  141. // ======================================================
  142. void DWM_SetDefault(void)
  143. {
  144. dwsConfig = defaultDwmConfig;
  145. }
  146. //
  147. // Configure instance tag/anchor/etc... addresses
  148. //
  149. void addressconfigure(uint32_t addr)
  150. {
  151. uint16_t instAddress ;
  152. instance_anchaddr = addr;
  153. instAddress = GATEWAY_ANCHOR_ADDR | instance_anchaddr;
  154. instSetAddress(instAddress);
  155. }
  156. //returns the use case / operational mode
  157. int decarangingmode(stStationConfig cfg)
  158. {
  159. return cfg.signal.prfMode;
  160. }
  161. uint32_t inittestapplication(stStationConfig cfg)
  162. {
  163. int chan;//, tagaddr, ancaddr;
  164. uint32_t devID ;
  165. instanceConfig_t instConfig;
  166. //uint16_t shortAddress;
  167. //uint16_t lp_osc_freq, sleep_cnt;
  168. BSP_DWM_SPI_FastRate(0); //max SPI before PLLs configured is ~4M
  169. //this is called here to wake up the device (i.e. if it was in sleep mode before the restart)
  170. devID = instReadDeviceID() ;
  171. /*sprintf(debugBuffer, "ID: %d\r\n", devID);
  172. ReportMessage(debugBuffer);*/
  173. if(DWT_DEVICE_ID != devID) //if the read of device ID fails, the DW1000 could be asleep
  174. {
  175. port_SPIx_CS_clear(); //CS low
  176. Sleep(2); //200 us to wake up then waits 5ms for DW1000 XTAL to stabilise
  177. port_SPIx_CS_set(); //CS high
  178. Sleep(7);
  179. devID = instReadDeviceID() ;
  180. // SPI not working or Unsupported Device ID
  181. if(DWT_DEVICE_ID != devID)
  182. return(-1) ;
  183. //clear the sleep bit - so that after the hard reset below the DW does not go into sleep
  184. dwt_softreset();
  185. }
  186. devID = dwt_read32bitoffsetreg(0x0F,0);//SYS_STATUS_ID
  187. devID++;
  188. //reset the DW1000 by driving the RSTn line low
  189. reset_DW1000();
  190. /*dwt_setaddress16(0xa5a5);
  191. dwt_getaddress16(&shortAddress);
  192. shortAddress++;*/
  193. devID = dwt_read32bitoffsetreg(0x0F,0);//SYS_STATUS_ID
  194. devID++;
  195. chan = instance_init();
  196. if (0 > chan)
  197. return(-1) ; // Some failure has occurred
  198. Sleep(1);
  199. /*port_SPIx_clear_chip_select(); //CS low
  200. Sleep(1); //200 us to wake up then waits 5ms for DW1000 XTAL to stabilise
  201. port_SPIx_set_chip_select(); //CS high
  202. Sleep(7);*/
  203. devID = dwt_read32bitoffsetreg(0x0F,0);//SYS_STATUS_ID
  204. devID++;
  205. #if (DEEP_SLEEP==2)
  206. /* Calibrate and configure sleep count. This has to be done with DW1000 clocks set to crystal speed. */
  207. lp_osc_freq = (XTAL_FREQ_HZ / 2) / dwt_calibratesleepcnt();
  208. sleep_cnt = ((SLEEP_TIME_MS * lp_osc_freq) / 1000) >> 12;
  209. dwt_configuresleepcnt(sleep_cnt);
  210. #endif
  211. BSP_DWM_SPI_FastRate(1); //increase SPI to max
  212. devID = instReadDeviceID() ;
  213. if (DWT_DEVICE_ID != devID) // Means it is NOT DW1000 device
  214. {
  215. // SPI not working or Unsupported Device ID
  216. return(-1) ;
  217. }
  218. instSetPanID(cfg.panID);
  219. addressconfigure(cfg.address) ; // set up initial payload configuration
  220. chConfig_t chCfg = defaultChConfig;
  221. chCfg.channel = cfg.signal.channel ? 5 : 2;
  222. chCfg.prf = cfg.signal.prfMode ? DWT_PRF_16M : DWT_PRF_64M;
  223. if (DWT_PRF_64M == chCfg.prf)
  224. {
  225. chCfg.preambleCode = prf64pCodes[cfg.signal.pCode];
  226. }
  227. else
  228. {
  229. chCfg.preambleCode = prf16pCodes[cfg.signal.pCode];
  230. }
  231. chan = instConfig.channelNumber = chCfg.channel ;
  232. instConfig.preambleCode = chCfg.preambleCode ;
  233. instConfig.pulseRepFreq = chCfg.prf ;
  234. instConfig.pacSize = chCfg.pacSize ;
  235. instConfig.nsSFD = chCfg.nsSFD ;
  236. instConfig.sfdTO = chCfg.sfdTO ;
  237. instConfig.dataRate = chCfg.datarate ;
  238. instConfig.preambleLen = chCfg.preambleLength ;
  239. instConfig.txPower = 0x1f;
  240. instance_config(&instConfig, &sfConfig) ; // Set operating channel etc
  241. chan = PeroidValue[MASK_RANGEPEROID & cfg.signal.peroid];
  242. instUpdateFramePeroid(chan);
  243. g_stExtandCommand.command = EXTCMD_ANCFRAMEPEROID;
  244. g_stExtandCommand.param = dwsConfig.signal.peroid;
  245. return devID;
  246. }
  247. void process_dwRSTn_irq(void)
  248. {
  249. }
  250. void process_deca_irq(void)
  251. {
  252. //do{
  253. //__HAL_GPIO_EXTI_CLEAR_IT(DWM_IRQ_PIN);
  254. instance_process_irq(0);
  255. //}while(port_CheckEXT_IRQ() == 1); //while IRS line active (ARM can only do edge sensitive interrupts)
  256. //}while(port_CheckEXT_IRQ() != RESET); //while IRS line active (ARM can only do edge sensitive interrupts)
  257. }
  258. // ======================================================
  259. //
  260. int UpdateTagCommand(uint8_t* buf, uint32_t len)
  261. {
  262. if(len < COMMAND_ITME_SIZE)
  263. {
  264. return -1;
  265. }
  266. int index = 0, count = 0;
  267. index = buf[CMD_ANC_ADDRL] + (buf[CMD_ANC_ADDRH]<<8);
  268. if(index != gInst->setting.instanceAddress16)
  269. {
  270. return -2;
  271. }
  272. ancCommand.command1 = buf[CMD_ANC_CMD1];
  273. ancCommand.command2 = buf[CMD_ANC_CMD2];
  274. ancCommand.command3 = buf[CMD_ANC_CMD3];
  275. ancCommand.tagCount = buf[CMD_ANC_TAGCOUNT];
  276. index = 6;
  277. count = 0;
  278. for(; index < len && count < ancCommand.tagCount && count < MAX_TAG_LIST_SIZE; count++)
  279. {
  280. SaveTagCommand(buf+index, COMMAND_ITME_SIZE);
  281. index += COMMAND_ITME_SIZE;
  282. }
  283. return count;
  284. }
  285. void StoreTagStatus()
  286. {
  287. //tagStatus
  288. }
  289. void ExcuteTimeSettingCommand(void)
  290. {
  291. stDateTime.year = rtcTime.tm_year;
  292. stDateTime.month = rtcTime.tm_mon;
  293. stDateTime.day = rtcTime.tm_mday;
  294. stDateTime.hour = rtcTime.tm_hour;
  295. stDateTime.minute = rtcTime.tm_min;
  296. stDateTime.second = rtcTime.tm_sec;
  297. RtcSetDateTime(&stDateTime);
  298. secondsFromCentry(&rtcTime, (uint32_t*)&g_stTimeValue.timeValue);
  299. g_stTimeValue.tickReceived = 2;
  300. }
  301. void ClearTimeSettingCommand(void)
  302. {
  303. if(0 < g_stTimeValue.timeValue)
  304. {
  305. if (g_stTimeValue.tickReceived > 0)
  306. {
  307. g_stTimeValue.tickReceived--;
  308. }
  309. else
  310. {
  311. g_stTimeValue.timeValue = 0;//clear it after sent
  312. g_stTimeValue.tickReceived = 0;
  313. }
  314. }
  315. }
  316. void SendRangeReport(void)
  317. {
  318. stReportBuffer* pReport = rangeReport.reportBuffers + rangeReport.index;
  319. //ReportMessage(pReport->buffer, pReport->length);
  320. SendMessage(pReport->buffer, pReport->length);
  321. SendMessageDbg(pReport->buffer, pReport->length);
  322. SendMessageWifi(pReport->buffer, pReport->length);
  323. }
  324. void CreateRangeReportHead()
  325. {
  326. if(rangeReport.state) return;
  327. stReportBuffer* pReport;
  328. rangeReport.state = 1;
  329. pReport = rangeReport.reportBuffers + rangeReport.index;
  330. pReport->state = 1;
  331. pReport->count = 0;
  332. pReport->buffer[PROTOCAL_START_INDEX] = PROTOCAL_HEAD_FLAG;
  333. pReport->buffer[PROTOCAL_LENLOW_INDEX] = 0;
  334. pReport->buffer[PROTOCAL_LENHIGH_INDEX] = 0;
  335. pReport->buffer[PROTOCAL_CMD_INDEX] = CFG_COMMAND_REPORTRANGE;
  336. int index = 0;
  337. pReport->buffer[PROTOCAL_DATASTART_INDEX + index++] = gInst->setting.instanceAddress16 & 0xff;
  338. pReport->buffer[PROTOCAL_DATASTART_INDEX + index++] = (gInst->setting.instanceAddress16>>8) & 0xff;
  339. pReport->buffer[PROTOCAL_DATASTART_INDEX + index++] = anchorStatus.value;
  340. uint8_t pwDCFlag = powerVoltage.dcToggleFlag >= 5 ? 4 : 0;
  341. uint8_t pwStatus = (powerVoltage.status & 0x0f) | pwDCFlag;
  342. pReport->buffer[PROTOCAL_DATASTART_INDEX + index++] = (pwStatus << 4) + (powerVoltage.power & 0x0f);
  343. pReport->buffer[PROTOCAL_DATASTART_INDEX + index++] = 0;
  344. pReport->buffer[PROTOCAL_DATASTART_INDEX + index++] = 0;
  345. pReport->length = PROTOCAL_DATASTART_INDEX+index;
  346. }
  347. void UpdateAnchorStatus(void)
  348. {
  349. stReportBuffer* pReport;
  350. pReport = rangeReport.reportBuffers + rangeReport.index;
  351. anchorStatus.status.range = pReport->count > 0 ? 1 : 0;
  352. pReport->buffer[PROTOCAL_DATASTART_INDEX + 2] = anchorStatus.value;
  353. uint8_t pwDCFlag = powerVoltage.dcToggleFlag >= 5 ? 4 : 0;
  354. uint8_t pwStatus = (powerVoltage.status & 0x0f) | pwDCFlag;
  355. pReport->buffer[PROTOCAL_DATASTART_INDEX + 3] = (pwStatus << 4) + (powerVoltage.power & 0x0f);
  356. uint8_t pwDCVoltage = powerVoltage.batteryVoltage > 400 ? powerVoltage.batteryVoltage - 400 : 0;
  357. pReport->buffer[PROTOCAL_DATASTART_INDEX + 4] = pwDCVoltage;
  358. pReport->buffer[PROTOCAL_DATASTART_INDEX + 5] = pReport->count;
  359. }
  360. void AddRange2Report()
  361. {
  362. if(!rangeReport.state) return;
  363. stReportBuffer* pReport = rangeReport.reportBuffers + rangeReport.index;
  364. if(!pReport->state)return;
  365. uint32_t valid, rxa, tof;
  366. stTagStatus tagStatus;
  367. uint32_t addr = instNewRangeTagAdd();
  368. pReport->buffer[pReport->length++] = (uint8_t)addr;
  369. pReport->buffer[pReport->length++] = (uint8_t)(addr>>8);
  370. pReport->buffer[pReport->length++] = instGetTagRangeNum(addr);
  371. GetTagStatus(addr, &tagStatus);
  372. pReport->buffer[pReport->length++] = tagStatus.reserved1;
  373. pReport->buffer[pReport->length++] = tagStatus.status;
  374. pReport->buffer[pReport->length++] = tagStatus.power;
  375. pReport->buffer[pReport->length++] = tagStatus.heartRate;
  376. pReport->buffer[pReport->length++] = tagStatus.reserved2;
  377. for(valid = 0,rxa = 0; valid < MAX_ANCHOR_LIST_SIZE; valid++)
  378. {
  379. addr = instGetIdistAddress(valid);
  380. if(BROADCAST_ADDR == addr)continue;
  381. tof = instGetIdist_mm(valid);
  382. pReport->buffer[pReport->length++] = (uint8_t)addr;
  383. pReport->buffer[pReport->length++] = (uint8_t)(addr>>8);
  384. pReport->buffer[pReport->length++] = (uint8_t)(tof);
  385. pReport->buffer[pReport->length++] = (uint8_t)(tof>>8);
  386. pReport->buffer[pReport->length++] = (uint8_t)(tof>>16);
  387. pReport->buffer[pReport->length++] = (uint8_t)(tof>>24);
  388. rxa++;
  389. if(rxa >= NUM_EXPECTED_RESPONSES)
  390. {
  391. break;
  392. }
  393. }
  394. if(rxa < NUM_EXPECTED_RESPONSES)
  395. {
  396. for(; rxa < NUM_EXPECTED_RESPONSES; rxa++)
  397. {
  398. pReport->buffer[pReport->length++] = 0;
  399. pReport->buffer[pReport->length++] = 0;
  400. pReport->buffer[pReport->length++] = 0;
  401. pReport->buffer[pReport->length++] = 0;
  402. pReport->buffer[pReport->length++] = 0;
  403. pReport->buffer[pReport->length++] = 0;
  404. }
  405. }
  406. pReport->count++;//tag count
  407. }
  408. void FinishRangeReport(void)
  409. {
  410. if(!rangeReport.state) return;
  411. stReportBuffer* pReport = rangeReport.reportBuffers + rangeReport.index;
  412. if(!pReport->state)return;
  413. UpdateAnchorStatus();
  414. uint32_t len = pReport->length - PROTOCAL_HEAD_LENGTH + 1 + PROTOCAL_CHECKSUM_LENGTH;
  415. pReport->buffer[PROTOCAL_LENLOW_INDEX] = (uint8_t)len;
  416. pReport->buffer[PROTOCAL_LENHIGH_INDEX] = (uint8_t)(len>>8);
  417. uint32_t dataLen = pReport->length - PROTOCAL_HEAD_LENGTH;
  418. uint32_t remainder = dataLen % 8;
  419. if (remainder)
  420. {
  421. remainder = 8 - remainder;
  422. }
  423. while (remainder)
  424. {
  425. pReport->buffer[pReport->length++] = 0;
  426. dataLen++;
  427. len++;
  428. remainder--;
  429. }
  430. if (dwsConfig.signal.encrypt)
  431. {
  432. pReport->buffer[PROTOCAL_START_INDEX] = PROTOCAL_HEAD_ENCRYPT_FLAG;
  433. EncryptTEA(pReport->buffer, PROTOCAL_DATASTART_INDEX, dataLen);
  434. }
  435. pReport->buffer[PROTOCAL_LENLOW_INDEX] = (uint8_t)len;
  436. pReport->buffer[PROTOCAL_LENHIGH_INDEX] = (uint8_t)(len>>8);
  437. len = CalculateCRC16(pReport->buffer, pReport->length);
  438. pReport->buffer[pReport->length++] = len;
  439. pReport->buffer[pReport->length++] = len>>8;
  440. SendRangeReport();
  441. pReport->state = 0;
  442. rangeReport.state = 0;
  443. }
  444. uint32_t GetCurrentBufferLength()
  445. {
  446. stReportBuffer* pReport = rangeReport.reportBuffers + rangeReport.index;
  447. return pReport->length;
  448. }
  449. void ReportResetSignPeriodically()
  450. {
  451. uint8_t buf[MAX_MESSAGE_LEN];
  452. uint32_t bufLen = 0;
  453. BuildResponse(CFG_COMMAND_RESETSIGN, buf, &bufLen);
  454. SendMessage(buf, bufLen);
  455. SendMessageDbg(buf, bufLen);
  456. SendMessageWifi(buf, bufLen);
  457. }
  458. void ReportRangePeriodically(uint32_t flag)
  459. {
  460. uint32_t tick;
  461. do
  462. {
  463. tick = HAL_GetTick();
  464. if(0 == rangeReport.state)
  465. {
  466. CreateRangeReportHead();
  467. rangeReport.time = tick;
  468. }
  469. else
  470. {
  471. UpdateAnchorStatus();
  472. }
  473. if(rangeReport.time > tick)
  474. {
  475. rangeReport.time = tick;
  476. }
  477. if(flag)
  478. {
  479. AddRange2Report();
  480. }
  481. if(REPORT_THRESHOLD <= GetCurrentBufferLength()||
  482. REPORT_PEROID <= (tick - rangeReport.time))
  483. {
  484. FinishRangeReport();
  485. rangeReport.index++;
  486. rangeReport.index &= REPORTBUFFER_MASK;
  487. }
  488. flag = 0;
  489. }
  490. while(0 == rangeReport.state);
  491. }
  492. void ReportRangeImmediately(uint32_t flag)
  493. {
  494. CreateRangeReportHead();
  495. AddRange2Report();
  496. FinishRangeReport();
  497. rangeReport.index++;
  498. rangeReport.index &= REPORTBUFFER_MASK;
  499. }
  500. void ReportRangeResult(int rx)
  501. {
  502. int l = 0, r= 0, aaddr=0, taddr=0, n =0;
  503. uint32_t rangeTime=0;
  504. uint16_t txa, rxa, valid=1;
  505. //send the new range information to LCD and/or USB
  506. aaddr = instNewRangeAncAdd() & 0xff;
  507. taddr = instNewRangeTagAdd();
  508. rangeTime = instNewRangeTim() & 0xffffffff;
  509. l = instGetLCount() & 0xFFFF;
  510. r = instGetTagRangeNum(taddr);
  511. taddr &= 0xff;
  512. txa = instTXAntennaDly();
  513. rxa = instRXAntennaDly();
  514. valid = instValidRanges();
  515. //n = sprintf((char*)debugBuffer, "T:%d\r\n",portGetTickCnt());
  516. //ReportMessage(debugBuffer,n);
  517. n = 0;
  518. if(rx == TOF_REPORT_T2A)
  519. {
  520. n += sprintf((char*)(&debugBuffer[n]), "mc ");
  521. valid = 0;
  522. for(valid = 0,rxa = 0; valid < MAX_ANCHOR_LIST_SIZE; valid++)
  523. {
  524. if(instGetIdistAddress(valid) != BROADCAST_ADDR)//address > 0
  525. {
  526. n += sprintf((char*)(&debugBuffer[n]), "%04x %08x ",
  527. instGetIdistAddress(valid), instGetIdist_mm(valid));
  528. rxa++;
  529. if(rxa >= NUM_EXPECTED_RESPONSES)
  530. {
  531. break;
  532. }
  533. }
  534. }
  535. if(rxa < NUM_EXPECTED_RESPONSES)
  536. {
  537. for(; rxa < NUM_EXPECTED_RESPONSES; rxa++)
  538. {
  539. n += sprintf((char*)(&debugBuffer[n]), "0000 00000000 ");
  540. }
  541. }
  542. n += sprintf((char*)(&debugBuffer[n]), "%04x %02x %08x %c%d:%d T %d\r\n",
  543. l, r, rangeTime,
  544. 'a', taddr,aaddr,
  545. portGetTickCnt());
  546. ReportMessage(debugBuffer,n);
  547. SendMessage(debugBuffer,n);
  548. SendMessageDbg(debugBuffer, n);
  549. SendMessageWifi(debugBuffer,n);
  550. }
  551. instClearDistTableAll();
  552. }
  553. /********************************* END OF FILE *********************************/