adc.c 56 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550
  1. //*****************************************************************************
  2. //
  3. // adc.c - Driver for the ADC.
  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 8049 of the Stellaris Peripheral Driver Library.
  22. //
  23. //*****************************************************************************
  24. //*****************************************************************************
  25. //
  26. //! \addtogroup adc_api
  27. //! @{
  28. //
  29. //*****************************************************************************
  30. #include "inc/hw_adc.h"
  31. #include "inc/hw_ints.h"
  32. #include "inc/hw_memmap.h"
  33. #include "inc/hw_types.h"
  34. #include "inc/hw_sysctl.h"
  35. #include "driverlib/adc.h"
  36. #include "driverlib/debug.h"
  37. #include "driverlib/interrupt.h"
  38. //*****************************************************************************
  39. //
  40. // These defines are used by the ADC driver to simplify access to the ADC
  41. // sequencer's registers.
  42. //
  43. //*****************************************************************************
  44. #define ADC_SEQ (ADC_O_SSMUX0)
  45. #define ADC_SEQ_STEP (ADC_O_SSMUX1 - ADC_O_SSMUX0)
  46. #define ADC_SSMUX (ADC_O_SSMUX0 - ADC_O_SSMUX0)
  47. #define ADC_SSEMUX (ADC_O_SSEMUX0 - ADC_O_SSMUX0)
  48. #define ADC_SSCTL (ADC_O_SSCTL0 - ADC_O_SSMUX0)
  49. #define ADC_SSFIFO (ADC_O_SSFIFO0 - ADC_O_SSMUX0)
  50. #define ADC_SSFSTAT (ADC_O_SSFSTAT0 - ADC_O_SSMUX0)
  51. #define ADC_SSOP (ADC_O_SSOP0 - ADC_O_SSMUX0)
  52. #define ADC_SSDC (ADC_O_SSDC0 - ADC_O_SSMUX0)
  53. //*****************************************************************************
  54. //
  55. // The currently configured software oversampling factor for each of the ADC
  56. // sequencers.
  57. //
  58. //*****************************************************************************
  59. static unsigned char g_pucOversampleFactor[3];
  60. //*****************************************************************************
  61. //
  62. //! Registers an interrupt handler for an ADC interrupt.
  63. //!
  64. //! \param ulBase is the base address of the ADC module.
  65. //! \param ulSequenceNum is the sample sequence number.
  66. //! \param pfnHandler is a pointer to the function to be called when the
  67. //! ADC sample sequence interrupt occurs.
  68. //!
  69. //! This function sets the handler to be called when a sample sequence
  70. //! interrupt occurs. This will enable the global interrupt in the interrupt
  71. //! controller; the sequence interrupt must be enabled with ADCIntEnable(). It
  72. //! is the interrupt handler's responsibility to clear the interrupt source via
  73. //! ADCIntClear().
  74. //!
  75. //! \sa IntRegister() for important information about registering interrupt
  76. //! handlers.
  77. //!
  78. //! \return None.
  79. //
  80. //*****************************************************************************
  81. void
  82. ADCIntRegister(unsigned long ulBase, unsigned long ulSequenceNum,
  83. void (*pfnHandler)(void))
  84. {
  85. unsigned long ulInt;
  86. //
  87. // Check the arguments.
  88. //
  89. ASSERT((ulBase == ADC0_BASE) || (ulBase == ADC1_BASE));
  90. ASSERT(ulSequenceNum < 4);
  91. //
  92. // Determine the interrupt to register based on the sequence number.
  93. //
  94. ulInt = ((ulBase == ADC0_BASE) ? (INT_ADC0SS0 + ulSequenceNum) :
  95. (INT_ADC1SS0 + ulSequenceNum));
  96. //
  97. // Register the interrupt handler.
  98. //
  99. IntRegister(ulInt, pfnHandler);
  100. //
  101. // Enable the timer interrupt.
  102. //
  103. IntEnable(ulInt);
  104. }
  105. //*****************************************************************************
  106. //
  107. //! Unregisters the interrupt handler for an ADC interrupt.
  108. //!
  109. //! \param ulBase is the base address of the ADC module.
  110. //! \param ulSequenceNum is the sample sequence number.
  111. //!
  112. //! This function unregisters the interrupt handler. This will disable the
  113. //! global interrupt in the interrupt controller; the sequence interrupt must
  114. //! be disabled via ADCIntDisable().
  115. //!
  116. //! \sa IntRegister() for important information about registering interrupt
  117. //! handlers.
  118. //!
  119. //! \return None.
  120. //
  121. //*****************************************************************************
  122. void
  123. ADCIntUnregister(unsigned long ulBase, unsigned long ulSequenceNum)
  124. {
  125. unsigned long ulInt;
  126. //
  127. // Check the arguments.
  128. //
  129. ASSERT((ulBase == ADC0_BASE) || (ulBase == ADC1_BASE));
  130. ASSERT(ulSequenceNum < 4);
  131. //
  132. // Determine the interrupt to unregister based on the sequence number.
  133. //
  134. ulInt = ((ulBase == ADC0_BASE) ? (INT_ADC0SS0 + ulSequenceNum) :
  135. (INT_ADC1SS0 + ulSequenceNum));
  136. //
  137. // Disable the interrupt.
  138. //
  139. IntDisable(ulInt);
  140. //
  141. // Unregister the interrupt handler.
  142. //
  143. IntUnregister(ulInt);
  144. }
  145. //*****************************************************************************
  146. //
  147. //! Disables a sample sequence interrupt.
  148. //!
  149. //! \param ulBase is the base address of the ADC module.
  150. //! \param ulSequenceNum is the sample sequence number.
  151. //!
  152. //! This function disables the requested sample sequence interrupt.
  153. //!
  154. //! \return None.
  155. //
  156. //*****************************************************************************
  157. void
  158. ADCIntDisable(unsigned long ulBase, unsigned long ulSequenceNum)
  159. {
  160. //
  161. // Check the arguments.
  162. //
  163. ASSERT((ulBase == ADC0_BASE) || (ulBase == ADC1_BASE));
  164. ASSERT(ulSequenceNum < 4);
  165. //
  166. // Disable this sample sequence interrupt.
  167. //
  168. HWREG(ulBase + ADC_O_IM) &= ~(1 << ulSequenceNum);
  169. }
  170. //*****************************************************************************
  171. //
  172. //! Enables a sample sequence interrupt.
  173. //!
  174. //! \param ulBase is the base address of the ADC module.
  175. //! \param ulSequenceNum is the sample sequence number.
  176. //!
  177. //! This function enables the requested sample sequence interrupt. Any
  178. //! outstanding interrupts are cleared before enabling the sample sequence
  179. //! interrupt.
  180. //!
  181. //! \return None.
  182. //
  183. //*****************************************************************************
  184. void
  185. ADCIntEnable(unsigned long ulBase, unsigned long ulSequenceNum)
  186. {
  187. //
  188. // Check the arguments.
  189. //
  190. ASSERT((ulBase == ADC0_BASE) || (ulBase == ADC1_BASE));
  191. ASSERT(ulSequenceNum < 4);
  192. //
  193. // Clear any outstanding interrupts on this sample sequence.
  194. //
  195. HWREG(ulBase + ADC_O_ISC) = 1 << ulSequenceNum;
  196. //
  197. // Enable this sample sequence interrupt.
  198. //
  199. HWREG(ulBase + ADC_O_IM) |= 1 << ulSequenceNum;
  200. }
  201. //*****************************************************************************
  202. //
  203. //! Gets the current interrupt status.
  204. //!
  205. //! \param ulBase is the base address of the ADC module.
  206. //! \param ulSequenceNum is the sample sequence number.
  207. //! \param bMasked is false if the raw interrupt status is required and true if
  208. //! the masked interrupt status is required.
  209. //!
  210. //! This returns the interrupt status for the specified sample sequence.
  211. //! Either the raw interrupt status or the status of interrupts that are
  212. //! allowed to reflect to the processor can be returned.
  213. //!
  214. //! \return The current raw or masked interrupt status.
  215. //
  216. //*****************************************************************************
  217. unsigned long
  218. ADCIntStatus(unsigned long ulBase, unsigned long ulSequenceNum,
  219. tBoolean bMasked)
  220. {
  221. unsigned long ulTemp;
  222. //
  223. // Check the arguments.
  224. //
  225. ASSERT((ulBase == ADC0_BASE) || (ulBase == ADC1_BASE));
  226. ASSERT(ulSequenceNum < 4);
  227. //
  228. // Return either the interrupt status or the raw interrupt status as
  229. // requested.
  230. //
  231. if(bMasked)
  232. {
  233. ulTemp = HWREG(ulBase + ADC_O_ISC) & (0x10001 << ulSequenceNum);
  234. }
  235. else
  236. {
  237. ulTemp = HWREG(ulBase + ADC_O_RIS) & (0x10000 | (1 << ulSequenceNum));
  238. //
  239. // If the digital comparator status bit is set, reflect it to the
  240. // appropriate sequence bit.
  241. //
  242. if(ulTemp & 0x10000)
  243. {
  244. ulTemp |= 0xF0000;
  245. ulTemp &= ~(0x10000 << ulSequenceNum);
  246. }
  247. }
  248. //
  249. // Return the interrupt status
  250. //
  251. return(ulTemp);
  252. }
  253. //*****************************************************************************
  254. //
  255. //! Clears sample sequence interrupt source.
  256. //!
  257. //! \param ulBase is the base address of the ADC module.
  258. //! \param ulSequenceNum is the sample sequence number.
  259. //!
  260. //! The specified sample sequence interrupt is cleared, so that it no longer
  261. //! asserts. This must be done in the interrupt handler to keep it from being
  262. //! called again immediately upon exit.
  263. //!
  264. //! \note Because there is a write buffer in the Cortex-M3 processor, it may
  265. //! take several clock cycles before the interrupt source is actually cleared.
  266. //! Therefore, it is recommended that the interrupt source be cleared early in
  267. //! the interrupt handler (as opposed to the very last action) to avoid
  268. //! returning from the interrupt handler before the interrupt source is
  269. //! actually cleared. Failure to do so may result in the interrupt handler
  270. //! being immediately reentered (because the interrupt controller still sees
  271. //! the interrupt source asserted).
  272. //!
  273. //! \return None.
  274. //
  275. //*****************************************************************************
  276. void
  277. ADCIntClear(unsigned long ulBase, unsigned long ulSequenceNum)
  278. {
  279. //
  280. // Check the arugments.
  281. //
  282. ASSERT((ulBase == ADC0_BASE) || (ulBase == ADC1_BASE));
  283. ASSERT(ulSequenceNum < 4);
  284. //
  285. // Clear the interrupt.
  286. //
  287. HWREG(ulBase + ADC_O_ISC) = 1 << ulSequenceNum;
  288. }
  289. //*****************************************************************************
  290. //
  291. //! Enables a sample sequence.
  292. //!
  293. //! \param ulBase is the base address of the ADC module.
  294. //! \param ulSequenceNum is the sample sequence number.
  295. //!
  296. //! Allows the specified sample sequence to be captured when its trigger is
  297. //! detected. A sample sequence must be configured before it is enabled.
  298. //!
  299. //! \return None.
  300. //
  301. //*****************************************************************************
  302. void
  303. ADCSequenceEnable(unsigned long ulBase, unsigned long ulSequenceNum)
  304. {
  305. //
  306. // Check the arugments.
  307. //
  308. ASSERT((ulBase == ADC0_BASE) || (ulBase == ADC1_BASE));
  309. ASSERT(ulSequenceNum < 4);
  310. //
  311. // Enable the specified sequence.
  312. //
  313. HWREG(ulBase + ADC_O_ACTSS) |= 1 << ulSequenceNum;
  314. }
  315. //*****************************************************************************
  316. //
  317. //! Disables a sample sequence.
  318. //!
  319. //! \param ulBase is the base address of the ADC module.
  320. //! \param ulSequenceNum is the sample sequence number.
  321. //!
  322. //! Prevents the specified sample sequence from being captured when its trigger
  323. //! is detected. A sample sequence should be disabled before it is configured.
  324. //!
  325. //! \return None.
  326. //
  327. //*****************************************************************************
  328. void
  329. ADCSequenceDisable(unsigned long ulBase, unsigned long ulSequenceNum)
  330. {
  331. //
  332. // Check the arugments.
  333. //
  334. ASSERT((ulBase == ADC0_BASE) || (ulBase == ADC1_BASE));
  335. ASSERT(ulSequenceNum < 4);
  336. //
  337. // Disable the specified sequences.
  338. //
  339. HWREG(ulBase + ADC_O_ACTSS) &= ~(1 << ulSequenceNum);
  340. }
  341. //*****************************************************************************
  342. //
  343. //! Configures the trigger source and priority of a sample sequence.
  344. //!
  345. //! \param ulBase is the base address of the ADC module.
  346. //! \param ulSequenceNum is the sample sequence number.
  347. //! \param ulTrigger is the trigger source that initiates the sample sequence;
  348. //! must be one of the \b ADC_TRIGGER_* values.
  349. //! \param ulPriority is the relative priority of the sample sequence with
  350. //! respect to the other sample sequences.
  351. //!
  352. //! This function configures the initiation criteria for a sample sequence.
  353. //! Valid sample sequences range from zero to three; sequence zero will capture
  354. //! up to eight samples, sequences one and two will capture up to four samples,
  355. //! and sequence three will capture a single sample. The trigger condition and
  356. //! priority (with respect to other sample sequence execution) is set.
  357. //!
  358. //! The \e ulTrigger parameter can take on the following values:
  359. //!
  360. //! - \b ADC_TRIGGER_PROCESSOR - A trigger generated by the processor, via the
  361. //! ADCProcessorTrigger() function.
  362. //! - \b ADC_TRIGGER_COMP0 - A trigger generated by the first analog
  363. //! comparator; configured with ComparatorConfigure().
  364. //! - \b ADC_TRIGGER_COMP1 - A trigger generated by the second analog
  365. //! comparator; configured with ComparatorConfigure().
  366. //! - \b ADC_TRIGGER_COMP2 - A trigger generated by the third analog
  367. //! comparator; configured with ComparatorConfigure().
  368. //! - \b ADC_TRIGGER_EXTERNAL - A trigger generated by an input from the Port
  369. //! B4 pin.
  370. //! - \b ADC_TRIGGER_TIMER - A trigger generated by a timer; configured with
  371. //! TimerControlTrigger().
  372. //! - \b ADC_TRIGGER_PWM0 - A trigger generated by the first PWM generator;
  373. //! configured with PWMGenIntTrigEnable().
  374. //! - \b ADC_TRIGGER_PWM1 - A trigger generated by the second PWM generator;
  375. //! configured with PWMGenIntTrigEnable().
  376. //! - \b ADC_TRIGGER_PWM2 - A trigger generated by the third PWM generator;
  377. //! configured with PWMGenIntTrigEnable().
  378. //! - \b ADC_TRIGGER_PWM3 - A trigger generated by the fourth PWM generator;
  379. //! configured with PWMGenIntTrigEnable().
  380. //! - \b ADC_TRIGGER_ALWAYS - A trigger that is always asserted, causing the
  381. //! sample sequence to capture repeatedly (so long as
  382. //! there is not a higher priority source active).
  383. //!
  384. //! Note that not all trigger sources are available on all Stellaris family
  385. //! members; consult the data sheet for the device in question to determine the
  386. //! availability of triggers.
  387. //!
  388. //! The \e ulPriority parameter is a value between 0 and 3, where 0 represents
  389. //! the highest priority and 3 the lowest. Note that when programming the
  390. //! priority among a set of sample sequences, each must have unique priority;
  391. //! it is up to the caller to guarantee the uniqueness of the priorities.
  392. //!
  393. //! \return None.
  394. //
  395. //*****************************************************************************
  396. void
  397. ADCSequenceConfigure(unsigned long ulBase, unsigned long ulSequenceNum,
  398. unsigned long ulTrigger, unsigned long ulPriority)
  399. {
  400. //
  401. // Check the arugments.
  402. //
  403. ASSERT((ulBase == ADC0_BASE) || (ulBase == ADC1_BASE));
  404. ASSERT(ulSequenceNum < 4);
  405. ASSERT((ulTrigger == ADC_TRIGGER_PROCESSOR) ||
  406. (ulTrigger == ADC_TRIGGER_COMP0) ||
  407. (ulTrigger == ADC_TRIGGER_COMP1) ||
  408. (ulTrigger == ADC_TRIGGER_COMP2) ||
  409. (ulTrigger == ADC_TRIGGER_EXTERNAL) ||
  410. (ulTrigger == ADC_TRIGGER_TIMER) ||
  411. (ulTrigger == ADC_TRIGGER_PWM0) ||
  412. (ulTrigger == ADC_TRIGGER_PWM1) ||
  413. (ulTrigger == ADC_TRIGGER_PWM2) ||
  414. (ulTrigger == ADC_TRIGGER_PWM3) ||
  415. (ulTrigger == ADC_TRIGGER_ALWAYS));
  416. ASSERT(ulPriority < 4);
  417. //
  418. // Compute the shift for the bits that control this sample sequence.
  419. //
  420. ulSequenceNum *= 4;
  421. //
  422. // Set the trigger event for this sample sequence.
  423. //
  424. HWREG(ulBase + ADC_O_EMUX) = ((HWREG(ulBase + ADC_O_EMUX) &
  425. ~(0xf << ulSequenceNum)) |
  426. ((ulTrigger & 0xf) << ulSequenceNum));
  427. //
  428. // Set the priority for this sample sequence.
  429. //
  430. HWREG(ulBase + ADC_O_SSPRI) = ((HWREG(ulBase + ADC_O_SSPRI) &
  431. ~(0xf << ulSequenceNum)) |
  432. ((ulPriority & 0x3) << ulSequenceNum));
  433. }
  434. //*****************************************************************************
  435. //
  436. //! Configure a step of the sample sequencer.
  437. //!
  438. //! \param ulBase is the base address of the ADC module.
  439. //! \param ulSequenceNum is the sample sequence number.
  440. //! \param ulStep is the step to be configured.
  441. //! \param ulConfig is the configuration of this step; must be a logical OR of
  442. //! \b ADC_CTL_TS, \b ADC_CTL_IE, \b ADC_CTL_END, \b ADC_CTL_D, one of the
  443. //! input channel selects (\b ADC_CTL_CH0 through \b ADC_CTL_CH15), and one of
  444. //! the digital comparator selects (\b ADC_CTL_CMP0 through \b ADC_CTL_CMP7).
  445. //!
  446. //! This function will set the configuration of the ADC for one step of a
  447. //! sample sequence. The ADC can be configured for single-ended or
  448. //! differential operation (the \b ADC_CTL_D bit selects differential
  449. //! operation when set), the channel to be sampled can be chosen (the
  450. //! \b ADC_CTL_CH0 through \b ADC_CTL_CH15 values), and the internal
  451. //! temperature sensor can be selected (the \b ADC_CTL_TS bit). Additionally,
  452. //! this step can be defined as the last in the sequence (the \b ADC_CTL_END
  453. //! bit) and it can be configured to cause an interrupt when the step is
  454. //! complete (the \b ADC_CTL_IE bit). If the digital comparators are present
  455. //! on the device, this step may also be configured to send the ADC sample to
  456. //! the selected comparator using \b ADC_CTL_CMP0 through \b ADC_CTL_CMP7.
  457. //! The configuration is used by the ADC at the appropriate time when the
  458. //! trigger for this sequence occurs.
  459. //!
  460. //! \note If the Digitial Comparator is present and enabled using the
  461. //! \b ADC_CTL_CMP0 through \b ADC_CTL_CMP7 selects, the ADC sample will NOT be
  462. //! written into the ADC sequence data FIFO.
  463. //!
  464. //! The \e ulStep parameter determines the order in which the samples are
  465. //! captured by the ADC when the trigger occurs. It can range from zero to
  466. //! seven for the first sample sequence, from zero to three for the second and
  467. //! third sample sequence, and can only be zero for the fourth sample sequence.
  468. //!
  469. //! Differential mode only works with adjacent channel pairs (for example, 0
  470. //! and 1). The channel select must be the number of the channel pair to
  471. //! sample (for example, \b ADC_CTL_CH0 for 0 and 1, or \b ADC_CTL_CH1 for 2
  472. //! and 3) or undefined results are returned by the ADC. Additionally, if
  473. //! differential mode is selected when the temperature sensor is being sampled,
  474. //! undefined results are returned by the ADC.
  475. //!
  476. //! It is the responsibility of the caller to ensure that a valid configuration
  477. //! is specified; this function does not check the validity of the specified
  478. //! configuration.
  479. //!
  480. //! \return None.
  481. //
  482. //*****************************************************************************
  483. void
  484. ADCSequenceStepConfigure(unsigned long ulBase, unsigned long ulSequenceNum,
  485. unsigned long ulStep, unsigned long ulConfig)
  486. {
  487. unsigned long ulTemp;
  488. //
  489. // Check the arugments.
  490. //
  491. ASSERT((ulBase == ADC0_BASE) || (ulBase == ADC1_BASE));
  492. ASSERT(ulSequenceNum < 4);
  493. ASSERT(((ulSequenceNum == 0) && (ulStep < 8)) ||
  494. ((ulSequenceNum == 1) && (ulStep < 4)) ||
  495. ((ulSequenceNum == 2) && (ulStep < 4)) ||
  496. ((ulSequenceNum == 3) && (ulStep < 1)));
  497. //
  498. // Get the offset of the sequence to be configured.
  499. //
  500. ulBase += ADC_SEQ + (ADC_SEQ_STEP * ulSequenceNum);
  501. //
  502. // Compute the shift for the bits that control this step.
  503. //
  504. ulStep *= 4;
  505. //
  506. // Set the analog mux value for this step.
  507. //
  508. HWREG(ulBase + ADC_SSMUX) = ((HWREG(ulBase + ADC_SSMUX) &
  509. ~(0x0000000f << ulStep)) |
  510. ((ulConfig & 0x0f) << ulStep));
  511. //
  512. // Set the upper bits of the analog mux value for this step.
  513. //
  514. HWREG(ulBase + ADC_SSEMUX) = ((HWREG(ulBase + ADC_SSEMUX) &
  515. ~(0x0000000f << ulStep)) |
  516. (((ulConfig & 0xf00) >> 8) << ulStep));
  517. //
  518. // Set the control value for this step.
  519. //
  520. HWREG(ulBase + ADC_SSCTL) = ((HWREG(ulBase + ADC_SSCTL) &
  521. ~(0x0000000f << ulStep)) |
  522. (((ulConfig & 0xf0) >> 4) << ulStep));
  523. //
  524. // Enable digital comparator if specified in the ulConfig bit-fields.
  525. //
  526. if(ulConfig & 0x000F0000)
  527. {
  528. //
  529. // Program the comparator for the specified step.
  530. //
  531. ulTemp = HWREG(ulBase + ADC_SSDC);
  532. ulTemp &= ~(0xF << ulStep);
  533. ulTemp |= (((ulConfig & 0x00070000) >> 16) << ulStep);
  534. HWREG(ulBase + ADC_SSDC) = ulTemp;
  535. //
  536. // Enable the comparator.
  537. //
  538. ulTemp = HWREG(ulBase + ADC_SSOP);
  539. ulTemp |= (1 << ulStep);
  540. HWREG(ulBase + ADC_SSOP) = ulTemp;
  541. }
  542. //
  543. // Disable digital comparator if not specified.
  544. //
  545. else
  546. {
  547. ulTemp = HWREG(ulBase + ADC_SSOP);
  548. ulTemp &= ~(1 << ulStep);
  549. HWREG(ulBase + ADC_SSOP) = ulTemp;
  550. }
  551. }
  552. //*****************************************************************************
  553. //
  554. //! Determines if a sample sequence overflow occurred.
  555. //!
  556. //! \param ulBase is the base address of the ADC module.
  557. //! \param ulSequenceNum is the sample sequence number.
  558. //!
  559. //! This determines if a sample sequence overflow has occurred. This will
  560. //! happen if the captured samples are not read from the FIFO before the next
  561. //! trigger occurs.
  562. //!
  563. //! \return Returns zero if there was not an overflow, and non-zero if there
  564. //! was.
  565. //
  566. //*****************************************************************************
  567. long
  568. ADCSequenceOverflow(unsigned long ulBase, unsigned long ulSequenceNum)
  569. {
  570. //
  571. // Check the arguments.
  572. //
  573. ASSERT((ulBase == ADC0_BASE) || (ulBase == ADC1_BASE));
  574. ASSERT(ulSequenceNum < 4);
  575. //
  576. // Determine if there was an overflow on this sequence.
  577. //
  578. return(HWREG(ulBase + ADC_O_OSTAT) & (1 << ulSequenceNum));
  579. }
  580. //*****************************************************************************
  581. //
  582. //! Clears the overflow condition on a sample sequence.
  583. //!
  584. //! \param ulBase is the base address of the ADC module.
  585. //! \param ulSequenceNum is the sample sequence number.
  586. //!
  587. //! This will clear an overflow condition on one of the sample sequences. The
  588. //! overflow condition must be cleared in order to detect a subsequent overflow
  589. //! condition (it otherwise causes no harm).
  590. //!
  591. //! \return None.
  592. //
  593. //*****************************************************************************
  594. void
  595. ADCSequenceOverflowClear(unsigned long ulBase, unsigned long ulSequenceNum)
  596. {
  597. //
  598. // Check the arguments.
  599. //
  600. ASSERT((ulBase == ADC0_BASE) || (ulBase == ADC1_BASE));
  601. ASSERT(ulSequenceNum < 4);
  602. //
  603. // Clear the overflow condition for this sequence.
  604. //
  605. HWREG(ulBase + ADC_O_OSTAT) = 1 << ulSequenceNum;
  606. }
  607. //*****************************************************************************
  608. //
  609. //! Determines if a sample sequence underflow occurred.
  610. //!
  611. //! \param ulBase is the base address of the ADC module.
  612. //! \param ulSequenceNum is the sample sequence number.
  613. //!
  614. //! This determines if a sample sequence underflow has occurred. This will
  615. //! happen if too many samples are read from the FIFO.
  616. //!
  617. //! \return Returns zero if there was not an underflow, and non-zero if there
  618. //! was.
  619. //
  620. //*****************************************************************************
  621. long
  622. ADCSequenceUnderflow(unsigned long ulBase, unsigned long ulSequenceNum)
  623. {
  624. //
  625. // Check the arguments.
  626. //
  627. ASSERT((ulBase == ADC0_BASE) || (ulBase == ADC1_BASE));
  628. ASSERT(ulSequenceNum < 4);
  629. //
  630. // Determine if there was an underflow on this sequence.
  631. //
  632. return(HWREG(ulBase + ADC_O_USTAT) & (1 << ulSequenceNum));
  633. }
  634. //*****************************************************************************
  635. //
  636. //! Clears the underflow condition on a sample sequence.
  637. //!
  638. //! \param ulBase is the base address of the ADC module.
  639. //! \param ulSequenceNum is the sample sequence number.
  640. //!
  641. //! This will clear an underflow condition on one of the sample sequences. The
  642. //! underflow condition must be cleared in order to detect a subsequent
  643. //! underflow condition (it otherwise causes no harm).
  644. //!
  645. //! \return None.
  646. //
  647. //*****************************************************************************
  648. void
  649. ADCSequenceUnderflowClear(unsigned long ulBase, unsigned long ulSequenceNum)
  650. {
  651. //
  652. // Check the arguments.
  653. //
  654. ASSERT((ulBase == ADC0_BASE) || (ulBase == ADC1_BASE));
  655. ASSERT(ulSequenceNum < 4);
  656. //
  657. // Clear the underflow condition for this sequence.
  658. //
  659. HWREG(ulBase + ADC_O_USTAT) = 1 << ulSequenceNum;
  660. }
  661. //*****************************************************************************
  662. //
  663. //! Gets the captured data for a sample sequence.
  664. //!
  665. //! \param ulBase is the base address of the ADC module.
  666. //! \param ulSequenceNum is the sample sequence number.
  667. //! \param pulBuffer is the address where the data is stored.
  668. //!
  669. //! This function copies data from the specified sample sequence output FIFO to
  670. //! a memory resident buffer. The number of samples available in the hardware
  671. //! FIFO are copied into the buffer, which is assumed to be large enough to
  672. //! hold that many samples. This will only return the samples that are
  673. //! presently available, which may not be the entire sample sequence if it is
  674. //! in the process of being executed.
  675. //!
  676. //! \return Returns the number of samples copied to the buffer.
  677. //
  678. //*****************************************************************************
  679. long
  680. ADCSequenceDataGet(unsigned long ulBase, unsigned long ulSequenceNum,
  681. unsigned long *pulBuffer)
  682. {
  683. unsigned long ulCount;
  684. //
  685. // Check the arguments.
  686. //
  687. ASSERT((ulBase == ADC0_BASE) || (ulBase == ADC1_BASE));
  688. ASSERT(ulSequenceNum < 4);
  689. //
  690. // Get the offset of the sequence to be read.
  691. //
  692. ulBase += ADC_SEQ + (ADC_SEQ_STEP * ulSequenceNum);
  693. //
  694. // Read samples from the FIFO until it is empty.
  695. //
  696. ulCount = 0;
  697. while(!(HWREG(ulBase + ADC_SSFSTAT) & ADC_SSFSTAT0_EMPTY) && (ulCount < 8))
  698. {
  699. //
  700. // Read the FIFO and copy it to the destination.
  701. //
  702. *pulBuffer++ = HWREG(ulBase + ADC_SSFIFO);
  703. //
  704. // Increment the count of samples read.
  705. //
  706. ulCount++;
  707. }
  708. //
  709. // Return the number of samples read.
  710. //
  711. return(ulCount);
  712. }
  713. //*****************************************************************************
  714. //
  715. //! Causes a processor trigger for a sample sequence.
  716. //!
  717. //! \param ulBase is the base address of the ADC module.
  718. //! \param ulSequenceNum is the sample sequence number, with
  719. //! \b ADC_TRIGGER_WAIT or \b ADC_TRIGGER_SIGNAL optionally ORed into it.
  720. //!
  721. //! This function triggers a processor-initiated sample sequence if the sample
  722. //! sequence trigger is configured to \b ADC_TRIGGER_PROCESSOR. If
  723. //! \b ADC_TRIGGER_WAIT is ORed into the sequence number, the
  724. //! processor-initiated trigger is delayed until a later processor-initiated
  725. //! trigger to a different ADC module that specifies \b ADC_TRIGGER_SIGNAL,
  726. //! allowing multiple ADCs to start from a processor-initiated trigger in a
  727. //! synchronous manner.
  728. //!
  729. //! \return None.
  730. //
  731. //*****************************************************************************
  732. void
  733. ADCProcessorTrigger(unsigned long ulBase, unsigned long ulSequenceNum)
  734. {
  735. //
  736. // Check the arguments.
  737. //
  738. ASSERT((ulBase == ADC0_BASE) || (ulBase == ADC1_BASE));
  739. ASSERT((ulSequenceNum & 0xf) < 4);
  740. //
  741. // Generate a processor trigger for this sample sequence.
  742. //
  743. HWREG(ulBase + ADC_O_PSSI) |= ((ulSequenceNum & 0xffff0000) |
  744. (1 << (ulSequenceNum & 0xf)));
  745. }
  746. //*****************************************************************************
  747. //
  748. //! Configures the software oversampling factor of the ADC.
  749. //!
  750. //! \param ulBase is the base address of the ADC module.
  751. //! \param ulSequenceNum is the sample sequence number.
  752. //! \param ulFactor is the number of samples to be averaged.
  753. //!
  754. //! This function configures the software oversampling for the ADC, which can
  755. //! be used to provide better resolution on the sampled data. Oversampling is
  756. //! accomplished by averaging multiple samples from the same analog input.
  757. //! Three different oversampling rates are supported; 2x, 4x, and 8x.
  758. //!
  759. //! Oversampling is only supported on the sample sequencers that are more than
  760. //! one sample in depth (that is, the fourth sample sequencer is not
  761. //! supported). Oversampling by 2x (for example) divides the depth of the
  762. //! sample sequencer by two; so 2x oversampling on the first sample sequencer
  763. //! can only provide four samples per trigger. This also means that 8x
  764. //! oversampling is only available on the first sample sequencer.
  765. //!
  766. //! \return None.
  767. //
  768. //*****************************************************************************
  769. void
  770. ADCSoftwareOversampleConfigure(unsigned long ulBase,
  771. unsigned long ulSequenceNum,
  772. unsigned long ulFactor)
  773. {
  774. unsigned long ulValue;
  775. //
  776. // Check the arguments.
  777. //
  778. ASSERT((ulBase == ADC0_BASE) || (ulBase == ADC1_BASE));
  779. ASSERT(ulSequenceNum < 3);
  780. ASSERT(((ulFactor == 2) || (ulFactor == 4) || (ulFactor == 8)) &&
  781. ((ulSequenceNum == 0) || (ulFactor != 8)));
  782. //
  783. // Convert the oversampling factor to a shift factor.
  784. //
  785. for(ulValue = 0, ulFactor >>= 1; ulFactor; ulValue++, ulFactor >>= 1)
  786. {
  787. }
  788. //
  789. // Save the sfiht factor.
  790. //
  791. g_pucOversampleFactor[ulSequenceNum] = ulValue;
  792. }
  793. //*****************************************************************************
  794. //
  795. //! Configures a step of the software oversampled sequencer.
  796. //!
  797. //! \param ulBase is the base address of the ADC module.
  798. //! \param ulSequenceNum is the sample sequence number.
  799. //! \param ulStep is the step to be configured.
  800. //! \param ulConfig is the configuration of this step.
  801. //!
  802. //! This function configures a step of the sample sequencer when using the
  803. //! software oversampling feature. The number of steps available depends on
  804. //! the oversampling factor set by ADCSoftwareOversampleConfigure(). The value
  805. //! of \e ulConfig is the same as defined for ADCSequenceStepConfigure().
  806. //!
  807. //! \return None.
  808. //
  809. //*****************************************************************************
  810. void
  811. ADCSoftwareOversampleStepConfigure(unsigned long ulBase,
  812. unsigned long ulSequenceNum,
  813. unsigned long ulStep,
  814. unsigned long ulConfig)
  815. {
  816. //
  817. // Check the arguments.
  818. //
  819. ASSERT((ulBase == ADC0_BASE) || (ulBase == ADC1_BASE));
  820. ASSERT(ulSequenceNum < 3);
  821. ASSERT(((ulSequenceNum == 0) &&
  822. (ulStep < (8 >> g_pucOversampleFactor[ulSequenceNum]))) ||
  823. (ulStep < (4 >> g_pucOversampleFactor[ulSequenceNum])));
  824. //
  825. // Get the offset of the sequence to be configured.
  826. //
  827. ulBase += ADC_SEQ + (ADC_SEQ_STEP * ulSequenceNum);
  828. //
  829. // Compute the shift for the bits that control this step.
  830. //
  831. ulStep *= 4 << g_pucOversampleFactor[ulSequenceNum];
  832. //
  833. // Loop through the hardware steps that make up this step of the software
  834. // oversampled sequence.
  835. //
  836. for(ulSequenceNum = 1 << g_pucOversampleFactor[ulSequenceNum];
  837. ulSequenceNum; ulSequenceNum--)
  838. {
  839. //
  840. // Set the analog mux value for this step.
  841. //
  842. HWREG(ulBase + ADC_SSMUX) = ((HWREG(ulBase + ADC_SSMUX) &
  843. ~(0x0000000f << ulStep)) |
  844. ((ulConfig & 0x0f) << ulStep));
  845. //
  846. // Set the upper bits of the analog mux value for this step.
  847. //
  848. HWREG(ulBase + ADC_SSEMUX) = ((HWREG(ulBase + ADC_SSEMUX) &
  849. ~(0x0000000f << ulStep)) |
  850. (((ulConfig & 0xf00) >> 8) << ulStep));
  851. //
  852. // Set the control value for this step.
  853. //
  854. HWREG(ulBase + ADC_SSCTL) = ((HWREG(ulBase + ADC_SSCTL) &
  855. ~(0x0000000f << ulStep)) |
  856. (((ulConfig & 0xf0) >> 4) << ulStep));
  857. if(ulSequenceNum != 1)
  858. {
  859. HWREG(ulBase + ADC_SSCTL) &= ~((ADC_SSCTL0_IE0 |
  860. ADC_SSCTL0_END0) << ulStep);
  861. }
  862. //
  863. // Go to the next hardware step.
  864. //
  865. ulStep += 4;
  866. }
  867. }
  868. //*****************************************************************************
  869. //
  870. //! Gets the captured data for a sample sequence using software oversampling.
  871. //!
  872. //! \param ulBase is the base address of the ADC module.
  873. //! \param ulSequenceNum is the sample sequence number.
  874. //! \param pulBuffer is the address where the data is stored.
  875. //! \param ulCount is the number of samples to be read.
  876. //!
  877. //! This function copies data from the specified sample sequence output FIFO to
  878. //! a memory resident buffer with software oversampling applied. The requested
  879. //! number of samples are copied into the data buffer; if there are not enough
  880. //! samples in the hardware FIFO to satisfy this many oversampled data items
  881. //! then incorrect results are returned. It is the caller's responsibility to
  882. //! read only the samples that are available and wait until enough data is
  883. //! available, for example as a result of receiving an interrupt.
  884. //!
  885. //! \return None.
  886. //
  887. //*****************************************************************************
  888. void
  889. ADCSoftwareOversampleDataGet(unsigned long ulBase, unsigned long ulSequenceNum,
  890. unsigned long *pulBuffer, unsigned long ulCount)
  891. {
  892. unsigned long ulIdx, ulAccum;
  893. //
  894. // Check the arguments.
  895. //
  896. ASSERT((ulBase == ADC0_BASE) || (ulBase == ADC1_BASE));
  897. ASSERT(ulSequenceNum < 3);
  898. ASSERT(((ulSequenceNum == 0) &&
  899. (ulCount < (8 >> g_pucOversampleFactor[ulSequenceNum]))) ||
  900. (ulCount < (4 >> g_pucOversampleFactor[ulSequenceNum])));
  901. //
  902. // Get the offset of the sequence to be read.
  903. //
  904. ulBase += ADC_SEQ + (ADC_SEQ_STEP * ulSequenceNum);
  905. //
  906. // Read the samples from the FIFO until it is empty.
  907. //
  908. while(ulCount--)
  909. {
  910. //
  911. // Compute the sum of the samples.
  912. //
  913. ulAccum = 0;
  914. for(ulIdx = 1 << g_pucOversampleFactor[ulSequenceNum]; ulIdx; ulIdx--)
  915. {
  916. //
  917. // Read the FIFO and add it to the accumulator.
  918. //
  919. ulAccum += HWREG(ulBase + ADC_SSFIFO);
  920. }
  921. //
  922. // Write the averaged sample to the output buffer.
  923. //
  924. *pulBuffer++ = ulAccum >> g_pucOversampleFactor[ulSequenceNum];
  925. }
  926. }
  927. //*****************************************************************************
  928. //
  929. //! Configures the hardware oversampling factor of the ADC.
  930. //!
  931. //! \param ulBase is the base address of the ADC module.
  932. //! \param ulFactor is the number of samples to be averaged.
  933. //!
  934. //! This function configures the hardware oversampling for the ADC, which can
  935. //! be used to provide better resolution on the sampled data. Oversampling is
  936. //! accomplished by averaging multiple samples from the same analog input. Six
  937. //! different oversampling rates are supported; 2x, 4x, 8x, 16x, 32x, and 64x.
  938. //! Specifying an oversampling factor of zero will disable hardware
  939. //! oversampling.
  940. //!
  941. //! Hardware oversampling applies uniformly to all sample sequencers. It does
  942. //! not reduce the depth of the sample sequencers like the software
  943. //! oversampling APIs; each sample written into the sample sequence FIFO is a
  944. //! fully oversampled analog input reading.
  945. //!
  946. //! Enabling hardware averaging increases the precision of the ADC at the cost
  947. //! of throughput. For example, enabling 4x oversampling reduces the
  948. //! throughput of a 250 Ksps ADC to 62.5 Ksps.
  949. //!
  950. //! \note Hardware oversampling is available beginning with Rev C0 of the
  951. //! Stellaris microcontroller.
  952. //!
  953. //! \return None.
  954. //
  955. //*****************************************************************************
  956. void
  957. ADCHardwareOversampleConfigure(unsigned long ulBase, unsigned long ulFactor)
  958. {
  959. unsigned long ulValue;
  960. //
  961. // Check the arguments.
  962. //
  963. ASSERT((ulBase == ADC0_BASE) || (ulBase == ADC1_BASE));
  964. ASSERT(((ulFactor == 0) || (ulFactor == 2) || (ulFactor == 4) ||
  965. (ulFactor == 8) || (ulFactor == 16) || (ulFactor == 32) ||
  966. (ulFactor == 64)));
  967. //
  968. // Convert the oversampling factor to a shift factor.
  969. //
  970. for(ulValue = 0, ulFactor >>= 1; ulFactor; ulValue++, ulFactor >>= 1)
  971. {
  972. }
  973. //
  974. // Write the shift factor to the ADC to configure the hardware oversampler.
  975. //
  976. HWREG(ulBase + ADC_O_SAC) = ulValue;
  977. }
  978. //*****************************************************************************
  979. //
  980. //! Configures an ADC digital comparator.
  981. //!
  982. //! \param ulBase is the base address of the ADC module.
  983. //! \param ulComp is the index of the comparator to configure.
  984. //! \param ulConfig is the configuration of the comparator.
  985. //!
  986. //! This function will configure a comparator. The \e ulConfig parameter is
  987. //! the result of a logical OR operation between the \b ADC_COMP_TRIG_xxx, and
  988. //! \b ADC_COMP_INT_xxx values.
  989. //!
  990. //! The \b ADC_COMP_TRIG_xxx term can take on the following values:
  991. //!
  992. //! - \b ADC_COMP_TRIG_NONE to never trigger PWM fault condition.
  993. //! - \b ADC_COMP_TRIG_LOW_ALWAYS to always trigger PWM fault condition when
  994. //! ADC output is in the low-band.
  995. //! - \b ADC_COMP_TRIG_LOW_ONCE to trigger PWM fault condition once when ADC
  996. //! output transitions into the low-band.
  997. //! - \b ADC_COMP_TRIG_LOW_HALWAYS to always trigger PWM fault condition when
  998. //! ADC output is in the low-band only if ADC output has been in the high-band
  999. //! since the last trigger output.
  1000. //! - \b ADC_COMP_TRIG_LOW_HONCE to trigger PWM fault condition once when ADC
  1001. //! output transitions into low-band only if ADC output has been in the
  1002. //! high-band since the last trigger output.
  1003. //! - \b ADC_COMP_TRIG_MID_ALWAYS to always trigger PWM fault condition when
  1004. //! ADC output is in the mid-band.
  1005. //! - \b ADC_COMP_TRIG_MID_ONCE to trigger PWM fault condition once when ADC
  1006. //! output transitions into the mid-band.
  1007. //! - \b ADC_COMP_TRIG_HIGH_ALWAYS to always trigger PWM fault condition when
  1008. //! ADC output is in the high-band.
  1009. //! - \b ADC_COMP_TRIG_HIGH_ONCE to trigger PWM fault condition once when ADC
  1010. //! output transitions into the high-band.
  1011. //! - \b ADC_COMP_TRIG_HIGH_HALWAYS to always trigger PWM fault condition when
  1012. //! ADC output is in the high-band only if ADC output has been in the low-band
  1013. //! since the last trigger output.
  1014. //! - \b ADC_COMP_TRIG_HIGH_HONCE to trigger PWM fault condition once when ADC
  1015. //! output transitions into high-band only if ADC output has been in the
  1016. //! low-band since the last trigger output.
  1017. //!
  1018. //! The \b ADC_COMP_INT_xxx term can take on the following values:
  1019. //!
  1020. //! - \b ADC_COMP_INT_NONE to never generate ADC interrupt.
  1021. //! - \b ADC_COMP_INT_LOW_ALWAYS to always generate ADC interrupt when ADC
  1022. //! output is in the low-band.
  1023. //! - \b ADC_COMP_INT_LOW_ONCE to generate ADC interrupt once when ADC output
  1024. //! transitions into the low-band.
  1025. //! - \b ADC_COMP__INT_LOW_HALWAYS to always generate ADC interrupt when ADC
  1026. //! output is in the low-band only if ADC output has been in the high-band
  1027. //! since the last trigger output.
  1028. //! - \b ADC_COMP_INT_LOW_HONCE to generate ADC interrupt once when ADC output
  1029. //! transitions into low-band only if ADC output has been in the high-band
  1030. //! since the last trigger output.
  1031. //! - \b ADC_COMP_INT_MID_ALWAYS to always generate ADC interrupt when ADC
  1032. //! output is in the mid-band.
  1033. //! - \b ADC_COMP_INT_MID_ONCE to generate ADC interrupt once when ADC output
  1034. //! transitions into the mid-band.
  1035. //! - \b ADC_COMP_INT_HIGH_ALWAYS to always generate ADC interrupt when ADC
  1036. //! output is in the high-band.
  1037. //! - \b ADC_COMP_INT_HIGH_ONCE to generate ADC interrupt once when ADC output
  1038. //! transitions into the high-band.
  1039. //! - \b ADC_COMP_INT_HIGH_HALWAYS to always generate ADC interrupt when ADC
  1040. //! output is in the high-band only if ADC output has been in the low-band
  1041. //! since the last trigger output.
  1042. //! - \b ADC_COMP_INT_HIGH_HONCE to generate ADC interrupt once when ADC output
  1043. //! transitions into high-band only if ADC output has been in the low-band
  1044. //! since the last trigger output.
  1045. //!
  1046. //! \return None.
  1047. //
  1048. //*****************************************************************************
  1049. void
  1050. ADCComparatorConfigure(unsigned long ulBase, unsigned long ulComp,
  1051. unsigned long ulConfig)
  1052. {
  1053. //
  1054. // Check the arguments.
  1055. //
  1056. ASSERT((ulBase == ADC0_BASE) || (ulBase == ADC1_BASE));
  1057. ASSERT(ulComp < 8);
  1058. //
  1059. // Save the new setting.
  1060. //
  1061. HWREG(ulBase + ADC_O_DCCTL0 + (ulComp * 4)) = ulConfig;
  1062. }
  1063. //*****************************************************************************
  1064. //
  1065. //! Defines the ADC digital comparator regions.
  1066. //!
  1067. //! \param ulBase is the base address of the ADC module.
  1068. //! \param ulComp is the index of the comparator to configure.
  1069. //! \param ulLowRef is the reference point for the low/mid band threshold.
  1070. //! \param ulHighRef is the reference point for the mid/high band threshold.
  1071. //!
  1072. //! The ADC digital comparator operation is based on three ADC value regions:
  1073. //! - \b low-band is defined as any ADC value less than or equal to the
  1074. //! \e ulLowRef value.
  1075. //! - \b mid-band is defined as any ADC value greater than the \e ulLowRef
  1076. //! value but less than or equal to the \e ulHighRef value.
  1077. //! - \b high-band is defined as any ADC value greater than the \e ulHighRef
  1078. //! value.
  1079. //!
  1080. //! \return None.
  1081. //
  1082. //*****************************************************************************
  1083. void
  1084. ADCComparatorRegionSet(unsigned long ulBase, unsigned long ulComp,
  1085. unsigned long ulLowRef, unsigned long ulHighRef)
  1086. {
  1087. //
  1088. // Check the arguments.
  1089. //
  1090. ASSERT((ulBase == ADC0_BASE) || (ulBase == ADC1_BASE));
  1091. ASSERT(ulComp < 8);
  1092. ASSERT((ulLowRef < 1024) && (ulLowRef <= ulHighRef));
  1093. ASSERT(ulHighRef < 1024);
  1094. //
  1095. // Save the new region settings.
  1096. //
  1097. HWREG(ulBase + ADC_O_DCCMP0 + (ulComp * 4)) = (ulHighRef << 16) | ulLowRef;
  1098. }
  1099. //*****************************************************************************
  1100. //
  1101. //! Resets the current ADC digital comparator conditions.
  1102. //!
  1103. //! \param ulBase is the base address of the ADC module.
  1104. //! \param ulComp is the index of the comparator.
  1105. //! \param bTrigger is the flag to indicate reset of Trigger conditions.
  1106. //! \param bInterrupt is the flag to indicate reset of Interrupt conditions.
  1107. //!
  1108. //! Because the digital comparator uses current and previous ADC values, this
  1109. //! function is provide to allow the comparator to be reset to its initial
  1110. //! value to prevent stale data from being used when a sequence is enabled.
  1111. //!
  1112. //! \return None.
  1113. //
  1114. //*****************************************************************************
  1115. void
  1116. ADCComparatorReset(unsigned long ulBase, unsigned long ulComp,
  1117. tBoolean bTrigger, tBoolean bInterrupt)
  1118. {
  1119. unsigned long ulTemp = 0;
  1120. //
  1121. // Check the arguments.
  1122. //
  1123. ASSERT((ulBase == ADC0_BASE) || (ulBase == ADC1_BASE));
  1124. ASSERT(ulComp < 8);
  1125. //
  1126. // Set the appropriate bits to reset the trigger and/or interrupt
  1127. // comparator conditions.
  1128. //
  1129. if(bTrigger)
  1130. {
  1131. ulTemp |= (1 << (16 + ulComp));
  1132. }
  1133. if(bInterrupt)
  1134. {
  1135. ulTemp |= (1 << ulComp);
  1136. }
  1137. HWREG(ulBase + ADC_O_DCRIC) = ulTemp;
  1138. }
  1139. //*****************************************************************************
  1140. //
  1141. //! Disables a sample sequence comparator interrupt.
  1142. //!
  1143. //! \param ulBase is the base address of the ADC module.
  1144. //! \param ulSequenceNum is the sample sequence number.
  1145. //!
  1146. //! This function disables the requested sample sequence comparator interrupt.
  1147. //!
  1148. //! \return None.
  1149. //
  1150. //*****************************************************************************
  1151. void
  1152. ADCComparatorIntDisable(unsigned long ulBase, unsigned long ulSequenceNum)
  1153. {
  1154. //
  1155. // Check the arguments.
  1156. //
  1157. ASSERT((ulBase == ADC0_BASE) || (ulBase == ADC1_BASE));
  1158. ASSERT(ulSequenceNum < 4);
  1159. //
  1160. // Disable this sample sequence comparator interrupt.
  1161. //
  1162. HWREG(ulBase + ADC_O_IM) &= ~(0x10000 << ulSequenceNum);
  1163. }
  1164. //*****************************************************************************
  1165. //
  1166. //! Enables a sample sequence comparator interrupt.
  1167. //!
  1168. //! \param ulBase is the base address of the ADC module.
  1169. //! \param ulSequenceNum is the sample sequence number.
  1170. //!
  1171. //! This function enables the requested sample sequence comparator interrupt.
  1172. //!
  1173. //! \return None.
  1174. //
  1175. //*****************************************************************************
  1176. void
  1177. ADCComparatorIntEnable(unsigned long ulBase, unsigned long ulSequenceNum)
  1178. {
  1179. //
  1180. // Check the arguments.
  1181. //
  1182. ASSERT((ulBase == ADC0_BASE) || (ulBase == ADC1_BASE));
  1183. ASSERT(ulSequenceNum < 4);
  1184. //
  1185. // Enable this sample sequence interrupt.
  1186. //
  1187. HWREG(ulBase + ADC_O_IM) |= 0x10000 << ulSequenceNum;
  1188. }
  1189. //*****************************************************************************
  1190. //
  1191. //! Gets the current comparator interrupt status.
  1192. //!
  1193. //! \param ulBase is the base address of the ADC module.
  1194. //!
  1195. //! This returns the digitial comparator interrupt status bits. This status
  1196. //! is sequence agnostic.
  1197. //!
  1198. //! \return The current comparator interrupt status.
  1199. //
  1200. //*****************************************************************************
  1201. unsigned long
  1202. ADCComparatorIntStatus(unsigned long ulBase)
  1203. {
  1204. //
  1205. // Check the arguments.
  1206. //
  1207. ASSERT((ulBase == ADC0_BASE) || (ulBase == ADC1_BASE));
  1208. //
  1209. // Return the digitial comparator interrupt status.
  1210. //
  1211. return(HWREG(ulBase + ADC_O_DCISC));
  1212. }
  1213. //*****************************************************************************
  1214. //
  1215. //! Clears sample sequence comparator interrupt source.
  1216. //!
  1217. //! \param ulBase is the base address of the ADC module.
  1218. //! \param ulStatus is the bit-mapped interrupts status to clear.
  1219. //!
  1220. //! The specified interrupt status is cleared.
  1221. //!
  1222. //! \return None.
  1223. //
  1224. //*****************************************************************************
  1225. void
  1226. ADCComparatorIntClear(unsigned long ulBase, unsigned long ulStatus)
  1227. {
  1228. //
  1229. // Check the arguments.
  1230. //
  1231. ASSERT((ulBase == ADC0_BASE) || (ulBase == ADC1_BASE));
  1232. //
  1233. // Clear the interrupt.
  1234. //
  1235. HWREG(ulBase + ADC_O_DCISC) = ulStatus;
  1236. }
  1237. //*****************************************************************************
  1238. //
  1239. //! Selects the ADC reference.
  1240. //!
  1241. //! \param ulBase is the base address of the ADC module.
  1242. //! \param ulRef is the reference to use.
  1243. //!
  1244. //! The ADC reference is set as specified by \e ulRef. It must be one of
  1245. //! \b ADC_REF_INT, \b ADC_REF_EXT_3V, or \b ADC_REF_EXT_1V for internal or
  1246. //! external reference. If \b ADC_REF_INT is chosen, then an internal 3V
  1247. //! reference is used and no external reference is needed. If
  1248. //! \b ADC_REF_EXT_3V is chosen, then a 3V reference must be supplied to the
  1249. //! AVREF pin. If \b ADC_REF_EXT_1V is chosen, then a 1V external referece
  1250. //! must be supplied to the AVREF pin.
  1251. //!
  1252. //! \note The ADC reference can only be selected on parts that have an external
  1253. //! reference. Consult the data sheet for your part to determine if there is
  1254. //! an external reference.
  1255. //!
  1256. //! \return None.
  1257. //
  1258. //*****************************************************************************
  1259. void
  1260. ADCReferenceSet(unsigned long ulBase, unsigned long ulRef)
  1261. {
  1262. //
  1263. // Check the arguments.
  1264. //
  1265. ASSERT((ulBase == ADC0_BASE) || (ulBase == ADC1_BASE));
  1266. ASSERT((ulRef == ADC_REF_INT) || (ulRef == ADC_REF_EXT_3V) ||
  1267. (ulRef == ADC_REF_EXT_1V));
  1268. //
  1269. // Set the reference.
  1270. //
  1271. HWREG(ulBase + ADC_O_CTL) = (HWREG(ulBase + ADC_O_CTL) & ~ADC_CTL_VREF) |
  1272. ulRef;
  1273. }
  1274. //*****************************************************************************
  1275. //
  1276. //! Returns the current setting of the ADC reference.
  1277. //!
  1278. //! \param ulBase is the base address of the ADC module.
  1279. //!
  1280. //! Returns the value of the ADC reference setting. The returned value is one
  1281. //! of \b ADC_REF_INT, \b ADC_REF_EXT_3V, or \b ADC_REF_EXT_1V.
  1282. //!
  1283. //! \note The value returned by this function is only meaningful if used on a
  1284. //! part that is capable of using an external reference. Consult the data
  1285. //! sheet for your part to determine if it has an external reference input.
  1286. //!
  1287. //! \return The current setting of the ADC reference.
  1288. //
  1289. //*****************************************************************************
  1290. unsigned long
  1291. ADCReferenceGet(unsigned long ulBase)
  1292. {
  1293. //
  1294. // Check the arguments.
  1295. //
  1296. ASSERT((ulBase == ADC0_BASE) || (ulBase == ADC1_BASE));
  1297. //
  1298. // Return the value of the reference.
  1299. //
  1300. return(HWREG(ulBase + ADC_O_CTL) & ADC_CTL_VREF);
  1301. }
  1302. //*****************************************************************************
  1303. //
  1304. //! Selects the ADC resolution.
  1305. //!
  1306. //! \param ulBase is the base address of the ADC module.
  1307. //! \param ulResolution is the ADC bit resolution.
  1308. //!
  1309. //! The ADC resolution is set as specified by \e ulResolution. It must be one
  1310. //! of \b ADC_RES_12BIT or \b ADC_RES_10BIT.
  1311. //!
  1312. //! \note The ADC resolution can only be set on parts that are capable of
  1313. //! greater than 10-bit conversions. Consult the data sheet for your part to
  1314. //! determine if it is capable of 12-bit conversions.
  1315. //!
  1316. //! \return None.
  1317. //
  1318. //*****************************************************************************
  1319. void
  1320. ADCResolutionSet(unsigned long ulBase, unsigned long ulResolution)
  1321. {
  1322. //
  1323. // Check the arguments.
  1324. //
  1325. ASSERT((ulBase == ADC0_BASE) || (ulBase == ADC1_BASE));
  1326. ASSERT((ulResolution == ADC_RES_10BIT) || (ulResolution == ADC_RES_12BIT));
  1327. //
  1328. // Set the resolution.
  1329. //
  1330. HWREG(ulBase + ADC_O_CTL) = (HWREG(ulBase + ADC_O_CTL) & ~ADC_CTL_RES) |
  1331. ulResolution;
  1332. }
  1333. //*****************************************************************************
  1334. //
  1335. //! Gets the setting of ADC resolution.
  1336. //!
  1337. //! \param ulBase is the base address of the ADC module.
  1338. //!
  1339. //! The ADC resolution is returned as one of \b ADC_RES_12BIT or
  1340. //! \b ADC_RES_10BIT.
  1341. //!
  1342. //! \note The value returned by this function is only meaningful if used on a
  1343. //! part that is capable of higher than 10-bit ADC resolution. Consult the
  1344. //! data sheet for your part to determine if it is capable of 12-bit
  1345. //! conversions.
  1346. //!
  1347. //! \return The current setting of the ADC resolution.
  1348. //
  1349. //*****************************************************************************
  1350. unsigned long
  1351. ADCResolutionGet(unsigned long ulBase)
  1352. {
  1353. //
  1354. // Check the arguments.
  1355. //
  1356. ASSERT((ulBase == ADC0_BASE) || (ulBase == ADC1_BASE));
  1357. //
  1358. // Get the resolution and return it to the caller.
  1359. //
  1360. return(HWREG(ulBase + ADC_O_CTL) & ADC_CTL_RES);
  1361. }
  1362. //*****************************************************************************
  1363. //
  1364. //! Sets the phase delay between a trigger and the start of a sequence.
  1365. //!
  1366. //! \param ulBase is the base address of the ADC module.
  1367. //! \param ulPhase is the phase delay, specified as one of \b ADC_PHASE_0,
  1368. //! \b ADC_PHASE_22_5, \b ADC_PHASE_45, \b ADC_PHASE_67_5, \b ADC_PHASE_90,
  1369. //! \b ADC_PHASE_112_5, \b ADC_PHASE_135, \b ADC_PHASE_157_5, \b ADC_PHASE_180,
  1370. //! \b ADC_PHASE_202_5, \b ADC_PHASE_225, \b ADC_PHASE_247_5, \b ADC_PHASE_270,
  1371. //! \b ADC_PHASE_292_5, \b ADC_PHASE_315, or \b ADC_PHASE_337_5.
  1372. //!
  1373. //! This function sets the phase delay between the detection of an ADC trigger
  1374. //! event and the start of the sample sequence. By selecting a different phase
  1375. //! delay for a pair of ADC modules (such as \b ADC_PHASE_0 and
  1376. //! \b ADC_PHASE_180) and having each ADC module sample the same analog input,
  1377. //! it is possible to increase the sampling rate of the analog input (with
  1378. //! samples N, N+2, N+4, and so on, coming from the first ADC and samples N+1,
  1379. //! N+3, N+5, and so on, coming from the second ADC). The ADC module has a
  1380. //! single phase delay that is applied to all sample sequences within that
  1381. //! module.
  1382. //!
  1383. //! \note This capability is not available on all parts.
  1384. //!
  1385. //! \return None.
  1386. //
  1387. //*****************************************************************************
  1388. void
  1389. ADCPhaseDelaySet(unsigned long ulBase, unsigned long ulPhase)
  1390. {
  1391. //
  1392. // Check the arguments.
  1393. //
  1394. ASSERT((ulBase == ADC0_BASE) || (ulBase == ADC1_BASE));
  1395. ASSERT((ulPhase == ADC_PHASE_0) || (ulPhase == ADC_PHASE_22_5) ||
  1396. (ulPhase == ADC_PHASE_45) || (ulPhase == ADC_PHASE_67_5) ||
  1397. (ulPhase == ADC_PHASE_90) || (ulPhase == ADC_PHASE_112_5) ||
  1398. (ulPhase == ADC_PHASE_135) || (ulPhase == ADC_PHASE_157_5) ||
  1399. (ulPhase == ADC_PHASE_180) || (ulPhase == ADC_PHASE_202_5) ||
  1400. (ulPhase == ADC_PHASE_225) || (ulPhase == ADC_PHASE_247_5) ||
  1401. (ulPhase == ADC_PHASE_270) || (ulPhase == ADC_PHASE_292_5) ||
  1402. (ulPhase == ADC_PHASE_315) || (ulPhase == ADC_PHASE_337_5));
  1403. //
  1404. // Set the phase delay.
  1405. //
  1406. HWREG(ulBase + ADC_O_SPC) = ulPhase;
  1407. }
  1408. //*****************************************************************************
  1409. //
  1410. //! Gets the phase delay between a trigger and the start of a sequence.
  1411. //!
  1412. //! \param ulBase is the base address of the ADC module.
  1413. //!
  1414. //! This function gets the current phase delay between the detection of an ADC
  1415. //! trigger event and the start of the sample sequence.
  1416. //!
  1417. //! \return Returns the phase delay, specified as one of \b ADC_PHASE_0,
  1418. //! \b ADC_PHASE_22_5, \b ADC_PHASE_45, \b ADC_PHASE_67_5, \b ADC_PHASE_90,
  1419. //! \b ADC_PHASE_112_5, \b ADC_PHASE_135, \b ADC_PHASE_157_5, \b ADC_PHASE_180,
  1420. //! \b ADC_PHASE_202_5, \b ADC_PHASE_225, \b ADC_PHASE_247_5, \b ADC_PHASE_270,
  1421. //! \b ADC_PHASE_292_5, \b ADC_PHASE_315, or \b ADC_PHASE_337_5.
  1422. //
  1423. //*****************************************************************************
  1424. unsigned long
  1425. ADCPhaseDelayGet(unsigned long ulBase)
  1426. {
  1427. //
  1428. // Check the arguments.
  1429. //
  1430. ASSERT((ulBase == ADC0_BASE) || (ulBase == ADC1_BASE));
  1431. //
  1432. // Return the phase delay.
  1433. //
  1434. return(HWREG(ulBase + ADC_O_SPC));
  1435. }
  1436. //*****************************************************************************
  1437. //
  1438. // Close the Doxygen group.
  1439. //! @}
  1440. //
  1441. //*****************************************************************************