i2c.c 61 KB

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