drv_adc.c 13 KB

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