adc.c 71 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019
  1. //*****************************************************************************
  2. //
  3. // adc.c - Driver for the ADC.
  4. //
  5. // Copyright (c) 2005-2014 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.1.0.12573 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[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_ADC0SS0_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. //
  472. // Check the arugments.
  473. //
  474. ASSERT((ui32Base == ADC0_BASE) || (ui32Base == ADC1_BASE));
  475. ASSERT(ui32SequenceNum < 4);
  476. ASSERT(((ui32Trigger & 0xF) == ADC_TRIGGER_PROCESSOR) ||
  477. ((ui32Trigger & 0xF) == ADC_TRIGGER_COMP0) ||
  478. ((ui32Trigger & 0xF) == ADC_TRIGGER_COMP1) ||
  479. ((ui32Trigger & 0xF) == ADC_TRIGGER_COMP2) ||
  480. ((ui32Trigger & 0xF) == ADC_TRIGGER_EXTERNAL) ||
  481. ((ui32Trigger & 0xF) == ADC_TRIGGER_TIMER) ||
  482. ((ui32Trigger & 0xF) == ADC_TRIGGER_PWM0) ||
  483. ((ui32Trigger & 0xF) == ADC_TRIGGER_PWM1) ||
  484. ((ui32Trigger & 0xF) == ADC_TRIGGER_PWM2) ||
  485. ((ui32Trigger & 0xF) == ADC_TRIGGER_PWM3) ||
  486. ((ui32Trigger & 0xF) == ADC_TRIGGER_ALWAYS) ||
  487. ((ui32Trigger & 0x30) == ADC_TRIGGER_PWM_MOD0) ||
  488. ((ui32Trigger & 0x30) == ADC_TRIGGER_PWM_MOD1));
  489. ASSERT(ui32Priority < 4);
  490. //
  491. // Compute the shift for the bits that control this sample sequence.
  492. //
  493. ui32SequenceNum *= 4;
  494. //
  495. // Set the trigger event for this sample sequence.
  496. //
  497. HWREG(ui32Base + ADC_O_EMUX) = ((HWREG(ui32Base + ADC_O_EMUX) &
  498. ~(0xf << ui32SequenceNum)) |
  499. ((ui32Trigger & 0xf) << ui32SequenceNum));
  500. //
  501. // Set the priority for this sample sequence.
  502. //
  503. HWREG(ui32Base + ADC_O_SSPRI) = ((HWREG(ui32Base + ADC_O_SSPRI) &
  504. ~(0xf << ui32SequenceNum)) |
  505. ((ui32Priority & 0x3) <<
  506. ui32SequenceNum));
  507. //
  508. // Set the source PWM module for this sequence's PWM triggers.
  509. //
  510. ui32SequenceNum *= 2;
  511. HWREG(ui32Base + ADC_O_TSSEL) = ((HWREG(ui32Base + ADC_O_TSSEL) &
  512. ~(0x30 << ui32SequenceNum)) |
  513. ((ui32Trigger & 0x30) <<
  514. ui32SequenceNum));
  515. }
  516. //*****************************************************************************
  517. //
  518. //! Configure a step of the sample sequencer.
  519. //!
  520. //! \param ui32Base is the base address of the ADC module.
  521. //! \param ui32SequenceNum is the sample sequence number.
  522. //! \param ui32Step is the step to be configured.
  523. //! \param ui32Config is the configuration of this step; must be a logical OR
  524. //! of \b ADC_CTL_TS, \b ADC_CTL_IE, \b ADC_CTL_END, \b ADC_CTL_D, one of the
  525. //! input channel selects (\b ADC_CTL_CH0 through \b ADC_CTL_CH23), and one of
  526. //! the digital comparator selects (\b ADC_CTL_CMP0 through \b ADC_CTL_CMP7).
  527. //!
  528. //! This function configures the ADC for one step of a sample sequence. The
  529. //! ADC can be configured for single-ended or differential operation (the
  530. //! \b ADC_CTL_D bit selects differential operation when set), the channel to
  531. //! be sampled can be chosen (the \b ADC_CTL_CH0 through \b ADC_CTL_CH23
  532. //! values), and the internal temperature sensor can be selected (the
  533. //! \b ADC_CTL_TS bit). Additionally, this step can be defined as the last in
  534. //! the sequence (the \b ADC_CTL_END bit) and it can be configured to cause an
  535. //! interrupt when the step is complete (the \b ADC_CTL_IE bit). If the
  536. //! digital comparators are present on the device, this step may also be
  537. //! configured to send the ADC sample to the selected comparator using
  538. //! \b ADC_CTL_CMP0 through \b ADC_CTL_CMP7. The configuration is used by the
  539. //! ADC at the appropriate time when the trigger for this sequence occurs.
  540. //!
  541. //! \note If the Digital Comparator is present and enabled using the
  542. //! \b ADC_CTL_CMP0 through \b ADC_CTL_CMP7 selects, the ADC sample is NOT
  543. //! written into the ADC sequence data FIFO.
  544. //!
  545. //! The \e ui32Step parameter determines the order in which the samples are
  546. //! captured by the ADC when the trigger occurs. It can range from zero to
  547. //! seven for the first sample sequencer, from zero to three for the second and
  548. //! third sample sequencer, and can only be zero for the fourth sample
  549. //! sequencer.
  550. //!
  551. //! Differential mode only works with adjacent channel pairs (for example, 0
  552. //! and 1). The channel select must be the number of the channel pair to
  553. //! sample (for example, \b ADC_CTL_CH0 for 0 and 1, or \b ADC_CTL_CH1 for 2
  554. //! and 3) or undefined results are returned by the ADC. Additionally, if
  555. //! differential mode is selected when the temperature sensor is being sampled,
  556. //! undefined results are returned by the ADC.
  557. //!
  558. //! It is the responsibility of the caller to ensure that a valid configuration
  559. //! is specified; this function does not check the validity of the specified
  560. //! configuration.
  561. //!
  562. //! \return None.
  563. //
  564. //*****************************************************************************
  565. void
  566. ADCSequenceStepConfigure(uint32_t ui32Base, uint32_t ui32SequenceNum,
  567. uint32_t ui32Step, uint32_t ui32Config)
  568. {
  569. uint32_t ui32Temp;
  570. //
  571. // Check the arguments.
  572. //
  573. ASSERT((ui32Base == ADC0_BASE) || (ui32Base == ADC1_BASE));
  574. ASSERT(ui32SequenceNum < 4);
  575. ASSERT(((ui32SequenceNum == 0) && (ui32Step < 8)) ||
  576. ((ui32SequenceNum == 1) && (ui32Step < 4)) ||
  577. ((ui32SequenceNum == 2) && (ui32Step < 4)) ||
  578. ((ui32SequenceNum == 3) && (ui32Step < 1)));
  579. //
  580. // Get the offset of the sequence to be configured.
  581. //
  582. ui32Base += ADC_SEQ + (ADC_SEQ_STEP * ui32SequenceNum);
  583. //
  584. // Compute the shift for the bits that control this step.
  585. //
  586. ui32Step *= 4;
  587. //
  588. // Set the analog mux value for this step.
  589. //
  590. HWREG(ui32Base + ADC_SSMUX) = ((HWREG(ui32Base + ADC_SSMUX) &
  591. ~(0x0000000f << ui32Step)) |
  592. ((ui32Config & 0x0f) << ui32Step));
  593. //
  594. // Set the upper bits of the analog mux value for this step.
  595. //
  596. HWREG(ui32Base + ADC_SSEMUX) = ((HWREG(ui32Base + ADC_SSEMUX) &
  597. ~(0x0000000f << ui32Step)) |
  598. (((ui32Config & 0xf00) >> 8) << ui32Step));
  599. //
  600. // Set the control value for this step.
  601. //
  602. HWREG(ui32Base + ADC_SSCTL) = ((HWREG(ui32Base + ADC_SSCTL) &
  603. ~(0x0000000f << ui32Step)) |
  604. (((ui32Config & 0xf0) >> 4) << ui32Step));
  605. //
  606. // Set the sample and hold time for this step. This is not available on
  607. // all devices, however on devices that do not support this feature these
  608. // reserved bits are ignored on write access.
  609. //
  610. HWREG(ui32Base + ADC_SSTSH) = ((HWREG(ui32Base + ADC_SSTSH) &
  611. ~(0x0000000f << ui32Step)) |
  612. (((ui32Config & 0xf00000) >> 20) << ui32Step));
  613. //
  614. // Enable digital comparator if specified in the ui32Config bit-fields.
  615. //
  616. if(ui32Config & 0x000F0000)
  617. {
  618. //
  619. // Program the comparator for the specified step.
  620. //
  621. ui32Temp = HWREG(ui32Base + ADC_SSDC);
  622. ui32Temp &= ~(0xF << ui32Step);
  623. ui32Temp |= (((ui32Config & 0x00070000) >> 16) << ui32Step);
  624. HWREG(ui32Base + ADC_SSDC) = ui32Temp;
  625. //
  626. // Enable the comparator.
  627. //
  628. HWREG(ui32Base + ADC_SSOP) |= (1 << ui32Step);
  629. }
  630. //
  631. // Disable digital comparator if not specified.
  632. //
  633. else
  634. {
  635. HWREG(ui32Base + ADC_SSOP) &= ~(1 << ui32Step);
  636. }
  637. }
  638. //*****************************************************************************
  639. //
  640. //! Determines if a sample sequence overflow occurred.
  641. //!
  642. //! \param ui32Base is the base address of the ADC module.
  643. //! \param ui32SequenceNum is the sample sequence number.
  644. //!
  645. //! This function determines if a sample sequence overflow has occurred.
  646. //! Overflow happens if the captured samples are not read from the FIFO before
  647. //! the next trigger occurs.
  648. //!
  649. //! \return Returns zero if there was not an overflow, and non-zero if there
  650. //! was.
  651. //
  652. //*****************************************************************************
  653. int32_t
  654. ADCSequenceOverflow(uint32_t ui32Base, uint32_t ui32SequenceNum)
  655. {
  656. //
  657. // Check the arguments.
  658. //
  659. ASSERT((ui32Base == ADC0_BASE) || (ui32Base == ADC1_BASE));
  660. ASSERT(ui32SequenceNum < 4);
  661. //
  662. // Determine if there was an overflow on this sequence.
  663. //
  664. return(HWREG(ui32Base + ADC_O_OSTAT) & (1 << ui32SequenceNum));
  665. }
  666. //*****************************************************************************
  667. //
  668. //! Clears the overflow condition on a sample sequence.
  669. //!
  670. //! \param ui32Base is the base address of the ADC module.
  671. //! \param ui32SequenceNum is the sample sequence number.
  672. //!
  673. //! This function clears an overflow condition on one of the sample sequences.
  674. //! The overflow condition must be cleared in order to detect a subsequent
  675. //! overflow condition (it otherwise causes no harm).
  676. //!
  677. //! \return None.
  678. //
  679. //*****************************************************************************
  680. void
  681. ADCSequenceOverflowClear(uint32_t ui32Base, uint32_t ui32SequenceNum)
  682. {
  683. //
  684. // Check the arguments.
  685. //
  686. ASSERT((ui32Base == ADC0_BASE) || (ui32Base == ADC1_BASE));
  687. ASSERT(ui32SequenceNum < 4);
  688. //
  689. // Clear the overflow condition for this sequence.
  690. //
  691. HWREG(ui32Base + ADC_O_OSTAT) = 1 << ui32SequenceNum;
  692. }
  693. //*****************************************************************************
  694. //
  695. //! Determines if a sample sequence underflow occurred.
  696. //!
  697. //! \param ui32Base is the base address of the ADC module.
  698. //! \param ui32SequenceNum is the sample sequence number.
  699. //!
  700. //! This function determines if a sample sequence underflow has occurred.
  701. //! Underflow happens if too many samples are read from the FIFO.
  702. //!
  703. //! \return Returns zero if there was not an underflow, and non-zero if there
  704. //! was.
  705. //
  706. //*****************************************************************************
  707. int32_t
  708. ADCSequenceUnderflow(uint32_t ui32Base, uint32_t ui32SequenceNum)
  709. {
  710. //
  711. // Check the arguments.
  712. //
  713. ASSERT((ui32Base == ADC0_BASE) || (ui32Base == ADC1_BASE));
  714. ASSERT(ui32SequenceNum < 4);
  715. //
  716. // Determine if there was an underflow on this sequence.
  717. //
  718. return(HWREG(ui32Base + ADC_O_USTAT) & (1 << ui32SequenceNum));
  719. }
  720. //*****************************************************************************
  721. //
  722. //! Clears the underflow condition on a sample sequence.
  723. //!
  724. //! \param ui32Base is the base address of the ADC module.
  725. //! \param ui32SequenceNum is the sample sequence number.
  726. //!
  727. //! This function clears an underflow condition on one of the sample
  728. //! sequencers. The underflow condition must be cleared in order to detect a
  729. //! subsequent underflow condition (it otherwise causes no harm).
  730. //!
  731. //! \return None.
  732. //
  733. //*****************************************************************************
  734. void
  735. ADCSequenceUnderflowClear(uint32_t ui32Base, uint32_t ui32SequenceNum)
  736. {
  737. //
  738. // Check the arguments.
  739. //
  740. ASSERT((ui32Base == ADC0_BASE) || (ui32Base == ADC1_BASE));
  741. ASSERT(ui32SequenceNum < 4);
  742. //
  743. // Clear the underflow condition for this sequence.
  744. //
  745. HWREG(ui32Base + ADC_O_USTAT) = 1 << ui32SequenceNum;
  746. }
  747. //*****************************************************************************
  748. //
  749. //! Gets the captured data for a sample sequence.
  750. //!
  751. //! \param ui32Base is the base address of the ADC module.
  752. //! \param ui32SequenceNum is the sample sequence number.
  753. //! \param pui32Buffer is the address where the data is stored.
  754. //!
  755. //! This function copies data from the specified sample sequencer output FIFO
  756. //! to a memory resident buffer. The number of samples available in the
  757. //! hardware FIFO are copied into the buffer, which is assumed to be large
  758. //! enough to hold that many samples. This function only returns the samples
  759. //! that are presently available, which may not be the entire sample sequence
  760. //! if it is in the process of being executed.
  761. //!
  762. //! \return Returns the number of samples copied to the buffer.
  763. //
  764. //*****************************************************************************
  765. int32_t
  766. ADCSequenceDataGet(uint32_t ui32Base, uint32_t ui32SequenceNum,
  767. uint32_t *pui32Buffer)
  768. {
  769. uint32_t ui32Count;
  770. //
  771. // Check the arguments.
  772. //
  773. ASSERT((ui32Base == ADC0_BASE) || (ui32Base == ADC1_BASE));
  774. ASSERT(ui32SequenceNum < 4);
  775. //
  776. // Get the offset of the sequence to be read.
  777. //
  778. ui32Base += ADC_SEQ + (ADC_SEQ_STEP * ui32SequenceNum);
  779. //
  780. // Read samples from the FIFO until it is empty.
  781. //
  782. ui32Count = 0;
  783. while(!(HWREG(ui32Base + ADC_SSFSTAT) & ADC_SSFSTAT0_EMPTY) &&
  784. (ui32Count < 8))
  785. {
  786. //
  787. // Read the FIFO and copy it to the destination.
  788. //
  789. *pui32Buffer++ = HWREG(ui32Base + ADC_SSFIFO);
  790. //
  791. // Increment the count of samples read.
  792. //
  793. ui32Count++;
  794. }
  795. //
  796. // Return the number of samples read.
  797. //
  798. return(ui32Count);
  799. }
  800. //*****************************************************************************
  801. //
  802. //! Causes a processor trigger for a sample sequence.
  803. //!
  804. //! \param ui32Base is the base address of the ADC module.
  805. //! \param ui32SequenceNum is the sample sequence number, with
  806. //! \b ADC_TRIGGER_WAIT or \b ADC_TRIGGER_SIGNAL optionally ORed into it.
  807. //!
  808. //! This function triggers a processor-initiated sample sequence if the sample
  809. //! sequence trigger is configured to \b ADC_TRIGGER_PROCESSOR. If
  810. //! \b ADC_TRIGGER_WAIT is ORed into the sequence number, the
  811. //! processor-initiated trigger is delayed until a later processor-initiated
  812. //! trigger to a different ADC module that specifies \b ADC_TRIGGER_SIGNAL,
  813. //! allowing multiple ADCs to start from a processor-initiated trigger in a
  814. //! synchronous manner.
  815. //!
  816. //! \return None.
  817. //
  818. //*****************************************************************************
  819. void
  820. ADCProcessorTrigger(uint32_t ui32Base, uint32_t ui32SequenceNum)
  821. {
  822. //
  823. // Check the arguments.
  824. //
  825. ASSERT((ui32Base == ADC0_BASE) || (ui32Base == ADC1_BASE));
  826. ASSERT(ui32SequenceNum < 4);
  827. //
  828. // Generate a processor trigger for this sample sequence.
  829. //
  830. HWREG(ui32Base + ADC_O_PSSI) |= ((ui32SequenceNum & 0xffff0000) |
  831. (1 << (ui32SequenceNum & 0xf)));
  832. }
  833. //*****************************************************************************
  834. //
  835. //! Configures the software oversampling factor of the ADC.
  836. //!
  837. //! \param ui32Base is the base address of the ADC module.
  838. //! \param ui32SequenceNum is the sample sequence number.
  839. //! \param ui32Factor is the number of samples to be averaged.
  840. //!
  841. //! This function configures the software oversampling for the ADC, which can
  842. //! be used to provide better resolution on the sampled data. Oversampling is
  843. //! accomplished by averaging multiple samples from the same analog input.
  844. //! Three different oversampling rates are supported; 2x, 4x, and 8x.
  845. //!
  846. //! Oversampling is only supported on the sample sequencers that are more than
  847. //! one sample in depth (that is, the fourth sample sequencer is not
  848. //! supported). Oversampling by 2x (for example) divides the depth of the
  849. //! sample sequencer by two; so 2x oversampling on the first sample sequencer
  850. //! can only provide four samples per trigger. This also means that 8x
  851. //! oversampling is only available on the first sample sequencer.
  852. //!
  853. //! \return None.
  854. //
  855. //*****************************************************************************
  856. void
  857. ADCSoftwareOversampleConfigure(uint32_t ui32Base, uint32_t ui32SequenceNum,
  858. uint32_t ui32Factor)
  859. {
  860. uint32_t ui32Value;
  861. //
  862. // Check the arguments.
  863. //
  864. ASSERT((ui32Base == ADC0_BASE) || (ui32Base == ADC1_BASE));
  865. ASSERT(ui32SequenceNum < 3);
  866. ASSERT(((ui32Factor == 2) || (ui32Factor == 4) || (ui32Factor == 8)) &&
  867. ((ui32SequenceNum == 0) || (ui32Factor != 8)));
  868. //
  869. // Convert the oversampling factor to a shift factor.
  870. //
  871. for(ui32Value = 0, ui32Factor >>= 1; ui32Factor;
  872. ui32Value++, ui32Factor >>= 1)
  873. {
  874. }
  875. //
  876. // Save the shift factor.
  877. //
  878. g_pui8OversampleFactor[ui32SequenceNum] = ui32Value;
  879. }
  880. //*****************************************************************************
  881. //
  882. //! Configures a step of the software oversampled sequencer.
  883. //!
  884. //! \param ui32Base is the base address of the ADC module.
  885. //! \param ui32SequenceNum is the sample sequence number.
  886. //! \param ui32Step is the step to be configured.
  887. //! \param ui32Config is the configuration of this step.
  888. //!
  889. //! This function configures a step of the sample sequencer when using the
  890. //! software oversampling feature. The number of steps available depends on
  891. //! the oversampling factor set by ADCSoftwareOversampleConfigure(). The value
  892. //! of \e ui32Config is the same as defined for ADCSequenceStepConfigure().
  893. //!
  894. //! \return None.
  895. //
  896. //*****************************************************************************
  897. void
  898. ADCSoftwareOversampleStepConfigure(uint32_t ui32Base, uint32_t ui32SequenceNum,
  899. uint32_t ui32Step, uint32_t ui32Config)
  900. {
  901. //
  902. // Check the arguments.
  903. //
  904. ASSERT((ui32Base == ADC0_BASE) || (ui32Base == ADC1_BASE));
  905. ASSERT(ui32SequenceNum < 3);
  906. ASSERT(((ui32SequenceNum == 0) &&
  907. (ui32Step < (8 >> g_pui8OversampleFactor[ui32SequenceNum]))) ||
  908. (ui32Step < (4 >> g_pui8OversampleFactor[ui32SequenceNum])));
  909. //
  910. // Get the offset of the sequence to be configured.
  911. //
  912. ui32Base += ADC_SEQ + (ADC_SEQ_STEP * ui32SequenceNum);
  913. //
  914. // Compute the shift for the bits that control this step.
  915. //
  916. ui32Step *= 4 << g_pui8OversampleFactor[ui32SequenceNum];
  917. //
  918. // Loop through the hardware steps that make up this step of the software
  919. // oversampled sequence.
  920. //
  921. for(ui32SequenceNum = 1 << g_pui8OversampleFactor[ui32SequenceNum];
  922. ui32SequenceNum; ui32SequenceNum--)
  923. {
  924. //
  925. // Set the analog mux value for this step.
  926. //
  927. HWREG(ui32Base + ADC_SSMUX) = ((HWREG(ui32Base + ADC_SSMUX) &
  928. ~(0x0000000f << ui32Step)) |
  929. ((ui32Config & 0x0f) << ui32Step));
  930. //
  931. // Set the upper bits of the analog mux value for this step.
  932. //
  933. HWREG(ui32Base + ADC_SSEMUX) = ((HWREG(ui32Base + ADC_SSEMUX) &
  934. ~(0x0000000f << ui32Step)) |
  935. (((ui32Config & 0xf00) >> 8) <<
  936. ui32Step));
  937. //
  938. // Set the control value for this step.
  939. //
  940. HWREG(ui32Base + ADC_SSCTL) = ((HWREG(ui32Base + ADC_SSCTL) &
  941. ~(0x0000000f << ui32Step)) |
  942. (((ui32Config & 0xf0) >> 4) <<
  943. ui32Step));
  944. if(ui32SequenceNum != 1)
  945. {
  946. HWREG(ui32Base + ADC_SSCTL) &= ~((ADC_SSCTL0_IE0 |
  947. ADC_SSCTL0_END0) << ui32Step);
  948. }
  949. //
  950. // Go to the next hardware step.
  951. //
  952. ui32Step += 4;
  953. }
  954. }
  955. //*****************************************************************************
  956. //
  957. //! Gets the captured data for a sample sequence using software oversampling.
  958. //!
  959. //! \param ui32Base is the base address of the ADC module.
  960. //! \param ui32SequenceNum is the sample sequence number.
  961. //! \param pui32Buffer is the address where the data is stored.
  962. //! \param ui32Count is the number of samples to be read.
  963. //!
  964. //! This function copies data from the specified sample sequence output FIFO to
  965. //! a memory resident buffer with software oversampling applied. The requested
  966. //! number of samples are copied into the data buffer; if there are not enough
  967. //! samples in the hardware FIFO to satisfy this many oversampled data items,
  968. //! then incorrect results are returned. It is the caller's responsibility to
  969. //! read only the samples that are available and wait until enough data is
  970. //! available, for example as a result of receiving an interrupt.
  971. //!
  972. //! \return None.
  973. //
  974. //*****************************************************************************
  975. void
  976. ADCSoftwareOversampleDataGet(uint32_t ui32Base, uint32_t ui32SequenceNum,
  977. uint32_t *pui32Buffer, uint32_t ui32Count)
  978. {
  979. uint32_t ui32Idx, ui32Accum;
  980. //
  981. // Check the arguments.
  982. //
  983. ASSERT((ui32Base == ADC0_BASE) || (ui32Base == ADC1_BASE));
  984. ASSERT(ui32SequenceNum < 3);
  985. ASSERT(((ui32SequenceNum == 0) &&
  986. (ui32Count < (8 >> g_pui8OversampleFactor[ui32SequenceNum]))) ||
  987. (ui32Count < (4 >> g_pui8OversampleFactor[ui32SequenceNum])));
  988. //
  989. // Get the offset of the sequence to be read.
  990. //
  991. ui32Base += ADC_SEQ + (ADC_SEQ_STEP * ui32SequenceNum);
  992. //
  993. // Read the samples from the FIFO until it is empty.
  994. //
  995. while(ui32Count--)
  996. {
  997. //
  998. // Compute the sum of the samples.
  999. //
  1000. ui32Accum = 0;
  1001. for(ui32Idx = 1 << g_pui8OversampleFactor[ui32SequenceNum]; ui32Idx;
  1002. ui32Idx--)
  1003. {
  1004. //
  1005. // Read the FIFO and add it to the accumulator.
  1006. //
  1007. ui32Accum += HWREG(ui32Base + ADC_SSFIFO);
  1008. }
  1009. //
  1010. // Write the averaged sample to the output buffer.
  1011. //
  1012. *pui32Buffer++ = ui32Accum >> g_pui8OversampleFactor[ui32SequenceNum];
  1013. }
  1014. }
  1015. //*****************************************************************************
  1016. //
  1017. //! Configures the hardware oversampling factor of the ADC.
  1018. //!
  1019. //! \param ui32Base is the base address of the ADC module.
  1020. //! \param ui32Factor is the number of samples to be averaged.
  1021. //!
  1022. //! This function configures the hardware oversampling for the ADC, which can
  1023. //! be used to provide better resolution on the sampled data. Oversampling is
  1024. //! accomplished by averaging multiple samples from the same analog input. Six
  1025. //! different oversampling rates are supported; 2x, 4x, 8x, 16x, 32x, and 64x.
  1026. //! Specifying an oversampling factor of zero disables hardware
  1027. //! oversampling.
  1028. //!
  1029. //! Hardware oversampling applies uniformly to all sample sequencers. It does
  1030. //! not reduce the depth of the sample sequencers like the software
  1031. //! oversampling APIs; each sample written into the sample sequencer FIFO is a
  1032. //! fully oversampled analog input reading.
  1033. //!
  1034. //! Enabling hardware averaging increases the precision of the ADC at the cost
  1035. //! of throughput. For example, enabling 4x oversampling reduces the
  1036. //! throughput of a 250 k samples/second ADC to 62.5 k samples/second.
  1037. //!
  1038. //! \return None.
  1039. //
  1040. //*****************************************************************************
  1041. void
  1042. ADCHardwareOversampleConfigure(uint32_t ui32Base, uint32_t ui32Factor)
  1043. {
  1044. uint32_t ui32Value;
  1045. //
  1046. // Check the arguments.
  1047. //
  1048. ASSERT((ui32Base == ADC0_BASE) || (ui32Base == ADC1_BASE));
  1049. ASSERT(((ui32Factor == 0) || (ui32Factor == 2) || (ui32Factor == 4) ||
  1050. (ui32Factor == 8) || (ui32Factor == 16) || (ui32Factor == 32) ||
  1051. (ui32Factor == 64)));
  1052. //
  1053. // Convert the oversampling factor to a shift factor.
  1054. //
  1055. for(ui32Value = 0, ui32Factor >>= 1; ui32Factor;
  1056. ui32Value++, ui32Factor >>= 1)
  1057. {
  1058. }
  1059. //
  1060. // Write the shift factor to the ADC to configure the hardware oversampler.
  1061. //
  1062. HWREG(ui32Base + ADC_O_SAC) = ui32Value;
  1063. }
  1064. //*****************************************************************************
  1065. //
  1066. //! Configures an ADC digital comparator.
  1067. //!
  1068. //! \param ui32Base is the base address of the ADC module.
  1069. //! \param ui32Comp is the index of the comparator to configure.
  1070. //! \param ui32Config is the configuration of the comparator.
  1071. //!
  1072. //! This function configures a comparator. The \e ui32Config parameter is
  1073. //! the result of a logical OR operation between the \b ADC_COMP_TRIG_xxx, and
  1074. //! \b ADC_COMP_INT_xxx values.
  1075. //!
  1076. //! The \b ADC_COMP_TRIG_xxx term can take on the following values:
  1077. //!
  1078. //! - \b ADC_COMP_TRIG_NONE to never trigger PWM fault condition.
  1079. //! - \b ADC_COMP_TRIG_LOW_ALWAYS to always trigger PWM fault condition when
  1080. //! ADC output is in the low-band.
  1081. //! - \b ADC_COMP_TRIG_LOW_ONCE to trigger PWM fault condition once when ADC
  1082. //! output transitions into the low-band.
  1083. //! - \b ADC_COMP_TRIG_LOW_HALWAYS to always trigger PWM fault condition when
  1084. //! ADC output is in the low-band only if ADC output has been in the high-band
  1085. //! since the last trigger output.
  1086. //! - \b ADC_COMP_TRIG_LOW_HONCE to trigger PWM fault condition once when ADC
  1087. //! output transitions into low-band only if ADC output has been in the
  1088. //! high-band since the last trigger output.
  1089. //! - \b ADC_COMP_TRIG_MID_ALWAYS to always trigger PWM fault condition when
  1090. //! ADC output is in the mid-band.
  1091. //! - \b ADC_COMP_TRIG_MID_ONCE to trigger PWM fault condition once when ADC
  1092. //! output transitions into the mid-band.
  1093. //! - \b ADC_COMP_TRIG_HIGH_ALWAYS to always trigger PWM fault condition when
  1094. //! ADC output is in the high-band.
  1095. //! - \b ADC_COMP_TRIG_HIGH_ONCE to trigger PWM fault condition once when ADC
  1096. //! output transitions into the high-band.
  1097. //! - \b ADC_COMP_TRIG_HIGH_HALWAYS to always trigger PWM fault condition when
  1098. //! ADC output is in the high-band only if ADC output has been in the low-band
  1099. //! since the last trigger output.
  1100. //! - \b ADC_COMP_TRIG_HIGH_HONCE to trigger PWM fault condition once when ADC
  1101. //! output transitions into high-band only if ADC output has been in the
  1102. //! low-band since the last trigger output.
  1103. //!
  1104. //! The \b ADC_COMP_INT_xxx term can take on the following values:
  1105. //!
  1106. //! - \b ADC_COMP_INT_NONE to never generate ADC interrupt.
  1107. //! - \b ADC_COMP_INT_LOW_ALWAYS to always generate ADC interrupt when ADC
  1108. //! output is in the low-band.
  1109. //! - \b ADC_COMP_INT_LOW_ONCE to generate ADC interrupt once when ADC output
  1110. //! transitions into the low-band.
  1111. //! - \b ADC_COMP_INT_LOW_HALWAYS to always generate ADC interrupt when ADC
  1112. //! output is in the low-band only if ADC output has been in the high-band
  1113. //! since the last trigger output.
  1114. //! - \b ADC_COMP_INT_LOW_HONCE to generate ADC interrupt once when ADC output
  1115. //! transitions into low-band only if ADC output has been in the high-band
  1116. //! since the last trigger output.
  1117. //! - \b ADC_COMP_INT_MID_ALWAYS to always generate ADC interrupt when ADC
  1118. //! output is in the mid-band.
  1119. //! - \b ADC_COMP_INT_MID_ONCE to generate ADC interrupt once when ADC output
  1120. //! transitions into the mid-band.
  1121. //! - \b ADC_COMP_INT_HIGH_ALWAYS to always generate ADC interrupt when ADC
  1122. //! output is in the high-band.
  1123. //! - \b ADC_COMP_INT_HIGH_ONCE to generate ADC interrupt once when ADC output
  1124. //! transitions into the high-band.
  1125. //! - \b ADC_COMP_INT_HIGH_HALWAYS to always generate ADC interrupt when ADC
  1126. //! output is in the high-band only if ADC output has been in the low-band
  1127. //! since the last trigger output.
  1128. //! - \b ADC_COMP_INT_HIGH_HONCE to generate ADC interrupt once when ADC output
  1129. //! transitions into high-band only if ADC output has been in the low-band
  1130. //! since the last trigger output.
  1131. //!
  1132. //! \return None.
  1133. //
  1134. //*****************************************************************************
  1135. void
  1136. ADCComparatorConfigure(uint32_t ui32Base, uint32_t ui32Comp,
  1137. uint32_t ui32Config)
  1138. {
  1139. //
  1140. // Check the arguments.
  1141. //
  1142. ASSERT((ui32Base == ADC0_BASE) || (ui32Base == ADC1_BASE));
  1143. ASSERT(ui32Comp < 8);
  1144. //
  1145. // Save the new setting.
  1146. //
  1147. HWREG(ui32Base + ADC_O_DCCTL0 + (ui32Comp * 4)) = ui32Config;
  1148. }
  1149. //*****************************************************************************
  1150. //
  1151. //! Defines the ADC digital comparator regions.
  1152. //!
  1153. //! \param ui32Base is the base address of the ADC module.
  1154. //! \param ui32Comp is the index of the comparator to configure.
  1155. //! \param ui32LowRef is the reference point for the low/mid band threshold.
  1156. //! \param ui32HighRef is the reference point for the mid/high band threshold.
  1157. //!
  1158. //! The ADC digital comparator operation is based on three ADC value regions:
  1159. //! - \b low-band is defined as any ADC value less than or equal to the
  1160. //! \e ui32LowRef value.
  1161. //! - \b mid-band is defined as any ADC value greater than the \e ui32LowRef
  1162. //! value but less than or equal to the \e ui32HighRef value.
  1163. //! - \b high-band is defined as any ADC value greater than the \e ui32HighRef
  1164. //! value.
  1165. //!
  1166. //! \return None.
  1167. //
  1168. //*****************************************************************************
  1169. void
  1170. ADCComparatorRegionSet(uint32_t ui32Base, uint32_t ui32Comp,
  1171. uint32_t ui32LowRef, uint32_t ui32HighRef)
  1172. {
  1173. //
  1174. // Check the arguments.
  1175. //
  1176. ASSERT((ui32Base == ADC0_BASE) || (ui32Base == ADC1_BASE));
  1177. ASSERT(ui32Comp < 8);
  1178. ASSERT((ui32LowRef < 4096) && (ui32LowRef <= ui32HighRef));
  1179. ASSERT(ui32HighRef < 4096);
  1180. //
  1181. // Save the new region settings.
  1182. //
  1183. HWREG(ui32Base + ADC_O_DCCMP0 + (ui32Comp * 4)) = ((ui32HighRef << 16) |
  1184. ui32LowRef);
  1185. }
  1186. //*****************************************************************************
  1187. //
  1188. //! Resets the current ADC digital comparator conditions.
  1189. //!
  1190. //! \param ui32Base is the base address of the ADC module.
  1191. //! \param ui32Comp is the index of the comparator.
  1192. //! \param bTrigger is the flag to indicate reset of Trigger conditions.
  1193. //! \param bInterrupt is the flag to indicate reset of Interrupt conditions.
  1194. //!
  1195. //! Because the digital comparator uses current and previous ADC values, this
  1196. //! function allows the comparator to be reset to its initial
  1197. //! value to prevent stale data from being used when a sequence is enabled.
  1198. //!
  1199. //! \return None.
  1200. //
  1201. //*****************************************************************************
  1202. void
  1203. ADCComparatorReset(uint32_t ui32Base, uint32_t ui32Comp, bool bTrigger,
  1204. bool bInterrupt)
  1205. {
  1206. uint32_t ui32Temp;
  1207. //
  1208. // Check the arguments.
  1209. //
  1210. ASSERT((ui32Base == ADC0_BASE) || (ui32Base == ADC1_BASE));
  1211. ASSERT(ui32Comp < 8);
  1212. //
  1213. // Set the appropriate bits to reset the trigger and/or interrupt
  1214. // comparator conditions.
  1215. //
  1216. ui32Temp = 0;
  1217. if(bTrigger)
  1218. {
  1219. ui32Temp |= (1 << (16 + ui32Comp));
  1220. }
  1221. if(bInterrupt)
  1222. {
  1223. ui32Temp |= (1 << ui32Comp);
  1224. }
  1225. HWREG(ui32Base + ADC_O_DCRIC) = ui32Temp;
  1226. }
  1227. //*****************************************************************************
  1228. //
  1229. //! Disables a sample sequence comparator interrupt.
  1230. //!
  1231. //! \param ui32Base is the base address of the ADC module.
  1232. //! \param ui32SequenceNum is the sample sequence number.
  1233. //!
  1234. //! This function disables the requested sample sequence comparator interrupt.
  1235. //!
  1236. //! \return None.
  1237. //
  1238. //*****************************************************************************
  1239. void
  1240. ADCComparatorIntDisable(uint32_t ui32Base, uint32_t ui32SequenceNum)
  1241. {
  1242. //
  1243. // Check the arguments.
  1244. //
  1245. ASSERT((ui32Base == ADC0_BASE) || (ui32Base == ADC1_BASE));
  1246. ASSERT(ui32SequenceNum < 4);
  1247. //
  1248. // Disable this sample sequence comparator interrupt.
  1249. //
  1250. HWREG(ui32Base + ADC_O_IM) &= ~(0x10000 << ui32SequenceNum);
  1251. }
  1252. //*****************************************************************************
  1253. //
  1254. //! Enables a sample sequence comparator interrupt.
  1255. //!
  1256. //! \param ui32Base is the base address of the ADC module.
  1257. //! \param ui32SequenceNum is the sample sequence number.
  1258. //!
  1259. //! This function enables the requested sample sequence comparator interrupt.
  1260. //!
  1261. //! \return None.
  1262. //
  1263. //*****************************************************************************
  1264. void
  1265. ADCComparatorIntEnable(uint32_t ui32Base, uint32_t ui32SequenceNum)
  1266. {
  1267. //
  1268. // Check the arguments.
  1269. //
  1270. ASSERT((ui32Base == ADC0_BASE) || (ui32Base == ADC1_BASE));
  1271. ASSERT(ui32SequenceNum < 4);
  1272. //
  1273. // Enable this sample sequence interrupt.
  1274. //
  1275. HWREG(ui32Base + ADC_O_IM) |= 0x10000 << ui32SequenceNum;
  1276. }
  1277. //*****************************************************************************
  1278. //
  1279. //! Gets the current comparator interrupt status.
  1280. //!
  1281. //! \param ui32Base is the base address of the ADC module.
  1282. //!
  1283. //! This function returns the digital comparator interrupt status bits. This
  1284. //! status is sequence agnostic.
  1285. //!
  1286. //! \return The current comparator interrupt status.
  1287. //
  1288. //*****************************************************************************
  1289. uint32_t
  1290. ADCComparatorIntStatus(uint32_t ui32Base)
  1291. {
  1292. //
  1293. // Check the arguments.
  1294. //
  1295. ASSERT((ui32Base == ADC0_BASE) || (ui32Base == ADC1_BASE));
  1296. //
  1297. // Return the digital comparator interrupt status.
  1298. //
  1299. return(HWREG(ui32Base + ADC_O_DCISC));
  1300. }
  1301. //*****************************************************************************
  1302. //
  1303. //! Clears sample sequence comparator interrupt source.
  1304. //!
  1305. //! \param ui32Base is the base address of the ADC module.
  1306. //! \param ui32Status is the bit-mapped interrupts status to clear.
  1307. //!
  1308. //! The specified interrupt status is cleared.
  1309. //!
  1310. //! \return None.
  1311. //
  1312. //*****************************************************************************
  1313. void
  1314. ADCComparatorIntClear(uint32_t ui32Base, uint32_t ui32Status)
  1315. {
  1316. //
  1317. // Check the arguments.
  1318. //
  1319. ASSERT((ui32Base == ADC0_BASE) || (ui32Base == ADC1_BASE));
  1320. //
  1321. // Clear the interrupt.
  1322. //
  1323. HWREG(ui32Base + ADC_O_DCISC) = ui32Status;
  1324. }
  1325. //*****************************************************************************
  1326. //
  1327. //! Disables ADC interrupt sources.
  1328. //!
  1329. //! \param ui32Base is the base address of the ADC module.
  1330. //! \param ui32IntFlags is the bit mask of the interrupt sources to disable.
  1331. //!
  1332. //! This function disables the indicated ADC interrupt sources. Only the
  1333. //! sources that are enabled can be reflected to the processor interrupt;
  1334. //! disabled sources have no effect on the processor.
  1335. //!
  1336. //! The \e ui32IntFlags parameter is the logical OR of any of the following:
  1337. //!
  1338. //! - \b ADC_INT_SS0 - interrupt due to ADC sample sequence 0.
  1339. //! - \b ADC_INT_SS1 - interrupt due to ADC sample sequence 1.
  1340. //! - \b ADC_INT_SS2 - interrupt due to ADC sample sequence 2.
  1341. //! - \b ADC_INT_SS3 - interrupt due to ADC sample sequence 3.
  1342. //! - \b ADC_INT_DMA_SS0 - interrupt due to DMA on ADC sample sequence 0.
  1343. //! - \b ADC_INT_DMA_SS1 - interrupt due to DMA on ADC sample sequence 1.
  1344. //! - \b ADC_INT_DMA_SS2 - interrupt due to DMA on ADC sample sequence 2.
  1345. //! - \b ADC_INT_DMA_SS3 - interrupt due to DMA on ADC sample sequence 3.
  1346. //! - \b ADC_INT_DCON_SS0 - interrupt due to digital comparator on ADC sample
  1347. //! sequence 0.
  1348. //! - \b ADC_INT_DCON_SS1 - interrupt due to digital comparator on ADC sample
  1349. //! sequence 1.
  1350. //! - \b ADC_INT_DCON_SS2 - interrupt due to digital comparator on ADC sample
  1351. //! sequence 2.
  1352. //! - \b ADC_INT_DCON_SS3 - interrupt due to digital comparator on ADC sample
  1353. //! sequence 3.
  1354. //!
  1355. //! \return None.
  1356. //
  1357. //*****************************************************************************
  1358. void
  1359. ADCIntDisableEx(uint32_t ui32Base, uint32_t ui32IntFlags)
  1360. {
  1361. //
  1362. // Check the arguments.
  1363. //
  1364. ASSERT((ui32Base == ADC0_BASE) || (ui32Base == ADC1_BASE));
  1365. //
  1366. // Disable the requested interrupts.
  1367. //
  1368. HWREG(ui32Base + ADC_O_IM) &= ~ui32IntFlags;
  1369. }
  1370. //*****************************************************************************
  1371. //
  1372. //! Enables ADC interrupt sources.
  1373. //!
  1374. //! \param ui32Base is the base address of the ADC module.
  1375. //! \param ui32IntFlags is the bit mask of the interrupt sources to disable.
  1376. //!
  1377. //! This function enables the indicated ADC interrupt sources. Only the
  1378. //! sources that are enabled can be reflected to the processor interrupt;
  1379. //! disabled sources have no effect on the processor.
  1380. //!
  1381. //! The \e ui32IntFlags parameter is the logical OR of any of the following:
  1382. //!
  1383. //! - \b ADC_INT_SS0 - interrupt due to ADC sample sequence 0.
  1384. //! - \b ADC_INT_SS1 - interrupt due to ADC sample sequence 1.
  1385. //! - \b ADC_INT_SS2 - interrupt due to ADC sample sequence 2.
  1386. //! - \b ADC_INT_SS3 - interrupt due to ADC sample sequence 3.
  1387. //! - \b ADC_INT_DMA_SS0 - interrupt due to DMA on ADC sample sequence 0.
  1388. //! - \b ADC_INT_DMA_SS1 - interrupt due to DMA on ADC sample sequence 1.
  1389. //! - \b ADC_INT_DMA_SS2 - interrupt due to DMA on ADC sample sequence 2.
  1390. //! - \b ADC_INT_DMA_SS3 - interrupt due to DMA on ADC sample sequence 3.
  1391. //! - \b ADC_INT_DCON_SS0 - interrupt due to digital comparator on ADC sample
  1392. //! sequence 0.
  1393. //! - \b ADC_INT_DCON_SS1 - interrupt due to digital comparator on ADC sample
  1394. //! sequence 1.
  1395. //! - \b ADC_INT_DCON_SS2 - interrupt due to digital comparator on ADC sample
  1396. //! sequence 2.
  1397. //! - \b ADC_INT_DCON_SS3 - interrupt due to digital comparator on ADC sample
  1398. //! sequence 3.
  1399. //!
  1400. //! \return None.
  1401. //
  1402. //*****************************************************************************
  1403. void
  1404. ADCIntEnableEx(uint32_t ui32Base, uint32_t ui32IntFlags)
  1405. {
  1406. //
  1407. // Check the arguments.
  1408. //
  1409. ASSERT((ui32Base == ADC0_BASE) || (ui32Base == ADC1_BASE));
  1410. //
  1411. // Enable the requested interrupts.
  1412. //
  1413. HWREG(ui32Base + ADC_O_IM) |= ui32IntFlags;
  1414. }
  1415. //*****************************************************************************
  1416. //
  1417. //! Gets interrupt status for the specified ADC module.
  1418. //!
  1419. //! \param ui32Base is the base address of the ADC module.
  1420. //! \param bMasked specifies whether masked or raw interrupt status is
  1421. //! returned.
  1422. //!
  1423. //! If \e bMasked is set as \b true, then the masked interrupt status is
  1424. //! returned; otherwise, the raw interrupt status is returned.
  1425. //!
  1426. //! \return Returns the current interrupt status for the specified ADC module.
  1427. //! The value returned is the logical OR of the \b ADC_INT_* values that are
  1428. //! currently active.
  1429. //
  1430. //*****************************************************************************
  1431. uint32_t
  1432. ADCIntStatusEx(uint32_t ui32Base, bool bMasked)
  1433. {
  1434. uint32_t ui32Temp;
  1435. //
  1436. // Check the arguments.
  1437. //
  1438. ASSERT((ui32Base == ADC0_BASE) || (ui32Base == ADC1_BASE));
  1439. //
  1440. // Return either the masked interrupt status or the raw interrupt status as
  1441. // requested.
  1442. //
  1443. if(bMasked)
  1444. {
  1445. ui32Temp = HWREG(ui32Base + ADC_O_ISC);
  1446. }
  1447. else
  1448. {
  1449. //
  1450. // Read the Raw interrupt status to see if a digital comparator
  1451. // interrupt is active.
  1452. //
  1453. ui32Temp = HWREG(ui32Base + ADC_O_RIS);
  1454. //
  1455. // Since, the raw interrupt status only indicates that any one of the
  1456. // digital comparators caused an interrupt, if the raw interrupt status
  1457. // is set then the return value is modified to indicate that all sample
  1458. // sequences have a pending digital comparator interrupt.
  1459. // This is exactly how the hardware works so the return code is
  1460. // modified to match this behavior.
  1461. //
  1462. if(ui32Temp & ADC_RIS_INRDC)
  1463. {
  1464. ui32Temp |= (ADC_INT_DCON_SS3 | ADC_INT_DCON_SS2 |
  1465. ADC_INT_DCON_SS1 | ADC_INT_DCON_SS0);
  1466. }
  1467. }
  1468. return(ui32Temp);
  1469. }
  1470. //*****************************************************************************
  1471. //
  1472. //! Clears the specified ADC interrupt sources.
  1473. //!
  1474. //! \param ui32Base is the base address of the ADC port.
  1475. //! \param ui32IntFlags is the bit mask of the interrupt sources to disable.
  1476. //!
  1477. //! Clears the interrupt for the specified interrupt source(s).
  1478. //!
  1479. //! The \e ui32IntFlags parameter is the logical OR of the \b ADC_INT_* values.
  1480. //! See the ADCIntEnableEx() function for the list of possible \b ADC_INT*
  1481. //! values.
  1482. //!
  1483. //! \note Because there is a write buffer in the Cortex-M processor, it may
  1484. //! take several clock cycles before the interrupt source is actually cleared.
  1485. //! Therefore, it is recommended that the interrupt source be cleared early in
  1486. //! the interrupt handler (as opposed to the very last action) to avoid
  1487. //! returning from the interrupt handler before the interrupt source is
  1488. //! actually cleared. Failure to do so may result in the interrupt handler
  1489. //! being immediately reentered (because the interrupt controller still sees
  1490. //! the interrupt source asserted).
  1491. //!
  1492. //! \return None.
  1493. //
  1494. //*****************************************************************************
  1495. void
  1496. ADCIntClearEx(uint32_t ui32Base, uint32_t ui32IntFlags)
  1497. {
  1498. HWREG(ui32Base + ADC_O_ISC) |= ui32IntFlags;
  1499. }
  1500. //*****************************************************************************
  1501. //
  1502. //! Selects the ADC reference.
  1503. //!
  1504. //! \param ui32Base is the base address of the ADC module.
  1505. //! \param ui32Ref is the reference to use.
  1506. //!
  1507. //! The ADC reference is set as specified by \e ui32Ref. It must be one of
  1508. //! \b ADC_REF_INT, \b ADC_REF_EXT_3V, or \b ADC_REF_EXT_1V for internal or
  1509. //! external reference. If \b ADC_REF_INT is chosen, then an internal 3V
  1510. //! reference is used and no external reference is needed. If
  1511. //! \b ADC_REF_EXT_3V is chosen, then a 3V reference must be supplied to the
  1512. //! AVREF pin. If \b ADC_REF_EXT_1V is chosen, then a 1V external reference
  1513. //! must be supplied to the AVREF pin.
  1514. //!
  1515. //! \note The ADC reference can only be selected on parts that have an external
  1516. //! reference. Consult the data sheet for your part to determine if there is
  1517. //! an external reference.
  1518. //!
  1519. //! \return None.
  1520. //
  1521. //*****************************************************************************
  1522. void
  1523. ADCReferenceSet(uint32_t ui32Base, uint32_t ui32Ref)
  1524. {
  1525. //
  1526. // Check the arguments.
  1527. //
  1528. ASSERT((ui32Base == ADC0_BASE) || (ui32Base == ADC1_BASE));
  1529. ASSERT((ui32Ref == ADC_REF_INT) || (ui32Ref == ADC_REF_EXT_3V) ||
  1530. (ui32Ref == ADC_REF_EXT_1V));
  1531. //
  1532. // Set the reference.
  1533. //
  1534. HWREG(ui32Base + ADC_O_CTL) =
  1535. (HWREG(ui32Base + ADC_O_CTL) & ~ADC_CTL_VREF_M) | ui32Ref;
  1536. }
  1537. //*****************************************************************************
  1538. //
  1539. //! Returns the current setting of the ADC reference.
  1540. //!
  1541. //! \param ui32Base is the base address of the ADC module.
  1542. //!
  1543. //! Returns the value of the ADC reference setting. The returned value is one
  1544. //! of \b ADC_REF_INT, \b ADC_REF_EXT_3V, or \b ADC_REF_EXT_1V.
  1545. //!
  1546. //! \note The value returned by this function is only meaningful if used on a
  1547. //! part that is capable of using an external reference. Consult the data
  1548. //! sheet for your part to determine if it has an external reference input.
  1549. //!
  1550. //! \return The current setting of the ADC reference.
  1551. //
  1552. //*****************************************************************************
  1553. uint32_t
  1554. ADCReferenceGet(uint32_t ui32Base)
  1555. {
  1556. //
  1557. // Check the arguments.
  1558. //
  1559. ASSERT((ui32Base == ADC0_BASE) || (ui32Base == ADC1_BASE));
  1560. //
  1561. // Return the value of the reference.
  1562. //
  1563. return(HWREG(ui32Base + ADC_O_CTL) & ADC_CTL_VREF_M);
  1564. }
  1565. //*****************************************************************************
  1566. //
  1567. //! Sets the phase delay between a trigger and the start of a sequence.
  1568. //!
  1569. //! \param ui32Base is the base address of the ADC module.
  1570. //! \param ui32Phase is the phase delay, specified as one of \b ADC_PHASE_0,
  1571. //! \b ADC_PHASE_22_5, \b ADC_PHASE_45, \b ADC_PHASE_67_5, \b ADC_PHASE_90,
  1572. //! \b ADC_PHASE_112_5, \b ADC_PHASE_135, \b ADC_PHASE_157_5, \b ADC_PHASE_180,
  1573. //! \b ADC_PHASE_202_5, \b ADC_PHASE_225, \b ADC_PHASE_247_5, \b ADC_PHASE_270,
  1574. //! \b ADC_PHASE_292_5, \b ADC_PHASE_315, or \b ADC_PHASE_337_5.
  1575. //!
  1576. //! This function sets the phase delay between the detection of an ADC trigger
  1577. //! event and the start of the sample sequence. By selecting a different phase
  1578. //! delay for a pair of ADC modules (such as \b ADC_PHASE_0 and
  1579. //! \b ADC_PHASE_180) and having each ADC module sample the same analog input,
  1580. //! it is possible to increase the sampling rate of the analog input (with
  1581. //! samples N, N+2, N+4, and so on, coming from the first ADC and samples N+1,
  1582. //! N+3, N+5, and so on, coming from the second ADC). The ADC module has a
  1583. //! single phase delay that is applied to all sample sequences within that
  1584. //! module.
  1585. //!
  1586. //! \note This capability is not available on all parts.
  1587. //!
  1588. //! \return None.
  1589. //
  1590. //*****************************************************************************
  1591. void
  1592. ADCPhaseDelaySet(uint32_t ui32Base, uint32_t ui32Phase)
  1593. {
  1594. //
  1595. // Check the arguments.
  1596. //
  1597. ASSERT((ui32Base == ADC0_BASE) || (ui32Base == ADC1_BASE));
  1598. ASSERT((ui32Phase == ADC_PHASE_0) || (ui32Phase == ADC_PHASE_22_5) ||
  1599. (ui32Phase == ADC_PHASE_45) || (ui32Phase == ADC_PHASE_67_5) ||
  1600. (ui32Phase == ADC_PHASE_90) || (ui32Phase == ADC_PHASE_112_5) ||
  1601. (ui32Phase == ADC_PHASE_135) || (ui32Phase == ADC_PHASE_157_5) ||
  1602. (ui32Phase == ADC_PHASE_180) || (ui32Phase == ADC_PHASE_202_5) ||
  1603. (ui32Phase == ADC_PHASE_225) || (ui32Phase == ADC_PHASE_247_5) ||
  1604. (ui32Phase == ADC_PHASE_270) || (ui32Phase == ADC_PHASE_292_5) ||
  1605. (ui32Phase == ADC_PHASE_315) || (ui32Phase == ADC_PHASE_337_5));
  1606. //
  1607. // Set the phase delay.
  1608. //
  1609. HWREG(ui32Base + ADC_O_SPC) = ui32Phase;
  1610. }
  1611. //*****************************************************************************
  1612. //
  1613. //! Gets the phase delay between a trigger and the start of a sequence.
  1614. //!
  1615. //! \param ui32Base is the base address of the ADC module.
  1616. //!
  1617. //! This function gets the current phase delay between the detection of an ADC
  1618. //! trigger event and the start of the sample sequence.
  1619. //!
  1620. //! \return Returns the phase delay, specified as one of \b ADC_PHASE_0,
  1621. //! \b ADC_PHASE_22_5, \b ADC_PHASE_45, \b ADC_PHASE_67_5, \b ADC_PHASE_90,
  1622. //! \b ADC_PHASE_112_5, \b ADC_PHASE_135, \b ADC_PHASE_157_5, \b ADC_PHASE_180,
  1623. //! \b ADC_PHASE_202_5, \b ADC_PHASE_225, \b ADC_PHASE_247_5, \b ADC_PHASE_270,
  1624. //! \b ADC_PHASE_292_5, \b ADC_PHASE_315, or \b ADC_PHASE_337_5.
  1625. //
  1626. //*****************************************************************************
  1627. uint32_t
  1628. ADCPhaseDelayGet(uint32_t ui32Base)
  1629. {
  1630. //
  1631. // Check the arguments.
  1632. //
  1633. ASSERT((ui32Base == ADC0_BASE) || (ui32Base == ADC1_BASE));
  1634. //
  1635. // Return the phase delay.
  1636. //
  1637. return(HWREG(ui32Base + ADC_O_SPC));
  1638. }
  1639. //*****************************************************************************
  1640. //
  1641. //! Enables DMA for sample sequencers.
  1642. //!
  1643. //! \param ui32Base is the base address of the ADC module.
  1644. //! \param ui32SequenceNum is the sample sequence number.
  1645. //!
  1646. //! Allows DMA requests to be generated based on the FIFO level of the sample
  1647. //! sequencer.
  1648. //!
  1649. //! \return None.
  1650. //
  1651. //*****************************************************************************
  1652. void
  1653. ADCSequenceDMAEnable(uint32_t ui32Base, uint32_t ui32SequenceNum)
  1654. {
  1655. //
  1656. // Check the arguments.
  1657. //
  1658. ASSERT((ui32Base == ADC0_BASE) || (ui32Base == ADC1_BASE));
  1659. ASSERT(ui32SequenceNum < 4);
  1660. //
  1661. // Enable the DMA on the specified sequencer.
  1662. //
  1663. HWREG(ui32Base + ADC_O_ACTSS) |= 0x100 << ui32SequenceNum;
  1664. }
  1665. //*****************************************************************************
  1666. //
  1667. //! Disables DMA for sample sequencers.
  1668. //!
  1669. //! \param ui32Base is the base address of the ADC module.
  1670. //! \param ui32SequenceNum is the sample sequence number.
  1671. //!
  1672. //! Prevents the specified sample sequencer from generating DMA requests.
  1673. //!
  1674. //! \return None.
  1675. //
  1676. //*****************************************************************************
  1677. void
  1678. ADCSequenceDMADisable(uint32_t ui32Base, uint32_t ui32SequenceNum)
  1679. {
  1680. //
  1681. // Check the arguments.
  1682. //
  1683. ASSERT((ui32Base == ADC0_BASE) || (ui32Base == ADC1_BASE));
  1684. ASSERT(ui32SequenceNum < 4);
  1685. //
  1686. // Disable the DMA on the specified sequencer.
  1687. //
  1688. HWREG(ui32Base + ADC_O_ACTSS) &= ~(0x100 << ui32SequenceNum);
  1689. }
  1690. //*****************************************************************************
  1691. //
  1692. //! Determines whether the ADC is busy or not.
  1693. //!
  1694. //! \param ui32Base is the base address of the ADC.
  1695. //!
  1696. //! This function allows the caller to determine whether or not the ADC is
  1697. //! currently sampling . If \b false is returned, then the ADC is not
  1698. //! sampling data.
  1699. //!
  1700. //! Use this function to detect that the ADC is finished sampling data before
  1701. //! putting the device into deep sleep. Before using this function, it is
  1702. //! highly recommended that the event trigger is changed to
  1703. //! \b ADC_TRIGGER_NEVER on all enabled sequencers to prevent the ADC from
  1704. //! starting after checking the busy status.
  1705. //!
  1706. //! \return Returns \b true if the ADC is sampling or \b false if all
  1707. //! samples are complete.
  1708. //
  1709. //*****************************************************************************
  1710. bool
  1711. ADCBusy(uint32_t ui32Base)
  1712. {
  1713. //
  1714. // Check the argument.
  1715. //
  1716. ASSERT((ui32Base == ADC0_BASE) || (ui32Base == ADC1_BASE));
  1717. //
  1718. // Determine if the ADC is busy.
  1719. //
  1720. return((HWREG(ui32Base + ADC_O_ACTSS) & ADC_ACTSS_BUSY) ? true : false);
  1721. }
  1722. //*****************************************************************************
  1723. //
  1724. //! Sets the clock configuration for the ADC.
  1725. //!
  1726. //! \param ui32Base is the base address of the ADC to configure, which must
  1727. //! always be \b ADC0_BASE.
  1728. //! \param ui32Config is a combination of the \b ADC_CLOCK_SRC_ and
  1729. //! \b ADC_CLOCK_RATE_* values used to configure the ADC clock input.
  1730. //! \param ui32ClockDiv is the input clock divider for the clock selected by
  1731. //! the \b ADC_CLOCK_SRC value.
  1732. //!
  1733. //! This function is used to configure the input clock to the ADC modules. The
  1734. //! clock configuration is shared across ADC units so \e ui32Base must
  1735. //! always be \b ADC0_BASE. The \e ui32Config value is logical OR of one
  1736. //! of the \b ADC_CLOCK_RATE_ and one of the \b ADC_CLOCK_SRC_ values defined
  1737. //! below. The \b ADC_CLOCK_SRC_* values determine the input clock for the ADC.
  1738. //! Not all values are available on all devices so check the device data sheet
  1739. //! to determine value configuration options. Regardless of the source, the
  1740. //! final frequency for TM4C123x devices must be 16 MHz and for TM4C129x parts
  1741. //! after dividing must be between 16 and 32 MHz.
  1742. //!
  1743. //! \note For TM4C123x devices, if the PLL is enabled, the PLL/25 is used as
  1744. //! the ADC clock unless ADC_CLOCK_SRC_PIOSC is specified. If the PLL is
  1745. //! disabled, the MOSC is used as the clock source unless ADC_CLOCK_SRC_PIOSC
  1746. //! is specified.
  1747. //!
  1748. //! - \b ADC_CLOCK_SRC_PLL - The main PLL output (TM4x129 class only).
  1749. //! - \b ADC_CLOCK_SRC_PIOSC - The internal PIOSC at 16 MHz.
  1750. //! - \b ADC_CLOCK_SRC_ALTCLK - The output of the ALTCLK in the system control
  1751. //! module (TM4x129 class only).
  1752. //! - \b ADC_CLOCK_SRC_MOSC - The external MOSC (TM4x129 class only).
  1753. //!
  1754. //! \b ADC_CLOCK_RATE values control how often samples are provided back to the
  1755. //! application. The values are the following:
  1756. //!
  1757. //! - \b ADC_CLOCK_RATE_FULL - All samples.
  1758. //! - \b ADC_CLOCK_RATE_HALF - Every other sample.
  1759. //! - \b ADC_CLOCK_RATE_QUARTER - Every fourth sample.
  1760. //! - \b ADC_CLOCK_RATE_EIGHTH - Every either sample.
  1761. //!
  1762. //! The \e ui32ClockDiv parameter allows for dividing a higher frequency down
  1763. //! into the valid range for the ADCs. This parameter is typically only used
  1764. //! \b ADC_CLOCK_SRC_PLL option because it is the only clock value that can be
  1765. //! with the in the correct range to use the divider. The actual value ranges
  1766. //! from 1 to 64.
  1767. //!
  1768. //! \b Example: ADC Clock Configurations
  1769. //!
  1770. //! \verbatim
  1771. //!
  1772. //! //
  1773. //! // Configure the ADC to use PIOSC divided by one (16 MHz) and sample at
  1774. //! // half the rate.
  1775. //! //
  1776. //! ADCClockConfigSet(ADC0_BASE, ADC_CLOCK_SRC_PIOSC | ADC_CLOCK_RATE_HALF, 1);
  1777. //!
  1778. //! ...
  1779. //!
  1780. //! //
  1781. //! // Configure the ADC to use PLL at 480 MHz divided by 24 to get an ADC
  1782. //! // clock of 20 MHz.
  1783. //! //
  1784. //! ADCClockConfigSet(ADC0_BASE, ADC_CLOCK_SRC_PLL | ADC_CLOCK_RATE_FULL, 24);
  1785. //! \endverbatim
  1786. //!
  1787. //! \return None.
  1788. //
  1789. //*****************************************************************************
  1790. void
  1791. ADCClockConfigSet(uint32_t ui32Base, uint32_t ui32Config,
  1792. uint32_t ui32ClockDiv)
  1793. {
  1794. //
  1795. // Check the argument.
  1796. //
  1797. ASSERT(ui32Base == ADC0_BASE);
  1798. //
  1799. // A rate must be supplied.
  1800. //
  1801. ASSERT((ui32Config & ADC_CLOCK_RATE_FULL) != 0);
  1802. //
  1803. // Clock must be valid divider.
  1804. //
  1805. ASSERT(((ui32ClockDiv - 1) & ~ADC_CC_CLKDIV_M) == 0);
  1806. //
  1807. // Write the sample conversion rate.
  1808. //
  1809. HWREG(ui32Base + ADC_O_PC) = (ui32Config >> 4) & ADC_PC_SR_M;
  1810. //
  1811. // Write the clock select and divider.
  1812. //
  1813. HWREG(ui32Base + ADC_O_CC) = (ui32Config & ADC_CC_CS_M) |
  1814. (((ui32ClockDiv - 1) << ADC_CC_CLKDIV_S)) ;
  1815. }
  1816. //*****************************************************************************
  1817. //
  1818. //! Returns the clock configuration for the ADC.
  1819. //!
  1820. //! \param ui32Base is the base address of the ADC to configure, which must
  1821. //! always be \b ADC0_BASE.
  1822. //! \param pui32ClockDiv is a pointer to the input clock divider for the clock
  1823. //! selected by the \b ADC_CLOCK_SRC in use by the ADCs.
  1824. //!
  1825. //! This function returns the ADC clock configuration and the clock divider for
  1826. //! the ADCs.
  1827. //!
  1828. //! \b Example: Read the current ADC clock configuration.
  1829. //!
  1830. //! \verbatim
  1831. //! uint32_t ui32Config, ui32ClockDiv;
  1832. //!
  1833. //! //
  1834. //! // Read the current ADC clock configuration.
  1835. //! //
  1836. //! ui32Config = ADCClockConfigGet(ADC0_BASE, &ui32ClockDiv);
  1837. //! \endverbatim
  1838. //!
  1839. //! \return The current clock configuration of the ADC defined as a combination
  1840. //! of one of \b ADC_CLOCK_SRC_PLL, \b ADC_CLOCK_SRC_PIOSC,
  1841. //! \b ADC_CLOCK_SRC_MOSC, or \b ADC_CLOCK_SRC_ALTCLK logical ORed with one of
  1842. //! \b ADC_CLOCK_RATE_FULL, \b ADC_CLOCK_RATE_HALF, \b ADC_CLOCK_RATE_QUARTER,
  1843. //! or \b ADC_CLOCK_RATE_EIGHTH. See ADCClockConfigSet() for more information
  1844. //! on these values.
  1845. //
  1846. //*****************************************************************************
  1847. uint32_t
  1848. ADCClockConfigGet(uint32_t ui32Base, uint32_t *pui32ClockDiv)
  1849. {
  1850. uint32_t ui32Config;
  1851. //
  1852. // Check the argument.
  1853. //
  1854. ASSERT(ui32Base == ADC0_BASE);
  1855. //
  1856. // Read the current configuration.
  1857. //
  1858. ui32Config = HWREG(ui32Base + ADC_O_CC);
  1859. //
  1860. // If the clock divider was requested provide the current value.
  1861. //
  1862. if(pui32ClockDiv)
  1863. {
  1864. *pui32ClockDiv =
  1865. ((ui32Config & ADC_CC_CLKDIV_M) >> ADC_CC_CLKDIV_S) + 1;
  1866. }
  1867. //
  1868. // Clear out the divider bits.
  1869. //
  1870. ui32Config &= ~ADC_CC_CLKDIV_M;
  1871. //
  1872. // Add in the sample interval to the configuration.
  1873. //
  1874. ui32Config = (HWREG(ui32Base + ADC_O_PC) & ADC_PC_SR_M) << 4;
  1875. return(ui32Config);
  1876. }
  1877. //*****************************************************************************
  1878. //
  1879. // Close the Doxygen group.
  1880. //! @}
  1881. //
  1882. //*****************************************************************************