wm_adc.h 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354
  1. /*****************************************************************************
  2. *
  3. * File Name : wm_adc.h
  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. #ifndef WM_ADC_H
  15. #define WM_ADC_H
  16. #include "wm_type_def.h"
  17. #define ADC_DEST_BUFFER_SIZE 16383//以字为单位
  18. /*ADC Result*/
  19. #define ADC_RESULT_MASK (0x3FFFF)
  20. #define ADC_RESULT_VAL(n) ((n)&ADC_RESULT_MASK)
  21. /*ADC_ANALOG_CTRL*/
  22. #define CONFIG_ADC_CHL_SEL_MASK (0xF<<8)
  23. #define CONFIG_ADC_CHL_SEL(n) ((n)<<8)
  24. #define CONFIG_PD_ADC_MASK (0x1<<2)
  25. #define CONFIG_PD_ADC_VAL(n) ((n)<<2) /*1:pd adc, 0: normal work*/
  26. #define CONFIG_RSTN_ADC_MASK (0x1<<1)
  27. #define CONFIG_RSTN_ADC_VAL(n) ((n)<<1) /*1:normal work, 0:adc reset*/
  28. #define CONFIG_EN_LDO_ADC_MASK (0x1<<0)
  29. #define CONFIG_EN_LDO_ADC_VAL(n) ((n)<<0) /*1:ldo work, 0: ldo shutdown*/
  30. /*PGA_CTRL*/
  31. #define CLK_CHOP_SEL_PGA_MASK (0x7<<4)
  32. #define CLK_CHOP_SEL_PGA_VAL(n) ((n)<<4)
  33. #define GAIN_CTRL_PGA_MASK (0x3<<7)
  34. #define GAIN_CTRL_PGA_VAL(n) ((n)<<7)
  35. #define PGA_BYPASS_MASK (0x1<<3)
  36. #define PGA_BYPASS_VAL(n) ((n)<<3) /*1:bypass pga, 0:use pga*/
  37. #define BYPASS_INNER_REF_SEL (0x1<<2) /*Internal or external reference select*/
  38. #define PGA_CHOP_ENP_MASK (0x1<<1)
  39. #define PGA_CHOP_ENP_VAL(n) ((n)<<1) /*1: enable chop, 0: disable chop*/
  40. #define PGA_EN_MASK (0x1<<0)
  41. #define PGA_EN_VAL(n) ((n)<<0) /*1: enable pga, 0: disable pga*/
  42. /*Temperature Control*/
  43. #define TEMP_GAIN_MASK (0x3<<4)
  44. #define TEMP_GAIN_VAL(n) ((n)<<4)
  45. #define TEMP_CAL_OFFSET_MASK (0x1<<1)
  46. #define TEMP_EN_MASK (0x1<<0)
  47. #define TEMP_EN_VAL(n) ((n)<<0) /*1: enable temperature, 0: disable temperature*/
  48. /*ADC CTRL*/
  49. #define ANALOG_SWITCH_TIME_MASK (0x3FF<<20)
  50. #define ANALOG_SWITCH_TIME_VAL(n) (((n)&0x3FF)<<20)
  51. #define ANALOG_INIT_TIME_MASK (0x3FF<<8)
  52. #define ANALOG_INIT_TIME_VAL(n) (((n)&0x3FF)<<8)
  53. #define CMP_POLAR_MASK (0x1<<6)
  54. #define CMP_IRQ_EN_MASK (0x1<<5)
  55. #define CMP_IRQ_EN_VAL(n) ((n)<<5) /*1: enable cmp irq, 0: disable cmp irq*/
  56. #define CMP_EN_MASK (0x1<<4)
  57. #define CMP_EN_VAL(n) ((n)<<4) /*1: enable cmp function, 0: disable cmp function*/
  58. #define ADC_IRQ_EN_MASK (0x1<<1)
  59. #define ADC_IRQ_EN_VAL(n) ((n)<<1) /*1:enable adc transfer irq, 0: disable*/
  60. #define ADC_DMA_EN_MASK (0x1<<0)
  61. #define ADC_DMA_EN_VAL(n) ((n)<<0) /*1:enable adc dma, 0: disable*/
  62. /*ADC IRQ Status*/
  63. #define CMP_INT_MASK (0x1<<1)
  64. #define ADC_INT_MASK (0x1<<0)
  65. /*CMP Value*/
  66. #define CONFIG_ADC_INPUT_CMP_VAL(n) ((n)&0x3FFFF)
  67. /*ADC Channel*/
  68. #define CONFIG_ADC_CHL_OFFSET (0x0E)
  69. #define CONFIG_ADC_CHL_VOLT (0x0D)
  70. #define CONFIG_ADC_CHL_TEMP (0x0C)
  71. #define ADC_INT_TYPE_ADC 0
  72. #define ADC_INT_TYPE_DMA 1
  73. #define ADC_INT_TYPE_ADC_COMP 2
  74. #define ADC_REFERENCE_EXTERNAL 0 //外部参考
  75. #define ADC_REFERENCE_INTERNAL 1 //内部参考
  76. typedef struct adc_st{
  77. u8 dmachannel;
  78. void (*adc_cb)(int *buf, u16 len);
  79. void (*adc_bigger_cb)(int *buf, u16 len);
  80. void (*adc_dma_cb)(int *buf,u16 len);
  81. u16 valuelen; /*dma 采样数据长度*/
  82. u16 offset;
  83. }ST_ADC;
  84. /**
  85. * @defgroup Driver_APIs Driver APIs
  86. * @brief Driver APIs
  87. */
  88. /**
  89. * @addtogroup Driver_APIs
  90. * @{
  91. */
  92. /**
  93. * @defgroup ADC_Driver_APIs ADC Driver APIs
  94. * @brief ADC driver APIs
  95. */
  96. /**
  97. * @addtogroup ADC_Driver_APIs
  98. * @{
  99. */
  100. /**
  101. * @brief This function is used to init adc.
  102. *
  103. * @param[in] ifusedma if use dma
  104. * @param[in] dmachannel dma channel
  105. *
  106. * @return None
  107. *
  108. * @note None
  109. */
  110. void tls_adc_init(u8 ifusedma,u8 dmachannel);
  111. /**
  112. * @brief This function is used to register interrupt callback function.
  113. *
  114. * @param[in] inttype interrupt type:
  115. * ADC_INT_TYPE_ADC adc interrupt,user get adc result from the callback function.
  116. * ADC_INT_TYPE_DMA dma interrupt,dma transfer the adc result to the user's buffer.
  117. * @param[in] callback interrupt callback function.
  118. *
  119. * @return None
  120. *
  121. * @note None
  122. */
  123. void tls_adc_irq_register(int inttype, void (*callback)(int *buf, u16 len));
  124. /**
  125. * @brief This function is used to clear the interrupt source.
  126. *
  127. * @param[in] inttype interrupt type:
  128. * ADC_INT_TYPE_ADC adc interrupt,user get adc result from the callback function.
  129. * ADC_INT_TYPE_DMA dma interrupt,dma transfer the adc result to the user's buffer.
  130. *
  131. * @return None
  132. *
  133. * @note None
  134. */
  135. void tls_adc_clear_irq(int inttype);
  136. /**
  137. * @brief This function is used to register interrupt callback function.
  138. *
  139. * @param[in] Channel adc channel,from 0 to 3 is single input;4 and 5 is differential input.
  140. * @param[in] Length byte data length,is an integer multiple of half word,need <= 0x500
  141. *
  142. * @return None
  143. *
  144. * @note None
  145. */
  146. void tls_adc_start_with_dma(int Channel, int Length);
  147. /**
  148. * @brief This function is used to start adc.
  149. *
  150. * @param[in] Channel adc channel,from 0 to 3 is single input;4 and 5 is differential input.
  151. *
  152. * @return None
  153. *
  154. * @note None
  155. */
  156. void tls_adc_start_with_cpu(int Channel);
  157. /**
  158. * @brief This function is used to read adc result.
  159. *
  160. * @param[in] None
  161. *
  162. * @retval adc result
  163. *
  164. * @note None
  165. */
  166. u32 tls_read_adc_result(void);
  167. /**
  168. * @brief This function is used to stop the adc.
  169. *
  170. * @param[in] ifusedma if use dma
  171. *
  172. * @return None
  173. *
  174. * @note None
  175. */
  176. void tls_adc_stop(int ifusedma);
  177. /**
  178. * @brief This function is used to config adc bigger register.
  179. *
  180. * @param[in] cmp_data compare data
  181. * @param[in] cmp_pol compare pol
  182. *
  183. * @return None
  184. *
  185. * @note None
  186. */
  187. void tls_adc_config_cmp_reg(int cmp_data, int cmp_pol);
  188. /**
  189. * @brief This function is used to set adc reference source.
  190. *
  191. * @param[in] ref ADC_REFERENCE_EXTERNAL,ADC_REFERENCE_INTERNAL
  192. *
  193. * @return None
  194. *
  195. * @note None
  196. */
  197. void tls_adc_reference_sel(int ref);
  198. /**
  199. * @brief This function is used to read internal temperature.
  200. *
  201. * @param[in] None
  202. *
  203. * @retval temperature
  204. *
  205. * @note None
  206. */
  207. int adc_get_interTemp(void);
  208. /**
  209. * @brief This function is used to read input voltage.
  210. *
  211. * @param[in] channel adc channel,from 0 to 3 is single input;8 and 9 is differential input.
  212. *
  213. * @retval voltage unit:mV
  214. *
  215. * @note None
  216. */
  217. int adc_get_inputVolt(u8 channel, int* origin);
  218. /**
  219. * @brief This function is used to read internal voltage.
  220. *
  221. * @param[in] None
  222. *
  223. * @retval voltage (mV)
  224. *
  225. * @note None
  226. */
  227. u32 adc_get_interVolt(void);
  228. /**
  229. * @brief This function is used to read temperature.
  230. *
  231. * @param[in] None
  232. *
  233. * @retval temperature
  234. *
  235. * @note None
  236. */
  237. int adc_temp(void);
  238. /**
  239. * @brief This function is used to calibrate adc single-end voltage. offset after FT or multipoint calibration.
  240. *
  241. * @param[in] chan: adc calibration channel to be used, ADC channel 0,1,2,3
  242. * @param[in] refvoltage: calibration reference voltage to be used, unit:mV
  243. * input range[100,2300)mV, suggest reference voltage[500,2000]mV
  244. *
  245. * @return 0: success, < 0: failure
  246. *
  247. * @note After FT calibration or mulitpoint calibration, adc curve is y=ax+b,
  248. * y is real voltage(unit:mV), x is adc sample data
  249. * a and b is the coefficient. This fuction only used to revise b value.
  250. */
  251. int adc_offset_calibration(int chan, int refvoltage);
  252. /**
  253. * @brief This function can used to calibrate adc coefficient if not calibrated,
  254. * or adc offset after FT or multipoint calibration.
  255. *
  256. * @param[in] chanused: bitmap, specified calibration channel,only bit0-3 can be used
  257. * @param[in] refvoltage[]: array, calibration reference voltage to be used, unit:mV
  258. * refvoltage keep the same position with chan bitmap
  259. * input range[100,2300)mV, suggest reference voltage[500,2000]mV
  260. *
  261. * @return 0: success, < 0: failure
  262. *
  263. * @note 1)Adc curve is y=ax+b,y is real voltage(unit:mV), x is adc sample data.
  264. * After calibration, we can get a and b, or only update b.
  265. * 2) Only used single-end adc
  266. * 3) For example, use chan 0,1,3, and refvoltage 500,1000,2000,
  267. * then chanused is 0xB, refvoltage[] value is {500,1000,0, 2000};
  268. */
  269. int adc_multipoint_calibration(int chanused, int refvoltage[]);
  270. /**
  271. * @}
  272. */
  273. /**
  274. * @}
  275. */
  276. void tls_adc_enable_calibration_buffer_offset(void);
  277. void tls_adc_voltage_start_with_cpu(void);
  278. void tls_adc_temp_offset_with_cpu(u8 calTemp12);
  279. void tls_adc_voltage_start_with_dma(int Length);
  280. void tls_adc_set_clk(int div);
  281. void signedToUnsignedData(int *adcValue);
  282. void tls_adc_buffer_bypass_set(u8 isset);
  283. void tls_adc_cmp_start(int Channel, int cmp_data, int cmp_pol);
  284. u32 adc_get_offset(void);
  285. void tls_adc_set_pga(int gain1, int gain2);
  286. int cal_voltage(double vol);
  287. #endif