drv_acmp.c 9.1 KB

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