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 > 12000) // 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. GP22_CLKHS_FREQ;
  239. // correctionFactor * GP22_CLKHS_FREQ;
  240. #ifdef __DEBUG_GP22_ATY
  241. printf("\r\nCHSC - Theoretical value: 0x%08X - %1.6f",
  242. hscReadValue, (float)hscReadValue / 65536);
  243. printf("\r\nCHSC - Correction factor for clock: %.6f", correctionFactor);
  244. printf("\r\nCHSC - Calibrated internal clock frequency: %.6fMHz - %.6fns",
  245. GP22_CALC_CLKHS_FREQ, 1000 / GP22_CALC_CLKHS_FREQ);
  246. #endif /* __DEBUG_GP22_ATY */
  247. return 0;
  248. }
  249. return 1;
  250. }
  251. uint8_t GP22_StartTemp(void)
  252. {
  253. uint8_t errCount = 5;
  254. uint32_t temp_uint32[4] = {0};
  255. float tempRefResFactor = 0.0;
  256. float tempRealRes[2] = {0.0};
  257. while(errCount != 0)
  258. {
  259. errCount--;
  260. GP22_WriteData(GP22_OPCODE_INIT);
  261. GP22_WriteData(GP22_OPCODE_START_TEMP);
  262. if(GP22_WaitInt(GP22_STEP_START_TEMP) != 0) continue;
  263. // GP22_AnalyseErrCode();
  264. temp_uint32[0] = GP22_ReadReg(GP22_OPCODE_READ_REG | 0x00);
  265. temp_uint32[1] = GP22_ReadReg(GP22_OPCODE_READ_REG | 0x01);
  266. temp_uint32[2] = GP22_ReadReg(GP22_OPCODE_READ_REG | 0x02);
  267. temp_uint32[3] = GP22_ReadReg(GP22_OPCODE_READ_REG | 0x03);
  268. tempRefResFactor =
  269. (float)(temp_uint32[2] + temp_uint32[3]) / 2
  270. / GP22_CALC_CLKHS_FREQ / GP22_TEMP_REF_RES;
  271. // tempRealRes[0] = (float)temp_uint32[0] / GP22_CALC_CLKHS_FREQ / tempRefRes;
  272. // tempRealRes[0] =
  273. // (GP22_TEMP_REF_RES * tempRealRes[0]) / (GP22_TEMP_REF_RES - tempRealRes[0]);
  274. // tempRealRes[1] = (float)temp_uint32[1] / GP22_CALC_CLKHS_FREQ / tempRefRes;
  275. // tempRealRes[1] =
  276. // (GP22_TEMP_REF_RES * tempRealRes[1]) / (GP22_TEMP_REF_RES - tempRealRes[1]);
  277. // tempRealValue[1] = ALGO_ResToKelvinTemp(tempRealRes[1], 10, 3937);
  278. tempRealRes[0] = (float)temp_uint32[0] / GP22_CALC_CLKHS_FREQ / tempRefResFactor;
  279. tempRealValue[0] = ALGO_ResToKelvinTemp(tempRealRes[0], 10, 4100);
  280. tempRealRes[1] = (float)temp_uint32[1] / GP22_CALC_CLKHS_FREQ / tempRefResFactor;
  281. tempRealValue[1] = ALGO_ResToKelvinTemp(tempRealRes[1], 10, 3950);
  282. #ifdef __DEBUG_GP22_ATY
  283. printf("\r\nSTEMP - Temp reg: 0x%08X - 0x%08X - 0x%08X - 0x%08X",
  284. temp_uint32[0], temp_uint32[1], temp_uint32[2], temp_uint32[3]);
  285. printf("\r\nSTEMP - Temp reg: %d - %d - %d - %d",
  286. temp_uint32[0], temp_uint32[1], temp_uint32[2], temp_uint32[3]);
  287. printf("\r\nSTEMP - PT1 res: %f KOhm - %f C", tempRealRes[0], tempRealValue[0]);
  288. printf("\r\nSTEMP - PT2 res: %f KOhm - %f C", tempRealRes[1], tempRealValue[1]);
  289. #endif /* __DEBUG_GP22_ATY */
  290. return 0;
  291. }
  292. return 1;
  293. }
  294. /* Use ************************************************************************/
  295. float GP22_CALC_CLKHS_FREQ = GP22_CLKHS_FREQ; // MHz
  296. uint8_t GP22_Ids[8] = {0xF0, 0xF1, 0xF2, 0xF3, 0xF4, 0xF5, 0xF6};
  297. uint32_t GP22_REG[7] = {0};
  298. uint32_t tofValue[4] = {0};
  299. float tempRealValue[2] = {0.0};
  300. float echoTimeDetect[2] = {0};
  301. float echoTimeCalc[2] = {0};
  302. float speedWave[2] = {0};
  303. uint32_t PW1ST_ValueA = 0;
  304. float machineDelayTime = 0;
  305. float usDistance = 0;
  306. volatile uint8_t pulseGenNum = 3;
  307. volatile uint8_t pulseNum = 4;
  308. volatile uint8_t firstWaveEnable = 1;
  309. volatile uint16_t ignoreTime = 1200;
  310. volatile uint16_t offsetValue = 0;
  311. // #define GP22_MEASURE_US_ANALOG
  312. // #define GP22_MEASURE_US_DIGITAL
  313. #define GP22_MEASURE_UsWave1MHz
  314. // #define GP22_MEASURE_UsWave1MHz
  315. /******************************************************************************/
  316. #if defined(GP22_MEASURE_UsWave1MHz)
  317. void GP22_RegInit_UsWave1MHz(void)
  318. {
  319. GP22_REG[0] = GP22_CFG_KEEP_DEFAULT0
  320. | GP22_CFG0_ANZ_FIRE_0 * pulseGenNum // 3-6 pulse
  321. | GP22_CFG0_DIV_FIRE_0 * 1 // 0CLK | 4 Div for 1MHz pulse
  322. | GP22_CFG0_ANZ_PER_CALRES_0 * GP22_CLKHS_PERIOD
  323. | GP22_CFG0_DIV_CLKHS_0 * GP22_CLKHS_DIV
  324. | GP22_CFG0_START_CLKHS_0 * 1
  325. | GP22_CFG0_CALIBRATE * 1
  326. | GP22_CFG0_NO_CAL_AUTO * 0 // 1 to disable, enable is necessary at mode 2
  327. | GP22_CFG0_MESSB2 * 1
  328. | GP22_CFG0_ANZ_PORT * 1
  329. | GP22_CFG0_TCYCLE * 1
  330. | GP22_CFG0_ANZ_FAKE * 0
  331. | GP22_CFG0_SEL_ECLK_TEMP * 1
  332. | GP22_Ids[0];
  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_CFG1_EN_FAST_INIT * 1
  342. | GP22_Ids[1];
  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. GP22_REG[3] = GP22_CFG_KEEP_DEFAULT3
  352. | GP22_CFG3FW_EN_AUTOCALC_MB2 * 1
  353. | GP22_CFG3FW_EN_ERR_VAL * 1
  354. | GP22_CFG3FW_EN_FIRST_WAVE * firstWaveEnable
  355. | GP22_CFG3FW_SEL_TIMO_MB2_0 * 1
  356. // | GP22_CFG3FW_SEL_TIMO_MB2_0 * 0
  357. | GP22_CFG3FW_DELREL3_0 * 5
  358. | GP22_CFG3FW_DELREL2_0 * 4
  359. | GP22_CFG3FW_DELREL1_0 * 3
  360. | GP22_Ids[3];
  361. GP22_REG[4] = GP22_CFG_KEEP_DEFAULT4
  362. | GP22_CFG4FW_DIS_PW * 1 // 1: disable
  363. | GP22_CFG4FW_OFFSRNG1 * (((uint16_t)offsetValue % 10) & 0x01)
  364. | GP22_CFG4FW_OFFSRNG2 * (((uint16_t)offsetValue % 10) & 0x02)
  365. | GP22_CFG4FW_EDGE_FW * (((uint16_t)offsetValue % 10) & 0x04)
  366. | GP22_CFG4FW_OFFS_0 * ((uint16_t)offsetValue / 10)
  367. // above 4 only take effect at first wave enable mode
  368. | GP22_Ids[4];
  369. GP22_REG[5] = GP22_CFG_KEEP_DEFAULT5
  370. | GP22_CFG5_CON_FIRE_DOWN * 1
  371. | GP22_CFG5_EN_STARTNOISE * 1
  372. | GP22_CFG5_DIS_PHASESHIFT * 1
  373. | GP22_Ids[5];
  374. GP22_REG[6] = GP22_CFG_KEEP_DEFAULT6
  375. | GP22_CFG6_EN_ANALOG * 1
  376. | GP22_CFG6_EN_INT_END * 1
  377. | GP22_CFG0_START_CLKHS_0 * 1
  378. | GP22_CFG6_TW2_0 * 3
  379. | GP22_CFG6_QUAD_RES * 1
  380. // | GP22_CFG6_DOUBLE_RES * 1
  381. // | GP22_CFG6_DA_KORR_0 * 8
  382. | GP22_CFG6_NEG_STOP_TEMP * 1 // Must set handly
  383. // | GP22_CFG6_FIREO_DEF * 1
  384. // | GP22_CFG6_TEMP_PORTDIR * 1
  385. | GP22_Ids[6];
  386. GP22_WriteReg(0x80, GP22_REG[0]);
  387. GP22_WriteReg(0x81, GP22_REG[1]);
  388. GP22_WriteReg(0x82, GP22_REG[2]);
  389. GP22_WriteReg(0x83, GP22_REG[3]);
  390. GP22_WriteReg(0x84, GP22_REG[4]);
  391. GP22_WriteReg(0x85, GP22_REG[5]);
  392. GP22_WriteReg(0x86, GP22_REG[6]);
  393. #ifdef __DEBUG_GP22_ATY
  394. printf("\r\nREGI - 0: %08X\r\nREGI - 1: %08X\r\nREGI - 2: %08X\
  395. \r\nREGI - 3: %08X\r\nREGI - 4: %08X\r\nREGI - 5: %08X\r\nREGI - 6: %08X",
  396. GP22_REG[0], GP22_REG[1], GP22_REG[2],
  397. GP22_REG[3], GP22_REG[4], GP22_REG[5], GP22_REG[6]);
  398. #endif /* __DEBUG_GP22_ATY */
  399. }
  400. uint8_t GP22_StartTof_UsWave1MHz(void)
  401. {
  402. float wavePeriod = 0.0;
  403. float waveFreq = 0.0;
  404. GP22_WriteData(GP22_OPCODE_INIT);
  405. GP22_WriteData(GP22_OPCODE_START_TOF);
  406. if(GP22_WaitInt(GP22_STEP_START_TOF) != 0) return 1;
  407. // GP22_AnalyseErrCode();
  408. tofValue[0] = GP22_ReadReg(GP22_OPCODE_READ_REG | 0x00);
  409. tofValue[1] = GP22_ReadReg(GP22_OPCODE_READ_REG | 0x01);
  410. tofValue[2] = GP22_ReadReg(GP22_OPCODE_READ_REG | 0x02);
  411. tofValue[3] = GP22_ReadReg(GP22_OPCODE_READ_REG | 0x03);
  412. if((GP22_REG[3] & GP22_CFG3FW_EN_FIRST_WAVE) != 0)
  413. PW1ST_ValueA = GP22_ReadReg(GP22_OPCODE_READ_REG | 0x08);
  414. wavePeriod =
  415. (float)((tofValue[2] - tofValue[1]) + (tofValue[1] - tofValue[0])) / 2
  416. / GP22_CALC_CLKHS_FREQ / 65536; // ns
  417. waveFreq = 1 / wavePeriod; // MHz
  418. echoTimeDetect[0] = ((float)tofValue[0] / GP22_CALC_CLKHS_FREQ / 65536);
  419. // - (wavePeriod * 2);
  420. // if(echoTimeDetect[0] <= 0)
  421. // echoTimeDetect[0] = machineDelayTime + 1;
  422. echoTimeCalc[0] = echoTimeDetect[0] - machineDelayTime;
  423. speedWave[0] = usDistance * 1000.0 / echoTimeCalc[0];
  424. #ifdef __DEBUG_GP22_ATY
  425. if((GP22_REG[3] & GP22_CFG3FW_EN_FIRST_WAVE) != 0)
  426. printf("\r\nSTOF - PW1ST value: 0x%02X - %f",
  427. PW1ST_ValueA, (float)(PW1ST_ValueA >> 24) / 128);
  428. printf("\r\nSTOF - TOF reg: 0x%08X - 0x%08X - 0x%08X - 0x%08X",
  429. tofValue[0], tofValue[1], tofValue[2], tofValue[3]);
  430. printf("\r\nSTOF - TOF reg: %d - %d - %d - %d",
  431. tofValue[0] / 65536, tofValue[1] / 65536,
  432. tofValue[2] / 65536, tofValue[3] / 65536);
  433. printf("\r\nSTOF - TOF reg: %f - %f - %f - %f",
  434. (float)tofValue[0] / GP22_CALC_CLKHS_FREQ / 65536,
  435. (float)tofValue[1] / GP22_CALC_CLKHS_FREQ / 65536,
  436. (float)tofValue[2] / GP22_CALC_CLKHS_FREQ / 65536,
  437. (float)tofValue[3] / GP22_CALC_CLKHS_FREQ / 65536);
  438. printf("\r\nSTOF - Wave: %f us - %f MHz", wavePeriod, waveFreq);
  439. printf("\r\nSTOF - Time: %f us - %f m/s", echoTimeCalc[0], speedWave[0]);
  440. printf("\r\nStep %02d-A done! ----------------------------------------------",
  441. GP22_STEP_START_TOF);
  442. #endif /* __DEBUG_GP22_ATY */
  443. return 0;
  444. }
  445. uint8_t GP22_Process_UsWave1MHz(uint8_t cmdCode)
  446. {
  447. // cmdCode = 0xFF;
  448. uint8_t errCode = 0;
  449. if(cmdCode & GP22_STEP_RESET)
  450. GP22_Reset();
  451. if(cmdCode & GP22_STEP_SPI_TEST)
  452. if(GP22_SpiTest(0x68)) return GP22_STEP_SPI_TEST;
  453. if(cmdCode & GP22_STEP_REG_INIT
  454. || cmdCode & GP22_STEP_READ_ID){
  455. GP22_RegInit_UsWave1MHz();
  456. if(GP22_ReadId()) return GP22_STEP_READ_ID;
  457. }
  458. // if(cmdCode & GP22_STEP_COMPARE_E2)
  459. // if(GP22_CompareE2()) return GP22_STEP_COMPARE_E2;
  460. if(cmdCode & GP22_STEP_CALC_HSC)
  461. if(GP22_CalcHSC()) return GP22_STEP_CALC_HSC;
  462. // GP22_CALC_CLKHS_FREQ = GP22_CLKHS_FREQ;
  463. if(cmdCode & GP22_STEP_START_TEMP)
  464. if(GP22_StartTemp()) errCode = GP22_STEP_START_TEMP;
  465. if(cmdCode & GP22_STEP_START_TOF)
  466. if(GP22_StartTof_UsWave1MHz()) errCode = GP22_STEP_START_TOF;
  467. return errCode;
  468. }
  469. uint8_t GP22_Process_Run(uint8_t cmdCode)
  470. {
  471. uint8_t errCode = 0;
  472. #ifdef __DEBUG_GP22_ATY
  473. printf("\r\nStart");
  474. #endif /* __DEBUG_GP22_ATY */
  475. // GP22_WAKE;
  476. errCode = GP22_Process_UsWave1MHz(cmdCode);
  477. // GP22_AnalyseErrCode();
  478. #ifdef __DEBUG_GP22_ATY
  479. printf("\r\nStep %02d done! ------------------------------------------------", errCode);
  480. printf("\r\nOver!");
  481. #endif /* __DEBUG_GP22_ATY */
  482. return errCode;
  483. }
  484. #elif defined(GP22_MEASURE_US_DIGITAL)
  485. void GP22_RegInitUs_Digital(void)
  486. {
  487. GP22_REG[0] = GP22_CFG_KEEP_DEFAULT0
  488. | GP22_CFG0_ANZ_FIRE_0 * 6
  489. // | GP22_CFG0_ANZ_FIRE_0 * 14
  490. | GP22_CFG0_DIV_FIRE_0 * 11
  491. | GP22_CFG0_ANZ_PER_CALRES_0 * 3
  492. | GP22_CFG0_DIV_CLKHS_0 * 2
  493. | GP22_CFG0_START_CLKHS_0 * 1
  494. | GP22_CFG0_CALIBRATE * 1
  495. | GP22_CFG0_MESSB2 * 1
  496. | GP22_Ids[0];
  497. GP22_REG[1] = GP22_CFG_KEEP_DEFAULT1
  498. | GP22_CFG1_HIT2_0 * 2
  499. | GP22_CFG1_HIT1_0 * 1
  500. | GP22_CFG1_HITIN2_0 * 0
  501. | GP22_CFG1_HITIN1_0 * 4
  502. | GP22_CFG1_SEL_START_FIRE * 1
  503. | GP22_CFG1_SEL_TSTO2_0 * 2
  504. | GP22_CFG1_SEL_TSTO1_0 * 1
  505. | GP22_Ids[1];
  506. GP22_REG[2] = GP22_CFG_KEEP_DEFAULT2
  507. | GP22_CFG2_EN_INT_TDC_TIMEOUT * 1
  508. | GP22_CFG2_EN_INT_HITS * 1
  509. | GP22_CFG2_EN_INT_ALU * 1
  510. | GP22_CFG2_RFEDGE1 * 1
  511. | GP22_Ids[2];
  512. GP22_REG[3] = GP22_CFG_KEEP_DEFAULT3
  513. | GP22_CFG3FW_EN_AUTOCALC_MB2 * 1
  514. // | GP22_CFG3FW_EN_FIRST_WAVE * 1
  515. | GP22_CFG3FW_EN_ERR_VAL * 1
  516. | GP22_CFG3FW_SEL_TIMO_MB2_0 * 3
  517. | GP22_CFG3FW_DELREL3_0 * 5
  518. | GP22_CFG3FW_DELREL2_0 * 4
  519. | GP22_CFG3FW_DELREL1_0 * 3
  520. | GP22_Ids[3];
  521. GP22_REG[4] = GP22_CFG_KEEP_DEFAULT4
  522. // | GP22_CFG4FW_DIS_PW * 1
  523. // | GP22_CFG4FW_OFFSRNG2 * 1
  524. // | GP22_CFG4FW_OFFSRNG1 * 1
  525. // | GP22_CFG4FW_EDGE_FW * 1
  526. // | GP22_CFG4FW_OFFS_0 * 15
  527. // | GP22_CFG4FW_OFFS_0 * 16
  528. | GP22_Ids[4];
  529. GP22_REG[5] = GP22_CFG_KEEP_DEFAULT5
  530. | GP22_CFG5_CON_FIRE_0 * 1
  531. | GP22_CFG5_EN_STARTNOISE * 1
  532. | GP22_CFG5_PHFIRE_0 * 0x5555
  533. | GP22_Ids[5];
  534. GP22_REG[6] = GP22_CFG_KEEP_DEFAULT6
  535. // | GP22_CFG6_NEG_STOP_TEMP * 1
  536. // | GP22_CFG6_DOUBLE_RES * 1
  537. | GP22_CFG6_QUAD_RES * 1
  538. // | GP22_CFG6_EN_ANALOG * 1
  539. // | GP22_CFG6_TW2_0 * 3
  540. | GP22_CFG6_EN_INT_END * 1
  541. // | GP22_CFG6_FIREO_DEF * 0
  542. | GP22_CFG6_ANZ_FIRE_END_0 * 0
  543. | GP22_Ids[6];
  544. #ifdef __DEBUG_GP22_ATY
  545. printf("\r\nREGI - 0: %08X\r\nREGI - 1: %08X\r\nREGI - 2: %08X\
  546. \r\nREGI - 3: %08X\r\nREGI - 4: %08X\r\nREGI - 5: %08X\r\nREGI - 6: %08X",
  547. GP22_REG[0], GP22_REG[1], GP22_REG[2],
  548. GP22_REG[3], GP22_REG[4], GP22_REG[5], GP22_REG[6]);
  549. #endif /* __DEBUG_GP22_ATY */
  550. GP22_WriteReg(0x80, GP22_REG[0]);
  551. GP22_WriteReg(0x81, GP22_REG[1]);
  552. GP22_WriteReg(0x82, GP22_REG[2]);
  553. GP22_WriteReg(0x83, GP22_REG[3]);
  554. GP22_WriteReg(0x84, GP22_REG[4]);
  555. GP22_WriteReg(0x85, GP22_REG[5]);
  556. GP22_WriteReg(0x86, GP22_REG[6]);
  557. }
  558. uint8_t gp22StartTofFlag = 0;
  559. uint32_t tofValue[4] = {0};
  560. uint8_t GP22_StartTofUs_Digital(void)
  561. {
  562. uint32_t tofReadValue[4] = {0};
  563. uint32_t PW1ST_Value = 0;
  564. gp22StartTofFlag = 1;
  565. GP22_WriteData(GP22_OPCODE_INIT);
  566. GP22_WriteData(GP22_OPCODE_START_TOF);
  567. // GP22_WriteData(GP22_OPCODE_START_TOF2);
  568. if(GP22_WaitInt(GP22_STEP_START_TOF) != 0) return 1;
  569. gp22StartTofFlag = 0;
  570. tofReadValue[0] = GP22_ReadReg(GP22_OPCODE_READ_REG | 0x00);
  571. tofReadValue[1] = GP22_ReadReg(GP22_OPCODE_READ_REG | 0x01);
  572. tofReadValue[2] = GP22_ReadReg(GP22_OPCODE_READ_REG | 0x02);
  573. tofReadValue[3] = GP22_ReadReg(GP22_OPCODE_READ_REG | 0x03);
  574. tofValue[0] = tofReadValue[0];
  575. tofValue[1] = tofReadValue[1];
  576. tofValue[2] = tofReadValue[2];
  577. tofValue[3] = tofReadValue[3];
  578. PW1ST_Value = GP22_ReadReg(GP22_OPCODE_READ_REG | 0x08);
  579. PW1ST_Value = PW1ST_Value * 1;
  580. #ifdef __DEBUG_GP22_ATY
  581. printf("\r\nSTOF - TOF reg: 0x%08X - 0x%08X - 0x%08X - 0x%08X",
  582. tofReadValue[0], tofReadValue[1], tofReadValue[2], tofReadValue[3]);
  583. printf("\r\nSTOF - TOF reg: %d - %d - %d - %d",
  584. tofReadValue[0], tofReadValue[1], tofReadValue[2], tofReadValue[3]);
  585. printf("\r\nSTOF - TOF reg: %.2f - %.2f - %.2f - %.2f",
  586. (float)tofReadValue[0] / 65536, (float)tofReadValue[1] / 65536,
  587. (float)tofReadValue[2] / 65536, (float)tofReadValue[3] / 65536);
  588. printf("\r\nSTOF - PW1ST value: 0x%02X - %f",
  589. PW1ST_Value, (float)(PW1ST_Value >> 24) / 128);
  590. #endif /* __DEBUG_GP22_ATY */
  591. return 0;
  592. }
  593. // Mode 1
  594. /* Laser distance calc ***********************************************************/
  595. #elif defined(GP22_MEASURE_UsWave1MHz1)
  596. // ---------------------------------------------------
  597. // T_ref
  598. // Time_Value = ----------- * measured_RAW_Value
  599. // Cal2-Cal1
  600. // 3*10^8 m/s light speed(0.3m/ns)
  601. // ---------------------------------------------------
  602. uint8_t GP22_StartTofLaserCal(void)
  603. {
  604. uint32_t tofValue = 0, calValue = 0;
  605. GP22_WriteReg(0x80, GP22_REG[0]
  606. | GP22_CFG0_CALIBRATE * 1);
  607. GP22_WriteReg(0x82, GP22_REG[2]
  608. & (~GP22_CFG2_EN_INT_ALU)
  609. & (~GP22_CFG2_EN_INT_TDC_TIMEOUT)
  610. | (GP22_CFG2_EN_INT_HITS * 1));
  611. GP22_WriteData(GP22_OPCODE_INIT);
  612. GP22_WriteData(GP22_OPCODE_START_CAL_TOF);
  613. if(GP22_WaitInt(GP22_STEP_START_TOF) != 0) return 1;
  614. #ifdef __DEBUG_GP22_ATY
  615. printf("\r\nTOF - Laser: Cal over");
  616. #endif /* __DEBUG_GP22_ATY */
  617. GP22_WriteReg(0x82, GP22_REG[2]
  618. & (~GP22_CFG2_EN_INT_HITS)
  619. | (GP22_CFG2_EN_INT_ALU * 1)
  620. | (GP22_CFG2_EN_INT_TDC_TIMEOUT * 1));
  621. GP22_WriteData(GP22_OPCODE_INIT);
  622. GP22_SIGNAL_L;
  623. GP22_SIGNAL_H;
  624. GP22_SIGNAL_L;
  625. // for(uint8_t i = 0; i < 2; i++){}
  626. GP22_SIGNAL_A_L;
  627. GP22_SIGNAL_A_H;
  628. GP22_SIGNAL_A_L;
  629. if(GP22_WaitInt(GP22_STEP_START_TOF) != 0) return 1;
  630. tofValue = GP22_ReadReg(GP22_OPCODE_READ_REG | 0x00);
  631. #ifdef __DEBUG_GP22_ATY
  632. printf("\r\nTOF - Laser: 0x%08X - %d", tofValue, tofValue);
  633. #endif /* __DEBUG_GP22_ATY */
  634. GP22_WriteReg(0x81, GP22_REG[1]
  635. & (~GP22_CFG1_HIT2_0)
  636. & (~GP22_CFG1_HIT2_1)
  637. & (~GP22_CFG1_HIT2_2)
  638. & (~GP22_CFG1_HIT2_3)
  639. & (~GP22_CFG1_HIT1_0)
  640. & (~GP22_CFG1_HIT1_1)
  641. & (~GP22_CFG1_HIT1_2)
  642. & (~GP22_CFG1_HIT1_3)
  643. | GP22_CFG1_HIT2_0 * 6
  644. | GP22_CFG1_HIT1_0 * 7); // Cal1 - Cal2, EN Stop2/Stop1 1 hit
  645. calValue = GP22_ReadReg(GP22_OPCODE_READ_REG | 0x01);
  646. #ifdef __DEBUG_GP22_ATY
  647. printf("\r\nTOF - Laser: 0x%08X - %d", calValue, calValue / 65536);
  648. printf("\r\nTOF - Laser: %f ns - %f m",
  649. (float)tofValue / ((float)calValue / 65536) * 16, // TODO: Time calculate wrong
  650. (float)tofValue / ((float)calValue / 65536) * 16 * 0.23);
  651. #endif /* __DEBUG_GP22_ATY */
  652. return 0;
  653. }
  654. void GP22_Process(uint32_t* gp22ProcessValue)
  655. {
  656. if(!GP22_Step(GP22_STEP_RESET)
  657. && !GP22_Step(GP22_STEP_SPI_TEST)
  658. && !GP22_Step(GP22_STEP_REG_INIT)
  659. && !GP22_Step(GP22_STEP_READ_ID)
  660. && !GP22_Step(GP22_STEP_COMPARE_E2)
  661. && !GP22_Step(GP22_STEP_START_TOF)
  662. // && !GP22_Step(GP22_STEP_CALC_HSC)
  663. // && !GP22_Step(GP22_STEP_START_TEMP)
  664. )
  665. {
  666. // gp22ProcessValue[0] = tofValue[0];
  667. // gp22ProcessValue[1] = tofValue[1];
  668. // gp22ProcessValue[2] = tofValue[2];
  669. // gp22ProcessValue[3] = tofValue[3];
  670. }
  671. GP22_AnalyseErrCode();
  672. #ifdef __DEBUG_GP22_ATY
  673. printf("\r\n");
  674. #endif /* __DEBUG_GP22_ATY */
  675. }
  676. #endif /* if defined*/
  677. #endif /* __GP22_ATY_C */
  678. /******************************** End Of File *********************************/