i2c.c 66 KB

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