1
0

drv_acmp.c 11 KB

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