efm32_vcmp.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352
  1. /***************************************************************************//**
  2. * @file
  3. * @brief Voltage Comparator (VCMP) 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_VCMP_H
  29. #define __EFM32_VCMP_H
  30. #include "efm32.h"
  31. #include <stdint.h>
  32. #include <stdbool.h>
  33. #ifdef __cplusplus
  34. extern "C" {
  35. #endif
  36. /***************************************************************************//**
  37. * @addtogroup EFM32_Library
  38. * @{
  39. ******************************************************************************/
  40. /***************************************************************************//**
  41. * @addtogroup VCMP
  42. * @{
  43. ******************************************************************************/
  44. /*******************************************************************************
  45. ******************************** ENUMS ************************************
  46. ******************************************************************************/
  47. /** Warm-up Time in High Frequency Peripheral Clock cycles */
  48. typedef enum
  49. {
  50. /** 4 cycles */
  51. vcmpWarmTime4Cycles = _VCMP_CTRL_WARMTIME_4CYCLES,
  52. /** 8 cycles */
  53. vcmpWarmTime8Cycles = _VCMP_CTRL_WARMTIME_8CYCLES,
  54. /** 16 cycles */
  55. vcmpWarmTime16Cycles = _VCMP_CTRL_WARMTIME_16CYCLES,
  56. /** 32 cycles */
  57. vcmpWarmTime32Cycles = _VCMP_CTRL_WARMTIME_32CYCLES,
  58. /** 64 cycles */
  59. vcmpWarmTime64Cycles = _VCMP_CTRL_WARMTIME_64CYCLES,
  60. /** 128 cycles */
  61. vcmpWarmTime128Cycles = _VCMP_CTRL_WARMTIME_128CYCLES,
  62. /** 256 cycles */
  63. vcmpWarmTime256Cycles = _VCMP_CTRL_WARMTIME_256CYCLES,
  64. /** 512 cycles */
  65. vcmpWarmTime512Cycles = _VCMP_CTRL_WARMTIME_512CYCLES
  66. } VCMP_WarmTime_TypeDef;
  67. /** Hyseresis configuration */
  68. typedef enum
  69. {
  70. /** Normal operation, no hysteresis */
  71. vcmpHystNone,
  72. /** Digital output will not toggle until positive edge is at least
  73. * 20mV above or below negative input voltage */
  74. vcmpHyst20mV
  75. } VCMP_Hysteresis_TypeDef;
  76. /*******************************************************************************
  77. ******************************* STRUCTS ***********************************
  78. ******************************************************************************/
  79. /** VCMP Initialization structure */
  80. typedef struct
  81. {
  82. /** If set to true, will reduce by half the bias current */
  83. bool halfBias;
  84. /** BIAS current configuration, depends on halfBias setting,
  85. * above, see reference manual */
  86. int biasProg;
  87. /** Enable interrupt for falling edge */
  88. bool irqFalling;
  89. /** Enable interrupt for rising edge */
  90. bool irqRising;
  91. /** Warm-up time in clock cycles */
  92. VCMP_WarmTime_TypeDef warmup;
  93. /** Hysteresis configuration */
  94. VCMP_Hysteresis_TypeDef hyst;
  95. /** Output value when comparator is inactive, should be 0 or 1 */
  96. int inactive;
  97. /** Enable low power mode for VDD and bandgap reference */
  98. bool lowPowerRef;
  99. /** Trigger level, according to formula
  100. * VDD Trigger Level = 1.667V + 0.034V x triggerLevel */
  101. int triggerLevel;
  102. /** Enable VCMP after configuration */
  103. bool enable;
  104. } VCMP_Init_TypeDef;
  105. /** Default VCMP initialization structure */
  106. #define VCMP_INIT_DEFAULT \
  107. { true, /** Half Bias enabled */ \
  108. 0x7, /** Bias curernt 0.7 uA when half bias enabled */ \
  109. false, /** Falling edge sense not enabled */ \
  110. false, /** Rising edge sense not enabled */ \
  111. vcmpWarmTime4Cycles, /** 4 clock cycles warm-up time */ \
  112. vcmpHystNone, /** No hysteresis */ \
  113. 0, /** 0 in digital ouput when inactive */ \
  114. true, /** Do not use low power reference */ \
  115. 39, /** Trigger level just below 3V */ \
  116. true, /** Enable after init */ \
  117. }
  118. /*******************************************************************************
  119. ***************************** PROTOTYPES **********************************
  120. ******************************************************************************/
  121. void VCMP_Init(const VCMP_Init_TypeDef *vcmpInit);
  122. void VCMP_LowPowerRefSet(bool enable);
  123. void VCMP_TriggerSet(int level);
  124. static __INLINE void VCMP_Enable(void);
  125. static __INLINE void VCMP_Disable(void);
  126. static __INLINE uint32_t VCMP_VoltageToLevel(float v);
  127. static __INLINE bool VCMP_VDDLower(void);
  128. static __INLINE bool VCMP_VDDHigher(void);
  129. static __INLINE bool VCMP_Ready(void);
  130. static __INLINE void VCMP_IntClear(uint32_t flags);
  131. static __INLINE void VCMP_IntSet(uint32_t flags);
  132. static __INLINE void VCMP_IntDisable(uint32_t flags);
  133. static __INLINE void VCMP_IntEnable(uint32_t flags);
  134. static __INLINE uint32_t VCMP_IntGet(void);
  135. static __INLINE uint32_t VCMP_IntGetEnabled(void);
  136. /***************************************************************************//**
  137. * @brief
  138. * Enable Voltage Comparator
  139. ******************************************************************************/
  140. static __INLINE void VCMP_Enable(void)
  141. {
  142. VCMP->CTRL |= VCMP_CTRL_EN;
  143. }
  144. /***************************************************************************//**
  145. * @brief
  146. * Disable Voltage Comparator
  147. ******************************************************************************/
  148. static __INLINE void VCMP_Disable(void)
  149. {
  150. VCMP->CTRL &= ~(VCMP_CTRL_EN);
  151. }
  152. /***************************************************************************//**
  153. * @brief
  154. * Calculate voltage to trigger level
  155. *
  156. * @note
  157. * You need soft float support for this function to be working
  158. *
  159. * @param[in] v
  160. * Voltage Level for trigger
  161. ******************************************************************************/
  162. static __INLINE uint32_t VCMP_VoltageToLevel(float v)
  163. {
  164. return (uint32_t)((v - (float)1.667) / (float)0.034);
  165. }
  166. /***************************************************************************//**
  167. * @brief
  168. * Returns true, if Voltage Comparator indicated VDD < trigger level, else
  169. * false
  170. ******************************************************************************/
  171. static __INLINE bool VCMP_VDDLower(void)
  172. {
  173. if (VCMP->STATUS & VCMP_STATUS_VCMPOUT)
  174. {
  175. return false;
  176. }
  177. else
  178. {
  179. return true;
  180. }
  181. }
  182. /***************************************************************************//**
  183. * @brief
  184. * Returns true, if Voltage Comparator indicated VDD > trigger level, else
  185. * false
  186. ******************************************************************************/
  187. static __INLINE bool VCMP_VDDHigher(void)
  188. {
  189. if (VCMP->STATUS & VCMP_STATUS_VCMPOUT)
  190. {
  191. return true;
  192. }
  193. else
  194. {
  195. return false;
  196. }
  197. }
  198. /***************************************************************************//**
  199. * @brief
  200. * VCMP output is ready
  201. ******************************************************************************/
  202. static __INLINE bool VCMP_Ready(void)
  203. {
  204. if (VCMP->STATUS & VCMP_STATUS_VCMPACT)
  205. {
  206. return true;
  207. }
  208. else
  209. {
  210. return false;
  211. }
  212. }
  213. /***************************************************************************//**
  214. * @brief
  215. * Clear one or more pending VCMP interrupts.
  216. *
  217. * @param[in] flags
  218. * VCMP interrupt sources to clear. Use a set of interrupt flags OR-ed
  219. * together to clear multiple interrupt sources for the VCMP module
  220. * (VCMP_IFS_nnn).
  221. ******************************************************************************/
  222. static __INLINE void VCMP_IntClear(uint32_t flags)
  223. {
  224. VCMP->IFC = flags;
  225. }
  226. /***************************************************************************//**
  227. * @brief
  228. * Set one or more pending VCMP interrupts from SW.
  229. *
  230. * @param[in] flags
  231. * VCMP interrupt sources to set to pending. Use a set of interrupt flags
  232. * OR-ed together to set multiple interrupt sources for the VCMP module
  233. * (VCMP_IFS_nnn).
  234. ******************************************************************************/
  235. static __INLINE void VCMP_IntSet(uint32_t flags)
  236. {
  237. VCMP->IFS = flags;
  238. }
  239. /***************************************************************************//**
  240. * @brief
  241. * Disable one or more VCMP interrupts
  242. *
  243. * @param[in] flags
  244. * VCMP interrupt sources to enable. Use a set of interrupt flags OR-ed
  245. * together to set multiple interrupt sources for the VCMP module
  246. * (VCMP_IFS_nnn).
  247. ******************************************************************************/
  248. static __INLINE void VCMP_IntDisable(uint32_t flags)
  249. {
  250. VCMP->IEN &= ~(flags);
  251. }
  252. /***************************************************************************//**
  253. * @brief
  254. * Enable one or more VCMP interrupts
  255. *
  256. * @param[in] flags
  257. * VCMP interrupt sources to enable. Use a set of interrupt flags OR-ed
  258. * together to set multiple interrupt sources for the VCMP module
  259. * (VCMP_IFS_nnn).
  260. ******************************************************************************/
  261. static __INLINE void VCMP_IntEnable(uint32_t flags)
  262. {
  263. VCMP->IEN |= flags;
  264. }
  265. /***************************************************************************//**
  266. * @brief
  267. * Get pending VCMP interrupt flags
  268. *
  269. * @note
  270. * The event bits are not cleared by the use of this function
  271. *
  272. * @return
  273. * Pending VCMP interrupt sources. Returns a set of interrupt flags OR-ed
  274. * together for multiple interrupt sources in the VCMP module (VCMP_IFS_nnn).
  275. ******************************************************************************/
  276. static __INLINE uint32_t VCMP_IntGet(void)
  277. {
  278. return(VCMP->IF);
  279. }
  280. /***************************************************************************//**
  281. * @brief
  282. * Get enabled and pending VCMP interrupt flags.
  283. *
  284. * @details
  285. * Useful for handling more interrupt sources in the same interrupt handler.
  286. *
  287. * @note
  288. * The event bits are not cleared by the use of this function.
  289. *
  290. * @return
  291. * Pending and enabled VCMP interrupt sources.
  292. * The return value is the bitwise AND combination of
  293. * - the OR combination of enabled interrupt sources in VCMP_IEN_nnn
  294. * register (VCMP_IEN_nnn) and
  295. * - the OR combination of valid interrupt flags of the VCMP module
  296. * (VCMP_IF_nnn).
  297. ******************************************************************************/
  298. static __INLINE uint32_t VCMP_IntGetEnabled(void)
  299. {
  300. uint32_t tmp = 0U;
  301. /* Store VCMP->IEN in temporary variable in order to define explicit order
  302. * of volatile accesses. */
  303. tmp = VCMP->IEN;
  304. /* Bitwise AND of pending and enabled interrupts */
  305. return VCMP->IF & tmp;
  306. }
  307. /** @} (end addtogroup VCMP) */
  308. /** @} (end addtogroup EFM32_Library) */
  309. #ifdef __cplusplus
  310. }
  311. #endif
  312. #endif /* __EFM32_VCMP_H */