1
0

drv_adc.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834
  1. /***************************************************************************//**
  2. * @file drv_adc.c
  3. * @brief ADC driver of RT-Thread RTOS for EFM32
  4. * COPYRIGHT (C) 2012, RT-Thread Development Team
  5. * @author onelife
  6. * @version 1.0
  7. *******************************************************************************
  8. * @section License
  9. * The license and distribution terms for this file may be found in the file
  10. * LICENSE in this 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. * 2011-07-14 onelife Add multiple channels support for scan mode
  16. ******************************************************************************/
  17. /***************************************************************************//**
  18. * @addtogroup efm32
  19. * @{
  20. ******************************************************************************/
  21. /* Includes ------------------------------------------------------------------*/
  22. #include "board.h"
  23. #include "drv_adc.h"
  24. #if defined(RT_USING_ADC0)
  25. /* Private typedef -----------------------------------------------------------*/
  26. /* Private define ------------------------------------------------------------*/
  27. /* Private macro -------------------------------------------------------------*/
  28. #ifdef RT_ADC_DEBUG
  29. #define adc_debug(format,args...) rt_kprintf(format, ##args)
  30. #else
  31. #define adc_debug(format,args...)
  32. #endif
  33. /* Private variables ---------------------------------------------------------*/
  34. #ifdef RT_USING_ADC0
  35. static struct rt_device adc0_device;
  36. #endif
  37. static rt_uint32_t adcErrataShift = 0;
  38. /* Private function prototypes -----------------------------------------------*/
  39. /* Private functions ---------------------------------------------------------*/
  40. /***************************************************************************//**
  41. * @brief
  42. * Calibrate offset and gain for the specified reference.
  43. * Supports currently only single ended gain calibration.
  44. * Could easily be expanded to support differential gain calibration.
  45. *
  46. * @details
  47. * The offset calibration routine measures 0 V with the ADC, and adjust
  48. * the calibration register until the converted value equals 0.
  49. * The gain calibration routine needs an external reference voltage equal
  50. * to the top value for the selected reference. For example if the 2.5 V
  51. * reference is to be calibrated, the external supply must also equal 2.5V.
  52. *
  53. * @param[in] adc
  54. * Pointer to ADC peripheral register block.
  55. *
  56. * @param[in] ref
  57. * Reference used during calibration. Can be both external and internal
  58. * references.
  59. *
  60. * @param[in] input
  61. * Input channel used during calibration.
  62. *
  63. * @return
  64. * The final value of the calibration register, note that the calibration
  65. * register gets updated with this value during the calibration.
  66. * No need to load the calibration values after the function returns.
  67. ******************************************************************************/
  68. rt_uint32_t efm32_adc_calibration(
  69. ADC_TypeDef *adc,
  70. ADC_Ref_TypeDef ref,
  71. ADC_SingleInput_TypeDef input)
  72. {
  73. rt_uint32_t cal;
  74. rt_int32_t sample;
  75. rt_int8_t high, mid, low, tmp;
  76. ADC_InitSingle_TypeDef singleInit = ADC_INITSINGLE_DEFAULT;
  77. /* Init for single conversion use, measure diff 0 with selected reference. */
  78. singleInit.reference = ref;
  79. singleInit.input = adcSingleInpDiff0;
  80. singleInit.acqTime = adcAcqTime32;
  81. singleInit.diff = true;
  82. /* Enable oversampling rate */
  83. singleInit.resolution = adcResOVS;
  84. ADC_InitSingle(adc, &singleInit);
  85. /* ADC is now set up for offset calibration */
  86. /* Offset calibration register is a 7 bit signed 2's complement value. */
  87. /* Use unsigned indexes for binary search, and convert when calibration */
  88. /* register is written to. */
  89. high = 63;
  90. low = -64;
  91. /* Do binary search for offset calibration*/
  92. while (low < high)
  93. {
  94. /* Calculate midpoint */
  95. mid = low + (high - low) / 2;
  96. /* Midpoint is converted to 2's complement and written to both scan and */
  97. /* single calibration registers */
  98. cal = adc->CAL & ~(_ADC_CAL_SINGLEOFFSET_MASK | _ADC_CAL_SCANOFFSET_MASK);
  99. tmp = mid < 0 ? (((mid & 0x3F) ^ 0x3F) | 0x40) + 1 : mid;
  100. cal |= tmp << _ADC_CAL_SINGLEOFFSET_SHIFT;
  101. cal |= tmp << _ADC_CAL_SCANOFFSET_SHIFT;
  102. adc_debug("adc->CAL = %x, cal = %x, tmp = %x\n", adc->CAL, cal, tmp);
  103. adc->CAL = cal;
  104. /* Do a conversion */
  105. ADC_Start(adc, adcStartSingle);
  106. /* Wait while conversion is active */
  107. while (adc->STATUS & ADC_STATUS_SINGLEACT) ;
  108. /* Get ADC result */
  109. sample = ADC_DataSingleGet(adc);
  110. /* Check result and decide in which part of to repeat search */
  111. /* Calibration register has negative effect on result */
  112. if (sample < 0)
  113. {
  114. /* Repeat search in bottom half. */
  115. high = mid;
  116. }
  117. else if (sample > 0)
  118. {
  119. /* Repeat search in top half. */
  120. low = mid + 1;
  121. }
  122. else
  123. {
  124. /* Found it, exit while loop */
  125. break;
  126. }
  127. }
  128. adc_debug("adc->CAL = %x\n", adc->CAL);
  129. /* Now do gain calibration, only input and diff settings needs to be changed */
  130. adc->SINGLECTRL &= ~(_ADC_SINGLECTRL_INPUTSEL_MASK | _ADC_SINGLECTRL_DIFF_MASK);
  131. adc->SINGLECTRL |= (input << _ADC_SINGLECTRL_INPUTSEL_SHIFT);
  132. adc->SINGLECTRL |= (false << _ADC_SINGLECTRL_DIFF_SHIFT);
  133. /* ADC is now set up for gain calibration */
  134. /* Gain calibration register is a 7 bit unsigned value. */
  135. high = 127;
  136. low = 0;
  137. /* Do binary search for gain calibration */
  138. while (low < high)
  139. {
  140. /* Calculate midpoint and write to calibration register */
  141. mid = low + (high - low) / 2;
  142. /* Midpoint is converted to 2's complement */
  143. cal = adc->CAL & ~(_ADC_CAL_SINGLEGAIN_MASK | _ADC_CAL_SCANGAIN_MASK);
  144. cal |= mid << _ADC_CAL_SINGLEGAIN_SHIFT;
  145. cal |= mid << _ADC_CAL_SCANGAIN_SHIFT;
  146. adc_debug("adc->CAL = %x, cal = %x, mid = %x\n", adc->CAL, cal, mid);
  147. adc->CAL = cal;
  148. /* Do a conversion */
  149. ADC_Start(adc, adcStartSingle);
  150. /* Wait while conversion is active */
  151. while (adc->STATUS & ADC_STATUS_SINGLEACT) ;
  152. /* Get ADC result */
  153. sample = ADC_DataSingleGet(adc);
  154. /* Check result and decide in which part to repeat search */
  155. /* Compare with a value atleast one LSB's less than top to avoid overshooting */
  156. /* Since oversampling is used, the result is 16 bits, but a couple of lsb's */
  157. /* applies to the 12 bit result value, if 0xffe is the top value in 12 bit, this */
  158. /* is in turn 0xffe0 in the 16 bit result. */
  159. /* Calibration register has positive effect on result */
  160. if (sample > 0xffd0)
  161. {
  162. /* Repeat search in bottom half. */
  163. high = mid;
  164. }
  165. else if (sample < 0xffd0)
  166. {
  167. /* Repeat search in top half. */
  168. low = mid + 1;
  169. }
  170. else
  171. {
  172. /* Found it, exit while loop */
  173. break;
  174. }
  175. }
  176. adc_debug("adc->CAL = %x\n", adc->CAL);
  177. return adc->CAL;
  178. }
  179. /***************************************************************************//**
  180. * @brief
  181. * Configure DMA for ADC
  182. *
  183. * @details
  184. *
  185. * @note
  186. *
  187. * @param[in] adc_device
  188. * Pointer to ADC registers base address
  189. *
  190. * @param[in] mode
  191. * ADC mode
  192. *
  193. * @param[in] channel
  194. * DMA channel
  195. ******************************************************************************/
  196. void efm32_adc_cfg_dma(
  197. ADC_TypeDef *adc_device,
  198. rt_uint8_t mode,
  199. rt_uint8_t channel)
  200. {
  201. DMA_CfgChannel_TypeDef chnlCfg;
  202. DMA_CfgDescr_TypeDef descrCfg;
  203. if (channel == (rt_uint8_t)EFM32_NO_DMA)
  204. {
  205. return;
  206. }
  207. /* Set up DMA channel */
  208. chnlCfg.highPri = false;
  209. chnlCfg.enableInt = false;
  210. if (adc_device == ADC0)
  211. {
  212. switch (mode & ADC_MASK_MODE)
  213. {
  214. case ADC_MODE_SINGLE:
  215. chnlCfg.select = DMAREQ_ADC0_SINGLE;
  216. break;
  217. case ADC_MODE_SCAN:
  218. chnlCfg.select = DMAREQ_ADC0_SCAN;
  219. break;
  220. default:
  221. return;
  222. }
  223. }
  224. else
  225. {
  226. // TODO: Any other channel?
  227. return;
  228. }
  229. chnlCfg.cb = RT_NULL;
  230. DMA_CfgChannel((rt_uint32_t)channel, &chnlCfg);
  231. /* Setting up DMA channel descriptor */
  232. descrCfg.dstInc = dmaDataInc4;
  233. descrCfg.srcInc = dmaDataIncNone;
  234. descrCfg.size = dmaDataSize4;
  235. descrCfg.arbRate = dmaArbitrate1;
  236. descrCfg.hprot = 0;
  237. DMA_CfgDescr((rt_uint32_t)channel, true, &descrCfg);
  238. }
  239. /***************************************************************************//**
  240. * @brief
  241. * Activate DMA for ADC
  242. *
  243. * @details
  244. *
  245. * @note
  246. *
  247. * @param[in] adc_device
  248. * Pointer to ADC registers base address
  249. *
  250. * @param[in] mode
  251. * ADC mode
  252. *
  253. * @param[in] count
  254. * ADC channel count
  255. *
  256. * @param[in] channel
  257. * DMA channel
  258. *
  259. * @param[out] buffer
  260. * Pointer to ADC results buffer
  261. ******************************************************************************/
  262. void efm32_adc_on_dma(
  263. ADC_TypeDef *adc_device,
  264. rt_uint8_t mode,
  265. rt_uint8_t count,
  266. rt_uint8_t channel,
  267. void *buffer)
  268. {
  269. switch (mode & ADC_MASK_MODE)
  270. {
  271. case ADC_MODE_SINGLE:
  272. /* Activate DMA */
  273. DMA_ActivateBasic(
  274. (rt_uint32_t)channel,
  275. true,
  276. false,
  277. buffer,
  278. (void *)&(adc_device->SINGLEDATA),
  279. count - 1);
  280. break;
  281. case ADC_MODE_SCAN:
  282. DMA_ActivateBasic(
  283. (rt_uint32_t)channel,
  284. true,
  285. false,
  286. buffer,
  287. (void *)&(adc_device->SCANDATA),
  288. count - 1);
  289. break;
  290. default:
  291. return;
  292. }
  293. }
  294. /***************************************************************************//**
  295. * @brief
  296. * Initialize ADC device
  297. *
  298. * @details
  299. *
  300. * @note
  301. *
  302. * @param[in] dev
  303. * Pointer to device descriptor
  304. *
  305. * @return
  306. * Error code
  307. ******************************************************************************/
  308. static rt_err_t rt_adc_init(rt_device_t dev)
  309. {
  310. RT_ASSERT(dev != RT_NULL);
  311. rt_uint32_t temp;
  312. struct efm32_adc_device_t *adc;
  313. adc = (struct efm32_adc_device_t *)(dev->user_data);
  314. temp = efm32_adc_calibration(adc->adc_device, ADC_CALI_REF, ADC_CALI_CH);
  315. adc_debug("adc->CAL = %x\n", temp);
  316. return RT_EOK;
  317. }
  318. /***************************************************************************//**
  319. * @brief
  320. * Configure ADC device
  321. *
  322. * @details
  323. *
  324. * @note
  325. *
  326. * @param[in] dev
  327. * Pointer to device descriptor
  328. *
  329. * @param[in] cmd
  330. * ADC control command
  331. *
  332. * @param[in] args
  333. * Arguments
  334. *
  335. * @return
  336. * Error code
  337. ******************************************************************************/
  338. static rt_err_t rt_adc_control(
  339. rt_device_t dev,
  340. rt_uint8_t cmd,
  341. void *args)
  342. {
  343. RT_ASSERT(dev != RT_NULL);
  344. struct efm32_adc_device_t *adc;
  345. adc = (struct efm32_adc_device_t *)(dev->user_data);
  346. switch (cmd)
  347. {
  348. case RT_DEVICE_CTRL_SUSPEND:
  349. /* Suspend device */
  350. dev->flag |= RT_DEVICE_FLAG_SUSPENDED;
  351. adc->adc_device->CMD = ADC_CMD_SINGLESTOP | ADC_CMD_SCANSTOP;
  352. break;
  353. case RT_DEVICE_CTRL_RESUME:
  354. {
  355. /* Resume device */
  356. struct efm32_adc_result_t *control = \
  357. (struct efm32_adc_result_t *)args;
  358. dev->flag &= ~RT_DEVICE_FLAG_SUSPENDED;
  359. switch (control->mode)
  360. {
  361. case ADC_MODE_SINGLE:
  362. if (adc->singleDmaChannel != (rt_uint8_t)EFM32_NO_DMA)
  363. {
  364. efm32_adc_on_dma(
  365. adc->adc_device,
  366. control->mode,
  367. adc->singleCount,
  368. adc->singleDmaChannel,
  369. control->buffer);
  370. }
  371. ADC_Start(adc->adc_device, adcStartSingle);
  372. break;
  373. case ADC_MODE_SCAN:
  374. if (adc->scanDmaChannel != (rt_uint8_t)EFM32_NO_DMA)
  375. {
  376. efm32_adc_on_dma(
  377. adc->adc_device,
  378. control->mode,
  379. adc->scanCount,
  380. adc->scanDmaChannel,
  381. control->buffer);
  382. }
  383. ADC_Start(adc->adc_device, adcStartScan);
  384. break;
  385. case ADC_MODE_TAILGATE:
  386. {
  387. void *index = control->buffer;
  388. if (adc->scanDmaChannel != (rt_uint8_t)EFM32_NO_DMA)
  389. {
  390. efm32_adc_on_dma(
  391. adc->adc_device,
  392. control->mode,
  393. adc->scanCount,
  394. adc->scanDmaChannel,
  395. index);
  396. index += adc->scanCount;
  397. }
  398. if (adc->singleDmaChannel != (rt_uint8_t)EFM32_NO_DMA)
  399. {
  400. efm32_adc_on_dma(
  401. adc->adc_device,
  402. control->mode,
  403. adc->singleCount,
  404. adc->singleDmaChannel,
  405. index);
  406. index += adc->singleCount;
  407. }
  408. ADC_Start(adc->adc_device, adcStartScanAndSingle);
  409. }
  410. break;
  411. default:
  412. return -RT_ERROR;
  413. }
  414. }
  415. break;
  416. case RT_DEVICE_CTRL_ADC_MODE:
  417. {
  418. /* change device setting */
  419. struct efm32_adc_control_t *control = \
  420. (struct efm32_adc_control_t *)args;
  421. switch (control->mode)
  422. {
  423. case ADC_MODE_SINGLE:
  424. ADC_InitSingle(adc->adc_device, control->single.init);
  425. break;
  426. case ADC_MODE_SCAN:
  427. ADC_InitScan(adc->adc_device, control->scan.init);
  428. break;
  429. case ADC_MODE_TAILGATE:
  430. ADC_InitSingle(adc->adc_device, control->single.init);
  431. ADC_InitScan(adc->adc_device, control->scan.init);
  432. break;
  433. default:
  434. return -RT_ERROR;
  435. }
  436. if (control->mode == ADC_MODE_TAILGATE)
  437. {
  438. adc->mode = ADC_MODE_TAILGATE;
  439. }
  440. else
  441. {
  442. adc->mode &= ~(rt_uint8_t)ADC_MODE_TAILGATE;
  443. adc->mode |= control->mode;
  444. }
  445. if ((control->mode == ADC_MODE_TAILGATE) || \
  446. (control->mode == ADC_MODE_SINGLE))
  447. {
  448. if (control->single.init->rep)
  449. {
  450. adc->mode |= ADC_OP_SINGLE_REPEAT;
  451. }
  452. adc->singleCount = control->single.count;
  453. adc->singleDmaChannel = control->single.dmaChannel;
  454. efm32_adc_cfg_dma(adc->adc_device, control->mode, adc->singleDmaChannel);
  455. }
  456. if ((control->mode == ADC_MODE_TAILGATE) || \
  457. (control->mode == ADC_MODE_SCAN))
  458. {
  459. if (control->scan.init->rep)
  460. {
  461. adc->mode |= ADC_OP_SCAN_REPEAT;
  462. }
  463. adc->scanCount = control->scan.count;
  464. adc->scanDmaChannel = control->scan.dmaChannel;
  465. efm32_adc_cfg_dma(adc->adc_device, control->mode, adc->scanDmaChannel);
  466. }
  467. }
  468. break;
  469. case RT_DEVICE_CTRL_ADC_RESULT:
  470. {
  471. struct efm32_adc_result_t *control = \
  472. (struct efm32_adc_result_t *)args;
  473. switch (control->mode)
  474. {
  475. case ADC_MODE_SINGLE:
  476. if (adc->singleDmaChannel != (rt_uint8_t)EFM32_NO_DMA)
  477. {
  478. if (adc->mode & ADC_OP_SINGLE_REPEAT)
  479. {
  480. if (!(DMA->IF & (1 << adc->singleDmaChannel)))
  481. {
  482. efm32_adc_on_dma(
  483. adc->adc_device,
  484. control->mode,
  485. adc->singleCount,
  486. adc->singleDmaChannel,
  487. control->buffer);
  488. }
  489. while (!(DMA->IF & (1 << adc->singleDmaChannel)));
  490. }
  491. else
  492. {
  493. while (adc->adc_device->STATUS & ADC_STATUS_SINGLEACT);
  494. }
  495. }
  496. else
  497. {
  498. while (adc->adc_device->STATUS & ADC_STATUS_SINGLEACT);
  499. *((rt_uint32_t *)control->buffer) = \
  500. ADC_DataSingleGet(adc->adc_device) << adcErrataShift;
  501. }
  502. break;
  503. case ADC_MODE_SCAN:
  504. if (adc->scanDmaChannel != (rt_uint8_t)EFM32_NO_DMA)
  505. {
  506. if (adc->mode & ADC_OP_SCAN_REPEAT)
  507. {
  508. if (!(DMA->IF & (1 << adc->scanDmaChannel)))
  509. {
  510. efm32_adc_on_dma(
  511. adc->adc_device,
  512. control->mode,
  513. adc->scanCount,
  514. adc->scanDmaChannel,
  515. control->buffer);
  516. }
  517. while (!(DMA->IF & (1 << adc->scanDmaChannel)));
  518. }
  519. else
  520. {
  521. while (adc->adc_device->STATUS & ADC_STATUS_SCANACT);
  522. }
  523. }
  524. else
  525. {
  526. while (adc->adc_device->STATUS & ADC_STATUS_SCANACT);
  527. *((rt_uint32_t *)control->buffer) = \
  528. ADC_DataScanGet(adc->adc_device) << adcErrataShift;
  529. }
  530. break;
  531. case ADC_MODE_TAILGATE:
  532. {
  533. void *index = control->buffer;
  534. if ((adc->scanDmaChannel != (rt_uint8_t)EFM32_NO_DMA) && \
  535. !(adc->mode & ADC_OP_SCAN_REPEAT))
  536. {
  537. index += adc->scanCount;
  538. }
  539. if ((adc->singleDmaChannel != (rt_uint8_t)EFM32_NO_DMA) && \
  540. !(adc->mode & ADC_OP_SINGLE_REPEAT))
  541. {
  542. index += adc->singleCount;
  543. }
  544. if (adc->scanDmaChannel != (rt_uint8_t)EFM32_NO_DMA)
  545. {
  546. if (adc->mode & ADC_OP_SCAN_REPEAT)
  547. {
  548. if (!(DMA->IF & (1 << adc->scanDmaChannel)))
  549. {
  550. efm32_adc_on_dma(
  551. adc->adc_device,
  552. control->mode,
  553. adc->scanCount,
  554. adc->scanDmaChannel,
  555. index);
  556. index += adc->scanCount;
  557. }
  558. while (!(DMA->IF & (1 << adc->scanDmaChannel)));
  559. }
  560. else
  561. {
  562. while (adc->adc_device->STATUS & ADC_STATUS_SCANACT);
  563. }
  564. }
  565. else
  566. {
  567. while (adc->adc_device->STATUS & ADC_STATUS_SCANACT);
  568. *(rt_uint32_t *)(index++) = \
  569. ADC_DataScanGet(adc->adc_device) << adcErrataShift;
  570. }
  571. if (adc->singleDmaChannel != (rt_uint8_t)EFM32_NO_DMA)
  572. {
  573. if (adc->mode & ADC_OP_SINGLE_REPEAT)
  574. {
  575. if (!(DMA->IF & (1 << adc->singleDmaChannel)))
  576. {
  577. efm32_adc_on_dma(
  578. adc->adc_device,
  579. control->mode,
  580. adc->singleCount,
  581. adc->singleDmaChannel,
  582. index);
  583. index += adc->singleCount;
  584. }
  585. while (!(DMA->IF & (1 << adc->singleDmaChannel)));
  586. }
  587. else
  588. {
  589. while (adc->adc_device->STATUS & ADC_STATUS_SINGLEACT);
  590. }
  591. }
  592. else
  593. {
  594. while (adc->adc_device->STATUS & ADC_STATUS_SINGLEACT);
  595. *(rt_uint32_t *)(index++) = \
  596. ADC_DataSingleGet(adc->adc_device) << adcErrataShift;
  597. }
  598. }
  599. break;
  600. default:
  601. return -RT_ERROR;
  602. }
  603. }
  604. break;
  605. default:
  606. return -RT_ERROR;
  607. }
  608. return RT_EOK;
  609. }
  610. /***************************************************************************//**
  611. * @brief
  612. * Register ADC device
  613. *
  614. * @details
  615. *
  616. * @note
  617. *
  618. * @param[in] device
  619. * Pointer to device descriptor
  620. *
  621. * @param[in] name
  622. * Device name
  623. *
  624. * @param[in] flag
  625. * Configuration flags
  626. *
  627. * @param[in] adc
  628. * Pointer to ADC device descriptor
  629. *
  630. * @return
  631. * Error code
  632. ******************************************************************************/
  633. rt_err_t rt_hw_adc_register(
  634. rt_device_t device,
  635. const char *name,
  636. rt_uint32_t flag,
  637. struct efm32_adc_device_t *adc)
  638. {
  639. RT_ASSERT(device != RT_NULL);
  640. device->type = RT_Device_Class_Char; /* fixme: should be adc type */
  641. device->rx_indicate = RT_NULL;
  642. device->tx_complete = RT_NULL;
  643. device->init = rt_adc_init;
  644. device->open = RT_NULL;
  645. device->close = RT_NULL;
  646. device->read = RT_NULL;
  647. device->write = RT_NULL;
  648. device->control = rt_adc_control;
  649. device->user_data = adc;
  650. /* register a character device */
  651. return rt_device_register(device, name, flag);
  652. }
  653. /***************************************************************************//**
  654. * @brief
  655. * Initialize the specified ADC unit
  656. *
  657. * @details
  658. *
  659. * @note
  660. *
  661. * @param[in] device
  662. * Pointer to device descriptor
  663. *
  664. * @param[in] unitNumber
  665. * Unit number
  666. *
  667. * @return
  668. * Pointer to ADC device
  669. ******************************************************************************/
  670. static struct efm32_adc_device_t *rt_hw_adc_unit_init(
  671. rt_device_t device,
  672. rt_uint8_t unitNumber)
  673. {
  674. struct efm32_adc_device_t *adc;
  675. CMU_Clock_TypeDef adcClock;
  676. ADC_Init_TypeDef init = ADC_INIT_DEFAULT;
  677. do
  678. {
  679. /* Allocate device and set default value */
  680. adc = rt_malloc(sizeof(struct efm32_adc_device_t));
  681. if (adc == RT_NULL)
  682. {
  683. adc_debug("no memory for ADC%d driver\n", unitNumber);
  684. break;
  685. }
  686. adc->mode = 0;
  687. adc->singleCount = 0;
  688. adc->singleDmaChannel = (rt_uint8_t)EFM32_NO_DMA;
  689. adc->scanCount = 0;
  690. adc->scanDmaChannel = (rt_uint8_t)EFM32_NO_DMA;
  691. /* Initialization */
  692. if (unitNumber >= ADC_COUNT)
  693. {
  694. break;
  695. }
  696. switch (unitNumber)
  697. {
  698. case 0:
  699. adc->adc_device = ADC0;
  700. adcClock = (CMU_Clock_TypeDef)cmuClock_ADC0;
  701. break;
  702. default:
  703. break;
  704. }
  705. /* Enable ADC clock */
  706. CMU_ClockEnable(adcClock, true);
  707. /* Reset */
  708. ADC_Reset(adc->adc_device);
  709. /* Configure ADC */
  710. // TODO: Fixed oversampling rate?
  711. init.ovsRateSel = adcOvsRateSel4096;
  712. init.timebase = ADC_TimebaseCalc(0);
  713. init.prescale = ADC_PrescaleCalc(ADC_CONVERT_FREQUENCY, 0);
  714. ADC_Init(adc->adc_device, &init);
  715. return adc;
  716. } while(0);
  717. if (adc)
  718. {
  719. rt_free(adc);
  720. }
  721. rt_kprintf("ADC: Init failed!\n");
  722. return RT_NULL;
  723. }
  724. /***************************************************************************//**
  725. * @brief
  726. * Initialize all ADC module related hardware and register ADC device to kernel
  727. *
  728. * @details
  729. *
  730. * @note
  731. *
  732. ******************************************************************************/
  733. void rt_hw_adc_init(void)
  734. {
  735. SYSTEM_ChipRevision_TypeDef chipRev;
  736. struct efm32_adc_device_t *adc;
  737. #ifdef RT_USING_ADC0
  738. if ((adc = rt_hw_adc_unit_init(&adc0_device, 0)) != RT_NULL)
  739. {
  740. rt_hw_adc_register(&adc0_device, RT_ADC0_NAME, EFM32_NO_DATA, adc);
  741. }
  742. #endif
  743. /* ADC errata for rev B when using VDD as reference, need to multiply */
  744. /* result by 2 */
  745. SYSTEM_ChipRevisionGet(&chipRev);
  746. if ((chipRev.major == 0x01) && (chipRev.minor == 0x01))
  747. {
  748. adcErrataShift = 1;
  749. }
  750. }
  751. #endif
  752. /***************************************************************************//**
  753. * @}
  754. ******************************************************************************/