adc.c 69 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002
  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. //*****************************************************************************
  37. //*****************************************************************************
  38. //
  39. //! \addtogroup adc_api
  40. //! @{
  41. //
  42. //*****************************************************************************
  43. #include <ti/devices/msp432e4/inc/msp432e411y.h>
  44. #include "types.h"
  45. #include <stdbool.h>
  46. #include <stdint.h>
  47. #include "inc/hw_adc.h"
  48. #include "inc/hw_sysctl.h"
  49. #include "adc.h"
  50. #include "debug.h"
  51. #include "interrupt.h"
  52. //*****************************************************************************
  53. //
  54. // These defines are used by the ADC driver to simplify access to the ADC
  55. // sequencer's registers.
  56. //
  57. //*****************************************************************************
  58. #define ADC_SEQ (ADC_O_SSMUX0)
  59. #define ADC_SEQ_STEP (ADC_O_SSMUX1 - ADC_O_SSMUX0)
  60. #define ADC_SSMUX (ADC_O_SSMUX0 - ADC_O_SSMUX0)
  61. #define ADC_SSEMUX (ADC_O_SSEMUX0 - ADC_O_SSMUX0)
  62. #define ADC_SSCTL (ADC_O_SSCTL0 - ADC_O_SSMUX0)
  63. #define ADC_SSFIFO (ADC_O_SSFIFO0 - ADC_O_SSMUX0)
  64. #define ADC_SSFSTAT (ADC_O_SSFSTAT0 - ADC_O_SSMUX0)
  65. #define ADC_SSOP (ADC_O_SSOP0 - ADC_O_SSMUX0)
  66. #define ADC_SSDC (ADC_O_SSDC0 - ADC_O_SSMUX0)
  67. #define ADC_SSTSH (ADC_O_SSTSH0 - ADC_O_SSMUX0)
  68. //*****************************************************************************
  69. //
  70. // The currently configured software oversampling factor for each of the ADC
  71. // sequencers.
  72. //
  73. //*****************************************************************************
  74. static uint8_t g_pui8OversampleFactor[2][3];
  75. //*****************************************************************************
  76. //
  77. //! Returns the interrupt number for a given ADC base address and sequence
  78. //! number.
  79. //!
  80. //! \param ui32Base is the base address of the ADC module.
  81. //! \param ui32SequenceNum is the sample sequence number.
  82. //!
  83. //! This function returns the interrupt number for the ADC module and sequence
  84. //! number provided in the \e ui32Base and \e ui32SequenceNum parameters.
  85. //!
  86. //! \return Returns the ADC sequence interrupt number or 0 if the interrupt
  87. //! does not exist.
  88. //
  89. //*****************************************************************************
  90. static uint_fast8_t
  91. _ADCIntNumberGet(uint32_t ui32Base, uint32_t ui32SequenceNum)
  92. {
  93. uint_fast8_t ui8Int;
  94. //
  95. // Determine the interrupt to register based on the sequence number.
  96. //
  97. ui8Int = ((ui32Base == ADC0_BASE) ?
  98. (INT_ADC0SS0 + ui32SequenceNum) :
  99. (INT_ADC1SS0 + ui32SequenceNum));
  100. return (ui8Int);
  101. }
  102. //*****************************************************************************
  103. //
  104. //! Registers an interrupt handler for an ADC interrupt.
  105. //!
  106. //! \param ui32Base is the base address of the ADC module.
  107. //! \param ui32SequenceNum is the sample sequence number.
  108. //! \param pfnHandler is a pointer to the function to be called when the
  109. //! ADC sample sequence interrupt occurs.
  110. //!
  111. //! This function sets the handler to be called when a sample sequence
  112. //! interrupt occurs. This function enables the global interrupt in the
  113. //! interrupt controller; the sequence interrupt must be enabled with
  114. //! ADCIntEnable(). It is the interrupt handler's responsibility to clear the
  115. //! interrupt source via ADCIntClear().
  116. //!
  117. //! \sa IntRegister() for important information about registering interrupt
  118. //! handlers.
  119. //!
  120. //! \return None.
  121. //
  122. //*****************************************************************************
  123. void
  124. ADCIntRegister(uint32_t ui32Base, uint32_t ui32SequenceNum,
  125. void (*pfnHandler)(void))
  126. {
  127. uint_fast8_t ui8Int;
  128. //
  129. // Check the arguments.
  130. //
  131. ASSERT((ui32Base == ADC0_BASE) || (ui32Base == ADC1_BASE));
  132. ASSERT(ui32SequenceNum < 4);
  133. //
  134. // Determine the interrupt to register based on the sequence number.
  135. //
  136. ui8Int = _ADCIntNumberGet(ui32Base, ui32SequenceNum);
  137. ASSERT(ui8Int != 0);
  138. //
  139. // Register the interrupt handler.
  140. //
  141. IntRegister(ui8Int, pfnHandler);
  142. //
  143. // Enable the timer interrupt.
  144. //
  145. IntEnable(ui8Int);
  146. }
  147. //*****************************************************************************
  148. //
  149. //! Unregisters the interrupt handler for an ADC interrupt.
  150. //!
  151. //! \param ui32Base is the base address of the ADC module.
  152. //! \param ui32SequenceNum is the sample sequence number.
  153. //!
  154. //! This function unregisters the interrupt handler. This function disables
  155. //! the global interrupt in the interrupt controller; the sequence interrupt
  156. //! must be disabled via ADCIntDisable().
  157. //!
  158. //! \sa IntRegister() for important information about registering interrupt
  159. //! handlers.
  160. //!
  161. //! \return None.
  162. //
  163. //*****************************************************************************
  164. void
  165. ADCIntUnregister(uint32_t ui32Base, uint32_t ui32SequenceNum)
  166. {
  167. uint_fast8_t ui8Int;
  168. //
  169. // Check the arguments.
  170. //
  171. ASSERT((ui32Base == ADC0_BASE) || (ui32Base == ADC1_BASE));
  172. ASSERT(ui32SequenceNum < 4);
  173. //
  174. // Determine the interrupt to unregister based on the sequence number.
  175. //
  176. ui8Int = _ADCIntNumberGet(ui32Base, ui32SequenceNum);
  177. ASSERT(ui8Int != 0);
  178. //
  179. // Disable the interrupt.
  180. //
  181. IntDisable(ui8Int);
  182. //
  183. // Unregister the interrupt handler.
  184. //
  185. IntUnregister(ui8Int);
  186. }
  187. //*****************************************************************************
  188. //
  189. //! Disables a sample sequence interrupt.
  190. //!
  191. //! \param ui32Base is the base address of the ADC module.
  192. //! \param ui32SequenceNum is the sample sequence number.
  193. //!
  194. //! This function disables the requested sample sequence interrupt.
  195. //!
  196. //! \return None.
  197. //
  198. //*****************************************************************************
  199. void
  200. ADCIntDisable(uint32_t ui32Base, uint32_t ui32SequenceNum)
  201. {
  202. //
  203. // Check the arguments.
  204. //
  205. ASSERT((ui32Base == ADC0_BASE) || (ui32Base == ADC1_BASE));
  206. ASSERT(ui32SequenceNum < 4);
  207. //
  208. // Disable this sample sequence interrupt.
  209. //
  210. HWREG(ui32Base + ADC_O_IM) &= ~(1 << ui32SequenceNum);
  211. }
  212. //*****************************************************************************
  213. //
  214. //! Enables a sample sequence interrupt.
  215. //!
  216. //! \param ui32Base is the base address of the ADC module.
  217. //! \param ui32SequenceNum is the sample sequence number.
  218. //!
  219. //! This function enables the requested sample sequence interrupt. Any
  220. //! outstanding interrupts are cleared before enabling the sample sequence
  221. //! interrupt.
  222. //!
  223. //! \return None.
  224. //
  225. //*****************************************************************************
  226. void
  227. ADCIntEnable(uint32_t ui32Base, uint32_t ui32SequenceNum)
  228. {
  229. //
  230. // Check the arguments.
  231. //
  232. ASSERT((ui32Base == ADC0_BASE) || (ui32Base == ADC1_BASE));
  233. ASSERT(ui32SequenceNum < 4);
  234. //
  235. // Clear any outstanding interrupts on this sample sequence.
  236. //
  237. HWREG(ui32Base + ADC_O_ISC) = 1 << ui32SequenceNum;
  238. //
  239. // Enable this sample sequence interrupt.
  240. //
  241. HWREG(ui32Base + ADC_O_IM) |= 1 << ui32SequenceNum;
  242. }
  243. //*****************************************************************************
  244. //
  245. //! Gets the current interrupt status.
  246. //!
  247. //! \param ui32Base is the base address of the ADC module.
  248. //! \param ui32SequenceNum is the sample sequence number.
  249. //! \param bMasked is false if the raw interrupt status is required and true if
  250. //! the masked interrupt status is required.
  251. //!
  252. //! This function returns the interrupt status for the specified sample
  253. //! sequence. Either the raw interrupt status or the status of interrupts that
  254. //! are allowed to reflect to the processor can be returned.
  255. //!
  256. //! \return The current raw or masked interrupt status.
  257. //
  258. //*****************************************************************************
  259. uint32_t
  260. ADCIntStatus(uint32_t ui32Base, uint32_t ui32SequenceNum, bool bMasked)
  261. {
  262. uint32_t ui32Temp;
  263. //
  264. // Check the arguments.
  265. //
  266. ASSERT((ui32Base == ADC0_BASE) || (ui32Base == ADC1_BASE));
  267. ASSERT(ui32SequenceNum < 4);
  268. //
  269. // Return either the interrupt status or the raw interrupt status as
  270. // requested.
  271. //
  272. if (bMasked)
  273. {
  274. ui32Temp = HWREG(ui32Base + ADC_O_ISC) & (0x10001 << ui32SequenceNum);
  275. }
  276. else
  277. {
  278. ui32Temp = (HWREG(ui32Base + ADC_O_RIS) &
  279. (0x10000 | (1 << ui32SequenceNum)));
  280. //
  281. // If the digital comparator status bit is set, reflect it to the
  282. // appropriate sequence bit.
  283. //
  284. if (ui32Temp & 0x10000)
  285. {
  286. ui32Temp |= 0xF0000;
  287. ui32Temp &= ~(0x10000 << ui32SequenceNum);
  288. }
  289. }
  290. //
  291. // Return the interrupt status
  292. //
  293. return (ui32Temp);
  294. }
  295. //*****************************************************************************
  296. //
  297. //! Clears sample sequence interrupt source.
  298. //!
  299. //! \param ui32Base is the base address of the ADC module.
  300. //! \param ui32SequenceNum is the sample sequence number.
  301. //!
  302. //! The specified sample sequence interrupt is cleared, so that it no longer
  303. //! asserts. This function must be called in the interrupt handler to keep
  304. //! the interrupt from being triggered again immediately upon exit.
  305. //!
  306. //! \note Because there is a write buffer in the Cortex-M processor, it may
  307. //! take several clock cycles before the interrupt source is actually cleared.
  308. //! Therefore, it is recommended that the interrupt source be cleared early in
  309. //! the interrupt handler (as opposed to the very last action) to avoid
  310. //! returning from the interrupt handler before the interrupt source is
  311. //! actually cleared. Failure to do so may result in the interrupt handler
  312. //! being immediately reentered (because the interrupt controller still sees
  313. //! the interrupt source asserted).
  314. //!
  315. //! \return None.
  316. //
  317. //*****************************************************************************
  318. void
  319. ADCIntClear(uint32_t ui32Base, uint32_t ui32SequenceNum)
  320. {
  321. //
  322. // Check the arguments.
  323. //
  324. ASSERT((ui32Base == ADC0_BASE) || (ui32Base == ADC1_BASE));
  325. ASSERT(ui32SequenceNum < 4);
  326. //
  327. // Clear the interrupt.
  328. //
  329. HWREG(ui32Base + ADC_O_ISC) = 1 << ui32SequenceNum;
  330. }
  331. //*****************************************************************************
  332. //
  333. //! Enables a sample sequence.
  334. //!
  335. //! \param ui32Base is the base address of the ADC module.
  336. //! \param ui32SequenceNum is the sample sequence number.
  337. //!
  338. //! Allows the specified sample sequence to be captured when its trigger is
  339. //! detected. A sample sequence must be configured before it is enabled.
  340. //!
  341. //! \return None.
  342. //
  343. //*****************************************************************************
  344. void
  345. ADCSequenceEnable(uint32_t ui32Base, uint32_t ui32SequenceNum)
  346. {
  347. //
  348. // Check the arguments.
  349. //
  350. ASSERT((ui32Base == ADC0_BASE) || (ui32Base == ADC1_BASE));
  351. ASSERT(ui32SequenceNum < 4);
  352. //
  353. // Enable the specified sequence.
  354. //
  355. HWREG(ui32Base + ADC_O_ACTSS) |= 1 << ui32SequenceNum;
  356. }
  357. //*****************************************************************************
  358. //
  359. //! Disables a sample sequence.
  360. //!
  361. //! \param ui32Base is the base address of the ADC module.
  362. //! \param ui32SequenceNum is the sample sequence number.
  363. //!
  364. //! Prevents the specified sample sequence from being captured when its trigger
  365. //! is detected. A sample sequence must be disabled before it is configured.
  366. //!
  367. //! \return None.
  368. //
  369. //*****************************************************************************
  370. void
  371. ADCSequenceDisable(uint32_t ui32Base, uint32_t ui32SequenceNum)
  372. {
  373. //
  374. // Check the arguments.
  375. //
  376. ASSERT((ui32Base == ADC0_BASE) || (ui32Base == ADC1_BASE));
  377. ASSERT(ui32SequenceNum < 4);
  378. //
  379. // Disable the specified sequences.
  380. //
  381. HWREG(ui32Base + ADC_O_ACTSS) &= ~(1 << ui32SequenceNum);
  382. }
  383. //*****************************************************************************
  384. //
  385. //! Configures the trigger source and priority of a sample sequence.
  386. //!
  387. //! \param ui32Base is the base address of the ADC module.
  388. //! \param ui32SequenceNum is the sample sequence number.
  389. //! \param ui32Trigger is the trigger source that initiates the sample
  390. //! sequence; must be one of the \b ADC_TRIGGER_* values.
  391. //! \param ui32Priority is the relative priority of the sample sequence with
  392. //! respect to the other sample sequences.
  393. //!
  394. //! This function configures the initiation criteria for a sample sequence.
  395. //! Valid sample sequencers range from zero to three; sequencer zero captures
  396. //! up to eight samples, sequencers one and two capture up to four samples,
  397. //! and sequencer three captures a single sample. The trigger condition and
  398. //! priority (with respect to other sample sequencer execution) are set.
  399. //!
  400. //! The \e ui32Trigger parameter can take on the following values:
  401. //!
  402. //! - \b ADC_TRIGGER_PROCESSOR - A trigger generated by the processor, via the
  403. //! ADCProcessorTrigger() function.
  404. //! - \b ADC_TRIGGER_COMP0 - A trigger generated by the first analog
  405. //! comparator; configured with ComparatorConfigure().
  406. //! - \b ADC_TRIGGER_COMP1 - A trigger generated by the second analog
  407. //! comparator; configured with ComparatorConfigure().
  408. //! - \b ADC_TRIGGER_COMP2 - A trigger generated by the third analog
  409. //! comparator; configured with ComparatorConfigure().
  410. //! - \b ADC_TRIGGER_EXTERNAL - A trigger generated by an input from the Port
  411. //! B4 pin. Note that some microcontrollers can
  412. //! select from any GPIO using the
  413. //! GPIOADCTriggerEnable() function.
  414. //! - \b ADC_TRIGGER_TIMER - A trigger generated by a timer; configured with
  415. //! TimerControlTrigger().
  416. //! - \b ADC_TRIGGER_PWM0 - A trigger generated by the first PWM generator;
  417. //! configured with PWMGenIntTrigEnable().
  418. //! - \b ADC_TRIGGER_PWM1 - A trigger generated by the second PWM generator;
  419. //! configured with PWMGenIntTrigEnable().
  420. //! - \b ADC_TRIGGER_PWM2 - A trigger generated by the third PWM generator;
  421. //! configured with PWMGenIntTrigEnable().
  422. //! - \b ADC_TRIGGER_PWM3 - A trigger generated by the fourth PWM generator;
  423. //! configured with PWMGenIntTrigEnable().
  424. //! - \b ADC_TRIGGER_ALWAYS - A trigger that is always asserted, causing the
  425. //! sample sequence to capture repeatedly (so long as
  426. //! there is not a higher priority source active).
  427. //!
  428. //! The \e ui32Priority parameter is a value between 0 and 3, where 0
  429. //! represents the highest priority and 3 the lowest. Note that when
  430. //! programming the priority among a set of sample sequences, each must have
  431. //! unique priority; it is up to the caller to guarantee the uniqueness of the
  432. //! priorities.
  433. //!
  434. //! \return None.
  435. //
  436. //*****************************************************************************
  437. void
  438. ADCSequenceConfigure(uint32_t ui32Base, uint32_t ui32SequenceNum,
  439. uint32_t ui32Trigger, uint32_t ui32Priority)
  440. {
  441. //
  442. // Check the arugments.
  443. //
  444. ASSERT((ui32Base == ADC0_BASE) || (ui32Base == ADC1_BASE));
  445. ASSERT(ui32SequenceNum < 4);
  446. ASSERT(((ui32Trigger & 0xF) == ADC_TRIGGER_PROCESSOR) ||
  447. ((ui32Trigger & 0xF) == ADC_TRIGGER_COMP0) ||
  448. ((ui32Trigger & 0xF) == ADC_TRIGGER_COMP1) ||
  449. ((ui32Trigger & 0xF) == ADC_TRIGGER_COMP2) ||
  450. ((ui32Trigger & 0xF) == ADC_TRIGGER_EXTERNAL) ||
  451. ((ui32Trigger & 0xF) == ADC_TRIGGER_TIMER) ||
  452. ((ui32Trigger & 0xF) == ADC_TRIGGER_PWM0) ||
  453. ((ui32Trigger & 0xF) == ADC_TRIGGER_PWM1) ||
  454. ((ui32Trigger & 0xF) == ADC_TRIGGER_PWM2) ||
  455. ((ui32Trigger & 0xF) == ADC_TRIGGER_PWM3) ||
  456. ((ui32Trigger & 0xF) == ADC_TRIGGER_ALWAYS));
  457. ASSERT(ui32Priority < 4);
  458. //
  459. // Compute the shift for the bits that control this sample sequence.
  460. //
  461. ui32SequenceNum *= 4;
  462. //
  463. // Set the trigger event for this sample sequence.
  464. //
  465. HWREG(ui32Base + ADC_O_EMUX) = ((HWREG(ui32Base + ADC_O_EMUX) &
  466. ~(0xf << ui32SequenceNum)) |
  467. ((ui32Trigger & 0xf) << ui32SequenceNum));
  468. //
  469. // Set the priority for this sample sequence.
  470. //
  471. HWREG(ui32Base + ADC_O_SSPRI) = ((HWREG(ui32Base + ADC_O_SSPRI) &
  472. ~(0xf << ui32SequenceNum)) |
  473. ((ui32Priority & 0x3) <<
  474. ui32SequenceNum));
  475. }
  476. //*****************************************************************************
  477. //
  478. //! Configure a step of the sample sequencer.
  479. //!
  480. //! \param ui32Base is the base address of the ADC module.
  481. //! \param ui32SequenceNum is the sample sequence number.
  482. //! \param ui32Step is the step to be configured.
  483. //! \param ui32Config is the configuration of this step; must be a logical OR
  484. //! of \b ADC_CTL_TS, \b ADC_CTL_IE, \b ADC_CTL_END, \b ADC_CTL_D, one of the
  485. //! input channel selects (\b ADC_CTL_CH0 through \b ADC_CTL_CH23), and one of
  486. //! the digital comparator selects (\b ADC_CTL_CMP0 through \b ADC_CTL_CMP7).
  487. //!
  488. //! This function configures the ADC for one step of a sample sequence. The
  489. //! ADC can be configured for single-ended or differential operation (the
  490. //! \b ADC_CTL_D bit selects differential operation when set), the channel to
  491. //! be sampled can be chosen (the \b ADC_CTL_CH0 through \b ADC_CTL_CH23
  492. //! values), and the internal temperature sensor can be selected (the
  493. //! \b ADC_CTL_TS bit). Additionally, this step can be defined as the last in
  494. //! the sequence (the \b ADC_CTL_END bit) and it can be configured to cause an
  495. //! interrupt when the step is complete (the \b ADC_CTL_IE bit). If the
  496. //! digital comparators are present on the device, this step may also be
  497. //! configured to send the ADC sample to the selected comparator using
  498. //! \b ADC_CTL_CMP0 through \b ADC_CTL_CMP7. The configuration is used by the
  499. //! ADC at the appropriate time when the trigger for this sequence occurs.
  500. //!
  501. //! \note If the Digital Comparator is present and enabled using the
  502. //! \b ADC_CTL_CMP0 through \b ADC_CTL_CMP7 selects, the ADC sample is NOT
  503. //! written into the ADC sequence data FIFO.
  504. //!
  505. //! The \e ui32Step parameter determines the order in which the samples are
  506. //! captured by the ADC when the trigger occurs. It can range from zero to
  507. //! seven for the first sample sequencer, from zero to three for the second and
  508. //! third sample sequencer, and can only be zero for the fourth sample
  509. //! sequencer.
  510. //!
  511. //! Differential mode only works with adjacent channel pairs (for example, 0
  512. //! and 1). The channel select must be the number of the channel pair to
  513. //! sample (for example, \b ADC_CTL_CH0 for 0 and 1, or \b ADC_CTL_CH1 for 2
  514. //! and 3) or undefined results are returned by the ADC. Additionally, if
  515. //! differential mode is selected when the temperature sensor is being sampled,
  516. //! undefined results are returned by the ADC.
  517. //!
  518. //! It is the responsibility of the caller to ensure that a valid configuration
  519. //! is specified; this function does not check the validity of the specified
  520. //! configuration.
  521. //!
  522. //! \return None.
  523. //
  524. //*****************************************************************************
  525. void
  526. ADCSequenceStepConfigure(uint32_t ui32Base, uint32_t ui32SequenceNum,
  527. uint32_t ui32Step, uint32_t ui32Config)
  528. {
  529. uint32_t ui32Temp;
  530. //
  531. // Check the arguments.
  532. //
  533. ASSERT((ui32Base == ADC0_BASE) || (ui32Base == ADC1_BASE));
  534. ASSERT(ui32SequenceNum < 4);
  535. ASSERT(((ui32SequenceNum == 0) && (ui32Step < 8)) ||
  536. ((ui32SequenceNum == 1) && (ui32Step < 4)) ||
  537. ((ui32SequenceNum == 2) && (ui32Step < 4)) ||
  538. ((ui32SequenceNum == 3) && (ui32Step < 1)));
  539. //
  540. // Get the offset of the sequence to be configured.
  541. //
  542. ui32Base += ADC_SEQ + (ADC_SEQ_STEP * ui32SequenceNum);
  543. //
  544. // Compute the shift for the bits that control this step.
  545. //
  546. ui32Step *= 4;
  547. //
  548. // Set the analog mux value for this step.
  549. //
  550. HWREG(ui32Base + ADC_SSMUX) = ((HWREG(ui32Base + ADC_SSMUX) &
  551. ~(0x0000000f << ui32Step)) |
  552. ((ui32Config & 0x0f) << ui32Step));
  553. //
  554. // Set the upper bits of the analog mux value for this step.
  555. //
  556. HWREG(ui32Base + ADC_SSEMUX) = ((HWREG(ui32Base + ADC_SSEMUX) &
  557. ~(0x0000000f << ui32Step)) |
  558. (((ui32Config & 0xf00) >> 8) << ui32Step));
  559. //
  560. // Set the control value for this step.
  561. //
  562. HWREG(ui32Base + ADC_SSCTL) = ((HWREG(ui32Base + ADC_SSCTL) &
  563. ~(0x0000000f << ui32Step)) |
  564. (((ui32Config & 0xf0) >> 4) << ui32Step));
  565. //
  566. // Set the sample and hold time for this step.
  567. //
  568. HWREG(ui32Base + ADC_SSTSH) = ((HWREG(ui32Base + ADC_SSTSH) &
  569. ~(0x0000000f << ui32Step)) |
  570. (((ui32Config & 0xf00000) >> 20) << ui32Step));
  571. //
  572. // Enable digital comparator if specified in the ui32Config bit-fields.
  573. //
  574. if (ui32Config & 0x000F0000)
  575. {
  576. //
  577. // Program the comparator for the specified step.
  578. //
  579. ui32Temp = HWREG(ui32Base + ADC_SSDC);
  580. ui32Temp &= ~(0xF << ui32Step);
  581. ui32Temp |= (((ui32Config & 0x00070000) >> 16) << ui32Step);
  582. HWREG(ui32Base + ADC_SSDC) = ui32Temp;
  583. //
  584. // Enable the comparator.
  585. //
  586. HWREG(ui32Base + ADC_SSOP) |= (1 << ui32Step);
  587. }
  588. //
  589. // Disable digital comparator if not specified.
  590. //
  591. else
  592. {
  593. HWREG(ui32Base + ADC_SSOP) &= ~(1 << ui32Step);
  594. }
  595. }
  596. //*****************************************************************************
  597. //
  598. //! Determines if a sample sequence overflow occurred.
  599. //!
  600. //! \param ui32Base is the base address of the ADC module.
  601. //! \param ui32SequenceNum is the sample sequence number.
  602. //!
  603. //! This function determines if a sample sequence overflow has occurred.
  604. //! Overflow happens if the captured samples are not read from the FIFO before
  605. //! the next trigger occurs.
  606. //!
  607. //! \return Returns zero if there was not an overflow, and non-zero if there
  608. //! was.
  609. //
  610. //*****************************************************************************
  611. int32_t
  612. ADCSequenceOverflow(uint32_t ui32Base, uint32_t ui32SequenceNum)
  613. {
  614. //
  615. // Check the arguments.
  616. //
  617. ASSERT((ui32Base == ADC0_BASE) || (ui32Base == ADC1_BASE));
  618. ASSERT(ui32SequenceNum < 4);
  619. //
  620. // Determine if there was an overflow on this sequence.
  621. //
  622. return (HWREG(ui32Base + ADC_O_OSTAT) & (1 << ui32SequenceNum));
  623. }
  624. //*****************************************************************************
  625. //
  626. //! Clears the overflow condition on a sample sequence.
  627. //!
  628. //! \param ui32Base is the base address of the ADC module.
  629. //! \param ui32SequenceNum is the sample sequence number.
  630. //!
  631. //! This function clears an overflow condition on one of the sample sequences.
  632. //! The overflow condition must be cleared in order to detect a subsequent
  633. //! overflow condition (it otherwise causes no harm).
  634. //!
  635. //! \return None.
  636. //
  637. //*****************************************************************************
  638. void
  639. ADCSequenceOverflowClear(uint32_t ui32Base, uint32_t ui32SequenceNum)
  640. {
  641. //
  642. // Check the arguments.
  643. //
  644. ASSERT((ui32Base == ADC0_BASE) || (ui32Base == ADC1_BASE));
  645. ASSERT(ui32SequenceNum < 4);
  646. //
  647. // Clear the overflow condition for this sequence.
  648. //
  649. HWREG(ui32Base + ADC_O_OSTAT) = 1 << ui32SequenceNum;
  650. }
  651. //*****************************************************************************
  652. //
  653. //! Determines if a sample sequence underflow occurred.
  654. //!
  655. //! \param ui32Base is the base address of the ADC module.
  656. //! \param ui32SequenceNum is the sample sequence number.
  657. //!
  658. //! This function determines if a sample sequence underflow has occurred.
  659. //! Underflow happens if too many samples are read from the FIFO.
  660. //!
  661. //! \return Returns zero if there was not an underflow, and non-zero if there
  662. //! was.
  663. //
  664. //*****************************************************************************
  665. int32_t
  666. ADCSequenceUnderflow(uint32_t ui32Base, uint32_t ui32SequenceNum)
  667. {
  668. //
  669. // Check the arguments.
  670. //
  671. ASSERT((ui32Base == ADC0_BASE) || (ui32Base == ADC1_BASE));
  672. ASSERT(ui32SequenceNum < 4);
  673. //
  674. // Determine if there was an underflow on this sequence.
  675. //
  676. return (HWREG(ui32Base + ADC_O_USTAT) & (1 << ui32SequenceNum));
  677. }
  678. //*****************************************************************************
  679. //
  680. //! Clears the underflow condition on a sample sequence.
  681. //!
  682. //! \param ui32Base is the base address of the ADC module.
  683. //! \param ui32SequenceNum is the sample sequence number.
  684. //!
  685. //! This function clears an underflow condition on one of the sample
  686. //! sequencers. The underflow condition must be cleared in order to detect a
  687. //! subsequent underflow condition (it otherwise causes no harm).
  688. //!
  689. //! \return None.
  690. //
  691. //*****************************************************************************
  692. void
  693. ADCSequenceUnderflowClear(uint32_t ui32Base, uint32_t ui32SequenceNum)
  694. {
  695. //
  696. // Check the arguments.
  697. //
  698. ASSERT((ui32Base == ADC0_BASE) || (ui32Base == ADC1_BASE));
  699. ASSERT(ui32SequenceNum < 4);
  700. //
  701. // Clear the underflow condition for this sequence.
  702. //
  703. HWREG(ui32Base + ADC_O_USTAT) = 1 << ui32SequenceNum;
  704. }
  705. //*****************************************************************************
  706. //
  707. //! Gets the captured data for a sample sequence.
  708. //!
  709. //! \param ui32Base is the base address of the ADC module.
  710. //! \param ui32SequenceNum is the sample sequence number.
  711. //! \param pui32Buffer is the address where the data is stored.
  712. //!
  713. //! This function copies data from the specified sample sequencer output FIFO
  714. //! to a memory resident buffer. The number of samples available in the
  715. //! hardware FIFO are copied into the buffer, which is assumed to be large
  716. //! enough to hold that many samples. This function only returns the samples
  717. //! that are presently available, which may not be the entire sample sequence
  718. //! if it is in the process of being executed.
  719. //!
  720. //! \return Returns the number of samples copied to the buffer.
  721. //
  722. //*****************************************************************************
  723. int32_t
  724. ADCSequenceDataGet(uint32_t ui32Base, uint32_t ui32SequenceNum,
  725. uint32_t *pui32Buffer)
  726. {
  727. uint32_t ui32Count;
  728. //
  729. // Check the arguments.
  730. //
  731. ASSERT((ui32Base == ADC0_BASE) || (ui32Base == ADC1_BASE));
  732. ASSERT(ui32SequenceNum < 4);
  733. //
  734. // Get the offset of the sequence to be read.
  735. //
  736. ui32Base += ADC_SEQ + (ADC_SEQ_STEP * ui32SequenceNum);
  737. //
  738. // Read samples from the FIFO until it is empty.
  739. //
  740. ui32Count = 0;
  741. while (!(HWREG(ui32Base + ADC_SSFSTAT) & ADC_SSFSTAT0_EMPTY) &&
  742. (ui32Count < 8))
  743. {
  744. //
  745. // Read the FIFO and copy it to the destination.
  746. //
  747. *pui32Buffer++ = HWREG(ui32Base + ADC_SSFIFO);
  748. //
  749. // Increment the count of samples read.
  750. //
  751. ui32Count++;
  752. }
  753. //
  754. // Return the number of samples read.
  755. //
  756. return (ui32Count);
  757. }
  758. //*****************************************************************************
  759. //
  760. //! Causes a processor trigger for a sample sequence.
  761. //!
  762. //! \param ui32Base is the base address of the ADC module.
  763. //! \param ui32SequenceNum is the sample sequence number, with
  764. //! \b ADC_TRIGGER_WAIT or \b ADC_TRIGGER_SIGNAL optionally ORed into it.
  765. //!
  766. //! This function triggers a processor-initiated sample sequence if the sample
  767. //! sequence trigger is configured to \b ADC_TRIGGER_PROCESSOR. If
  768. //! \b ADC_TRIGGER_WAIT is ORed into the sequence number, the
  769. //! processor-initiated trigger is delayed until a later processor-initiated
  770. //! trigger to a different ADC module that specifies \b ADC_TRIGGER_SIGNAL,
  771. //! allowing multiple ADCs to start from a processor-initiated trigger in a
  772. //! synchronous manner.
  773. //!
  774. //! \return None.
  775. //
  776. //*****************************************************************************
  777. void
  778. ADCProcessorTrigger(uint32_t ui32Base, uint32_t ui32SequenceNum)
  779. {
  780. //
  781. // Check the arguments.
  782. //
  783. ASSERT((ui32Base == ADC0_BASE) || (ui32Base == ADC1_BASE));
  784. ASSERT(ui32SequenceNum < 4);
  785. //
  786. // Generate a processor trigger for this sample sequence.
  787. //
  788. HWREG(ui32Base + ADC_O_PSSI) |= ((ui32SequenceNum & 0xffff0000) |
  789. (1 << (ui32SequenceNum & 0xf)));
  790. }
  791. //*****************************************************************************
  792. //
  793. //! Configures the software oversampling factor of the ADC.
  794. //!
  795. //! \param ui32Base is the base address of the ADC module.
  796. //! \param ui32SequenceNum is the sample sequence number.
  797. //! \param ui32Factor is the number of samples to be averaged.
  798. //!
  799. //! This function configures the software oversampling for the ADC, which can
  800. //! be used to provide better resolution on the sampled data. Oversampling is
  801. //! accomplished by averaging multiple samples from the same analog input.
  802. //! Three different oversampling rates are supported; 2x, 4x, and 8x.
  803. //!
  804. //! Oversampling is only supported on the sample sequencers that are more than
  805. //! one sample in depth (that is, the fourth sample sequencer is not
  806. //! supported). Oversampling by 2x (for example) divides the depth of the
  807. //! sample sequencer by two; so 2x oversampling on the first sample sequencer
  808. //! can only provide four samples per trigger. This also means that 8x
  809. //! oversampling is only available on the first sample sequencer.
  810. //!
  811. //! \return None.
  812. //
  813. //*****************************************************************************
  814. void
  815. ADCSoftwareOversampleConfigure(uint32_t ui32Base, uint32_t ui32SequenceNum,
  816. uint32_t ui32Factor)
  817. {
  818. uint32_t ui32Value;
  819. uint32_t ui32ADCInst;
  820. //
  821. // Check the arguments.
  822. //
  823. ASSERT((ui32Base == ADC0_BASE) || (ui32Base == ADC1_BASE));
  824. ASSERT(ui32SequenceNum < 3);
  825. ASSERT(((ui32Factor == 2) || (ui32Factor == 4) || (ui32Factor == 8)) &&
  826. ((ui32SequenceNum == 0) || (ui32Factor != 8)));
  827. //
  828. // Convert the oversampling factor to a shift factor.
  829. //
  830. for (ui32Value = 0, ui32Factor >>= 1; ui32Factor;
  831. ui32Value++, ui32Factor >>= 1)
  832. {
  833. }
  834. //
  835. // Evaluate the ADC Instance.
  836. //
  837. if (ui32Base == ADC0_BASE)
  838. {
  839. ui32ADCInst = 0;
  840. }
  841. else
  842. {
  843. ui32ADCInst = 1;
  844. }
  845. //
  846. // Save the shift factor.
  847. //
  848. g_pui8OversampleFactor[ui32ADCInst][ui32SequenceNum] = ui32Value;
  849. }
  850. //*****************************************************************************
  851. //
  852. //! Configures a step of the software oversampled sequencer.
  853. //!
  854. //! \param ui32Base is the base address of the ADC module.
  855. //! \param ui32SequenceNum is the sample sequence number.
  856. //! \param ui32Step is the step to be configured.
  857. //! \param ui32Config is the configuration of this step.
  858. //!
  859. //! This function configures a step of the sample sequencer when using the
  860. //! software oversampling feature. The number of steps available depends on
  861. //! the oversampling factor set by ADCSoftwareOversampleConfigure(). The value
  862. //! of \e ui32Config is the same as defined for ADCSequenceStepConfigure().
  863. //!
  864. //! \return None.
  865. //
  866. //*****************************************************************************
  867. void
  868. ADCSoftwareOversampleStepConfigure(uint32_t ui32Base, uint32_t ui32SequenceNum,
  869. uint32_t ui32Step, uint32_t ui32Config)
  870. {
  871. uint32_t ui32ADCInst;
  872. //
  873. // Evaluate the ADC Instance.
  874. //
  875. if (ui32Base == ADC0_BASE)
  876. {
  877. ui32ADCInst = 0;
  878. }
  879. else
  880. {
  881. ui32ADCInst = 1;
  882. }
  883. //
  884. // Check the arguments.
  885. //
  886. ASSERT((ui32Base == ADC0_BASE) || (ui32Base == ADC1_BASE));
  887. ASSERT(ui32SequenceNum < 3);
  888. ASSERT(((ui32SequenceNum == 0) &&
  889. (ui32Step <
  890. (8 >> g_pui8OversampleFactor[ui32ADCInst][ui32SequenceNum]))) ||
  891. (ui32Step <
  892. (4 >> g_pui8OversampleFactor[ui32ADCInst][ui32SequenceNum])));
  893. //
  894. // Get the offset of the sequence to be configured.
  895. //
  896. ui32Base += ADC_SEQ + (ADC_SEQ_STEP * ui32SequenceNum);
  897. //
  898. // Compute the shift for the bits that control this step.
  899. //
  900. ui32Step *= 4 << g_pui8OversampleFactor[ui32ADCInst][ui32SequenceNum];
  901. //
  902. // Loop through the hardware steps that make up this step of the software
  903. // oversampled sequence.
  904. //
  905. for (ui32SequenceNum =
  906. (1 << g_pui8OversampleFactor[ui32ADCInst][ui32SequenceNum]);
  907. ui32SequenceNum; ui32SequenceNum--)
  908. {
  909. //
  910. // Set the analog mux value for this step.
  911. //
  912. HWREG(ui32Base + ADC_SSMUX) = ((HWREG(ui32Base + ADC_SSMUX) &
  913. ~(0x0000000f << ui32Step)) |
  914. ((ui32Config & 0x0f) << ui32Step));
  915. //
  916. // Set the upper bits of the analog mux value for this step.
  917. //
  918. HWREG(ui32Base + ADC_SSEMUX) = ((HWREG(ui32Base + ADC_SSEMUX) &
  919. ~(0x0000000f << ui32Step)) |
  920. (((ui32Config & 0xf00) >> 8) <<
  921. ui32Step));
  922. //
  923. // Set the control value for this step.
  924. //
  925. HWREG(ui32Base + ADC_SSCTL) = ((HWREG(ui32Base + ADC_SSCTL) &
  926. ~(0x0000000f << ui32Step)) |
  927. (((ui32Config & 0xf0) >> 4) <<
  928. ui32Step));
  929. if (ui32SequenceNum != 1)
  930. {
  931. HWREG(ui32Base + ADC_SSCTL) &= ~((ADC_SSCTL0_IE0 |
  932. ADC_SSCTL0_END0) << ui32Step);
  933. }
  934. //
  935. // Go to the next hardware step.
  936. //
  937. ui32Step += 4;
  938. }
  939. }
  940. //*****************************************************************************
  941. //
  942. //! Gets the captured data for a sample sequence using software oversampling.
  943. //!
  944. //! \param ui32Base is the base address of the ADC module.
  945. //! \param ui32SequenceNum is the sample sequence number.
  946. //! \param pui32Buffer is the address where the data is stored.
  947. //! \param ui32Count is the number of samples to be read.
  948. //!
  949. //! This function copies data from the specified sample sequence output FIFO to
  950. //! a memory resident buffer with software oversampling applied. The requested
  951. //! number of samples are copied into the data buffer; if there are not enough
  952. //! samples in the hardware FIFO to satisfy this many oversampled data items,
  953. //! then incorrect results are returned. It is the caller's responsibility to
  954. //! read only the samples that are available and wait until enough data is
  955. //! available, for example as a result of receiving an interrupt.
  956. //!
  957. //! \return None.
  958. //
  959. //*****************************************************************************
  960. void
  961. ADCSoftwareOversampleDataGet(uint32_t ui32Base, uint32_t ui32SequenceNum,
  962. uint32_t *pui32Buffer, uint32_t ui32Count)
  963. {
  964. uint32_t ui32Idx, ui32Accum;
  965. uint32_t ui32ADCInst;
  966. //
  967. // Evaluate the ADC Instance.
  968. //
  969. if (ui32Base == ADC0_BASE)
  970. {
  971. ui32ADCInst = 0;
  972. }
  973. else
  974. {
  975. ui32ADCInst = 1;
  976. }
  977. //
  978. // Check the arguments.
  979. //
  980. ASSERT((ui32Base == ADC0_BASE) || (ui32Base == ADC1_BASE));
  981. ASSERT(ui32SequenceNum < 3);
  982. ASSERT(((ui32SequenceNum == 0) &&
  983. (ui32Count <
  984. (8 >> g_pui8OversampleFactor[ui32ADCInst][ui32SequenceNum]))) ||
  985. (ui32Count <
  986. (4 >> g_pui8OversampleFactor[ui32ADCInst][ui32SequenceNum])));
  987. //
  988. // Get the offset of the sequence to be read.
  989. //
  990. ui32Base += ADC_SEQ + (ADC_SEQ_STEP * ui32SequenceNum);
  991. //
  992. // Read the samples from the FIFO until it is empty.
  993. //
  994. while (ui32Count--)
  995. {
  996. //
  997. // Compute the sum of the samples.
  998. //
  999. ui32Accum = 0;
  1000. for (ui32Idx = 1 << g_pui8OversampleFactor[ui32ADCInst][ui32SequenceNum];
  1001. ui32Idx; ui32Idx--)
  1002. {
  1003. //
  1004. // Read the FIFO and add it to the accumulator.
  1005. //
  1006. ui32Accum += HWREG(ui32Base + ADC_SSFIFO);
  1007. }
  1008. //
  1009. // Write the averaged sample to the output buffer.
  1010. //
  1011. *pui32Buffer++ =
  1012. ui32Accum >> g_pui8OversampleFactor[ui32ADCInst][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. //
  1499. // Note: The interrupt bits are "W1C" so we DO NOT use a logical OR
  1500. // here to clear the requested bits. Doing so would clear all outstanding
  1501. // interrupts rather than just those which the caller has specified.
  1502. //
  1503. HWREG(ui32Base + ADC_O_ISC) = ui32IntFlags;
  1504. }
  1505. //*****************************************************************************
  1506. //
  1507. //! Selects the ADC reference.
  1508. //!
  1509. //! \param ui32Base is the base address of the ADC module.
  1510. //! \param ui32Ref is the reference to use.
  1511. //!
  1512. //! The ADC reference is set as specified by \e ui32Ref. It must be one of
  1513. //! \b ADC_REF_INT, or \b ADC_REF_EXT_3V for internal or external reference
  1514. //! If \b ADC_REF_INT is chosen, then an internal 3V reference is used and
  1515. //! no external reference is needed. If \b ADC_REF_EXT_3V is chosen, then
  1516. //! a 3V reference must be supplied to the AVREF pin.
  1517. //!
  1518. //! \return None.
  1519. //
  1520. //*****************************************************************************
  1521. void
  1522. ADCReferenceSet(uint32_t ui32Base, uint32_t ui32Ref)
  1523. {
  1524. //
  1525. // Check the arguments.
  1526. //
  1527. ASSERT((ui32Base == ADC0_BASE) || (ui32Base == ADC1_BASE));
  1528. ASSERT((ui32Ref == ADC_REF_INT) || (ui32Ref == ADC_REF_EXT_3V));
  1529. //
  1530. // Set the reference.
  1531. //
  1532. HWREG(ui32Base + ADC_O_CTL) =
  1533. (HWREG(ui32Base + ADC_O_CTL) & ~ADC_CTL_VREF_M) | ui32Ref;
  1534. }
  1535. //*****************************************************************************
  1536. //
  1537. //! Returns the current setting of the ADC reference.
  1538. //!
  1539. //! \param ui32Base is the base address of the ADC module.
  1540. //!
  1541. //! Returns the value of the ADC reference setting. The returned value is one
  1542. //! of \b ADC_REF_INT, or \b ADC_REF_EXT_3V.
  1543. //!
  1544. //! \return The current setting of the ADC reference.
  1545. //
  1546. //*****************************************************************************
  1547. uint32_t
  1548. ADCReferenceGet(uint32_t ui32Base)
  1549. {
  1550. //
  1551. // Check the arguments.
  1552. //
  1553. ASSERT((ui32Base == ADC0_BASE) || (ui32Base == ADC1_BASE));
  1554. //
  1555. // Return the value of the reference.
  1556. //
  1557. return (HWREG(ui32Base + ADC_O_CTL) & ADC_CTL_VREF_M);
  1558. }
  1559. //*****************************************************************************
  1560. //
  1561. //! Sets the phase delay between a trigger and the start of a sequence.
  1562. //!
  1563. //! \param ui32Base is the base address of the ADC module.
  1564. //! \param ui32Phase is the phase delay, specified as one of \b ADC_PHASE_0,
  1565. //! \b ADC_PHASE_22_5, \b ADC_PHASE_45, \b ADC_PHASE_67_5, \b ADC_PHASE_90,
  1566. //! \b ADC_PHASE_112_5, \b ADC_PHASE_135, \b ADC_PHASE_157_5, \b ADC_PHASE_180,
  1567. //! \b ADC_PHASE_202_5, \b ADC_PHASE_225, \b ADC_PHASE_247_5, \b ADC_PHASE_270,
  1568. //! \b ADC_PHASE_292_5, \b ADC_PHASE_315, or \b ADC_PHASE_337_5.
  1569. //!
  1570. //! This function sets the phase delay between the detection of an ADC trigger
  1571. //! event and the start of the sample sequence. By selecting a different phase
  1572. //! delay for a pair of ADC modules (such as \b ADC_PHASE_0 and
  1573. //! \b ADC_PHASE_180) and having each ADC module sample the same analog input,
  1574. //! it is possible to increase the sampling rate of the analog input (with
  1575. //! samples N, N+2, N+4, and so on, coming from the first ADC and samples N+1,
  1576. //! N+3, N+5, and so on, coming from the second ADC). The ADC module has a
  1577. //! single phase delay that is applied to all sample sequences within that
  1578. //! module.
  1579. //!
  1580. //! \return None.
  1581. //
  1582. //*****************************************************************************
  1583. void
  1584. ADCPhaseDelaySet(uint32_t ui32Base, uint32_t ui32Phase)
  1585. {
  1586. //
  1587. // Check the arguments.
  1588. //
  1589. ASSERT((ui32Base == ADC0_BASE) || (ui32Base == ADC1_BASE));
  1590. ASSERT((ui32Phase == ADC_PHASE_0) || (ui32Phase == ADC_PHASE_22_5) ||
  1591. (ui32Phase == ADC_PHASE_45) || (ui32Phase == ADC_PHASE_67_5) ||
  1592. (ui32Phase == ADC_PHASE_90) || (ui32Phase == ADC_PHASE_112_5) ||
  1593. (ui32Phase == ADC_PHASE_135) || (ui32Phase == ADC_PHASE_157_5) ||
  1594. (ui32Phase == ADC_PHASE_180) || (ui32Phase == ADC_PHASE_202_5) ||
  1595. (ui32Phase == ADC_PHASE_225) || (ui32Phase == ADC_PHASE_247_5) ||
  1596. (ui32Phase == ADC_PHASE_270) || (ui32Phase == ADC_PHASE_292_5) ||
  1597. (ui32Phase == ADC_PHASE_315) || (ui32Phase == ADC_PHASE_337_5));
  1598. //
  1599. // Set the phase delay.
  1600. //
  1601. HWREG(ui32Base + ADC_O_SPC) = ui32Phase;
  1602. }
  1603. //*****************************************************************************
  1604. //
  1605. //! Gets the phase delay between a trigger and the start of a sequence.
  1606. //!
  1607. //! \param ui32Base is the base address of the ADC module.
  1608. //!
  1609. //! This function gets the current phase delay between the detection of an ADC
  1610. //! trigger event and the start of the sample sequence.
  1611. //!
  1612. //! \return Returns the phase delay, specified as one of \b ADC_PHASE_0,
  1613. //! \b ADC_PHASE_22_5, \b ADC_PHASE_45, \b ADC_PHASE_67_5, \b ADC_PHASE_90,
  1614. //! \b ADC_PHASE_112_5, \b ADC_PHASE_135, \b ADC_PHASE_157_5, \b ADC_PHASE_180,
  1615. //! \b ADC_PHASE_202_5, \b ADC_PHASE_225, \b ADC_PHASE_247_5, \b ADC_PHASE_270,
  1616. //! \b ADC_PHASE_292_5, \b ADC_PHASE_315, or \b ADC_PHASE_337_5.
  1617. //
  1618. //*****************************************************************************
  1619. uint32_t
  1620. ADCPhaseDelayGet(uint32_t ui32Base)
  1621. {
  1622. //
  1623. // Check the arguments.
  1624. //
  1625. ASSERT((ui32Base == ADC0_BASE) || (ui32Base == ADC1_BASE));
  1626. //
  1627. // Return the phase delay.
  1628. //
  1629. return (HWREG(ui32Base + ADC_O_SPC));
  1630. }
  1631. //*****************************************************************************
  1632. //
  1633. //! Enables DMA for sample sequencers.
  1634. //!
  1635. //! \param ui32Base is the base address of the ADC module.
  1636. //! \param ui32SequenceNum is the sample sequence number.
  1637. //!
  1638. //! Allows DMA requests to be generated based on the FIFO level of the sample
  1639. //! sequencer.
  1640. //!
  1641. //! \return None.
  1642. //
  1643. //*****************************************************************************
  1644. void
  1645. ADCSequenceDMAEnable(uint32_t ui32Base, uint32_t ui32SequenceNum)
  1646. {
  1647. //
  1648. // Check the arguments.
  1649. //
  1650. ASSERT((ui32Base == ADC0_BASE) || (ui32Base == ADC1_BASE));
  1651. ASSERT(ui32SequenceNum < 4);
  1652. //
  1653. // Enable the DMA on the specified sequencer.
  1654. //
  1655. HWREG(ui32Base + ADC_O_ACTSS) |= 0x100 << ui32SequenceNum;
  1656. }
  1657. //*****************************************************************************
  1658. //
  1659. //! Disables DMA for sample sequencers.
  1660. //!
  1661. //! \param ui32Base is the base address of the ADC module.
  1662. //! \param ui32SequenceNum is the sample sequence number.
  1663. //!
  1664. //! Prevents the specified sample sequencer from generating DMA requests.
  1665. //!
  1666. //! \return None.
  1667. //
  1668. //*****************************************************************************
  1669. void
  1670. ADCSequenceDMADisable(uint32_t ui32Base, uint32_t ui32SequenceNum)
  1671. {
  1672. //
  1673. // Check the arguments.
  1674. //
  1675. ASSERT((ui32Base == ADC0_BASE) || (ui32Base == ADC1_BASE));
  1676. ASSERT(ui32SequenceNum < 4);
  1677. //
  1678. // Disable the DMA on the specified sequencer.
  1679. //
  1680. HWREG(ui32Base + ADC_O_ACTSS) &= ~(0x100 << ui32SequenceNum);
  1681. }
  1682. //*****************************************************************************
  1683. //
  1684. //! Determines whether the ADC is busy or not.
  1685. //!
  1686. //! \param ui32Base is the base address of the ADC.
  1687. //!
  1688. //! This function allows the caller to determine whether or not the ADC is
  1689. //! currently sampling . If \b false is returned, then the ADC is not
  1690. //! sampling data.
  1691. //!
  1692. //! Use this function to detect that the ADC is finished sampling data before
  1693. //! putting the device into deep sleep. Before using this function, it is
  1694. //! highly recommended that the event trigger is changed to
  1695. //! \b ADC_TRIGGER_NEVER on all enabled sequencers to prevent the ADC from
  1696. //! starting after checking the busy status.
  1697. //!
  1698. //! \return Returns \b true if the ADC is sampling or \b false if all
  1699. //! samples are complete.
  1700. //
  1701. //*****************************************************************************
  1702. bool
  1703. ADCBusy(uint32_t ui32Base)
  1704. {
  1705. //
  1706. // Check the argument.
  1707. //
  1708. ASSERT((ui32Base == ADC0_BASE) || (ui32Base == ADC1_BASE));
  1709. //
  1710. // Determine if the ADC is busy.
  1711. //
  1712. return ((HWREG(ui32Base + ADC_O_ACTSS) & ADC_ACTSS_BUSY) ? true : false);
  1713. }
  1714. //*****************************************************************************
  1715. //
  1716. //! Sets the clock configuration for the ADC.
  1717. //!
  1718. //! \param ui32Base is the base address of the ADC to configure, which must
  1719. //! always be \b ADC0_BASE.
  1720. //! \param ui32Config is a combination of the \b ADC_CLOCK_SRC_ and
  1721. //! \b ADC_CLOCK_RATE_* values used to configure the ADC clock input.
  1722. //! \param ui32ClockDiv is the input clock divider for the clock selected by
  1723. //! the \b ADC_CLOCK_SRC value.
  1724. //!
  1725. //! This function is used to configure the input clock to the ADC modules. The
  1726. //! clock configuration is shared across ADC units so \e ui32Base must
  1727. //! always be \b ADC0_BASE. The \e ui32Config value is logical OR of one
  1728. //! of the \b ADC_CLOCK_RATE_ and one of the \b ADC_CLOCK_SRC_ values defined
  1729. //! below. The \b ADC_CLOCK_SRC_* values determine the input clock for the ADC.
  1730. //! Regardless of the source, the final frequency after dividing must be between
  1731. //! 16 and 32 MHz.
  1732. //!
  1733. //! - \b ADC_CLOCK_SRC_PLL - The main PLL output.
  1734. //! - \b ADC_CLOCK_SRC_ALTCLK - The output of the ALTCLK in the system control
  1735. //! module.
  1736. //! - \b ADC_CLOCK_SRC_MOSC - The external MOSC.
  1737. //!
  1738. //! \b ADC_CLOCK_RATE values control how often samples are provided back to the
  1739. //! application. The values are the following:
  1740. //!
  1741. //! - \b ADC_CLOCK_RATE_FULL - All samples.
  1742. //! - \b ADC_CLOCK_RATE_HALF - Every other sample.
  1743. //! - \b ADC_CLOCK_RATE_QUARTER - Every fourth sample.
  1744. //! - \b ADC_CLOCK_RATE_EIGHTH - Every either sample.
  1745. //!
  1746. //! The \e ui32ClockDiv parameter allows for dividing a higher frequency down
  1747. //! into the valid range for the ADCs. This parameter is typically only used
  1748. //! \b ADC_CLOCK_SRC_PLL option because it is the only clock value that can be
  1749. //! with the in the correct range to use the divider. The actual value ranges
  1750. //! from 1 to 64.
  1751. //!
  1752. //! \b Example: ADC Clock Configurations
  1753. //!
  1754. //! \verbatim
  1755. //!
  1756. //! //
  1757. //! // Configure the ADC to use ALTCLK and sample at half the rate.
  1758. //! //
  1759. //! ADCClockConfigSet(ADC0_BASE, ADC_CLOCK_SRC_ALTCLK | ADC_CLOCK_RATE_HALF, 1);
  1760. //!
  1761. //! ...
  1762. //!
  1763. //! //
  1764. //! // Configure the ADC to use PLL at 480 MHz divided by 24 to get an ADC
  1765. //! // clock of 20 MHz.
  1766. //! //
  1767. //! ADCClockConfigSet(ADC0_BASE, ADC_CLOCK_SRC_PLL | ADC_CLOCK_RATE_FULL, 24);
  1768. //! \endverbatim
  1769. //!
  1770. //! \return None.
  1771. //
  1772. //*****************************************************************************
  1773. void
  1774. ADCClockConfigSet(uint32_t ui32Base, uint32_t ui32Config,
  1775. uint32_t ui32ClockDiv)
  1776. {
  1777. //
  1778. // Check the argument.
  1779. //
  1780. ASSERT((ui32Base == ADC0_BASE) || (ui32Base == ADC1_BASE));
  1781. ASSERT((ui32ClockDiv - 1) <= (ADC_CC_CLKDIV_M >> ADC_CC_CLKDIV_S));
  1782. //
  1783. // A rate must be supplied.
  1784. //
  1785. ASSERT((ui32Config & ADC_CLOCK_RATE_FULL) != 0);
  1786. //
  1787. // Write the sample conversion rate.
  1788. //
  1789. HWREG(ui32Base + ADC_O_PC) = (ui32Config >> 4) & ADC_PC_SR_M;
  1790. //
  1791. // Write the clock select and divider.
  1792. //
  1793. HWREG(ui32Base + ADC_O_CC) = (ui32Config & ADC_CC_CS_M) |
  1794. (((ui32ClockDiv - 1) << ADC_CC_CLKDIV_S)) ;
  1795. }
  1796. //*****************************************************************************
  1797. //
  1798. //! Returns the clock configuration for the ADC.
  1799. //!
  1800. //! \param ui32Base is the base address of the ADC to configure, which must
  1801. //! always be \b ADC0_BASE.
  1802. //! \param pui32ClockDiv is a pointer to the input clock divider for the clock
  1803. //! selected by the \b ADC_CLOCK_SRC in use by the ADCs.
  1804. //!
  1805. //! This function returns the ADC clock configuration and the clock divider for
  1806. //! the ADCs.
  1807. //!
  1808. //! \b Example: Read the current ADC clock configuration.
  1809. //!
  1810. //! \verbatim
  1811. //! uint32_t ui32Config, ui32ClockDiv;
  1812. //!
  1813. //! //
  1814. //! // Read the current ADC clock configuration.
  1815. //! //
  1816. //! ui32Config = ADCClockConfigGet(ADC0_BASE, &ui32ClockDiv);
  1817. //! \endverbatim
  1818. //!
  1819. //! \return The current clock configuration of the ADC defined as a combination
  1820. //! of one of \b ADC_CLOCK_SRC_PLL,
  1821. //! \b ADC_CLOCK_SRC_MOSC, or \b ADC_CLOCK_SRC_ALTCLK logical ORed with one of
  1822. //! \b ADC_CLOCK_RATE_FULL, \b ADC_CLOCK_RATE_HALF, \b ADC_CLOCK_RATE_QUARTER,
  1823. //! or \b ADC_CLOCK_RATE_EIGHTH. See ADCClockConfigSet() for more information
  1824. //! on these values.
  1825. //
  1826. //*****************************************************************************
  1827. uint32_t
  1828. ADCClockConfigGet(uint32_t ui32Base, uint32_t *pui32ClockDiv)
  1829. {
  1830. uint32_t ui32Config;
  1831. //
  1832. // Check the argument.
  1833. //
  1834. ASSERT((ui32Base == ADC0_BASE) || (ui32Base == ADC1_BASE));
  1835. //
  1836. // Read the current configuration.
  1837. //
  1838. ui32Config = HWREG(ui32Base + ADC_O_CC);
  1839. //
  1840. // If the clock divider was requested provide the current value.
  1841. //
  1842. if (pui32ClockDiv)
  1843. {
  1844. *pui32ClockDiv =
  1845. ((ui32Config & ADC_CC_CLKDIV_M) >> ADC_CC_CLKDIV_S) + 1;
  1846. }
  1847. //
  1848. // Clear out the divider bits.
  1849. //
  1850. ui32Config &= ~ADC_CC_CLKDIV_M;
  1851. //
  1852. // Add in the sample interval to the configuration.
  1853. //
  1854. ui32Config |= (HWREG(ui32Base + ADC_O_PC) & ADC_PC_SR_M) << 4;
  1855. return (ui32Config);
  1856. }
  1857. //*****************************************************************************
  1858. //
  1859. // Close the Doxygen group.
  1860. //! @}
  1861. //
  1862. //*****************************************************************************