adc.c 72 KB

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