tdc_ms1030.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509
  1. #include "board.h"
  2. #include "tdc_tof.h"
  3. #define ANZ_FIRE 12u //fire发送脉冲数
  4. #define DIV_FIRE 3u //内部时钟产生脉冲的分频
  5. #define DIV_CLKHS 0u //高速时钟分频
  6. #define START_CLKHS 2u //高速启动后测量前晶振启动间隔时间,2(120us),0 = 晶振关闭;1 = 晶振持续开启;2 = 启振延迟时间 120 μs
  7. #define NEG_START 0u //start通道边缘
  8. #define NEG_STOP 0u //stop通道边缘
  9. #define HITIN 9u //stop预期脉冲数
  10. #define SEL_TIMO_MB 3u //测量范围溢出选择时间限制,3 = 512μs,4 = 1024μs
  11. #define SEL_TSTO2 3u //TSTO2 管脚状态设置,3 = 32kHz 输出
  12. #define SEL_TSTO1 0u //TSTO1 管脚状态设置,0 = 高速时钟输入
  13. #define DFLT_WREG0 ((ANZ_FIRE<<25) | \
  14. ( DIV_FIRE<<19) | \
  15. ( DIV_CLKHS<<17) | \
  16. ( START_CLKHS<<14) | \
  17. ( NEG_START<<13) | \
  18. ( NEG_STOP<<12) | \
  19. ( HITIN<< 8) | \
  20. ( SEL_TIMO_MB<< 4) | \
  21. ( SEL_TSTO2<< 2) | \
  22. ( SEL_TSTO1<< 0))
  23. #define EN_INT_TO 1 //TIMOUT中断触发位,1=开启中断
  24. #define EN_INT_HIT 0 //END HITS中断触发位,0=关闭中断
  25. #define EN_INT_ALU 1 //alu中断触发位,1=开启中断
  26. #define RFEDGE 0 //stop通道边沿敏感性,0 = 上升或下降沿
  27. #define UP_DELVAL1 200u //当EN_FIST_WAVE=1时,为第一波顺流屏蔽窗口 BIT12~27,200*0.25=50us
  28. #define OFFSET 0 //当en_hc=0时, OFFSET电压31~-32mV;0=0mV,1=1mV,...31=31mV,32=-32mV...63=-1mV,60=-4mV
  29. #define DFLT_WREG1 ((EN_INT_TO<<31) | \
  30. ( EN_INT_HIT<<30) | \
  31. ( EN_INT_ALU<<29) | \
  32. ( RFEDGE<<28) | \
  33. ( UP_DELVAL1<<12) | \
  34. ( OFFSET<< 0))
  35. #define EN_FIRST_WAVE 1u //BIT31, 为1时后面的定义有效
  36. #define EDGE_FW 0u //第一波边缘敏感性,0 = 上升沿
  37. #define DELREL1 6u //第1个stop接收第几个回波周期
  38. #define DELREL2 8u //第2个stop接收第几个回波周期
  39. #define DELREL3 10u //第3个stop接收第几个回波周期
  40. #define DELREL4 12u //第4个stop接收第几个回波周期
  41. #define DELREL5 14u //第5个stop接收第几个回波周期
  42. #define DFLT_WREG2 ((EN_FIRST_WAVE<<31) | \
  43. (EDGE_FW<<30) | \
  44. (DELREL1<<24) | \
  45. (DELREL2<<18) | \
  46. (DELREL3<<12) | \
  47. (DELREL4<< 6) | \
  48. (DELREL5<< 0))
  49. #define DELREL6 16u //第6个stop接收第几个回波周期
  50. #define DELREL7 18u //第7个stop接收第几个回波周期
  51. #define DELREL8 20u //第8个stop接收第几个回波周期
  52. #define DIS_PW 1u //关闭脉冲宽度测量功能,1=关闭
  53. #define WAVE_OFFS 0u //第一波比较器偏置电压:EN_HC = 0 -64mV~63mV;0=0mV,1=1mV,...63=63mV,64=-64mV,...127=-1mV;16=16mV
  54. #define DFLT_WREG3 ((DELREL6<<26) | \
  55. (DELREL7<<20) | \
  56. (DELREL8<<14) | \
  57. (DIS_PW<<13) | \
  58. (WAVE_OFFS<<6))
  59. #define CONF_FIRE 2 //脉冲触发器的输出设置,不可以设置为3, bit 30 = 1: 开启输出 FIRE_UP
  60. #define PHASE_FIREUP 0 //FIREUP反向设置,0:同相
  61. #define PHASE_FIREDOWN 0 //FIREDOWN 反向设置,0:同相
  62. #define SEL_START_FIRE 1 //FIRE 脉冲用作触发 TDC START,1 = FIRE 内部触发 START
  63. #define EN_ANALOG 1 //模拟测量部分开启控制,1 = 模拟部分开启, STOP1 和 STOP2 模拟信号输入
  64. #define HZ60 0 //顺流测量和逆流测量的时间单位,0 = 50 Hz,20ms
  65. #define TCYCLE 1 //设置温度测量的循环时间,1 = 512μs
  66. #define ANZ_FAKE 0 //在温度测量前的热身测量次数,1 = 7 次热身测量
  67. #define SEL_ECLK_TMP 1 //选择温度测量内部 cycle参考时钟,1 = CLKHS/128 作为 cycle 时钟
  68. #define TW2 5 //EN_ANALOG = 1,给STOP 端口的电容充电时间,0=60μs,1=120μs,2=180μs,3=240μs,4=300μs
  69. #define EN_PEAK 0 //峰值检波使能,0:不使能
  70. #define EN_DISCHARGE 0 //检波放电使能,0:不使能
  71. #define CYCLE_TOF 0 //顺流逆流自动检测模式下,顺流逆流测量间隔时间,0=0.25×HZ60(20ms@50Hz),5ms
  72. #define EN_ERR_VAL 1 //溢出时 ALU 写入全 F 到第一个 STOP 结果寄存器与累加和结果寄存器,1=开启
  73. #define EN_DC 1 //stop端口充电模式;0=交流模式,1=直流模式
  74. #define FIRE0_DEF 1 //非工作情况下 FIRE 通道的默认状态; 0=高阻(数字模式),1=低(模拟模式)
  75. #define DOUBLE_RES 0 //双精度模式使能,0:不使能
  76. #define QUAD_RES 1 //四精度模式使能,1:使能
  77. #define DFLT_WREG4 ((CONF_FIRE<<29) | \
  78. ( PHASE_FIREUP<<28) | \
  79. ( PHASE_FIREDOWN<<27) | \
  80. ( SEL_START_FIRE<<26) | \
  81. ( EN_ANALOG<<25) | \
  82. ( HZ60<<24) | \
  83. ( TCYCLE<<23) | \
  84. ( ANZ_FAKE<<22) | \
  85. ( SEL_ECLK_TMP<<21) | \
  86. ( TW2<<17) | \
  87. ( EN_PEAK<<16) | \
  88. ( EN_DISCHARGE<<15) | \
  89. ( CYCLE_TOF<<11) | \
  90. ( EN_ERR_VAL<<10) | \
  91. ( EN_DC<<9) | \
  92. ( FIRE0_DEF<<8) | \
  93. ( DOUBLE_RES<<7) | \
  94. ( QUAD_RES<<6))
  95. #define CYCLE_AUTOTOF 0 //自动测量循环时间, 0: 自动循环测量禁止
  96. #define ANZ_AUTOTOF 0 //自动循环测量的次数,0:不开启
  97. #define AUTOTOF_MODE 0 //自动循环测量模式,0:顺-逆-顺-逆
  98. #define PHASE_FIRENUM 0 //定义在第几个脉冲进行相位插入,0 = 关闭
  99. #define ANZ_PHASE 0 //插入的 FIRE 周期个数,0:不进行相位插入
  100. #define DFLT_WREG5 ((CYCLE_AUTOTOF<<20) | \
  101. ( ANZ_AUTOTOF<<16) | \
  102. ( AUTOTOF_MODE<<15) | \
  103. ( PHASE_FIRENUM<<6) | \
  104. ( ANZ_PHASE<<0))
  105. #define DOWN_DELVAL1 200u //当EN_FIST_WAVE=1时,为第一波逆流屏蔽窗口 BIT12~27,200*0.25=50us
  106. #define SEL_SRC_CLK 0 //选择高速时钟输入源,0:高速晶振产生时钟
  107. #define EN_HC 0 //比较器调节步长,0:1mV,1:2mV
  108. #define CURR32K 0 //32kHz 晶振功耗设置,0:低功耗
  109. #define DIS_Vref 1 //基准电压输出控制,0:开启基准输出, 1:关闭基准输出
  110. #define ADJ_LDO_ROUGH 0 //内部 LDO 输出粗调,0:1.8V 档位, 1:1.5V 档位
  111. #define ADJ_LDO_FINE 0 //内部 LDO 输出细调, 0-7:细调 LDO 输出电压
  112. #define DFLT_WREG6 ((DOWN_DELVAL1<<12) | \
  113. ( SEL_SRC_CLK<<7) | \
  114. ( EN_HC<<6) | \
  115. ( CURR32K<<5) | \
  116. ( DIS_Vref<<4) | \
  117. ( ADJ_LDO_ROUGH<<3) | \
  118. ( ADJ_LDO_FINE<<0))
  119. uint16_t tof_state = TOF_ST_INIT;
  120. uint32_t tof_flag;
  121. uint16_t TdcStatus;
  122. uint16_t TdcUPwFw;
  123. uint16_t TdcUPwHw;
  124. uint32_t TdcUToF[9];
  125. uint16_t TdcDPwFw;
  126. uint16_t TdcDPwHw;
  127. uint32_t TdcDToF[9];
  128. uint32_t TdcCal;
  129. //float TdcScale = 250.0f/65536;
  130. float TdcScale = 0.00381147861f;
  131. uint32_t TdcPT[4];
  132. float TdcResist = 5000; // OhmX10
  133. //modified by yuewei 20260520
  134. //float WaterPrmt[2] = {25.0f, 1496.6f};
  135. float WaterPrmt[2] = {25.0f, 1497.1484f};
  136. /**
  137. * @brief MS1031初始化函数
  138. * @param 无
  139. * @retval 无
  140. */
  141. void tdc_tof_init(void)
  142. {
  143. //MUX ADDA : PA08; ADDB : PC09
  144. mux_init();
  145. // MS1030 RSTN : PD02
  146. GpioInit(PD02_AF0, GPIO_DIG_OUT_WEAK_PUSH_NONEUP_NONEDOWN);
  147. GpioClrPins(GpioPortD, GpioBitMsk2); // reset active low
  148. us_delay(10); // 10us > tPH(30ns)
  149. GpioSetPins(GpioPortD, GpioBitMsk2); // reset release
  150. us_delay(10); // 10us > tRFS(30ns)
  151. tdc_spi_init(); // peripheral init
  152. PeriClk_MutEnable(PeriClk_Spi1);
  153. DMA_ReqClkOn();
  154. // TDC POR
  155. tdc_spi_wr_code(0x50);
  156. us_delay(1000); // 1000us > specification(500us)
  157. tdc_spi_wr_d32(0x80, DFLT_WREG0);
  158. tof_flag = TOF_FLAG_UNALGND_CHOP;
  159. if (tdc_spi_rd_d32(0xa0) != DFLT_WREG0)
  160. {
  161. tof_flag |= TOF_FLAG_TDC_FAULT;
  162. }
  163. else
  164. {
  165. tdc_spi_wr_d32(0x80,DFLT_WREG0);
  166. tdc_spi_wr_d32(0x81,DFLT_WREG1);
  167. tdc_spi_wr_d32(0x82,DFLT_WREG2);
  168. tdc_spi_wr_d32(0x83,DFLT_WREG3);
  169. tdc_spi_wr_d32(0x84,DFLT_WREG4);
  170. tdc_spi_wr_d32(0x85,DFLT_WREG5);
  171. tdc_spi_wr_d32(0x86,DFLT_WREG6);
  172. //读取时钟校准时,如果不加该项,状态寄存器d0中的TDC_Flood位会一直置位
  173. tdc_spi_wr_d32(0x87,0x20000000);//!!!!!!!!do not delete!!!!!!!!!
  174. tdc_spi_wr_code(0x70);
  175. //启动一次时钟校准
  176. tdc_spi_wr_code(0x06);
  177. //延时等待,时间必须大于时钟校准中断时间,便于后一步正确读取校准时间
  178. us_delay(2000);//!!!!!!!!do not delete!!!!!!!!!
  179. //读取一次时钟校准
  180. tdc_read_cal(0);
  181. }
  182. DMA_ReqClkOff();
  183. PeriClk_MutDisable(PeriClk_Spi1);
  184. // MS1030 INTN : PD03 falling edge trigger
  185. GpioInit(PD03_AF0, GPIO_DIG_IN_PULLUP_NONEDOWN);
  186. GpioEventInit(PD03_AF0, GPIO_EVENT_FALL);
  187. // enable interrupt at NVIC side
  188. IRQMutEnable(PORTD_F_IRQn, NVIC_PRIO_1);
  189. }
  190. /**
  191. * @brief MS1031开启第一波自动双向测量
  192. * @param uint32_t cfg可选下面参数
  193. * 1、TDC_CFG_DO_CKENA:开启SPI时钟+DMA中断
  194. * 2、TDC_CFG_DO_WREG:调整局部寄存器
  195. * 3、TDC_CFG_DO_CKDIS:关闭SPI时钟+DMA中断
  196. * @retval 无
  197. */
  198. void tdc_start_tof(uint32_t cfg)
  199. {
  200. uint32_t u32tmp;
  201. if (cfg & TDC_CFG_DO_CKENA)
  202. {
  203. PeriClk_MutEnable(PeriClk_Spi1);
  204. DMA_ReqClkOn();
  205. }
  206. if (cfg & TDC_CFG_DO_WREG)
  207. {
  208. // write_reg3
  209. u32tmp = (DFLT_WREG3 & ~((1u << 13) | (0x7F << 6))) | (TdcFwOfst << 6); //6到12位以及13位清0然后写入新数据到6到12位
  210. // measure FW pulse width, 0:on; 1:off
  211. // FW wave_offs, 0:0mv; 1~63:2~126mV; 64~127:-128~-2mV;
  212. if (tof_state&TOF_ST_VOID)
  213. {
  214. u32tmp |= (1u << 13); // disable FW pulse width to save power
  215. }
  216. tdc_spi_wr_d32(0x83, u32tmp);
  217. }
  218. // Initial TDC status regs
  219. tdc_spi_wr_code(0x70);
  220. // 开启第一波自动双向测量
  221. tdc_spi_wr_code(0x03);
  222. if (cfg & TDC_CFG_DO_CKDIS)
  223. {
  224. DMA_ReqClkOff();
  225. PeriClk_MutDisable(PeriClk_Spi1);
  226. }
  227. }
  228. /**
  229. * @brief MS1031读取第一波自动双向测量结果
  230. * @param uint32_t cfg可选下面参数
  231. * 1、TDC_CFG_DO_CKENA:开启SPI时钟+DMA中断
  232. * 2、TDC_CFG_DO_WREG:调整局部寄存器
  233. * 3、TDC_CFG_DO_CKDIS:关闭SPI时钟+DMA中断
  234. * @retval 无
  235. */
  236. void tdc_read_tof(uint32_t cfg)
  237. {
  238. if (cfg & TDC_CFG_DO_CKENA)
  239. {
  240. PeriClk_MutEnable(PeriClk_Spi1);
  241. DMA_ReqClkOn();
  242. }
  243. TdcStatus = tdc_spi_rd_d16(0xd0); //modified by yuewei 260813 0xd2 -> 0xd0, to match ms1031
  244. if ((TdcStatus&0x0300) == 0x0000u) //modified by yuewei 260813 0x0600 -> 0x0300, to match ms1031
  245. {
  246. tof_flag ^= TOF_FLAG_UNALGND_CHOP;
  247. //tdc_spi_wr_code(0x70);
  248. }
  249. if ((TdcStatus&0x03F0) != 0x0090u) //modified by yuewei 260813 0x06F0 -> 0x03F0, to match ms1031
  250. {
  251. tof_flag |= TOF_FLAG_BLANK_PIPE;
  252. tdc_spi_wr_code(0x70);
  253. }
  254. else
  255. {
  256. //modified by yuewei 260813 start
  257. TdcUPwFw = tdc_spi_rd_d16(0xd3); // First wave pulse width
  258. TdcUPwHw = tdc_spi_rd_d16(0xd4); // 1st Hit wave pulse width
  259. TdcDPwFw = TdcUPwFw;
  260. TdcDPwHw = TdcUPwHw;
  261. // TOF_UP_HITs
  262. TdcUToF[0] = tdc_spi_rd_d32(0xb8)*(1.0f/8); // average of all
  263. // TOF_DOWN_HITs
  264. TdcDToF[0] = tdc_spi_rd_d32(0xc8)*(1.0f/8); // average of all
  265. tdc_spi_wr_code(0x70);
  266. //modified by yuewei 260813 end
  267. }
  268. if (cfg & TDC_CFG_DO_CKDIS)
  269. {
  270. DMA_ReqClkOff();
  271. PeriClk_MutDisable(PeriClk_Spi1);
  272. }
  273. }
  274. /**
  275. * @brief MS1031开启高速晶振校准
  276. * @param uint32_t cfg可选下面参数
  277. * 1、TDC_CFG_DO_CKENA:开启SPI时钟+DMA中断
  278. * 2、TDC_CFG_DO_WREG:调整局部寄存器
  279. * 3、TDC_CFG_DO_CKDIS:关闭SPI时钟+DMA中断
  280. * @retval 无
  281. */
  282. void tdc_start_cal(uint32_t cfg)
  283. {
  284. if (cfg & TDC_CFG_DO_CKENA)
  285. {
  286. PeriClk_MutEnable(PeriClk_Spi1);
  287. DMA_ReqClkOn();
  288. }
  289. // 8*T_32k = 244.140625us
  290. if (cfg & TDC_CFG_DO_WREG)
  291. {
  292. // write_reg3
  293. tdc_spi_wr_d32(0x83, DFLT_WREG3 |
  294. ( 1u<<13) // measure FW pulse width, 0:on; 1:off
  295. );
  296. }
  297. tdc_spi_wr_code(0x70);
  298. tdc_spi_wr_code(0x06);
  299. if (cfg & TDC_CFG_DO_CKDIS)
  300. {
  301. DMA_ReqClkOff();
  302. PeriClk_MutDisable(PeriClk_Spi1);
  303. }
  304. }
  305. /**
  306. * @brief MS1031读取高速晶振校准结果
  307. * @param uint32_t cfg可选下面参数
  308. * 1、TDC_CFG_DO_CKENA:开启SPI时钟+DMA中断
  309. * 2、TDC_CFG_DO_WREG:调整局部寄存器
  310. * 3、TDC_CFG_DO_CKDIS:关闭SPI时钟+DMA中断
  311. * @retval 无
  312. */
  313. void tdc_read_cal(uint32_t cfg)
  314. {
  315. if (cfg & TDC_CFG_DO_CKENA)
  316. {
  317. PeriClk_MutEnable(PeriClk_Spi1);
  318. DMA_ReqClkOn();
  319. }
  320. TdcStatus = tdc_spi_rd_d16(0xd0); //modified by yuewei 260813 0xd2 -> 0xd0, to match ms1031
  321. if ((TdcStatus&0x0300) == 0x0000u) //modified by yuewei 260813 0x0600 -> 0x0300, to match ms1031
  322. {
  323. tof_flag ^= TOF_FLAG_UNALGND_CHOP;
  324. TdcCal = tdc_spi_rd_d32(0xd5);
  325. //TdcScale = 244140.625f/TdcCal;
  326. TdcScale += (244140.625f/TdcCal - TdcScale)*(1.0f/2);
  327. tdc_spi_wr_code(0x70);
  328. }
  329. else
  330. {
  331. tdc_spi_wr_code(0x70);
  332. }
  333. if (cfg & TDC_CFG_DO_CKDIS)
  334. {
  335. DMA_ReqClkOff();
  336. PeriClk_MutDisable(PeriClk_Spi1);
  337. }
  338. }
  339. /**
  340. * @brief MS1031开启温度采集
  341. * @param uint32_t cfg可选下面参数
  342. * 1、TDC_CFG_DO_CKENA:开启SPI时钟+DMA中断
  343. * 2、TDC_CFG_DO_WREG:调整局部寄存器
  344. * 3、TDC_CFG_DO_CKDIS:关闭SPI时钟+DMA中断
  345. * @retval 无
  346. */
  347. void tdc_start_temp(uint32_t cfg)
  348. {
  349. if (cfg & TDC_CFG_DO_CKENA)
  350. {
  351. PeriClk_MutEnable(PeriClk_Spi1);
  352. DMA_ReqClkOn();
  353. }
  354. if (cfg & TDC_CFG_DO_WREG)
  355. {
  356. // write_reg3
  357. tdc_spi_wr_d32(0x83, DFLT_WREG3 |
  358. ( 1u<<13) // measure FW pulse width, 0:on; 1:off
  359. );
  360. }
  361. tdc_spi_wr_code(0x70);
  362. tdc_spi_wr_code(0x04);
  363. if (cfg & TDC_CFG_DO_CKDIS)
  364. {
  365. DMA_ReqClkOff();
  366. PeriClk_MutDisable(PeriClk_Spi1);
  367. }
  368. }
  369. /**
  370. * @brief MS1031读取温度采集结果
  371. * @param uint32_t cfg可选下面参数
  372. * 1、TDC_CFG_DO_CKENA:开启SPI时钟+DMA中断
  373. * 2、TDC_CFG_DO_WREG:调整局部寄存器
  374. * 3、TDC_CFG_DO_CKDIS:关闭SPI时钟+DMA中断
  375. * @retval 无
  376. */
  377. void tdc_read_temp(uint32_t cfg)
  378. {
  379. if (cfg & TDC_CFG_DO_CKENA)
  380. {
  381. PeriClk_MutEnable(PeriClk_Spi1);
  382. DMA_ReqClkOn();
  383. }
  384. //delete by yuewei 260813, ms1031 status reg(d0) bit 11 and bit 10 can only indicate the PT1 is short or open
  385. //TdcStatus = tdc_spi_rd_d16(0xd0); //modified by yuewei 260813 0xd2 -> 0xd0, to match ms1031
  386. // REF 25K
  387. TdcPT[0] = tdc_spi_rd_d32(0xd1); //modified by yuewei 260813 0xc2 -> 0xd1, to match ms1031
  388. // NTC
  389. TdcPT[1] = tdc_spi_rd_d32(0xd2); //modified by yuewei 260813 0xc3 -> 0xd2, to match ms1031
  390. tdc_spi_wr_code(0x70);
  391. //add by yuewei 260813 to deal Rref or Rntc is short or open
  392. if((TdcPT[0] == 0) || (TdcPT[0] == 0xFFFFFFFF) || (TdcPT[1] == 0) || (TdcPT[1] == 0xFFFFFFFF))
  393. {
  394. TdcResist = 0;//when Rref or Rntc is short or open, set TdcResist = 0, to set the Temprature = -3276.8f in Ohm2Dgr2USSpd(TdcResist, WaterPrmt)
  395. }
  396. else
  397. {
  398. TdcResist = 2500.0f*TdcPT[1]/TdcPT[0]; // ohmX10
  399. }
  400. Ohm2Dgr2USSpd(TdcResist, WaterPrmt); // [0]:Temperature; [1]:ultrasonic speed @Temp
  401. if (cfg & TDC_CFG_DO_CKDIS)
  402. {
  403. DMA_ReqClkOff();
  404. PeriClk_MutDisable(PeriClk_Spi1);
  405. }
  406. }
  407. /**
  408. * @brief 打开、关闭MS1031高速时钟(空管时,节约功耗)
  409. * @param state:0关闭,1开启
  410. * @retval 无
  411. */
  412. void tdc_turn_on_off_high_clk(uint8_t state)
  413. {
  414. PeriClk_MutEnable(PeriClk_Spi1);
  415. DMA_ReqClkOn();
  416. if(state == 0)
  417. {
  418. tdc_spi_wr_d32(0x80, DFLT_WREG0 & ~(0x03 << 14));//set bit 14、15、16=0, disable the high clk
  419. }
  420. else
  421. {
  422. GpioInit(PD02_AF0, GPIO_DIG_OUT_WEAK_PUSH_NONEUP_NONEDOWN);
  423. GpioClrPins(GpioPortD, GpioBitMsk2); // reset active low
  424. us_delay(10); // 10us > tPH(30ns)
  425. GpioSetPins(GpioPortD, GpioBitMsk2); // reset release
  426. us_delay(10); // 10us > tRFS(30ns)
  427. tdc_spi_wr_d32(0x80,DFLT_WREG0);
  428. tdc_spi_wr_d32(0x81,DFLT_WREG1);
  429. tdc_spi_wr_d32(0x82,DFLT_WREG2);
  430. tdc_spi_wr_d32(0x83,DFLT_WREG3);
  431. tdc_spi_wr_d32(0x84,DFLT_WREG4);
  432. tdc_spi_wr_d32(0x85,DFLT_WREG5);
  433. tdc_spi_wr_d32(0x86,DFLT_WREG6);
  434. //读取时钟校准时,如果不加该项,状态寄存器d0中的TDC_Flood位会一直置位
  435. tdc_spi_wr_d32(0x87,0x20000000);//!!!!!!!!do not delete!!!!!!!!!
  436. tdc_spi_wr_code(0x70);
  437. }
  438. DMA_ReqClkOff();
  439. PeriClk_MutDisable(PeriClk_Spi1);
  440. }