adc.c 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979
  1. //*****************************************************************************
  2. //
  3. // adc.c - Driver for the ADC.
  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 adc_api
  30. //! @{
  31. //
  32. //*****************************************************************************
  33. #include "inc/hw_adc.h"
  34. #include "inc/hw_ints.h"
  35. #include "inc/hw_memmap.h"
  36. #include "inc/hw_types.h"
  37. #include "driverlib/adc.h"
  38. #include "driverlib/debug.h"
  39. #include "driverlib/interrupt.h"
  40. //*****************************************************************************
  41. //
  42. // These defines are used by the ADC driver to simplify access to the ADC
  43. // sequencer's registers.
  44. //
  45. //*****************************************************************************
  46. #define ADC_SEQ (ADC_O_SSMUX0)
  47. #define ADC_SEQ_STEP (ADC_O_SSMUX1 - ADC_O_SSMUX0)
  48. #define ADC_SSMUX (ADC_O_SSMUX0 - ADC_O_SSMUX0)
  49. #define ADC_SSCTL (ADC_O_SSCTL0 - ADC_O_SSMUX0)
  50. #define ADC_SSFIFO (ADC_O_SSFIFO0 - ADC_O_SSMUX0)
  51. #define ADC_SSFSTAT (ADC_O_SSFSTAT0 - ADC_O_SSMUX0)
  52. //*****************************************************************************
  53. //
  54. // The currently configured software oversampling factor for each of the ADC
  55. // sequencers.
  56. //
  57. //*****************************************************************************
  58. static unsigned char g_pucOversampleFactor[3];
  59. //*****************************************************************************
  60. //
  61. //! Registers an interrupt handler for an ADC interrupt.
  62. //!
  63. //! \param ulBase is the base address of the ADC module.
  64. //! \param ulSequenceNum is the sample sequence number.
  65. //! \param pfnHandler is a pointer to the function to be called when the
  66. //! ADC sample sequence interrupt occurs.
  67. //!
  68. //! This function sets the handler to be called when a sample sequence
  69. //! interrupt occurs. This will enable the global interrupt in the interrupt
  70. //! controller; the sequence interrupt must be enabled with ADCIntEnable(). It
  71. //! is the interrupt handler's responsibility to clear the interrupt source via
  72. //! ADCIntClear().
  73. //!
  74. //! \sa IntRegister() for important information about registering interrupt
  75. //! handlers.
  76. //!
  77. //! \return None.
  78. //
  79. //*****************************************************************************
  80. void
  81. ADCIntRegister(unsigned long ulBase, unsigned long ulSequenceNum,
  82. void (*pfnHandler)(void))
  83. {
  84. unsigned long ulInt;
  85. //
  86. // Check the arguments.
  87. //
  88. ASSERT((ulBase == ADC0_BASE) || (ulBase == ADC1_BASE));
  89. ASSERT(ulSequenceNum < 4);
  90. //
  91. // Determine the interrupt to register based on the sequence number.
  92. //
  93. ulInt = INT_ADC0 + ulSequenceNum;
  94. //
  95. // Register the interrupt handler.
  96. //
  97. IntRegister(ulInt, pfnHandler);
  98. //
  99. // Enable the timer interrupt.
  100. //
  101. IntEnable(ulInt);
  102. }
  103. //*****************************************************************************
  104. //
  105. //! Unregisters the interrupt handler for an ADC interrupt.
  106. //!
  107. //! \param ulBase is the base address of the ADC module.
  108. //! \param ulSequenceNum is the sample sequence number.
  109. //!
  110. //! This function unregisters the interrupt handler. This will disable the
  111. //! global interrupt in the interrupt controller; the sequence interrupt must
  112. //! be disabled via ADCIntDisable().
  113. //!
  114. //! \sa IntRegister() for important information about registering interrupt
  115. //! handlers.
  116. //!
  117. //! \return None.
  118. //
  119. //*****************************************************************************
  120. void
  121. ADCIntUnregister(unsigned long ulBase, unsigned long ulSequenceNum)
  122. {
  123. unsigned long ulInt;
  124. //
  125. // Check the arguments.
  126. //
  127. ASSERT((ulBase == ADC0_BASE) || (ulBase == ADC1_BASE));
  128. ASSERT(ulSequenceNum < 4);
  129. //
  130. // Determine the interrupt to unregister based on the sequence number.
  131. //
  132. ulInt = INT_ADC0 + ulSequenceNum;
  133. //
  134. // Disable the interrupt.
  135. //
  136. IntDisable(ulInt);
  137. //
  138. // Unregister the interrupt handler.
  139. //
  140. IntUnregister(ulInt);
  141. }
  142. //*****************************************************************************
  143. //
  144. //! Disables a sample sequence interrupt.
  145. //!
  146. //! \param ulBase is the base address of the ADC module.
  147. //! \param ulSequenceNum is the sample sequence number.
  148. //!
  149. //! This function disables the requested sample sequence interrupt.
  150. //!
  151. //! \return None.
  152. //
  153. //*****************************************************************************
  154. void
  155. ADCIntDisable(unsigned long ulBase, unsigned long ulSequenceNum)
  156. {
  157. //
  158. // Check the arguments.
  159. //
  160. ASSERT((ulBase == ADC0_BASE) || (ulBase == ADC1_BASE));
  161. ASSERT(ulSequenceNum < 4);
  162. //
  163. // Disable this sample sequence interrupt.
  164. //
  165. HWREG(ulBase + ADC_O_IM) &= ~(1 << ulSequenceNum);
  166. }
  167. //*****************************************************************************
  168. //
  169. //! Enables a sample sequence interrupt.
  170. //!
  171. //! \param ulBase is the base address of the ADC module.
  172. //! \param ulSequenceNum is the sample sequence number.
  173. //!
  174. //! This function enables the requested sample sequence interrupt. Any
  175. //! outstanding interrupts are cleared before enabling the sample sequence
  176. //! interrupt.
  177. //!
  178. //! \return None.
  179. //
  180. //*****************************************************************************
  181. void
  182. ADCIntEnable(unsigned long ulBase, unsigned long ulSequenceNum)
  183. {
  184. //
  185. // Check the arguments.
  186. //
  187. ASSERT((ulBase == ADC0_BASE) || (ulBase == ADC1_BASE));
  188. ASSERT(ulSequenceNum < 4);
  189. //
  190. // Clear any outstanding interrupts on this sample sequence.
  191. //
  192. HWREG(ulBase + ADC_O_ISC) = 1 << ulSequenceNum;
  193. //
  194. // Enable this sample sequence interrupt.
  195. //
  196. HWREG(ulBase + ADC_O_IM) |= 1 << ulSequenceNum;
  197. }
  198. //*****************************************************************************
  199. //
  200. //! Gets the current interrupt status.
  201. //!
  202. //! \param ulBase is the base address of the ADC module.
  203. //! \param ulSequenceNum is the sample sequence number.
  204. //! \param bMasked is false if the raw interrupt status is required and true if
  205. //! the masked interrupt status is required.
  206. //!
  207. //! This returns the interrupt status for the specified sample sequence.
  208. //! Either the raw interrupt status or the status of interrupts that are
  209. //! allowed to reflect to the processor can be returned.
  210. //!
  211. //! \return The current raw or masked interrupt status.
  212. //
  213. //*****************************************************************************
  214. unsigned long
  215. ADCIntStatus(unsigned long ulBase, unsigned long ulSequenceNum,
  216. tBoolean bMasked)
  217. {
  218. //
  219. // Check the arguments.
  220. //
  221. ASSERT((ulBase == ADC0_BASE) || (ulBase == ADC1_BASE));
  222. ASSERT(ulSequenceNum < 4);
  223. //
  224. // Return either the interrupt status or the raw interrupt status as
  225. // requested.
  226. //
  227. if(bMasked)
  228. {
  229. return(HWREG(ulBase + ADC_O_ISC) & (1 << ulSequenceNum));
  230. }
  231. else
  232. {
  233. return(HWREG(ulBase + ADC_O_RIS) & (1 << ulSequenceNum));
  234. }
  235. }
  236. //*****************************************************************************
  237. //
  238. //! Clears sample sequence interrupt source.
  239. //!
  240. //! \param ulBase is the base address of the ADC module.
  241. //! \param ulSequenceNum is the sample sequence number.
  242. //!
  243. //! The specified sample sequence interrupt is cleared, so that it no longer
  244. //! asserts. This must be done in the interrupt handler to keep it from being
  245. //! called again immediately upon exit.
  246. //!
  247. //! \note Since there is a write buffer in the Cortex-M3 processor, it may take
  248. //! several clock cycles before the interrupt source is actually cleared.
  249. //! Therefore, it is recommended that the interrupt source be cleared early in
  250. //! the interrupt handler (as opposed to the very last action) to avoid
  251. //! returning from the interrupt handler before the interrupt source is
  252. //! actually cleared. Failure to do so may result in the interrupt handler
  253. //! being immediately reentered (since NVIC still sees the interrupt source
  254. //! asserted).
  255. //!
  256. //! \return None.
  257. //
  258. //*****************************************************************************
  259. void
  260. ADCIntClear(unsigned long ulBase, unsigned long ulSequenceNum)
  261. {
  262. //
  263. // Check the arugments.
  264. //
  265. ASSERT((ulBase == ADC0_BASE) || (ulBase == ADC1_BASE));
  266. ASSERT(ulSequenceNum < 4);
  267. //
  268. // Clear the interrupt.
  269. //
  270. HWREG(ulBase + ADC_O_ISC) = 1 << ulSequenceNum;
  271. }
  272. //*****************************************************************************
  273. //
  274. //! Enables a sample sequence.
  275. //!
  276. //! \param ulBase is the base address of the ADC module.
  277. //! \param ulSequenceNum is the sample sequence number.
  278. //!
  279. //! Allows the specified sample sequence to be captured when its trigger is
  280. //! detected. A sample sequence must be configured before it is enabled.
  281. //!
  282. //! \return None.
  283. //
  284. //*****************************************************************************
  285. void
  286. ADCSequenceEnable(unsigned long ulBase, unsigned long ulSequenceNum)
  287. {
  288. //
  289. // Check the arugments.
  290. //
  291. ASSERT((ulBase == ADC0_BASE) || (ulBase == ADC1_BASE));
  292. ASSERT(ulSequenceNum < 4);
  293. //
  294. // Enable the specified sequence.
  295. //
  296. HWREG(ulBase + ADC_O_ACTSS) |= 1 << ulSequenceNum;
  297. }
  298. //*****************************************************************************
  299. //
  300. //! Disables a sample sequence.
  301. //!
  302. //! \param ulBase is the base address of the ADC module.
  303. //! \param ulSequenceNum is the sample sequence number.
  304. //!
  305. //! Prevents the specified sample sequence from being captured when its trigger
  306. //! is detected. A sample sequence should be disabled before it is configured.
  307. //!
  308. //! \return None.
  309. //
  310. //*****************************************************************************
  311. void
  312. ADCSequenceDisable(unsigned long ulBase, unsigned long ulSequenceNum)
  313. {
  314. //
  315. // Check the arugments.
  316. //
  317. ASSERT((ulBase == ADC0_BASE) || (ulBase == ADC1_BASE));
  318. ASSERT(ulSequenceNum < 4);
  319. //
  320. // Disable the specified sequences.
  321. //
  322. HWREG(ulBase + ADC_O_ACTSS) &= ~(1 << ulSequenceNum);
  323. }
  324. //*****************************************************************************
  325. //
  326. //! Configures the trigger source and priority of a sample sequence.
  327. //!
  328. //! \param ulBase is the base address of the ADC module.
  329. //! \param ulSequenceNum is the sample sequence number.
  330. //! \param ulTrigger is the trigger source that initiates the sample sequence;
  331. //! must be one of the \b ADC_TRIGGER_* values.
  332. //! \param ulPriority is the relative priority of the sample sequence with
  333. //! respect to the other sample sequences.
  334. //!
  335. //! This function configures the initiation criteria for a sample sequence.
  336. //! Valid sample sequences range from zero to three; sequence zero will capture
  337. //! up to eight samples, sequences one and two will capture up to four samples,
  338. //! and sequence three will capture a single sample. The trigger condition and
  339. //! priority (with respect to other sample sequence execution) is set.
  340. //!
  341. //! The \e ulTrigger parameter can take on the following values:
  342. //!
  343. //! - \b ADC_TRIGGER_PROCESSOR - A trigger generated by the processor, via the
  344. //! ADCProcessorTrigger() function.
  345. //! - \b ADC_TRIGGER_COMP0 - A trigger generated by the first analog
  346. //! comparator; configured with ComparatorConfigure().
  347. //! - \b ADC_TRIGGER_COMP1 - A trigger generated by the second analog
  348. //! comparator; configured with ComparatorConfigure().
  349. //! - \b ADC_TRIGGER_COMP2 - A trigger generated by the third analog
  350. //! comparator; configured with ComparatorConfigure().
  351. //! - \b ADC_TRIGGER_EXTERNAL - A trigger generated by an input from the Port
  352. //! B4 pin.
  353. //! - \b ADC_TRIGGER_TIMER - A trigger generated by a timer; configured with
  354. //! TimerControlTrigger().
  355. //! - \b ADC_TRIGGER_PWM0 - A trigger generated by the first PWM generator;
  356. //! configured with PWMGenIntTrigEnable().
  357. //! - \b ADC_TRIGGER_PWM1 - A trigger generated by the second PWM generator;
  358. //! configured with PWMGenIntTrigEnable().
  359. //! - \b ADC_TRIGGER_PWM2 - A trigger generated by the third PWM generator;
  360. //! configured with PWMGenIntTrigEnable().
  361. //! - \b ADC_TRIGGER_ALWAYS - A trigger that is always asserted, causing the
  362. //! sample sequence to capture repeatedly (so long as
  363. //! there is not a higher priority source active).
  364. //!
  365. //! Note that not all trigger sources are available on all Stellaris family
  366. //! members; consult the data sheet for the device in question to determine the
  367. //! availability of triggers.
  368. //!
  369. //! The \e ulPriority parameter is a value between 0 and 3, where 0 represents
  370. //! the highest priority and 3 the lowest. Note that when programming the
  371. //! priority among a set of sample sequences, each must have unique priority;
  372. //! it is up to the caller to guarantee the uniqueness of the priorities.
  373. //!
  374. //! \return None.
  375. //
  376. //*****************************************************************************
  377. void
  378. ADCSequenceConfigure(unsigned long ulBase, unsigned long ulSequenceNum,
  379. unsigned long ulTrigger, unsigned long ulPriority)
  380. {
  381. //
  382. // Check the arugments.
  383. //
  384. ASSERT((ulBase == ADC0_BASE) || (ulBase == ADC1_BASE));
  385. ASSERT(ulSequenceNum < 4);
  386. ASSERT((ulTrigger == ADC_TRIGGER_PROCESSOR) ||
  387. (ulTrigger == ADC_TRIGGER_COMP0) ||
  388. (ulTrigger == ADC_TRIGGER_COMP1) ||
  389. (ulTrigger == ADC_TRIGGER_COMP2) ||
  390. (ulTrigger == ADC_TRIGGER_EXTERNAL) ||
  391. (ulTrigger == ADC_TRIGGER_TIMER) ||
  392. (ulTrigger == ADC_TRIGGER_PWM0) ||
  393. (ulTrigger == ADC_TRIGGER_PWM1) ||
  394. (ulTrigger == ADC_TRIGGER_PWM2) ||
  395. (ulTrigger == ADC_TRIGGER_ALWAYS));
  396. ASSERT(ulPriority < 4);
  397. //
  398. // Compute the shift for the bits that control this sample sequence.
  399. //
  400. ulSequenceNum *= 4;
  401. //
  402. // Set the trigger event for this sample sequence.
  403. //
  404. HWREG(ulBase + ADC_O_EMUX) = ((HWREG(ulBase + ADC_O_EMUX) &
  405. ~(0xf << ulSequenceNum)) |
  406. ((ulTrigger & 0xf) << ulSequenceNum));
  407. //
  408. // Set the priority for this sample sequence.
  409. //
  410. HWREG(ulBase + ADC_O_SSPRI) = ((HWREG(ulBase + ADC_O_SSPRI) &
  411. ~(0xf << ulSequenceNum)) |
  412. ((ulPriority & 0x3) << ulSequenceNum));
  413. }
  414. //*****************************************************************************
  415. //
  416. //! Configure a step of the sample sequencer.
  417. //!
  418. //! \param ulBase is the base address of the ADC module.
  419. //! \param ulSequenceNum is the sample sequence number.
  420. //! \param ulStep is the step to be configured.
  421. //! \param ulConfig is the configuration of this step; must be a logical OR of
  422. //! \b ADC_CTL_TS, \b ADC_CTL_IE, \b ADC_CTL_END, \b ADC_CTL_D, and one of the
  423. //! input channel selects (\b ADC_CTL_CH0 through \b ADC_CTL_CH7).
  424. //!
  425. //! This function will set the configuration of the ADC for one step of a
  426. //! sample sequence. The ADC can be configured for single-ended or
  427. //! differential operation (the \b ADC_CTL_D bit selects differential
  428. //! operation when set), the channel to be sampled can be chosen (the
  429. //! \b ADC_CTL_CH0 through \b ADC_CTL_CH7 values), and the internal temperature
  430. //! sensor can be selected (the \b ADC_CTL_TS bit). Additionally, this step
  431. //! can be defined as the last in the sequence (the \b ADC_CTL_END bit) and it
  432. //! can be configured to cause an interrupt when the step is complete (the
  433. //! \b ADC_CTL_IE bit). The configuration is used by the ADC at the
  434. //! appropriate time when the trigger for this sequence occurs.
  435. //!
  436. //! The \e ulStep parameter determines the order in which the samples are
  437. //! captured by the ADC when the trigger occurs. It can range from zero to
  438. //! seven for the first sample sequence, from zero to three for the second and
  439. //! third sample sequence, and can only be zero for the fourth sample sequence.
  440. //!
  441. //! Differential mode only works with adjacent channel pairs (for example, 0
  442. //! and 1). The channel select must be the number of the channel pair to
  443. //! sample (for example, \b ADC_CTL_CH0 for 0 and 1, or \b ADC_CTL_CH1 for 2
  444. //! and 3) or undefined results will be returned by the ADC. Additionally, if
  445. //! differential mode is selected when the temperature sensor is being sampled,
  446. //! undefined results will be returned by the ADC.
  447. //!
  448. //! It is the responsibility of the caller to ensure that a valid configuration
  449. //! is specified; this function does not check the validity of the specified
  450. //! configuration.
  451. //!
  452. //! \return None.
  453. //
  454. //*****************************************************************************
  455. void
  456. ADCSequenceStepConfigure(unsigned long ulBase, unsigned long ulSequenceNum,
  457. unsigned long ulStep, unsigned long ulConfig)
  458. {
  459. //
  460. // Check the arugments.
  461. //
  462. ASSERT((ulBase == ADC0_BASE) || (ulBase == ADC1_BASE));
  463. ASSERT(ulSequenceNum < 4);
  464. ASSERT(((ulSequenceNum == 0) && (ulStep < 8)) ||
  465. ((ulSequenceNum == 1) && (ulStep < 4)) ||
  466. ((ulSequenceNum == 2) && (ulStep < 4)) ||
  467. ((ulSequenceNum == 3) && (ulStep < 1)));
  468. //
  469. // Get the offset of the sequence to be configured.
  470. //
  471. ulBase += ADC_SEQ + (ADC_SEQ_STEP * ulSequenceNum);
  472. //
  473. // Compute the shift for the bits that control this step.
  474. //
  475. ulStep *= 4;
  476. //
  477. // Set the analog mux value for this step.
  478. //
  479. HWREG(ulBase + ADC_SSMUX) = ((HWREG(ulBase + ADC_SSMUX) &
  480. ~(0x0000000f << ulStep)) |
  481. ((ulConfig & 0x0f) << ulStep));
  482. //
  483. // Set the control value for this step.
  484. //
  485. HWREG(ulBase + ADC_SSCTL) = ((HWREG(ulBase + ADC_SSCTL) &
  486. ~(0x0000000f << ulStep)) |
  487. (((ulConfig & 0xf0) >> 4) << ulStep));
  488. }
  489. //*****************************************************************************
  490. //
  491. //! Determines if a sample sequence overflow occurred.
  492. //!
  493. //! \param ulBase is the base address of the ADC module.
  494. //! \param ulSequenceNum is the sample sequence number.
  495. //!
  496. //! This determines if a sample sequence overflow has occurred. This will
  497. //! happen if the captured samples are not read from the FIFO before the next
  498. //! trigger occurs.
  499. //!
  500. //! \return Returns zero if there was not an overflow, and non-zero if there
  501. //! was.
  502. //
  503. //*****************************************************************************
  504. long
  505. ADCSequenceOverflow(unsigned long ulBase, unsigned long ulSequenceNum)
  506. {
  507. //
  508. // Check the arguments.
  509. //
  510. ASSERT((ulBase == ADC0_BASE) || (ulBase == ADC1_BASE));
  511. ASSERT(ulSequenceNum < 4);
  512. //
  513. // Determine if there was an overflow on this sequence.
  514. //
  515. return(HWREG(ulBase + ADC_O_OSTAT) & (1 << ulSequenceNum));
  516. }
  517. //*****************************************************************************
  518. //
  519. //! Clears the overflow condition on a sample sequence.
  520. //!
  521. //! \param ulBase is the base address of the ADC module.
  522. //! \param ulSequenceNum is the sample sequence number.
  523. //!
  524. //! This will clear an overflow condition on one of the sample sequences. The
  525. //! overflow condition must be cleared in order to detect a subsequent overflow
  526. //! condition (it otherwise causes no harm).
  527. //!
  528. //! \return None.
  529. //
  530. //*****************************************************************************
  531. void
  532. ADCSequenceOverflowClear(unsigned long ulBase, unsigned long ulSequenceNum)
  533. {
  534. //
  535. // Check the arguments.
  536. //
  537. ASSERT((ulBase == ADC0_BASE) || (ulBase == ADC1_BASE));
  538. ASSERT(ulSequenceNum < 4);
  539. //
  540. // Clear the overflow condition for this sequence.
  541. //
  542. HWREG(ulBase + ADC_O_OSTAT) = 1 << ulSequenceNum;
  543. }
  544. //*****************************************************************************
  545. //
  546. //! Determines if a sample sequence underflow occurred.
  547. //!
  548. //! \param ulBase is the base address of the ADC module.
  549. //! \param ulSequenceNum is the sample sequence number.
  550. //!
  551. //! This determines if a sample sequence underflow has occurred. This will
  552. //! happen if too many samples are read from the FIFO.
  553. //!
  554. //! \return Returns zero if there was not an underflow, and non-zero if there
  555. //! was.
  556. //
  557. //*****************************************************************************
  558. long
  559. ADCSequenceUnderflow(unsigned long ulBase, unsigned long ulSequenceNum)
  560. {
  561. //
  562. // Check the arguments.
  563. //
  564. ASSERT((ulBase == ADC0_BASE) || (ulBase == ADC1_BASE));
  565. ASSERT(ulSequenceNum < 4);
  566. //
  567. // Determine if there was an underflow on this sequence.
  568. //
  569. return(HWREG(ulBase + ADC_O_USTAT) & (1 << ulSequenceNum));
  570. }
  571. //*****************************************************************************
  572. //
  573. //! Clears the underflow condition on a sample sequence.
  574. //!
  575. //! \param ulBase is the base address of the ADC module.
  576. //! \param ulSequenceNum is the sample sequence number.
  577. //!
  578. //! This will clear an underflow condition on one of the sample sequences. The
  579. //! underflow condition must be cleared in order to detect a subsequent
  580. //! underflow condition (it otherwise causes no harm).
  581. //!
  582. //! \return None.
  583. //
  584. //*****************************************************************************
  585. void
  586. ADCSequenceUnderflowClear(unsigned long ulBase, unsigned long ulSequenceNum)
  587. {
  588. //
  589. // Check the arguments.
  590. //
  591. ASSERT((ulBase == ADC0_BASE) || (ulBase == ADC1_BASE));
  592. ASSERT(ulSequenceNum < 4);
  593. //
  594. // Clear the underflow condition for this sequence.
  595. //
  596. HWREG(ulBase + ADC_O_USTAT) = 1 << ulSequenceNum;
  597. }
  598. //*****************************************************************************
  599. //
  600. //! Gets the captured data for a sample sequence.
  601. //!
  602. //! \param ulBase is the base address of the ADC module.
  603. //! \param ulSequenceNum is the sample sequence number.
  604. //! \param pulBuffer is the address where the data is stored.
  605. //!
  606. //! This function copies data from the specified sample sequence output FIFO to
  607. //! a memory resident buffer. The number of samples available in the hardware
  608. //! FIFO are copied into the buffer, which is assumed to be large enough to
  609. //! hold that many samples. This will only return the samples that are
  610. //! presently available, which may not be the entire sample sequence if it is
  611. //! in the process of being executed.
  612. //!
  613. //! \return Returns the number of samples copied to the buffer.
  614. //
  615. //*****************************************************************************
  616. long
  617. ADCSequenceDataGet(unsigned long ulBase, unsigned long ulSequenceNum,
  618. unsigned long *pulBuffer)
  619. {
  620. unsigned long ulCount;
  621. //
  622. // Check the arguments.
  623. //
  624. ASSERT((ulBase == ADC0_BASE) || (ulBase == ADC1_BASE));
  625. ASSERT(ulSequenceNum < 4);
  626. //
  627. // Get the offset of the sequence to be read.
  628. //
  629. ulBase += ADC_SEQ + (ADC_SEQ_STEP * ulSequenceNum);
  630. //
  631. // Read samples from the FIFO until it is empty.
  632. //
  633. ulCount = 0;
  634. while(!(HWREG(ulBase + ADC_SSFSTAT) & ADC_SSFSTAT0_EMPTY) && (ulCount < 8))
  635. {
  636. //
  637. // Read the FIFO and copy it to the destination.
  638. //
  639. *pulBuffer++ = HWREG(ulBase + ADC_SSFIFO);
  640. //
  641. // Increment the count of samples read.
  642. //
  643. ulCount++;
  644. }
  645. //
  646. // Return the number of samples read.
  647. //
  648. return(ulCount);
  649. }
  650. //*****************************************************************************
  651. //
  652. //! Causes a processor trigger for a sample sequence.
  653. //!
  654. //! \param ulBase is the base address of the ADC module.
  655. //! \param ulSequenceNum is the sample sequence number.
  656. //!
  657. //! This function triggers a processor-initiated sample sequence if the sample
  658. //! sequence trigger is configured to \b ADC_TRIGGER_PROCESSOR.
  659. //!
  660. //! \return None.
  661. //
  662. //*****************************************************************************
  663. void
  664. ADCProcessorTrigger(unsigned long ulBase, unsigned long ulSequenceNum)
  665. {
  666. //
  667. // Check the arguments.
  668. //
  669. ASSERT((ulBase == ADC0_BASE) || (ulBase == ADC1_BASE));
  670. ASSERT(ulSequenceNum < 4);
  671. //
  672. // Generate a processor trigger for this sample sequence.
  673. //
  674. HWREG(ulBase + ADC_O_PSSI) = 1 << ulSequenceNum;
  675. }
  676. //*****************************************************************************
  677. //
  678. //! Configures the software oversampling factor of the ADC.
  679. //!
  680. //! \param ulBase is the base address of the ADC module.
  681. //! \param ulSequenceNum is the sample sequence number.
  682. //! \param ulFactor is the number of samples to be averaged.
  683. //!
  684. //! This function configures the software oversampling for the ADC, which can
  685. //! be used to provide better resolution on the sampled data. Oversampling is
  686. //! accomplished by averaging multiple samples from the same analog input.
  687. //! Three different oversampling rates are supported; 2x, 4x, and 8x.
  688. //!
  689. //! Oversampling is only supported on the sample sequencers that are more than
  690. //! one sample in depth (that is, the fourth sample sequencer is not
  691. //! supported). Oversampling by 2x (for example) divides the depth of the
  692. //! sample sequencer by two; so 2x oversampling on the first sample sequencer
  693. //! can only provide four samples per trigger. This also means that 8x
  694. //! oversampling is only available on the first sample sequencer.
  695. //!
  696. //! \return None.
  697. //
  698. //*****************************************************************************
  699. void
  700. ADCSoftwareOversampleConfigure(unsigned long ulBase,
  701. unsigned long ulSequenceNum,
  702. unsigned long ulFactor)
  703. {
  704. unsigned long ulValue;
  705. //
  706. // Check the arguments.
  707. //
  708. ASSERT((ulBase == ADC0_BASE) || (ulBase == ADC1_BASE));
  709. ASSERT(ulSequenceNum < 3);
  710. ASSERT(((ulFactor == 2) || (ulFactor == 4) || (ulFactor == 8)) &&
  711. ((ulSequenceNum == 0) || (ulFactor != 8)));
  712. //
  713. // Convert the oversampling factor to a shift factor.
  714. //
  715. for(ulValue = 0, ulFactor >>= 1; ulFactor; ulValue++, ulFactor >>= 1)
  716. {
  717. }
  718. //
  719. // Save the sfiht factor.
  720. //
  721. g_pucOversampleFactor[ulSequenceNum] = ulValue;
  722. }
  723. //*****************************************************************************
  724. //
  725. //! Configures a step of the software oversampled sequencer.
  726. //!
  727. //! \param ulBase is the base address of the ADC module.
  728. //! \param ulSequenceNum is the sample sequence number.
  729. //! \param ulStep is the step to be configured.
  730. //! \param ulConfig is the configuration of this step.
  731. //!
  732. //! This function configures a step of the sample sequencer when using the
  733. //! software oversampling feature. The number of steps available depends on
  734. //! the oversampling factor set by ADCSoftwareOversampleConfigure(). The value
  735. //! of \e ulConfig is the same as defined for ADCSequenceStepConfigure().
  736. //!
  737. //! \return None.
  738. //
  739. //*****************************************************************************
  740. void
  741. ADCSoftwareOversampleStepConfigure(unsigned long ulBase,
  742. unsigned long ulSequenceNum,
  743. unsigned long ulStep,
  744. unsigned long ulConfig)
  745. {
  746. //
  747. // Check the arguments.
  748. //
  749. ASSERT((ulBase == ADC0_BASE) || (ulBase == ADC1_BASE));
  750. ASSERT(ulSequenceNum < 3);
  751. ASSERT(((ulSequenceNum == 0) &&
  752. (ulStep < (8 >> g_pucOversampleFactor[ulSequenceNum]))) ||
  753. (ulStep < (4 >> g_pucOversampleFactor[ulSequenceNum])));
  754. //
  755. // Get the offset of the sequence to be configured.
  756. //
  757. ulBase += ADC_SEQ + (ADC_SEQ_STEP * ulSequenceNum);
  758. //
  759. // Compute the shift for the bits that control this step.
  760. //
  761. ulStep *= 4 << g_pucOversampleFactor[ulSequenceNum];
  762. //
  763. // Loop through the hardware steps that make up this step of the software
  764. // oversampled sequence.
  765. //
  766. for(ulSequenceNum = 1 << g_pucOversampleFactor[ulSequenceNum];
  767. ulSequenceNum; ulSequenceNum--)
  768. {
  769. //
  770. // Set the analog mux value for this step.
  771. //
  772. HWREG(ulBase + ADC_SSMUX) = ((HWREG(ulBase + ADC_SSMUX) &
  773. ~(0x0000000f << ulStep)) |
  774. ((ulConfig & 0x0f) << ulStep));
  775. //
  776. // Set the control value for this step.
  777. //
  778. HWREG(ulBase + ADC_SSCTL) = ((HWREG(ulBase + ADC_SSCTL) &
  779. ~(0x0000000f << ulStep)) |
  780. (((ulConfig & 0xf0) >> 4) << ulStep));
  781. if(ulSequenceNum != 1)
  782. {
  783. HWREG(ulBase + ADC_SSCTL) &= ~((ADC_SSCTL0_IE0 |
  784. ADC_SSCTL0_END0) << ulStep);
  785. }
  786. //
  787. // Go to the next hardware step.
  788. //
  789. ulStep += 4;
  790. }
  791. }
  792. //*****************************************************************************
  793. //
  794. //! Gets the captured data for a sample sequence using software oversampling.
  795. //!
  796. //! \param ulBase is the base address of the ADC module.
  797. //! \param ulSequenceNum is the sample sequence number.
  798. //! \param pulBuffer is the address where the data is stored.
  799. //! \param ulCount is the number of samples to be read.
  800. //!
  801. //! This function copies data from the specified sample sequence output FIFO to
  802. //! a memory resident buffer with software oversampling applied. The requested
  803. //! number of samples are copied into the data buffer; if there are not enough
  804. //! samples in the hardware FIFO to satisfy this many oversampled data items
  805. //! then incorrect results will be returned. It is the caller's responsibility
  806. //! to read only the samples that are available and wait until enough data is
  807. //! available, for example as a result of receiving an interrupt.
  808. //!
  809. //! \return None.
  810. //
  811. //*****************************************************************************
  812. void
  813. ADCSoftwareOversampleDataGet(unsigned long ulBase, unsigned long ulSequenceNum,
  814. unsigned long *pulBuffer, unsigned long ulCount)
  815. {
  816. unsigned long ulIdx, ulAccum;
  817. //
  818. // Check the arguments.
  819. //
  820. ASSERT((ulBase == ADC0_BASE) || (ulBase == ADC1_BASE));
  821. ASSERT(ulSequenceNum < 3);
  822. ASSERT(((ulSequenceNum == 0) &&
  823. (ulCount < (8 >> g_pucOversampleFactor[ulSequenceNum]))) ||
  824. (ulCount < (4 >> g_pucOversampleFactor[ulSequenceNum])));
  825. //
  826. // Get the offset of the sequence to be read.
  827. //
  828. ulBase += ADC_SEQ + (ADC_SEQ_STEP * ulSequenceNum);
  829. //
  830. // Read the samples from the FIFO until it is empty.
  831. //
  832. while(ulCount--)
  833. {
  834. //
  835. // Compute the sum of the samples.
  836. //
  837. ulAccum = 0;
  838. for(ulIdx = 1 << g_pucOversampleFactor[ulSequenceNum]; ulIdx; ulIdx--)
  839. {
  840. //
  841. // Read the FIFO and add it to the accumulator.
  842. //
  843. ulAccum += HWREG(ulBase + ADC_SSFIFO);
  844. }
  845. //
  846. // Write the averaged sample to the output buffer.
  847. //
  848. *pulBuffer++ = ulAccum >> g_pucOversampleFactor[ulSequenceNum];
  849. }
  850. }
  851. //*****************************************************************************
  852. //
  853. //! Configures the hardware oversampling factor of the ADC.
  854. //!
  855. //! \param ulBase is the base address of the ADC module.
  856. //! \param ulFactor is the number of samples to be averaged.
  857. //!
  858. //! This function configures the hardware oversampling for the ADC, which can
  859. //! be used to provide better resolution on the sampled data. Oversampling is
  860. //! accomplished by averaging multiple samples from the same analog input. Six
  861. //! different oversampling rates are supported; 2x, 4x, 8x, 16x, 32x, and 64x.
  862. //! Specifying an oversampling factor of zero will disable hardware
  863. //! oversampling.
  864. //!
  865. //! Hardware oversampling applies uniformly to all sample sequencers. It does
  866. //! not reduce the depth of the sample sequencers like the software
  867. //! oversampling APIs; each sample written into the sample sequence FIFO is a
  868. //! fully oversampled analog input reading.
  869. //!
  870. //! Enabling hardware averaging increases the precision of the ADC at the cost
  871. //! of throughput. For example, enabling 4x oversampling reduces the
  872. //! throughput of a 250 Ksps ADC to 62.5 Ksps.
  873. //!
  874. //! \note Hardware oversampling is available beginning with Rev C0 of the
  875. //! Stellaris microcontroller.
  876. //!
  877. //! \return None.
  878. //
  879. //*****************************************************************************
  880. void
  881. ADCHardwareOversampleConfigure(unsigned long ulBase, unsigned long ulFactor)
  882. {
  883. unsigned long ulValue;
  884. //
  885. // Check the arguments.
  886. //
  887. ASSERT((ulBase == ADC0_BASE) || (ulBase == ADC1_BASE));
  888. ASSERT(((ulFactor == 0) || (ulFactor == 2) || (ulFactor == 4) ||
  889. (ulFactor == 8) || (ulFactor == 16) || (ulFactor == 32) ||
  890. (ulFactor == 64)));
  891. //
  892. // Convert the oversampling factor to a shift factor.
  893. //
  894. for(ulValue = 0, ulFactor >>= 1; ulFactor; ulValue++, ulFactor >>= 1)
  895. {
  896. }
  897. //
  898. // Write the shift factor to the ADC to configure the hardware oversampler.
  899. //
  900. HWREG(ulBase + ADC_O_SAC) = ulValue;
  901. }
  902. //*****************************************************************************
  903. //
  904. // Close the Doxygen group.
  905. //! @}
  906. //
  907. //*****************************************************************************