efm32_acmp.h 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389
  1. /***************************************************************************//**
  2. * @file
  3. * @brief Analog Comparator (ACMP) peripheral API for EFM32.
  4. * @author Energy Micro AS
  5. * @version 2.0.0
  6. *******************************************************************************
  7. * @section License
  8. * <b>(C) Copyright 2010 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. } ACMP_Channel_TypeDef;
  121. /*******************************************************************************
  122. ****************************** STRUCTS ************************************
  123. ******************************************************************************/
  124. /** Capsense initialization structure. */
  125. typedef struct
  126. {
  127. /** Full bias current. See the ACMP chapter about bias and response time in
  128. * the reference manual for details. */
  129. bool fullBias;
  130. /** Half bias current. See the ACMP chapter about bias and response time in
  131. * the reference manual for details. */
  132. bool halfBias;
  133. /** Bias current. See the ACMP chapter about bias and response time in the
  134. * reference manual for details. Valid values are in the range 0-7. */
  135. uint32_t biasProg;
  136. /** Warmup time. This is measured in HFPERCLK cycles and should be
  137. * about 10us in wall clock time. */
  138. ACMP_WarmTime_TypeDef warmTime;
  139. /** Hysteresis level */
  140. ACMP_HysteresisLevel_TypeDef hysteresisLevel;
  141. /** Resistor used in the capacative sensing circuit. For values see
  142. * your device datasheet. */
  143. ACMP_CapsenseResistor_TypeDef resistor;
  144. /** Low power reference enabled. This setting, if enabled, reduces the
  145. * power used by the VDD and bandgap references. */
  146. bool lowPowerReferenceEnabled;
  147. /** Vdd reference value. VDD_SCALED = VDD × VDDLEVEL × 50mV/3.8V.
  148. * Valid values are in the range 0-63. */
  149. uint32_t vddLevel;
  150. /** If true, ACMP is being enabled after configuration. */
  151. bool enable;
  152. } ACMP_CapsenseInit_TypeDef;
  153. /** Default config for capacitive sense mode initialization. */
  154. #define ACMP_CAPSENSE_INIT_DEFAULT \
  155. { false, /* fullBias */ \
  156. false, /* halfBias */ \
  157. 0x7, /* biasProg */ \
  158. acmpWarmTime512, /* 512 cycle warmup to be safe */ \
  159. acmpHysteresisLevel5, \
  160. acmpResistor3, \
  161. false, /* low power reference */ \
  162. 0x3D, /* VDD level */ \
  163. true /* Enable after init. */ \
  164. }
  165. /** ACMP initialization structure. */
  166. typedef struct
  167. {
  168. /** Full bias current. See the ACMP chapter about bias and response time in
  169. * the reference manual for details. */
  170. bool fullBias;
  171. /** Half bias current. See the ACMP chapter about bias and response time in
  172. * the reference manual for details. */
  173. bool halfBias;
  174. /** Bias current. See the ACMP chapter about bias and response time in the
  175. * reference manual for details. Valid values are in the range 0-7. */
  176. uint32_t biasProg;
  177. /** Enable setting the interrupt flag on falling edge */
  178. bool interruptOnFallingEdge;
  179. /** Enable setting the interrupt flag on rising edge */
  180. bool interruptOnRisingEdge;
  181. /** Warmup time. This is measured in HFPERCLK cycles and should be
  182. * about 10us in wall clock time. */
  183. ACMP_WarmTime_TypeDef warmTime;
  184. /** Hysteresis level */
  185. ACMP_HysteresisLevel_TypeDef hysteresisLevel;
  186. /** Inactive value emitted by the ACMP during warmup */
  187. bool inactiveValue;
  188. /** Low power reference enabled. This setting, if enabled, reduces the
  189. * power used by the VDD and bandgap references. */
  190. bool lowPowerReferenceEnabled;
  191. /** Vdd reference value. VDD_SCALED = VDD × VDDLEVEL × 50mV/3.8V.
  192. * Valid values are in the range 0-63. */
  193. uint32_t vddLevel;
  194. /** If true, ACMP is being enabled after configuration. */
  195. bool enable;
  196. } ACMP_Init_TypeDef;
  197. /** Default config for ACMP regular initialization. */
  198. #define ACMP_INIT_DEFAULT \
  199. { false, /* fullBias */ \
  200. false, /* halfBias */ \
  201. 0x7, /* biasProg */ \
  202. false, /* No interrupt on falling edge. */ \
  203. false, /* No interrupt on rising edge. */ \
  204. acmpWarmTime512, /* 512 cycle warmup to be safe */ \
  205. acmpHysteresisLevel5, \
  206. false, /* Disabled emitting inactive value during warmup. */ \
  207. false, /* low power reference */ \
  208. 0x3D, /* VDD level */ \
  209. true /* Enable after init. */ \
  210. }
  211. /*******************************************************************************
  212. ***************************** PROTOTYPES **********************************
  213. ******************************************************************************/
  214. void ACMP_CapsenseInit(ACMP_TypeDef *acmp, const ACMP_CapsenseInit_TypeDef *init);
  215. void ACMP_CapsenseChannelSet(ACMP_TypeDef *acmp, ACMP_Channel_TypeDef channel);
  216. void ACMP_ChannelSet(ACMP_TypeDef *acmp, ACMP_Channel_TypeDef negSel, ACMP_Channel_TypeDef posSel);
  217. void ACMP_Disable(ACMP_TypeDef *acmp);
  218. void ACMP_Enable(ACMP_TypeDef *acmp);
  219. void ACMP_GPIOSetup(ACMP_TypeDef *acmp, uint32_t location, bool enable, bool invert);
  220. void ACMP_Init(ACMP_TypeDef *acmp, const ACMP_Init_TypeDef *init);
  221. void ACMP_Reset(ACMP_TypeDef *acmp);
  222. /***************************************************************************//**
  223. * @brief
  224. * Clear one or more pending ACMP interrupts.
  225. *
  226. * @param[in] acmp
  227. * Pointer to ACMP peripheral register block.
  228. *
  229. * @param[in] flags
  230. * Pending ACMP interrupt source to clear. Use a bitwise logic OR combination
  231. * of valid interrupt flags for the ACMP module (ACMP_IF_nnn).
  232. ******************************************************************************/
  233. static __INLINE void ACMP_IntClear(ACMP_TypeDef *acmp, uint32_t flags)
  234. {
  235. acmp->IFC = flags;
  236. }
  237. /***************************************************************************//**
  238. * @brief
  239. * Disable one or more ACMP interrupts.
  240. *
  241. * @param[in] acmp
  242. * Pointer to ACMP peripheral register block.
  243. *
  244. * @param[in] flags
  245. * ACMP interrupt sources to disable. Use a bitwise logic OR combination of
  246. * valid interrupt flags for the ACMP module (ACMP_IF_nnn).
  247. ******************************************************************************/
  248. static __INLINE void ACMP_IntDisable(ACMP_TypeDef *acmp, uint32_t flags)
  249. {
  250. acmp->IEN &= ~(flags);
  251. }
  252. /***************************************************************************//**
  253. * @brief
  254. * Enable one or more ACMP interrupts.
  255. *
  256. * @note
  257. * Depending on the use, a pending interrupt may already be set prior to
  258. * enabling the interrupt. Consider using ACMP_IntClear() prior to enabling
  259. * if such a pending interrupt should be ignored.
  260. *
  261. * @param[in] acmp
  262. * Pointer to ACMP peripheral register block.
  263. *
  264. * @param[in] flags
  265. * ACMP interrupt sources to enable. Use a bitwise logic OR combination of
  266. * valid interrupt flags for the ACMP module (ACMP_IF_nnn).
  267. ******************************************************************************/
  268. static __INLINE void ACMP_IntEnable(ACMP_TypeDef *acmp, uint32_t flags)
  269. {
  270. acmp->IEN |= flags;
  271. }
  272. /***************************************************************************//**
  273. * @brief
  274. * Get pending ACMP interrupt flags.
  275. *
  276. * @note
  277. * The event bits are not cleared by the use of this function.
  278. *
  279. * @param[in] acmp
  280. * Pointer to ACMP peripheral register block.
  281. *
  282. * @return
  283. * ACMP interrupt sources pending. A bitwise logic OR combination of valid
  284. * interrupt flags for the ACMP module (ACMP_IF_nnn).
  285. ******************************************************************************/
  286. static __INLINE uint32_t ACMP_IntGet(ACMP_TypeDef *acmp)
  287. {
  288. return(acmp->IF);
  289. }
  290. /***************************************************************************//**
  291. * @brief
  292. * Get enabled and pending ACMP interrupt flags.
  293. * Useful for handling more interrupt sources in the same interrupt handler.
  294. *
  295. * @param[in] usart
  296. * Pointer to ACMP peripheral register block.
  297. *
  298. * @note
  299. * Interrupt flags are not cleared by the use of this function.
  300. *
  301. * @return
  302. * Pending and enabled ACMP interrupt sources.
  303. * The return value is the bitwise AND combination of
  304. * - the OR combination of enabled interrupt sources in ACMPx_IEN_nnn
  305. * register (ACMPx_IEN_nnn) and
  306. * - the OR combination of valid interrupt flags of the ACMP module
  307. * (ACMPx_IF_nnn).
  308. ******************************************************************************/
  309. static __INLINE uint32_t ACMP_IntGetEnabled(ACMP_TypeDef *acmp)
  310. {
  311. uint32_t tmp;
  312. /* Store ACMPx->IEN in temporary variable in order to define explicit order
  313. * of volatile accesses. */
  314. tmp = acmp->IEN;
  315. /* Bitwise AND of pending and enabled interrupts */
  316. return acmp->IF & tmp;
  317. }
  318. /***************************************************************************//**
  319. * @brief
  320. * Set one or more pending ACMP interrupts from SW.
  321. *
  322. * @param[in] acmp
  323. * Pointer to ACMP peripheral register block.
  324. *
  325. * @param[in] flags
  326. * ACMP interrupt sources to set to pending. Use a bitwise logic OR
  327. * combination of valid interrupt flags for the ACMP module (ACMP_IF_nnn).
  328. ******************************************************************************/
  329. static __INLINE void ACMP_IntSet(ACMP_TypeDef *acmp, uint32_t flags)
  330. {
  331. acmp->IFS = flags;
  332. }
  333. /** @} (end addtogroup ACMP) */
  334. /** @} (end addtogroup EFM32_Library) */
  335. #ifdef __cplusplus
  336. }
  337. #endif
  338. #endif /* __EFM32_ACMP_H */