GP22_ATY.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773
  1. /**
  2. * @file GP22_ATY.c
  3. *
  4. * @param Project DEVICE_GENERAL_ATY_LIB
  5. *
  6. * @author ATY
  7. *
  8. * @copyright
  9. * - Copyright 2017 - 2023 MZ-ATY
  10. * - This code follows:
  11. * - MZ-ATY Various Contents Joint Statement -
  12. * <a href="https://mengze.top/MZ-ATY_VCJS">
  13. * https://mengze.top/MZ-ATY_VCJS</a>
  14. * - CC 4.0 BY-NC-SA -
  15. * <a href="https://creativecommons.org/licenses/by-nc-sa/4.0/">
  16. * https://creativecommons.org/licenses/by-nc-sa/4.0/</a>
  17. * - Your use will be deemed to have accepted the terms of this statement.
  18. *
  19. * @brief Base functions of GP22 for all embedded device
  20. *
  21. * @version
  22. * - 1_01_220901 > ATY
  23. * -# Preliminary version, first Release
  24. * - Undone
  25. ********************************************************************************
  26. */
  27. #ifndef __GP22_ATY_C
  28. #define __GP22_ATY_C
  29. #include "GP22_ATY.h"
  30. /******************************* For user *************************************/
  31. /******************************************************************************/
  32. /* Basic **********************************************************************/
  33. /**
  34. * @brief write data with spi
  35. * @param data_t data to wirite
  36. */
  37. void GP22_WriteData(uint8_t data_t)
  38. {
  39. GP22_ENABLE;
  40. SPI_Write(&data_t, 1);
  41. GP22_DISABLE;
  42. }
  43. void GP22_WriteReg(uint8_t opcode_address, uint32_t reg_data)
  44. {
  45. uint8_t temp_uint8[4];
  46. temp_uint8[0] = reg_data >> 24;
  47. temp_uint8[1] = reg_data >> 16;
  48. temp_uint8[2] = reg_data >> 8;
  49. temp_uint8[3] = reg_data;
  50. GP22_ENABLE;
  51. SPI_Write(&opcode_address, 1);
  52. SPI_Write(temp_uint8, 4);
  53. GP22_DISABLE;
  54. }
  55. uint32_t GP22_ReadReg(uint8_t opcode_address)
  56. {
  57. uint8_t data_t[4] = {0};
  58. GP22_ENABLE;
  59. SPI_Write(&opcode_address, 1);
  60. SPI_Read(data_t, 4);
  61. GP22_DISABLE;
  62. #ifdef __DEBUG_GP22_OPCODE_ATY
  63. printf("\r\nRead opcode %02X: 0x%02X%02X%02X%02X",
  64. opcode_address, data_t[0], data_t[1], data_t[2], data_t[3]);
  65. #endif /* __DEBUG_GP22_OPCODE_ATY */
  66. return (((uint32_t)data_t[0] << 24) + ((uint32_t)data_t[1] << 16)
  67. + ((uint32_t)data_t[2] << 8) + ((uint32_t)data_t[3]));
  68. }
  69. void GP22_Reset(void)
  70. {
  71. GP22_NORESET;
  72. DelayMs(1);
  73. GP22_RESET;
  74. DelayMs(1);
  75. GP22_NORESET;
  76. DelayMs(1);
  77. GP22_WriteData(GP22_OPCODE_RESET);
  78. }
  79. // TODO: set generic
  80. uint8_t GP22_WaitInt(uint8_t step)
  81. {
  82. uint32_t errTimeCount = 0;
  83. // GP22_ENABLE;
  84. while(GP22_READ_INT_H)
  85. {
  86. errTimeCount++;
  87. if(errTimeCount > 24000) // 24000 About 36ms at 64M HCLK, 1.25MIPS/Mhz for arm
  88. {
  89. errTimeCount = 0;
  90. #ifdef __DEBUG_GP22_ATY
  91. printf("\r\nERR - INT timeout %d", step);
  92. #endif /* __DEBUG_GP22_ATY */
  93. return step;
  94. }
  95. }
  96. return 0;
  97. }
  98. uint8_t GP22_SpiTest(uint8_t data_t)
  99. {
  100. uint8_t i = 0;
  101. uint32_t temp_uint32 = 0;
  102. for(i = 0; i < 5; i++)
  103. {
  104. GP22_Reset();
  105. GP22_WriteReg(GP22_OPCODE_WRITE_REG | 0x01, ((uint32_t)data_t << 24) | 0x00123456);
  106. DelayMs(2);
  107. temp_uint32 = GP22_ReadReg(GP22_OPCODE_READ_REG | GP22_REGADDR_TEST);
  108. #ifdef __DEBUG_GP22_ATY
  109. printf("\r\nSPIT - 0x%08X", temp_uint32);
  110. #endif /* __DEBUG_GP22_ATY */
  111. if((temp_uint32 >> 24) == data_t)
  112. return 0;
  113. }
  114. #ifdef __DEBUG_GP22_ATY
  115. printf("\r\nERR - SPI Commucate Err!");
  116. #endif /* __DEBUG_GP22_ATY */
  117. return 1;
  118. }
  119. uint8_t GP22_ReadId(void)
  120. {
  121. uint8_t temp_uint8 = GP22_OPCODE_READ_ID;
  122. uint8_t id[8] = {0};
  123. GP22_ENABLE;
  124. SPI_Write(&temp_uint8, 1);
  125. SPI_Read(id, 8);
  126. GP22_DISABLE;
  127. #ifdef __DEBUG_GP22_ATY
  128. printf("\r\nRID - Read id: 0x%02X%02X%02X%02X%02X%02X%02X%02X",
  129. id[0], id[1], id[2], id[3], id[4], id[5], id[6], id[7]);
  130. // printf("\r\nRID - Save id: 0x%02X%02X%02X%02X%02X%02X%02X%02X",
  131. // GP22_Ids[0], GP22_Ids[1], GP22_Ids[2], GP22_Ids[3], GP22_Ids[4],
  132. // GP22_Ids[5], GP22_Ids[6], GP22_Ids[7]);
  133. #endif /* __DEBUG_GP22_ATY */
  134. if((id[0] == GP22_Ids[0]) && (id[1] == GP22_Ids[1]) && (id[2] == GP22_Ids[2])
  135. && (id[3] == GP22_Ids[3]) && (id[4] == GP22_Ids[4]) && (id[5] == GP22_Ids[5])
  136. && (id[6] == GP22_Ids[6]))
  137. {
  138. return 0;
  139. }
  140. else
  141. {
  142. #ifdef __DEBUG_GP22_ATY
  143. printf("\r\nERR - Read wrong id number!");
  144. #endif /* __DEBUG_GP22_ATY */
  145. return 1;
  146. }
  147. }
  148. uint16_t GP22_AnalyseErrCode(void)
  149. {
  150. uint16_t statusCode = 0;
  151. statusCode = GP22_ReadReg(GP22_OPCODE_READ_REG | GP22_REGADDR_STATUS) >> 16;
  152. #ifdef __DEBUG_GP22_ATY
  153. printf("\r\nSTAT - Code: 0x%04X | ALU - %d | HIT1 - %d | HIT2 - %d",
  154. statusCode,
  155. statusCode & 0x0007,
  156. (statusCode >> 3) & 0x0007,
  157. (statusCode >> 6) & 0x0007);
  158. //Bit9: Timeout_TDC
  159. if((statusCode & 0x0200) == 0x0200)
  160. printf("\r\nERR - Indicates an overflow of the TDC unit");
  161. //Bit10: Timeout_Precounter
  162. if((statusCode & 0x0400) == 0x0400)
  163. printf("\r\nERR - Indicates an overflow of the 14 bit precounter in MR 2");
  164. //Bit11: Error_open
  165. if((statusCode & 0x0800) == 0x0800)
  166. printf("\r\nERR - Indicates an open sensor at temperature measurement");
  167. //Bit12: Error_short
  168. if((statusCode & 0x1000) == 0x1000)
  169. printf("\r\nERR - Indicates a shorted sensor at temperature measurement");
  170. //Bit13: EEPROM_eq_CREG
  171. if((statusCode & 0x2000) != 0x2000)
  172. printf("\r\nERR - The content of the configuration registers not equals the EEPROM");
  173. //Bit14: EEPROM_DED
  174. if((statusCode & 0x4000) == 0x4000)
  175. printf("\r\nERR - Double error detection. A multiple error has been detected whcich can not be corrected");
  176. //Bit15: EEPROM_Error
  177. if((statusCode & 0x8000) == 0x8000)
  178. printf("\r\nERR - Single error in EEPROM which has been corrected");
  179. #endif /* __DEBUG_GP22_ATY */
  180. return statusCode;
  181. }
  182. // TODO: E2 tested is not reality, state changed with power time
  183. uint8_t GP22_CompareE2(void)
  184. {
  185. uint8_t errCount = 5;
  186. uint16_t statusCode = 0x0000;
  187. while(errCount != 0)
  188. {
  189. errCount--;
  190. GP22_WriteData(GP22_OPCODE_COMPARE_E2);
  191. statusCode = GP22_ReadReg(GP22_OPCODE_READ_REG | GP22_REGADDR_STATUS);
  192. if((statusCode & 0x2000) != 0x2000)
  193. {
  194. GP22_WriteData(GP22_OPCODE_WRITE_E2);
  195. DelayMs(100);
  196. if(GP22_WaitInt(GP22_STEP_CALC_HSC) != 0)
  197. {
  198. #ifdef __DEBUG_GP22_ATY
  199. printf("\r\nERR - Write E2 timeout");
  200. #endif /* __DEBUG_GP22_ATY */
  201. return 1;
  202. }
  203. }
  204. else
  205. {
  206. #ifdef __DEBUG_GP22_ATY
  207. printf("\r\nCE2 - Compare E2 success");
  208. #endif /* __DEBUG_GP22_ATY */
  209. return 0;
  210. }
  211. }
  212. #ifdef __DEBUG_GP22_ATY
  213. printf("\r\nERR - Compare E2 failed");
  214. #endif /* __DEBUG_GP22_ATY */
  215. return 1;
  216. }
  217. uint8_t GP22_CalcHSC(void)
  218. {
  219. uint8_t errCount = 5;
  220. uint32_t hscReadValue;
  221. float correctionFactor;
  222. while(errCount != 0)
  223. {
  224. errCount--;
  225. GP22_WriteData(GP22_OPCODE_INIT);
  226. // EN_START need to set high
  227. GP22_WriteData(GP22_OPCODE_CALC_HSC);
  228. if(GP22_WaitInt(GP22_STEP_START_TEMP) != 0) continue;
  229. hscReadValue = GP22_ReadReg(GP22_OPCODE_READ_REG | 0x00);
  230. // CLKHS periods * 1000 / 32.768 us
  231. // 0: 2 periods = 61.03515625us
  232. // 1: 4 periods = 122.0703125us
  233. // 2: 8 periods = 244.140625us
  234. // 3: 16 periods = 488.28125us
  235. correctionFactor = ((1 << (GP22_CLKHS_PERIOD + 1)) * 1000 / 32.768)
  236. / (hscReadValue / 65536) * GP22_CLKHS_FREQ;
  237. GP22_CALC_CLKHS_FREQ =
  238. correctionFactor * GP22_CLKHS_FREQ;
  239. #ifdef __DEBUG_GP22_ATY
  240. printf("\r\nCHSC - Theoretical value: 0x%08X - %1.6f",
  241. hscReadValue, (float)hscReadValue / 65536);
  242. printf("\r\nCHSC - Correction factor for clock: %.6f", correctionFactor);
  243. printf("\r\nCHSC - Calibrated internal clock frequency: %.6fMHz - %.6fns",
  244. GP22_CALC_CLKHS_FREQ, 1000 / GP22_CALC_CLKHS_FREQ);
  245. #endif /* __DEBUG_GP22_ATY */
  246. return 0;
  247. }
  248. return 1;
  249. }
  250. uint8_t GP22_StartTemp(void)
  251. {
  252. uint8_t errCount = 5;
  253. uint32_t temp_uint32[4] = {0};
  254. float tempRefResFactor = 0.0;
  255. float tempRealRes[2] = {0.0};
  256. while(errCount != 0)
  257. {
  258. errCount--;
  259. GP22_WriteData(GP22_OPCODE_INIT);
  260. GP22_WriteData(GP22_OPCODE_START_TEMP);
  261. if(GP22_WaitInt(GP22_STEP_START_TEMP) != 0) continue;
  262. // GP22_AnalyseErrCode();
  263. temp_uint32[0] = GP22_ReadReg(GP22_OPCODE_READ_REG | 0x00);
  264. temp_uint32[1] = GP22_ReadReg(GP22_OPCODE_READ_REG | 0x01);
  265. temp_uint32[2] = GP22_ReadReg(GP22_OPCODE_READ_REG | 0x02);
  266. temp_uint32[3] = GP22_ReadReg(GP22_OPCODE_READ_REG | 0x03);
  267. tempRefResFactor =
  268. (float)(temp_uint32[2] + temp_uint32[3]) / 2
  269. / GP22_CALC_CLKHS_FREQ / GP22_TEMP_REF_RES;
  270. // tempRealRes[0] = (float)temp_uint32[0] / GP22_CALC_CLKHS_FREQ / tempRefRes;
  271. // tempRealRes[0] =
  272. // (GP22_TEMP_REF_RES * tempRealRes[0]) / (GP22_TEMP_REF_RES - tempRealRes[0]);
  273. // tempRealRes[1] = (float)temp_uint32[1] / GP22_CALC_CLKHS_FREQ / tempRefRes;
  274. // tempRealRes[1] =
  275. // (GP22_TEMP_REF_RES * tempRealRes[1]) / (GP22_TEMP_REF_RES - tempRealRes[1]);
  276. // tempRealValue[1] = ALGO_ResToKelvinTemp(tempRealRes[1], 10, 3937);
  277. tempRealRes[0] = (float)temp_uint32[0] / GP22_CALC_CLKHS_FREQ / tempRefResFactor;
  278. tempRealValue[0] = ALGO_ResToKelvinTemp(tempRealRes[0], 10, 4100);
  279. tempRealRes[1] = (float)temp_uint32[1] / GP22_CALC_CLKHS_FREQ / tempRefResFactor;
  280. tempRealValue[1] = ALGO_ResToKelvinTemp(tempRealRes[1], 10, 3950);
  281. #ifdef __DEBUG_GP22_ATY
  282. printf("\r\nSTEMP - Temp reg: 0x%08X - 0x%08X - 0x%08X - 0x%08X",
  283. temp_uint32[0], temp_uint32[1], temp_uint32[2], temp_uint32[3]);
  284. printf("\r\nSTEMP - Temp reg: %d - %d - %d - %d",
  285. temp_uint32[0], temp_uint32[1], temp_uint32[2], temp_uint32[3]);
  286. printf("\r\nSTEMP - PT1 res: %f KOhm - %f C", tempRealRes[0], tempRealValue[0]);
  287. printf("\r\nSTEMP - PT2 res: %f KOhm - %f C", tempRealRes[1], tempRealValue[1]);
  288. #endif /* __DEBUG_GP22_ATY */
  289. return 0;
  290. }
  291. return 1;
  292. }
  293. /* Use ************************************************************************/
  294. float GP22_CALC_CLKHS_FREQ = GP22_CLKHS_FREQ; // MHz
  295. uint8_t GP22_Ids[8] = {0xF0, 0xF1, 0xF2, 0xF3, 0xF4, 0xF5, 0xF6};
  296. uint32_t GP22_REG[7] = {0};
  297. uint32_t tofValue[4] = {0};
  298. float tempRealValue[2] = {0.0};
  299. float echoTimeDetect[2] = {0};
  300. float echoTimeCalc[2] = {0};
  301. float speedWave[2] = {0};
  302. uint32_t PW1ST_ValueA = 0;
  303. float machineDelayTime = 0;
  304. float usDistance = 0;
  305. uint8_t pulseNum = 4;
  306. uint8_t firstWaveEnable = 1;
  307. uint16_t ignoreTime = 1200;
  308. uint16_t offsetValue = 0;
  309. // #define GP22_MEASURE_US_ANALOG
  310. // #define GP22_MEASURE_US_DIGITAL
  311. #define GP22_MEASURE_UsWave1MHz
  312. // #define GP22_MEASURE_UsWave1MHz
  313. /******************************************************************************/
  314. #if defined(GP22_MEASURE_UsWave1MHz)
  315. void GP22_RegInit_UsWave1MHz(void)
  316. {
  317. // 630BE800
  318. GP22_REG[0] = GP22_CFG_KEEP_DEFAULT0
  319. | GP22_CFG0_ANZ_FIRE_0 * 4 // 3-6 pulse
  320. | GP22_CFG0_DIV_FIRE_0 * 3 // 0CLK | 3FIRE 1MHz pulse
  321. | GP22_CFG0_ANZ_PER_CALRES_0 * GP22_CLKHS_PERIOD
  322. | GP22_CFG0_DIV_CLKHS_0 * GP22_CLKHS_DIV
  323. | GP22_CFG0_START_CLKHS_0 * 1
  324. | GP22_CFG0_CALIBRATE * 1
  325. // | GP22_CFG0_NO_CAL_AUTO * 1
  326. | GP22_CFG0_MESSB2 * 1
  327. | GP22_CFG0_ANZ_PORT * 1
  328. | GP22_CFG0_TCYCLE * 1
  329. | GP22_CFG0_ANZ_FAKE * 1
  330. | GP22_CFG0_SEL_ECLK_TEMP * 1
  331. | GP22_Ids[0];
  332. // 194913F1
  333. GP22_REG[1] = GP22_CFG_KEEP_DEFAULT1
  334. | GP22_CFG1_HIT2_0 * 2
  335. | GP22_CFG1_HIT1_0 * 1
  336. | GP22_CFG1_HITIN2_0 * 0
  337. | GP22_CFG1_HITIN1_0 * pulseNum
  338. | GP22_CFG1_SEL_START_FIRE * 1
  339. | GP22_CFG1_SEL_TSTO2_0 * 2
  340. | GP22_CFG1_SEL_TSTO1_0 * 6
  341. | GP22_Ids[1];
  342. // A00000F2
  343. GP22_REG[2] = GP22_CFG_KEEP_DEFAULT2
  344. | GP22_CFG2_EN_INT_TDC_TIMEOUT * 1
  345. | GP22_CFG2_EN_INT_HITS * 1
  346. | GP22_CFG2_EN_INT_ALU * 1
  347. // | GP22_CFG2_RFEDGE2 * 0 // 0: rising or falling, 1: and
  348. | GP22_CFG2_RFEDGE1 * 0 // 0: rising or falling, 1: and
  349. | GP22_CFG2_DELVAL1_0 * ignoreTime / (1 << GP22_CLKHS_DIV)
  350. | GP22_Ids[2];
  351. // 000000F3
  352. GP22_REG[3] = GP22_CFG_KEEP_DEFAULT3
  353. | GP22_CFG3FW_EN_AUTOCALC_MB2 * 1
  354. | GP22_CFG3FW_EN_ERR_VAL * 1
  355. | GP22_CFG3FW_EN_FIRST_WAVE * firstWaveEnable
  356. | GP22_CFG3FW_SEL_TIMO_MB2_0 * 3
  357. | GP22_CFG3FW_DELREL3_0 * 5
  358. | GP22_CFG3FW_DELREL2_0 * 4
  359. | GP22_CFG3FW_DELREL1_0 * 3
  360. | GP22_Ids[3];
  361. // 200000F4
  362. GP22_REG[4] = GP22_CFG_KEEP_DEFAULT4
  363. | GP22_CFG4FW_DIS_PW * 0 // 1: disable
  364. | GP22_CFG4FW_OFFSRNG1 * (((uint16_t)offsetValue % 10) & 0x01)
  365. | GP22_CFG4FW_OFFSRNG2 * (((uint16_t)offsetValue % 10) & 0x02)
  366. | GP22_CFG4FW_EDGE_FW * (((uint16_t)offsetValue % 10) & 0x04)
  367. | GP22_CFG4FW_OFFS_0 * ((uint16_t)offsetValue / 10)
  368. // above 4 for first wave mode
  369. | GP22_Ids[4];
  370. // 100000F5
  371. GP22_REG[5] = GP22_CFG_KEEP_DEFAULT5
  372. | GP22_CFG5_CON_FIRE_DOWN * 1
  373. | GP22_CFG5_EN_STARTNOISE * 1
  374. | GP22_CFG5_DIS_PHASESHIFT * 1
  375. | GP22_Ids[5];
  376. // 002000F6
  377. GP22_REG[6] = GP22_CFG_KEEP_DEFAULT6
  378. | GP22_CFG6_EN_ANALOG * 1
  379. | GP22_CFG6_EN_INT_END * 1
  380. // | GP22_CFG6_TW2_0 * 3
  381. | GP22_CFG6_QUAD_RES * 1
  382. | GP22_CFG6_DA_KORR_0 * 8
  383. | GP22_CFG6_NEG_STOP_TEMP * 1 // Must set handly
  384. // | GP22_CFG6_FIREO_DEF * 1
  385. // | GP22_CFG6_TEMP_PORTDIR * 1
  386. | GP22_Ids[6];
  387. GP22_WriteReg(0x80, GP22_REG[0]);
  388. GP22_WriteReg(0x81, GP22_REG[1]);
  389. GP22_WriteReg(0x82, GP22_REG[2]);
  390. GP22_WriteReg(0x83, GP22_REG[3]);
  391. GP22_WriteReg(0x84, GP22_REG[4]);
  392. GP22_WriteReg(0x85, GP22_REG[5]);
  393. GP22_WriteReg(0x86, GP22_REG[6]);
  394. #ifdef __DEBUG_GP22_ATY
  395. printf("\r\nREGI - 0: %08X\r\nREGI - 1: %08X\r\nREGI - 2: %08X\
  396. \r\nREGI - 3: %08X\r\nREGI - 4: %08X\r\nREGI - 5: %08X\r\nREGI - 6: %08X",
  397. GP22_REG[0], GP22_REG[1], GP22_REG[2],
  398. GP22_REG[3], GP22_REG[4], GP22_REG[5], GP22_REG[6]);
  399. #endif /* __DEBUG_GP22_ATY */
  400. }
  401. uint8_t GP22_StartTof_UsWave1MHz(void)
  402. {
  403. float wavePeriod = 0.0;
  404. float waveFreq = 0.0;
  405. GP22_WriteData(GP22_OPCODE_INIT);
  406. GP22_WriteData(GP22_OPCODE_START_TOF);
  407. if(GP22_WaitInt(GP22_STEP_START_TOF) != 0) return 1;
  408. // GP22_AnalyseErrCode();
  409. tofValue[0] = GP22_ReadReg(GP22_OPCODE_READ_REG | 0x00);
  410. tofValue[1] = GP22_ReadReg(GP22_OPCODE_READ_REG | 0x01);
  411. tofValue[2] = GP22_ReadReg(GP22_OPCODE_READ_REG | 0x02);
  412. tofValue[3] = GP22_ReadReg(GP22_OPCODE_READ_REG | 0x03);
  413. if((GP22_REG[3] & GP22_CFG3FW_EN_FIRST_WAVE) != 0)
  414. PW1ST_ValueA = GP22_ReadReg(GP22_OPCODE_READ_REG | 0x08);
  415. wavePeriod =
  416. (float)((tofValue[2] - tofValue[1]) + (tofValue[1] - tofValue[0])) / 2
  417. / GP22_CALC_CLKHS_FREQ / 65536; // ns
  418. waveFreq = 1 / wavePeriod; // MHz
  419. echoTimeDetect[0] = ((float)tofValue[0] / GP22_CALC_CLKHS_FREQ / 65536);
  420. // - (wavePeriod * 2);
  421. if(echoTimeDetect[0] <= 0)
  422. echoTimeDetect[0] = machineDelayTime + 1;
  423. echoTimeCalc[0] = echoTimeDetect[0] - machineDelayTime;
  424. speedWave[0] = usDistance * 1000.0 / echoTimeCalc[0];
  425. #ifdef __DEBUG_GP22_ATY
  426. if((GP22_REG[3] & GP22_CFG3FW_EN_FIRST_WAVE) != 0)
  427. printf("\r\nSTOF - PW1ST value: 0x%02X - %f",
  428. PW1ST_ValueA, (float)(PW1ST_ValueA >> 24) / 128);
  429. printf("\r\nSTOF - TOF reg: 0x%08X - 0x%08X - 0x%08X - 0x%08X",
  430. tofValue[0], tofValue[1], tofValue[2], tofValue[3]);
  431. printf("\r\nSTOF - TOF reg: %d - %d - %d - %d",
  432. tofValue[0] / 65536, tofValue[1] / 65536,
  433. tofValue[2] / 65536, tofValue[3] / 65536);
  434. printf("\r\nSTOF - TOF reg: %f - %f - %f - %f",
  435. (float)tofValue[0] / GP22_CALC_CLKHS_FREQ / 65536,
  436. (float)tofValue[1] / GP22_CALC_CLKHS_FREQ / 65536,
  437. (float)tofValue[2] / GP22_CALC_CLKHS_FREQ / 65536,
  438. (float)tofValue[3] / GP22_CALC_CLKHS_FREQ / 65536);
  439. printf("\r\nSTOF - Wave: %f us - %f MHz", wavePeriod, waveFreq);
  440. printf("\r\nSTOF - Time: %f us - %f m/s", echoTimeCalc[0], speedWave[0]);
  441. printf("\r\nStep %02d-A done! ----------------------------------------------",
  442. GP22_STEP_START_TOF);
  443. #endif /* __DEBUG_GP22_ATY */
  444. return 0;
  445. }
  446. uint8_t GP22_Process_UsWave1MHz(uint8_t cmdCode)
  447. {
  448. // cmdCode = 0xFF;
  449. uint8_t errCode = 0;
  450. if(cmdCode & GP22_STEP_RESET)
  451. GP22_Reset();
  452. if(cmdCode & GP22_STEP_SPI_TEST)
  453. if(GP22_SpiTest(0x68)) return GP22_STEP_SPI_TEST;
  454. if(cmdCode & GP22_STEP_REG_INIT
  455. || cmdCode & GP22_STEP_READ_ID)
  456. {
  457. GP22_RegInit_UsWave1MHz();
  458. if(GP22_ReadId()) return GP22_STEP_READ_ID;
  459. }
  460. if(cmdCode & GP22_STEP_COMPARE_E2)
  461. if(GP22_CompareE2()) return GP22_STEP_COMPARE_E2;
  462. if(cmdCode & GP22_STEP_CALC_HSC)
  463. if(GP22_CalcHSC()) return GP22_STEP_CALC_HSC;
  464. if(cmdCode & GP22_STEP_START_TEMP)
  465. if(GP22_StartTemp()) errCode = GP22_STEP_START_TEMP;
  466. if(cmdCode & GP22_STEP_START_TOF)
  467. if(GP22_StartTof_UsWave1MHz()) errCode = GP22_STEP_START_TOF;
  468. return errCode;
  469. }
  470. uint8_t GP22_Process_Run(uint8_t cmdCode)
  471. {
  472. uint8_t errCode = 0;
  473. #ifdef __DEBUG_GP22_ATY
  474. printf("\r\nStart");
  475. #endif /* __DEBUG_GP22_ATY */
  476. GP22_WAKE;
  477. errCode = GP22_Process_UsWave1MHz(cmdCode);
  478. // GP22_AnalyseErrCode();
  479. #ifdef __DEBUG_GP22_ATY
  480. printf("\r\nStep %02d done! ------------------------------------------------", errCode);
  481. printf("\r\nOver!");
  482. #endif /* __DEBUG_GP22_ATY */
  483. return errCode;
  484. }
  485. #elif defined(GP22_MEASURE_US_DIGITAL)
  486. void GP22_RegInitUs_Digital(void)
  487. {
  488. GP22_REG[0] = GP22_CFG_KEEP_DEFAULT0
  489. | GP22_CFG0_ANZ_FIRE_0 * 6
  490. // | GP22_CFG0_ANZ_FIRE_0 * 14
  491. | GP22_CFG0_DIV_FIRE_0 * 11
  492. | GP22_CFG0_ANZ_PER_CALRES_0 * 3
  493. | GP22_CFG0_DIV_CLKHS_0 * 2
  494. | GP22_CFG0_START_CLKHS_0 * 1
  495. | GP22_CFG0_CALIBRATE * 1
  496. | GP22_CFG0_MESSB2 * 1
  497. | GP22_Ids[0];
  498. GP22_REG[1] = GP22_CFG_KEEP_DEFAULT1
  499. | GP22_CFG1_HIT2_0 * 2
  500. | GP22_CFG1_HIT1_0 * 1
  501. | GP22_CFG1_HITIN2_0 * 0
  502. | GP22_CFG1_HITIN1_0 * 4
  503. | GP22_CFG1_SEL_START_FIRE * 1
  504. | GP22_CFG1_SEL_TSTO2_0 * 2
  505. | GP22_CFG1_SEL_TSTO1_0 * 1
  506. | GP22_Ids[1];
  507. GP22_REG[2] = GP22_CFG_KEEP_DEFAULT2
  508. | GP22_CFG2_EN_INT_TDC_TIMEOUT * 1
  509. | GP22_CFG2_EN_INT_HITS * 1
  510. | GP22_CFG2_EN_INT_ALU * 1
  511. | GP22_CFG2_RFEDGE1 * 1
  512. | GP22_Ids[2];
  513. GP22_REG[3] = GP22_CFG_KEEP_DEFAULT3
  514. | GP22_CFG3FW_EN_AUTOCALC_MB2 * 1
  515. // | GP22_CFG3FW_EN_FIRST_WAVE * 1
  516. | GP22_CFG3FW_EN_ERR_VAL * 1
  517. | GP22_CFG3FW_SEL_TIMO_MB2_0 * 3
  518. | GP22_CFG3FW_DELREL3_0 * 5
  519. | GP22_CFG3FW_DELREL2_0 * 4
  520. | GP22_CFG3FW_DELREL1_0 * 3
  521. | GP22_Ids[3];
  522. GP22_REG[4] = GP22_CFG_KEEP_DEFAULT4
  523. // | GP22_CFG4FW_DIS_PW * 1
  524. // | GP22_CFG4FW_OFFSRNG2 * 1
  525. // | GP22_CFG4FW_OFFSRNG1 * 1
  526. // | GP22_CFG4FW_EDGE_FW * 1
  527. // | GP22_CFG4FW_OFFS_0 * 15
  528. // | GP22_CFG4FW_OFFS_0 * 16
  529. | GP22_Ids[4];
  530. GP22_REG[5] = GP22_CFG_KEEP_DEFAULT5
  531. | GP22_CFG5_CON_FIRE_0 * 1
  532. | GP22_CFG5_EN_STARTNOISE * 1
  533. | GP22_CFG5_PHFIRE_0 * 0x5555
  534. | GP22_Ids[5];
  535. GP22_REG[6] = GP22_CFG_KEEP_DEFAULT6
  536. // | GP22_CFG6_NEG_STOP_TEMP * 1
  537. // | GP22_CFG6_DOUBLE_RES * 1
  538. | GP22_CFG6_QUAD_RES * 1
  539. // | GP22_CFG6_EN_ANALOG * 1
  540. // | GP22_CFG6_TW2_0 * 3
  541. | GP22_CFG6_EN_INT_END * 1
  542. // | GP22_CFG6_FIREO_DEF * 0
  543. | GP22_CFG6_ANZ_FIRE_END_0 * 0
  544. | GP22_Ids[6];
  545. #ifdef __DEBUG_GP22_ATY
  546. printf("\r\nREGI - 0: %08X\r\nREGI - 1: %08X\r\nREGI - 2: %08X\
  547. \r\nREGI - 3: %08X\r\nREGI - 4: %08X\r\nREGI - 5: %08X\r\nREGI - 6: %08X",
  548. GP22_REG[0], GP22_REG[1], GP22_REG[2],
  549. GP22_REG[3], GP22_REG[4], GP22_REG[5], GP22_REG[6]);
  550. #endif /* __DEBUG_GP22_ATY */
  551. GP22_WriteReg(0x80, GP22_REG[0]);
  552. GP22_WriteReg(0x81, GP22_REG[1]);
  553. GP22_WriteReg(0x82, GP22_REG[2]);
  554. GP22_WriteReg(0x83, GP22_REG[3]);
  555. GP22_WriteReg(0x84, GP22_REG[4]);
  556. GP22_WriteReg(0x85, GP22_REG[5]);
  557. GP22_WriteReg(0x86, GP22_REG[6]);
  558. }
  559. uint8_t gp22StartTofFlag = 0;
  560. uint32_t tofValue[4] = {0};
  561. uint8_t GP22_StartTofUs_Digital(void)
  562. {
  563. uint32_t tofReadValue[4] = {0};
  564. uint32_t PW1ST_Value = 0;
  565. gp22StartTofFlag = 1;
  566. GP22_WriteData(GP22_OPCODE_INIT);
  567. GP22_WriteData(GP22_OPCODE_START_TOF);
  568. // GP22_WriteData(GP22_OPCODE_START_TOF2);
  569. if(GP22_WaitInt(GP22_STEP_START_TOF) != 0) return 1;
  570. gp22StartTofFlag = 0;
  571. tofReadValue[0] = GP22_ReadReg(GP22_OPCODE_READ_REG | 0x00);
  572. tofReadValue[1] = GP22_ReadReg(GP22_OPCODE_READ_REG | 0x01);
  573. tofReadValue[2] = GP22_ReadReg(GP22_OPCODE_READ_REG | 0x02);
  574. tofReadValue[3] = GP22_ReadReg(GP22_OPCODE_READ_REG | 0x03);
  575. tofValue[0] = tofReadValue[0];
  576. tofValue[1] = tofReadValue[1];
  577. tofValue[2] = tofReadValue[2];
  578. tofValue[3] = tofReadValue[3];
  579. PW1ST_Value = GP22_ReadReg(GP22_OPCODE_READ_REG | 0x08);
  580. PW1ST_Value = PW1ST_Value * 1;
  581. #ifdef __DEBUG_GP22_ATY
  582. printf("\r\nSTOF - TOF reg: 0x%08X - 0x%08X - 0x%08X - 0x%08X",
  583. tofReadValue[0], tofReadValue[1], tofReadValue[2], tofReadValue[3]);
  584. printf("\r\nSTOF - TOF reg: %d - %d - %d - %d",
  585. tofReadValue[0], tofReadValue[1], tofReadValue[2], tofReadValue[3]);
  586. printf("\r\nSTOF - TOF reg: %.2f - %.2f - %.2f - %.2f",
  587. (float)tofReadValue[0] / 65536, (float)tofReadValue[1] / 65536,
  588. (float)tofReadValue[2] / 65536, (float)tofReadValue[3] / 65536);
  589. printf("\r\nSTOF - PW1ST value: 0x%02X - %f",
  590. PW1ST_Value, (float)(PW1ST_Value >> 24) / 128);
  591. #endif /* __DEBUG_GP22_ATY */
  592. return 0;
  593. }
  594. // Mode 1
  595. /* Laser distance calc ***********************************************************/
  596. #elif defined(GP22_MEASURE_UsWave1MHz1)
  597. // ---------------------------------------------------
  598. // T_ref
  599. // Time_Value = ----------- * measured_RAW_Value
  600. // Cal2-Cal1
  601. // 3*10^8 m/s light speed(0.3m/ns)
  602. // ---------------------------------------------------
  603. uint8_t GP22_StartTofLaserCal(void)
  604. {
  605. uint32_t tofValue = 0, calValue = 0;
  606. GP22_WriteReg(0x80, GP22_REG[0]
  607. | GP22_CFG0_CALIBRATE * 1);
  608. GP22_WriteReg(0x82, GP22_REG[2]
  609. & (~GP22_CFG2_EN_INT_ALU)
  610. & (~GP22_CFG2_EN_INT_TDC_TIMEOUT)
  611. | (GP22_CFG2_EN_INT_HITS * 1));
  612. GP22_WriteData(GP22_OPCODE_INIT);
  613. GP22_WriteData(GP22_OPCODE_START_CAL_TOF);
  614. if(GP22_WaitInt(GP22_STEP_START_TOF) != 0) return 1;
  615. #ifdef __DEBUG_GP22_ATY
  616. printf("\r\nTOF - Laser: Cal over");
  617. #endif /* __DEBUG_GP22_ATY */
  618. GP22_WriteReg(0x82, GP22_REG[2]
  619. & (~GP22_CFG2_EN_INT_HITS)
  620. | (GP22_CFG2_EN_INT_ALU * 1)
  621. | (GP22_CFG2_EN_INT_TDC_TIMEOUT * 1));
  622. GP22_WriteData(GP22_OPCODE_INIT);
  623. GP22_SIGNAL_L;
  624. GP22_SIGNAL_H;
  625. GP22_SIGNAL_L;
  626. // for(uint8_t i = 0; i < 2; i++){}
  627. GP22_SIGNAL_A_L;
  628. GP22_SIGNAL_A_H;
  629. GP22_SIGNAL_A_L;
  630. if(GP22_WaitInt(GP22_STEP_START_TOF) != 0) return 1;
  631. tofValue = GP22_ReadReg(GP22_OPCODE_READ_REG | 0x00);
  632. #ifdef __DEBUG_GP22_ATY
  633. printf("\r\nTOF - Laser: 0x%08X - %d", tofValue, tofValue);
  634. #endif /* __DEBUG_GP22_ATY */
  635. GP22_WriteReg(0x81, GP22_REG[1]
  636. & (~GP22_CFG1_HIT2_0)
  637. & (~GP22_CFG1_HIT2_1)
  638. & (~GP22_CFG1_HIT2_2)
  639. & (~GP22_CFG1_HIT2_3)
  640. & (~GP22_CFG1_HIT1_0)
  641. & (~GP22_CFG1_HIT1_1)
  642. & (~GP22_CFG1_HIT1_2)
  643. & (~GP22_CFG1_HIT1_3)
  644. | GP22_CFG1_HIT2_0 * 6
  645. | GP22_CFG1_HIT1_0 * 7); // Cal1 - Cal2, EN Stop2/Stop1 1 hit
  646. calValue = GP22_ReadReg(GP22_OPCODE_READ_REG | 0x01);
  647. #ifdef __DEBUG_GP22_ATY
  648. printf("\r\nTOF - Laser: 0x%08X - %d", calValue, calValue / 65536);
  649. printf("\r\nTOF - Laser: %f ns - %f m",
  650. (float)tofValue / ((float)calValue / 65536) * 16, // TODO: Time calculate wrong
  651. (float)tofValue / ((float)calValue / 65536) * 16 * 0.23);
  652. #endif /* __DEBUG_GP22_ATY */
  653. return 0;
  654. }
  655. void GP22_Process(uint32_t* gp22ProcessValue)
  656. {
  657. if(!GP22_Step(GP22_STEP_RESET)
  658. && !GP22_Step(GP22_STEP_SPI_TEST)
  659. && !GP22_Step(GP22_STEP_REG_INIT)
  660. && !GP22_Step(GP22_STEP_READ_ID)
  661. && !GP22_Step(GP22_STEP_COMPARE_E2)
  662. && !GP22_Step(GP22_STEP_START_TOF)
  663. // && !GP22_Step(GP22_STEP_CALC_HSC)
  664. // && !GP22_Step(GP22_STEP_START_TEMP)
  665. )
  666. {
  667. // gp22ProcessValue[0] = tofValue[0];
  668. // gp22ProcessValue[1] = tofValue[1];
  669. // gp22ProcessValue[2] = tofValue[2];
  670. // gp22ProcessValue[3] = tofValue[3];
  671. }
  672. GP22_AnalyseErrCode();
  673. #ifdef __DEBUG_GP22_ATY
  674. printf("\r\n");
  675. #endif /* __DEBUG_GP22_ATY */
  676. }
  677. #endif /* if defined*/
  678. #endif /* __GP22_ATY_C */
  679. /******************************** End Of File *********************************/