GP22_ATY.c 25 KB

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