tdc_ms1030 - bak.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407
  1. #include "board.h"
  2. #include "tdc_tof.h"
  3. #define DFLT_WREG0 ((24u<<25) | \
  4. ( 3u<<19) | \
  5. ( 0u<<17) | \
  6. ( 2u<<14) | \
  7. ( 0u<<13) | \
  8. ( 0u<<12) | \
  9. ( 9u<< 8) | \
  10. ( 0u<< 2) | \
  11. ( 3u<< 0))
  12. #define DFLT_WREG1 (( 1u<<31) | \
  13. ( 0u<<30) | \
  14. ( 1u<<29) | \
  15. ( 0u<<28) | \
  16. ( 0u<< 0))
  17. #define DFLT_WREG2 (( 0u<<30) | \
  18. ( 6u<<24) | \
  19. ( 8u<<18) | \
  20. (10u<<12) | \
  21. (12u<< 6) | \
  22. (14u<< 0))
  23. #define DFLT_WREG3 ((16u<<26) | \
  24. (18u<<20) | \
  25. (20u<<14))
  26. #define DFLT_WREG4 (( 0u<<28) | \
  27. ( 0u<<27) | \
  28. ( 1u<<26) | \
  29. ( 1u<<25) | \
  30. ( 0u<<24) | \
  31. ( 1u<<23) | \
  32. ( 0u<<22) | \
  33. ( 1u<<21) | \
  34. ( 0u<<20) | \
  35. /*( 0u<<18) |*/ \
  36. ( 3u<<18) | \
  37. ( 0u<<17) | \
  38. ( 0u<<14) | \
  39. ( 0u<<11) | \
  40. ( 1u<<10) | \
  41. ( 1u<< 8))
  42. uint16_t tof_state = TOF_ST_INIT;
  43. uint32_t tof_flag;
  44. //uint16_t CntSync = 0;
  45. void tdc_tof_init(void)
  46. {
  47. //MUX ADDA : PA08; ADDB : PC09
  48. mux_init();
  49. // MS1030 RSTN : PD02
  50. GpioInit(PD02_AF0, GPIO_DIG_OUT_WEAK_PUSH_NONEUP_NONEDOWN);
  51. GpioClrPins(GpioPortD, GpioBitMsk2); // reset active low
  52. us_delay(10); // 10us > tPH(30ns)
  53. GpioSetPins(GpioPortD, GpioBitMsk2); // reset release
  54. us_delay(10); // 10us > tRFS(30ns)
  55. tdc_spi_init(); // peripheral init
  56. PeriClk_MutEnable(PeriClk_Spi1);
  57. DMA_ReqClkOn();
  58. // TDC POR
  59. tdc_spi_wr_code(0x50);
  60. us_delay(1000); // 1000us > specification(500us)
  61. tdc_spi_wr_d32(0x80, DFLT_WREG0);
  62. tof_flag = TOF_FLAG_UNALGND_CHOP;
  63. if (tdc_spi_rd_d8(0xd3)!=(DFLT_WREG0&0xFFu)){
  64. tof_flag |= TOF_FLAG_TDC_FAULT;
  65. } else {
  66. // write_reg0
  67. tdc_spi_wr_d32(0x80, DFLT_WREG0 |
  68. ( 2u<< 4) // SEL_TIMO_MB, 0:64us; 1:128us; 2:256us; ~ 6:4096us;
  69. );
  70. // write_reg1
  71. tdc_spi_wr_d32(0x81, DFLT_WREG1 |
  72. (5760u<< 7) // DELVAL1=45us, format:16.5, time unit:Tref;
  73. );
  74. // write_reg2
  75. tdc_spi_wr_d32(0x82, DFLT_WREG2 |
  76. ( 0u<<31) // EN_FIRST_WAVE, 0:disable; 1:enable;
  77. );
  78. // write_reg3
  79. tdc_spi_wr_d32(0x83, DFLT_WREG3 |
  80. ( 1u<<13) // measure FW pulse width, 0:on; 1:off
  81. );
  82. // write_reg4
  83. tdc_spi_wr_d32(0x84, DFLT_WREG4);
  84. tdc_spi_wr_code(0x70);
  85. tdc_spi_wr_code(0x06);
  86. // not 'INTN' but 'us_delay' to avoid dead loop when INTN fault
  87. us_delay(2000); // > 480 + 600 + 16 + 70
  88. tdc_read_cal(0x0u);
  89. // tof_flag ^= TOF_FLAG_UNALGND_CHOP;
  90. // // start a single ToF to align MS1030 internal calibration order(1 CAL every 24 ToF)
  91. // // must be sure that CAL not in the middle of UP Tof and DOWN Tof
  92. // tdc_start_tof(TDC_CFG_DO_WREG | TDC_CFG_FIRE_UP);
  93. //
  94. // // not 'INTN' but 'us_delay' to avoid dead loop when INTN fault
  95. // us_delay(2000); // > 480 + 600 + 16 + 70
  96. //
  97. // tdc_read_tof(TDC_CFG_FIRE_UP); // read data and clear interrupt
  98. }
  99. DMA_ReqClkOff();
  100. PeriClk_MutDisable(PeriClk_Spi1);
  101. // MS1030 INTN : PD03 falling edge trigger
  102. GpioInit(PD03_AF0, GPIO_DIG_IN_PULLUP_NONEDOWN);
  103. GpioEventInit(PD03_AF0, GPIO_EVENT_FALL);
  104. // enable interrupt at NVIC side
  105. IRQMutEnable(PORTD_F_IRQn, NVIC_PRIO_1);
  106. }
  107. void tdc_start_tof(uint32_t cfg)
  108. {
  109. //rt_enter_critical();
  110. uint32_t u32tmp;
  111. if (cfg & TDC_CFG_DO_CKENA){
  112. PeriClk_MutEnable(PeriClk_Spi1);
  113. DMA_ReqClkOn();
  114. }
  115. if (cfg & TDC_CFG_DO_WREG){
  116. // write_reg0
  117. tdc_spi_wr_d32(0x80, DFLT_WREG0 |
  118. ( 1u<< 4) // SEL_TIMO_MB, 0:64us; 1:128us; 2:256us; ~ 6:4096us;
  119. );
  120. // write_reg1
  121. // DN15: L/C > (42+21+10*2)*1000/1555=53us
  122. tdc_spi_wr_d32(0x81, DFLT_WREG1 |
  123. (5760u<< 7) // DELVAL1=45us, format:16.5, time unit:Tref;
  124. // (6400u<< 7) // DELVAL1=50us, format:16.5, time unit:Tref;
  125. // (7040u<< 7) // DELVAL1=55us, format:16.5, time unit:Tref;
  126. // (7680u<< 7) // DELVAL1=60us, format:16.5, time unit:Tref;
  127. // (8230u<< 7) // DELVAL1=65us, format:16.5, time unit:Tref;
  128. // (8960u<< 7) // DELVAL1=70us, format:16.5, time unit:Tref;
  129. );
  130. // write_reg2
  131. tdc_spi_wr_d32(0x82, DFLT_WREG2 |
  132. ( 1u<<31) // EN_FIRST_WAVE, 0:disable; 1:enable;
  133. );
  134. // write_reg3
  135. u32tmp = DFLT_WREG3 |
  136. ( 0u<<13) | // measure FW pulse width, 0:on; 1:off
  137. (TdcFwOfst<<6); // FW wave_offs, 0:0mv; 1~63:2~126mV; 64~127:-128~-2mV;
  138. if (tof_state&TOF_ST_VOID){
  139. u32tmp |= (1u<<13); // disable FW pulse width to save power
  140. }
  141. tdc_spi_wr_d32(0x83, u32tmp);
  142. }
  143. // write_reg4
  144. u32tmp = DFLT_WREG4;
  145. // if (cfg & TDC_CFG_FIRE_UP){
  146. // u32tmp |= (1u<<30); // FIRE_UP, 0:off; 1:on;
  147. // } else {
  148. // u32tmp |= (1u<<29); // FIRE_DOWN, 0:off; 1:on;
  149. // }
  150. u32tmp |= (1u<<30); // FIRE_UP, 0:off; 1:on;
  151. tdc_spi_wr_d32(0x84, u32tmp);
  152. // Initial TDC status regs
  153. tdc_spi_wr_code(0x70);
  154. // start single-direction measure
  155. tdc_spi_wr_code(0x03);
  156. if (cfg & TDC_CFG_DO_CKDIS){
  157. DMA_ReqClkOff();
  158. PeriClk_MutDisable(PeriClk_Spi1);
  159. }
  160. //rt_exit_critical();
  161. }
  162. uint16_t TdcStatus;
  163. uint16_t TdcUPwFw;
  164. uint16_t TdcUPwHw;
  165. uint32_t TdcUToF[9];
  166. uint16_t TdcDPwFw;
  167. uint16_t TdcDPwHw;
  168. uint32_t TdcDToF[9];
  169. uint32_t TdcCal;
  170. //float TdcScale = 250.0f/65536;
  171. float TdcScale = 0.00381147861f;
  172. uint32_t TdcPT[4];
  173. float TdcResist = 5000; // OhmX10
  174. float WaterPrmt[2] = {25.0f, 1496.6f};
  175. void tdc_read_tof(uint32_t cfg)
  176. {
  177. uint32_t ch = get_mux_ch();
  178. // uint16_t i;
  179. if (cfg & TDC_CFG_DO_CKENA){
  180. PeriClk_MutEnable(PeriClk_Spi1);
  181. DMA_ReqClkOn();
  182. }
  183. TdcStatus = tdc_spi_rd_d16(0xd2); // status
  184. if ((TdcStatus&0x0600)==0x0000u){
  185. tof_flag ^= TOF_FLAG_UNALGND_CHOP;
  186. // if (CntSync>=240){
  187. // CntSync = 10;
  188. // } else {
  189. // CntSync += 10;
  190. // }
  191. }
  192. if ((TdcStatus&0x06F0)!=0x0090u){
  193. tof_flag |= TOF_FLAG_BLANK_PIPE;
  194. tdc_spi_wr_code(0x70);
  195. }
  196. else
  197. {
  198. TdcUPwFw = tdc_spi_rd_d16(0xd0); // First wave pulse width
  199. TdcUPwHw = tdc_spi_rd_d16(0xd1); // 1st Hit wave pulse width
  200. TdcUToF[ch] = tdc_spi_rd_d32(0xb8)*(1.0f/8); // average of all
  201. TdcDToF[ch] = tdc_spi_rd_d32(0xC1)*(1.0f/8); // average of all
  202. #if 0
  203. if (cfg & TDC_CFG_FIRE_UP){
  204. TdcUPwFw = tdc_spi_rd_d16(0xd0); // First wave pulse width
  205. TdcUPwHw = tdc_spi_rd_d16(0xd1); // 1st Hit wave pulse width
  206. tdc_spi_wr_code(0x70);
  207. // TOF_UP_HITs
  208. TdcUToF[0] = tdc_spi_rd_d32(0xb8)*(1.0f/8); // average of all
  209. // for(i=0; i<8; i++){
  210. // TdcUToF[i+1] = tdc_spi_rd_d32(0xb0 + i);
  211. // };
  212. } else if (cfg & TDC_CFG_FIRE_DOWN){
  213. TdcDPwFw = tdc_spi_rd_d16(0xd0); // First wave pulse width
  214. TdcDPwHw = tdc_spi_rd_d16(0xd1); // 1st Hit wave pulse width
  215. tdc_spi_wr_code(0x70);
  216. // TOF_DOWN_HITs
  217. TdcDToF[0] = tdc_spi_rd_d32(0xb8)*(1.0f/8); // average of all
  218. // for(i=0; i<8; i++){
  219. // TdcDToF[i+1] = tdc_spi_rd_d32(0xb0 + i);
  220. // };
  221. }
  222. #endif
  223. }
  224. if (cfg & TDC_CFG_DO_CKDIS){
  225. DMA_ReqClkOff();
  226. PeriClk_MutDisable(PeriClk_Spi1);
  227. }
  228. }
  229. void tdc_start_cal(uint32_t cfg)
  230. {
  231. //rt_enter_critical();
  232. if (cfg & TDC_CFG_DO_CKENA){
  233. PeriClk_MutEnable(PeriClk_Spi1);
  234. DMA_ReqClkOn();
  235. }
  236. // 8*T_32k = 244.140625us
  237. if (cfg & TDC_CFG_DO_WREG){
  238. // write_reg0
  239. tdc_spi_wr_d32(0x80, DFLT_WREG0 |
  240. ( 2u<< 4) // SEL_TIMO_MB, 0:64us; 1:128us; 2:256us; ~ 6:4096us;
  241. );
  242. // write_reg3
  243. tdc_spi_wr_d32(0x83, DFLT_WREG3 |
  244. ( 1u<<13) // measure FW pulse width, 0:on; 1:off
  245. );
  246. }
  247. tdc_spi_wr_code(0x70);
  248. tdc_spi_wr_code(0x06);
  249. if (cfg & TDC_CFG_DO_CKDIS){
  250. DMA_ReqClkOff();
  251. PeriClk_MutDisable(PeriClk_Spi1);
  252. }
  253. //rt_exit_critical();
  254. }
  255. void tdc_read_cal(uint32_t cfg)
  256. {
  257. //rt_enter_critical();
  258. if (cfg & TDC_CFG_DO_CKENA){
  259. PeriClk_MutEnable(PeriClk_Spi1);
  260. DMA_ReqClkOn();
  261. }
  262. TdcStatus = tdc_spi_rd_d16(0xd2); // status
  263. if ((TdcStatus&0x0600)==0x0000u){
  264. tof_flag ^= TOF_FLAG_UNALGND_CHOP;
  265. // if (CntSync>=240){
  266. // CntSync = 10;
  267. // } else {
  268. // CntSync += 10;
  269. // }
  270. tdc_spi_wr_code(0x70);
  271. TdcCal = tdc_spi_rd_d32(0xd4);
  272. // TdcScale = 244140.625f/TdcCal;
  273. TdcScale += (244140.625f/TdcCal - TdcScale)*(1.0f/2);
  274. //LOG("----------TdcScale=%.6f\n",TdcScale);
  275. } else {
  276. tdc_spi_wr_code(0x70);
  277. }
  278. if (cfg & TDC_CFG_DO_CKDIS){
  279. DMA_ReqClkOff();
  280. PeriClk_MutDisable(PeriClk_Spi1);
  281. }
  282. //rt_exit_critical();
  283. }
  284. void tdc_start_temp(uint32_t cfg)
  285. {
  286. //rt_enter_critical();
  287. if (cfg & TDC_CFG_DO_CKENA){
  288. PeriClk_MutEnable(PeriClk_Spi1);
  289. DMA_ReqClkOn();
  290. }
  291. if (cfg & TDC_CFG_DO_WREG){
  292. // write_reg0
  293. tdc_spi_wr_d32(0x80, DFLT_WREG0 |
  294. ( 3u<< 4) // SEL_TIMO_MB, 0:64us; 1:128us; 2:256us; 3:512us; ~ 6:4096us;
  295. );
  296. // write_reg1
  297. tdc_spi_wr_d32(0x81, DFLT_WREG1 |
  298. ( 0u<< 7) // DELVAL1=0us, format:16.5, time unit:Tref;
  299. );
  300. // write_reg3
  301. tdc_spi_wr_d32(0x83, DFLT_WREG3 |
  302. ( 1u<<13) // measure FW pulse width, 0:on; 1:off
  303. );
  304. }
  305. tdc_spi_wr_code(0x70);
  306. tdc_spi_wr_code(0x04);
  307. if (cfg & TDC_CFG_DO_CKDIS){
  308. DMA_ReqClkOff();
  309. PeriClk_MutDisable(PeriClk_Spi1);
  310. }
  311. //rt_exit_critical();
  312. }
  313. void tdc_read_temp(uint32_t cfg)
  314. {
  315. //rt_enter_critical();
  316. if (cfg & TDC_CFG_DO_CKENA){
  317. PeriClk_MutEnable(PeriClk_Spi1);
  318. DMA_ReqClkOn();
  319. }
  320. TdcStatus = tdc_spi_rd_d16(0xd2); // status
  321. tdc_spi_wr_code(0x70);
  322. TdcPT[0] = tdc_spi_rd_d32(0xc2); // REF 5K +/- 5
  323. TdcPT[1] = tdc_spi_rd_d32(0xc3); // U7
  324. //TdcPT[2] = tdc_spi_rd_d32(0xc4); // U8
  325. //TdcPT[3] = tdc_spi_rd_d32(0xc5); // REF 1.5K
  326. // if (TdcPT[0]>0x1000000u && TdcPT[0]<0x2000000u){
  327. // TdcResist = 5000.0f*TdcPT[1]/TdcPT[0];
  328. // Ohm2Dgr2USSpd(TdcResist, WaterPrmt); // [0]:Temperature; [1]:ultrasonic speed @Temp
  329. // }
  330. if (TdcPT[0]>0xB00000u && TdcPT[0]<0xE00000u){
  331. TdcResist = 2500.0f*TdcPT[1]/TdcPT[0]; // ohmX10
  332. Ohm2Dgr2USSpd(TdcResist, WaterPrmt); // [0]:Temperature; [1]:ultrasonic speed @Temp
  333. }
  334. LOG("----------T=%.6f,C=%.6f\n",WaterPrmt[0],WaterPrmt[1]);
  335. if (cfg & TDC_CFG_DO_CKDIS){
  336. DMA_ReqClkOff();
  337. PeriClk_MutDisable(PeriClk_Spi1);
  338. }
  339. //rt_exit_critical();
  340. }