comp.c 15 KB

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