drv_acmp.c 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374
  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. #ifdef RT_ACMP_DEBUG
  103. rt_kprintf("ACMP: control -> init start\n");
  104. #endif
  105. /* change device setting */
  106. struct efm32_acmp_control_t *control;
  107. control = (struct efm32_acmp_control_t *)args;
  108. /* Configure ACMPn */
  109. if (control->init == RT_NULL)
  110. {
  111. return -RT_ERROR;
  112. }
  113. ACMP_Init(acmp->acmp_device, control->init);
  114. ACMP_ChannelSet(acmp->acmp_device, control->negInput, control->posInput);
  115. if (control->output != RT_NULL)
  116. {
  117. ACMP_GPIOSetup(
  118. acmp->acmp_device,
  119. control->output->location,
  120. control->output->enable,
  121. control->output->invert);
  122. int_en = true;
  123. }
  124. if (control->hook.cbFunc != RT_NULL)
  125. {
  126. acmp->hook.cbFunc = control->hook.cbFunc;
  127. acmp->hook.userPtr = control->hook.userPtr;
  128. int_en = true;
  129. }
  130. if (int_en)
  131. {
  132. /* Enable edge interrupt */
  133. ACMP_IntEnable(acmp->acmp_device, ACMP_IEN_EDGE);
  134. ACMP_IntClear(acmp->acmp_device, ACMP_IFC_EDGE);
  135. /* Enable ACMP0/1 interrupt vector in NVIC */
  136. NVIC_ClearPendingIRQ(ACMP0_IRQn);
  137. NVIC_SetPriority(ACMP0_IRQn, EFM32_IRQ_PRI_DEFAULT);
  138. NVIC_EnableIRQ(ACMP0_IRQn);
  139. }
  140. }
  141. break;
  142. case RT_DEVICE_CTRL_ACMP_OUTPUT:
  143. *((rt_bool_t *)args) = \
  144. (acmp->acmp_device->STATUS & ACMP_STATUS_ACMPOUT) ? true : false;
  145. break;
  146. default:
  147. return -RT_ERROR;
  148. }
  149. return RT_EOK;
  150. }
  151. /******************************************************************//**
  152. * @brief
  153. * Register ACMP device
  154. *
  155. * @details
  156. *
  157. * @note
  158. *
  159. * @param[in] device
  160. * Pointer to device descriptor
  161. *
  162. * @param[in] name
  163. * Device name
  164. *
  165. * @param[in] flag
  166. * Configuration flags
  167. *
  168. * @param[in] acmp
  169. * Pointer to ACMP device descriptor
  170. *
  171. * @return
  172. * Error code
  173. *********************************************************************/
  174. rt_err_t rt_hw_acmp_register(
  175. rt_device_t device,
  176. const char *name,
  177. rt_uint32_t flag,
  178. struct efm32_acmp_device_t *acmp)
  179. {
  180. RT_ASSERT(device != RT_NULL);
  181. device->type = RT_Device_Class_Char; /* fixme: should be acmp type */
  182. device->rx_indicate = RT_NULL;
  183. device->tx_complete = RT_NULL;
  184. device->init = rt_acmp_init;
  185. device->open = RT_NULL;
  186. device->close = RT_NULL;
  187. device->read = RT_NULL;
  188. device->write = RT_NULL;
  189. device->control = rt_acmp_control;
  190. device->user_data = acmp;
  191. /* register a character device */
  192. return rt_device_register(device, name, flag);
  193. }
  194. /******************************************************************//**
  195. * @brief
  196. * ACMP edge trigger interrupt handler
  197. *
  198. * @details
  199. *
  200. * @note
  201. *********************************************************************/
  202. void rt_hw_acmp_isr(rt_device_t dev)
  203. {
  204. RT_ASSERT(dev != RT_NULL);
  205. struct efm32_acmp_device_t *acmp;
  206. acmp = (struct efm32_acmp_device_t *)(dev->user_data);
  207. if (acmp->hook.cbFunc != RT_NULL)
  208. {
  209. (acmp->hook.cbFunc)(acmp->hook.userPtr);
  210. }
  211. }
  212. /******************************************************************//**
  213. * @brief
  214. * Initialize all ACMP module related hardware and register ACMP device to kernel
  215. *
  216. * @details
  217. *
  218. * @note
  219. *
  220. *********************************************************************/
  221. void rt_hw_acmp_init(void)
  222. {
  223. struct efm32_acmp_device_t *acmp;
  224. efm32_irq_hook_init_t hook;
  225. #ifdef RT_USING_ACMP0
  226. acmp = rt_malloc(sizeof(struct efm32_acmp_device_t));
  227. if (acmp == RT_NULL)
  228. {
  229. #ifdef RT_ACMP_DEBUG
  230. rt_kprintf("no memory for ACMP0 driver\n");
  231. #endif
  232. return;
  233. }
  234. acmp->acmp_device = ACMP0;
  235. /* Enable clock for ACMP0 module */
  236. CMU_ClockEnable(cmuClock_ACMP0, true);
  237. /* Reset */
  238. ACMP_Reset(ACMP0);
  239. hook.type = efm32_irq_type_acmp;
  240. hook.unit = 0;
  241. hook.cbFunc = rt_hw_acmp_isr;
  242. hook.userPtr = &acmp0_device;
  243. efm32_irq_hook_register(&hook);
  244. rt_hw_acmp_register(&acmp0_device, RT_ACMP0_NAME, EFM32_NO_DATA, acmp);
  245. #endif
  246. #ifdef RT_USING_ACMP1
  247. acmp = rt_malloc(sizeof(struct efm32_acmp_device_t));
  248. if (acmp == RT_NULL)
  249. {
  250. #ifdef RT_ACMP_DEBUG
  251. rt_kprintf("no memory for ACMP1 driver\n");
  252. #endif
  253. return;
  254. }
  255. acmp->acmp_device = ACMP1;
  256. /* Enable clock for ACMP1 module */
  257. CMU_ClockEnable(cmuClock_ACMP1, true);
  258. /* Reset */
  259. ACMP_Reset(ACMP1);
  260. hook.type = efm32_irq_type_acmp;
  261. hook.unit = 0;
  262. hook.cbFunc = rt_hw_acmp_isr;
  263. hook.userPtr = &acmp0_device;
  264. efm32_irq_hook_register(&hook);
  265. rt_hw_acmp_register(&acmp1_device, RT_ACMP1_NAME, EFM32_NO_DATA, acmp);
  266. #endif
  267. }
  268. /******************************************************************//**
  269. * @brief
  270. * Calculate the warm-up time value providing at least 10us
  271. *
  272. * @param[in] hfperFreq
  273. * Frequency in Hz of reference HFPER clock. Set to 0 to use currently defined HFPER clock
  274. * setting
  275. *
  276. * @return
  277. * Warm-up time value to use for ACMP in order to achieve at least 10us
  278. *********************************************************************/
  279. ACMP_WarmTime_TypeDef efm32_acmp_WarmTimeCalc(rt_uint32_t hfperFreq)
  280. {
  281. if (!hfperFreq)
  282. {
  283. hfperFreq = CMU_ClockFreqGet(cmuClock_HFPER);
  284. /* Just in case, make sure we get non-zero freq for below calculation */
  285. if (!hfperFreq)
  286. {
  287. hfperFreq = 1;
  288. }
  289. }
  290. /* Determine number of HFPERCLK cycle >= 10us */
  291. if (4 * 1000000 / hfperFreq > 10)
  292. {
  293. return acmpWarmTime4;
  294. }
  295. else if (8 * 1000000 / hfperFreq > 10)
  296. {
  297. return acmpWarmTime8;
  298. }
  299. else if (16 * 1000000 / hfperFreq > 10)
  300. {
  301. return acmpWarmTime16;
  302. }
  303. else if (32 * 1000000 / hfperFreq > 10)
  304. {
  305. return acmpWarmTime32;
  306. }
  307. else if (64 * 1000000 / hfperFreq > 10)
  308. {
  309. return acmpWarmTime64;
  310. }
  311. else if (128 * 1000000 / hfperFreq > 10)
  312. {
  313. return acmpWarmTime128;
  314. }
  315. else if (256 * 1000000 / hfperFreq > 10)
  316. {
  317. return acmpWarmTime256;
  318. }
  319. else if (512 * 1000000 / hfperFreq > 10)
  320. {
  321. return acmpWarmTime512;
  322. }
  323. }
  324. /******************************************************************//**
  325. * @}
  326. *********************************************************************/