em_vcmp.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  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. #include "em_assert.h"
  34. #include "em_vcmp.h"
  35. /***************************************************************************//**
  36. * @addtogroup EM_Library
  37. * @{
  38. ******************************************************************************/
  39. /***************************************************************************//**
  40. * @addtogroup VCMP
  41. * @brief Voltage Comparator (VCMP) Peripheral API
  42. * @{
  43. ******************************************************************************/
  44. /***************************************************************************//**
  45. * @brief
  46. * Configure and enable Voltage Comparator
  47. *
  48. * @param[in] vcmpInit
  49. * VCMP Initialization structure
  50. ******************************************************************************/
  51. void VCMP_Init(const VCMP_Init_TypeDef *vcmpInit)
  52. {
  53. /* Verify input */
  54. EFM_ASSERT((vcmpInit->inactive == 0) || (vcmpInit->inactive == 1));
  55. EFM_ASSERT((vcmpInit->biasProg >= 0) && (vcmpInit->biasProg < 16));
  56. /* Configure Half Bias setting */
  57. if (vcmpInit->halfBias)
  58. {
  59. VCMP->CTRL |= VCMP_CTRL_HALFBIAS;
  60. }
  61. else
  62. {
  63. VCMP->CTRL &= ~(VCMP_CTRL_HALFBIAS);
  64. }
  65. /* Configure bias prog */
  66. VCMP->CTRL &= ~(_VCMP_CTRL_BIASPROG_MASK);
  67. VCMP->CTRL |= (vcmpInit->biasProg << _VCMP_CTRL_BIASPROG_SHIFT);
  68. /* Configure sense for falling edge */
  69. if (vcmpInit->irqFalling)
  70. {
  71. VCMP->CTRL |= VCMP_CTRL_IFALL;
  72. }
  73. else
  74. {
  75. VCMP->CTRL &= ~(VCMP_CTRL_IFALL);
  76. }
  77. /* Configure sense for rising edge */
  78. if (vcmpInit->irqRising)
  79. {
  80. VCMP->CTRL |= VCMP_CTRL_IRISE;
  81. }
  82. else
  83. {
  84. VCMP->CTRL &= ~(VCMP_CTRL_IRISE);
  85. }
  86. /* Configure warm-up time */
  87. VCMP->CTRL &= ~(_VCMP_CTRL_WARMTIME_MASK);
  88. VCMP->CTRL |= (vcmpInit->warmup << _VCMP_CTRL_WARMTIME_SHIFT);
  89. /* Configure hysteresis */
  90. switch (vcmpInit->hyst)
  91. {
  92. case vcmpHyst20mV:
  93. VCMP->CTRL |= VCMP_CTRL_HYSTEN;
  94. break;
  95. case vcmpHystNone:
  96. VCMP->CTRL &= ~(VCMP_CTRL_HYSTEN);
  97. break;
  98. default:
  99. break;
  100. }
  101. /* Configure inactive output value */
  102. VCMP->CTRL |= (vcmpInit->inactive << _VCMP_CTRL_INACTVAL_SHIFT);
  103. /* Configure trigger level */
  104. VCMP_TriggerSet(vcmpInit->triggerLevel);
  105. /* Enable or disable VCMP */
  106. if (vcmpInit->enable)
  107. {
  108. VCMP->CTRL |= VCMP_CTRL_EN;
  109. }
  110. else
  111. {
  112. VCMP->CTRL &= ~(VCMP_CTRL_EN);
  113. }
  114. /* If Low Power Reference is enabled, wait until VCMP is ready */
  115. /* before enabling it, see reference manual for deatils */
  116. /* Configuring Low Power Ref without enable has no effect */
  117. if(vcmpInit->lowPowerRef && vcmpInit->enable)
  118. {
  119. /* Poll for VCMP ready */
  120. while(!VCMP_Ready());
  121. VCMP_LowPowerRefSet(vcmpInit->lowPowerRef);
  122. }
  123. /* Clear edge interrupt */
  124. VCMP_IntClear(VCMP_IF_EDGE);
  125. }
  126. /***************************************************************************//**
  127. * @brief
  128. * Enable or disable Low Power Reference setting
  129. *
  130. * @param[in] enable
  131. * If true, enables low power reference, if false disable low power reference
  132. ******************************************************************************/
  133. void VCMP_LowPowerRefSet(bool enable)
  134. {
  135. if (enable)
  136. {
  137. VCMP->INPUTSEL |= VCMP_INPUTSEL_LPREF;
  138. }
  139. else
  140. {
  141. VCMP->INPUTSEL &= ~(VCMP_INPUTSEL_LPREF);
  142. }
  143. }
  144. /***************************************************************************//**
  145. * @brief
  146. * Configure trigger level of voltage comparator
  147. *
  148. * @param[in] level
  149. * Trigger value, in range 0-63
  150. ******************************************************************************/
  151. void VCMP_TriggerSet(int level)
  152. {
  153. /* Trigger range is 6 bits, value from 0-63 */
  154. EFM_ASSERT((level > 0) && (level < 64));
  155. /* Set trigger level */
  156. VCMP->INPUTSEL = (VCMP->INPUTSEL & ~(_VCMP_INPUTSEL_TRIGLEVEL_MASK)) |
  157. (level << _VCMP_INPUTSEL_TRIGLEVEL_SHIFT);
  158. }
  159. /** @} (end addtogroup VCMP) */
  160. /** @} (end addtogroup EM_Library) */