comp.c 15 KB

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