comp.c 15 KB

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