adc.c 54 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 8264 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 function enables the global interrupt in the
  71. //! interrupt controller; the sequence interrupt must be enabled with
  72. //! ADCIntEnable(). It is the interrupt handler's responsibility to clear the
  73. //! interrupt source via 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 function disables
  113. //! the global interrupt in the interrupt controller; the sequence interrupt
  114. //! must 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 function returns the interrupt status for the specified sample
  211. //! sequence. Either the raw interrupt status or the status of interrupts that
  212. //! are 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 function must be called in the interrupt handler to keep
  262. //! the interrupt from being triggered again immediately upon exit.
  263. //!
  264. //! \note Because there is a write buffer in the Cortex-M 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 sequencers range from zero to three; sequencer zero captures
  354. //! up to eight samples, sequencers one and two capture up to four samples,
  355. //! and sequencer three captures a single sample. The trigger condition and
  356. //! priority (with respect to other sample sequencer execution) are 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. Note that some microcontrollers can
  370. //! select from any GPIO using the
  371. //! GPIOADCTriggerEnable() function.
  372. //! - \b ADC_TRIGGER_TIMER - A trigger generated by a timer; configured with
  373. //! TimerControlTrigger().
  374. //! - \b ADC_TRIGGER_PWM0 - A trigger generated by the first PWM generator;
  375. //! configured with PWMGenIntTrigEnable().
  376. //! - \b ADC_TRIGGER_PWM1 - A trigger generated by the second PWM generator;
  377. //! configured with PWMGenIntTrigEnable().
  378. //! - \b ADC_TRIGGER_PWM2 - A trigger generated by the third PWM generator;
  379. //! configured with PWMGenIntTrigEnable().
  380. //! - \b ADC_TRIGGER_PWM3 - A trigger generated by the fourth PWM generator;
  381. //! configured with PWMGenIntTrigEnable().
  382. //! - \b ADC_TRIGGER_ALWAYS - A trigger that is always asserted, causing the
  383. //! sample sequence to capture repeatedly (so long as
  384. //! there is not a higher priority source active).
  385. //!
  386. //! Note that not all trigger sources are available on all Stellaris family
  387. //! members; consult the data sheet for the device in question to determine the
  388. //! availability of triggers.
  389. //!
  390. //! The \e ulPriority parameter is a value between 0 and 3, where 0 represents
  391. //! the highest priority and 3 the lowest. Note that when programming the
  392. //! priority among a set of sample sequences, each must have unique priority;
  393. //! it is up to the caller to guarantee the uniqueness of the priorities.
  394. //!
  395. //! \return None.
  396. //
  397. //*****************************************************************************
  398. void
  399. ADCSequenceConfigure(unsigned long ulBase, unsigned long ulSequenceNum,
  400. unsigned long ulTrigger, unsigned long ulPriority)
  401. {
  402. //
  403. // Check the arugments.
  404. //
  405. ASSERT((ulBase == ADC0_BASE) || (ulBase == ADC1_BASE));
  406. ASSERT(ulSequenceNum < 4);
  407. ASSERT((ulTrigger == ADC_TRIGGER_PROCESSOR) ||
  408. (ulTrigger == ADC_TRIGGER_COMP0) ||
  409. (ulTrigger == ADC_TRIGGER_COMP1) ||
  410. (ulTrigger == ADC_TRIGGER_COMP2) ||
  411. (ulTrigger == ADC_TRIGGER_EXTERNAL) ||
  412. (ulTrigger == ADC_TRIGGER_TIMER) ||
  413. (ulTrigger == ADC_TRIGGER_PWM0) ||
  414. (ulTrigger == ADC_TRIGGER_PWM1) ||
  415. (ulTrigger == ADC_TRIGGER_PWM2) ||
  416. (ulTrigger == ADC_TRIGGER_PWM3) ||
  417. (ulTrigger == ADC_TRIGGER_ALWAYS));
  418. ASSERT(ulPriority < 4);
  419. //
  420. // Compute the shift for the bits that control this sample sequence.
  421. //
  422. ulSequenceNum *= 4;
  423. //
  424. // Set the trigger event for this sample sequence.
  425. //
  426. HWREG(ulBase + ADC_O_EMUX) = ((HWREG(ulBase + ADC_O_EMUX) &
  427. ~(0xf << ulSequenceNum)) |
  428. ((ulTrigger & 0xf) << ulSequenceNum));
  429. //
  430. // Set the priority for this sample sequence.
  431. //
  432. HWREG(ulBase + ADC_O_SSPRI) = ((HWREG(ulBase + ADC_O_SSPRI) &
  433. ~(0xf << ulSequenceNum)) |
  434. ((ulPriority & 0x3) << ulSequenceNum));
  435. }
  436. //*****************************************************************************
  437. //
  438. //! Configure a step of the sample sequencer.
  439. //!
  440. //! \param ulBase is the base address of the ADC module.
  441. //! \param ulSequenceNum is the sample sequence number.
  442. //! \param ulStep is the step to be configured.
  443. //! \param ulConfig is the configuration of this step; must be a logical OR of
  444. //! \b ADC_CTL_TS, \b ADC_CTL_IE, \b ADC_CTL_END, \b ADC_CTL_D, one of the
  445. //! input channel selects (\b ADC_CTL_CH0 through \b ADC_CTL_CH23), and one of
  446. //! the digital comparator selects (\b ADC_CTL_CMP0 through \b ADC_CTL_CMP7).
  447. //!
  448. //! This function configures the ADC for one step of a sample sequence. The
  449. //! ADC can be configured for single-ended or differential operation
  450. //! (the \b ADC_CTL_D bit selects differential operation when set), the
  451. //! channel to be sampled can be chosen (the \b ADC_CTL_CH0 through
  452. //! \b ADC_CTL_CH23 values), and the internal temperature sensor can be
  453. //! selected (the \b ADC_CTL_TS bit). Additionally, this step can be defined
  454. //! as the last in the sequence (the \b ADC_CTL_END bit) and it can be
  455. //! configured to cause an interrupt when the step is complete (the
  456. //! \b ADC_CTL_IE bit). If the digital comparators are present on the device,
  457. //! this step may also be configured to send the ADC sample to the selected
  458. //! comparator using \b ADC_CTL_CMP0 through \b ADC_CTL_CMP7. The configuration
  459. //! is used by the ADC at the appropriate time when the trigger for
  460. //! this sequence occurs.
  461. //!
  462. //! \note If the Digitial Comparator is present and enabled using the
  463. //! \b ADC_CTL_CMP0 through \b ADC_CTL_CMP7 selects, the ADC sample is NOT
  464. //! written into the ADC sequence data FIFO.
  465. //!
  466. //! The \e ulStep parameter determines the order in which the samples are
  467. //! captured by the ADC when the trigger occurs. It can range from zero to
  468. //! seven for the first sample sequencer, from zero to three for the second and
  469. //! third sample sequencer, and can only be zero for the fourth sample
  470. //! sequencer.
  471. //!
  472. //! Differential mode only works with adjacent channel pairs (for example, 0
  473. //! and 1). The channel select must be the number of the channel pair to
  474. //! sample (for example, \b ADC_CTL_CH0 for 0 and 1, or \b ADC_CTL_CH1 for 2
  475. //! and 3) or undefined results are returned by the ADC. Additionally, if
  476. //! differential mode is selected when the temperature sensor is being sampled,
  477. //! undefined results are returned by the ADC.
  478. //!
  479. //! It is the responsibility of the caller to ensure that a valid configuration
  480. //! is specified; this function does not check the validity of the specified
  481. //! configuration.
  482. //!
  483. //! \return None.
  484. //
  485. //*****************************************************************************
  486. void
  487. ADCSequenceStepConfigure(unsigned long ulBase, unsigned long ulSequenceNum,
  488. unsigned long ulStep, unsigned long ulConfig)
  489. {
  490. unsigned long ulTemp;
  491. //
  492. // Check the arugments.
  493. //
  494. ASSERT((ulBase == ADC0_BASE) || (ulBase == ADC1_BASE));
  495. ASSERT(ulSequenceNum < 4);
  496. ASSERT(((ulSequenceNum == 0) && (ulStep < 8)) ||
  497. ((ulSequenceNum == 1) && (ulStep < 4)) ||
  498. ((ulSequenceNum == 2) && (ulStep < 4)) ||
  499. ((ulSequenceNum == 3) && (ulStep < 1)));
  500. //
  501. // Get the offset of the sequence to be configured.
  502. //
  503. ulBase += ADC_SEQ + (ADC_SEQ_STEP * ulSequenceNum);
  504. //
  505. // Compute the shift for the bits that control this step.
  506. //
  507. ulStep *= 4;
  508. //
  509. // Set the analog mux value for this step.
  510. //
  511. HWREG(ulBase + ADC_SSMUX) = ((HWREG(ulBase + ADC_SSMUX) &
  512. ~(0x0000000f << ulStep)) |
  513. ((ulConfig & 0x0f) << ulStep));
  514. //
  515. // Set the upper bits of the analog mux value for this step.
  516. //
  517. HWREG(ulBase + ADC_SSEMUX) = ((HWREG(ulBase + ADC_SSEMUX) &
  518. ~(0x0000000f << ulStep)) |
  519. (((ulConfig & 0xf00) >> 8) << ulStep));
  520. //
  521. // Set the control value for this step.
  522. //
  523. HWREG(ulBase + ADC_SSCTL) = ((HWREG(ulBase + ADC_SSCTL) &
  524. ~(0x0000000f << ulStep)) |
  525. (((ulConfig & 0xf0) >> 4) << ulStep));
  526. //
  527. // Enable digital comparator if specified in the ulConfig bit-fields.
  528. //
  529. if(ulConfig & 0x000F0000)
  530. {
  531. //
  532. // Program the comparator for the specified step.
  533. //
  534. ulTemp = HWREG(ulBase + ADC_SSDC);
  535. ulTemp &= ~(0xF << ulStep);
  536. ulTemp |= (((ulConfig & 0x00070000) >> 16) << ulStep);
  537. HWREG(ulBase + ADC_SSDC) = ulTemp;
  538. //
  539. // Enable the comparator.
  540. //
  541. ulTemp = HWREG(ulBase + ADC_SSOP);
  542. ulTemp |= (1 << ulStep);
  543. HWREG(ulBase + ADC_SSOP) = ulTemp;
  544. }
  545. //
  546. // Disable digital comparator if not specified.
  547. //
  548. else
  549. {
  550. ulTemp = HWREG(ulBase + ADC_SSOP);
  551. ulTemp &= ~(1 << ulStep);
  552. HWREG(ulBase + ADC_SSOP) = ulTemp;
  553. }
  554. }
  555. //*****************************************************************************
  556. //
  557. //! Determines if a sample sequence overflow occurred.
  558. //!
  559. //! \param ulBase is the base address of the ADC module.
  560. //! \param ulSequenceNum is the sample sequence number.
  561. //!
  562. //! This function determines if a sample sequence overflow has occurred.
  563. //! Overflow happens if the captured samples are not read from the FIFO before
  564. //! the next trigger occurs.
  565. //!
  566. //! \return Returns zero if there was not an overflow, and non-zero if there
  567. //! was.
  568. //
  569. //*****************************************************************************
  570. long
  571. ADCSequenceOverflow(unsigned long ulBase, unsigned long ulSequenceNum)
  572. {
  573. //
  574. // Check the arguments.
  575. //
  576. ASSERT((ulBase == ADC0_BASE) || (ulBase == ADC1_BASE));
  577. ASSERT(ulSequenceNum < 4);
  578. //
  579. // Determine if there was an overflow on this sequence.
  580. //
  581. return(HWREG(ulBase + ADC_O_OSTAT) & (1 << ulSequenceNum));
  582. }
  583. //*****************************************************************************
  584. //
  585. //! Clears the overflow condition on a sample sequence.
  586. //!
  587. //! \param ulBase is the base address of the ADC module.
  588. //! \param ulSequenceNum is the sample sequence number.
  589. //!
  590. //! This function clears an overflow condition on one of the sample sequences.
  591. //! The overflow condition must be cleared in order to detect a subsequent
  592. //! overflow condition (it otherwise causes no harm).
  593. //!
  594. //! \return None.
  595. //
  596. //*****************************************************************************
  597. void
  598. ADCSequenceOverflowClear(unsigned long ulBase, unsigned long ulSequenceNum)
  599. {
  600. //
  601. // Check the arguments.
  602. //
  603. ASSERT((ulBase == ADC0_BASE) || (ulBase == ADC1_BASE));
  604. ASSERT(ulSequenceNum < 4);
  605. //
  606. // Clear the overflow condition for this sequence.
  607. //
  608. HWREG(ulBase + ADC_O_OSTAT) = 1 << ulSequenceNum;
  609. }
  610. //*****************************************************************************
  611. //
  612. //! Determines if a sample sequence underflow occurred.
  613. //!
  614. //! \param ulBase is the base address of the ADC module.
  615. //! \param ulSequenceNum is the sample sequence number.
  616. //!
  617. //! This function determines if a sample sequence underflow has occurred.
  618. //! Underflow happens if too many samples are read from the FIFO.
  619. //!
  620. //! \return Returns zero if there was not an underflow, and non-zero if there
  621. //! was.
  622. //
  623. //*****************************************************************************
  624. long
  625. ADCSequenceUnderflow(unsigned long ulBase, unsigned long ulSequenceNum)
  626. {
  627. //
  628. // Check the arguments.
  629. //
  630. ASSERT((ulBase == ADC0_BASE) || (ulBase == ADC1_BASE));
  631. ASSERT(ulSequenceNum < 4);
  632. //
  633. // Determine if there was an underflow on this sequence.
  634. //
  635. return(HWREG(ulBase + ADC_O_USTAT) & (1 << ulSequenceNum));
  636. }
  637. //*****************************************************************************
  638. //
  639. //! Clears the underflow condition on a sample sequence.
  640. //!
  641. //! \param ulBase is the base address of the ADC module.
  642. //! \param ulSequenceNum is the sample sequence number.
  643. //!
  644. //! This function clears an underflow condition on one of the sample sequencers.
  645. //! The underflow condition must be cleared in order to detect a subsequent
  646. //! underflow condition (it otherwise causes no harm).
  647. //!
  648. //! \return None.
  649. //
  650. //*****************************************************************************
  651. void
  652. ADCSequenceUnderflowClear(unsigned long ulBase, unsigned long ulSequenceNum)
  653. {
  654. //
  655. // Check the arguments.
  656. //
  657. ASSERT((ulBase == ADC0_BASE) || (ulBase == ADC1_BASE));
  658. ASSERT(ulSequenceNum < 4);
  659. //
  660. // Clear the underflow condition for this sequence.
  661. //
  662. HWREG(ulBase + ADC_O_USTAT) = 1 << ulSequenceNum;
  663. }
  664. //*****************************************************************************
  665. //
  666. //! Gets the captured data for a sample sequence.
  667. //!
  668. //! \param ulBase is the base address of the ADC module.
  669. //! \param ulSequenceNum is the sample sequence number.
  670. //! \param pulBuffer is the address where the data is stored.
  671. //!
  672. //! This function copies data from the specified sample sequencer output FIFO
  673. //! to a memory resident buffer. The number of samples available in the
  674. //! hardware FIFO are copied into the buffer, which is assumed to be large
  675. //! enough to hold that many samples. This function only returns the samples
  676. //! that are presently available, which may not be the entire sample sequence
  677. //! if it is in the process of being executed.
  678. //!
  679. //! \return Returns the number of samples copied to the buffer.
  680. //
  681. //*****************************************************************************
  682. long
  683. ADCSequenceDataGet(unsigned long ulBase, unsigned long ulSequenceNum,
  684. unsigned long *pulBuffer)
  685. {
  686. unsigned long ulCount;
  687. //
  688. // Check the arguments.
  689. //
  690. ASSERT((ulBase == ADC0_BASE) || (ulBase == ADC1_BASE));
  691. ASSERT(ulSequenceNum < 4);
  692. //
  693. // Get the offset of the sequence to be read.
  694. //
  695. ulBase += ADC_SEQ + (ADC_SEQ_STEP * ulSequenceNum);
  696. //
  697. // Read samples from the FIFO until it is empty.
  698. //
  699. ulCount = 0;
  700. while(!(HWREG(ulBase + ADC_SSFSTAT) & ADC_SSFSTAT0_EMPTY) && (ulCount < 8))
  701. {
  702. //
  703. // Read the FIFO and copy it to the destination.
  704. //
  705. *pulBuffer++ = HWREG(ulBase + ADC_SSFIFO);
  706. //
  707. // Increment the count of samples read.
  708. //
  709. ulCount++;
  710. }
  711. //
  712. // Return the number of samples read.
  713. //
  714. return(ulCount);
  715. }
  716. //*****************************************************************************
  717. //
  718. //! Causes a processor trigger for a sample sequence.
  719. //!
  720. //! \param ulBase is the base address of the ADC module.
  721. //! \param ulSequenceNum is the sample sequence number, with
  722. //! \b ADC_TRIGGER_WAIT or \b ADC_TRIGGER_SIGNAL optionally ORed into it.
  723. //!
  724. //! This function triggers a processor-initiated sample sequence if the sample
  725. //! sequence trigger is configured to \b ADC_TRIGGER_PROCESSOR. If
  726. //! \b ADC_TRIGGER_WAIT is ORed into the sequence number, the
  727. //! processor-initiated trigger is delayed until a later processor-initiated
  728. //! trigger to a different ADC module that specifies \b ADC_TRIGGER_SIGNAL,
  729. //! allowing multiple ADCs to start from a processor-initiated trigger in a
  730. //! synchronous manner.
  731. //!
  732. //! \return None.
  733. //
  734. //*****************************************************************************
  735. void
  736. ADCProcessorTrigger(unsigned long ulBase, unsigned long ulSequenceNum)
  737. {
  738. //
  739. // Check the arguments.
  740. //
  741. ASSERT((ulBase == ADC0_BASE) || (ulBase == ADC1_BASE));
  742. ASSERT((ulSequenceNum & 0xf) < 4);
  743. //
  744. // Generate a processor trigger for this sample sequence.
  745. //
  746. HWREG(ulBase + ADC_O_PSSI) |= ((ulSequenceNum & 0xffff0000) |
  747. (1 << (ulSequenceNum & 0xf)));
  748. }
  749. //*****************************************************************************
  750. //
  751. //! Configures the software oversampling factor of the ADC.
  752. //!
  753. //! \param ulBase is the base address of the ADC module.
  754. //! \param ulSequenceNum is the sample sequence number.
  755. //! \param ulFactor is the number of samples to be averaged.
  756. //!
  757. //! This function configures the software oversampling for the ADC, which can
  758. //! be used to provide better resolution on the sampled data. Oversampling is
  759. //! accomplished by averaging multiple samples from the same analog input.
  760. //! Three different oversampling rates are supported; 2x, 4x, and 8x.
  761. //!
  762. //! Oversampling is only supported on the sample sequencers that are more than
  763. //! one sample in depth (that is, the fourth sample sequencer is not
  764. //! supported). Oversampling by 2x (for example) divides the depth of the
  765. //! sample sequencer by two; so 2x oversampling on the first sample sequencer
  766. //! can only provide four samples per trigger. This also means that 8x
  767. //! oversampling is only available on the first sample sequencer.
  768. //!
  769. //! \return None.
  770. //
  771. //*****************************************************************************
  772. void
  773. ADCSoftwareOversampleConfigure(unsigned long ulBase,
  774. unsigned long ulSequenceNum,
  775. unsigned long ulFactor)
  776. {
  777. unsigned long ulValue;
  778. //
  779. // Check the arguments.
  780. //
  781. ASSERT((ulBase == ADC0_BASE) || (ulBase == ADC1_BASE));
  782. ASSERT(ulSequenceNum < 3);
  783. ASSERT(((ulFactor == 2) || (ulFactor == 4) || (ulFactor == 8)) &&
  784. ((ulSequenceNum == 0) || (ulFactor != 8)));
  785. //
  786. // Convert the oversampling factor to a shift factor.
  787. //
  788. for(ulValue = 0, ulFactor >>= 1; ulFactor; ulValue++, ulFactor >>= 1)
  789. {
  790. }
  791. //
  792. // Save the sfiht factor.
  793. //
  794. g_pucOversampleFactor[ulSequenceNum] = ulValue;
  795. }
  796. //*****************************************************************************
  797. //
  798. //! Configures a step of the software oversampled sequencer.
  799. //!
  800. //! \param ulBase is the base address of the ADC module.
  801. //! \param ulSequenceNum is the sample sequence number.
  802. //! \param ulStep is the step to be configured.
  803. //! \param ulConfig is the configuration of this step.
  804. //!
  805. //! This function configures a step of the sample sequencer when using the
  806. //! software oversampling feature. The number of steps available depends on
  807. //! the oversampling factor set by ADCSoftwareOversampleConfigure(). The value
  808. //! of \e ulConfig is the same as defined for ADCSequenceStepConfigure().
  809. //!
  810. //! \return None.
  811. //
  812. //*****************************************************************************
  813. void
  814. ADCSoftwareOversampleStepConfigure(unsigned long ulBase,
  815. unsigned long ulSequenceNum,
  816. unsigned long ulStep,
  817. unsigned long ulConfig)
  818. {
  819. //
  820. // Check the arguments.
  821. //
  822. ASSERT((ulBase == ADC0_BASE) || (ulBase == ADC1_BASE));
  823. ASSERT(ulSequenceNum < 3);
  824. ASSERT(((ulSequenceNum == 0) &&
  825. (ulStep < (8 >> g_pucOversampleFactor[ulSequenceNum]))) ||
  826. (ulStep < (4 >> g_pucOversampleFactor[ulSequenceNum])));
  827. //
  828. // Get the offset of the sequence to be configured.
  829. //
  830. ulBase += ADC_SEQ + (ADC_SEQ_STEP * ulSequenceNum);
  831. //
  832. // Compute the shift for the bits that control this step.
  833. //
  834. ulStep *= 4 << g_pucOversampleFactor[ulSequenceNum];
  835. //
  836. // Loop through the hardware steps that make up this step of the software
  837. // oversampled sequence.
  838. //
  839. for(ulSequenceNum = 1 << g_pucOversampleFactor[ulSequenceNum];
  840. ulSequenceNum; ulSequenceNum--)
  841. {
  842. //
  843. // Set the analog mux value for this step.
  844. //
  845. HWREG(ulBase + ADC_SSMUX) = ((HWREG(ulBase + ADC_SSMUX) &
  846. ~(0x0000000f << ulStep)) |
  847. ((ulConfig & 0x0f) << ulStep));
  848. //
  849. // Set the upper bits of the analog mux value for this step.
  850. //
  851. HWREG(ulBase + ADC_SSEMUX) = ((HWREG(ulBase + ADC_SSEMUX) &
  852. ~(0x0000000f << ulStep)) |
  853. (((ulConfig & 0xf00) >> 8) << ulStep));
  854. //
  855. // Set the control value for this step.
  856. //
  857. HWREG(ulBase + ADC_SSCTL) = ((HWREG(ulBase + ADC_SSCTL) &
  858. ~(0x0000000f << ulStep)) |
  859. (((ulConfig & 0xf0) >> 4) << ulStep));
  860. if(ulSequenceNum != 1)
  861. {
  862. HWREG(ulBase + ADC_SSCTL) &= ~((ADC_SSCTL0_IE0 |
  863. ADC_SSCTL0_END0) << ulStep);
  864. }
  865. //
  866. // Go to the next hardware step.
  867. //
  868. ulStep += 4;
  869. }
  870. }
  871. //*****************************************************************************
  872. //
  873. //! Gets the captured data for a sample sequence using software oversampling.
  874. //!
  875. //! \param ulBase is the base address of the ADC module.
  876. //! \param ulSequenceNum is the sample sequence number.
  877. //! \param pulBuffer is the address where the data is stored.
  878. //! \param ulCount is the number of samples to be read.
  879. //!
  880. //! This function copies data from the specified sample sequence output FIFO to
  881. //! a memory resident buffer with software oversampling applied. The requested
  882. //! number of samples are copied into the data buffer; if there are not enough
  883. //! samples in the hardware FIFO to satisfy this many oversampled data items,
  884. //! then incorrect results are returned. It is the caller's responsibility to
  885. //! read only the samples that are available and wait until enough data is
  886. //! available, for example as a result of receiving an interrupt.
  887. //!
  888. //! \return None.
  889. //
  890. //*****************************************************************************
  891. void
  892. ADCSoftwareOversampleDataGet(unsigned long ulBase, unsigned long ulSequenceNum,
  893. unsigned long *pulBuffer, unsigned long ulCount)
  894. {
  895. unsigned long ulIdx, ulAccum;
  896. //
  897. // Check the arguments.
  898. //
  899. ASSERT((ulBase == ADC0_BASE) || (ulBase == ADC1_BASE));
  900. ASSERT(ulSequenceNum < 3);
  901. ASSERT(((ulSequenceNum == 0) &&
  902. (ulCount < (8 >> g_pucOversampleFactor[ulSequenceNum]))) ||
  903. (ulCount < (4 >> g_pucOversampleFactor[ulSequenceNum])));
  904. //
  905. // Get the offset of the sequence to be read.
  906. //
  907. ulBase += ADC_SEQ + (ADC_SEQ_STEP * ulSequenceNum);
  908. //
  909. // Read the samples from the FIFO until it is empty.
  910. //
  911. while(ulCount--)
  912. {
  913. //
  914. // Compute the sum of the samples.
  915. //
  916. ulAccum = 0;
  917. for(ulIdx = 1 << g_pucOversampleFactor[ulSequenceNum]; ulIdx; ulIdx--)
  918. {
  919. //
  920. // Read the FIFO and add it to the accumulator.
  921. //
  922. ulAccum += HWREG(ulBase + ADC_SSFIFO);
  923. }
  924. //
  925. // Write the averaged sample to the output buffer.
  926. //
  927. *pulBuffer++ = ulAccum >> g_pucOversampleFactor[ulSequenceNum];
  928. }
  929. }
  930. //*****************************************************************************
  931. //
  932. //! Configures the hardware oversampling factor of the ADC.
  933. //!
  934. //! \param ulBase is the base address of the ADC module.
  935. //! \param ulFactor is the number of samples to be averaged.
  936. //!
  937. //! This function configures the hardware oversampling for the ADC, which can
  938. //! be used to provide better resolution on the sampled data. Oversampling is
  939. //! accomplished by averaging multiple samples from the same analog input. Six
  940. //! different oversampling rates are supported; 2x, 4x, 8x, 16x, 32x, and 64x.
  941. //! Specifying an oversampling factor of zero disables hardware
  942. //! oversampling.
  943. //!
  944. //! Hardware oversampling applies uniformly to all sample sequencers. It does
  945. //! not reduce the depth of the sample sequencers like the software
  946. //! oversampling APIs; each sample written into the sample sequencer FIFO is a
  947. //! fully oversampled analog input reading.
  948. //!
  949. //! Enabling hardware averaging increases the precision of the ADC at the cost
  950. //! of throughput. For example, enabling 4x oversampling reduces the
  951. //! throughput of a 250 Ksps ADC to 62.5 Ksps.
  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 configures 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 allows 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 function returns the digitial comparator interrupt status bits. This
  1196. //! status 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_M) |
  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_M);
  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. //! changing ADC resolution mode. Consult the data sheet for your part to
  1314. //! determine if it is capable of operating in more than one resolution mode.
  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 changing ADC resolution mode. Consult the
  1344. //! data sheet for your part to determine if it is capable of operating in
  1345. //! more than one resolution mode.
  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. //*****************************************************************************