comp.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449
  1. //*****************************************************************************
  2. //
  3. // comp.c - Driver for the analog comparator.
  4. //
  5. // Copyright (c) 2005-2017 Texas Instruments Incorporated. All rights reserved.
  6. // Software License Agreement
  7. //
  8. // Redistribution and use in source and binary forms, with or without
  9. // modification, are permitted provided that the following conditions
  10. // are met:
  11. //
  12. // Redistributions of source code must retain the above copyright
  13. // notice, this list of conditions and the following disclaimer.
  14. //
  15. // Redistributions in binary form must reproduce the above copyright
  16. // notice, this list of conditions and the following disclaimer in the
  17. // documentation and/or other materials provided with the
  18. // distribution.
  19. //
  20. // Neither the name of Texas Instruments Incorporated nor the names of
  21. // its contributors may be used to endorse or promote products derived
  22. // from this software without specific prior written permission.
  23. //
  24. // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  25. // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  26. // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  27. // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  28. // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  29. // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  30. // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  31. // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  32. // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  33. // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  34. // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  35. //
  36. //*****************************************************************************
  37. //*****************************************************************************
  38. //
  39. //! \addtogroup comp_api
  40. //! @{
  41. //
  42. //*****************************************************************************
  43. #include <ti/devices/msp432e4/inc/msp432e411y.h>
  44. #include "types.h"
  45. #include <stdbool.h>
  46. #include <stdint.h>
  47. #include "inc/hw_comp.h"
  48. #include "comp.h"
  49. #include "debug.h"
  50. #include "interrupt.h"
  51. //*****************************************************************************
  52. //
  53. //! Configures a comparator.
  54. //!
  55. //! \param ui32Base is the base address of the comparator module.
  56. //! \param ui32Comp is the index of the comparator to configure.
  57. //! \param ui32Config is the configuration of the comparator.
  58. //!
  59. //! This function configures a comparator. The \e ui32Config parameter is the
  60. //! result of a logical OR operation between the \b COMP_TRIG_xxx,
  61. //! \b COMP_INT_xxx, \b COMP_ASRCP_xxx, and \b COMP_OUTPUT_xxx values.
  62. //!
  63. //! The \b COMP_TRIG_xxx term can take on the following values:
  64. //!
  65. //! - \b COMP_TRIG_NONE to have no trigger to the ADC.
  66. //! - \b COMP_TRIG_HIGH to trigger the ADC when the comparator output is high.
  67. //! - \b COMP_TRIG_LOW to trigger the ADC when the comparator output is low.
  68. //! - \b COMP_TRIG_FALL to trigger the ADC when the comparator output goes low.
  69. //! - \b COMP_TRIG_RISE to trigger the ADC when the comparator output goes
  70. //! high.
  71. //! - \b COMP_TRIG_BOTH to trigger the ADC when the comparator output goes low
  72. //! or high.
  73. //!
  74. //! The \b COMP_INT_xxx term can take on the following values:
  75. //!
  76. //! - \b COMP_INT_HIGH to generate an interrupt when the comparator output is
  77. //! high.
  78. //! - \b COMP_INT_LOW to generate an interrupt when the comparator output is
  79. //! low.
  80. //! - \b COMP_INT_FALL to generate an interrupt when the comparator output goes
  81. //! low.
  82. //! - \b COMP_INT_RISE to generate an interrupt when the comparator output goes
  83. //! high.
  84. //! - \b COMP_INT_BOTH to generate an interrupt when the comparator output goes
  85. //! low or high.
  86. //!
  87. //! The \b COMP_ASRCP_xxx term can take on the following values:
  88. //!
  89. //! - \b COMP_ASRCP_PIN to use the dedicated Comp+ pin as the reference
  90. //! voltage.
  91. //! - \b COMP_ASRCP_PIN0 to use the Comp0+ pin as the reference voltage (this
  92. //! the same as \b COMP_ASRCP_PIN for the comparator 0).
  93. //! - \b COMP_ASRCP_REF to use the internally generated voltage as the
  94. //! reference voltage.
  95. //!
  96. //! The \b COMP_OUTPUT_xxx term can take on the following values:
  97. //!
  98. //! - \b COMP_OUTPUT_NORMAL to enable a non-inverted output from the comparator
  99. //! to a device pin.
  100. //! - \b COMP_OUTPUT_INVERT to enable an inverted output from the comparator to
  101. //! a device pin.
  102. //!
  103. //! \return None.
  104. //
  105. //*****************************************************************************
  106. void
  107. ComparatorConfigure(uint32_t ui32Base, uint32_t ui32Comp, uint32_t ui32Config)
  108. {
  109. //
  110. // Check the arguments.
  111. //
  112. ASSERT(ui32Base == COMP_BASE);
  113. ASSERT(ui32Comp < 3);
  114. //
  115. // Configure this comparator.
  116. //
  117. HWREG(ui32Base + (ui32Comp * 0x20) + COMP_O_ACCTL0) = ui32Config;
  118. }
  119. //*****************************************************************************
  120. //
  121. //! Sets the internal reference voltage.
  122. //!
  123. //! \param ui32Base is the base address of the comparator module.
  124. //! \param ui32Ref is the desired reference voltage.
  125. //!
  126. //! This function sets the internal reference voltage value. The voltage is
  127. //! specified as one of the following values:
  128. //!
  129. //! - \b COMP_REF_OFF to turn off the reference voltage
  130. //! - \b COMP_REF_0V to set the reference voltage to 0 V
  131. //! - \b COMP_REF_0_1375V to set the reference voltage to 0.1375 V
  132. //! - \b COMP_REF_0_275V to set the reference voltage to 0.275 V
  133. //! - \b COMP_REF_0_4125V to set the reference voltage to 0.4125 V
  134. //! - \b COMP_REF_0_55V to set the reference voltage to 0.55 V
  135. //! - \b COMP_REF_0_6875V to set the reference voltage to 0.6875 V
  136. //! - \b COMP_REF_0_825V to set the reference voltage to 0.825 V
  137. //! - \b COMP_REF_0_928125V to set the reference voltage to 0.928125 V
  138. //! - \b COMP_REF_0_9625V to set the reference voltage to 0.9625 V
  139. //! - \b COMP_REF_1_03125V to set the reference voltage to 1.03125 V
  140. //! - \b COMP_REF_1_134375V to set the reference voltage to 1.134375 V
  141. //! - \b COMP_REF_1_1V to set the reference voltage to 1.1 V
  142. //! - \b COMP_REF_1_2375V to set the reference voltage to 1.2375 V
  143. //! - \b COMP_REF_1_340625V to set the reference voltage to 1.340625 V
  144. //! - \b COMP_REF_1_375V to set the reference voltage to 1.375 V
  145. //! - \b COMP_REF_1_44375V to set the reference voltage to 1.44375 V
  146. //! - \b COMP_REF_1_5125V to set the reference voltage to 1.5125 V
  147. //! - \b COMP_REF_1_546875V to set the reference voltage to 1.546875 V
  148. //! - \b COMP_REF_1_65V to set the reference voltage to 1.65 V
  149. //! - \b COMP_REF_1_753125V to set the reference voltage to 1.753125 V
  150. //! - \b COMP_REF_1_7875V to set the reference voltage to 1.7875 V
  151. //! - \b COMP_REF_1_85625V to set the reference voltage to 1.85625 V
  152. //! - \b COMP_REF_1_925V to set the reference voltage to 1.925 V
  153. //! - \b COMP_REF_1_959375V to set the reference voltage to 1.959375 V
  154. //! - \b COMP_REF_2_0625V to set the reference voltage to 2.0625 V
  155. //! - \b COMP_REF_2_165625V to set the reference voltage to 2.165625 V
  156. //! - \b COMP_REF_2_26875V to set the reference voltage to 2.26875 V
  157. //! - \b COMP_REF_2_371875V to set the reference voltage to 2.371875 V
  158. //!
  159. //! \return None.
  160. //
  161. //*****************************************************************************
  162. void
  163. ComparatorRefSet(uint32_t ui32Base, uint32_t ui32Ref)
  164. {
  165. //
  166. // Check the arguments.
  167. //
  168. ASSERT(ui32Base == COMP_BASE);
  169. //
  170. // Set the voltage reference voltage as requested.
  171. //
  172. HWREG(ui32Base + COMP_O_ACREFCTL) = ui32Ref;
  173. }
  174. //*****************************************************************************
  175. //
  176. //! Gets the current comparator output value.
  177. //!
  178. //! \param ui32Base is the base address of the comparator module.
  179. //! \param ui32Comp is the index of the comparator.
  180. //!
  181. //! This function retrieves the current value of the comparator output.
  182. //!
  183. //! \return Returns \b true if the comparator output is high and \b false if
  184. //! the comparator output is low.
  185. //
  186. //*****************************************************************************
  187. bool
  188. ComparatorValueGet(uint32_t ui32Base, uint32_t ui32Comp)
  189. {
  190. //
  191. // Check the arguments.
  192. //
  193. ASSERT(ui32Base == COMP_BASE);
  194. ASSERT(ui32Comp < 3);
  195. //
  196. // Return the appropriate value based on the comparator's present output
  197. // value.
  198. //
  199. if (HWREG(ui32Base + (ui32Comp * 0x20) + COMP_O_ACSTAT0) &
  200. COMP_ACSTAT0_OVAL)
  201. {
  202. return (true);
  203. }
  204. else
  205. {
  206. return (false);
  207. }
  208. }
  209. //*****************************************************************************
  210. //
  211. //! Registers an interrupt handler for the comparator interrupt.
  212. //!
  213. //! \param ui32Base is the base address of the comparator module.
  214. //! \param ui32Comp is the index of the comparator.
  215. //! \param pfnHandler is a pointer to the function to be called when the
  216. //! comparator interrupt occurs.
  217. //!
  218. //! This function sets the handler to be called when the comparator interrupt
  219. //! occurs and enables the interrupt in the interrupt controller. It is the
  220. //! interrupt handler's responsibility to clear the interrupt source via
  221. //! ComparatorIntClear().
  222. //!
  223. //! \sa IntRegister() for important information about registering interrupt
  224. //! handlers.
  225. //!
  226. //! \return None.
  227. //
  228. //*****************************************************************************
  229. void
  230. ComparatorIntRegister(uint32_t ui32Base, uint32_t ui32Comp,
  231. void (*pfnHandler)(void))
  232. {
  233. //
  234. // Check the arguments.
  235. //
  236. ASSERT(ui32Base == COMP_BASE);
  237. ASSERT(ui32Comp < 3);
  238. //
  239. // Register the interrupt handler, returning an error if an error occurs.
  240. //
  241. IntRegister(INT_COMP0 + ui32Comp, pfnHandler);
  242. //
  243. // Enable the interrupt in the interrupt controller.
  244. //
  245. IntEnable(INT_COMP0 + ui32Comp);
  246. //
  247. // Enable the comparator interrupt.
  248. //
  249. HWREG(ui32Base + COMP_O_ACINTEN) |= 1 << ui32Comp;
  250. }
  251. //*****************************************************************************
  252. //
  253. //! Unregisters an interrupt handler for a comparator interrupt.
  254. //!
  255. //! \param ui32Base is the base address of the comparator module.
  256. //! \param ui32Comp is the index of the comparator.
  257. //!
  258. //! This function clears the handler to be called when a comparator interrupt
  259. //! occurs. This function also masks off the interrupt in the interrupt
  260. //! controller so that the interrupt handler no longer is called.
  261. //!
  262. //! \sa IntRegister() for important information about registering interrupt
  263. //! handlers.
  264. //!
  265. //! \return None.
  266. //
  267. //*****************************************************************************
  268. void
  269. ComparatorIntUnregister(uint32_t ui32Base, uint32_t ui32Comp)
  270. {
  271. //
  272. // Check the arguments.
  273. //
  274. ASSERT(ui32Base == COMP_BASE);
  275. ASSERT(ui32Comp < 3);
  276. //
  277. // Disable the comparator interrupt.
  278. //
  279. HWREG(ui32Base + COMP_O_ACINTEN) &= ~(1 << ui32Comp);
  280. //
  281. // Disable the interrupt in the interrupt controller.
  282. //
  283. IntDisable(INT_COMP0 + ui32Comp);
  284. //
  285. // Unregister the interrupt handler.
  286. //
  287. IntUnregister(INT_COMP0 + ui32Comp);
  288. }
  289. //*****************************************************************************
  290. //
  291. //! Enables the comparator interrupt.
  292. //!
  293. //! \param ui32Base is the base address of the comparator module.
  294. //! \param ui32Comp is the index of the comparator.
  295. //!
  296. //! This function enables generation of an interrupt from the specified
  297. //! comparator. Only enabled comparator interrupts can be reflected
  298. //! to the processor.
  299. //!
  300. //! \return None.
  301. //
  302. //*****************************************************************************
  303. void
  304. ComparatorIntEnable(uint32_t ui32Base, uint32_t ui32Comp)
  305. {
  306. //
  307. // Check the arguments.
  308. //
  309. ASSERT(ui32Base == COMP_BASE);
  310. ASSERT(ui32Comp < 3);
  311. //
  312. // Enable the comparator interrupt.
  313. //
  314. HWREG(ui32Base + COMP_O_ACINTEN) |= 1 << ui32Comp;
  315. }
  316. //*****************************************************************************
  317. //
  318. //! Disables the comparator interrupt.
  319. //!
  320. //! \param ui32Base is the base address of the comparator module.
  321. //! \param ui32Comp is the index of the comparator.
  322. //!
  323. //! This function disables generation of an interrupt from the specified
  324. //! comparator. Only enabled comparator interrupts can be reflected
  325. //! to the processor.
  326. //!
  327. //! \return None.
  328. //
  329. //*****************************************************************************
  330. void
  331. ComparatorIntDisable(uint32_t ui32Base, uint32_t ui32Comp)
  332. {
  333. //
  334. // Check the arguments.
  335. //
  336. ASSERT(ui32Base == COMP_BASE);
  337. ASSERT(ui32Comp < 3);
  338. //
  339. // Disable the comparator interrupt.
  340. //
  341. HWREG(ui32Base + COMP_O_ACINTEN) &= ~(1 << ui32Comp);
  342. }
  343. //*****************************************************************************
  344. //
  345. //! Gets the current interrupt status.
  346. //!
  347. //! \param ui32Base is the base address of the comparator module.
  348. //! \param ui32Comp is the index of the comparator.
  349. //! \param bMasked is \b false if the raw interrupt status is required and
  350. //! \b true if the masked interrupt status is required.
  351. //!
  352. //! This function returns the interrupt status for the comparator. Either the
  353. //! raw or the masked interrupt status can be returned.
  354. //!
  355. //! \return \b true if the interrupt is asserted and \b false if it is not
  356. //! asserted.
  357. //
  358. //*****************************************************************************
  359. bool
  360. ComparatorIntStatus(uint32_t ui32Base, uint32_t ui32Comp, bool bMasked)
  361. {
  362. //
  363. // Check the arguments.
  364. //
  365. ASSERT(ui32Base == COMP_BASE);
  366. ASSERT(ui32Comp < 3);
  367. //
  368. // Return either the interrupt status or the raw interrupt status as
  369. // requested.
  370. //
  371. if (bMasked)
  372. {
  373. return (((HWREG(ui32Base + COMP_O_ACMIS) >> ui32Comp) & 1) ? true :
  374. false);
  375. }
  376. else
  377. {
  378. return (((HWREG(ui32Base + COMP_O_ACRIS) >> ui32Comp) & 1) ? true :
  379. false);
  380. }
  381. }
  382. //*****************************************************************************
  383. //
  384. //! Clears a comparator interrupt.
  385. //!
  386. //! \param ui32Base is the base address of the comparator module.
  387. //! \param ui32Comp is the index of the comparator.
  388. //!
  389. //! The comparator interrupt is cleared, so that it no longer asserts. This
  390. //! function must be called in the interrupt handler to keep the handler from
  391. //! being called again immediately upon exit. Note that for a level-triggered
  392. //! interrupt, the interrupt cannot be cleared until it stops asserting.
  393. //!
  394. //! \note Because there is a write buffer in the Cortex-M processor, it may
  395. //! take several clock cycles before the interrupt source is actually cleared.
  396. //! Therefore, it is recommended that the interrupt source be cleared early in
  397. //! the interrupt handler (as opposed to the very last action) to avoid
  398. //! returning from the interrupt handler before the interrupt source is
  399. //! actually cleared. Failure to do so may result in the interrupt handler
  400. //! being immediately reentered (because the interrupt controller still sees
  401. //! the interrupt source asserted).
  402. //!
  403. //! \return None.
  404. //
  405. //*****************************************************************************
  406. void
  407. ComparatorIntClear(uint32_t ui32Base, uint32_t ui32Comp)
  408. {
  409. //
  410. // Check the arguments.
  411. //
  412. ASSERT(ui32Base == COMP_BASE);
  413. ASSERT(ui32Comp < 3);
  414. //
  415. // Clear the interrupt.
  416. //
  417. HWREG(ui32Base + COMP_O_ACMIS) = 1 << ui32Comp;
  418. }
  419. //*****************************************************************************
  420. //
  421. // Close the Doxygen group.
  422. //! @}
  423. //
  424. //*****************************************************************************