efm32_acmp.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335
  1. /***************************************************************************//**
  2. * @file
  3. * @brief Analog Comparator (ACMP) peripheral API for EFM32.
  4. * @author Energy Micro AS
  5. * @version 1.3.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 section 23.3.2 in the reference manual
  128. * for details. */
  129. bool fullBias;
  130. /** Half bias current. See section 23.3.2 in the reference manual
  131. * for details. */
  132. bool halfBias;
  133. /** Bias current. See section 23.3.2 in the reference manual for
  134. * 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. } ACMP_CapsenseInit_TypeDef;
  151. /** Default config for capacitive sense on the STK */
  152. #define ACMP_CAPSENSE_STK_DEFAULT \
  153. { false, /* fullBias */ \
  154. false, /* halfBias */ \
  155. 0xF, /* biasProg */ \
  156. acmpWarmTime512, /* 512 cycle warmup to be safe */ \
  157. acmpHysteresisLevel5, \
  158. acmpResistor3, \
  159. false, /* low power reference */ \
  160. 0x3D /* VDD level */ \
  161. }
  162. /** ACMP initialization structure. */
  163. typedef struct
  164. {
  165. /** Full bias current. See section 23.3.2 in the reference manual
  166. * for details. */
  167. bool fullBias;
  168. /** Half bias current. See section 23.3.2 in the reference manual
  169. * for details. */
  170. bool halfBias;
  171. /** Bias current. See section 23.3.2 in the reference manual for
  172. * details. Valid values are in the range 0-7. */
  173. uint32_t biasProg;
  174. /** Enable setting the interrupt flag on falling edge */
  175. bool interruptOnFallingEdge;
  176. /** Enable setting the interrupt flag on rising edge */
  177. bool interruptOnRisingEdge;
  178. /** Warmup time. This is measured in HFPERCLK cycles and should be
  179. * about 10us in wall clock time. */
  180. ACMP_WarmTime_TypeDef warmTime;
  181. /** Hysteresis level */
  182. ACMP_HysteresisLevel_TypeDef hysteresisLevel;
  183. /** Inactive value emitted by the ACMP during warmup */
  184. bool inactiveValue;
  185. /** Low power reference enabled. This setting, if enabled, reduces the
  186. * power used by the VDD and bandgap references. */
  187. bool lowPowerReferenceEnabled;
  188. /** Vdd reference value. VDD_SCALED = VDD × VDDLEVEL × 50mV/3.8V.
  189. * Valid values are in the range 0-63. */
  190. uint32_t vddLevel;
  191. } ACMP_Init_TypeDef;
  192. /*******************************************************************************
  193. ***************************** PROTOTYPES **********************************
  194. ******************************************************************************/
  195. void ACMP_CapsenseInit(ACMP_TypeDef *acmp, const ACMP_CapsenseInit_TypeDef *init);
  196. void ACMP_CapsenseChannelSet(ACMP_TypeDef *acmp, ACMP_Channel_TypeDef channel);
  197. void ACMP_ChannelSet(ACMP_TypeDef *acmp, ACMP_Channel_TypeDef negSel, ACMP_Channel_TypeDef posSel);
  198. void ACMP_Disable(ACMP_TypeDef *acmp);
  199. void ACMP_DisableNoReset(ACMP_TypeDef *acmp);
  200. void ACMP_Enable(ACMP_TypeDef *acmp);
  201. void ACMP_GPIOSetup(ACMP_TypeDef *acmp, uint32_t location, bool enable, bool invert);
  202. void ACMP_Init(ACMP_TypeDef *acmp, const ACMP_Init_TypeDef *init);
  203. void ACMP_Reset(ACMP_TypeDef *acmp);
  204. /***************************************************************************//**
  205. * @brief
  206. * Clear one or more pending ACMP interrupts.
  207. *
  208. * @param[in] acmp
  209. * Pointer to ACMP peripheral register block.
  210. *
  211. * @param[in] flags
  212. * Pending ACMP interrupt source to clear. Use a logical OR combination
  213. * of valid interrupt flags for the ACMP module (ACMP_IF_nnn).
  214. ******************************************************************************/
  215. static __INLINE void ACMP_IntClear(ACMP_TypeDef *acmp, uint32_t flags)
  216. {
  217. acmp->IFC = flags;
  218. }
  219. /***************************************************************************//**
  220. * @brief
  221. * Disable one or more ACMP interrupts.
  222. *
  223. * @param[in] acmp
  224. * Pointer to ACMP peripheral register block.
  225. *
  226. * @param[in] flags
  227. * ACMP interrupt sources to disable. Use a logical OR combination of
  228. * valid interrupt flags for the ACMP module (ACMP_IF_nnn).
  229. ******************************************************************************/
  230. static __INLINE void ACMP_IntDisable(ACMP_TypeDef *acmp, uint32_t flags)
  231. {
  232. acmp->IEN &= ~(flags);
  233. }
  234. /***************************************************************************//**
  235. * @brief
  236. * Enable one or more ACMP interrupts.
  237. *
  238. * @note
  239. * Depending on the use, a pending interrupt may already be set prior to
  240. * enabling the interrupt. Consider using ACMP_IntClear() prior to enabling
  241. * if such a pending interrupt should be ignored.
  242. *
  243. * @param[in] acmp
  244. * Pointer to ACMP peripheral register block.
  245. *
  246. * @param[in] flags
  247. * ACMP interrupt sources to enable. Use a logical OR combination of
  248. * valid interrupt flags for the ACMP module (ACMP_IF_nnn).
  249. ******************************************************************************/
  250. static __INLINE void ACMP_IntEnable(ACMP_TypeDef *acmp, uint32_t flags)
  251. {
  252. acmp->IEN |= flags;
  253. }
  254. /***************************************************************************//**
  255. * @brief
  256. * Get pending ACMP interrupt flags.
  257. *
  258. * @note
  259. * The event bits are not cleared by the use of this function.
  260. *
  261. * @param[in] acmp
  262. * Pointer to ACMP peripheral register block.
  263. *
  264. * @return
  265. * ACMP interrupt sources pending. A logical OR combination of valid
  266. * interrupt flags for the ACMP module (ACMP_IF_nnn).
  267. ******************************************************************************/
  268. static __INLINE uint32_t ACMP_IntGet(ACMP_TypeDef *acmp)
  269. {
  270. return(acmp->IF);
  271. }
  272. /***************************************************************************//**
  273. * @brief
  274. * Set one or more pending ACMP interrupts from SW.
  275. *
  276. * @param[in] acmp
  277. * Pointer to ACMP peripheral register block.
  278. *
  279. * @param[in] flags
  280. * ACMP interrupt sources to set to pending. Use a logical OR combination
  281. * of valid interrupt flags for the ACMP module (ACMP_IF_nnn).
  282. ******************************************************************************/
  283. static __INLINE void ACMP_IntSet(ACMP_TypeDef *acmp, uint32_t flags)
  284. {
  285. acmp->IFS = flags;
  286. }
  287. /** @} (end addtogroup ACMP) */
  288. /** @} (end addtogroup EFM32_Library) */
  289. #ifdef __cplusplus
  290. }
  291. #endif
  292. #endif /* __EFM32_ACMP_H */