efm32_acmp.h 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395
  1. /***************************************************************************//**
  2. * @file
  3. * @brief Analog Comparator (ACMP) peripheral API for EFM32.
  4. * @author Energy Micro AS
  5. * @version 2.3.2
  6. *******************************************************************************
  7. * @section License
  8. * <b>(C) Copyright 2011 Energy Micro AS, http://www.energymicro.com</b>
  9. *******************************************************************************
  10. *
  11. * This source code is the property of Energy Micro AS. The source and compiled
  12. * code may only be used on Energy Micro "EFM32" microcontrollers.
  13. *
  14. * This copyright notice may not be removed from the source code nor changed.
  15. *
  16. * DISCLAIMER OF WARRANTY/LIMITATION OF REMEDIES: Energy Micro AS has no
  17. * obligation to support this Software. Energy Micro AS is providing the
  18. * Software "AS IS", with no express or implied warranties of any kind,
  19. * including, but not limited to, any implied warranties of merchantability
  20. * or fitness for any particular purpose or warranties against infringement
  21. * of any proprietary rights of a third party.
  22. *
  23. * Energy Micro AS will not be liable for any consequential, incidental, or
  24. * special damages, or any other relief, or for any claim by any third party,
  25. * arising from your use of this Software.
  26. *
  27. ******************************************************************************/
  28. #ifndef __EFM32_ACMP_H
  29. #define __EFM32_ACMP_H
  30. #include <stdint.h>
  31. #include <stdbool.h>
  32. #include "efm32.h"
  33. #ifdef __cplusplus
  34. extern "C" {
  35. #endif
  36. /***************************************************************************//**
  37. * @addtogroup EFM32_Library
  38. * @{
  39. ******************************************************************************/
  40. /***************************************************************************//**
  41. * @addtogroup ACMP
  42. * @{
  43. ******************************************************************************/
  44. /*******************************************************************************
  45. ******************************** ENUMS ************************************
  46. ******************************************************************************/
  47. /** Resistor values used for capacative sense. See the datasheet for your
  48. * device for details on each resistor value. */
  49. typedef enum
  50. {
  51. /** resistor value 0 */
  52. acmpResistor0 = _ACMP_INPUTSEL_CSRESSEL_RES0,
  53. /** resistor value 1 */
  54. acmpResistor1 = _ACMP_INPUTSEL_CSRESSEL_RES1,
  55. /** resistor value 2 */
  56. acmpResistor2 = _ACMP_INPUTSEL_CSRESSEL_RES2,
  57. /** resistor value 3 */
  58. acmpResistor3 = _ACMP_INPUTSEL_CSRESSEL_RES3
  59. } ACMP_CapsenseResistor_TypeDef;
  60. /** Hysteresis level. See datasheet for your device for details on each
  61. * level. */
  62. typedef enum
  63. {
  64. acmpHysteresisLevel0 = _ACMP_CTRL_HYSTSEL_HYST0, /**< Hysteresis level 0 */
  65. acmpHysteresisLevel1 = _ACMP_CTRL_HYSTSEL_HYST1, /**< Hysteresis level 1 */
  66. acmpHysteresisLevel2 = _ACMP_CTRL_HYSTSEL_HYST2, /**< Hysteresis level 2 */
  67. acmpHysteresisLevel3 = _ACMP_CTRL_HYSTSEL_HYST3, /**< Hysteresis level 3 */
  68. acmpHysteresisLevel4 = _ACMP_CTRL_HYSTSEL_HYST4, /**< Hysteresis level 4 */
  69. acmpHysteresisLevel5 = _ACMP_CTRL_HYSTSEL_HYST5, /**< Hysteresis level 5 */
  70. acmpHysteresisLevel6 = _ACMP_CTRL_HYSTSEL_HYST6, /**< Hysteresis level 6 */
  71. acmpHysteresisLevel7 = _ACMP_CTRL_HYSTSEL_HYST7 /**< Hysteresis level 7 */
  72. } ACMP_HysteresisLevel_TypeDef;
  73. /** ACMP warmup time. The delay is measured in HFPERCLK cycles and should
  74. * be at least 10 us. */
  75. typedef enum
  76. {
  77. /** 4 HFPERCLK cycles warmup */
  78. acmpWarmTime4 = _ACMP_CTRL_WARMTIME_4CYCLES,
  79. /** 8 HFPERCLK cycles warmup */
  80. acmpWarmTime8 = _ACMP_CTRL_WARMTIME_8CYCLES,
  81. /** 16 HFPERCLK cycles warmup */
  82. acmpWarmTime16 = _ACMP_CTRL_WARMTIME_16CYCLES,
  83. /** 32 HFPERCLK cycles warmup */
  84. acmpWarmTime32 = _ACMP_CTRL_WARMTIME_32CYCLES,
  85. /** 64 HFPERCLK cycles warmup */
  86. acmpWarmTime64 = _ACMP_CTRL_WARMTIME_64CYCLES,
  87. /** 128 HFPERCLK cycles warmup */
  88. acmpWarmTime128 = _ACMP_CTRL_WARMTIME_128CYCLES,
  89. /** 256 HFPERCLK cycles warmup */
  90. acmpWarmTime256 = _ACMP_CTRL_WARMTIME_256CYCLES,
  91. /** 512 HFPERCLK cycles warmup */
  92. acmpWarmTime512 = _ACMP_CTRL_WARMTIME_512CYCLES
  93. } ACMP_WarmTime_TypeDef;
  94. /** ACMP inputs. Note that scaled VDD and bandgap references can only be used
  95. * as negative inputs. */
  96. typedef enum
  97. {
  98. /** Channel 0 */
  99. acmpChannel0 = _ACMP_INPUTSEL_NEGSEL_CH0,
  100. /** Channel 1 */
  101. acmpChannel1 = _ACMP_INPUTSEL_NEGSEL_CH1,
  102. /** Channel 2 */
  103. acmpChannel2 = _ACMP_INPUTSEL_NEGSEL_CH2,
  104. /** Channel 3 */
  105. acmpChannel3 = _ACMP_INPUTSEL_NEGSEL_CH3,
  106. /** Channel 4 */
  107. acmpChannel4 = _ACMP_INPUTSEL_NEGSEL_CH4,
  108. /** Channel 5 */
  109. acmpChannel5 = _ACMP_INPUTSEL_NEGSEL_CH5,
  110. /** Channel 6 */
  111. acmpChannel6 = _ACMP_INPUTSEL_NEGSEL_CH6,
  112. /** Channel 7 */
  113. acmpChannel7 = _ACMP_INPUTSEL_NEGSEL_CH7,
  114. /** 1.25V internal reference */
  115. acmpChannel1V25 = _ACMP_INPUTSEL_NEGSEL_1V25,
  116. /** 2.5V internal reference */
  117. acmpChannel2V5 = _ACMP_INPUTSEL_NEGSEL_2V5,
  118. /** Scaled VDD reference */
  119. acmpChannelVDD = _ACMP_INPUTSEL_NEGSEL_VDD,
  120. #if defined(_EFM32_GIANT_FAMILY) || defined(_EFM32_TINY_FAMILY)
  121. /** DAC0 channel 0 */
  122. acmpChannelDAC0Ch0 = _ACMP_INPUTSEL_NEGSEL_DAC0CH0,
  123. /** DAC0 channel 1 */
  124. acmpChannelDAC0Ch1 = _ACMP_INPUTSEL_NEGSEL_DAC0CH1,
  125. #endif
  126. } ACMP_Channel_TypeDef;
  127. /*******************************************************************************
  128. ****************************** STRUCTS ************************************
  129. ******************************************************************************/
  130. /** Capsense initialization structure. */
  131. typedef struct
  132. {
  133. /** Full bias current. See the ACMP chapter about bias and response time in
  134. * the reference manual for details. */
  135. bool fullBias;
  136. /** Half bias current. See the ACMP chapter about bias and response time in
  137. * the reference manual for details. */
  138. bool halfBias;
  139. /** Bias current. See the ACMP chapter about bias and response time in the
  140. * reference manual for details. Valid values are in the range 0-7. */
  141. uint32_t biasProg;
  142. /** Warmup time. This is measured in HFPERCLK cycles and should be
  143. * about 10us in wall clock time. */
  144. ACMP_WarmTime_TypeDef warmTime;
  145. /** Hysteresis level */
  146. ACMP_HysteresisLevel_TypeDef hysteresisLevel;
  147. /** Resistor used in the capacative sensing circuit. For values see
  148. * your device datasheet. */
  149. ACMP_CapsenseResistor_TypeDef resistor;
  150. /** Low power reference enabled. This setting, if enabled, reduces the
  151. * power used by the VDD and bandgap references. */
  152. bool lowPowerReferenceEnabled;
  153. /** Vdd reference value. VDD_SCALED = VDD × VDDLEVEL × 50mV/3.8V.
  154. * Valid values are in the range 0-63. */
  155. uint32_t vddLevel;
  156. /** If true, ACMP is being enabled after configuration. */
  157. bool enable;
  158. } ACMP_CapsenseInit_TypeDef;
  159. /** Default config for capacitive sense mode initialization. */
  160. #define ACMP_CAPSENSE_INIT_DEFAULT \
  161. { false, /* fullBias */ \
  162. false, /* halfBias */ \
  163. 0x7, /* biasProg */ \
  164. acmpWarmTime512, /* 512 cycle warmup to be safe */ \
  165. acmpHysteresisLevel5, \
  166. acmpResistor3, \
  167. false, /* low power reference */ \
  168. 0x3D, /* VDD level */ \
  169. true /* Enable after init. */ \
  170. }
  171. /** ACMP initialization structure. */
  172. typedef struct
  173. {
  174. /** Full bias current. See the ACMP chapter about bias and response time in
  175. * the reference manual for details. */
  176. bool fullBias;
  177. /** Half bias current. See the ACMP chapter about bias and response time in
  178. * the reference manual for details. */
  179. bool halfBias;
  180. /** Bias current. See the ACMP chapter about bias and response time in the
  181. * reference manual for details. Valid values are in the range 0-7. */
  182. uint32_t biasProg;
  183. /** Enable setting the interrupt flag on falling edge */
  184. bool interruptOnFallingEdge;
  185. /** Enable setting the interrupt flag on rising edge */
  186. bool interruptOnRisingEdge;
  187. /** Warmup time. This is measured in HFPERCLK cycles and should be
  188. * about 10us in wall clock time. */
  189. ACMP_WarmTime_TypeDef warmTime;
  190. /** Hysteresis level */
  191. ACMP_HysteresisLevel_TypeDef hysteresisLevel;
  192. /** Inactive value emitted by the ACMP during warmup */
  193. bool inactiveValue;
  194. /** Low power reference enabled. This setting, if enabled, reduces the
  195. * power used by the VDD and bandgap references. */
  196. bool lowPowerReferenceEnabled;
  197. /** Vdd reference value. VDD_SCALED = VDD × VDDLEVEL × 50mV/3.8V.
  198. * Valid values are in the range 0-63. */
  199. uint32_t vddLevel;
  200. /** If true, ACMP is being enabled after configuration. */
  201. bool enable;
  202. } ACMP_Init_TypeDef;
  203. /** Default config for ACMP regular initialization. */
  204. #define ACMP_INIT_DEFAULT \
  205. { false, /* fullBias */ \
  206. false, /* halfBias */ \
  207. 0x7, /* biasProg */ \
  208. false, /* No interrupt on falling edge. */ \
  209. false, /* No interrupt on rising edge. */ \
  210. acmpWarmTime512, /* 512 cycle warmup to be safe */ \
  211. acmpHysteresisLevel5, \
  212. false, /* Disabled emitting inactive value during warmup. */ \
  213. false, /* low power reference */ \
  214. 0x3D, /* VDD level */ \
  215. true /* Enable after init. */ \
  216. }
  217. /*******************************************************************************
  218. ***************************** PROTOTYPES **********************************
  219. ******************************************************************************/
  220. void ACMP_CapsenseInit(ACMP_TypeDef *acmp, const ACMP_CapsenseInit_TypeDef *init);
  221. void ACMP_CapsenseChannelSet(ACMP_TypeDef *acmp, ACMP_Channel_TypeDef channel);
  222. void ACMP_ChannelSet(ACMP_TypeDef *acmp, ACMP_Channel_TypeDef negSel, ACMP_Channel_TypeDef posSel);
  223. void ACMP_Disable(ACMP_TypeDef *acmp);
  224. void ACMP_Enable(ACMP_TypeDef *acmp);
  225. void ACMP_GPIOSetup(ACMP_TypeDef *acmp, uint32_t location, bool enable, bool invert);
  226. void ACMP_Init(ACMP_TypeDef *acmp, const ACMP_Init_TypeDef *init);
  227. void ACMP_Reset(ACMP_TypeDef *acmp);
  228. /***************************************************************************//**
  229. * @brief
  230. * Clear one or more pending ACMP interrupts.
  231. *
  232. * @param[in] acmp
  233. * Pointer to ACMP peripheral register block.
  234. *
  235. * @param[in] flags
  236. * Pending ACMP interrupt source to clear. Use a bitwise logic OR combination
  237. * of valid interrupt flags for the ACMP module (ACMP_IF_nnn).
  238. ******************************************************************************/
  239. static __INLINE void ACMP_IntClear(ACMP_TypeDef *acmp, uint32_t flags)
  240. {
  241. acmp->IFC = flags;
  242. }
  243. /***************************************************************************//**
  244. * @brief
  245. * Disable one or more ACMP interrupts.
  246. *
  247. * @param[in] acmp
  248. * Pointer to ACMP peripheral register block.
  249. *
  250. * @param[in] flags
  251. * ACMP interrupt sources to disable. Use a bitwise logic OR combination of
  252. * valid interrupt flags for the ACMP module (ACMP_IF_nnn).
  253. ******************************************************************************/
  254. static __INLINE void ACMP_IntDisable(ACMP_TypeDef *acmp, uint32_t flags)
  255. {
  256. acmp->IEN &= ~(flags);
  257. }
  258. /***************************************************************************//**
  259. * @brief
  260. * Enable one or more ACMP interrupts.
  261. *
  262. * @note
  263. * Depending on the use, a pending interrupt may already be set prior to
  264. * enabling the interrupt. Consider using ACMP_IntClear() prior to enabling
  265. * if such a pending interrupt should be ignored.
  266. *
  267. * @param[in] acmp
  268. * Pointer to ACMP peripheral register block.
  269. *
  270. * @param[in] flags
  271. * ACMP interrupt sources to enable. Use a bitwise logic OR combination of
  272. * valid interrupt flags for the ACMP module (ACMP_IF_nnn).
  273. ******************************************************************************/
  274. static __INLINE void ACMP_IntEnable(ACMP_TypeDef *acmp, uint32_t flags)
  275. {
  276. acmp->IEN |= flags;
  277. }
  278. /***************************************************************************//**
  279. * @brief
  280. * Get pending ACMP interrupt flags.
  281. *
  282. * @note
  283. * The event bits are not cleared by the use of this function.
  284. *
  285. * @param[in] acmp
  286. * Pointer to ACMP peripheral register block.
  287. *
  288. * @return
  289. * ACMP interrupt sources pending. A bitwise logic OR combination of valid
  290. * interrupt flags for the ACMP module (ACMP_IF_nnn).
  291. ******************************************************************************/
  292. static __INLINE uint32_t ACMP_IntGet(ACMP_TypeDef *acmp)
  293. {
  294. return(acmp->IF);
  295. }
  296. /***************************************************************************//**
  297. * @brief
  298. * Get enabled and pending ACMP interrupt flags.
  299. * Useful for handling more interrupt sources in the same interrupt handler.
  300. *
  301. * @param[in] usart
  302. * Pointer to ACMP peripheral register block.
  303. *
  304. * @note
  305. * Interrupt flags are not cleared by the use of this function.
  306. *
  307. * @return
  308. * Pending and enabled ACMP interrupt sources.
  309. * The return value is the bitwise AND combination of
  310. * - the OR combination of enabled interrupt sources in ACMPx_IEN_nnn
  311. * register (ACMPx_IEN_nnn) and
  312. * - the OR combination of valid interrupt flags of the ACMP module
  313. * (ACMPx_IF_nnn).
  314. ******************************************************************************/
  315. static __INLINE uint32_t ACMP_IntGetEnabled(ACMP_TypeDef *acmp)
  316. {
  317. uint32_t tmp;
  318. /* Store ACMPx->IEN in temporary variable in order to define explicit order
  319. * of volatile accesses. */
  320. tmp = acmp->IEN;
  321. /* Bitwise AND of pending and enabled interrupts */
  322. return acmp->IF & tmp;
  323. }
  324. /***************************************************************************//**
  325. * @brief
  326. * Set one or more pending ACMP interrupts from SW.
  327. *
  328. * @param[in] acmp
  329. * Pointer to ACMP peripheral register block.
  330. *
  331. * @param[in] flags
  332. * ACMP interrupt sources to set to pending. Use a bitwise logic OR
  333. * combination of valid interrupt flags for the ACMP module (ACMP_IF_nnn).
  334. ******************************************************************************/
  335. static __INLINE void ACMP_IntSet(ACMP_TypeDef *acmp, uint32_t flags)
  336. {
  337. acmp->IFS = flags;
  338. }
  339. /** @} (end addtogroup ACMP) */
  340. /** @} (end addtogroup EFM32_Library) */
  341. #ifdef __cplusplus
  342. }
  343. #endif
  344. #endif /* __EFM32_ACMP_H */