i2c.c 60 KB

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