adc.c 73 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078
  1. //*****************************************************************************
  2. //
  3. // adc.c - Driver for the ADC.
  4. //
  5. // Copyright (c) 2005-2020 Texas Instruments Incorporated. All rights reserved.
  6. // Software License Agreement
  7. //
  8. // Redistribution and use in source and binary forms, with or without
  9. // modification, are permitted provided that the following conditions
  10. // are met:
  11. //
  12. // Redistributions of source code must retain the above copyright
  13. // notice, this list of conditions and the following disclaimer.
  14. //
  15. // Redistributions in binary form must reproduce the above copyright
  16. // notice, this list of conditions and the following disclaimer in the
  17. // documentation and/or other materials provided with the
  18. // distribution.
  19. //
  20. // Neither the name of Texas Instruments Incorporated nor the names of
  21. // its contributors may be used to endorse or promote products derived
  22. // from this software without specific prior written permission.
  23. //
  24. // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  25. // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  26. // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  27. // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  28. // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  29. // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  30. // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  31. // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  32. // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  33. // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  34. // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  35. //
  36. // This is part of revision 2.2.0.295 of the Tiva Peripheral Driver Library.
  37. //
  38. //*****************************************************************************
  39. //*****************************************************************************
  40. //
  41. //! \addtogroup adc_api
  42. //! @{
  43. //
  44. //*****************************************************************************
  45. #include <stdbool.h>
  46. #include <stdint.h>
  47. #include "inc/hw_adc.h"
  48. #include "inc/hw_ints.h"
  49. #include "inc/hw_memmap.h"
  50. #include "inc/hw_types.h"
  51. #include "inc/hw_sysctl.h"
  52. #include "driverlib/adc.h"
  53. #include "driverlib/debug.h"
  54. #include "driverlib/interrupt.h"
  55. //*****************************************************************************
  56. //
  57. // These defines are used by the ADC driver to simplify access to the ADC
  58. // sequencer's registers.
  59. //
  60. //*****************************************************************************
  61. #define ADC_SEQ (ADC_O_SSMUX0)
  62. #define ADC_SEQ_STEP (ADC_O_SSMUX1 - ADC_O_SSMUX0)
  63. #define ADC_SSMUX (ADC_O_SSMUX0 - ADC_O_SSMUX0)
  64. #define ADC_SSEMUX (ADC_O_SSEMUX0 - ADC_O_SSMUX0)
  65. #define ADC_SSCTL (ADC_O_SSCTL0 - ADC_O_SSMUX0)
  66. #define ADC_SSFIFO (ADC_O_SSFIFO0 - ADC_O_SSMUX0)
  67. #define ADC_SSFSTAT (ADC_O_SSFSTAT0 - ADC_O_SSMUX0)
  68. #define ADC_SSOP (ADC_O_SSOP0 - ADC_O_SSMUX0)
  69. #define ADC_SSDC (ADC_O_SSDC0 - ADC_O_SSMUX0)
  70. #define ADC_SSTSH (ADC_O_SSTSH0 - ADC_O_SSMUX0)
  71. //*****************************************************************************
  72. //
  73. // The currently configured software oversampling factor for each of the ADC
  74. // sequencers.
  75. //
  76. //*****************************************************************************
  77. static uint8_t g_pui8OversampleFactor[2][3];
  78. //*****************************************************************************
  79. //
  80. //! Returns the interrupt number for a given ADC base address and sequence
  81. //! number.
  82. //!
  83. //! \param ui32Base is the base address of the ADC module.
  84. //! \param ui32SequenceNum is the sample sequence number.
  85. //!
  86. //! This function returns the interrupt number for the ADC module and sequence
  87. //! number provided in the \e ui32Base and \e ui32SequenceNum parameters.
  88. //!
  89. //! \return Returns the ADC sequence interrupt number or 0 if the interrupt
  90. //! does not exist.
  91. //
  92. //*****************************************************************************
  93. static uint_fast8_t
  94. _ADCIntNumberGet(uint32_t ui32Base, uint32_t ui32SequenceNum)
  95. {
  96. uint_fast8_t ui8Int;
  97. //
  98. // Determine the interrupt to register based on the sequence number.
  99. //
  100. if(CLASS_IS_TM4C123)
  101. {
  102. ui8Int = ((ui32Base == ADC0_BASE) ?
  103. (INT_ADC0SS0_TM4C123 + ui32SequenceNum) :
  104. (INT_ADC1SS0_TM4C123 + ui32SequenceNum));
  105. }
  106. else if(CLASS_IS_TM4C129)
  107. {
  108. ui8Int = ((ui32Base == ADC0_BASE) ?
  109. (INT_ADC0SS0_TM4C129 + ui32SequenceNum) :
  110. (INT_ADC1SS0_TM4C129 + ui32SequenceNum));
  111. }
  112. else
  113. {
  114. ui8Int = 0;
  115. }
  116. return(ui8Int);
  117. }
  118. //*****************************************************************************
  119. //
  120. //! Registers an interrupt handler for an ADC interrupt.
  121. //!
  122. //! \param ui32Base is the base address of the ADC module.
  123. //! \param ui32SequenceNum is the sample sequence number.
  124. //! \param pfnHandler is a pointer to the function to be called when the
  125. //! ADC sample sequence interrupt occurs.
  126. //!
  127. //! This function sets the handler to be called when a sample sequence
  128. //! interrupt occurs. This function enables the global interrupt in the
  129. //! interrupt controller; the sequence interrupt must be enabled with
  130. //! ADCIntEnable(). It is the interrupt handler's responsibility to clear the
  131. //! interrupt source via ADCIntClear().
  132. //!
  133. //! \sa IntRegister() for important information about registering interrupt
  134. //! handlers.
  135. //!
  136. //! \return None.
  137. //
  138. //*****************************************************************************
  139. void
  140. ADCIntRegister(uint32_t ui32Base, uint32_t ui32SequenceNum,
  141. void (*pfnHandler)(void))
  142. {
  143. uint_fast8_t ui8Int;
  144. //
  145. // Check the arguments.
  146. //
  147. ASSERT((ui32Base == ADC0_BASE) || (ui32Base == ADC1_BASE));
  148. ASSERT(ui32SequenceNum < 4);
  149. //
  150. // Determine the interrupt to register based on the sequence number.
  151. //
  152. ui8Int = _ADCIntNumberGet(ui32Base, ui32SequenceNum);
  153. ASSERT(ui8Int != 0);
  154. //
  155. // Register the interrupt handler.
  156. //
  157. IntRegister(ui8Int, pfnHandler);
  158. //
  159. // Enable the timer interrupt.
  160. //
  161. IntEnable(ui8Int);
  162. }
  163. //*****************************************************************************
  164. //
  165. //! Unregisters the interrupt handler for an ADC interrupt.
  166. //!
  167. //! \param ui32Base is the base address of the ADC module.
  168. //! \param ui32SequenceNum is the sample sequence number.
  169. //!
  170. //! This function unregisters the interrupt handler. This function disables
  171. //! the global interrupt in the interrupt controller; the sequence interrupt
  172. //! must be disabled via ADCIntDisable().
  173. //!
  174. //! \sa IntRegister() for important information about registering interrupt
  175. //! handlers.
  176. //!
  177. //! \return None.
  178. //
  179. //*****************************************************************************
  180. void
  181. ADCIntUnregister(uint32_t ui32Base, uint32_t ui32SequenceNum)
  182. {
  183. uint_fast8_t ui8Int;
  184. //
  185. // Check the arguments.
  186. //
  187. ASSERT((ui32Base == ADC0_BASE) || (ui32Base == ADC1_BASE));
  188. ASSERT(ui32SequenceNum < 4);
  189. //
  190. // Determine the interrupt to unregister based on the sequence number.
  191. //
  192. ui8Int = _ADCIntNumberGet(ui32Base, ui32SequenceNum);
  193. ASSERT(ui8Int != 0);
  194. //
  195. // Disable the interrupt.
  196. //
  197. IntDisable(ui8Int);
  198. //
  199. // Unregister the interrupt handler.
  200. //
  201. IntUnregister(ui8Int);
  202. }
  203. //*****************************************************************************
  204. //
  205. //! Disables a sample sequence interrupt.
  206. //!
  207. //! \param ui32Base is the base address of the ADC module.
  208. //! \param ui32SequenceNum is the sample sequence number.
  209. //!
  210. //! This function disables the requested sample sequence interrupt.
  211. //!
  212. //! \return None.
  213. //
  214. //*****************************************************************************
  215. void
  216. ADCIntDisable(uint32_t ui32Base, uint32_t ui32SequenceNum)
  217. {
  218. //
  219. // Check the arguments.
  220. //
  221. ASSERT((ui32Base == ADC0_BASE) || (ui32Base == ADC1_BASE));
  222. ASSERT(ui32SequenceNum < 4);
  223. //
  224. // Disable this sample sequence interrupt.
  225. //
  226. HWREG(ui32Base + ADC_O_IM) &= ~(1 << ui32SequenceNum);
  227. }
  228. //*****************************************************************************
  229. //
  230. //! Enables a sample sequence interrupt.
  231. //!
  232. //! \param ui32Base is the base address of the ADC module.
  233. //! \param ui32SequenceNum is the sample sequence number.
  234. //!
  235. //! This function enables the requested sample sequence interrupt. Any
  236. //! outstanding interrupts are cleared before enabling the sample sequence
  237. //! interrupt.
  238. //!
  239. //! \return None.
  240. //
  241. //*****************************************************************************
  242. void
  243. ADCIntEnable(uint32_t ui32Base, uint32_t ui32SequenceNum)
  244. {
  245. //
  246. // Check the arguments.
  247. //
  248. ASSERT((ui32Base == ADC0_BASE) || (ui32Base == ADC1_BASE));
  249. ASSERT(ui32SequenceNum < 4);
  250. //
  251. // Clear any outstanding interrupts on this sample sequence.
  252. //
  253. HWREG(ui32Base + ADC_O_ISC) = 1 << ui32SequenceNum;
  254. //
  255. // Enable this sample sequence interrupt.
  256. //
  257. HWREG(ui32Base + ADC_O_IM) |= 1 << ui32SequenceNum;
  258. }
  259. //*****************************************************************************
  260. //
  261. //! Gets the current interrupt status.
  262. //!
  263. //! \param ui32Base is the base address of the ADC module.
  264. //! \param ui32SequenceNum is the sample sequence number.
  265. //! \param bMasked is false if the raw interrupt status is required and true if
  266. //! the masked interrupt status is required.
  267. //!
  268. //! This function returns the interrupt status for the specified sample
  269. //! sequence. Either the raw interrupt status or the status of interrupts that
  270. //! are allowed to reflect to the processor can be returned.
  271. //!
  272. //! \return The current raw or masked interrupt status.
  273. //
  274. //*****************************************************************************
  275. uint32_t
  276. ADCIntStatus(uint32_t ui32Base, uint32_t ui32SequenceNum, bool bMasked)
  277. {
  278. uint32_t ui32Temp;
  279. //
  280. // Check the arguments.
  281. //
  282. ASSERT((ui32Base == ADC0_BASE) || (ui32Base == ADC1_BASE));
  283. ASSERT(ui32SequenceNum < 4);
  284. //
  285. // Return either the interrupt status or the raw interrupt status as
  286. // requested.
  287. //
  288. if(bMasked)
  289. {
  290. ui32Temp = HWREG(ui32Base + ADC_O_ISC) & (0x10001 << ui32SequenceNum);
  291. }
  292. else
  293. {
  294. ui32Temp = (HWREG(ui32Base + ADC_O_RIS) &
  295. (0x10000 | (1 << ui32SequenceNum)));
  296. //
  297. // If the digital comparator status bit is set, reflect it to the
  298. // appropriate sequence bit.
  299. //
  300. if(ui32Temp & 0x10000)
  301. {
  302. ui32Temp |= 0xF0000;
  303. ui32Temp &= ~(0x10000 << ui32SequenceNum);
  304. }
  305. }
  306. //
  307. // Return the interrupt status
  308. //
  309. return(ui32Temp);
  310. }
  311. //*****************************************************************************
  312. //
  313. //! Clears sample sequence interrupt source.
  314. //!
  315. //! \param ui32Base is the base address of the ADC module.
  316. //! \param ui32SequenceNum is the sample sequence number.
  317. //!
  318. //! The specified sample sequence interrupt is cleared, so that it no longer
  319. //! asserts. This function must be called in the interrupt handler to keep
  320. //! the interrupt from being triggered again immediately upon exit.
  321. //!
  322. //! \note Because there is a write buffer in the Cortex-M processor, it may
  323. //! take several clock cycles before the interrupt source is actually cleared.
  324. //! Therefore, it is recommended that the interrupt source be cleared early in
  325. //! the interrupt handler (as opposed to the very last action) to avoid
  326. //! returning from the interrupt handler before the interrupt source is
  327. //! actually cleared. Failure to do so may result in the interrupt handler
  328. //! being immediately reentered (because the interrupt controller still sees
  329. //! the interrupt source asserted).
  330. //!
  331. //! \return None.
  332. //
  333. //*****************************************************************************
  334. void
  335. ADCIntClear(uint32_t ui32Base, uint32_t ui32SequenceNum)
  336. {
  337. //
  338. // Check the arguments.
  339. //
  340. ASSERT((ui32Base == ADC0_BASE) || (ui32Base == ADC1_BASE));
  341. ASSERT(ui32SequenceNum < 4);
  342. //
  343. // Clear the interrupt.
  344. //
  345. HWREG(ui32Base + ADC_O_ISC) = 1 << ui32SequenceNum;
  346. }
  347. //*****************************************************************************
  348. //
  349. //! Enables a sample sequence.
  350. //!
  351. //! \param ui32Base is the base address of the ADC module.
  352. //! \param ui32SequenceNum is the sample sequence number.
  353. //!
  354. //! Allows the specified sample sequence to be captured when its trigger is
  355. //! detected. A sample sequence must be configured before it is enabled.
  356. //!
  357. //! \return None.
  358. //
  359. //*****************************************************************************
  360. void
  361. ADCSequenceEnable(uint32_t ui32Base, uint32_t ui32SequenceNum)
  362. {
  363. //
  364. // Check the arguments.
  365. //
  366. ASSERT((ui32Base == ADC0_BASE) || (ui32Base == ADC1_BASE));
  367. ASSERT(ui32SequenceNum < 4);
  368. //
  369. // Enable the specified sequence.
  370. //
  371. HWREG(ui32Base + ADC_O_ACTSS) |= 1 << ui32SequenceNum;
  372. }
  373. //*****************************************************************************
  374. //
  375. //! Disables a sample sequence.
  376. //!
  377. //! \param ui32Base is the base address of the ADC module.
  378. //! \param ui32SequenceNum is the sample sequence number.
  379. //!
  380. //! Prevents the specified sample sequence from being captured when its trigger
  381. //! is detected. A sample sequence must be disabled before it is configured.
  382. //!
  383. //! \return None.
  384. //
  385. //*****************************************************************************
  386. void
  387. ADCSequenceDisable(uint32_t ui32Base, uint32_t ui32SequenceNum)
  388. {
  389. //
  390. // Check the arguments.
  391. //
  392. ASSERT((ui32Base == ADC0_BASE) || (ui32Base == ADC1_BASE));
  393. ASSERT(ui32SequenceNum < 4);
  394. //
  395. // Disable the specified sequences.
  396. //
  397. HWREG(ui32Base + ADC_O_ACTSS) &= ~(1 << ui32SequenceNum);
  398. }
  399. //*****************************************************************************
  400. //
  401. //! Configures the trigger source and priority of a sample sequence.
  402. //!
  403. //! \param ui32Base is the base address of the ADC module.
  404. //! \param ui32SequenceNum is the sample sequence number.
  405. //! \param ui32Trigger is the trigger source that initiates the sample
  406. //! sequence; must be one of the \b ADC_TRIGGER_* values.
  407. //! \param ui32Priority is the relative priority of the sample sequence with
  408. //! respect to the other sample sequences.
  409. //!
  410. //! This function configures the initiation criteria for a sample sequence.
  411. //! Valid sample sequencers range from zero to three; sequencer zero captures
  412. //! up to eight samples, sequencers one and two capture up to four samples,
  413. //! and sequencer three captures a single sample. The trigger condition and
  414. //! priority (with respect to other sample sequencer execution) are set.
  415. //!
  416. //! The \e ui32Trigger parameter can take on the following values:
  417. //!
  418. //! - \b ADC_TRIGGER_PROCESSOR - A trigger generated by the processor, via the
  419. //! ADCProcessorTrigger() function.
  420. //! - \b ADC_TRIGGER_COMP0 - A trigger generated by the first analog
  421. //! comparator; configured with ComparatorConfigure().
  422. //! - \b ADC_TRIGGER_COMP1 - A trigger generated by the second analog
  423. //! comparator; configured with ComparatorConfigure().
  424. //! - \b ADC_TRIGGER_COMP2 - A trigger generated by the third analog
  425. //! comparator; configured with ComparatorConfigure().
  426. //! - \b ADC_TRIGGER_EXTERNAL - A trigger generated by an input from the Port
  427. //! B4 pin. Note that some microcontrollers can
  428. //! select from any GPIO using the
  429. //! GPIOADCTriggerEnable() function.
  430. //! - \b ADC_TRIGGER_TIMER - A trigger generated by a timer; configured with
  431. //! TimerControlTrigger().
  432. //! - \b ADC_TRIGGER_PWM0 - A trigger generated by the first PWM generator;
  433. //! configured with PWMGenIntTrigEnable().
  434. //! - \b ADC_TRIGGER_PWM1 - A trigger generated by the second PWM generator;
  435. //! configured with PWMGenIntTrigEnable().
  436. //! - \b ADC_TRIGGER_PWM2 - A trigger generated by the third PWM generator;
  437. //! configured with PWMGenIntTrigEnable().
  438. //! - \b ADC_TRIGGER_PWM3 - A trigger generated by the fourth PWM generator;
  439. //! configured with PWMGenIntTrigEnable().
  440. //! - \b ADC_TRIGGER_ALWAYS - A trigger that is always asserted, causing the
  441. //! sample sequence to capture repeatedly (so long as
  442. //! there is not a higher priority source active).
  443. //!
  444. //! When \b ADC_TRIGGER_PWM0, \b ADC_TRIGGER_PWM1, \b ADC_TRIGGER_PWM2 or
  445. //! \b ADC_TRIGGER_PWM3 is specified, one of the following should be ORed into
  446. //! \e ui32Trigger to select the PWM module from which the triggers will be
  447. //! routed for this sequence:
  448. //!
  449. //! - \b ADC_TRIGGER_PWM_MOD0 - Selects PWM module 0 as the source of the
  450. //! PWM0 to PWM3 triggers for this sequence.
  451. //! - \b ADC_TRIGGER_PWM_MOD1 - Selects PWM module 1 as the source of the
  452. //! PWM0 to PWM3 triggers for this sequence.
  453. //!
  454. //! Note that not all trigger sources are available on all Tiva family
  455. //! members; consult the data sheet for the device in question to determine the
  456. //! availability of triggers.
  457. //!
  458. //! The \e ui32Priority parameter is a value between 0 and 3, where 0
  459. //! represents the highest priority and 3 the lowest. Note that when
  460. //! programming the priority among a set of sample sequences, each must have
  461. //! unique priority; it is up to the caller to guarantee the uniqueness of the
  462. //! priorities.
  463. //!
  464. //! \return None.
  465. //
  466. //*****************************************************************************
  467. void
  468. ADCSequenceConfigure(uint32_t ui32Base, uint32_t ui32SequenceNum,
  469. uint32_t ui32Trigger, uint32_t ui32Priority)
  470. {
  471. uint32_t ui32Gen;
  472. //
  473. // Check the arugments.
  474. //
  475. ASSERT((ui32Base == ADC0_BASE) || (ui32Base == ADC1_BASE));
  476. ASSERT(ui32SequenceNum < 4);
  477. ASSERT(((ui32Trigger & 0xF) == ADC_TRIGGER_PROCESSOR) ||
  478. ((ui32Trigger & 0xF) == ADC_TRIGGER_COMP0) ||
  479. ((ui32Trigger & 0xF) == ADC_TRIGGER_COMP1) ||
  480. ((ui32Trigger & 0xF) == ADC_TRIGGER_COMP2) ||
  481. ((ui32Trigger & 0xF) == ADC_TRIGGER_EXTERNAL) ||
  482. ((ui32Trigger & 0xF) == ADC_TRIGGER_TIMER) ||
  483. ((ui32Trigger & 0xF) == ADC_TRIGGER_PWM0) ||
  484. ((ui32Trigger & 0xF) == ADC_TRIGGER_PWM1) ||
  485. ((ui32Trigger & 0xF) == ADC_TRIGGER_PWM2) ||
  486. ((ui32Trigger & 0xF) == ADC_TRIGGER_PWM3) ||
  487. ((ui32Trigger & 0xF) == ADC_TRIGGER_ALWAYS) ||
  488. ((ui32Trigger & 0x30) == ADC_TRIGGER_PWM_MOD0) ||
  489. ((ui32Trigger & 0x30) == ADC_TRIGGER_PWM_MOD1));
  490. ASSERT(ui32Priority < 4);
  491. //
  492. // Compute the shift for the bits that control this sample sequence.
  493. //
  494. ui32SequenceNum *= 4;
  495. //
  496. // Set the trigger event for this sample sequence.
  497. //
  498. HWREG(ui32Base + ADC_O_EMUX) = ((HWREG(ui32Base + ADC_O_EMUX) &
  499. ~(0xf << ui32SequenceNum)) |
  500. ((ui32Trigger & 0xf) << ui32SequenceNum));
  501. //
  502. // Set the priority for this sample sequence.
  503. //
  504. HWREG(ui32Base + ADC_O_SSPRI) = ((HWREG(ui32Base + ADC_O_SSPRI) &
  505. ~(0xf << ui32SequenceNum)) |
  506. ((ui32Priority & 0x3) <<
  507. ui32SequenceNum));
  508. //
  509. // Set the source PWM module for this sequence's PWM triggers.
  510. //
  511. ui32Gen = ui32Trigger & 0x0f;
  512. if(ui32Gen >= ADC_TRIGGER_PWM0 && ui32Gen <= ADC_TRIGGER_PWM3)
  513. {
  514. //
  515. // Set the shift for the module and generator
  516. //
  517. ui32Gen = (ui32Gen - ADC_TRIGGER_PWM0) * 8;
  518. HWREG(ui32Base + ADC_O_TSSEL) = ((HWREG(ui32Base + ADC_O_TSSEL) &
  519. ~(0x30 << ui32Gen)) |
  520. ((ui32Trigger & 0x30) << ui32Gen));
  521. }
  522. }
  523. //*****************************************************************************
  524. //
  525. //! Configure a step of the sample sequencer.
  526. //!
  527. //! \param ui32Base is the base address of the ADC module.
  528. //! \param ui32SequenceNum is the sample sequence number.
  529. //! \param ui32Step is the step to be configured.
  530. //! \param ui32Config is the configuration of this step; is a logical OR
  531. //! of \b ADC_CTL_TS, \b ADC_CTL_IE, \b ADC_CTL_END, \b ADC_CTL_D, one of the
  532. //! input channel selects (\b ADC_CTL_CH0 through \b ADC_CTL_CH23), and one of
  533. //! the digital comparator selects (\b ADC_CTL_CMP0 through \b ADC_CTL_CMP7).
  534. //! On some parts the sample and hold time can be increased by including a
  535. //! logical OR of one of \b ADC_CTL_SHOLD_4, \b ADC_CTL_SHOLD_8,
  536. //! \b ADC_CTL_SHOLD_16, \b ADC_CTL_SHOLD_32, \b ADC_CTL_SHOLD_64,
  537. //! \b ADC_CTL_SHOLD_128 or \b ADC_CTL_SHOLD_256. The default sample time is 4
  538. //! ADC clocks.
  539. //!
  540. //! This function configures the ADC for one step of a sample sequence. The
  541. //! ADC can be configured for single-ended or differential operation (the
  542. //! \b ADC_CTL_D bit selects differential operation when set), the channel to
  543. //! be sampled can be chosen (the \b ADC_CTL_CH0 through \b ADC_CTL_CH23
  544. //! values), and the internal temperature sensor can be selected (the
  545. //! \b ADC_CTL_TS bit). Additionally, this step can be defined as the last in
  546. //! the sequence (the \b ADC_CTL_END bit) and it can be configured to cause an
  547. //! interrupt when the step is complete (the \b ADC_CTL_IE bit). If the
  548. //! digital comparators are present on the device, this step may also be
  549. //! configured to send the ADC sample to the selected comparator using
  550. //! \b ADC_CTL_CMP0 through \b ADC_CTL_CMP7. The configuration is used by the
  551. //! ADC at the appropriate time when the trigger for this sequence occurs.
  552. //!
  553. //! \note If the Digital Comparator is present and enabled using the
  554. //! \b ADC_CTL_CMP0 through \b ADC_CTL_CMP7 selects, the ADC sample is NOT
  555. //! written into the ADC sequence data FIFO.
  556. //!
  557. //! The \e ui32Step parameter determines the order in which the samples are
  558. //! captured by the ADC when the trigger occurs. It can range from zero to
  559. //! seven for the first sample sequencer, from zero to three for the second and
  560. //! third sample sequencer, and can only be zero for the fourth sample
  561. //! sequencer.
  562. //!
  563. //! Differential mode only works with adjacent channel pairs (for example, 0
  564. //! and 1). The channel select must be the number of the channel pair to
  565. //! sample (for example, \b ADC_CTL_CH0 for 0 and 1, or \b ADC_CTL_CH1 for 2
  566. //! and 3) or undefined results are returned by the ADC. Additionally, if
  567. //! differential mode is selected when the temperature sensor is being sampled,
  568. //! undefined results are returned by the ADC.
  569. //!
  570. //! It is the responsibility of the caller to ensure that a valid configuration
  571. //! is specified; this function does not check the validity of the specified
  572. //! configuration.
  573. //!
  574. //! \return None.
  575. //
  576. //*****************************************************************************
  577. void
  578. ADCSequenceStepConfigure(uint32_t ui32Base, uint32_t ui32SequenceNum,
  579. uint32_t ui32Step, uint32_t ui32Config)
  580. {
  581. uint32_t ui32Temp;
  582. //
  583. // Check the arguments.
  584. //
  585. ASSERT((ui32Base == ADC0_BASE) || (ui32Base == ADC1_BASE));
  586. ASSERT(ui32SequenceNum < 4);
  587. ASSERT(((ui32SequenceNum == 0) && (ui32Step < 8)) ||
  588. ((ui32SequenceNum == 1) && (ui32Step < 4)) ||
  589. ((ui32SequenceNum == 2) && (ui32Step < 4)) ||
  590. ((ui32SequenceNum == 3) && (ui32Step < 1)));
  591. //
  592. // Get the offset of the sequence to be configured.
  593. //
  594. ui32Base += ADC_SEQ + (ADC_SEQ_STEP * ui32SequenceNum);
  595. //
  596. // Compute the shift for the bits that control this step.
  597. //
  598. ui32Step *= 4;
  599. //
  600. // Set the analog mux value for this step.
  601. //
  602. HWREG(ui32Base + ADC_SSMUX) = ((HWREG(ui32Base + ADC_SSMUX) &
  603. ~(0x0000000f << ui32Step)) |
  604. ((ui32Config & 0x0f) << ui32Step));
  605. //
  606. // Set the upper bits of the analog mux value for this step.
  607. //
  608. HWREG(ui32Base + ADC_SSEMUX) = ((HWREG(ui32Base + ADC_SSEMUX) &
  609. ~(0x0000000f << ui32Step)) |
  610. (((ui32Config & 0xf00) >> 8) << ui32Step));
  611. //
  612. // Set the control value for this step.
  613. //
  614. HWREG(ui32Base + ADC_SSCTL) = ((HWREG(ui32Base + ADC_SSCTL) &
  615. ~(0x0000000f << ui32Step)) |
  616. (((ui32Config & 0xf0) >> 4) << ui32Step));
  617. //
  618. // Set the sample and hold time for this step. This is not available on
  619. // all devices, however on devices that do not support this feature these
  620. // reserved bits are ignored on write access.
  621. //
  622. HWREG(ui32Base + ADC_SSTSH) = ((HWREG(ui32Base + ADC_SSTSH) &
  623. ~(0x0000000f << ui32Step)) |
  624. (((ui32Config & 0xf00000) >> 20) << ui32Step));
  625. //
  626. // Enable digital comparator if specified in the ui32Config bit-fields.
  627. //
  628. if(ui32Config & 0x000F0000)
  629. {
  630. //
  631. // Program the comparator for the specified step.
  632. //
  633. ui32Temp = HWREG(ui32Base + ADC_SSDC);
  634. ui32Temp &= ~(0xF << ui32Step);
  635. ui32Temp |= (((ui32Config & 0x00070000) >> 16) << ui32Step);
  636. HWREG(ui32Base + ADC_SSDC) = ui32Temp;
  637. //
  638. // Enable the comparator.
  639. //
  640. HWREG(ui32Base + ADC_SSOP) |= (1 << ui32Step);
  641. }
  642. //
  643. // Disable digital comparator if not specified.
  644. //
  645. else
  646. {
  647. HWREG(ui32Base + ADC_SSOP) &= ~(1 << ui32Step);
  648. }
  649. }
  650. //*****************************************************************************
  651. //
  652. //! Determines if a sample sequence overflow occurred.
  653. //!
  654. //! \param ui32Base is the base address of the ADC module.
  655. //! \param ui32SequenceNum is the sample sequence number.
  656. //!
  657. //! This function determines if a sample sequence overflow has occurred.
  658. //! Overflow happens if the captured samples are not read from the FIFO before
  659. //! the next trigger occurs.
  660. //!
  661. //! \return Returns zero if there was not an overflow, and non-zero if there
  662. //! was.
  663. //
  664. //*****************************************************************************
  665. int32_t
  666. ADCSequenceOverflow(uint32_t ui32Base, uint32_t ui32SequenceNum)
  667. {
  668. //
  669. // Check the arguments.
  670. //
  671. ASSERT((ui32Base == ADC0_BASE) || (ui32Base == ADC1_BASE));
  672. ASSERT(ui32SequenceNum < 4);
  673. //
  674. // Determine if there was an overflow on this sequence.
  675. //
  676. return(HWREG(ui32Base + ADC_O_OSTAT) & (1 << ui32SequenceNum));
  677. }
  678. //*****************************************************************************
  679. //
  680. //! Clears the overflow condition on a sample sequence.
  681. //!
  682. //! \param ui32Base is the base address of the ADC module.
  683. //! \param ui32SequenceNum is the sample sequence number.
  684. //!
  685. //! This function clears an overflow condition on one of the sample sequences.
  686. //! The overflow condition must be cleared in order to detect a subsequent
  687. //! overflow condition (it otherwise causes no harm).
  688. //!
  689. //! \return None.
  690. //
  691. //*****************************************************************************
  692. void
  693. ADCSequenceOverflowClear(uint32_t ui32Base, uint32_t ui32SequenceNum)
  694. {
  695. //
  696. // Check the arguments.
  697. //
  698. ASSERT((ui32Base == ADC0_BASE) || (ui32Base == ADC1_BASE));
  699. ASSERT(ui32SequenceNum < 4);
  700. //
  701. // Clear the overflow condition for this sequence.
  702. //
  703. HWREG(ui32Base + ADC_O_OSTAT) = 1 << ui32SequenceNum;
  704. }
  705. //*****************************************************************************
  706. //
  707. //! Determines if a sample sequence underflow occurred.
  708. //!
  709. //! \param ui32Base is the base address of the ADC module.
  710. //! \param ui32SequenceNum is the sample sequence number.
  711. //!
  712. //! This function determines if a sample sequence underflow has occurred.
  713. //! Underflow happens if too many samples are read from the FIFO.
  714. //!
  715. //! \return Returns zero if there was not an underflow, and non-zero if there
  716. //! was.
  717. //
  718. //*****************************************************************************
  719. int32_t
  720. ADCSequenceUnderflow(uint32_t ui32Base, uint32_t ui32SequenceNum)
  721. {
  722. //
  723. // Check the arguments.
  724. //
  725. ASSERT((ui32Base == ADC0_BASE) || (ui32Base == ADC1_BASE));
  726. ASSERT(ui32SequenceNum < 4);
  727. //
  728. // Determine if there was an underflow on this sequence.
  729. //
  730. return(HWREG(ui32Base + ADC_O_USTAT) & (1 << ui32SequenceNum));
  731. }
  732. //*****************************************************************************
  733. //
  734. //! Clears the underflow condition on a sample sequence.
  735. //!
  736. //! \param ui32Base is the base address of the ADC module.
  737. //! \param ui32SequenceNum is the sample sequence number.
  738. //!
  739. //! This function clears an underflow condition on one of the sample
  740. //! sequencers. The underflow condition must be cleared in order to detect a
  741. //! subsequent underflow condition (it otherwise causes no harm).
  742. //!
  743. //! \return None.
  744. //
  745. //*****************************************************************************
  746. void
  747. ADCSequenceUnderflowClear(uint32_t ui32Base, uint32_t ui32SequenceNum)
  748. {
  749. //
  750. // Check the arguments.
  751. //
  752. ASSERT((ui32Base == ADC0_BASE) || (ui32Base == ADC1_BASE));
  753. ASSERT(ui32SequenceNum < 4);
  754. //
  755. // Clear the underflow condition for this sequence.
  756. //
  757. HWREG(ui32Base + ADC_O_USTAT) = 1 << ui32SequenceNum;
  758. }
  759. //*****************************************************************************
  760. //
  761. //! Gets the captured data for a sample sequence.
  762. //!
  763. //! \param ui32Base is the base address of the ADC module.
  764. //! \param ui32SequenceNum is the sample sequence number.
  765. //! \param pui32Buffer is the address where the data is stored.
  766. //!
  767. //! This function copies data from the specified sample sequencer output FIFO
  768. //! to a memory resident buffer. The number of samples available in the
  769. //! hardware FIFO are copied into the buffer, which is assumed to be large
  770. //! enough to hold that many samples. This function only returns the samples
  771. //! that are presently available, which may not be the entire sample sequence
  772. //! if it is in the process of being executed.
  773. //!
  774. //! \return Returns the number of samples copied to the buffer.
  775. //
  776. //*****************************************************************************
  777. int32_t
  778. ADCSequenceDataGet(uint32_t ui32Base, uint32_t ui32SequenceNum,
  779. uint32_t *pui32Buffer)
  780. {
  781. uint32_t ui32Count;
  782. //
  783. // Check the arguments.
  784. //
  785. ASSERT((ui32Base == ADC0_BASE) || (ui32Base == ADC1_BASE));
  786. ASSERT(ui32SequenceNum < 4);
  787. //
  788. // Get the offset of the sequence to be read.
  789. //
  790. ui32Base += ADC_SEQ + (ADC_SEQ_STEP * ui32SequenceNum);
  791. //
  792. // Read samples from the FIFO until it is empty.
  793. //
  794. ui32Count = 0;
  795. while(!(HWREG(ui32Base + ADC_SSFSTAT) & ADC_SSFSTAT0_EMPTY) &&
  796. (ui32Count < 8))
  797. {
  798. //
  799. // Read the FIFO and copy it to the destination.
  800. //
  801. *pui32Buffer++ = HWREG(ui32Base + ADC_SSFIFO);
  802. //
  803. // Increment the count of samples read.
  804. //
  805. ui32Count++;
  806. }
  807. //
  808. // Return the number of samples read.
  809. //
  810. return(ui32Count);
  811. }
  812. //*****************************************************************************
  813. //
  814. //! Causes a processor trigger for a sample sequence.
  815. //!
  816. //! \param ui32Base is the base address of the ADC module.
  817. //! \param ui32SequenceNum is the sample sequence number, with
  818. //! \b ADC_TRIGGER_WAIT or \b ADC_TRIGGER_SIGNAL optionally ORed into it.
  819. //!
  820. //! This function triggers a processor-initiated sample sequence if the sample
  821. //! sequence trigger is configured to \b ADC_TRIGGER_PROCESSOR. If
  822. //! \b ADC_TRIGGER_WAIT is ORed into the sequence number, the
  823. //! processor-initiated trigger is delayed until a later processor-initiated
  824. //! trigger to a different ADC module that specifies \b ADC_TRIGGER_SIGNAL,
  825. //! allowing multiple ADCs to start from a processor-initiated trigger in a
  826. //! synchronous manner.
  827. //!
  828. //! \return None.
  829. //
  830. //*****************************************************************************
  831. void
  832. ADCProcessorTrigger(uint32_t ui32Base, uint32_t ui32SequenceNum)
  833. {
  834. //
  835. // Check the arguments.
  836. //
  837. ASSERT((ui32Base == ADC0_BASE) || (ui32Base == ADC1_BASE));
  838. ASSERT(ui32SequenceNum < 4);
  839. //
  840. // Generate a processor trigger for this sample sequence.
  841. //
  842. HWREG(ui32Base + ADC_O_PSSI) |= ((ui32SequenceNum & 0xffff0000) |
  843. (1 << (ui32SequenceNum & 0xf)));
  844. }
  845. //*****************************************************************************
  846. //
  847. //! Configures the software oversampling factor of the ADC.
  848. //!
  849. //! \param ui32Base is the base address of the ADC module.
  850. //! \param ui32SequenceNum is the sample sequence number.
  851. //! \param ui32Factor is the number of samples to be averaged.
  852. //!
  853. //! This function configures the software oversampling for the ADC, which can
  854. //! be used to provide better resolution on the sampled data. Oversampling is
  855. //! accomplished by averaging multiple samples from the same analog input.
  856. //! Three different oversampling rates are supported; 2x, 4x, and 8x.
  857. //!
  858. //! Oversampling is only supported on the sample sequencers that are more than
  859. //! one sample in depth (that is, the fourth sample sequencer is not
  860. //! supported). Oversampling by 2x (for example) divides the depth of the
  861. //! sample sequencer by two; so 2x oversampling on the first sample sequencer
  862. //! can only provide four samples per trigger. This also means that 8x
  863. //! oversampling is only available on the first sample sequencer.
  864. //!
  865. //! \return None.
  866. //
  867. //*****************************************************************************
  868. void
  869. ADCSoftwareOversampleConfigure(uint32_t ui32Base, uint32_t ui32SequenceNum,
  870. uint32_t ui32Factor)
  871. {
  872. uint32_t ui32Value;
  873. uint32_t ui32ADCInst;
  874. //
  875. // Check the arguments.
  876. //
  877. ASSERT((ui32Base == ADC0_BASE) || (ui32Base == ADC1_BASE));
  878. ASSERT(ui32SequenceNum < 3);
  879. ASSERT(((ui32Factor == 2) || (ui32Factor == 4) || (ui32Factor == 8)) &&
  880. ((ui32SequenceNum == 0) || (ui32Factor != 8)));
  881. //
  882. // Convert the oversampling factor to a shift factor.
  883. //
  884. for(ui32Value = 0, ui32Factor >>= 1; ui32Factor;
  885. ui32Value++, ui32Factor >>= 1)
  886. {
  887. }
  888. //
  889. // Evaluate the ADC Instance.
  890. //
  891. if(ui32Base == ADC0_BASE)
  892. {
  893. ui32ADCInst = 0;
  894. }
  895. else
  896. {
  897. ui32ADCInst = 1;
  898. }
  899. //
  900. // Save the shift factor.
  901. //
  902. g_pui8OversampleFactor[ui32ADCInst][ui32SequenceNum] = ui32Value;
  903. }
  904. //*****************************************************************************
  905. //
  906. //! Configures a step of the software oversampled sequencer.
  907. //!
  908. //! \param ui32Base is the base address of the ADC module.
  909. //! \param ui32SequenceNum is the sample sequence number.
  910. //! \param ui32Step is the step to be configured.
  911. //! \param ui32Config is the configuration of this step.
  912. //!
  913. //! This function configures a step of the sample sequencer when using the
  914. //! software oversampling feature. The number of steps available depends on
  915. //! the oversampling factor set by ADCSoftwareOversampleConfigure(). The value
  916. //! of \e ui32Config is the same as defined for ADCSequenceStepConfigure().
  917. //!
  918. //! \return None.
  919. //
  920. //*****************************************************************************
  921. void
  922. ADCSoftwareOversampleStepConfigure(uint32_t ui32Base, uint32_t ui32SequenceNum,
  923. uint32_t ui32Step, uint32_t ui32Config)
  924. {
  925. uint32_t ui32ADCInst;
  926. //
  927. // Evaluate the ADC Instance.
  928. //
  929. if(ui32Base == ADC0_BASE)
  930. {
  931. ui32ADCInst = 0;
  932. }
  933. else
  934. {
  935. ui32ADCInst = 1;
  936. }
  937. //
  938. // Check the arguments.
  939. //
  940. ASSERT((ui32Base == ADC0_BASE) || (ui32Base == ADC1_BASE));
  941. ASSERT(ui32SequenceNum < 3);
  942. ASSERT(((ui32SequenceNum == 0) &&
  943. (ui32Step <
  944. (8 >> g_pui8OversampleFactor[ui32ADCInst][ui32SequenceNum]))) ||
  945. (ui32Step <
  946. (4 >> g_pui8OversampleFactor[ui32ADCInst][ui32SequenceNum])));
  947. //
  948. // Get the offset of the sequence to be configured.
  949. //
  950. ui32Base += ADC_SEQ + (ADC_SEQ_STEP * ui32SequenceNum);
  951. //
  952. // Compute the shift for the bits that control this step.
  953. //
  954. ui32Step *= 4 << g_pui8OversampleFactor[ui32ADCInst][ui32SequenceNum];
  955. //
  956. // Loop through the hardware steps that make up this step of the software
  957. // oversampled sequence.
  958. //
  959. for(ui32SequenceNum =
  960. (1 << g_pui8OversampleFactor[ui32ADCInst][ui32SequenceNum]);
  961. ui32SequenceNum; ui32SequenceNum--)
  962. {
  963. //
  964. // Set the analog mux value for this step.
  965. //
  966. HWREG(ui32Base + ADC_SSMUX) = ((HWREG(ui32Base + ADC_SSMUX) &
  967. ~(0x0000000f << ui32Step)) |
  968. ((ui32Config & 0x0f) << ui32Step));
  969. //
  970. // Set the upper bits of the analog mux value for this step.
  971. //
  972. HWREG(ui32Base + ADC_SSEMUX) = ((HWREG(ui32Base + ADC_SSEMUX) &
  973. ~(0x0000000f << ui32Step)) |
  974. (((ui32Config & 0xf00) >> 8) <<
  975. ui32Step));
  976. //
  977. // Set the control value for this step.
  978. //
  979. HWREG(ui32Base + ADC_SSCTL) = ((HWREG(ui32Base + ADC_SSCTL) &
  980. ~(0x0000000f << ui32Step)) |
  981. (((ui32Config & 0xf0) >> 4) <<
  982. ui32Step));
  983. if(ui32SequenceNum != 1)
  984. {
  985. HWREG(ui32Base + ADC_SSCTL) &= ~((ADC_SSCTL0_IE0 |
  986. ADC_SSCTL0_END0) << ui32Step);
  987. }
  988. //
  989. // Go to the next hardware step.
  990. //
  991. ui32Step += 4;
  992. }
  993. }
  994. //*****************************************************************************
  995. //
  996. //! Gets the captured data for a sample sequence using software oversampling.
  997. //!
  998. //! \param ui32Base is the base address of the ADC module.
  999. //! \param ui32SequenceNum is the sample sequence number.
  1000. //! \param pui32Buffer is the address where the data is stored.
  1001. //! \param ui32Count is the number of samples to be read.
  1002. //!
  1003. //! This function copies data from the specified sample sequence output FIFO to
  1004. //! a memory resident buffer with software oversampling applied. The requested
  1005. //! number of samples are copied into the data buffer; if there are not enough
  1006. //! samples in the hardware FIFO to satisfy this many oversampled data items,
  1007. //! then incorrect results are returned. It is the caller's responsibility to
  1008. //! read only the samples that are available and wait until enough data is
  1009. //! available, for example as a result of receiving an interrupt.
  1010. //!
  1011. //! \return None.
  1012. //
  1013. //*****************************************************************************
  1014. void
  1015. ADCSoftwareOversampleDataGet(uint32_t ui32Base, uint32_t ui32SequenceNum,
  1016. uint32_t *pui32Buffer, uint32_t ui32Count)
  1017. {
  1018. uint32_t ui32Idx, ui32Accum;
  1019. uint32_t ui32ADCInst;
  1020. //
  1021. // Evaluate the ADC Instance.
  1022. //
  1023. if(ui32Base == ADC0_BASE)
  1024. {
  1025. ui32ADCInst = 0;
  1026. }
  1027. else
  1028. {
  1029. ui32ADCInst = 1;
  1030. }
  1031. //
  1032. // Check the arguments.
  1033. //
  1034. ASSERT((ui32Base == ADC0_BASE) || (ui32Base == ADC1_BASE));
  1035. ASSERT(ui32SequenceNum < 3);
  1036. ASSERT(((ui32SequenceNum == 0) &&
  1037. (ui32Count <
  1038. (8 >> g_pui8OversampleFactor[ui32ADCInst][ui32SequenceNum]))) ||
  1039. (ui32Count <
  1040. (4 >> g_pui8OversampleFactor[ui32ADCInst][ui32SequenceNum])));
  1041. //
  1042. // Get the offset of the sequence to be read.
  1043. //
  1044. ui32Base += ADC_SEQ + (ADC_SEQ_STEP * ui32SequenceNum);
  1045. //
  1046. // Read the samples from the FIFO until it is empty.
  1047. //
  1048. while(ui32Count--)
  1049. {
  1050. //
  1051. // Compute the sum of the samples.
  1052. //
  1053. ui32Accum = 0;
  1054. for(ui32Idx = 1 << g_pui8OversampleFactor[ui32ADCInst][ui32SequenceNum];
  1055. ui32Idx; ui32Idx--)
  1056. {
  1057. //
  1058. // Read the FIFO and add it to the accumulator.
  1059. //
  1060. ui32Accum += HWREG(ui32Base + ADC_SSFIFO);
  1061. }
  1062. //
  1063. // Write the averaged sample to the output buffer.
  1064. //
  1065. *pui32Buffer++ =
  1066. ui32Accum >> g_pui8OversampleFactor[ui32ADCInst][ui32SequenceNum];
  1067. }
  1068. }
  1069. //*****************************************************************************
  1070. //
  1071. //! Configures the hardware oversampling factor of the ADC.
  1072. //!
  1073. //! \param ui32Base is the base address of the ADC module.
  1074. //! \param ui32Factor is the number of samples to be averaged.
  1075. //!
  1076. //! This function configures the hardware oversampling for the ADC, which can
  1077. //! be used to provide better resolution on the sampled data. Oversampling is
  1078. //! accomplished by averaging multiple samples from the same analog input. Six
  1079. //! different oversampling rates are supported; 2x, 4x, 8x, 16x, 32x, and 64x.
  1080. //! Specifying an oversampling factor of zero disables hardware
  1081. //! oversampling.
  1082. //!
  1083. //! Hardware oversampling applies uniformly to all sample sequencers. It does
  1084. //! not reduce the depth of the sample sequencers like the software
  1085. //! oversampling APIs; each sample written into the sample sequencer FIFO is a
  1086. //! fully oversampled analog input reading.
  1087. //!
  1088. //! Enabling hardware averaging increases the precision of the ADC at the cost
  1089. //! of throughput. For example, enabling 4x oversampling reduces the
  1090. //! throughput of a 250 k samples/second ADC to 62.5 k samples/second.
  1091. //!
  1092. //! \return None.
  1093. //
  1094. //*****************************************************************************
  1095. void
  1096. ADCHardwareOversampleConfigure(uint32_t ui32Base, uint32_t ui32Factor)
  1097. {
  1098. uint32_t ui32Value;
  1099. //
  1100. // Check the arguments.
  1101. //
  1102. ASSERT((ui32Base == ADC0_BASE) || (ui32Base == ADC1_BASE));
  1103. ASSERT(((ui32Factor == 0) || (ui32Factor == 2) || (ui32Factor == 4) ||
  1104. (ui32Factor == 8) || (ui32Factor == 16) || (ui32Factor == 32) ||
  1105. (ui32Factor == 64)));
  1106. //
  1107. // Convert the oversampling factor to a shift factor.
  1108. //
  1109. for(ui32Value = 0, ui32Factor >>= 1; ui32Factor;
  1110. ui32Value++, ui32Factor >>= 1)
  1111. {
  1112. }
  1113. //
  1114. // Write the shift factor to the ADC to configure the hardware oversampler.
  1115. //
  1116. HWREG(ui32Base + ADC_O_SAC) = ui32Value;
  1117. }
  1118. //*****************************************************************************
  1119. //
  1120. //! Configures an ADC digital comparator.
  1121. //!
  1122. //! \param ui32Base is the base address of the ADC module.
  1123. //! \param ui32Comp is the index of the comparator to configure.
  1124. //! \param ui32Config is the configuration of the comparator.
  1125. //!
  1126. //! This function configures a comparator. The \e ui32Config parameter is
  1127. //! the result of a logical OR operation between the \b ADC_COMP_TRIG_xxx, and
  1128. //! \b ADC_COMP_INT_xxx values.
  1129. //!
  1130. //! The \b ADC_COMP_TRIG_xxx term can take on the following values:
  1131. //!
  1132. //! - \b ADC_COMP_TRIG_NONE to never trigger PWM fault condition.
  1133. //! - \b ADC_COMP_TRIG_LOW_ALWAYS to always trigger PWM fault condition when
  1134. //! ADC output is in the low-band.
  1135. //! - \b ADC_COMP_TRIG_LOW_ONCE to trigger PWM fault condition once when ADC
  1136. //! output transitions into the low-band.
  1137. //! - \b ADC_COMP_TRIG_LOW_HALWAYS to always trigger PWM fault condition when
  1138. //! ADC output is in the low-band only if ADC output has been in the high-band
  1139. //! since the last trigger output.
  1140. //! - \b ADC_COMP_TRIG_LOW_HONCE to trigger PWM fault condition once when ADC
  1141. //! output transitions into low-band only if ADC output has been in the
  1142. //! high-band since the last trigger output.
  1143. //! - \b ADC_COMP_TRIG_MID_ALWAYS to always trigger PWM fault condition when
  1144. //! ADC output is in the mid-band.
  1145. //! - \b ADC_COMP_TRIG_MID_ONCE to trigger PWM fault condition once when ADC
  1146. //! output transitions into the mid-band.
  1147. //! - \b ADC_COMP_TRIG_HIGH_ALWAYS to always trigger PWM fault condition when
  1148. //! ADC output is in the high-band.
  1149. //! - \b ADC_COMP_TRIG_HIGH_ONCE to trigger PWM fault condition once when ADC
  1150. //! output transitions into the high-band.
  1151. //! - \b ADC_COMP_TRIG_HIGH_HALWAYS to always trigger PWM fault condition when
  1152. //! ADC output is in the high-band only if ADC output has been in the low-band
  1153. //! since the last trigger output.
  1154. //! - \b ADC_COMP_TRIG_HIGH_HONCE to trigger PWM fault condition once when ADC
  1155. //! output transitions into high-band only if ADC output has been in the
  1156. //! low-band since the last trigger output.
  1157. //!
  1158. //! The \b ADC_COMP_INT_xxx term can take on the following values:
  1159. //!
  1160. //! - \b ADC_COMP_INT_NONE to never generate ADC interrupt.
  1161. //! - \b ADC_COMP_INT_LOW_ALWAYS to always generate ADC interrupt when ADC
  1162. //! output is in the low-band.
  1163. //! - \b ADC_COMP_INT_LOW_ONCE to generate ADC interrupt once when ADC output
  1164. //! transitions into the low-band.
  1165. //! - \b ADC_COMP_INT_LOW_HALWAYS to always generate ADC interrupt when ADC
  1166. //! output is in the low-band only if ADC output has been in the high-band
  1167. //! since the last trigger output.
  1168. //! - \b ADC_COMP_INT_LOW_HONCE to generate ADC interrupt once when ADC output
  1169. //! transitions into low-band only if ADC output has been in the high-band
  1170. //! since the last trigger output.
  1171. //! - \b ADC_COMP_INT_MID_ALWAYS to always generate ADC interrupt when ADC
  1172. //! output is in the mid-band.
  1173. //! - \b ADC_COMP_INT_MID_ONCE to generate ADC interrupt once when ADC output
  1174. //! transitions into the mid-band.
  1175. //! - \b ADC_COMP_INT_HIGH_ALWAYS to always generate ADC interrupt when ADC
  1176. //! output is in the high-band.
  1177. //! - \b ADC_COMP_INT_HIGH_ONCE to generate ADC interrupt once when ADC output
  1178. //! transitions into the high-band.
  1179. //! - \b ADC_COMP_INT_HIGH_HALWAYS to always generate ADC interrupt when ADC
  1180. //! output is in the high-band only if ADC output has been in the low-band
  1181. //! since the last trigger output.
  1182. //! - \b ADC_COMP_INT_HIGH_HONCE to generate ADC interrupt once when ADC output
  1183. //! transitions into high-band only if ADC output has been in the low-band
  1184. //! since the last trigger output.
  1185. //!
  1186. //! \return None.
  1187. //
  1188. //*****************************************************************************
  1189. void
  1190. ADCComparatorConfigure(uint32_t ui32Base, uint32_t ui32Comp,
  1191. uint32_t ui32Config)
  1192. {
  1193. //
  1194. // Check the arguments.
  1195. //
  1196. ASSERT((ui32Base == ADC0_BASE) || (ui32Base == ADC1_BASE));
  1197. ASSERT(ui32Comp < 8);
  1198. //
  1199. // Save the new setting.
  1200. //
  1201. HWREG(ui32Base + ADC_O_DCCTL0 + (ui32Comp * 4)) = ui32Config;
  1202. }
  1203. //*****************************************************************************
  1204. //
  1205. //! Defines the ADC digital comparator regions.
  1206. //!
  1207. //! \param ui32Base is the base address of the ADC module.
  1208. //! \param ui32Comp is the index of the comparator to configure.
  1209. //! \param ui32LowRef is the reference point for the low/mid band threshold.
  1210. //! \param ui32HighRef is the reference point for the mid/high band threshold.
  1211. //!
  1212. //! The ADC digital comparator operation is based on three ADC value regions:
  1213. //! - \b low-band is defined as any ADC value less than or equal to the
  1214. //! \e ui32LowRef value.
  1215. //! - \b mid-band is defined as any ADC value greater than the \e ui32LowRef
  1216. //! value but less than or equal to the \e ui32HighRef value.
  1217. //! - \b high-band is defined as any ADC value greater than the \e ui32HighRef
  1218. //! value.
  1219. //!
  1220. //! \return None.
  1221. //
  1222. //*****************************************************************************
  1223. void
  1224. ADCComparatorRegionSet(uint32_t ui32Base, uint32_t ui32Comp,
  1225. uint32_t ui32LowRef, uint32_t ui32HighRef)
  1226. {
  1227. //
  1228. // Check the arguments.
  1229. //
  1230. ASSERT((ui32Base == ADC0_BASE) || (ui32Base == ADC1_BASE));
  1231. ASSERT(ui32Comp < 8);
  1232. ASSERT((ui32LowRef < 4096) && (ui32LowRef <= ui32HighRef));
  1233. ASSERT(ui32HighRef < 4096);
  1234. //
  1235. // Save the new region settings.
  1236. //
  1237. HWREG(ui32Base + ADC_O_DCCMP0 + (ui32Comp * 4)) = ((ui32HighRef << 16) |
  1238. ui32LowRef);
  1239. }
  1240. //*****************************************************************************
  1241. //
  1242. //! Resets the current ADC digital comparator conditions.
  1243. //!
  1244. //! \param ui32Base is the base address of the ADC module.
  1245. //! \param ui32Comp is the index of the comparator.
  1246. //! \param bTrigger is the flag to indicate reset of Trigger conditions.
  1247. //! \param bInterrupt is the flag to indicate reset of Interrupt conditions.
  1248. //!
  1249. //! Because the digital comparator uses current and previous ADC values, this
  1250. //! function allows the comparator to be reset to its initial
  1251. //! value to prevent stale data from being used when a sequence is enabled.
  1252. //!
  1253. //! \return None.
  1254. //
  1255. //*****************************************************************************
  1256. void
  1257. ADCComparatorReset(uint32_t ui32Base, uint32_t ui32Comp, bool bTrigger,
  1258. bool bInterrupt)
  1259. {
  1260. uint32_t ui32Temp;
  1261. //
  1262. // Check the arguments.
  1263. //
  1264. ASSERT((ui32Base == ADC0_BASE) || (ui32Base == ADC1_BASE));
  1265. ASSERT(ui32Comp < 8);
  1266. //
  1267. // Set the appropriate bits to reset the trigger and/or interrupt
  1268. // comparator conditions.
  1269. //
  1270. ui32Temp = 0;
  1271. if(bTrigger)
  1272. {
  1273. ui32Temp |= (1 << (16 + ui32Comp));
  1274. }
  1275. if(bInterrupt)
  1276. {
  1277. ui32Temp |= (1 << ui32Comp);
  1278. }
  1279. HWREG(ui32Base + ADC_O_DCRIC) = ui32Temp;
  1280. }
  1281. //*****************************************************************************
  1282. //
  1283. //! Disables a sample sequence comparator interrupt.
  1284. //!
  1285. //! \param ui32Base is the base address of the ADC module.
  1286. //! \param ui32SequenceNum is the sample sequence number.
  1287. //!
  1288. //! This function disables the requested sample sequence comparator interrupt.
  1289. //!
  1290. //! \return None.
  1291. //
  1292. //*****************************************************************************
  1293. void
  1294. ADCComparatorIntDisable(uint32_t ui32Base, uint32_t ui32SequenceNum)
  1295. {
  1296. //
  1297. // Check the arguments.
  1298. //
  1299. ASSERT((ui32Base == ADC0_BASE) || (ui32Base == ADC1_BASE));
  1300. ASSERT(ui32SequenceNum < 4);
  1301. //
  1302. // Disable this sample sequence comparator interrupt.
  1303. //
  1304. HWREG(ui32Base + ADC_O_IM) &= ~(0x10000 << ui32SequenceNum);
  1305. }
  1306. //*****************************************************************************
  1307. //
  1308. //! Enables a sample sequence comparator interrupt.
  1309. //!
  1310. //! \param ui32Base is the base address of the ADC module.
  1311. //! \param ui32SequenceNum is the sample sequence number.
  1312. //!
  1313. //! This function enables the requested sample sequence comparator interrupt.
  1314. //!
  1315. //! \return None.
  1316. //
  1317. //*****************************************************************************
  1318. void
  1319. ADCComparatorIntEnable(uint32_t ui32Base, uint32_t ui32SequenceNum)
  1320. {
  1321. //
  1322. // Check the arguments.
  1323. //
  1324. ASSERT((ui32Base == ADC0_BASE) || (ui32Base == ADC1_BASE));
  1325. ASSERT(ui32SequenceNum < 4);
  1326. //
  1327. // Enable this sample sequence interrupt.
  1328. //
  1329. HWREG(ui32Base + ADC_O_IM) |= 0x10000 << ui32SequenceNum;
  1330. }
  1331. //*****************************************************************************
  1332. //
  1333. //! Gets the current comparator interrupt status.
  1334. //!
  1335. //! \param ui32Base is the base address of the ADC module.
  1336. //!
  1337. //! This function returns the digital comparator interrupt status bits. This
  1338. //! status is sequence agnostic.
  1339. //!
  1340. //! \return The current comparator interrupt status.
  1341. //
  1342. //*****************************************************************************
  1343. uint32_t
  1344. ADCComparatorIntStatus(uint32_t ui32Base)
  1345. {
  1346. //
  1347. // Check the arguments.
  1348. //
  1349. ASSERT((ui32Base == ADC0_BASE) || (ui32Base == ADC1_BASE));
  1350. //
  1351. // Return the digital comparator interrupt status.
  1352. //
  1353. return(HWREG(ui32Base + ADC_O_DCISC));
  1354. }
  1355. //*****************************************************************************
  1356. //
  1357. //! Clears sample sequence comparator interrupt source.
  1358. //!
  1359. //! \param ui32Base is the base address of the ADC module.
  1360. //! \param ui32Status is the bit-mapped interrupts status to clear.
  1361. //!
  1362. //! The specified interrupt status is cleared.
  1363. //!
  1364. //! \return None.
  1365. //
  1366. //*****************************************************************************
  1367. void
  1368. ADCComparatorIntClear(uint32_t ui32Base, uint32_t ui32Status)
  1369. {
  1370. //
  1371. // Check the arguments.
  1372. //
  1373. ASSERT((ui32Base == ADC0_BASE) || (ui32Base == ADC1_BASE));
  1374. //
  1375. // Clear the interrupt.
  1376. //
  1377. HWREG(ui32Base + ADC_O_DCISC) = ui32Status;
  1378. }
  1379. //*****************************************************************************
  1380. //
  1381. //! Disables ADC interrupt sources.
  1382. //!
  1383. //! \param ui32Base is the base address of the ADC module.
  1384. //! \param ui32IntFlags is the bit mask of the interrupt sources to disable.
  1385. //!
  1386. //! This function disables the indicated ADC interrupt sources. Only the
  1387. //! sources that are enabled can be reflected to the processor interrupt;
  1388. //! disabled sources have no effect on the processor.
  1389. //!
  1390. //! The \e ui32IntFlags parameter is the logical OR of any of the following:
  1391. //!
  1392. //! - \b ADC_INT_SS0 - interrupt due to ADC sample sequence 0.
  1393. //! - \b ADC_INT_SS1 - interrupt due to ADC sample sequence 1.
  1394. //! - \b ADC_INT_SS2 - interrupt due to ADC sample sequence 2.
  1395. //! - \b ADC_INT_SS3 - interrupt due to ADC sample sequence 3.
  1396. //! - \b ADC_INT_DMA_SS0 - interrupt due to DMA on ADC sample sequence 0.
  1397. //! - \b ADC_INT_DMA_SS1 - interrupt due to DMA on ADC sample sequence 1.
  1398. //! - \b ADC_INT_DMA_SS2 - interrupt due to DMA on ADC sample sequence 2.
  1399. //! - \b ADC_INT_DMA_SS3 - interrupt due to DMA on ADC sample sequence 3.
  1400. //! - \b ADC_INT_DCON_SS0 - interrupt due to digital comparator on ADC sample
  1401. //! sequence 0.
  1402. //! - \b ADC_INT_DCON_SS1 - interrupt due to digital comparator on ADC sample
  1403. //! sequence 1.
  1404. //! - \b ADC_INT_DCON_SS2 - interrupt due to digital comparator on ADC sample
  1405. //! sequence 2.
  1406. //! - \b ADC_INT_DCON_SS3 - interrupt due to digital comparator on ADC sample
  1407. //! sequence 3.
  1408. //!
  1409. //! \return None.
  1410. //
  1411. //*****************************************************************************
  1412. void
  1413. ADCIntDisableEx(uint32_t ui32Base, uint32_t ui32IntFlags)
  1414. {
  1415. //
  1416. // Check the arguments.
  1417. //
  1418. ASSERT((ui32Base == ADC0_BASE) || (ui32Base == ADC1_BASE));
  1419. //
  1420. // Disable the requested interrupts.
  1421. //
  1422. HWREG(ui32Base + ADC_O_IM) &= ~ui32IntFlags;
  1423. }
  1424. //*****************************************************************************
  1425. //
  1426. //! Enables ADC interrupt sources.
  1427. //!
  1428. //! \param ui32Base is the base address of the ADC module.
  1429. //! \param ui32IntFlags is the bit mask of the interrupt sources to disable.
  1430. //!
  1431. //! This function enables the indicated ADC interrupt sources. Only the
  1432. //! sources that are enabled can be reflected to the processor interrupt;
  1433. //! disabled sources have no effect on the processor.
  1434. //!
  1435. //! The \e ui32IntFlags parameter is the logical OR of any of the following:
  1436. //!
  1437. //! - \b ADC_INT_SS0 - interrupt due to ADC sample sequence 0.
  1438. //! - \b ADC_INT_SS1 - interrupt due to ADC sample sequence 1.
  1439. //! - \b ADC_INT_SS2 - interrupt due to ADC sample sequence 2.
  1440. //! - \b ADC_INT_SS3 - interrupt due to ADC sample sequence 3.
  1441. //! - \b ADC_INT_DMA_SS0 - interrupt due to DMA on ADC sample sequence 0.
  1442. //! - \b ADC_INT_DMA_SS1 - interrupt due to DMA on ADC sample sequence 1.
  1443. //! - \b ADC_INT_DMA_SS2 - interrupt due to DMA on ADC sample sequence 2.
  1444. //! - \b ADC_INT_DMA_SS3 - interrupt due to DMA on ADC sample sequence 3.
  1445. //! - \b ADC_INT_DCON_SS0 - interrupt due to digital comparator on ADC sample
  1446. //! sequence 0.
  1447. //! - \b ADC_INT_DCON_SS1 - interrupt due to digital comparator on ADC sample
  1448. //! sequence 1.
  1449. //! - \b ADC_INT_DCON_SS2 - interrupt due to digital comparator on ADC sample
  1450. //! sequence 2.
  1451. //! - \b ADC_INT_DCON_SS3 - interrupt due to digital comparator on ADC sample
  1452. //! sequence 3.
  1453. //!
  1454. //! \return None.
  1455. //
  1456. //*****************************************************************************
  1457. void
  1458. ADCIntEnableEx(uint32_t ui32Base, uint32_t ui32IntFlags)
  1459. {
  1460. //
  1461. // Check the arguments.
  1462. //
  1463. ASSERT((ui32Base == ADC0_BASE) || (ui32Base == ADC1_BASE));
  1464. //
  1465. // Enable the requested interrupts.
  1466. //
  1467. HWREG(ui32Base + ADC_O_IM) |= ui32IntFlags;
  1468. }
  1469. //*****************************************************************************
  1470. //
  1471. //! Gets interrupt status for the specified ADC module.
  1472. //!
  1473. //! \param ui32Base is the base address of the ADC module.
  1474. //! \param bMasked specifies whether masked or raw interrupt status is
  1475. //! returned.
  1476. //!
  1477. //! If \e bMasked is set as \b true, then the masked interrupt status is
  1478. //! returned; otherwise, the raw interrupt status is returned.
  1479. //!
  1480. //! \return Returns the current interrupt status for the specified ADC module.
  1481. //! The value returned is the logical OR of the \b ADC_INT_* values that are
  1482. //! currently active.
  1483. //
  1484. //*****************************************************************************
  1485. uint32_t
  1486. ADCIntStatusEx(uint32_t ui32Base, bool bMasked)
  1487. {
  1488. uint32_t ui32Temp;
  1489. //
  1490. // Check the arguments.
  1491. //
  1492. ASSERT((ui32Base == ADC0_BASE) || (ui32Base == ADC1_BASE));
  1493. //
  1494. // Return either the masked interrupt status or the raw interrupt status as
  1495. // requested.
  1496. //
  1497. if(bMasked)
  1498. {
  1499. ui32Temp = HWREG(ui32Base + ADC_O_ISC);
  1500. }
  1501. else
  1502. {
  1503. //
  1504. // Read the Raw interrupt status to see if a digital comparator
  1505. // interrupt is active.
  1506. //
  1507. ui32Temp = HWREG(ui32Base + ADC_O_RIS);
  1508. //
  1509. // Since, the raw interrupt status only indicates that any one of the
  1510. // digital comparators caused an interrupt, if the raw interrupt status
  1511. // is set then the return value is modified to indicate that all sample
  1512. // sequences have a pending digital comparator interrupt.
  1513. // This is exactly how the hardware works so the return code is
  1514. // modified to match this behavior.
  1515. //
  1516. if(ui32Temp & ADC_RIS_INRDC)
  1517. {
  1518. ui32Temp |= (ADC_INT_DCON_SS3 | ADC_INT_DCON_SS2 |
  1519. ADC_INT_DCON_SS1 | ADC_INT_DCON_SS0);
  1520. }
  1521. }
  1522. return(ui32Temp);
  1523. }
  1524. //*****************************************************************************
  1525. //
  1526. //! Clears the specified ADC interrupt sources.
  1527. //!
  1528. //! \param ui32Base is the base address of the ADC port.
  1529. //! \param ui32IntFlags is the bit mask of the interrupt sources to disable.
  1530. //!
  1531. //! Clears the interrupt for the specified interrupt source(s).
  1532. //!
  1533. //! The \e ui32IntFlags parameter is the logical OR of the \b ADC_INT_* values.
  1534. //! See the ADCIntEnableEx() function for the list of possible \b ADC_INT*
  1535. //! values.
  1536. //!
  1537. //! \note Because there is a write buffer in the Cortex-M processor, it may
  1538. //! take several clock cycles before the interrupt source is actually cleared.
  1539. //! Therefore, it is recommended that the interrupt source be cleared early in
  1540. //! the interrupt handler (as opposed to the very last action) to avoid
  1541. //! returning from the interrupt handler before the interrupt source is
  1542. //! actually cleared. Failure to do so may result in the interrupt handler
  1543. //! being immediately reentered (because the interrupt controller still sees
  1544. //! the interrupt source asserted).
  1545. //!
  1546. //! \return None.
  1547. //
  1548. //*****************************************************************************
  1549. void
  1550. ADCIntClearEx(uint32_t ui32Base, uint32_t ui32IntFlags)
  1551. {
  1552. //
  1553. // Note: The interrupt bits are "W1C" so we DO NOT use a logical OR
  1554. // here to clear the requested bits. Doing so would clear all outstanding
  1555. // interrupts rather than just those which the caller has specified.
  1556. //
  1557. HWREG(ui32Base + ADC_O_ISC) = ui32IntFlags;
  1558. }
  1559. //*****************************************************************************
  1560. //
  1561. //! Selects the ADC reference.
  1562. //!
  1563. //! \param ui32Base is the base address of the ADC module.
  1564. //! \param ui32Ref is the reference to use.
  1565. //!
  1566. //! The ADC reference is set as specified by \e ui32Ref. It must be one of
  1567. //! \b ADC_REF_INT, or \b ADC_REF_EXT_3V for internal or external reference
  1568. //! If \b ADC_REF_INT is chosen, then an internal 3V reference is used and
  1569. //! no external reference is needed. If \b ADC_REF_EXT_3V is chosen, then
  1570. //! a 3V reference must be supplied to the AVREF pin.
  1571. //!
  1572. //! \note The ADC reference can only be selected on parts that have an external
  1573. //! reference. Consult the data sheet for your part to determine if there is
  1574. //! an external reference.
  1575. //!
  1576. //! \return None.
  1577. //
  1578. //*****************************************************************************
  1579. void
  1580. ADCReferenceSet(uint32_t ui32Base, uint32_t ui32Ref)
  1581. {
  1582. //
  1583. // Check the arguments.
  1584. //
  1585. ASSERT((ui32Base == ADC0_BASE) || (ui32Base == ADC1_BASE));
  1586. ASSERT((ui32Ref == ADC_REF_INT) || (ui32Ref == ADC_REF_EXT_3V));
  1587. //
  1588. // Set the reference.
  1589. //
  1590. HWREG(ui32Base + ADC_O_CTL) =
  1591. (HWREG(ui32Base + ADC_O_CTL) & ~ADC_CTL_VREF_M) | ui32Ref;
  1592. }
  1593. //*****************************************************************************
  1594. //
  1595. //! Returns the current setting of the ADC reference.
  1596. //!
  1597. //! \param ui32Base is the base address of the ADC module.
  1598. //!
  1599. //! Returns the value of the ADC reference setting. The returned value is one
  1600. //! of \b ADC_REF_INT, or \b ADC_REF_EXT_3V.
  1601. //!
  1602. //! \note The value returned by this function is only meaningful if used on a
  1603. //! part that is capable of using an external reference. Consult the data
  1604. //! sheet for your part to determine if it has an external reference input.
  1605. //!
  1606. //! \return The current setting of the ADC reference.
  1607. //
  1608. //*****************************************************************************
  1609. uint32_t
  1610. ADCReferenceGet(uint32_t ui32Base)
  1611. {
  1612. //
  1613. // Check the arguments.
  1614. //
  1615. ASSERT((ui32Base == ADC0_BASE) || (ui32Base == ADC1_BASE));
  1616. //
  1617. // Return the value of the reference.
  1618. //
  1619. return(HWREG(ui32Base + ADC_O_CTL) & ADC_CTL_VREF_M);
  1620. }
  1621. //*****************************************************************************
  1622. //
  1623. //! Sets the phase delay between a trigger and the start of a sequence.
  1624. //!
  1625. //! \param ui32Base is the base address of the ADC module.
  1626. //! \param ui32Phase is the phase delay, specified as one of \b ADC_PHASE_0,
  1627. //! \b ADC_PHASE_22_5, \b ADC_PHASE_45, \b ADC_PHASE_67_5, \b ADC_PHASE_90,
  1628. //! \b ADC_PHASE_112_5, \b ADC_PHASE_135, \b ADC_PHASE_157_5, \b ADC_PHASE_180,
  1629. //! \b ADC_PHASE_202_5, \b ADC_PHASE_225, \b ADC_PHASE_247_5, \b ADC_PHASE_270,
  1630. //! \b ADC_PHASE_292_5, \b ADC_PHASE_315, or \b ADC_PHASE_337_5.
  1631. //!
  1632. //! This function sets the phase delay between the detection of an ADC trigger
  1633. //! event and the start of the sample sequence. By selecting a different phase
  1634. //! delay for a pair of ADC modules (such as \b ADC_PHASE_0 and
  1635. //! \b ADC_PHASE_180) and having each ADC module sample the same analog input,
  1636. //! it is possible to increase the sampling rate of the analog input (with
  1637. //! samples N, N+2, N+4, and so on, coming from the first ADC and samples N+1,
  1638. //! N+3, N+5, and so on, coming from the second ADC). The ADC module has a
  1639. //! single phase delay that is applied to all sample sequences within that
  1640. //! module.
  1641. //!
  1642. //! \note This capability is not available on all parts.
  1643. //!
  1644. //! \return None.
  1645. //
  1646. //*****************************************************************************
  1647. void
  1648. ADCPhaseDelaySet(uint32_t ui32Base, uint32_t ui32Phase)
  1649. {
  1650. //
  1651. // Check the arguments.
  1652. //
  1653. ASSERT((ui32Base == ADC0_BASE) || (ui32Base == ADC1_BASE));
  1654. ASSERT((ui32Phase == ADC_PHASE_0) || (ui32Phase == ADC_PHASE_22_5) ||
  1655. (ui32Phase == ADC_PHASE_45) || (ui32Phase == ADC_PHASE_67_5) ||
  1656. (ui32Phase == ADC_PHASE_90) || (ui32Phase == ADC_PHASE_112_5) ||
  1657. (ui32Phase == ADC_PHASE_135) || (ui32Phase == ADC_PHASE_157_5) ||
  1658. (ui32Phase == ADC_PHASE_180) || (ui32Phase == ADC_PHASE_202_5) ||
  1659. (ui32Phase == ADC_PHASE_225) || (ui32Phase == ADC_PHASE_247_5) ||
  1660. (ui32Phase == ADC_PHASE_270) || (ui32Phase == ADC_PHASE_292_5) ||
  1661. (ui32Phase == ADC_PHASE_315) || (ui32Phase == ADC_PHASE_337_5));
  1662. //
  1663. // Set the phase delay.
  1664. //
  1665. HWREG(ui32Base + ADC_O_SPC) = ui32Phase;
  1666. }
  1667. //*****************************************************************************
  1668. //
  1669. //! Gets the phase delay between a trigger and the start of a sequence.
  1670. //!
  1671. //! \param ui32Base is the base address of the ADC module.
  1672. //!
  1673. //! This function gets the current phase delay between the detection of an ADC
  1674. //! trigger event and the start of the sample sequence.
  1675. //!
  1676. //! \return Returns the phase delay, specified as one of \b ADC_PHASE_0,
  1677. //! \b ADC_PHASE_22_5, \b ADC_PHASE_45, \b ADC_PHASE_67_5, \b ADC_PHASE_90,
  1678. //! \b ADC_PHASE_112_5, \b ADC_PHASE_135, \b ADC_PHASE_157_5, \b ADC_PHASE_180,
  1679. //! \b ADC_PHASE_202_5, \b ADC_PHASE_225, \b ADC_PHASE_247_5, \b ADC_PHASE_270,
  1680. //! \b ADC_PHASE_292_5, \b ADC_PHASE_315, or \b ADC_PHASE_337_5.
  1681. //
  1682. //*****************************************************************************
  1683. uint32_t
  1684. ADCPhaseDelayGet(uint32_t ui32Base)
  1685. {
  1686. //
  1687. // Check the arguments.
  1688. //
  1689. ASSERT((ui32Base == ADC0_BASE) || (ui32Base == ADC1_BASE));
  1690. //
  1691. // Return the phase delay.
  1692. //
  1693. return(HWREG(ui32Base + ADC_O_SPC));
  1694. }
  1695. //*****************************************************************************
  1696. //
  1697. //! Enables DMA for sample sequencers.
  1698. //!
  1699. //! \param ui32Base is the base address of the ADC module.
  1700. //! \param ui32SequenceNum is the sample sequence number.
  1701. //!
  1702. //! Allows DMA requests to be generated based on the FIFO level of the sample
  1703. //! sequencer.
  1704. //!
  1705. //! \return None.
  1706. //
  1707. //*****************************************************************************
  1708. void
  1709. ADCSequenceDMAEnable(uint32_t ui32Base, uint32_t ui32SequenceNum)
  1710. {
  1711. //
  1712. // Check the arguments.
  1713. //
  1714. ASSERT((ui32Base == ADC0_BASE) || (ui32Base == ADC1_BASE));
  1715. ASSERT(ui32SequenceNum < 4);
  1716. //
  1717. // Enable the DMA on the specified sequencer.
  1718. //
  1719. HWREG(ui32Base + ADC_O_ACTSS) |= 0x100 << ui32SequenceNum;
  1720. }
  1721. //*****************************************************************************
  1722. //
  1723. //! Disables DMA for sample sequencers.
  1724. //!
  1725. //! \param ui32Base is the base address of the ADC module.
  1726. //! \param ui32SequenceNum is the sample sequence number.
  1727. //!
  1728. //! Prevents the specified sample sequencer from generating DMA requests.
  1729. //!
  1730. //! \return None.
  1731. //
  1732. //*****************************************************************************
  1733. void
  1734. ADCSequenceDMADisable(uint32_t ui32Base, uint32_t ui32SequenceNum)
  1735. {
  1736. //
  1737. // Check the arguments.
  1738. //
  1739. ASSERT((ui32Base == ADC0_BASE) || (ui32Base == ADC1_BASE));
  1740. ASSERT(ui32SequenceNum < 4);
  1741. //
  1742. // Disable the DMA on the specified sequencer.
  1743. //
  1744. HWREG(ui32Base + ADC_O_ACTSS) &= ~(0x100 << ui32SequenceNum);
  1745. }
  1746. //*****************************************************************************
  1747. //
  1748. //! Determines whether the ADC is busy or not.
  1749. //!
  1750. //! \param ui32Base is the base address of the ADC.
  1751. //!
  1752. //! This function allows the caller to determine whether or not the ADC is
  1753. //! currently sampling . If \b false is returned, then the ADC is not
  1754. //! sampling data.
  1755. //!
  1756. //! Use this function to detect that the ADC is finished sampling data before
  1757. //! putting the device into deep sleep. Before using this function, it is
  1758. //! highly recommended that the event trigger is changed to
  1759. //! \b ADC_TRIGGER_NEVER on all enabled sequencers to prevent the ADC from
  1760. //! starting after checking the busy status.
  1761. //!
  1762. //! \return Returns \b true if the ADC is sampling or \b false if all
  1763. //! samples are complete.
  1764. //
  1765. //*****************************************************************************
  1766. bool
  1767. ADCBusy(uint32_t ui32Base)
  1768. {
  1769. //
  1770. // Check the argument.
  1771. //
  1772. ASSERT((ui32Base == ADC0_BASE) || (ui32Base == ADC1_BASE));
  1773. //
  1774. // Determine if the ADC is busy.
  1775. //
  1776. return((HWREG(ui32Base + ADC_O_ACTSS) & ADC_ACTSS_BUSY) ? true : false);
  1777. }
  1778. //*****************************************************************************
  1779. //
  1780. //! Sets the clock configuration for the ADC.
  1781. //!
  1782. //! \param ui32Base is the base address of the ADC to configure, which must
  1783. //! always be \b ADC0_BASE.
  1784. //! \param ui32Config is a combination of the \b ADC_CLOCK_SRC_ and
  1785. //! \b ADC_CLOCK_RATE_* values used to configure the ADC clock input.
  1786. //! \param ui32ClockDiv is the input clock divider for the clock selected by
  1787. //! the \b ADC_CLOCK_SRC value.
  1788. //!
  1789. //! This function is used to configure the input clock to the ADC modules. The
  1790. //! clock configuration is shared across ADC units so \e ui32Base must
  1791. //! always be \b ADC0_BASE. The \e ui32Config value is logical OR of one
  1792. //! of the \b ADC_CLOCK_RATE_ and one of the \b ADC_CLOCK_SRC_ values defined
  1793. //! below. The \b ADC_CLOCK_SRC_* values determine the input clock for the ADC.
  1794. //! Not all values are available on all devices so check the device data sheet
  1795. //! to determine value configuration options. Regardless of the source, the
  1796. //! final frequency for TM4C123x devices must be 16 MHz and for TM4C129x parts
  1797. //! after dividing must be between 16 and 32 MHz.
  1798. //!
  1799. //! \note For TM4C123x devices, if the PLL is enabled, the PLL/25 is used as
  1800. //! the ADC clock unless ADC_CLOCK_SRC_PIOSC is specified. If the PLL is
  1801. //! disabled, the MOSC is used as the clock source unless ADC_CLOCK_SRC_PIOSC
  1802. //! is specified.
  1803. //!
  1804. //! - \b ADC_CLOCK_SRC_PLL - The main PLL output (TM4x129 class only).
  1805. //! - \b ADC_CLOCK_SRC_PIOSC - The internal PIOSC at 16 MHz.
  1806. //! - \b ADC_CLOCK_SRC_ALTCLK - The output of the ALTCLK in the system control
  1807. //! module (TM4x129 class only).
  1808. //! - \b ADC_CLOCK_SRC_MOSC - The external MOSC (TM4x129 class only).
  1809. //!
  1810. //! \b ADC_CLOCK_RATE values control how often samples are provided back to the
  1811. //! application. The values are the following:
  1812. //!
  1813. //! - \b ADC_CLOCK_RATE_FULL - All samples.
  1814. //! - \b ADC_CLOCK_RATE_HALF - Every other sample.
  1815. //! - \b ADC_CLOCK_RATE_QUARTER - Every fourth sample.
  1816. //! - \b ADC_CLOCK_RATE_EIGHTH - Every either sample.
  1817. //!
  1818. //! The \e ui32ClockDiv parameter allows for dividing a higher frequency down
  1819. //! into the valid range for the ADCs. This parameter is typically only used
  1820. //! \b ADC_CLOCK_SRC_PLL option because it is the only clock value that can be
  1821. //! with the in the correct range to use the divider. The actual value ranges
  1822. //! from 1 to 64.
  1823. //!
  1824. //! \b Example: ADC Clock Configurations
  1825. //!
  1826. //! \verbatim
  1827. //!
  1828. //! //
  1829. //! // Configure the ADC to use PIOSC divided by one (16 MHz) and sample at
  1830. //! // half the rate.
  1831. //! //
  1832. //! ADCClockConfigSet(ADC0_BASE, ADC_CLOCK_SRC_PIOSC | ADC_CLOCK_RATE_HALF, 1);
  1833. //!
  1834. //! ...
  1835. //!
  1836. //! //
  1837. //! // Configure the ADC to use PLL at 480 MHz divided by 24 to get an ADC
  1838. //! // clock of 20 MHz.
  1839. //! //
  1840. //! ADCClockConfigSet(ADC0_BASE, ADC_CLOCK_SRC_PLL | ADC_CLOCK_RATE_FULL, 24);
  1841. //! \endverbatim
  1842. //!
  1843. //! \return None.
  1844. //
  1845. //*****************************************************************************
  1846. void
  1847. ADCClockConfigSet(uint32_t ui32Base, uint32_t ui32Config,
  1848. uint32_t ui32ClockDiv)
  1849. {
  1850. //
  1851. // Check the argument.
  1852. //
  1853. ASSERT(ui32Base == ADC0_BASE);
  1854. ASSERT((ui32ClockDiv - 1) <= (ADC_CC_CLKDIV_M >> ADC_CC_CLKDIV_S));
  1855. //
  1856. // A rate must be supplied.
  1857. //
  1858. ASSERT((ui32Config & ADC_CLOCK_RATE_FULL) != 0);
  1859. //
  1860. // Write the sample conversion rate.
  1861. //
  1862. HWREG(ui32Base + ADC_O_PC) = (ui32Config >> 4) & ADC_PC_SR_M;
  1863. //
  1864. // Write the clock select and divider.
  1865. //
  1866. HWREG(ui32Base + ADC_O_CC) = (ui32Config & ADC_CC_CS_M) |
  1867. (((ui32ClockDiv - 1) << ADC_CC_CLKDIV_S)) ;
  1868. }
  1869. //*****************************************************************************
  1870. //
  1871. //! Returns the clock configuration for the ADC.
  1872. //!
  1873. //! \param ui32Base is the base address of the ADC to configure, which must
  1874. //! always be \b ADC0_BASE.
  1875. //! \param pui32ClockDiv is a pointer to the input clock divider for the clock
  1876. //! selected by the \b ADC_CLOCK_SRC in use by the ADCs.
  1877. //!
  1878. //! This function returns the ADC clock configuration and the clock divider for
  1879. //! the ADCs.
  1880. //!
  1881. //! \b Example: Read the current ADC clock configuration.
  1882. //!
  1883. //! \verbatim
  1884. //! uint32_t ui32Config, ui32ClockDiv;
  1885. //!
  1886. //! //
  1887. //! // Read the current ADC clock configuration.
  1888. //! //
  1889. //! ui32Config = ADCClockConfigGet(ADC0_BASE, &ui32ClockDiv);
  1890. //! \endverbatim
  1891. //!
  1892. //! \return The current clock configuration of the ADC defined as a combination
  1893. //! of one of \b ADC_CLOCK_SRC_PLL, \b ADC_CLOCK_SRC_PIOSC,
  1894. //! \b ADC_CLOCK_SRC_MOSC, or \b ADC_CLOCK_SRC_ALTCLK logical ORed with one of
  1895. //! \b ADC_CLOCK_RATE_FULL, \b ADC_CLOCK_RATE_HALF, \b ADC_CLOCK_RATE_QUARTER,
  1896. //! or \b ADC_CLOCK_RATE_EIGHTH. See ADCClockConfigSet() for more information
  1897. //! on these values.
  1898. //
  1899. //*****************************************************************************
  1900. uint32_t
  1901. ADCClockConfigGet(uint32_t ui32Base, uint32_t *pui32ClockDiv)
  1902. {
  1903. uint32_t ui32Config;
  1904. //
  1905. // Check the argument.
  1906. //
  1907. ASSERT(ui32Base == ADC0_BASE);
  1908. //
  1909. // Read the current configuration.
  1910. //
  1911. ui32Config = HWREG(ADC0_BASE + ADC_O_CC);
  1912. //
  1913. // If the clock divider was requested provide the current value.
  1914. //
  1915. if(pui32ClockDiv)
  1916. {
  1917. *pui32ClockDiv =
  1918. ((ui32Config & ADC_CC_CLKDIV_M) >> ADC_CC_CLKDIV_S) + 1;
  1919. }
  1920. //
  1921. // Clear out the divider bits.
  1922. //
  1923. ui32Config &= ~ADC_CC_CLKDIV_M;
  1924. //
  1925. // Add in the sample interval to the configuration.
  1926. //
  1927. ui32Config |= (HWREG(ADC0_BASE + ADC_O_PC) & ADC_PC_SR_M) << 4;
  1928. return(ui32Config);
  1929. }
  1930. //*****************************************************************************
  1931. //
  1932. // Close the Doxygen group.
  1933. //! @}
  1934. //
  1935. //*****************************************************************************