wm_adc.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657
  1. /*****************************************************************************
  2. *
  3. * File Name : wm_adc.c
  4. *
  5. * Description: adc Driver Module
  6. *
  7. * Copyright (c) 2014 Winner Microelectronics Co., Ltd.
  8. * All rights reserved.
  9. *
  10. * Author : dave
  11. *
  12. * Date : 2014-8-15
  13. *****************************************************************************/
  14. #include <stdio.h>
  15. #include <string.h>
  16. #include <stdlib.h>
  17. #include "wm_regs.h"
  18. #include "wm_adc.h"
  19. #include "wm_dma.h"
  20. #include "wm_io.h"
  21. #include "wm_irq.h"
  22. #include "wm_mem.h"
  23. #include "wm_efuse.h"
  24. #define ATTRIBUTE_ISR __attribute__((isr))
  25. typedef struct
  26. {
  27. int poly_n;
  28. double a[3];
  29. }ST_ADC_POLYFIT_PARAM;
  30. ST_ADC_POLYFIT_PARAM _polyfit_param = {0};
  31. extern void polyfit(int n,double x[],double y[],int poly_n,double a[]);
  32. //TODO
  33. #define HR_SD_ADC_CONFIG_REG 0
  34. static int adc_offset = 0;
  35. static int *adc_dma_buffer = NULL;
  36. volatile ST_ADC gst_adc;
  37. ATTRIBUTE_ISR void ADC_IRQHandler(void)
  38. {
  39. u32 adcvalue;
  40. int reg;
  41. csi_kernel_intrpt_enter();
  42. reg = tls_reg_read32(HR_SD_ADC_INT_STATUS);
  43. if(reg & ADC_INT_MASK) //ADC中断
  44. {
  45. tls_adc_clear_irq(ADC_INT_TYPE_ADC);
  46. if(gst_adc.adc_cb)
  47. {
  48. adcvalue = tls_read_adc_result();
  49. gst_adc.adc_cb((int *)&adcvalue,1);
  50. }
  51. }
  52. if(reg & CMP_INT_MASK)
  53. {
  54. tls_adc_clear_irq(ADC_INT_TYPE_ADC_COMP);
  55. if(gst_adc.adc_bigger_cb)
  56. gst_adc.adc_bigger_cb(NULL, 0);
  57. }
  58. csi_kernel_intrpt_exit();
  59. }
  60. static void adc_dma_isr_callbk(void)
  61. {
  62. if(gst_adc.adc_dma_cb)
  63. {
  64. if (adc_dma_buffer)
  65. {
  66. gst_adc.adc_dma_cb((int *)(adc_dma_buffer), gst_adc.valuelen);
  67. }
  68. }
  69. }
  70. int adc_polyfit_init(ST_ADC_POLYFIT_PARAM *polyfit_param)
  71. {
  72. FT_ADC_CAL_ST adc_st;
  73. double x[16] = {0};
  74. double y[16] = {0};
  75. int n = 4, poly_n = 1; // or = 2
  76. int i;
  77. polyfit_param->poly_n = 0;
  78. memset(&adc_st, 0, sizeof(adc_st));
  79. tls_get_adc_cal_param(&adc_st);
  80. //dumpBuffer("adc_st",(char *)&adc_st, sizeof(adc_st));
  81. n = adc_st.valid_cnt;
  82. if(n >= 4 && n <= 8)
  83. {
  84. for(i = 0; i < n; i++)
  85. {
  86. x[i] = (double)adc_st.units[i].real_val;
  87. y[i] = (double)adc_st.units[i].ref_val;
  88. }
  89. polyfit_param->poly_n = poly_n;
  90. polyfit(n,x,y,poly_n,polyfit_param->a);
  91. }
  92. return 0;
  93. }
  94. void tls_adc_init(u8 ifusedma,u8 dmachannel)
  95. {
  96. /*ADC LDO ON here, after 90ns, ADC can work to avoid ADC affect those ADC IOs that are multiplexed GPIO input to capture irq*/
  97. u32 value = 0;
  98. value = tls_reg_read32(HR_SD_ADC_ANA_CTRL);
  99. value |= CONFIG_EN_LDO_ADC_VAL(1);
  100. tls_reg_write32(HR_SD_ADC_ANA_CTRL, value);
  101. memset(&_polyfit_param, 0, sizeof(_polyfit_param));
  102. adc_polyfit_init(&_polyfit_param);
  103. tls_reg_write32(HR_SD_ADC_CTRL, ANALOG_SWITCH_TIME_VAL(0x50)|ANALOG_INIT_TIME_VAL(0x50)|ADC_IRQ_EN_VAL(0x1));
  104. tls_irq_enable(ADC_IRQn);
  105. //注册中断和channel有关,所以需要先请求
  106. if(ifusedma)
  107. {
  108. gst_adc.dmachannel = tls_dma_request(dmachannel, TLS_DMA_FLAGS_CHANNEL_SEL(TLS_DMA_SEL_SDADC_CH0 + dmachannel) |
  109. TLS_DMA_FLAGS_HARD_MODE); //请求dma,不要直接指定,因为请求的dma可能会被别的任务使用
  110. if (gst_adc.dmachannel != 0xFF)
  111. {
  112. tls_dma_irq_register(gst_adc.dmachannel, (void(*)(void*))adc_dma_isr_callbk, NULL, TLS_DMA_IRQ_TRANSFER_DONE);
  113. }
  114. }
  115. //printf("\ndma channel = %d\n",gst_adc.dmachannel);
  116. }
  117. void tls_adc_clear_irq(int inttype)
  118. {
  119. int reg;
  120. reg = tls_reg_read32(HR_SD_ADC_INT_STATUS);
  121. if(ADC_INT_TYPE_ADC == inttype)
  122. {
  123. reg |= ADC_INT_MASK;
  124. tls_reg_write32(HR_SD_ADC_INT_STATUS, reg);
  125. }
  126. else if(ADC_INT_TYPE_ADC_COMP== inttype)
  127. {
  128. reg |= CMP_INT_MASK;
  129. tls_reg_write32(HR_SD_ADC_INT_STATUS, reg);
  130. }
  131. else if(ADC_INT_TYPE_DMA == inttype)
  132. {
  133. tls_dma_irq_clr(gst_adc.dmachannel, TLS_DMA_IRQ_TRANSFER_DONE);
  134. }
  135. }
  136. void tls_adc_irq_register(int inttype, void (*callback)(int *buf, u16 len))
  137. {
  138. if(ADC_INT_TYPE_ADC == inttype)
  139. {
  140. gst_adc.adc_cb = callback;
  141. }
  142. else if(ADC_INT_TYPE_DMA == inttype)
  143. {
  144. gst_adc.adc_dma_cb = callback;
  145. }
  146. else if(ADC_INT_TYPE_ADC_COMP == inttype)
  147. {
  148. gst_adc.adc_bigger_cb = callback;
  149. }
  150. }
  151. u32 tls_read_adc_result(void)
  152. {
  153. u32 value;
  154. u32 ret;
  155. value = tls_reg_read32(HR_SD_ADC_RESULT_REG);
  156. ret = ADC_RESULT_VAL(value);
  157. return ret;
  158. }
  159. void tls_adc_start_with_cpu(int Channel)
  160. {
  161. u32 value;
  162. /* Stop adc first */
  163. value = tls_reg_read32(HR_SD_ADC_ANA_CTRL);
  164. value |= CONFIG_PD_ADC_VAL(1);
  165. value &= ~(CONFIG_RSTN_ADC_VAL(1));
  166. value &= ~(CONFIG_ADC_CHL_SEL_MASK);
  167. value |= CONFIG_ADC_CHL_SEL(Channel);
  168. tls_reg_write32(HR_SD_ADC_ANA_CTRL, value);
  169. value = tls_reg_read32(HR_SD_ADC_ANA_CTRL);
  170. value &= ~(CONFIG_PD_ADC_VAL(1));
  171. value |= (CONFIG_RSTN_ADC_VAL(1));
  172. tls_reg_write32(HR_SD_ADC_ANA_CTRL, value);
  173. }
  174. void tls_adc_start_with_dma(int Channel, int Length)
  175. {
  176. u32 value;
  177. int len;
  178. if(Channel < 0 || Channel > 11)
  179. return;
  180. if(Length > ADC_DEST_BUFFER_SIZE)
  181. len = ADC_DEST_BUFFER_SIZE;
  182. else
  183. len = Length;
  184. gst_adc.valuelen = len;
  185. if (adc_dma_buffer)
  186. {
  187. tls_mem_free(adc_dma_buffer);
  188. adc_dma_buffer = NULL;
  189. }
  190. adc_dma_buffer = tls_mem_alloc(len*4);
  191. if (adc_dma_buffer == NULL)
  192. {
  193. //wm_printf("adc dma buffer alloc failed\r\n");
  194. return;
  195. }
  196. Channel &= 0xF;
  197. /*disable adc:set adc pd, rstn and ldo disable*/
  198. value = tls_reg_read32(HR_SD_ADC_ANA_CTRL);
  199. value |= CONFIG_PD_ADC_VAL(1);
  200. value &= ~(CONFIG_RSTN_ADC_VAL(1));
  201. tls_reg_write32(HR_SD_ADC_ANA_CTRL, value);
  202. /* Stop dma if necessary */
  203. while(DMA_CHNLCTRL_REG(gst_adc.dmachannel) & 1)
  204. {
  205. DMA_CHNLCTRL_REG(gst_adc.dmachannel) = 2;
  206. }
  207. DMA_SRCADDR_REG(gst_adc.dmachannel) = HR_SD_ADC_RESULT_REG;
  208. DMA_DESTADDR_REG(gst_adc.dmachannel) = (u32)adc_dma_buffer;
  209. DMA_SRCWRAPADDR_REG(gst_adc.dmachannel) = HR_SD_ADC_RESULT_REG;
  210. DMA_DESTWRAPADDR_REG(gst_adc.dmachannel) = (u32)adc_dma_buffer;
  211. DMA_WRAPSIZE_REG(gst_adc.dmachannel) = (len*4) << 16;
  212. /* Dest_add_inc, halfword, */
  213. DMA_CTRL_REG(gst_adc.dmachannel) = (3<<3)|(2<<5)|((len*4)<<8)|(1<<0);
  214. DMA_INTMASK_REG &= ~(0x01 << (gst_adc.dmachannel *2 + 1));
  215. DMA_CHNLCTRL_REG(gst_adc.dmachannel) = 1; /* Enable dma */
  216. /*Enable dma*/
  217. value = tls_reg_read32(HR_SD_ADC_CTRL);
  218. value |= (1<<0);
  219. tls_reg_write32(HR_SD_ADC_CTRL, value);
  220. value = tls_reg_read32(HR_SD_ADC_ANA_CTRL);
  221. value &= ~(CONFIG_ADC_CHL_SEL_MASK);
  222. value |= CONFIG_ADC_CHL_SEL(Channel);
  223. value &= ~(CONFIG_PD_ADC_VAL(1));
  224. value |= (CONFIG_RSTN_ADC_VAL(1));
  225. tls_reg_write32(HR_SD_ADC_ANA_CTRL, value); /*start adc*/
  226. }
  227. void tls_adc_stop(int ifusedma)
  228. {
  229. u32 value;
  230. tls_reg_write32(HR_SD_ADC_PGA_CTRL, 0);
  231. value = tls_reg_read32(HR_SD_ADC_ANA_CTRL);
  232. value |= CONFIG_PD_ADC_VAL(1);
  233. value &= ~(CONFIG_RSTN_ADC_VAL(1)|CONFIG_EN_LDO_ADC_VAL(1));
  234. tls_reg_write32(HR_SD_ADC_ANA_CTRL, value);
  235. /*Disable dma*/
  236. value = tls_reg_read32(HR_SD_ADC_CTRL);
  237. value &= ~(1<<0);
  238. tls_reg_write32(HR_SD_ADC_CTRL, value);
  239. /*Disable compare function and compare irq*/
  240. value = tls_reg_read32(HR_SD_ADC_CTRL);
  241. value &= ~(3<<4);
  242. tls_reg_write32(HR_SD_ADC_CTRL, value);
  243. if(ifusedma)
  244. tls_dma_free(gst_adc.dmachannel);
  245. if (adc_dma_buffer)
  246. {
  247. tls_mem_free(adc_dma_buffer);
  248. adc_dma_buffer = NULL;
  249. }
  250. }
  251. void tls_adc_config_cmp_reg(int cmp_data, int cmp_pol)
  252. {
  253. u32 value;
  254. tls_reg_write32(HR_SD_ADC_CMP_VALUE, CONFIG_ADC_INPUT_CMP_VAL(cmp_data));
  255. value = tls_reg_read32(HR_SD_ADC_CTRL);
  256. if(cmp_pol)
  257. {
  258. value |= CMP_POLAR_MASK;
  259. }
  260. else
  261. {
  262. value &= ~CMP_POLAR_MASK;
  263. }
  264. tls_reg_write32(HR_SD_ADC_CTRL, value);
  265. }
  266. void tls_adc_cmp_start(int Channel, int cmp_data, int cmp_pol)
  267. {
  268. u32 value;
  269. /* Stop adc first */
  270. value = tls_reg_read32(HR_SD_ADC_ANA_CTRL);
  271. value |= CONFIG_PD_ADC_VAL(1);
  272. value &= ~(CONFIG_RSTN_ADC_VAL(1));
  273. value |= CONFIG_ADC_CHL_SEL(Channel);
  274. tls_reg_write32(HR_SD_ADC_ANA_CTRL, value);
  275. tls_adc_config_cmp_reg(cmp_data, cmp_pol);
  276. value = tls_reg_read32(HR_SD_ADC_ANA_CTRL);
  277. value &= ~(CONFIG_PD_ADC_VAL(1));
  278. value |= (CONFIG_RSTN_ADC_VAL(1));
  279. tls_reg_write32(HR_SD_ADC_ANA_CTRL, value); /*start adc*/
  280. /*Enable compare function and compare irq*/
  281. value = tls_reg_read32(HR_SD_ADC_CTRL);
  282. value |= (3<<4);
  283. tls_reg_write32(HR_SD_ADC_CTRL, value);
  284. }
  285. void tls_adc_reference_sel(int ref)
  286. {
  287. u32 value;
  288. value = tls_reg_read32(HR_SD_ADC_PGA_CTRL);
  289. if(ADC_REFERENCE_EXTERNAL == ref)
  290. {
  291. value |= BYPASS_INNER_REF_SEL;
  292. }
  293. else if(ADC_REFERENCE_INTERNAL == ref)
  294. {
  295. value &= ~BYPASS_INNER_REF_SEL;
  296. }
  297. tls_reg_write32(HR_SD_ADC_PGA_CTRL, value);
  298. }
  299. void tls_adc_set_clk(int div)
  300. {
  301. u32 value;
  302. value = tls_reg_read32(HR_CLK_SEL_CTL);
  303. value &= ~(0xFF<<8);
  304. value |= (div&0xFF)<<8;
  305. tls_reg_write32(HR_CLK_SEL_CTL, value);
  306. value = tls_reg_read32(HR_CLK_DIV_CTL);
  307. value |= (1 << 31);
  308. tls_reg_write32(HR_CLK_DIV_CTL, value);
  309. }
  310. void tls_adc_set_pga(int gain1, int gain2)
  311. {
  312. u32 val = 0;
  313. u8 gain1times = 0;
  314. u8 gain2times = 0;
  315. switch(gain1)
  316. {
  317. case 1:
  318. gain1times = 0;
  319. break;
  320. case 16:
  321. gain1times = 1;
  322. break;
  323. case 32:
  324. gain1times = 2;
  325. break;
  326. case 64:
  327. gain1times = 3;
  328. break;
  329. case 128:
  330. gain1times = 4;
  331. break;
  332. case 256:
  333. gain1times = 5;
  334. break;
  335. default:
  336. gain1times = 0;
  337. break;
  338. }
  339. switch(gain2)
  340. {
  341. case 1:
  342. gain2times = 0;
  343. break;
  344. case 2:
  345. gain2times = 1;
  346. break;
  347. case 3:
  348. gain2times = 2;
  349. break;
  350. case 4:
  351. gain2times = 3;
  352. break;
  353. default:
  354. gain2times = 0;
  355. break;
  356. }
  357. val = tls_reg_read32(HR_SD_ADC_PGA_CTRL);
  358. val = GAIN_CTRL_PGA_VAL(gain2times)|CLK_CHOP_SEL_PGA_VAL(gain1times)|PGA_BYPASS_VAL(0)|PGA_CHOP_ENP_VAL(1)|PGA_EN_VAL(1);
  359. tls_reg_write32(HR_SD_ADC_PGA_CTRL, val);
  360. }
  361. void signedToUnsignedData(int *adcValue)
  362. {
  363. if (*adcValue &0x20000)
  364. {
  365. *adcValue = *adcValue &0x1FFFF;
  366. }
  367. else
  368. {
  369. *adcValue = *adcValue |0x20000;
  370. }
  371. }
  372. static void waitForAdcDone(void)
  373. {
  374. u32 counter = 0;
  375. u32 timeout = 10000;
  376. u32 reg = 0;
  377. /*wait for transfer success*/
  378. tls_irq_disable(ADC_IRQn);
  379. while(timeout--)
  380. {
  381. reg = tls_reg_read32(HR_SD_ADC_INT_STATUS);
  382. if (reg & ADC_INT_MASK)
  383. {
  384. counter++;
  385. tls_reg_write32(HR_SD_ADC_INT_STATUS, reg|ADC_INT_MASK);
  386. if (counter == 4)
  387. {
  388. break;
  389. }
  390. }
  391. else if(reg & CMP_INT_MASK)
  392. {
  393. counter++;
  394. tls_reg_write32(HR_SD_ADC_INT_STATUS, reg|CMP_INT_MASK);
  395. if (counter == 4)
  396. {
  397. break;
  398. }
  399. }
  400. }
  401. tls_irq_enable(ADC_IRQn);
  402. }
  403. int cal_voltage(double vol)
  404. {
  405. double y1, voltage;
  406. double vol_30mV;
  407. int average = ((int)vol >> 2) & 0xFFFF;
  408. if(_polyfit_param.poly_n == 2)
  409. {
  410. y1 = _polyfit_param.a[2]*average*average + _polyfit_param.a[1]*average + _polyfit_param.a[0];
  411. }
  412. else if(_polyfit_param.poly_n == 1)
  413. {
  414. y1 = _polyfit_param.a[1]*average + _polyfit_param.a[0];
  415. vol_30mV = ((double)300.0L - _polyfit_param.a[0]) / _polyfit_param.a[1];
  416. if(average + 170 < vol_30mV)
  417. {
  418. return 0;
  419. }
  420. else if(average < vol_30mV)
  421. {
  422. y1 = 300.0L - 200.0L*(vol_30mV - average) / 170.0L;
  423. }
  424. }
  425. else
  426. {
  427. int val = 0;
  428. if (vol < 46134) {
  429. val = 0;
  430. }
  431. else if (vol > 123405) {
  432. val = 2300;
  433. }
  434. else {
  435. val = (int)((double)(vol - 46134) / (double)(32.196));
  436. }
  437. return val;
  438. // voltage = ((double)vol - (double)adc_offset)/4.0;
  439. // voltage = 1.196 + voltage*(126363/1000.0)/1000000;
  440. // y1 = voltage*10000;
  441. }
  442. return (int)(y1/10);
  443. }
  444. u32 adc_get_offset(void)
  445. {
  446. adc_offset = 0;
  447. tls_adc_init(0, 0);
  448. tls_adc_reference_sel(ADC_REFERENCE_INTERNAL);
  449. tls_adc_start_with_cpu(CONFIG_ADC_CHL_OFFSET);
  450. tls_adc_set_pga(1,1);
  451. tls_adc_set_clk(0x28);
  452. waitForAdcDone();
  453. adc_offset = tls_read_adc_result(); //获取adc转换结果
  454. signedToUnsignedData(&adc_offset);
  455. tls_adc_stop(0);
  456. //printf("adc_offset: 0x%x\r\n", adc_offset);
  457. return adc_offset;
  458. }
  459. int adc_get_interTemp(void)
  460. {
  461. return adc_temp();
  462. }
  463. int adc_get_inputVolt(u8 channel, int* origin)
  464. {
  465. int average = 0;
  466. double voltage = 0.0;
  467. if(_polyfit_param.poly_n == 0 || (channel == 8) || (channel == 9))
  468. {
  469. adc_get_offset();
  470. }
  471. tls_adc_init(0, 0);
  472. tls_adc_reference_sel(ADC_REFERENCE_INTERNAL);
  473. tls_adc_set_pga(1,1);
  474. tls_adc_set_clk(0x28);
  475. tls_adc_start_with_cpu(channel);
  476. waitForAdcDone();
  477. average = tls_read_adc_result();
  478. signedToUnsignedData(&average);
  479. *origin = average;
  480. tls_adc_stop(0);
  481. if ((channel == 8) || (channel == 9))
  482. {
  483. voltage = ((double)average - (double)adc_offset)/4.0;
  484. voltage = voltage*(126363/1000)/1000000;
  485. }
  486. else
  487. {
  488. return cal_voltage((double)average);
  489. }
  490. average = (int)(voltage*1000);
  491. return average;
  492. }
  493. u32 adc_get_interVolt(void)
  494. {
  495. u32 voltValue;
  496. u32 value = 0;
  497. //u32 code = 0;
  498. int i = 0;
  499. adc_get_offset();
  500. tls_adc_init(0, 0);
  501. tls_adc_reference_sel(ADC_REFERENCE_INTERNAL);
  502. tls_adc_set_pga(1,1);
  503. tls_adc_set_clk(0x28);
  504. tls_adc_start_with_cpu(CONFIG_ADC_CHL_VOLT);
  505. voltValue = 0;
  506. for (i = 0;i < 10; i++)
  507. {
  508. waitForAdcDone();
  509. value = tls_read_adc_result();
  510. signedToUnsignedData((int *)&value);
  511. voltValue += value;
  512. }
  513. voltValue = voltValue/10;
  514. //code = voltValue;
  515. voltValue = voltValue;
  516. adc_offset = adc_offset;
  517. tls_adc_stop(0);
  518. voltValue = ((voltValue - adc_offset)*685/20+1200000)*2;
  519. value = voltValue - voltValue*10/100;
  520. //printf("Power voltage code:0x%x, interVolt:%d(uV)---%d.%d(V)\r\n", code, value, value/1000000, (value%1000000)/1000);
  521. return value/1000;
  522. }
  523. /**
  524. * @brief This function is used to get chip's internal work temperature
  525. *
  526. * @return chip temperature, unit: 1/1000 degree
  527. *
  528. * @note Only use to get chip's internal work temperature.
  529. */
  530. int adc_temp(void)
  531. {
  532. u32 code1 = 0, code2 = 0;
  533. u32 val = 0;
  534. int temperature = 0;
  535. tls_adc_init(0, 0);
  536. tls_adc_reference_sel(ADC_REFERENCE_INTERNAL);
  537. tls_adc_set_pga(1,4);
  538. tls_adc_start_with_cpu(CONFIG_ADC_CHL_TEMP);
  539. tls_adc_set_clk(0x28);
  540. val = tls_reg_read32(HR_SD_ADC_TEMP_CTRL);
  541. val &= ~TEMP_GAIN_MASK;
  542. val |= TEMP_GAIN_VAL(0);
  543. val |= TEMP_EN_VAL(1);
  544. val &= (~(TEMP_CAL_OFFSET_MASK));
  545. tls_reg_write32(HR_SD_ADC_TEMP_CTRL, val);
  546. waitForAdcDone();
  547. code1 = tls_read_adc_result();
  548. signedToUnsignedData((int *)&code1);
  549. val |= TEMP_CAL_OFFSET_MASK;
  550. tls_reg_write32(HR_SD_ADC_TEMP_CTRL, val);
  551. waitForAdcDone();
  552. code2 = tls_read_adc_result();
  553. signedToUnsignedData((int *)&code2);
  554. val &= ~(TEMP_EN_VAL(1));
  555. tls_reg_write32(HR_SD_ADC_TEMP_CTRL, val);
  556. tls_adc_stop(0);
  557. temperature = ((int)code1 - (int)code2);
  558. temperature = ((temperature*1000/(int)(2*2*4)-4120702)*1000/15548);
  559. return temperature;
  560. }