1
0

em_vcmp.h 12 KB

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