drv_adc.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455
  1. /******************************************************************//**
  2. * @file drv_adc.c
  3. * @brief ADC driver of RT-Thread RTOS for EFM32
  4. * COPYRIGHT (C) 2011, RT-Thread Development Team
  5. * @author onelife
  6. * @version 0.4 beta
  7. **********************************************************************
  8. * @section License
  9. * The license and distribution terms for this file may be found in the file LICENSE in this
  10. * distribution or at http://www.rt-thread.org/license/LICENSE
  11. **********************************************************************
  12. * @section Change Logs
  13. * Date Author Notes
  14. * 2011-02-21 onelife Initial creation for EFM32
  15. *********************************************************************/
  16. /******************************************************************//**
  17. * @addtogroup efm32
  18. * @{
  19. *********************************************************************/
  20. /* Includes -------------------------------------------------------------------*/
  21. #include "board.h"
  22. #include "drv_adc.h"
  23. /* Private typedef -------------------------------------------------------------*/
  24. /* Private define --------------------------------------------------------------*/
  25. /* Private macro --------------------------------------------------------------*/
  26. /* Private variables ------------------------------------------------------------*/
  27. #ifdef RT_USING_ADC0
  28. static struct rt_device adc0_device;
  29. #endif
  30. /* Private function prototypes ---------------------------------------------------*/
  31. rt_uint32_t efm32_adc_calibration(
  32. ADC_TypeDef *adc,
  33. ADC_Ref_TypeDef ref,
  34. ADC_SingleInput_TypeDef input);
  35. /* Private functions ------------------------------------------------------------*/
  36. /******************************************************************//**
  37. * @brief
  38. * Initialize ADC device
  39. *
  40. * @details
  41. *
  42. * @note
  43. *
  44. * @param[in] dev
  45. * Pointer to device descriptor
  46. *
  47. * @return
  48. * Error code
  49. *********************************************************************/
  50. static rt_err_t rt_adc_init(rt_device_t dev)
  51. {
  52. RT_ASSERT(dev != RT_NULL);
  53. rt_uint32_t temp;
  54. struct efm32_adc_device_t *adc;
  55. adc = (struct efm32_adc_device_t *)(dev->user_data);
  56. temp = efm32_adc_calibration(adc->adc_device, ADC_INIT_REF, ADC_INIT_CH);
  57. #ifdef RT_ADC_DEBUG
  58. rt_kprintf("adc->CAL = %x\n", temp);
  59. #endif
  60. return RT_EOK;
  61. }
  62. /******************************************************************//**
  63. * @brief
  64. * Configure ADC device
  65. *
  66. * @details
  67. *
  68. * @note
  69. *
  70. * @param[in] dev
  71. * Pointer to device descriptor
  72. *
  73. * @param[in] cmd
  74. * ADC control command
  75. *
  76. * @param[in] args
  77. * Arguments
  78. *
  79. * @return
  80. * Error code
  81. *********************************************************************/
  82. static rt_err_t rt_adc_control(
  83. rt_device_t dev,
  84. rt_uint8_t cmd,
  85. void *args)
  86. {
  87. RT_ASSERT(dev != RT_NULL);
  88. struct efm32_adc_device_t *adc;
  89. adc = (struct efm32_adc_device_t *)(dev->user_data);
  90. switch (cmd)
  91. {
  92. case RT_DEVICE_CTRL_SUSPEND:
  93. /* Suspend device */
  94. dev->flag |= RT_DEVICE_FLAG_SUSPENDED;
  95. adc->adc_device->CMD = ADC_CMD_SINGLESTOP | ADC_CMD_SCANSTOP;
  96. break;
  97. case RT_DEVICE_CTRL_RESUME:
  98. /* Resume device */
  99. dev->flag &= ~RT_DEVICE_FLAG_SUSPENDED;
  100. switch (adc->mode)
  101. {
  102. case ADC_MODE_SINGLE:
  103. ADC_Start(adc->adc_device, adcStartSingle);
  104. break;
  105. case ADC_MODE_SCAN:
  106. ADC_Start(adc->adc_device, adcStartScan);
  107. break;
  108. case ADC_MODE_TAILGATE:
  109. ADC_Start(adc->adc_device, adcStartScanAndSingle);
  110. break;
  111. default:
  112. return -RT_ERROR;
  113. }
  114. break;
  115. case RT_DEVICE_CTRL_ADC_MODE:
  116. {
  117. /* change device setting */
  118. struct efm32_adc_control_t *control;
  119. control = (struct efm32_adc_control_t *)args;
  120. switch (control->mode)
  121. {
  122. case ADC_MODE_SINGLE:
  123. ADC_InitSingle(adc->adc_device, control->singleInit);
  124. break;
  125. case ADC_MODE_SCAN:
  126. ADC_InitScan(adc->adc_device, control->scanInit);
  127. break;
  128. case ADC_MODE_TAILGATE:
  129. ADC_InitSingle(adc->adc_device, control->singleInit);
  130. ADC_InitScan(adc->adc_device, control->scanInit);
  131. break;
  132. default:
  133. return -RT_ERROR;
  134. }
  135. adc->mode = control->mode;
  136. }
  137. break;
  138. case RT_DEVICE_CTRL_ADC_RESULT:
  139. switch (adc->mode)
  140. {
  141. case ADC_MODE_SINGLE:
  142. while (adc->adc_device->STATUS & ADC_STATUS_SINGLEACT);
  143. *((rt_uint32_t *)args) = ADC_DataSingleGet(adc->adc_device);
  144. break;
  145. case ADC_MODE_SCAN:
  146. while (adc->adc_device->STATUS & ADC_STATUS_SCANACT);
  147. *((rt_uint32_t *)args) = ADC_DataScanGet(adc->adc_device);
  148. break;
  149. case ADC_MODE_TAILGATE:
  150. while (adc->adc_device->STATUS & ADC_STATUS_SCANACT);
  151. *((rt_uint32_t *)args) = ADC_DataScanGet(adc->adc_device);
  152. while (adc->adc_device->STATUS & ADC_STATUS_SINGLEACT);
  153. *((rt_uint32_t *)args + 1) = ADC_DataSingleGet(adc->adc_device);
  154. break;
  155. default:
  156. return -RT_ERROR;
  157. }
  158. break;
  159. }
  160. return RT_EOK;
  161. }
  162. /******************************************************************//**
  163. * @brief
  164. * Register ADC device
  165. *
  166. * @details
  167. *
  168. * @note
  169. *
  170. * @param[in] device
  171. * Pointer to device descriptor
  172. *
  173. * @param[in] name
  174. * Device name
  175. *
  176. * @param[in] flag
  177. * Configuration flags
  178. *
  179. * @param[in] adc
  180. * Pointer to ADC device descriptor
  181. *
  182. * @return
  183. * Error code
  184. *********************************************************************/
  185. rt_err_t rt_hw_adc_register(
  186. rt_device_t device,
  187. const char *name,
  188. rt_uint32_t flag,
  189. struct efm32_adc_device_t *adc)
  190. {
  191. RT_ASSERT(device != RT_NULL);
  192. device->type = RT_Device_Class_Char; /* fixme: should be adc type */
  193. device->rx_indicate = RT_NULL;
  194. device->tx_complete = RT_NULL;
  195. device->init = rt_adc_init;
  196. device->open = RT_NULL;
  197. device->close = RT_NULL;
  198. device->read = RT_NULL;
  199. device->write = RT_NULL;
  200. device->control = rt_adc_control;
  201. device->user_data = adc;
  202. /* register a character device */
  203. return rt_device_register(device, name, flag);
  204. }
  205. /******************************************************************//**
  206. * @brief
  207. * Initialize all ADC module related hardware and register ADC device to kernel
  208. *
  209. * @details
  210. *
  211. * @note
  212. *
  213. *********************************************************************/
  214. void rt_hw_adc_init(void)
  215. {
  216. struct efm32_adc_device_t *adc;
  217. ADC_Init_TypeDef init = ADC_INIT_DEFAULT;
  218. // TODO: Fixed oversampling rate?
  219. init.ovsRateSel = adcOvsRateSel4096;
  220. init.timebase = ADC_TimebaseCalc(0);
  221. init.prescale = ADC_PrescaleCalc(ADC_CONVERT_FREQUENCY, 0);
  222. #ifdef RT_USING_ADC0
  223. adc = rt_malloc(sizeof(struct efm32_adc_device_t));
  224. if (adc == RT_NULL)
  225. {
  226. #ifdef RT_ADC_DEBUG
  227. rt_kprintf("no memory for ADC driver\n");
  228. #endif
  229. return;
  230. }
  231. adc->adc_device = ADC0;
  232. adc->mode = ADC_MODE_SINGLE;
  233. /* Enable clock for ADCn module */
  234. CMU_ClockEnable(cmuClock_ADC0, true);
  235. /* Reset */
  236. ADC_Reset(ADC0);
  237. /* Configure ADC */
  238. ADC_Init(adc->adc_device, &init);
  239. rt_hw_adc_register(&adc0_device, RT_ADC0_NAME, EFM32_NO_DATA, adc);
  240. #endif
  241. }
  242. /***************************************************************************//**
  243. * @brief
  244. * Calibrate offset and gain for the specified reference.
  245. * Supports currently only single ended gain calibration.
  246. * Could easily be expanded to support differential gain calibration.
  247. *
  248. * @details
  249. * The offset calibration routine measures 0 V with the ADC, and adjust
  250. * the calibration register until the converted value equals 0.
  251. * The gain calibration routine needs an external reference voltage equal
  252. * to the top value for the selected reference. For example if the 2.5 V
  253. * reference is to be calibrated, the external supply must also equal 2.5V.
  254. *
  255. * @param[in] adc
  256. * Pointer to ADC peripheral register block.
  257. *
  258. * @param[in] ref
  259. * Reference used during calibration. Can be both external and internal
  260. * references.
  261. *
  262. * @param[in] input
  263. * Input channel used during calibration.
  264. *
  265. * @return
  266. * The final value of the calibration register, note that the calibration
  267. * register gets updated with this value during the calibration.
  268. * No need to load the calibration values after the function returns.
  269. ******************************************************************************/
  270. rt_uint32_t efm32_adc_calibration(
  271. ADC_TypeDef *adc,
  272. ADC_Ref_TypeDef ref,
  273. ADC_SingleInput_TypeDef input)
  274. {
  275. rt_uint32_t cal;
  276. rt_int32_t sample;
  277. rt_int8_t high, mid, low, tmp;
  278. ADC_InitSingle_TypeDef singleInit = ADC_INITSINGLE_DEFAULT;
  279. /* Init for single conversion use, measure diff 0 with selected reference. */
  280. singleInit.reference = ref;
  281. singleInit.input = adcSingleInpDiff0;
  282. singleInit.acqTime = adcAcqTime32;
  283. singleInit.diff = true;
  284. /* Enable oversampling rate */
  285. singleInit.resolution = adcResOVS;
  286. ADC_InitSingle(adc, &singleInit);
  287. /* ADC is now set up for offset calibration */
  288. /* Offset calibration register is a 7 bit signed 2's complement value. */
  289. /* Use unsigned indexes for binary search, and convert when calibration */
  290. /* register is written to. */
  291. high = 63;
  292. low = -64;
  293. /* Do binary search for offset calibration*/
  294. while (low < high)
  295. {
  296. /* Calculate midpoint */
  297. mid = low + (high - low) / 2;
  298. /* Midpoint is converted to 2's complement and written to both scan and */
  299. /* single calibration registers */
  300. cal = adc->CAL & ~(_ADC_CAL_SINGLEOFFSET_MASK | _ADC_CAL_SCANOFFSET_MASK);
  301. tmp = mid < 0 ? (mid & 0x3F ^ 0x3F | 0x40) + 1 : mid;
  302. cal |= tmp << _ADC_CAL_SINGLEOFFSET_SHIFT;
  303. cal |= tmp << _ADC_CAL_SCANOFFSET_SHIFT;
  304. #ifdef RT_ADC_DEBUG
  305. rt_kprintf("adc->CAL = %x, cal = %x, tmp = %x\n", adc->CAL, cal, tmp);
  306. #endif
  307. adc->CAL = cal;
  308. /* Do a conversion */
  309. ADC_Start(adc, adcStartSingle);
  310. /* Wait while conversion is active */
  311. while (adc->STATUS & ADC_STATUS_SINGLEACT) ;
  312. /* Get ADC result */
  313. sample = ADC_DataSingleGet(adc);
  314. /* Check result and decide in which part of to repeat search */
  315. /* Calibration register has negative effect on result */
  316. if (sample < 0)
  317. {
  318. /* Repeat search in bottom half. */
  319. high = mid;
  320. }
  321. else if (sample > 0)
  322. {
  323. /* Repeat search in top half. */
  324. low = mid + 1;
  325. }
  326. else
  327. {
  328. /* Found it, exit while loop */
  329. break;
  330. }
  331. }
  332. #ifdef RT_ADC_DEBUG
  333. rt_kprintf("adc->CAL = %x\n", adc->CAL);
  334. #endif
  335. /* Now do gain calibration, only input and diff settings needs to be changed */
  336. adc->SINGLECTRL &= ~(_ADC_SINGLECTRL_INPUTSEL_MASK | _ADC_SINGLECTRL_DIFF_MASK);
  337. adc->SINGLECTRL |= (input << _ADC_SINGLECTRL_INPUTSEL_SHIFT);
  338. adc->SINGLECTRL |= (false << _ADC_SINGLECTRL_DIFF_SHIFT);
  339. /* ADC is now set up for gain calibration */
  340. /* Gain calibration register is a 7 bit unsigned value. */
  341. high = 127;
  342. low = 0;
  343. /* Do binary search for gain calibration */
  344. while (low < high)
  345. {
  346. /* Calculate midpoint and write to calibration register */
  347. mid = low + (high - low) / 2;
  348. /* Midpoint is converted to 2's complement */
  349. cal = adc->CAL & ~(_ADC_CAL_SINGLEGAIN_MASK | _ADC_CAL_SCANGAIN_MASK);
  350. cal |= mid << _ADC_CAL_SINGLEGAIN_SHIFT;
  351. cal |= mid << _ADC_CAL_SCANGAIN_SHIFT;
  352. #ifdef RT_ADC_DEBUG
  353. rt_kprintf("adc->CAL = %x, cal = %x, mid = %x\n", adc->CAL, cal, mid);
  354. #endif
  355. adc->CAL = cal;
  356. /* Do a conversion */
  357. ADC_Start(adc, adcStartSingle);
  358. /* Wait while conversion is active */
  359. while (adc->STATUS & ADC_STATUS_SINGLEACT) ;
  360. /* Get ADC result */
  361. sample = ADC_DataSingleGet(adc);
  362. /* Check result and decide in which part to repeat search */
  363. /* Compare with a value atleast one LSB's less than top to avoid overshooting */
  364. /* Since oversampling is used, the result is 16 bits, but a couple of lsb's */
  365. /* applies to the 12 bit result value, if 0xffe is the top value in 12 bit, this */
  366. /* is in turn 0xffe0 in the 16 bit result. */
  367. /* Calibration register has positive effect on result */
  368. if (sample > 0xffd0)
  369. {
  370. /* Repeat search in bottom half. */
  371. high = mid;
  372. }
  373. else if (sample < 0xffd0)
  374. {
  375. /* Repeat search in top half. */
  376. low = mid + 1;
  377. }
  378. else
  379. {
  380. /* Found it, exit while loop */
  381. break;
  382. }
  383. }
  384. #ifdef RT_ADC_DEBUG
  385. rt_kprintf("adc->CAL = %x\n", adc->CAL);
  386. #endif
  387. return adc->CAL;
  388. }
  389. /******************************************************************//**
  390. * @}
  391. *********************************************************************/