drv_acmp.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419
  1. /******************************************************************//**
  2. * @file drv_acmp.c
  3. * @brief ACMP (analog comparator) 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. * 2011-06-17 onelife Modify init and control function for efm32lib v2 upgrading
  16. *********************************************************************/
  17. /******************************************************************//**
  18. * @addtogroup efm32
  19. * @{
  20. *********************************************************************/
  21. /* Includes -------------------------------------------------------------------*/
  22. #include "board.h"
  23. #include "drv_acmp.h"
  24. #if (defined(RT_USING_ACMP0) || defined(RT_USING_ACMP1))
  25. /* Private typedef -------------------------------------------------------------*/
  26. /* Private define --------------------------------------------------------------*/
  27. /* Private macro --------------------------------------------------------------*/
  28. #ifdef RT_ACMP_DEBUG
  29. #define acmp_debug(format,args...) rt_kprintf(format, ##args)
  30. #else
  31. #define acmp_debug(format,args...)
  32. #endif
  33. /* Private variables ------------------------------------------------------------*/
  34. #ifdef RT_USING_ACMP0
  35. static struct rt_device acmp0_device;
  36. #endif
  37. #ifdef RT_USING_ACMP1
  38. static struct rt_device acmp1_device;
  39. #endif
  40. /* Private function prototypes ---------------------------------------------------*/
  41. ACMP_WarmTime_TypeDef efm32_acmp_WarmTimeCalc(rt_uint32_t hfperFreq);
  42. /* Private functions ------------------------------------------------------------*/
  43. /******************************************************************//**
  44. * @brief
  45. * Initialize ACMP device
  46. *
  47. * @details
  48. *
  49. * @note
  50. *
  51. * @param[in] dev
  52. * Pointer to device descriptor
  53. *
  54. * @return
  55. * Error code
  56. *********************************************************************/
  57. static rt_err_t rt_acmp_init(rt_device_t dev)
  58. {
  59. RT_ASSERT(dev != RT_NULL);
  60. struct efm32_acmp_device_t *acmp;
  61. acmp = (struct efm32_acmp_device_t *)(dev->user_data);
  62. acmp->hook.cbFunc = RT_NULL;
  63. acmp->hook.userPtr = RT_NULL;
  64. return RT_EOK;
  65. }
  66. /******************************************************************//**
  67. * @brief
  68. * Configure ACMP device
  69. *
  70. * @details
  71. *
  72. * @note
  73. *
  74. * @param[in] dev
  75. * Pointer to device descriptor
  76. *
  77. * @param[in] cmd
  78. * ACMP control command
  79. *
  80. * @param[in] args
  81. * Arguments
  82. *
  83. * @return
  84. * Error code
  85. *********************************************************************/
  86. static rt_err_t rt_acmp_control(
  87. rt_device_t dev,
  88. rt_uint8_t cmd,
  89. void *args)
  90. {
  91. RT_ASSERT(dev != RT_NULL);
  92. struct efm32_acmp_device_t *acmp;
  93. acmp = (struct efm32_acmp_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. ACMP_Disable(acmp->acmp_device);
  100. break;
  101. case RT_DEVICE_CTRL_RESUME:
  102. /* Resume device */
  103. dev->flag &= ~RT_DEVICE_FLAG_SUSPENDED;
  104. ACMP_Enable(acmp->acmp_device);
  105. break;
  106. case RT_DEVICE_CTRL_ACMP_INIT:
  107. {
  108. rt_bool_t int_en = false;
  109. struct efm32_acmp_control_t *control;
  110. control = (struct efm32_acmp_control_t *)args;
  111. acmp_debug("ACMP: control -> init start\n");
  112. /* Configure ACMPn */
  113. if (control->init == RT_NULL)
  114. {
  115. return -RT_ERROR;
  116. }
  117. ACMP_Init(acmp->acmp_device, control->init);
  118. ACMP_ChannelSet(acmp->acmp_device, control->negInput, control->posInput);
  119. if (control->output != RT_NULL)
  120. {
  121. ACMP_GPIOSetup(
  122. acmp->acmp_device,
  123. control->output->location,
  124. control->output->enable,
  125. control->output->invert);
  126. int_en = true;
  127. }
  128. if (control->hook.cbFunc != RT_NULL)
  129. {
  130. acmp->hook.cbFunc = control->hook.cbFunc;
  131. acmp->hook.userPtr = control->hook.userPtr;
  132. int_en = true;
  133. }
  134. if (int_en)
  135. {
  136. /* Enable edge interrupt */
  137. ACMP_IntEnable(acmp->acmp_device, ACMP_IEN_EDGE);
  138. ACMP_IntClear(acmp->acmp_device, ACMP_IFC_EDGE);
  139. /* Enable ACMP0/1 interrupt vector in NVIC */
  140. NVIC_ClearPendingIRQ(ACMP0_IRQn);
  141. NVIC_SetPriority(ACMP0_IRQn, EFM32_IRQ_PRI_DEFAULT);
  142. NVIC_EnableIRQ(ACMP0_IRQn);
  143. }
  144. }
  145. break;
  146. case RT_DEVICE_CTRL_ACMP_OUTPUT:
  147. *((rt_bool_t *)args) = \
  148. (acmp->acmp_device->STATUS & ACMP_STATUS_ACMPOUT) ? true : false;
  149. break;
  150. default:
  151. return -RT_ERROR;
  152. }
  153. return RT_EOK;
  154. }
  155. /******************************************************************//**
  156. * @brief
  157. * Register ACMP device
  158. *
  159. * @details
  160. *
  161. * @note
  162. *
  163. * @param[in] device
  164. * Pointer to device descriptor
  165. *
  166. * @param[in] name
  167. * Device name
  168. *
  169. * @param[in] flag
  170. * Configuration flags
  171. *
  172. * @param[in] acmp
  173. * Pointer to ACMP device descriptor
  174. *
  175. * @return
  176. * Error code
  177. *********************************************************************/
  178. rt_err_t rt_hw_acmp_register(
  179. rt_device_t device,
  180. const char *name,
  181. rt_uint32_t flag,
  182. struct efm32_acmp_device_t *acmp)
  183. {
  184. RT_ASSERT(device != RT_NULL);
  185. device->type = RT_Device_Class_Char; /* fixme: should be acmp type */
  186. device->rx_indicate = RT_NULL;
  187. device->tx_complete = RT_NULL;
  188. device->init = rt_acmp_init;
  189. device->open = RT_NULL;
  190. device->close = RT_NULL;
  191. device->read = RT_NULL;
  192. device->write = RT_NULL;
  193. device->control = rt_acmp_control;
  194. device->user_data = acmp;
  195. /* register a character device */
  196. return rt_device_register(device, name, flag);
  197. }
  198. /******************************************************************//**
  199. * @brief
  200. * ACMP edge trigger interrupt handler
  201. *
  202. * @details
  203. *
  204. * @note
  205. *********************************************************************/
  206. void rt_hw_acmp_isr(rt_device_t dev)
  207. {
  208. RT_ASSERT(dev != RT_NULL);
  209. struct efm32_acmp_device_t *acmp;
  210. acmp = (struct efm32_acmp_device_t *)(dev->user_data);
  211. if (acmp->hook.cbFunc != RT_NULL)
  212. {
  213. (acmp->hook.cbFunc)(acmp->hook.userPtr);
  214. }
  215. }
  216. /******************************************************************//**
  217. * @brief
  218. * Initialize the specified ACMP unit
  219. *
  220. * @details
  221. *
  222. * @note
  223. *
  224. * @param[in] device
  225. * Pointer to device descriptor
  226. *
  227. * @param[in] unitNumber
  228. * Unit number
  229. *
  230. * @return
  231. * Pointer to ACMP device
  232. *********************************************************************/
  233. static struct efm32_acmp_device_t *rt_hw_acmp_unit_init(
  234. rt_device_t device,
  235. rt_uint8_t unitNumber)
  236. {
  237. struct efm32_acmp_device_t *acmp;
  238. efm32_irq_hook_init_t hook;
  239. CMU_Clock_TypeDef acmpClock;
  240. do
  241. {
  242. /* Allocate device */
  243. acmp = rt_malloc(sizeof(struct efm32_acmp_device_t));
  244. if (acmp == RT_NULL)
  245. {
  246. acmp_debug("no memory for ACMP%d driver\n", unitNumber);
  247. break;
  248. }
  249. /* Initialization */
  250. if (unitNumber >= ACMP_COUNT)
  251. {
  252. break;
  253. }
  254. switch (unitNumber)
  255. {
  256. case 0:
  257. acmp->acmp_device = ACMP0;
  258. acmpClock = (CMU_Clock_TypeDef)cmuClock_ACMP0;
  259. break;
  260. case 1:
  261. acmp->acmp_device = ACMP1;
  262. acmpClock = (CMU_Clock_TypeDef)cmuClock_ACMP1;
  263. break;
  264. default:
  265. break;
  266. }
  267. /* Enable ACMP clock */
  268. CMU_ClockEnable(acmpClock, true);
  269. /* Reset */
  270. ACMP_Reset(acmp->acmp_device);
  271. /* Config interrupt and NVIC */
  272. hook.type = efm32_irq_type_acmp;
  273. hook.unit = unitNumber;
  274. hook.cbFunc = rt_hw_acmp_isr;
  275. hook.userPtr = device;
  276. efm32_irq_hook_register(&hook);
  277. return acmp;
  278. } while(0);
  279. if (acmp)
  280. {
  281. rt_free(acmp);
  282. }
  283. rt_kprintf("ACMP: Init failed!\n");
  284. return RT_NULL;
  285. }
  286. /******************************************************************//**
  287. * @brief
  288. * Initialize all ACMP module related hardware and register ACMP device to kernel
  289. *
  290. * @details
  291. *
  292. * @note
  293. *
  294. *********************************************************************/
  295. void rt_hw_acmp_init(void)
  296. {
  297. struct efm32_acmp_device_t *acmp;
  298. #ifdef RT_USING_ACMP0
  299. if ((acmp = rt_hw_acmp_unit_init(&acmp0_device, 0)) != RT_NULL)
  300. {
  301. rt_hw_acmp_register(&acmp0_device, RT_ACMP0_NAME, EFM32_NO_DATA, acmp);
  302. }
  303. #endif
  304. #ifdef RT_USING_ACMP1
  305. if ((acmp = rt_hw_acmp_unit_init(&acmp1_device, 1)) != RT_NULL)
  306. {
  307. rt_hw_acmp_register(&acmp1_device, RT_ACMP1_NAME, EFM32_NO_DATA, acmp);
  308. }
  309. #endif
  310. }
  311. /******************************************************************//**
  312. * @brief
  313. * Calculate the warm-up time value providing at least 10us
  314. *
  315. * @param[in] hfperFreq
  316. * Frequency in Hz of reference HFPER clock. Set to 0 to use currently defined HFPER clock
  317. * setting
  318. *
  319. * @return
  320. * Warm-up time value to use for ACMP in order to achieve at least 10us
  321. *********************************************************************/
  322. ACMP_WarmTime_TypeDef efm32_acmp_WarmTimeCalc(rt_uint32_t hfperFreq)
  323. {
  324. if (!hfperFreq)
  325. {
  326. hfperFreq = CMU_ClockFreqGet(cmuClock_HFPER);
  327. /* Just in case, make sure we get non-zero freq for below calculation */
  328. if (!hfperFreq)
  329. {
  330. hfperFreq = 1;
  331. }
  332. }
  333. /* Determine number of HFPERCLK cycle >= 10us */
  334. if (4 * 1000000 / hfperFreq > 10)
  335. {
  336. return acmpWarmTime4;
  337. }
  338. else if (8 * 1000000 / hfperFreq > 10)
  339. {
  340. return acmpWarmTime8;
  341. }
  342. else if (16 * 1000000 / hfperFreq > 10)
  343. {
  344. return acmpWarmTime16;
  345. }
  346. else if (32 * 1000000 / hfperFreq > 10)
  347. {
  348. return acmpWarmTime32;
  349. }
  350. else if (64 * 1000000 / hfperFreq > 10)
  351. {
  352. return acmpWarmTime64;
  353. }
  354. else if (128 * 1000000 / hfperFreq > 10)
  355. {
  356. return acmpWarmTime128;
  357. }
  358. else if (256 * 1000000 / hfperFreq > 10)
  359. {
  360. return acmpWarmTime256;
  361. }
  362. else if (512 * 1000000 / hfperFreq > 10)
  363. {
  364. return acmpWarmTime512;
  365. }
  366. }
  367. #endif
  368. /******************************************************************//**
  369. * @}
  370. *********************************************************************/