efm32_vcmp.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  1. /***************************************************************************//**
  2. * @file
  3. * @brief Voltage Comparator (VCMP) 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_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. /***************************************************************************//**
  125. * @brief
  126. * Enable Voltage Comparator
  127. ******************************************************************************/
  128. static __INLINE void VCMP_Enable(void)
  129. {
  130. VCMP->CTRL |= VCMP_CTRL_EN;
  131. }
  132. /***************************************************************************//**
  133. * @brief
  134. * Disable Voltage Comparator
  135. ******************************************************************************/
  136. static __INLINE void VCMP_Disable(void)
  137. {
  138. VCMP->CTRL &= ~(VCMP_CTRL_EN);
  139. }
  140. /***************************************************************************//**
  141. * @brief
  142. * Calculate voltage to trigger level
  143. *
  144. * @note
  145. * You need soft float support for this function to be working
  146. *
  147. * @param[in] v
  148. * Voltage Level for trigger
  149. ******************************************************************************/
  150. static __INLINE uint32_t VCMP_VoltageToLevel(float v)
  151. {
  152. return (uint32_t)((v - (float) 1.667) / (float) 0.034);
  153. }
  154. /***************************************************************************//**
  155. * @brief
  156. * Returns true, if Voltage Comparator indicated VDD < trigger level, else
  157. * false
  158. ******************************************************************************/
  159. static __INLINE bool VCMP_VDDLower(void)
  160. {
  161. if (VCMP->STATUS & VCMP_STATUS_VCMPOUT)
  162. {
  163. return false;
  164. }
  165. else
  166. {
  167. return true;
  168. }
  169. }
  170. /***************************************************************************//**
  171. * @brief
  172. * Returns true, if Voltage Comparator indicated VDD > trigger level, else
  173. * false
  174. ******************************************************************************/
  175. static __INLINE bool VCMP_VDDHigher(void)
  176. {
  177. if (VCMP->STATUS & VCMP_STATUS_VCMPOUT)
  178. {
  179. return true;
  180. }
  181. else
  182. {
  183. return false;
  184. }
  185. }
  186. /***************************************************************************//**
  187. * @brief
  188. * VCMP output is ready
  189. ******************************************************************************/
  190. static __INLINE bool VCMP_Ready(void)
  191. {
  192. if (VCMP->STATUS & VCMP_STATUS_VCMPACT)
  193. {
  194. return true;
  195. }
  196. else
  197. {
  198. return false;
  199. }
  200. }
  201. /***************************************************************************//**
  202. * @brief
  203. * Clear one or more pending VCMP interrupts.
  204. *
  205. * @param[in] flags
  206. * Pending VCMP interrupt source to clear. Use a logical OR combination
  207. * of valid interrupt flags for the VCMP module (VCMP_IF_nnn).
  208. ******************************************************************************/
  209. static __INLINE void VCMP_IntClear(uint32_t flags)
  210. {
  211. VCMP->IFC = flags;
  212. }
  213. /***************************************************************************//**
  214. * @brief
  215. * Set one or more pending VCMP interrupts from SW.
  216. *
  217. * @param[in] flags
  218. * VCMP interrupt sources to set to pending. Use a logical OR combination of
  219. * valid interrupt flags for the VCMP module (VCMP_IF_nnn).
  220. ******************************************************************************/
  221. static __INLINE void VCMP_IntSet(uint32_t flags)
  222. {
  223. VCMP->IFS = flags;
  224. }
  225. /***************************************************************************//**
  226. * @brief
  227. * Disable one or more VCMP interrupts
  228. *
  229. * @param[in] flags
  230. * VCMP interrupt sources to disable. Use logical OR combination of valid
  231. * interrupt flags for the VCMP module (VCMP_IF_nnn)
  232. ******************************************************************************/
  233. static __INLINE void VCMP_IntDisable(uint32_t flags)
  234. {
  235. VCMP->IEN &= ~(flags);
  236. }
  237. /***************************************************************************//**
  238. * @brief
  239. * Enable one or more VCMP interrupts
  240. *
  241. * @param[in] flags
  242. * VCMP interrupt sources to enable. Use logical OR combination of valid
  243. * interrupt flags for the VCMP module (VCMP_IF_nnn)
  244. ******************************************************************************/
  245. static __INLINE void VCMP_IntEnable(uint32_t flags)
  246. {
  247. VCMP->IEN |= flags;
  248. }
  249. /***************************************************************************//**
  250. * @brief
  251. * Get pending VCMP interrupt flags
  252. *
  253. * @note
  254. * The event bits are not cleared by the use of this function
  255. *
  256. * @return
  257. * VCMP interrupt sources pending, a logical combination of valid VCMP
  258. * interrupt flags, VCMP_IF_nnn
  259. ******************************************************************************/
  260. static __INLINE uint32_t VCMP_IntGet(void)
  261. {
  262. return(VCMP->IF);
  263. }
  264. /** @} (end addtogroup VCMP) */
  265. /** @} (end addtogroup EFM32_Library) */
  266. #ifdef __cplusplus
  267. }
  268. #endif
  269. #endif /* __EFM32_VCMP_H */