i2c.c 64 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143
  1. //*****************************************************************************
  2. //
  3. // i2c.c - Driver for Inter-IC (I2C) bus block.
  4. //
  5. // Copyright (c) 2005-2014 Texas Instruments Incorporated. All rights reserved.
  6. // Software License Agreement
  7. //
  8. // Redistribution and use in source and binary forms, with or without
  9. // modification, are permitted provided that the following conditions
  10. // are met:
  11. //
  12. // Redistributions of source code must retain the above copyright
  13. // notice, this list of conditions and the following disclaimer.
  14. //
  15. // Redistributions in binary form must reproduce the above copyright
  16. // notice, this list of conditions and the following disclaimer in the
  17. // documentation and/or other materials provided with the
  18. // distribution.
  19. //
  20. // Neither the name of Texas Instruments Incorporated nor the names of
  21. // its contributors may be used to endorse or promote products derived
  22. // from this software without specific prior written permission.
  23. //
  24. // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  25. // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  26. // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  27. // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  28. // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  29. // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  30. // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  31. // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  32. // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  33. // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  34. // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  35. //
  36. // This is part of revision 2.1.0.12573 of the Tiva Peripheral Driver Library.
  37. //
  38. //*****************************************************************************
  39. //*****************************************************************************
  40. //
  41. //! \addtogroup i2c_api
  42. //! @{
  43. //
  44. //*****************************************************************************
  45. #include <stdbool.h>
  46. #include <stdint.h>
  47. #include "inc/hw_i2c.h"
  48. #include "inc/hw_ints.h"
  49. #include "inc/hw_memmap.h"
  50. #include "inc/hw_sysctl.h"
  51. #include "inc/hw_types.h"
  52. #include "driverlib/debug.h"
  53. #include "driverlib/i2c.h"
  54. #include "driverlib/interrupt.h"
  55. //*****************************************************************************
  56. //
  57. // A mapping of I2C base address to interrupt number.
  58. //
  59. //*****************************************************************************
  60. static const uint32_t g_ppui32I2CIntMap[][2] =
  61. {
  62. { I2C0_BASE, INT_I2C0_TM4C123 },
  63. { I2C1_BASE, INT_I2C1_TM4C123 },
  64. { I2C2_BASE, INT_I2C2_TM4C123 },
  65. { I2C3_BASE, INT_I2C3_TM4C123 },
  66. { I2C4_BASE, INT_I2C4_TM4C123 },
  67. { I2C5_BASE, INT_I2C5_TM4C123 },
  68. };
  69. static const int_fast8_t g_i8I2CIntMapRows =
  70. sizeof(g_ppui32I2CIntMap) / sizeof(g_ppui32I2CIntMap[0]);
  71. static const uint32_t g_ppui32I2CIntMapSnowflake[][2] =
  72. {
  73. { I2C0_BASE, INT_I2C0_TM4C129 },
  74. { I2C1_BASE, INT_I2C1_TM4C129 },
  75. { I2C2_BASE, INT_I2C2_TM4C129 },
  76. { I2C3_BASE, INT_I2C3_TM4C129 },
  77. { I2C4_BASE, INT_I2C4_TM4C129 },
  78. { I2C5_BASE, INT_I2C5_TM4C129 },
  79. { I2C6_BASE, INT_I2C6_TM4C129 },
  80. { I2C7_BASE, INT_I2C7_TM4C129 },
  81. { I2C8_BASE, INT_I2C8_TM4C129 },
  82. { I2C9_BASE, INT_I2C9_TM4C129 },
  83. };
  84. static const int_fast8_t g_i8I2CIntMapSnowflakeRows =
  85. sizeof(g_ppui32I2CIntMapSnowflake) / sizeof(g_ppui32I2CIntMapSnowflake[0]);
  86. //*****************************************************************************
  87. //
  88. //! \internal
  89. //! Checks an I2C base address.
  90. //!
  91. //! \param ui32Base is the base address of the I2C module.
  92. //!
  93. //! This function determines if a I2C module base address is valid.
  94. //!
  95. //! \return Returns \b true if the base address is valid and \b false
  96. //! otherwise.
  97. //
  98. //*****************************************************************************
  99. #ifdef DEBUG
  100. static bool
  101. _I2CBaseValid(uint32_t ui32Base)
  102. {
  103. return((ui32Base == I2C0_BASE) || (ui32Base == I2C1_BASE) ||
  104. (ui32Base == I2C2_BASE) || (ui32Base == I2C3_BASE) ||
  105. (ui32Base == I2C4_BASE) || (ui32Base == I2C5_BASE) ||
  106. (ui32Base == I2C6_BASE) || (ui32Base == I2C7_BASE) ||
  107. (ui32Base == I2C8_BASE) || (ui32Base == I2C9_BASE));
  108. }
  109. #endif
  110. //*****************************************************************************
  111. //
  112. //! \internal
  113. //! Gets the I2C interrupt number.
  114. //!
  115. //! \param ui32Base is the base address of the I2C module.
  116. //!
  117. //! Given a I2C base address, this function returns the corresponding
  118. //! interrupt number.
  119. //!
  120. //! \return Returns an I2C interrupt number, or 0 if \e ui32Base is invalid.
  121. //
  122. //*****************************************************************************
  123. static uint32_t
  124. _I2CIntNumberGet(uint32_t ui32Base)
  125. {
  126. int_fast8_t i8Idx, i8Rows;
  127. const uint32_t (*ppui32I2CIntMap)[2];
  128. //
  129. // Check the arguments.
  130. //
  131. ASSERT(_I2CBaseValid(ui32Base));
  132. ppui32I2CIntMap = g_ppui32I2CIntMap;
  133. i8Rows = g_i8I2CIntMapRows;
  134. if(CLASS_IS_TM4C129)
  135. {
  136. ppui32I2CIntMap = g_ppui32I2CIntMapSnowflake;
  137. i8Rows = g_i8I2CIntMapSnowflakeRows;
  138. }
  139. //
  140. // Loop through the table that maps I2C base addresses to interrupt
  141. // numbers.
  142. //
  143. for(i8Idx = 0; i8Idx < i8Rows; i8Idx++)
  144. {
  145. //
  146. // See if this base address matches.
  147. //
  148. if(ppui32I2CIntMap[i8Idx][0] == ui32Base)
  149. {
  150. //
  151. // Return the corresponding interrupt number.
  152. //
  153. return(ppui32I2CIntMap[i8Idx][1]);
  154. }
  155. }
  156. //
  157. // The base address could not be found, so return an error.
  158. //
  159. return(0);
  160. }
  161. //*****************************************************************************
  162. //
  163. //! Initializes the I2C Master block.
  164. //!
  165. //! \param ui32Base is the base address of the I2C module.
  166. //! \param ui32I2CClk is the rate of the clock supplied to the I2C module.
  167. //! \param bFast set up for fast data transfers.
  168. //!
  169. //! This function initializes operation of the I2C Master block by configuring
  170. //! the bus speed for the master and enabling the I2C Master block.
  171. //!
  172. //! If the parameter \e bFast is \b true, then the master block is set up to
  173. //! transfer data at 400 Kbps; otherwise, it is set up to transfer data at
  174. //! 100 Kbps. If Fast Mode Plus (1 Mbps) is desired, software should manually
  175. //! write the I2CMTPR after calling this function. For High Speed (3.4 Mbps)
  176. //! mode, a specific command is used to switch to the faster clocks after the
  177. //! initial communication with the slave is done at either 100 Kbps or
  178. //! 400 Kbps.
  179. //!
  180. //! The peripheral clock is the same as the processor clock. This value is
  181. //! returned by SysCtlClockGet(), or it can be explicitly hard coded if it is
  182. //! constant and known (to save the code/execution overhead of a call to
  183. //! SysCtlClockGet()).
  184. //!
  185. //! \return None.
  186. //
  187. //*****************************************************************************
  188. void
  189. I2CMasterInitExpClk(uint32_t ui32Base, uint32_t ui32I2CClk,
  190. bool bFast)
  191. {
  192. uint32_t ui32SCLFreq;
  193. uint32_t ui32TPR;
  194. //
  195. // Check the arguments.
  196. //
  197. ASSERT(_I2CBaseValid(ui32Base));
  198. //
  199. // Must enable the device before doing anything else.
  200. //
  201. I2CMasterEnable(ui32Base);
  202. //
  203. // Get the desired SCL speed.
  204. //
  205. if(bFast == true)
  206. {
  207. ui32SCLFreq = 400000;
  208. }
  209. else
  210. {
  211. ui32SCLFreq = 100000;
  212. }
  213. //
  214. // Compute the clock divider that achieves the fastest speed less than or
  215. // equal to the desired speed. The numerator is biased to favor a larger
  216. // clock divider so that the resulting clock is always less than or equal
  217. // to the desired clock, never greater.
  218. //
  219. ui32TPR = ((ui32I2CClk + (2 * 10 * ui32SCLFreq) - 1) /
  220. (2 * 10 * ui32SCLFreq)) - 1;
  221. HWREG(ui32Base + I2C_O_MTPR) = ui32TPR;
  222. //
  223. // Check to see if this I2C peripheral is High-Speed enabled. If yes, also
  224. // choose the fastest speed that is less than or equal to 3.4 Mbps.
  225. //
  226. if(HWREG(ui32Base + I2C_O_PP) & I2C_PP_HS)
  227. {
  228. ui32TPR = ((ui32I2CClk + (2 * 3 * 3400000) - 1) /
  229. (2 * 3 * 3400000)) - 1;
  230. HWREG(ui32Base + I2C_O_MTPR) = I2C_MTPR_HS | ui32TPR;
  231. }
  232. }
  233. //*****************************************************************************
  234. //
  235. //! Initializes the I2C Slave block.
  236. //!
  237. //! \param ui32Base is the base address of the I2C module.
  238. //! \param ui8SlaveAddr 7-bit slave address
  239. //!
  240. //! This function initializes operation of the I2C Slave block by configuring
  241. //! the slave address and enabling the I2C Slave block.
  242. //!
  243. //! The parameter \e ui8SlaveAddr is the value that is compared against the
  244. //! slave address sent by an I2C master.
  245. //!
  246. //! \return None.
  247. //
  248. //*****************************************************************************
  249. void
  250. I2CSlaveInit(uint32_t ui32Base, uint8_t ui8SlaveAddr)
  251. {
  252. //
  253. // Check the arguments.
  254. //
  255. ASSERT(_I2CBaseValid(ui32Base));
  256. ASSERT(!(ui8SlaveAddr & 0x80));
  257. //
  258. // Must enable the device before doing anything else.
  259. //
  260. I2CSlaveEnable(ui32Base);
  261. //
  262. // Set up the slave address.
  263. //
  264. HWREG(ui32Base + I2C_O_SOAR) = ui8SlaveAddr;
  265. }
  266. //*****************************************************************************
  267. //
  268. //! Sets the I2C slave address.
  269. //!
  270. //! \param ui32Base is the base address of the I2C module.
  271. //! \param ui8AddrNum determines which slave address is set.
  272. //! \param ui8SlaveAddr is the 7-bit slave address
  273. //!
  274. //! This function writes the specified slave address. The \e ui32AddrNum field
  275. //! dictates which slave address is configured. For example, a value of 0
  276. //! configures the primary address and a value of 1 configures the secondary.
  277. //!
  278. //! \note Not all Tiva devices support a secondary address. Please
  279. //! consult the device data sheet to determine if this feature is supported.
  280. //!
  281. //! \return None.
  282. //
  283. //*****************************************************************************
  284. void
  285. I2CSlaveAddressSet(uint32_t ui32Base, uint8_t ui8AddrNum, uint8_t ui8SlaveAddr)
  286. {
  287. //
  288. // Check the arguments.
  289. //
  290. ASSERT(_I2CBaseValid(ui32Base));
  291. ASSERT(!(ui8AddrNum > 1));
  292. ASSERT(!(ui8SlaveAddr & 0x80));
  293. //
  294. // Determine which slave address is being set.
  295. //
  296. switch(ui8AddrNum)
  297. {
  298. //
  299. // Set up the primary slave address.
  300. //
  301. case 0:
  302. {
  303. HWREG(ui32Base + I2C_O_SOAR) = ui8SlaveAddr;
  304. break;
  305. }
  306. //
  307. // Set up and enable the secondary slave address.
  308. //
  309. case 1:
  310. {
  311. HWREG(ui32Base + I2C_O_SOAR2) = I2C_SOAR2_OAR2EN | ui8SlaveAddr;
  312. break;
  313. }
  314. }
  315. }
  316. //*****************************************************************************
  317. //
  318. //! Enables the I2C Master block.
  319. //!
  320. //! \param ui32Base is the base address of the I2C module.
  321. //!
  322. //! This function enables operation of the I2C Master block.
  323. //!
  324. //! \return None.
  325. //
  326. //*****************************************************************************
  327. void
  328. I2CMasterEnable(uint32_t ui32Base)
  329. {
  330. //
  331. // Check the arguments.
  332. //
  333. ASSERT(_I2CBaseValid(ui32Base));
  334. //
  335. // Enable the master block.
  336. //
  337. HWREG(ui32Base + I2C_O_MCR) |= I2C_MCR_MFE;
  338. }
  339. //*****************************************************************************
  340. //
  341. //! Enables the I2C Slave block.
  342. //!
  343. //! \param ui32Base is the base address of the I2C module.
  344. //!
  345. //! This fucntion enables operation of the I2C Slave block.
  346. //!
  347. //! \return None.
  348. //
  349. //*****************************************************************************
  350. void
  351. I2CSlaveEnable(uint32_t ui32Base)
  352. {
  353. //
  354. // Check the arguments.
  355. //
  356. ASSERT(_I2CBaseValid(ui32Base));
  357. //
  358. // Enable the clock to the slave block.
  359. //
  360. HWREG(ui32Base + I2C_O_MCR) |= I2C_MCR_SFE;
  361. //
  362. // Enable the slave.
  363. //
  364. HWREG(ui32Base + I2C_O_SCSR) = I2C_SCSR_DA;
  365. }
  366. //*****************************************************************************
  367. //
  368. //! Disables the I2C master block.
  369. //!
  370. //! \param ui32Base is the base address of the I2C module.
  371. //!
  372. //! This function disables operation of the I2C master block.
  373. //!
  374. //! \return None.
  375. //
  376. //*****************************************************************************
  377. void
  378. I2CMasterDisable(uint32_t ui32Base)
  379. {
  380. //
  381. // Check the arguments.
  382. //
  383. ASSERT(_I2CBaseValid(ui32Base));
  384. //
  385. // Disable the master block.
  386. //
  387. HWREG(ui32Base + I2C_O_MCR) &= ~(I2C_MCR_MFE);
  388. }
  389. //*****************************************************************************
  390. //
  391. //! Disables the I2C slave block.
  392. //!
  393. //! \param ui32Base is the base address of the I2C module.
  394. //!
  395. //! This function disables operation of the I2C slave block.
  396. //!
  397. //! \return None.
  398. //
  399. //*****************************************************************************
  400. void
  401. I2CSlaveDisable(uint32_t ui32Base)
  402. {
  403. //
  404. // Check the arguments.
  405. //
  406. ASSERT(_I2CBaseValid(ui32Base));
  407. //
  408. // Disable the slave.
  409. //
  410. HWREG(ui32Base + I2C_O_SCSR) = 0;
  411. //
  412. // Disable the clock to the slave block.
  413. //
  414. HWREG(ui32Base + I2C_O_MCR) &= ~(I2C_MCR_SFE);
  415. }
  416. //*****************************************************************************
  417. //
  418. //! Registers an interrupt handler for the I2C module.
  419. //!
  420. //! \param ui32Base is the base address of the I2C module.
  421. //! \param pfnHandler is a pointer to the function to be called when the
  422. //! I2C interrupt occurs.
  423. //!
  424. //! This function sets the handler to be called when an I2C interrupt occurs.
  425. //! This function enables the global interrupt in the interrupt controller;
  426. //! specific I2C interrupts must be enabled via I2CMasterIntEnable() and
  427. //! I2CSlaveIntEnable(). If necessary, it is the interrupt handler's
  428. //! responsibility to clear the interrupt source via I2CMasterIntClear() and
  429. //! I2CSlaveIntClear().
  430. //!
  431. //! \sa IntRegister() for important information about registering interrupt
  432. //! handlers.
  433. //!
  434. //! \return None.
  435. //
  436. //*****************************************************************************
  437. void
  438. I2CIntRegister(uint32_t ui32Base, void (*pfnHandler)(void))
  439. {
  440. uint32_t ui32Int;
  441. //
  442. // Check the arguments.
  443. //
  444. ASSERT(_I2CBaseValid(ui32Base));
  445. //
  446. // Determine the interrupt number based on the I2C port.
  447. //
  448. ui32Int = _I2CIntNumberGet(ui32Base);
  449. ASSERT(ui32Int != 0);
  450. //
  451. // Register the interrupt handler, returning an error if an error occurs.
  452. //
  453. IntRegister(ui32Int, pfnHandler);
  454. //
  455. // Enable the I2C interrupt.
  456. //
  457. IntEnable(ui32Int);
  458. }
  459. //*****************************************************************************
  460. //
  461. //! Unregisters an interrupt handler for the I2C module.
  462. //!
  463. //! \param ui32Base is the base address of the I2C module.
  464. //!
  465. //! This function clears the handler to be called when an I2C interrupt
  466. //! occurs. This function also masks off the interrupt in the interrupt r
  467. //! controller so that the interrupt handler no longer is called.
  468. //!
  469. //! \sa IntRegister() for important information about registering interrupt
  470. //! handlers.
  471. //!
  472. //! \return None.
  473. //
  474. //*****************************************************************************
  475. void
  476. I2CIntUnregister(uint32_t ui32Base)
  477. {
  478. uint32_t ui32Int;
  479. //
  480. // Check the arguments.
  481. //
  482. ASSERT(_I2CBaseValid(ui32Base));
  483. //
  484. // Determine the interrupt number based on the I2C port.
  485. //
  486. ui32Int = _I2CIntNumberGet(ui32Base);
  487. ASSERT(ui32Int != 0);
  488. //
  489. // Disable the interrupt.
  490. //
  491. IntDisable(ui32Int);
  492. //
  493. // Unregister the interrupt handler.
  494. //
  495. IntUnregister(ui32Int);
  496. }
  497. //*****************************************************************************
  498. //
  499. //! Enables the I2C Master interrupt.
  500. //!
  501. //! \param ui32Base is the base address of the I2C module.
  502. //!
  503. //! This function enables the I2C Master interrupt source.
  504. //!
  505. //! \return None.
  506. //
  507. //*****************************************************************************
  508. void
  509. I2CMasterIntEnable(uint32_t ui32Base)
  510. {
  511. //
  512. // Check the arguments.
  513. //
  514. ASSERT(_I2CBaseValid(ui32Base));
  515. //
  516. // Enable the master interrupt.
  517. //
  518. HWREG(ui32Base + I2C_O_MIMR) = 1;
  519. }
  520. //*****************************************************************************
  521. //
  522. //! Enables individual I2C Master interrupt sources.
  523. //!
  524. //! \param ui32Base is the base address of the I2C module.
  525. //! \param ui32IntFlags is the bit mask of the interrupt sources to be enabled.
  526. //!
  527. //! This function enables the indicated I2C Master interrupt sources. Only the
  528. //! sources that are enabled can be reflected to the processor interrupt;
  529. //! disabled sources have no effect on the processor.
  530. //!
  531. //! The \e ui32IntFlags parameter is the logical OR of any of the following:
  532. //!
  533. //! - \b I2C_MASTER_INT_RX_FIFO_FULL - RX FIFO Full interrupt
  534. //! - \b I2C_MASTER_INT_TX_FIFO_EMPTY - TX FIFO Empty interrupt
  535. //! - \b I2C_MASTER_INT_RX_FIFO_REQ - RX FIFO Request interrupt
  536. //! - \b I2C_MASTER_INT_TX_FIFO_REQ - TX FIFO Request interrupt
  537. //! - \b I2C_MASTER_INT_ARB_LOST - Arbitration Lost interrupt
  538. //! - \b I2C_MASTER_INT_STOP - Stop Condition interrupt
  539. //! - \b I2C_MASTER_INT_START - Start Condition interrupt
  540. //! - \b I2C_MASTER_INT_NACK - Address/Data NACK interrupt
  541. //! - \b I2C_MASTER_INT_TX_DMA_DONE - TX DMA Complete interrupt
  542. //! - \b I2C_MASTER_INT_RX_DMA_DONE - RX DMA Complete interrupt
  543. //! - \b I2C_MASTER_INT_TIMEOUT - Clock Timeout interrupt
  544. //! - \b I2C_MASTER_INT_DATA - Data interrupt
  545. //!
  546. //! \note Not all Tiva devices support all of the listed interrupt
  547. //! sources. Please consult the device data sheet to determine if these
  548. //! features are supported.
  549. //!
  550. //! \return None.
  551. //
  552. //*****************************************************************************
  553. void
  554. I2CMasterIntEnableEx(uint32_t ui32Base, uint32_t ui32IntFlags)
  555. {
  556. //
  557. // Check the arguments.
  558. //
  559. ASSERT(_I2CBaseValid(ui32Base));
  560. //
  561. // Enable the master interrupt.
  562. //
  563. HWREG(ui32Base + I2C_O_MIMR) |= ui32IntFlags;
  564. }
  565. //*****************************************************************************
  566. //
  567. //! Enables the I2C Slave interrupt.
  568. //!
  569. //! \param ui32Base is the base address of the I2C module.
  570. //!
  571. //! This function enables the I2C Slave interrupt source.
  572. //!
  573. //! \return None.
  574. //
  575. //*****************************************************************************
  576. void
  577. I2CSlaveIntEnable(uint32_t ui32Base)
  578. {
  579. //
  580. // Check the arguments.
  581. //
  582. ASSERT(_I2CBaseValid(ui32Base));
  583. //
  584. // Enable the slave interrupt.
  585. //
  586. HWREG(ui32Base + I2C_O_SIMR) |= I2C_SLAVE_INT_DATA;
  587. }
  588. //*****************************************************************************
  589. //
  590. //! Enables individual I2C Slave interrupt sources.
  591. //!
  592. //! \param ui32Base is the base address of the I2C module.
  593. //! \param ui32IntFlags is the bit mask of the interrupt sources to be enabled.
  594. //!
  595. //! This function enables the indicated I2C Slave interrupt sources. Only the
  596. //! sources that are enabled can be reflected to the processor interrupt;
  597. //! disabled sources have no effect on the processor.
  598. //!
  599. //! The \e ui32IntFlags parameter is the logical OR of any of the following:
  600. //!
  601. //! - \b I2C_SLAVE_INT_RX_FIFO_FULL - RX FIFO Full interrupt
  602. //! - \b I2C_SLAVE_INT_TX_FIFO_EMPTY - TX FIFO Empty interrupt
  603. //! - \b I2C_SLAVE_INT_RX_FIFO_REQ - RX FIFO Request interrupt
  604. //! - \b I2C_SLAVE_INT_TX_FIFO_REQ - TX FIFO Request interrupt
  605. //! - \b I2C_SLAVE_INT_TX_DMA_DONE - TX DMA Complete interrupt
  606. //! - \b I2C_SLAVE_INT_RX_DMA_DONE - RX DMA Complete interrupt
  607. //! - \b I2C_SLAVE_INT_STOP - Stop condition detected interrupt
  608. //! - \b I2C_SLAVE_INT_START - Start condition detected interrupt
  609. //! - \b I2C_SLAVE_INT_DATA - Data interrupt
  610. //!
  611. //! \note Not all Tiva devices support the all of the listed interrupts.
  612. //! Please consult the device data sheet to determine if these features are
  613. //! supported.
  614. //!
  615. //! \return None.
  616. //
  617. //*****************************************************************************
  618. void
  619. I2CSlaveIntEnableEx(uint32_t ui32Base, uint32_t ui32IntFlags)
  620. {
  621. //
  622. // Check the arguments.
  623. //
  624. ASSERT(_I2CBaseValid(ui32Base));
  625. //
  626. // Enable the slave interrupt.
  627. //
  628. HWREG(ui32Base + I2C_O_SIMR) |= ui32IntFlags;
  629. }
  630. //*****************************************************************************
  631. //
  632. //! Disables the I2C Master interrupt.
  633. //!
  634. //! \param ui32Base is the base address of the I2C module.
  635. //!
  636. //! This function disables the I2C Master interrupt source.
  637. //!
  638. //! \return None.
  639. //
  640. //*****************************************************************************
  641. void
  642. I2CMasterIntDisable(uint32_t ui32Base)
  643. {
  644. //
  645. // Check the arguments.
  646. //
  647. ASSERT(_I2CBaseValid(ui32Base));
  648. //
  649. // Disable the master interrupt.
  650. //
  651. HWREG(ui32Base + I2C_O_MIMR) = 0;
  652. }
  653. //*****************************************************************************
  654. //
  655. //! Disables individual I2C Master interrupt sources.
  656. //!
  657. //! \param ui32Base is the base address of the I2C module.
  658. //! \param ui32IntFlags is the bit mask of the interrupt sources to be
  659. //! disabled.
  660. //!
  661. //! This function disables the indicated I2C Master interrupt sources. Only
  662. //! the sources that are enabled can be reflected to the processor interrupt;
  663. //! disabled sources have no effect on the processor.
  664. //!
  665. //! The \e ui32IntFlags parameter has the same definition as the
  666. //! \e ui32IntFlags parameter to I2CMasterIntEnableEx().
  667. //!
  668. //! \return None.
  669. //
  670. //*****************************************************************************
  671. void
  672. I2CMasterIntDisableEx(uint32_t ui32Base, uint32_t ui32IntFlags)
  673. {
  674. //
  675. // Check the arguments.
  676. //
  677. ASSERT(_I2CBaseValid(ui32Base));
  678. //
  679. // Disable the master interrupt.
  680. //
  681. HWREG(ui32Base + I2C_O_MIMR) &= ~ui32IntFlags;
  682. }
  683. //*****************************************************************************
  684. //
  685. //! Disables the I2C Slave interrupt.
  686. //!
  687. //! \param ui32Base is the base address of the I2C module.
  688. //!
  689. //! This function disables the I2C Slave interrupt source.
  690. //!
  691. //! \return None.
  692. //
  693. //*****************************************************************************
  694. void
  695. I2CSlaveIntDisable(uint32_t ui32Base)
  696. {
  697. //
  698. // Check the arguments.
  699. //
  700. ASSERT(_I2CBaseValid(ui32Base));
  701. //
  702. // Disable the slave interrupt.
  703. //
  704. HWREG(ui32Base + I2C_O_SIMR) &= ~I2C_SLAVE_INT_DATA;
  705. }
  706. //*****************************************************************************
  707. //
  708. //! Disables individual I2C Slave interrupt sources.
  709. //!
  710. //! \param ui32Base is the base address of the I2C module.
  711. //! \param ui32IntFlags is the bit mask of the interrupt sources to be
  712. //! disabled.
  713. //!
  714. //! This function disables the indicated I2C Slave interrupt sources. Only
  715. //! the sources that are enabled can be reflected to the processor interrupt;
  716. //! disabled sources have no effect on the processor.
  717. //!
  718. //! The \e ui32IntFlags parameter has the same definition as the
  719. //! \e ui32IntFlags parameter to I2CSlaveIntEnableEx().
  720. //!
  721. //! \return None.
  722. //
  723. //*****************************************************************************
  724. void
  725. I2CSlaveIntDisableEx(uint32_t ui32Base, uint32_t ui32IntFlags)
  726. {
  727. //
  728. // Check the arguments.
  729. //
  730. ASSERT(_I2CBaseValid(ui32Base));
  731. //
  732. // Disable the slave interrupt.
  733. //
  734. HWREG(ui32Base + I2C_O_SIMR) &= ~ui32IntFlags;
  735. }
  736. //*****************************************************************************
  737. //
  738. //! Gets the current I2C Master interrupt status.
  739. //!
  740. //! \param ui32Base is the base address of the I2C module.
  741. //! \param bMasked is false if the raw interrupt status is requested and
  742. //! true if the masked interrupt status is requested.
  743. //!
  744. //! This function returns the interrupt status for the I2C module.
  745. //! Either the raw interrupt status or the status of interrupts that are
  746. //! allowed to reflect to the processor can be returned.
  747. //!
  748. //! \return The current interrupt status, returned as \b true if active
  749. //! or \b false if not active.
  750. //
  751. //*****************************************************************************
  752. bool
  753. I2CMasterIntStatus(uint32_t ui32Base, bool bMasked)
  754. {
  755. //
  756. // Check the arguments.
  757. //
  758. ASSERT(_I2CBaseValid(ui32Base));
  759. //
  760. // Return either the interrupt status or the raw interrupt status as
  761. // requested.
  762. //
  763. if(bMasked)
  764. {
  765. return((HWREG(ui32Base + I2C_O_MMIS)) ? true : false);
  766. }
  767. else
  768. {
  769. return((HWREG(ui32Base + I2C_O_MRIS)) ? true : false);
  770. }
  771. }
  772. //*****************************************************************************
  773. //
  774. //! Gets the current I2C Master interrupt status.
  775. //!
  776. //! \param ui32Base is the base address of the I2C module.
  777. //! \param bMasked is false if the raw interrupt status is requested and
  778. //! true if the masked interrupt status is requested.
  779. //!
  780. //! This function returns the interrupt status for the I2C module.
  781. //! Either the raw interrupt status or the status of interrupts that are
  782. //! allowed to reflect to the processor can be returned.
  783. //!
  784. //! \return Returns the current interrupt status, enumerated as a bit field of
  785. //! values described in I2CMasterIntEnableEx().
  786. //
  787. //*****************************************************************************
  788. uint32_t
  789. I2CMasterIntStatusEx(uint32_t ui32Base, bool bMasked)
  790. {
  791. //
  792. // Check the arguments.
  793. //
  794. ASSERT(_I2CBaseValid(ui32Base));
  795. //
  796. // Return either the interrupt status or the raw interrupt status as
  797. // requested.
  798. //
  799. if(bMasked)
  800. {
  801. return(HWREG(ui32Base + I2C_O_MMIS));
  802. }
  803. else
  804. {
  805. return(HWREG(ui32Base + I2C_O_MRIS));
  806. }
  807. }
  808. //*****************************************************************************
  809. //
  810. //! Gets the current I2C Slave interrupt status.
  811. //!
  812. //! \param ui32Base is the base address of the I2C module.
  813. //! \param bMasked is false if the raw interrupt status is requested and
  814. //! true if the masked interrupt status is requested.
  815. //!
  816. //! This function returns the interrupt status for the I2C Slave.
  817. //! Either the raw interrupt status or the status of interrupts that are
  818. //! allowed to reflect to the processor can be returned.
  819. //!
  820. //! \return The current interrupt status, returned as \b true if active
  821. //! or \b false if not active.
  822. //
  823. //*****************************************************************************
  824. bool
  825. I2CSlaveIntStatus(uint32_t ui32Base, bool bMasked)
  826. {
  827. //
  828. // Check the arguments.
  829. //
  830. ASSERT(_I2CBaseValid(ui32Base));
  831. //
  832. // Return either the interrupt status or the raw interrupt status as
  833. // requested.
  834. //
  835. if(bMasked)
  836. {
  837. return((HWREG(ui32Base + I2C_O_SMIS)) ? true : false);
  838. }
  839. else
  840. {
  841. return((HWREG(ui32Base + I2C_O_SRIS)) ? true : false);
  842. }
  843. }
  844. //*****************************************************************************
  845. //
  846. //! Gets the current I2C Slave interrupt status.
  847. //!
  848. //! \param ui32Base is the base address of the I2C module.
  849. //! \param bMasked is false if the raw interrupt status is requested and
  850. //! true if the masked interrupt status is requested.
  851. //!
  852. //! This function returns the interrupt status for the I2C Slave.
  853. //! Either the raw interrupt status or the status of interrupts that are
  854. //! allowed to reflect to the processor can be returned.
  855. //!
  856. //! \return Returns the current interrupt status, enumerated as a bit field of
  857. //! values described in I2CSlaveIntEnableEx().
  858. //
  859. //*****************************************************************************
  860. uint32_t
  861. I2CSlaveIntStatusEx(uint32_t ui32Base, bool bMasked)
  862. {
  863. //
  864. // Check the arguments.
  865. //
  866. ASSERT(_I2CBaseValid(ui32Base));
  867. //
  868. // Return either the interrupt status or the raw interrupt status as
  869. // requested.
  870. //
  871. if(bMasked)
  872. {
  873. return(HWREG(ui32Base + I2C_O_SMIS));
  874. }
  875. else
  876. {
  877. return(HWREG(ui32Base + I2C_O_SRIS));
  878. }
  879. }
  880. //*****************************************************************************
  881. //
  882. //! Clears I2C Master interrupt sources.
  883. //!
  884. //! \param ui32Base is the base address of the I2C module.
  885. //!
  886. //! The I2C Master interrupt source is cleared, so that it no longer
  887. //! asserts. This function must be called in the interrupt handler to keep the
  888. //! interrupt from being triggered again immediately upon exit.
  889. //!
  890. //! \note Because there is a write buffer in the Cortex-M processor, it may
  891. //! take several clock cycles before the interrupt source is actually cleared.
  892. //! Therefore, it is recommended that the interrupt source be cleared early in
  893. //! the interrupt handler (as opposed to the very last action) to avoid
  894. //! returning from the interrupt handler before the interrupt source is
  895. //! actually cleared. Failure to do so may result in the interrupt handler
  896. //! being immediately reentered (because the interrupt controller still sees
  897. //! the interrupt source asserted).
  898. //!
  899. //! \return None.
  900. //
  901. //*****************************************************************************
  902. void
  903. I2CMasterIntClear(uint32_t ui32Base)
  904. {
  905. //
  906. // Check the arguments.
  907. //
  908. ASSERT(_I2CBaseValid(ui32Base));
  909. //
  910. // Clear the I2C master interrupt source.
  911. //
  912. HWREG(ui32Base + I2C_O_MICR) = I2C_MICR_IC;
  913. //
  914. // Workaround for I2C master interrupt clear errata for rev B Tiva
  915. // devices. For later devices, this write is ignored and therefore
  916. // harmless (other than the slight performance hit).
  917. //
  918. HWREG(ui32Base + I2C_O_MMIS) = I2C_MICR_IC;
  919. }
  920. //*****************************************************************************
  921. //
  922. //! Clears I2C Master interrupt sources.
  923. //!
  924. //! \param ui32Base is the base address of the I2C module.
  925. //! \param ui32IntFlags is a bit mask of the interrupt sources to be cleared.
  926. //!
  927. //! The specified I2C Master interrupt sources are cleared, so that they no
  928. //! longer assert. This function must be called in the interrupt handler to
  929. //! keep the interrupt from being triggered again immediately upon exit.
  930. //!
  931. //! The \e ui32IntFlags parameter has the same definition as the
  932. //! \e ui32IntFlags parameter to I2CMasterIntEnableEx().
  933. //!
  934. //! \note Because there is a write buffer in the Cortex-M processor, it may
  935. //! take several clock cycles before the interrupt source is actually cleared.
  936. //! Therefore, it is recommended that the interrupt source be cleared early in
  937. //! the interrupt handler (as opposed to the very last action) to avoid
  938. //! returning from the interrupt handler before the interrupt source is
  939. //! actually cleared. Failure to do so may result in the interrupt handler
  940. //! being immediately reentered (because the interrupt controller still sees
  941. //! the interrupt source asserted).
  942. //!
  943. //! \return None.
  944. //
  945. //*****************************************************************************
  946. void
  947. I2CMasterIntClearEx(uint32_t ui32Base, uint32_t ui32IntFlags)
  948. {
  949. //
  950. // Check the arguments.
  951. //
  952. ASSERT(_I2CBaseValid(ui32Base));
  953. //
  954. // Clear the I2C master interrupt source.
  955. //
  956. HWREG(ui32Base + I2C_O_MICR) = ui32IntFlags;
  957. }
  958. //*****************************************************************************
  959. //
  960. //! Clears I2C Slave interrupt sources.
  961. //!
  962. //! \param ui32Base is the base address of the I2C module.
  963. //!
  964. //! The I2C Slave interrupt source is cleared, so that it no longer asserts.
  965. //! This function must be called in the interrupt handler to keep the interrupt
  966. //! from being triggered again immediately upon exit.
  967. //!
  968. //! \note Because there is a write buffer in the Cortex-M processor, it may
  969. //! take several clock cycles before the interrupt source is actually cleared.
  970. //! Therefore, it is recommended that the interrupt source be cleared early in
  971. //! the interrupt handler (as opposed to the very last action) to avoid
  972. //! returning from the interrupt handler before the interrupt source is
  973. //! actually cleared. Failure to do so may result in the interrupt handler
  974. //! being immediately reentered (because the interrupt controller still sees
  975. //! the interrupt source asserted).
  976. //!
  977. //! \return None.
  978. //
  979. //*****************************************************************************
  980. void
  981. I2CSlaveIntClear(uint32_t ui32Base)
  982. {
  983. //
  984. // Check the arguments.
  985. //
  986. ASSERT(_I2CBaseValid(ui32Base));
  987. //
  988. // Clear the I2C slave interrupt source.
  989. //
  990. HWREG(ui32Base + I2C_O_SICR) = I2C_SICR_DATAIC;
  991. }
  992. //*****************************************************************************
  993. //
  994. //! Clears I2C Slave interrupt sources.
  995. //!
  996. //! \param ui32Base is the base address of the I2C module.
  997. //! \param ui32IntFlags is a bit mask of the interrupt sources to be cleared.
  998. //!
  999. //! The specified I2C Slave interrupt sources are cleared, so that they no
  1000. //! longer assert. This function must be called in the interrupt handler to
  1001. //! keep the interrupt from being triggered again immediately upon exit.
  1002. //!
  1003. //! The \e ui32IntFlags parameter has the same definition as the
  1004. //! \e ui32IntFlags parameter to I2CSlaveIntEnableEx().
  1005. //!
  1006. //! \note Because there is a write buffer in the Cortex-M processor, it may
  1007. //! take several clock cycles before the interrupt source is actually cleared.
  1008. //! Therefore, it is recommended that the interrupt source be cleared early in
  1009. //! the interrupt handler (as opposed to the very last action) to avoid
  1010. //! returning from the interrupt handler before the interrupt source is
  1011. //! actually cleared. Failure to do so may result in the interrupt handler
  1012. //! being immediately reentered (because the interrupt controller still sees
  1013. //! the interrupt source asserted).
  1014. //!
  1015. //! \return None.
  1016. //
  1017. //*****************************************************************************
  1018. void
  1019. I2CSlaveIntClearEx(uint32_t ui32Base, uint32_t ui32IntFlags)
  1020. {
  1021. //
  1022. // Check the arguments.
  1023. //
  1024. ASSERT(_I2CBaseValid(ui32Base));
  1025. //
  1026. // Clear the I2C slave interrupt source.
  1027. //
  1028. HWREG(ui32Base + I2C_O_SICR) = ui32IntFlags;
  1029. }
  1030. //*****************************************************************************
  1031. //
  1032. //! Sets the address that the I2C Master places on the bus.
  1033. //!
  1034. //! \param ui32Base is the base address of the I2C module.
  1035. //! \param ui8SlaveAddr 7-bit slave address
  1036. //! \param bReceive flag indicating the type of communication with the slave
  1037. //!
  1038. //! This function configures the address that the I2C Master places on the
  1039. //! bus when initiating a transaction. When the \e bReceive parameter is set
  1040. //! to \b true, the address indicates that the I2C Master is initiating a
  1041. //! read from the slave; otherwise the address indicates that the I2C
  1042. //! Master is initiating a write to the slave.
  1043. //!
  1044. //! \return None.
  1045. //
  1046. //*****************************************************************************
  1047. void
  1048. I2CMasterSlaveAddrSet(uint32_t ui32Base, uint8_t ui8SlaveAddr,
  1049. bool bReceive)
  1050. {
  1051. //
  1052. // Check the arguments.
  1053. //
  1054. ASSERT(_I2CBaseValid(ui32Base));
  1055. ASSERT(!(ui8SlaveAddr & 0x80));
  1056. //
  1057. // Set the address of the slave with which the master will communicate.
  1058. //
  1059. HWREG(ui32Base + I2C_O_MSA) = (ui8SlaveAddr << 1) | bReceive;
  1060. }
  1061. //*****************************************************************************
  1062. //
  1063. //! Reads the state of the SDA and SCL pins.
  1064. //!
  1065. //! \param ui32Base is the base address of the I2C module.
  1066. //!
  1067. //! This function returns the state of the I2C bus by providing the real time
  1068. //! values of the SDA and SCL pins.
  1069. //!
  1070. //! \note Not all Tiva devices support this function. Please consult the
  1071. //! device data sheet to determine if this feature is supported.
  1072. //!
  1073. //! \return Returns the state of the bus with SDA in bit position 1 and SCL in
  1074. //! bit position 0.
  1075. //
  1076. //*****************************************************************************
  1077. uint32_t
  1078. I2CMasterLineStateGet(uint32_t ui32Base)
  1079. {
  1080. //
  1081. // Check the arguments.
  1082. //
  1083. ASSERT(_I2CBaseValid(ui32Base));
  1084. //
  1085. // Return the line state.
  1086. //
  1087. return(HWREG(ui32Base + I2C_O_MBMON));
  1088. }
  1089. //*****************************************************************************
  1090. //
  1091. //! Indicates whether or not the I2C Master is busy.
  1092. //!
  1093. //! \param ui32Base is the base address of the I2C module.
  1094. //!
  1095. //! This function returns an indication of whether or not the I2C Master is
  1096. //! busy transmitting or receiving data.
  1097. //!
  1098. //! \return Returns \b true if the I2C Master is busy; otherwise, returns
  1099. //! \b false.
  1100. //
  1101. //*****************************************************************************
  1102. bool
  1103. I2CMasterBusy(uint32_t ui32Base)
  1104. {
  1105. //
  1106. // Check the arguments.
  1107. //
  1108. ASSERT(_I2CBaseValid(ui32Base));
  1109. //
  1110. // Return the busy status.
  1111. //
  1112. if(HWREG(ui32Base + I2C_O_MCS) & I2C_MCS_BUSY)
  1113. {
  1114. return(true);
  1115. }
  1116. else
  1117. {
  1118. return(false);
  1119. }
  1120. }
  1121. //*****************************************************************************
  1122. //
  1123. //! Indicates whether or not the I2C bus is busy.
  1124. //!
  1125. //! \param ui32Base is the base address of the I2C module.
  1126. //!
  1127. //! This function returns an indication of whether or not the I2C bus is busy.
  1128. //! This function can be used in a multi-master environment to determine if
  1129. //! another master is currently using the bus.
  1130. //!
  1131. //! \return Returns \b true if the I2C bus is busy; otherwise, returns
  1132. //! \b false.
  1133. //
  1134. //*****************************************************************************
  1135. bool
  1136. I2CMasterBusBusy(uint32_t ui32Base)
  1137. {
  1138. //
  1139. // Check the arguments.
  1140. //
  1141. ASSERT(_I2CBaseValid(ui32Base));
  1142. //
  1143. // Return the bus busy status.
  1144. //
  1145. if(HWREG(ui32Base + I2C_O_MCS) & I2C_MCS_BUSBSY)
  1146. {
  1147. return(true);
  1148. }
  1149. else
  1150. {
  1151. return(false);
  1152. }
  1153. }
  1154. //*****************************************************************************
  1155. //
  1156. //! Controls the state of the I2C Master.
  1157. //!
  1158. //! \param ui32Base is the base address of the I2C module.
  1159. //! \param ui32Cmd command to be issued to the I2C Master.
  1160. //!
  1161. //! This function is used to control the state of the Master send and
  1162. //! receive operations. The \e ui8Cmd parameter can be one of the following
  1163. //! values:
  1164. //!
  1165. //! - \b I2C_MASTER_CMD_SINGLE_SEND
  1166. //! - \b I2C_MASTER_CMD_SINGLE_RECEIVE
  1167. //! - \b I2C_MASTER_CMD_BURST_SEND_START
  1168. //! - \b I2C_MASTER_CMD_BURST_SEND_CONT
  1169. //! - \b I2C_MASTER_CMD_BURST_SEND_FINISH
  1170. //! - \b I2C_MASTER_CMD_BURST_SEND_ERROR_STOP
  1171. //! - \b I2C_MASTER_CMD_BURST_RECEIVE_START
  1172. //! - \b I2C_MASTER_CMD_BURST_RECEIVE_CONT
  1173. //! - \b I2C_MASTER_CMD_BURST_RECEIVE_FINISH
  1174. //! - \b I2C_MASTER_CMD_BURST_RECEIVE_ERROR_STOP
  1175. //! - \b I2C_MASTER_CMD_QUICK_COMMAND
  1176. //! - \b I2C_MASTER_CMD_HS_MASTER_CODE_SEND
  1177. //! - \b I2C_MASTER_CMD_FIFO_SINGLE_SEND
  1178. //! - \b I2C_MASTER_CMD_FIFO_SINGLE_RECEIVE
  1179. //! - \b I2C_MASTER_CMD_FIFO_BURST_SEND_START
  1180. //! - \b I2C_MASTER_CMD_FIFO_BURST_SEND_CONT
  1181. //! - \b I2C_MASTER_CMD_FIFO_BURST_SEND_FINISH
  1182. //! - \b I2C_MASTER_CMD_FIFO_BURST_SEND_ERROR_STOP
  1183. //! - \b I2C_MASTER_CMD_FIFO_BURST_RECEIVE_START
  1184. //! - \b I2C_MASTER_CMD_FIFO_BURST_RECEIVE_CONT
  1185. //! - \b I2C_MASTER_CMD_FIFO_BURST_RECEIVE_FINISH
  1186. //! - \b I2C_MASTER_CMD_FIFO_BURST_RECEIVE_ERROR_STOP
  1187. //!
  1188. //! \note Not all Tiva devices have an I2C FIFO and support the FIFO
  1189. //! commands. Please consult the device data sheet to determine if this
  1190. //! feature is supported.
  1191. //!
  1192. //! \return None.
  1193. //
  1194. //*****************************************************************************
  1195. void
  1196. I2CMasterControl(uint32_t ui32Base, uint32_t ui32Cmd)
  1197. {
  1198. //
  1199. // Check the arguments.
  1200. //
  1201. ASSERT(_I2CBaseValid(ui32Base));
  1202. ASSERT((ui32Cmd == I2C_MASTER_CMD_SINGLE_SEND) ||
  1203. (ui32Cmd == I2C_MASTER_CMD_SINGLE_RECEIVE) ||
  1204. (ui32Cmd == I2C_MASTER_CMD_BURST_SEND_START) ||
  1205. (ui32Cmd == I2C_MASTER_CMD_BURST_SEND_CONT) ||
  1206. (ui32Cmd == I2C_MASTER_CMD_BURST_SEND_FINISH) ||
  1207. (ui32Cmd == I2C_MASTER_CMD_BURST_SEND_ERROR_STOP) ||
  1208. (ui32Cmd == I2C_MASTER_CMD_BURST_RECEIVE_START) ||
  1209. (ui32Cmd == I2C_MASTER_CMD_BURST_RECEIVE_CONT) ||
  1210. (ui32Cmd == I2C_MASTER_CMD_BURST_RECEIVE_FINISH) ||
  1211. (ui32Cmd == I2C_MASTER_CMD_BURST_RECEIVE_ERROR_STOP) ||
  1212. (ui32Cmd == I2C_MASTER_CMD_QUICK_COMMAND) ||
  1213. (ui32Cmd == I2C_MASTER_CMD_FIFO_SINGLE_SEND) ||
  1214. (ui32Cmd == I2C_MASTER_CMD_FIFO_SINGLE_RECEIVE) ||
  1215. (ui32Cmd == I2C_MASTER_CMD_FIFO_BURST_SEND_START) ||
  1216. (ui32Cmd == I2C_MASTER_CMD_FIFO_BURST_SEND_CONT) ||
  1217. (ui32Cmd == I2C_MASTER_CMD_FIFO_BURST_SEND_FINISH) ||
  1218. (ui32Cmd == I2C_MASTER_CMD_FIFO_BURST_SEND_ERROR_STOP) ||
  1219. (ui32Cmd == I2C_MASTER_CMD_FIFO_BURST_RECEIVE_START) ||
  1220. (ui32Cmd == I2C_MASTER_CMD_FIFO_BURST_RECEIVE_CONT) ||
  1221. (ui32Cmd == I2C_MASTER_CMD_FIFO_BURST_RECEIVE_FINISH) ||
  1222. (ui32Cmd == I2C_MASTER_CMD_FIFO_BURST_RECEIVE_ERROR_STOP) ||
  1223. (ui32Cmd == I2C_MASTER_CMD_HS_MASTER_CODE_SEND));
  1224. //
  1225. // Send the command.
  1226. //
  1227. HWREG(ui32Base + I2C_O_MCS) = ui32Cmd;
  1228. }
  1229. //*****************************************************************************
  1230. //
  1231. //! Gets the error status of the I2C Master.
  1232. //!
  1233. //! \param ui32Base is the base address of the I2C module.
  1234. //!
  1235. //! This function is used to obtain the error status of the Master send
  1236. //! and receive operations.
  1237. //!
  1238. //! \return Returns the error status, as one of \b I2C_MASTER_ERR_NONE,
  1239. //! \b I2C_MASTER_ERR_ADDR_ACK, \b I2C_MASTER_ERR_DATA_ACK, or
  1240. //! \b I2C_MASTER_ERR_ARB_LOST.
  1241. //
  1242. //*****************************************************************************
  1243. uint32_t
  1244. I2CMasterErr(uint32_t ui32Base)
  1245. {
  1246. uint32_t ui32Err;
  1247. //
  1248. // Check the arguments.
  1249. //
  1250. ASSERT(_I2CBaseValid(ui32Base));
  1251. //
  1252. // Get the raw error state
  1253. //
  1254. ui32Err = HWREG(ui32Base + I2C_O_MCS);
  1255. //
  1256. // If the I2C master is busy, then all the other bit are invalid, and
  1257. // don't have an error to report.
  1258. //
  1259. if(ui32Err & I2C_MCS_BUSY)
  1260. {
  1261. return(I2C_MASTER_ERR_NONE);
  1262. }
  1263. //
  1264. // Check for errors.
  1265. //
  1266. if(ui32Err & (I2C_MCS_ERROR | I2C_MCS_ARBLST))
  1267. {
  1268. return(ui32Err & (I2C_MCS_ARBLST | I2C_MCS_DATACK | I2C_MCS_ADRACK));
  1269. }
  1270. else
  1271. {
  1272. return(I2C_MASTER_ERR_NONE);
  1273. }
  1274. }
  1275. //*****************************************************************************
  1276. //
  1277. //! Transmits a byte from the I2C Master.
  1278. //!
  1279. //! \param ui32Base is the base address of the I2C module.
  1280. //! \param ui8Data data to be transmitted from the I2C Master.
  1281. //!
  1282. //! This function places the supplied data into I2C Master Data Register.
  1283. //!
  1284. //! \return None.
  1285. //
  1286. //*****************************************************************************
  1287. void
  1288. I2CMasterDataPut(uint32_t ui32Base, uint8_t ui8Data)
  1289. {
  1290. //
  1291. // Check the arguments.
  1292. //
  1293. ASSERT(_I2CBaseValid(ui32Base));
  1294. //
  1295. // Write the byte.
  1296. //
  1297. HWREG(ui32Base + I2C_O_MDR) = ui8Data;
  1298. }
  1299. //*****************************************************************************
  1300. //
  1301. //! Receives a byte that has been sent to the I2C Master.
  1302. //!
  1303. //! \param ui32Base is the base address of the I2C module.
  1304. //!
  1305. //! This function reads a byte of data from the I2C Master Data Register.
  1306. //!
  1307. //! \return Returns the byte received from by the I2C Master, cast as an
  1308. //! uint32_t.
  1309. //
  1310. //*****************************************************************************
  1311. uint32_t
  1312. I2CMasterDataGet(uint32_t ui32Base)
  1313. {
  1314. //
  1315. // Check the arguments.
  1316. //
  1317. ASSERT(_I2CBaseValid(ui32Base));
  1318. //
  1319. // Read a byte.
  1320. //
  1321. return(HWREG(ui32Base + I2C_O_MDR));
  1322. }
  1323. //*****************************************************************************
  1324. //
  1325. //! Sets the Master clock timeout value.
  1326. //!
  1327. //! \param ui32Base is the base address of the I2C module.
  1328. //! \param ui32Value is the number of I2C clocks before the timeout is
  1329. //! asserted.
  1330. //!
  1331. //! This function enables and configures the clock low timeout feature in the
  1332. //! I2C peripheral. This feature is implemented as a 12-bit counter, with the
  1333. //! upper 8-bits being programmable. For example, to program a timeout of 20ms
  1334. //! with a 100-kHz SCL frequency, \e ui32Value is 0x7d.
  1335. //!
  1336. //! \note Not all Tiva devices support this function. Please consult the
  1337. //! device data sheet to determine if this feature is supported.
  1338. //!
  1339. //! \return None.
  1340. //
  1341. //*****************************************************************************
  1342. void
  1343. I2CMasterTimeoutSet(uint32_t ui32Base, uint32_t ui32Value)
  1344. {
  1345. //
  1346. // Check the arguments.
  1347. //
  1348. ASSERT(_I2CBaseValid(ui32Base));
  1349. //
  1350. // Write the timeout value.
  1351. //
  1352. HWREG(ui32Base + I2C_O_MCLKOCNT) = ui32Value;
  1353. }
  1354. //*****************************************************************************
  1355. //
  1356. //! Configures ACK override behavior of the I2C Slave.
  1357. //!
  1358. //! \param ui32Base is the base address of the I2C module.
  1359. //! \param bEnable enables or disables ACK override.
  1360. //!
  1361. //! This function enables or disables ACK override, allowing the user
  1362. //! application to drive the value on SDA during the ACK cycle.
  1363. //!
  1364. //! \note Not all Tiva devices support this function. Please consult the
  1365. //! device data sheet to determine if this feature is supported.
  1366. //!
  1367. //! \return None.
  1368. //
  1369. //*****************************************************************************
  1370. void
  1371. I2CSlaveACKOverride(uint32_t ui32Base, bool bEnable)
  1372. {
  1373. //
  1374. // Check the arguments.
  1375. //
  1376. ASSERT(_I2CBaseValid(ui32Base));
  1377. //
  1378. // Enable or disable based on bEnable.
  1379. //
  1380. if(bEnable)
  1381. {
  1382. HWREG(ui32Base + I2C_O_SACKCTL) |= I2C_SACKCTL_ACKOEN;
  1383. }
  1384. else
  1385. {
  1386. HWREG(ui32Base + I2C_O_SACKCTL) &= ~I2C_SACKCTL_ACKOEN;
  1387. }
  1388. }
  1389. //*****************************************************************************
  1390. //
  1391. //! Writes the ACK value.
  1392. //!
  1393. //! \param ui32Base is the base address of the I2C module.
  1394. //! \param bACK chooses whether to ACK (true) or NACK (false) the transfer.
  1395. //!
  1396. //! This function puts the desired ACK value on SDA during the ACK cycle. The
  1397. //! value written is only valid when ACK override is enabled using
  1398. //! I2CSlaveACKOverride().
  1399. //!
  1400. //! \return None.
  1401. //
  1402. //*****************************************************************************
  1403. void
  1404. I2CSlaveACKValueSet(uint32_t ui32Base, bool bACK)
  1405. {
  1406. //
  1407. // Check the arguments.
  1408. //
  1409. ASSERT(_I2CBaseValid(ui32Base));
  1410. //
  1411. // ACK or NACK based on the value of bACK.
  1412. //
  1413. if(bACK)
  1414. {
  1415. HWREG(ui32Base + I2C_O_SACKCTL) &= ~I2C_SACKCTL_ACKOVAL;
  1416. }
  1417. else
  1418. {
  1419. HWREG(ui32Base + I2C_O_SACKCTL) |= I2C_SACKCTL_ACKOVAL;
  1420. }
  1421. }
  1422. //*****************************************************************************
  1423. //
  1424. //! Gets the I2C Slave status
  1425. //!
  1426. //! \param ui32Base is the base address of the I2C module.
  1427. //!
  1428. //! This function returns the action requested from a master, if any.
  1429. //! Possible values are:
  1430. //!
  1431. //! - \b I2C_SLAVE_ACT_NONE
  1432. //! - \b I2C_SLAVE_ACT_RREQ
  1433. //! - \b I2C_SLAVE_ACT_TREQ
  1434. //! - \b I2C_SLAVE_ACT_RREQ_FBR
  1435. //! - \b I2C_SLAVE_ACT_OWN2SEL
  1436. //! - \b I2C_SLAVE_ACT_QCMD
  1437. //! - \b I2C_SLAVE_ACT_QCMD_DATA
  1438. //!
  1439. //! \note Not all Tiva devices support the second I2C slave's own address
  1440. //! or the quick command function. Please consult the device data sheet to
  1441. //! determine if these features are supported.
  1442. //!
  1443. //! \return Returns \b I2C_SLAVE_ACT_NONE to indicate that no action has been
  1444. //! requested of the I2C Slave, \b I2C_SLAVE_ACT_RREQ to indicate that
  1445. //! an I2C master has sent data to the I2C Slave, \b I2C_SLAVE_ACT_TREQ
  1446. //! to indicate that an I2C master has requested that the I2C Slave send
  1447. //! data, \b I2C_SLAVE_ACT_RREQ_FBR to indicate that an I2C master has sent
  1448. //! data to the I2C slave and the first byte following the slave's own address
  1449. //! has been received, \b I2C_SLAVE_ACT_OWN2SEL to indicate that the second I2C
  1450. //! slave address was matched, \b I2C_SLAVE_ACT_QCMD to indicate that a quick
  1451. //! command was received, and \b I2C_SLAVE_ACT_QCMD_DATA to indicate that the
  1452. //! data bit was set when the quick command was received.
  1453. //
  1454. //*****************************************************************************
  1455. uint32_t
  1456. I2CSlaveStatus(uint32_t ui32Base)
  1457. {
  1458. //
  1459. // Check the arguments.
  1460. //
  1461. ASSERT(_I2CBaseValid(ui32Base));
  1462. //
  1463. // Return the slave status.
  1464. //
  1465. return(HWREG(ui32Base + I2C_O_SCSR));
  1466. }
  1467. //*****************************************************************************
  1468. //
  1469. //! Transmits a byte from the I2C Slave.
  1470. //!
  1471. //! \param ui32Base is the base address of the I2C module.
  1472. //! \param ui8Data is the data to be transmitted from the I2C Slave
  1473. //!
  1474. //! This function places the supplied data into I2C Slave Data Register.
  1475. //!
  1476. //! \return None.
  1477. //
  1478. //*****************************************************************************
  1479. void
  1480. I2CSlaveDataPut(uint32_t ui32Base, uint8_t ui8Data)
  1481. {
  1482. //
  1483. // Check the arguments.
  1484. //
  1485. ASSERT(_I2CBaseValid(ui32Base));
  1486. //
  1487. // Write the byte.
  1488. //
  1489. HWREG(ui32Base + I2C_O_SDR) = ui8Data;
  1490. }
  1491. //*****************************************************************************
  1492. //
  1493. //! Receives a byte that has been sent to the I2C Slave.
  1494. //!
  1495. //! \param ui32Base is the base address of the I2C module.
  1496. //!
  1497. //! This function reads a byte of data from the I2C Slave Data Register.
  1498. //!
  1499. //! \return Returns the byte received from by the I2C Slave, cast as an
  1500. //! uint32_t.
  1501. //
  1502. //*****************************************************************************
  1503. uint32_t
  1504. I2CSlaveDataGet(uint32_t ui32Base)
  1505. {
  1506. //
  1507. // Check the arguments.
  1508. //
  1509. ASSERT(_I2CBaseValid(ui32Base));
  1510. //
  1511. // Read a byte.
  1512. //
  1513. return(HWREG(ui32Base + I2C_O_SDR));
  1514. }
  1515. //*****************************************************************************
  1516. //
  1517. //! Configures the I2C transmit (TX) FIFO.
  1518. //!
  1519. //! \param ui32Base is the base address of the I2C module.
  1520. //! \param ui32Config is the configuration of the FIFO using specified macros.
  1521. //!
  1522. //! This configures the I2C peripheral's transmit FIFO. The transmit FIFO can
  1523. //! be used by the master or slave, but not both. The following macros are
  1524. //! used to configure the TX FIFO behavior for master or slave, with or without
  1525. //! DMA:
  1526. //!
  1527. //! \b I2C_FIFO_CFG_TX_MASTER, \b I2C_FIFO_CFG_TX_SLAVE,
  1528. //! \b I2C_FIFO_CFG_TX_MASTER_DMA, \b I2C_FIFO_CFG_TX_SLAVE_DMA
  1529. //!
  1530. //! To select the trigger level, one of the following macros should be used:
  1531. //!
  1532. //! \b I2C_FIFO_CFG_TX_TRIG_1, \b I2C_FIFO_CFG_TX_TRIG_2,
  1533. //! \b I2C_FIFO_CFG_TX_TRIG_3, \b I2C_FIFO_CFG_TX_TRIG_4,
  1534. //! \b I2C_FIFO_CFG_TX_TRIG_5, \b I2C_FIFO_CFG_TX_TRIG_6,
  1535. //! \b I2C_FIFO_CFG_TX_TRIG_7, \b I2C_FIFO_CFG_TX_TRIG_8
  1536. //!
  1537. //! \note Not all Tiva devices have an I2C FIFO. Please consult the
  1538. //! device data sheet to determine if this feature is supported.
  1539. //!
  1540. //! \return None.
  1541. //
  1542. //*****************************************************************************
  1543. void
  1544. I2CTxFIFOConfigSet(uint32_t ui32Base, uint32_t ui32Config)
  1545. {
  1546. //
  1547. // Check the arguments.
  1548. //
  1549. ASSERT(_I2CBaseValid(ui32Base));
  1550. //
  1551. // Clear transmit configuration data.
  1552. //
  1553. HWREG(ui32Base + I2C_O_FIFOCTL) &= 0xffff0000;
  1554. //
  1555. // Store new transmit configuration data.
  1556. //
  1557. HWREG(ui32Base + I2C_O_FIFOCTL) |= ui32Config;
  1558. }
  1559. //*****************************************************************************
  1560. //
  1561. //! Flushes the transmit (TX) FIFO.
  1562. //!
  1563. //! \param ui32Base is the base address of the I2C module.
  1564. //!
  1565. //! This function flushes the I2C transmit FIFO.
  1566. //!
  1567. //! \note Not all Tiva devices have an I2C FIFO. Please consult the
  1568. //! device data sheet to determine if this feature is supported.
  1569. //!
  1570. //! \return None.
  1571. //
  1572. //*****************************************************************************
  1573. void
  1574. I2CTxFIFOFlush(uint32_t ui32Base)
  1575. {
  1576. //
  1577. // Check the arguments.
  1578. //
  1579. ASSERT(_I2CBaseValid(ui32Base));
  1580. //
  1581. // Flush the TX FIFO.
  1582. //
  1583. HWREG(ui32Base + I2C_O_FIFOCTL) |= I2C_FIFOCTL_TXFLUSH;
  1584. }
  1585. //*****************************************************************************
  1586. //
  1587. //! Configures the I2C receive (RX) FIFO.
  1588. //!
  1589. //! \param ui32Base is the base address of the I2C module.
  1590. //! \param ui32Config is the configuration of the FIFO using specified macros.
  1591. //!
  1592. //! This configures the I2C peripheral's receive FIFO. The receive FIFO can be
  1593. //! used by the master or slave, but not both. The following macros are used
  1594. //! to configure the RX FIFO behavior for master or slave, with or without DMA:
  1595. //!
  1596. //! \b I2C_FIFO_CFG_RX_MASTER, \b I2C_FIFO_CFG_RX_SLAVE,
  1597. //! \b I2C_FIFO_CFG_RX_MASTER_DMA, \b I2C_FIFO_CFG_RX_SLAVE_DMA
  1598. //!
  1599. //! To select the trigger level, one of the following macros should be used:
  1600. //!
  1601. //! \b I2C_FIFO_CFG_RX_TRIG_1, \b I2C_FIFO_CFG_RX_TRIG_2,
  1602. //! \b I2C_FIFO_CFG_RX_TRIG_3, \b I2C_FIFO_CFG_RX_TRIG_4,
  1603. //! \b I2C_FIFO_CFG_RX_TRIG_5, \b I2C_FIFO_CFG_RX_TRIG_6,
  1604. //! \b I2C_FIFO_CFG_RX_TRIG_7, \b I2C_FIFO_CFG_RX_TRIG_8
  1605. //!
  1606. //! \note Not all Tiva devices have an I2C FIFO. Please consult the
  1607. //! device data sheet to determine if this feature is supported.
  1608. //!
  1609. //! \return None.
  1610. //
  1611. //*****************************************************************************
  1612. void
  1613. I2CRxFIFOConfigSet(uint32_t ui32Base, uint32_t ui32Config)
  1614. {
  1615. //
  1616. // Check the arguments.
  1617. //
  1618. ASSERT(_I2CBaseValid(ui32Base));
  1619. //
  1620. // Clear receive configuration data.
  1621. //
  1622. HWREG(ui32Base + I2C_O_FIFOCTL) &= 0x0000ffff;
  1623. //
  1624. // Store new receive configuration data.
  1625. //
  1626. HWREG(ui32Base + I2C_O_FIFOCTL) |= ui32Config;
  1627. }
  1628. //*****************************************************************************
  1629. //
  1630. //! Flushes the receive (RX) FIFO.
  1631. //!
  1632. //! \param ui32Base is the base address of the I2C module.
  1633. //!
  1634. //! This function flushes the I2C receive FIFO.
  1635. //!
  1636. //! \note Not all Tiva devices have an I2C FIFO. Please consult the
  1637. //! device data sheet to determine if this feature is supported.
  1638. //!
  1639. //! \return None.
  1640. //
  1641. //*****************************************************************************
  1642. void
  1643. I2CRxFIFOFlush(uint32_t ui32Base)
  1644. {
  1645. //
  1646. // Check the arguments.
  1647. //
  1648. ASSERT(_I2CBaseValid(ui32Base));
  1649. //
  1650. // Flush the TX FIFO.
  1651. //
  1652. HWREG(ui32Base + I2C_O_FIFOCTL) |= I2C_FIFOCTL_RXFLUSH;
  1653. }
  1654. //*****************************************************************************
  1655. //
  1656. //! Gets the current FIFO status.
  1657. //!
  1658. //! \param ui32Base is the base address of the I2C module.
  1659. //!
  1660. //! This function retrieves the status for both the transmit (TX) and receive
  1661. //! (RX) FIFOs. The trigger level for the transmit FIFO is set using
  1662. //! I2CTxFIFOConfigSet() and for the receive FIFO using I2CTxFIFOConfigSet().
  1663. //!
  1664. //! \note Not all Tiva devices have an I2C FIFO. Please consult the
  1665. //! device data sheet to determine if this feature is supported.
  1666. //!
  1667. //! \return Returns the FIFO status, enumerated as a bit field containing
  1668. //! \b I2C_FIFO_RX_BELOW_TRIG_LEVEL, \b I2C_FIFO_RX_FULL, \b I2C_FIFO_RX_EMPTY,
  1669. //! \b I2C_FIFO_TX_BELOW_TRIG_LEVEL, \b I2C_FIFO_TX_FULL, and
  1670. //! \b I2C_FIFO_TX_EMPTY.
  1671. //
  1672. //*****************************************************************************
  1673. uint32_t
  1674. I2CFIFOStatus(uint32_t ui32Base)
  1675. {
  1676. //
  1677. // Check the arguments.
  1678. //
  1679. ASSERT(_I2CBaseValid(ui32Base));
  1680. //
  1681. // Return the contents of the FIFO status register.
  1682. //
  1683. return(HWREG(ui32Base + I2C_O_FIFOSTATUS));
  1684. }
  1685. //*****************************************************************************
  1686. //
  1687. //! Writes a data byte to the I2C transmit FIFO.
  1688. //!
  1689. //! \param ui32Base is the base address of the I2C module.
  1690. //! \param ui8Data is the data to be placed into the transmit FIFO.
  1691. //!
  1692. //! This function adds a byte of data to the I2C transmit FIFO. If there is
  1693. //! no space available in the FIFO, this function waits for space to become
  1694. //! available before returning.
  1695. //!
  1696. //! \note Not all Tiva devices have an I2C FIFO. Please consult the
  1697. //! device data sheet to determine if this feature is supported.
  1698. //!
  1699. //! \return None.
  1700. //
  1701. //*****************************************************************************
  1702. void
  1703. I2CFIFODataPut(uint32_t ui32Base, uint8_t ui8Data)
  1704. {
  1705. //
  1706. // Check the arguments.
  1707. //
  1708. ASSERT(_I2CBaseValid(ui32Base));
  1709. //
  1710. // Wait until there is space.
  1711. //
  1712. while(HWREG(ui32Base + I2C_O_FIFOSTATUS) & I2C_FIFOSTATUS_TXFF)
  1713. {
  1714. }
  1715. //
  1716. // Place data into the FIFO.
  1717. //
  1718. HWREG(ui32Base + I2C_O_FIFODATA) = ui8Data;
  1719. }
  1720. //*****************************************************************************
  1721. //
  1722. //! Writes a data byte to the I2C transmit FIFO.
  1723. //!
  1724. //! \param ui32Base is the base address of the I2C module.
  1725. //! \param ui8Data is the data to be placed into the transmit FIFO.
  1726. //!
  1727. //! This function adds a byte of data to the I2C transmit FIFO. If there is
  1728. //! no space available in the FIFO, this function returns a zero.
  1729. //!
  1730. //! \note Not all Tiva devices have an I2C FIFO. Please consult the
  1731. //! device data sheet to determine if this feature is supported.
  1732. //!
  1733. //! \return The number of elements added to the I2C transmit FIFO.
  1734. //
  1735. //*****************************************************************************
  1736. uint32_t
  1737. I2CFIFODataPutNonBlocking(uint32_t ui32Base, uint8_t ui8Data)
  1738. {
  1739. //
  1740. // Check the arguments.
  1741. //
  1742. ASSERT(_I2CBaseValid(ui32Base));
  1743. //
  1744. // If FIFO is full, return zero.
  1745. //
  1746. if(HWREG(ui32Base + I2C_O_FIFOSTATUS) & I2C_FIFOSTATUS_TXFF)
  1747. {
  1748. return(0);
  1749. }
  1750. else
  1751. {
  1752. HWREG(ui32Base + I2C_O_FIFODATA) = ui8Data;
  1753. return(1);
  1754. }
  1755. }
  1756. //*****************************************************************************
  1757. //
  1758. //! Reads a byte from the I2C receive FIFO.
  1759. //!
  1760. //! \param ui32Base is the base address of the I2C module.
  1761. //!
  1762. //! This function reads a byte of data from I2C receive FIFO and places it in
  1763. //! the location specified by the \e pui8Data parameter. If there is no data
  1764. //! available, this function waits until data is received before returning.
  1765. //!
  1766. //! \note Not all Tiva devices have an I2C FIFO. Please consult the
  1767. //! device data sheet to determine if this feature is supported.
  1768. //!
  1769. //! \return The data byte.
  1770. //
  1771. //*****************************************************************************
  1772. uint32_t
  1773. I2CFIFODataGet(uint32_t ui32Base)
  1774. {
  1775. //
  1776. // Check the arguments.
  1777. //
  1778. ASSERT(_I2CBaseValid(ui32Base));
  1779. //
  1780. // Wait until there is data to read.
  1781. //
  1782. while(HWREG(ui32Base + I2C_O_FIFOSTATUS) & I2C_FIFOSTATUS_RXFE)
  1783. {
  1784. }
  1785. //
  1786. // Read a byte.
  1787. //
  1788. return(HWREG(ui32Base + I2C_O_FIFODATA));
  1789. }
  1790. //*****************************************************************************
  1791. //
  1792. //! Reads a byte from the I2C receive FIFO.
  1793. //!
  1794. //! \param ui32Base is the base address of the I2C module.
  1795. //! \param pui8Data is a pointer where the read data is stored.
  1796. //!
  1797. //! This function reads a byte of data from I2C receive FIFO and places it in
  1798. //! the location specified by the \e pui8Data parameter. If there is no data
  1799. //! available, this functions returns 0.
  1800. //!
  1801. //! \note Not all Tiva devices have an I2C FIFO. Please consult the
  1802. //! device data sheet to determine if this feature is supported.
  1803. //!
  1804. //! \return The number of elements read from the I2C receive FIFO.
  1805. //
  1806. //*****************************************************************************
  1807. uint32_t
  1808. I2CFIFODataGetNonBlocking(uint32_t ui32Base, uint8_t *pui8Data)
  1809. {
  1810. //
  1811. // Check the arguments.
  1812. //
  1813. ASSERT(_I2CBaseValid(ui32Base));
  1814. //
  1815. // If nothing in the FIFO, return zero.
  1816. //
  1817. if(HWREG(ui32Base + I2C_O_FIFOSTATUS) & I2C_FIFOSTATUS_RXFE)
  1818. {
  1819. return(0);
  1820. }
  1821. else
  1822. {
  1823. *pui8Data = HWREG(ui32Base + I2C_O_FIFODATA);
  1824. return(1);
  1825. }
  1826. }
  1827. //*****************************************************************************
  1828. //
  1829. //! Set the burst length for a I2C master FIFO operation.
  1830. //!
  1831. //! \param ui32Base is the base address of the I2C module.
  1832. //! \param ui8Length is the length of the burst transfer.
  1833. //!
  1834. //! This function configures the burst length for a I2C Master FIFO operation.
  1835. //! The burst field is limited to 8 bits or 256 bytes. The burst length
  1836. //! applies to a single I2CMCS BURST operation meaning that it specifies the
  1837. //! burst length for only the current operation (can be TX or RX). Each burst
  1838. //! operation must configure the burst length prior to writing the BURST bit
  1839. //! in the I2CMCS using I2CMasterControl().
  1840. //!
  1841. //! \note Not all Tiva devices have an I2C FIFO. Please consult the
  1842. //! device data sheet to determine if this feature is supported.
  1843. //!
  1844. //! \return None.
  1845. //
  1846. //*****************************************************************************
  1847. void
  1848. I2CMasterBurstLengthSet(uint32_t ui32Base, uint8_t ui8Length)
  1849. {
  1850. //
  1851. // Check the arguments.
  1852. //
  1853. ASSERT(_I2CBaseValid(ui32Base) && (ui8Length < 255));
  1854. //
  1855. // Set the burst length.
  1856. //
  1857. HWREG(ui32Base + I2C_O_MBLEN) = ui8Length;
  1858. }
  1859. //*****************************************************************************
  1860. //
  1861. //! Returns the current value of the burst transfer counter.
  1862. //!
  1863. //! \param ui32Base is the base address of the I2C module.
  1864. //!
  1865. //! This function returns the current value of the burst transfer counter that
  1866. //! is used by the FIFO mechanism. Software can use this value to determine
  1867. //! how many bytes remain in a transfer, or where in the transfer the burst
  1868. //! operation was if an error has occurred.
  1869. //!
  1870. //! \note Not all Tiva devices have an I2C FIFO. Please consult the
  1871. //! device data sheet to determine if this feature is supported.
  1872. //!
  1873. //! \return None.
  1874. //
  1875. //*****************************************************************************
  1876. uint32_t
  1877. I2CMasterBurstCountGet(uint32_t ui32Base)
  1878. {
  1879. //
  1880. // Check the arguments.
  1881. //
  1882. ASSERT(_I2CBaseValid(ui32Base));
  1883. //
  1884. // Get burst count.
  1885. //
  1886. return(HWREG(ui32Base + I2C_O_MBCNT));
  1887. }
  1888. //*****************************************************************************
  1889. //
  1890. //! Configures the I2C Master glitch filter.
  1891. //!
  1892. //! \param ui32Base is the base address of the I2C module.
  1893. //! \param ui32Config is the glitch filter configuration.
  1894. //!
  1895. //! This function configures the I2C Master glitch filter. The value passed in
  1896. //! to \e ui32Config determines the sampling range of the glitch filter, which
  1897. //! is configurable between 1 and 32 system clock cycles. The default
  1898. //! configuration of the glitch filter is 0 system clock cycles, which means
  1899. //! that it's disabled.
  1900. //!
  1901. //! The \e ui32Config field should be any of the following values:
  1902. //!
  1903. //! - \b I2C_MASTER_GLITCH_FILTER_DISABLED
  1904. //! - \b I2C_MASTER_GLITCH_FILTER_1
  1905. //! - \b I2C_MASTER_GLITCH_FILTER_2
  1906. //! - \b I2C_MASTER_GLITCH_FILTER_3
  1907. //! - \b I2C_MASTER_GLITCH_FILTER_4
  1908. //! - \b I2C_MASTER_GLITCH_FILTER_8
  1909. //! - \b I2C_MASTER_GLITCH_FILTER_16
  1910. //! - \b I2C_MASTER_GLITCH_FILTER_32
  1911. //!
  1912. //! \note Not all Tiva devices support this function. Please consult the
  1913. //! device data sheet to determine if this feature is supported.
  1914. //!
  1915. //! \return None.
  1916. //
  1917. //*****************************************************************************
  1918. void
  1919. I2CMasterGlitchFilterConfigSet(uint32_t ui32Base, uint32_t ui32Config)
  1920. {
  1921. //
  1922. // Check the arguments.
  1923. //
  1924. ASSERT(_I2CBaseValid(ui32Base));
  1925. //
  1926. // Configure the glitch filter field of MTPR.
  1927. //
  1928. HWREG(ui32Base + I2C_O_MTPR) |= ui32Config;
  1929. }
  1930. //*****************************************************************************
  1931. //
  1932. //! Enables FIFO usage for the I2C Slave.
  1933. //!
  1934. //! \param ui32Base is the base address of the I2C module.
  1935. //! \param ui32Config is the desired FIFO configuration of the I2C Slave.
  1936. //!
  1937. //! This function configures the I2C Slave to use the FIFO(s). This
  1938. //! function should be used in combination with I2CTxFIFOConfigSet() and/or
  1939. //! I2CRxFIFOConfigSet(), which configure the FIFO trigger level and tell
  1940. //! the FIFO hardware whether to interact with the I2C Master or Slave. The
  1941. //! application appropriate combination of \b I2C_SLAVE_TX_FIFO_ENABLE and
  1942. //! \b I2C_SLAVE_RX_FIFO_ENABLE should be passed in to the \e ui32Config
  1943. //! field.
  1944. //!
  1945. //! The Slave I2CSCSR register is write-only, so any call to I2CSlaveEnable(),
  1946. //! I2CSlaveDisable or I2CSlaveFIFOEnable() overwrites the slave configuration.
  1947. //! Therefore, application software should call I2CSlaveEnable() followed by
  1948. //! I2CSlaveFIFOEnable() with the desired FIFO configuration.
  1949. //!
  1950. //! \note Not all Tiva devices have an I2C FIFO. Please consult the
  1951. //! device data sheet to determine if this feature is supported.
  1952. //!
  1953. //! \return None.
  1954. //
  1955. //*****************************************************************************
  1956. void
  1957. I2CSlaveFIFOEnable(uint32_t ui32Base, uint32_t ui32Config)
  1958. {
  1959. //
  1960. // Check the arguments.
  1961. //
  1962. ASSERT(_I2CBaseValid(ui32Base));
  1963. //
  1964. // Enable the FIFOs for the slave.
  1965. //
  1966. HWREG(ui32Base + I2C_O_SCSR) = ui32Config | I2C_SCSR_DA;
  1967. }
  1968. //*****************************************************************************
  1969. //
  1970. //! Disable FIFO usage for the I2C Slave.
  1971. //!
  1972. //! \param ui32Base is the base address of the I2C module.
  1973. //!
  1974. //! This function disables the FIFOs for the I2C Slave. After calling this
  1975. //! this function, the FIFOs are disabled, but the Slave remains active.
  1976. //!
  1977. //! \note Not all Tiva devices have an I2C FIFO. Please consult the
  1978. //! device data sheet to determine if this feature is supported.
  1979. //!
  1980. //! \return None.
  1981. //
  1982. //*****************************************************************************
  1983. void
  1984. I2CSlaveFIFODisable(uint32_t ui32Base)
  1985. {
  1986. //
  1987. // Check the arguments.
  1988. //
  1989. ASSERT(_I2CBaseValid(ui32Base));
  1990. //
  1991. // Disable slave FIFOs.
  1992. //
  1993. HWREG(ui32Base + I2C_O_SCSR) = I2C_SCSR_DA;
  1994. }
  1995. //*****************************************************************************
  1996. //
  1997. // Close the Doxygen group.
  1998. //! @}
  1999. //
  2000. //*****************************************************************************