em_acmp.h 15 KB

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