am_hal_vcomp.c 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  1. //*****************************************************************************
  2. //
  3. // am_hal_vcomp.c
  4. //! @file
  5. //!
  6. //! @brief Functions for operating the on-chip Voltage Comparator
  7. //!
  8. //! @addtogroup vcomp2 Voltage Comparator (VCOMP)
  9. //! @ingroup apollo2hal
  10. //! @{
  11. //
  12. //*****************************************************************************
  13. //*****************************************************************************
  14. //
  15. // Copyright (c) 2017, Ambiq Micro
  16. // All rights reserved.
  17. //
  18. // Redistribution and use in source and binary forms, with or without
  19. // modification, are permitted provided that the following conditions are met:
  20. //
  21. // 1. Redistributions of source code must retain the above copyright notice,
  22. // this list of conditions and the following disclaimer.
  23. //
  24. // 2. Redistributions in binary form must reproduce the above copyright
  25. // notice, this list of conditions and the following disclaimer in the
  26. // documentation and/or other materials provided with the distribution.
  27. //
  28. // 3. Neither the name of the copyright holder nor the names of its
  29. // contributors may be used to endorse or promote products derived from this
  30. // software without specific prior written permission.
  31. //
  32. // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  33. // AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  34. // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  35. // ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
  36. // LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  37. // CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  38. // SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  39. // INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  40. // CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  41. // ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  42. // POSSIBILITY OF SUCH DAMAGE.
  43. //
  44. // This is part of revision 1.2.11 of the AmbiqSuite Development Package.
  45. //
  46. //*****************************************************************************
  47. #include <stdint.h>
  48. #include <stdbool.h>
  49. #include "am_mcu_apollo.h"
  50. //*****************************************************************************
  51. //
  52. //! @brief Configure the Voltage Comparator module.
  53. //!
  54. //! @param psConfig is a structure containing configuration information for the
  55. //! voltage comparator.
  56. //!
  57. //! This function configures the positive and negative input signals for the
  58. //! voltage comparator.
  59. //!
  60. //! @return None
  61. //
  62. //*****************************************************************************
  63. void
  64. am_hal_vcomp_config(const am_hal_vcomp_config_t *psConfig)
  65. {
  66. //
  67. // The configuration word should be a simple OR of the components of the
  68. // configuration structure.
  69. //
  70. AM_REG(VCOMP, CFG) = (psConfig->ui32LevelSelect |
  71. psConfig->ui32PosInput |
  72. psConfig->ui32NegInput);
  73. }
  74. //*****************************************************************************
  75. //
  76. //! @brief Set the Voltage Comparator DAC Level Select in Configuration Reg.
  77. //!
  78. //! @param ui32Level - DAC voltage selector (use macros enumerations)
  79. //!
  80. //! This function sets the DAC level select in the configuration register.
  81. //!
  82. //! @return None
  83. //
  84. //*****************************************************************************
  85. void
  86. am_hal_vcomp_dac_level_set(uint32_t ui32Level)
  87. {
  88. //
  89. // Insert the supplied level into the vcomp configuration register
  90. //
  91. AM_BFW(VCOMP, CFG, LVLSEL, ui32Level >> AM_REG_VCOMP_CFG_LVLSEL_S);
  92. }
  93. //*****************************************************************************
  94. //
  95. //! @brief Read the state of the voltage comparator.
  96. //!
  97. //! This function extracts the comparator state from the status register.
  98. //!
  99. //! @return the voltage comparator state
  100. //
  101. //*****************************************************************************
  102. bool
  103. am_hal_vcomp_read(void)
  104. {
  105. return (AM_BFR(VCOMP, STAT, CMPOUT) == 1);
  106. }
  107. //*****************************************************************************
  108. //
  109. //! @brief Enable the voltage comparator.
  110. //!
  111. //! This function powers up the voltage comparator.
  112. //!
  113. //! @return None
  114. //
  115. //*****************************************************************************
  116. void
  117. am_hal_vcomp_enable(void)
  118. {
  119. AM_REG(VCOMP, PWDKEY) = 0;
  120. }
  121. //*****************************************************************************
  122. //
  123. //! @brief Disable the voltage comparator.
  124. //!
  125. //! This function powers down the voltage comparator.
  126. //!
  127. //! @return None
  128. //
  129. //*****************************************************************************
  130. void
  131. am_hal_vcomp_disable(void)
  132. {
  133. AM_REG(VCOMP, PWDKEY) = AM_REG_VCOMP_PWDKEY_KEYVAL;
  134. }
  135. //*****************************************************************************
  136. //
  137. //! @brief Read the state of the voltage comparator interrupt status bits.
  138. //!
  139. //! @param bEnabledOnly - return the status of only the enabled interrupts.
  140. //!
  141. //! This function extracts the interrupt status bits and returns the raw or
  142. //! only the enabled based on bEnabledOnly.
  143. //!
  144. //! @return Bitwise representation of the current interrupt status.
  145. //!
  146. //! The return value will be the logical OR of one or more of the following
  147. //! values:
  148. //!
  149. //! AM_HAL_VCOMP_INT_OUTHI
  150. //! AM_HAL_VCOMP_INT_OUTLO
  151. //
  152. //*****************************************************************************
  153. uint32_t
  154. am_hal_vcomp_int_status_get(bool bEnabledOnly)
  155. {
  156. if (bEnabledOnly)
  157. {
  158. uint32_t u32RetVal = AM_REG(VCOMP, INTSTAT);
  159. return u32RetVal & AM_REG(VCOMP, INTEN);
  160. }
  161. else
  162. {
  163. return AM_REG(VCOMP, INTSTAT);
  164. }
  165. }
  166. //*****************************************************************************
  167. //
  168. //! @brief Set the state of the voltage comparator interrupt status bits.
  169. //!
  170. //! @param ui32Interrupt - interrupts to be set.
  171. //!
  172. //! This function sets the specified interrupt status bits.
  173. //!
  174. //! ui32Interrupt should be a logical or of:
  175. //!
  176. //! AM_HAL_VCOMP_INT_OUTHI
  177. //! AM_HAL_VCOMP_INT_OUTLO
  178. //!
  179. //! @return None
  180. //
  181. //*****************************************************************************
  182. void
  183. am_hal_vcomp_int_set(uint32_t ui32Interrupt)
  184. {
  185. AM_REG(VCOMP, INTSET) = ui32Interrupt;
  186. }
  187. //*****************************************************************************
  188. //
  189. //! @brief Clear the state of the voltage comparator interrupt status bits.
  190. //!
  191. //! @param ui32Interrupt - interrupts to be cleared.
  192. //!
  193. //! This function clears the specified interrupt status bits.
  194. //!
  195. //! ui32Interrupt should be a logical or of:
  196. //!
  197. //! AM_HAL_VCOMP_INT_OUTHI
  198. //! AM_HAL_VCOMP_INT_OUTLO
  199. //!
  200. //! @return None
  201. //
  202. //*****************************************************************************
  203. void
  204. am_hal_vcomp_int_clear(uint32_t ui32Interrupt)
  205. {
  206. AM_REG(VCOMP, INTCLR) = ui32Interrupt;
  207. }
  208. //*****************************************************************************
  209. //
  210. //! @brief Enable the voltage comparator interrupt status bits.
  211. //!
  212. //! @param ui32Interrupt - interrupts to be enabled.
  213. //!
  214. //! This function enables desired interrupt status bits.
  215. //!
  216. //! ui32Interrupt should be a logical or of:
  217. //!
  218. //! AM_HAL_VCOMP_INT_OUTHI
  219. //! AM_HAL_VCOMP_INT_OUTLO
  220. //!
  221. //! @return None
  222. //
  223. //*****************************************************************************
  224. void
  225. am_hal_vcomp_int_enable(uint32_t ui32Interrupt)
  226. {
  227. AM_REG(VCOMP, INTEN) |= ui32Interrupt;
  228. }
  229. //*****************************************************************************
  230. //
  231. //! @brief Return the enabled, voltage comparator interrupt status bits.
  232. //!
  233. //! This function returns the enabled interrupt status bits
  234. //!
  235. //! @return returns the enabled interrupt status bits. The return is a logical
  236. //! or of:
  237. //!
  238. //! AM_HAL_VCOMP_INT_OUTHI
  239. //! AM_HAL_VCOMP_INT_OUTLO
  240. //
  241. //*****************************************************************************
  242. uint32_t
  243. am_hal_vcomp_int_enable_get(void)
  244. {
  245. return AM_REG(VCOMP, INTEN);
  246. }
  247. //*****************************************************************************
  248. //
  249. //! @brief Disable the voltage comparator interrupt status bits.
  250. //!
  251. //! @param ui32Interrupt - interrupts to be disabled.
  252. //!
  253. //! This function disables desired interrupt status bits.
  254. //!
  255. //! ui32Interrupt should be a logical or of:
  256. //!
  257. //! AM_HAL_VCOMP_INT_OUTHI
  258. //! AM_HAL_VCOMP_INT_OUTLO
  259. //!
  260. //! @return None
  261. //
  262. //*****************************************************************************
  263. void
  264. am_hal_vcomp_int_disable(uint32_t ui32Interrupt)
  265. {
  266. AM_REG(VCOMP, INTEN) &= ~ui32Interrupt;
  267. }
  268. //*****************************************************************************
  269. //
  270. // End Doxygen group.
  271. //! @}
  272. //
  273. //*****************************************************************************