i2c.c 46 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533
  1. //*****************************************************************************
  2. //
  3. // i2c.c - Driver for Inter-IC (I2C) bus block.
  4. //
  5. // Copyright (c) 2005-2011 Texas Instruments Incorporated. All rights reserved.
  6. // Software License Agreement
  7. //
  8. // Texas Instruments (TI) is supplying this software for use solely and
  9. // exclusively on TI's microcontroller products. The software is owned by
  10. // TI and/or its suppliers, and is protected under applicable copyright
  11. // laws. You may not combine this software with "viral" open-source
  12. // software in order to form a larger program.
  13. //
  14. // THIS SOFTWARE IS PROVIDED "AS IS" AND WITH ALL FAULTS.
  15. // NO WARRANTIES, WHETHER EXPRESS, IMPLIED OR STATUTORY, INCLUDING, BUT
  16. // NOT LIMITED TO, IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  17. // A PARTICULAR PURPOSE APPLY TO THIS SOFTWARE. TI SHALL NOT, UNDER ANY
  18. // CIRCUMSTANCES, BE LIABLE FOR SPECIAL, INCIDENTAL, OR CONSEQUENTIAL
  19. // DAMAGES, FOR ANY REASON WHATSOEVER.
  20. //
  21. // This is part of revision 8049 of the Stellaris Peripheral Driver Library.
  22. //
  23. //*****************************************************************************
  24. //*****************************************************************************
  25. //
  26. //! \addtogroup i2c_api
  27. //! @{
  28. //
  29. //*****************************************************************************
  30. #include "inc/hw_i2c.h"
  31. #include "inc/hw_ints.h"
  32. #include "inc/hw_memmap.h"
  33. #include "inc/hw_sysctl.h"
  34. #include "inc/hw_types.h"
  35. #include "driverlib/debug.h"
  36. #include "driverlib/i2c.h"
  37. #include "driverlib/interrupt.h"
  38. //*****************************************************************************
  39. //
  40. // A mapping of I2C base address to interupt number.
  41. //
  42. //*****************************************************************************
  43. static const unsigned long g_ppulI2CIntMap[][2] =
  44. {
  45. { I2C0_MASTER_BASE, INT_I2C0 },
  46. { I2C1_MASTER_BASE, INT_I2C1 },
  47. { I2C2_MASTER_BASE, INT_I2C2 },
  48. { I2C3_MASTER_BASE, INT_I2C3 },
  49. { I2C4_MASTER_BASE, INT_I2C4 },
  50. { I2C5_MASTER_BASE, INT_I2C5 },
  51. };
  52. //*****************************************************************************
  53. //
  54. //! \internal
  55. //! Checks a I2C master base address.
  56. //!
  57. //! \param ulBase is the base address of the I2C Master module.
  58. //!
  59. //! This function determines if a I2C master module base address is valid.
  60. //!
  61. //! \return Returns \b true if the base address is valid and \b false
  62. //! otherwise.
  63. //
  64. //*****************************************************************************
  65. #ifdef DEBUG
  66. static tBoolean
  67. I2CMasterBaseValid(unsigned long ulBase)
  68. {
  69. return((ulBase == I2C0_MASTER_BASE) || (ulBase == I2C1_MASTER_BASE) ||
  70. (ulBase == I2C2_MASTER_BASE) || (ulBase == I2C3_MASTER_BASE) ||
  71. (ulBase == I2C4_MASTER_BASE) || (ulBase == I2C5_MASTER_BASE));
  72. }
  73. #endif
  74. //*****************************************************************************
  75. //
  76. //! \internal
  77. //! Checks a I2C slave base address.
  78. //!
  79. //! \param ulBase is the base address of the I2C Slave module.
  80. //!
  81. //! This function determines if a I2C slave module base address is valid.
  82. //!
  83. //! \return Returns \b true if the base address is valid and \b false
  84. //! otherwise.
  85. //
  86. //*****************************************************************************
  87. #ifdef DEBUG
  88. static tBoolean
  89. I2CSlaveBaseValid(unsigned long ulBase)
  90. {
  91. return((ulBase == I2C0_SLAVE_BASE) || (ulBase == I2C1_SLAVE_BASE) ||
  92. (ulBase == I2C2_SLAVE_BASE) || (ulBase == I2C3_SLAVE_BASE) ||
  93. (ulBase == I2C4_SLAVE_BASE) || (ulBase == I2C5_SLAVE_BASE));
  94. }
  95. #endif
  96. //*****************************************************************************
  97. //
  98. //! \internal
  99. //! Gets the I2C interrupt number.
  100. //!
  101. //! \param ulBase is the base address of the I2C Master module.
  102. //!
  103. //! Given a I2C base address, returns the corresponding interrupt number.
  104. //!
  105. //! \return Returns a I2C interrupt number, or -1 if \e ulBase is invalid.
  106. //
  107. //*****************************************************************************
  108. static long
  109. I2CIntNumberGet(unsigned long ulBase)
  110. {
  111. unsigned long ulIdx;
  112. //
  113. // Loop through the table that maps I2C base addresses to interrupt
  114. // numbers.
  115. //
  116. for(ulIdx = 0; ulIdx < (sizeof(g_ppulI2CIntMap) /
  117. sizeof(g_ppulI2CIntMap[0])); ulIdx++)
  118. {
  119. //
  120. // See if this base address matches.
  121. //
  122. if(g_ppulI2CIntMap[ulIdx][0] == ulBase)
  123. {
  124. //
  125. // Return the corresponding interrupt number.
  126. //
  127. return(g_ppulI2CIntMap[ulIdx][1]);
  128. }
  129. }
  130. //
  131. // The base address could not be found, so return an error.
  132. //
  133. return(-1);
  134. }
  135. //*****************************************************************************
  136. //
  137. //! Initializes the I2C Master block.
  138. //!
  139. //! \param ulBase is the base address of the I2C Master module.
  140. //! \param ulI2CClk is the rate of the clock supplied to the I2C module.
  141. //! \param bFast set up for fast data transfers
  142. //!
  143. //! This function initializes operation of the I2C Master block. Upon
  144. //! successful initialization of the I2C block, this function will have set the
  145. //! bus speed for the master, and will have enabled the I2C Master block.
  146. //!
  147. //! If the parameter \e bFast is \b true, then the master block is set up to
  148. //! transfer data at 400 kbps; otherwise, it is set up to transfer data at
  149. //! 100 kbps.
  150. //!
  151. //! The peripheral clock is the same as the processor clock. This is the value
  152. //! returned by SysCtlClockGet(), or it can be explicitly hard coded if it is
  153. //! constant and known (to save the code/execution overhead of a call to
  154. //! SysCtlClockGet()).
  155. //!
  156. //! This function replaces the original I2CMasterInit() API and performs the
  157. //! same actions. A macro is provided in <tt>i2c.h</tt> to map the original
  158. //! API to this API.
  159. //!
  160. //! \return None.
  161. //
  162. //*****************************************************************************
  163. void
  164. I2CMasterInitExpClk(unsigned long ulBase, unsigned long ulI2CClk,
  165. tBoolean bFast)
  166. {
  167. unsigned long ulSCLFreq;
  168. unsigned long ulTPR;
  169. //
  170. // Check the arguments.
  171. //
  172. ASSERT(I2CMasterBaseValid(ulBase));
  173. //
  174. // Must enable the device before doing anything else.
  175. //
  176. I2CMasterEnable(ulBase);
  177. //
  178. // Get the desired SCL speed.
  179. //
  180. if(bFast == true)
  181. {
  182. ulSCLFreq = 400000;
  183. }
  184. else
  185. {
  186. ulSCLFreq = 100000;
  187. }
  188. //
  189. // Compute the clock divider that achieves the fastest speed less than or
  190. // equal to the desired speed. The numerator is biased to favor a larger
  191. // clock divider so that the resulting clock is always less than or equal
  192. // to the desired clock, never greater.
  193. //
  194. ulTPR = ((ulI2CClk + (2 * 10 * ulSCLFreq) - 1) / (2 * 10 * ulSCLFreq)) - 1;
  195. HWREG(ulBase + I2C_O_MTPR) = ulTPR;
  196. }
  197. //*****************************************************************************
  198. //
  199. //! Initializes the I2C Slave block.
  200. //!
  201. //! \param ulBase is the base address of the I2C Slave module.
  202. //! \param ucSlaveAddr 7-bit slave address
  203. //!
  204. //! This function initializes operation of the I2C Slave block. Upon
  205. //! successful initialization of the I2C blocks, this function will have set
  206. //! the slave address and have enabled the I2C Slave block.
  207. //!
  208. //! The parameter \e ucSlaveAddr is the value that is compared against the
  209. //! slave address sent by an I2C master.
  210. //!
  211. //! \return None.
  212. //
  213. //*****************************************************************************
  214. void
  215. I2CSlaveInit(unsigned long ulBase, unsigned char ucSlaveAddr)
  216. {
  217. //
  218. // Check the arguments.
  219. //
  220. ASSERT(I2CSlaveBaseValid(ulBase));
  221. ASSERT(!(ucSlaveAddr & 0x80));
  222. //
  223. // Must enable the device before doing anything else.
  224. //
  225. I2CSlaveEnable(ulBase);
  226. //
  227. // Set up the slave address.
  228. //
  229. HWREG(ulBase + I2C_O_SOAR) = ucSlaveAddr;
  230. }
  231. //*****************************************************************************
  232. //
  233. //! Sets the I2C slave address.
  234. //!
  235. //! \param ulBase is the base address of the I2C Slave module.
  236. //! \param ucAddrNum determines which slave address is set.
  237. //! \param ucSlaveAddr 7-bit slave address
  238. //!
  239. //! This function writes the specified slave address. The \e ulAddrNum field
  240. //! dictates which slave address is configured. For example, a value of 0
  241. //! configures the primary address and a value of 1 the secondary.
  242. //!
  243. //! \return None.
  244. //
  245. //*****************************************************************************
  246. void
  247. I2CSlaveAddressSet(unsigned long ulBase, unsigned char ucAddrNum,
  248. unsigned char ucSlaveAddr)
  249. {
  250. //
  251. // Check the arguments.
  252. //
  253. ASSERT(I2CSlaveBaseValid(ulBase));
  254. ASSERT(!(ucAddrNum > 1));
  255. ASSERT(!(ucSlaveAddr & 0x80));
  256. //
  257. // Determine which slave address is being set.
  258. //
  259. switch(ucAddrNum)
  260. {
  261. //
  262. // Set up the primary slave address.
  263. //
  264. case 0:
  265. {
  266. HWREG(ulBase + I2C_O_SOAR) = ucSlaveAddr;
  267. break;
  268. }
  269. //
  270. // Set up and enable the secondary slave address.
  271. //
  272. case 1:
  273. {
  274. HWREG(ulBase + I2C_O_SOAR2) = I2C_SOAR2_OAR2EN | ucSlaveAddr;
  275. break;
  276. }
  277. }
  278. }
  279. //*****************************************************************************
  280. //
  281. //! Enables the I2C Master block.
  282. //!
  283. //! \param ulBase is the base address of the I2C Master module.
  284. //!
  285. //! This will enable operation of the I2C Master block.
  286. //!
  287. //! \return None.
  288. //
  289. //*****************************************************************************
  290. void
  291. I2CMasterEnable(unsigned long ulBase)
  292. {
  293. //
  294. // Check the arguments.
  295. //
  296. ASSERT(I2CMasterBaseValid(ulBase));
  297. //
  298. // Enable the master block.
  299. //
  300. HWREG(ulBase + I2C_O_MCR) |= I2C_MCR_MFE;
  301. }
  302. //*****************************************************************************
  303. //
  304. //! Enables the I2C Slave block.
  305. //!
  306. //! \param ulBase is the base address of the I2C Slave module.
  307. //!
  308. //! This will enable operation of the I2C Slave block.
  309. //!
  310. //! \return None.
  311. //
  312. //*****************************************************************************
  313. void
  314. I2CSlaveEnable(unsigned long ulBase)
  315. {
  316. //
  317. // Check the arguments.
  318. //
  319. ASSERT(I2CSlaveBaseValid(ulBase));
  320. //
  321. // Enable the clock to the slave block.
  322. //
  323. HWREG(ulBase - I2C0_SLAVE_BASE + I2C0_MASTER_BASE + I2C_O_MCR) |=
  324. I2C_MCR_SFE;
  325. //
  326. // Enable the slave.
  327. //
  328. HWREG(ulBase + I2C_O_SCSR) = I2C_SCSR_DA;
  329. }
  330. //*****************************************************************************
  331. //
  332. //! Disables the I2C master block.
  333. //!
  334. //! \param ulBase is the base address of the I2C Master module.
  335. //!
  336. //! This will disable operation of the I2C master block.
  337. //!
  338. //! \return None.
  339. //
  340. //*****************************************************************************
  341. void
  342. I2CMasterDisable(unsigned long ulBase)
  343. {
  344. //
  345. // Check the arguments.
  346. //
  347. ASSERT(I2CMasterBaseValid(ulBase));
  348. //
  349. // Disable the master block.
  350. //
  351. HWREG(ulBase + I2C_O_MCR) &= ~(I2C_MCR_MFE);
  352. }
  353. //*****************************************************************************
  354. //
  355. //! Disables the I2C slave block.
  356. //!
  357. //! \param ulBase is the base address of the I2C Slave module.
  358. //!
  359. //! This will disable operation of the I2C slave block.
  360. //!
  361. //! \return None.
  362. //
  363. //*****************************************************************************
  364. void
  365. I2CSlaveDisable(unsigned long ulBase)
  366. {
  367. //
  368. // Check the arguments.
  369. //
  370. ASSERT(I2CSlaveBaseValid(ulBase));
  371. //
  372. // Disable the slave.
  373. //
  374. HWREG(ulBase + I2C_O_SCSR) = 0;
  375. //
  376. // Disable the clock to the slave block.
  377. //
  378. HWREG(ulBase - I2C0_SLAVE_BASE + I2C0_MASTER_BASE + I2C_O_MCR) &=
  379. ~(I2C_MCR_SFE);
  380. }
  381. //*****************************************************************************
  382. //
  383. //! Registers an interrupt handler for the I2C module.
  384. //!
  385. //! \param ulBase is the base address of the I2C Master module.
  386. //! \param pfnHandler is a pointer to the function to be called when the
  387. //! I2C interrupt occurs.
  388. //!
  389. //! This sets the handler to be called when an I2C interrupt occurs. This will
  390. //! enable the global interrupt in the interrupt controller; specific I2C
  391. //! interrupts must be enabled via I2CMasterIntEnable() and
  392. //! I2CSlaveIntEnable(). If necessary, it is the interrupt handler's
  393. //! responsibility to clear the interrupt source via I2CMasterIntClear() and
  394. //! I2CSlaveIntClear().
  395. //!
  396. //! \sa IntRegister() for important information about registering interrupt
  397. //! handlers.
  398. //!
  399. //! \return None.
  400. //
  401. //*****************************************************************************
  402. void
  403. I2CIntRegister(unsigned long ulBase, void (*pfnHandler)(void))
  404. {
  405. unsigned long ulInt;
  406. //
  407. // Check the arguments.
  408. //
  409. ASSERT(I2CMasterBaseValid(ulBase));
  410. //
  411. // Determine the interrupt number based on the I2C port.
  412. //
  413. ulInt = I2CIntNumberGet(ulBase);
  414. //
  415. // Register the interrupt handler, returning an error if an error occurs.
  416. //
  417. IntRegister(ulInt, pfnHandler);
  418. //
  419. // Enable the I2C interrupt.
  420. //
  421. IntEnable(ulInt);
  422. }
  423. //*****************************************************************************
  424. //
  425. //! Unregisters an interrupt handler for the I2C module.
  426. //!
  427. //! \param ulBase is the base address of the I2C Master module.
  428. //!
  429. //! This function will clear the handler to be called when an I2C interrupt
  430. //! occurs. This will also mask off the interrupt in the interrupt controller
  431. //! so that the interrupt handler no longer is called.
  432. //!
  433. //! \sa IntRegister() for important information about registering interrupt
  434. //! handlers.
  435. //!
  436. //! \return None.
  437. //
  438. //*****************************************************************************
  439. void
  440. I2CIntUnregister(unsigned long ulBase)
  441. {
  442. unsigned long ulInt;
  443. //
  444. // Check the arguments.
  445. //
  446. ASSERT(I2CMasterBaseValid(ulBase));
  447. //
  448. // Determine the interrupt number based on the I2C port.
  449. //
  450. ulInt = I2CIntNumberGet(ulBase);
  451. //
  452. // Disable the interrupt.
  453. //
  454. IntDisable(ulInt);
  455. //
  456. // Unregister the interrupt handler.
  457. //
  458. IntUnregister(ulInt);
  459. }
  460. //*****************************************************************************
  461. //
  462. //! Enables the I2C Master interrupt.
  463. //!
  464. //! \param ulBase is the base address of the I2C Master module.
  465. //!
  466. //! Enables the I2C Master interrupt source.
  467. //!
  468. //! \return None.
  469. //
  470. //*****************************************************************************
  471. void
  472. I2CMasterIntEnable(unsigned long ulBase)
  473. {
  474. //
  475. // Check the arguments.
  476. //
  477. ASSERT(I2CMasterBaseValid(ulBase));
  478. //
  479. // Enable the master interrupt.
  480. //
  481. HWREG(ulBase + I2C_O_MIMR) = 1;
  482. }
  483. //*****************************************************************************
  484. //
  485. //! Enables individual I2C Master interrupt sources.
  486. //!
  487. //! \param ulBase is the base address of the I2C Master module.
  488. //! \param ulIntFlags is the bit mask of the interrupt sources to be enabled.
  489. //!
  490. //! Enables the indicated I2C Master interrupt sources. Only the sources that
  491. //! are enabled can be reflected to the processor interrupt; disabled sources
  492. //! have no effect on the processor.
  493. //!
  494. //! The \e ulIntFlags parameter is the logical OR of any of the following:
  495. //!
  496. //! - \b I2C_MASTER_INT_TIMEOUT - Clock Timeout interrupt
  497. //! - \b I2C_MASTER_INT_DATA - Data interrupt
  498. //!
  499. //! \return None.
  500. //
  501. //*****************************************************************************
  502. void
  503. I2CMasterIntEnableEx(unsigned long ulBase, unsigned long ulIntFlags)
  504. {
  505. //
  506. // Check the arguments.
  507. //
  508. ASSERT(I2CMasterBaseValid(ulBase));
  509. //
  510. // Enable the master interrupt.
  511. //
  512. HWREG(ulBase + I2C_O_MIMR) |= ulIntFlags;
  513. }
  514. //*****************************************************************************
  515. //
  516. //! Enables the I2C Slave interrupt.
  517. //!
  518. //! \param ulBase is the base address of the I2C Slave module.
  519. //!
  520. //! Enables the I2C Slave interrupt source.
  521. //!
  522. //! \return None.
  523. //
  524. //*****************************************************************************
  525. void
  526. I2CSlaveIntEnable(unsigned long ulBase)
  527. {
  528. //
  529. // Check the arguments.
  530. //
  531. ASSERT(I2CSlaveBaseValid(ulBase));
  532. //
  533. // Enable the slave interrupt.
  534. //
  535. HWREG(ulBase + I2C_O_SIMR) |= I2C_SLAVE_INT_DATA;
  536. }
  537. //*****************************************************************************
  538. //
  539. //! Enables individual I2C Slave interrupt sources.
  540. //!
  541. //! \param ulBase is the base address of the I2C Slave module.
  542. //! \param ulIntFlags is the bit mask of the interrupt sources to be enabled.
  543. //!
  544. //! Enables the indicated I2C Slave interrupt sources. Only the sources that
  545. //! are enabled can be reflected to the processor interrupt; disabled sources
  546. //! have no effect on the processor.
  547. //!
  548. //! The \e ulIntFlags parameter is the logical OR of any of the following:
  549. //!
  550. //! - \b I2C_SLAVE_INT_STOP - Stop condition detected interrupt
  551. //! - \b I2C_SLAVE_INT_START - Start condition detected interrupt
  552. //! - \b I2C_SLAVE_INT_DATA - Data interrupt
  553. //!
  554. //! \return None.
  555. //
  556. //*****************************************************************************
  557. void
  558. I2CSlaveIntEnableEx(unsigned long ulBase, unsigned long ulIntFlags)
  559. {
  560. //
  561. // Check the arguments.
  562. //
  563. ASSERT(I2CSlaveBaseValid(ulBase));
  564. //
  565. // Enable the slave interrupt.
  566. //
  567. HWREG(ulBase + I2C_O_SIMR) |= ulIntFlags;
  568. }
  569. //*****************************************************************************
  570. //
  571. //! Disables the I2C Master interrupt.
  572. //!
  573. //! \param ulBase is the base address of the I2C Master module.
  574. //!
  575. //! Disables the I2C Master interrupt source.
  576. //!
  577. //! \return None.
  578. //
  579. //*****************************************************************************
  580. void
  581. I2CMasterIntDisable(unsigned long ulBase)
  582. {
  583. //
  584. // Check the arguments.
  585. //
  586. ASSERT(I2CMasterBaseValid(ulBase));
  587. //
  588. // Disable the master interrupt.
  589. //
  590. HWREG(ulBase + I2C_O_MIMR) = 0;
  591. }
  592. //*****************************************************************************
  593. //
  594. //! Disables individual I2C Master interrupt sources.
  595. //!
  596. //! \param ulBase is the base address of the I2C Master module.
  597. //! \param ulIntFlags is the bit mask of the interrupt sources to be disabled.
  598. //!
  599. //! Disables the indicated I2C Master interrupt sources. Only the sources that
  600. //! are enabled can be reflected to the processor interrupt; disabled sources
  601. //! have no effect on the processor.
  602. //!
  603. //! The \e ulIntFlags parameter has the same definition as the \e ulIntFlags
  604. //! parameter to I2CMasterIntEnableEx().
  605. //!
  606. //! \return None.
  607. //
  608. //*****************************************************************************
  609. void
  610. I2CMasterIntDisableEx(unsigned long ulBase, unsigned long ulIntFlags)
  611. {
  612. //
  613. // Check the arguments.
  614. //
  615. ASSERT(I2CMasterBaseValid(ulBase));
  616. //
  617. // Disable the master interrupt.
  618. //
  619. HWREG(ulBase + I2C_O_MIMR) &= ~ulIntFlags;
  620. }
  621. //*****************************************************************************
  622. //
  623. //! Disables the I2C Slave interrupt.
  624. //!
  625. //! \param ulBase is the base address of the I2C Slave module.
  626. //!
  627. //! Disables the I2C Slave interrupt source.
  628. //!
  629. //! \return None.
  630. //
  631. //*****************************************************************************
  632. void
  633. I2CSlaveIntDisable(unsigned long ulBase)
  634. {
  635. //
  636. // Check the arguments.
  637. //
  638. ASSERT(I2CSlaveBaseValid(ulBase));
  639. //
  640. // Disable the slave interrupt.
  641. //
  642. HWREG(ulBase + I2C_O_SIMR) &= ~I2C_SLAVE_INT_DATA;
  643. }
  644. //*****************************************************************************
  645. //
  646. //! Disables individual I2C Slave interrupt sources.
  647. //!
  648. //! \param ulBase is the base address of the I2C Slave module.
  649. //! \param ulIntFlags is the bit mask of the interrupt sources to be disabled.
  650. //!
  651. //! Disables the indicated I2C Slave interrupt sources. Only the sources that
  652. //! are enabled can be reflected to the processor interrupt; disabled sources
  653. //! have no effect on the processor.
  654. //!
  655. //! The \e ulIntFlags parameter has the same definition as the \e ulIntFlags
  656. //! parameter to I2CSlaveIntEnableEx().
  657. //!
  658. //! \return None.
  659. //
  660. //*****************************************************************************
  661. void
  662. I2CSlaveIntDisableEx(unsigned long ulBase, unsigned long ulIntFlags)
  663. {
  664. //
  665. // Check the arguments.
  666. //
  667. ASSERT(I2CSlaveBaseValid(ulBase));
  668. //
  669. // Disable the slave interrupt.
  670. //
  671. HWREG(ulBase + I2C_O_SIMR) &= ~ulIntFlags;
  672. }
  673. //*****************************************************************************
  674. //
  675. //! Gets the current I2C Master interrupt status.
  676. //!
  677. //! \param ulBase is the base address of the I2C Master module.
  678. //! \param bMasked is false if the raw interrupt status is requested and
  679. //! true if the masked interrupt status is requested.
  680. //!
  681. //! This returns the interrupt status for the I2C Master module. Either the
  682. //! raw interrupt status or the status of interrupts that are allowed to
  683. //! reflect to the processor can be returned.
  684. //!
  685. //! \return The current interrupt status, returned as \b true if active
  686. //! or \b false if not active.
  687. //
  688. //*****************************************************************************
  689. tBoolean
  690. I2CMasterIntStatus(unsigned long ulBase, tBoolean bMasked)
  691. {
  692. //
  693. // Check the arguments.
  694. //
  695. ASSERT(I2CMasterBaseValid(ulBase));
  696. //
  697. // Return either the interrupt status or the raw interrupt status as
  698. // requested.
  699. //
  700. if(bMasked)
  701. {
  702. return((HWREG(ulBase + I2C_O_MMIS)) ? true : false);
  703. }
  704. else
  705. {
  706. return((HWREG(ulBase + I2C_O_MRIS)) ? true : false);
  707. }
  708. }
  709. //*****************************************************************************
  710. //
  711. //! Gets the current I2C Master interrupt status.
  712. //!
  713. //! \param ulBase is the base address of the I2C Master module.
  714. //! \param bMasked is false if the raw interrupt status is requested and
  715. //! true if the masked interrupt status is requested.
  716. //!
  717. //! This returns the interrupt status for the I2C Master module. Either the
  718. //! raw interrupt status or the status of interrupts that are allowed to
  719. //! reflect to the processor can be returned.
  720. //!
  721. //! \return Returns the current interrupt status, enumerated as a bit field of
  722. //! values described in I2CMasterIntEnableEx().
  723. //
  724. //*****************************************************************************
  725. unsigned long
  726. I2CMasterIntStatusEx(unsigned long ulBase, tBoolean bMasked)
  727. {
  728. //
  729. // Check the arguments.
  730. //
  731. ASSERT(I2CMasterBaseValid(ulBase));
  732. //
  733. // Return either the interrupt status or the raw interrupt status as
  734. // requested.
  735. //
  736. if(bMasked)
  737. {
  738. return(HWREG(ulBase + I2C_O_MMIS));
  739. }
  740. else
  741. {
  742. return(HWREG(ulBase + I2C_O_MRIS));
  743. }
  744. }
  745. //*****************************************************************************
  746. //
  747. //! Gets the current I2C Slave interrupt status.
  748. //!
  749. //! \param ulBase is the base address of the I2C Slave module.
  750. //! \param bMasked is false if the raw interrupt status is requested and
  751. //! true if the masked interrupt status is requested.
  752. //!
  753. //! This returns the interrupt status for the I2C Slave module. Either the raw
  754. //! interrupt status or the status of interrupts that are allowed to reflect to
  755. //! the processor can be returned.
  756. //!
  757. //! \return The current interrupt status, returned as \b true if active
  758. //! or \b false if not active.
  759. //
  760. //*****************************************************************************
  761. tBoolean
  762. I2CSlaveIntStatus(unsigned long ulBase, tBoolean bMasked)
  763. {
  764. //
  765. // Check the arguments.
  766. //
  767. ASSERT(I2CSlaveBaseValid(ulBase));
  768. //
  769. // Return either the interrupt status or the raw interrupt status as
  770. // requested.
  771. //
  772. if(bMasked)
  773. {
  774. return((HWREG(ulBase + I2C_O_SMIS)) ? true : false);
  775. }
  776. else
  777. {
  778. return((HWREG(ulBase + I2C_O_SRIS)) ? true : false);
  779. }
  780. }
  781. //*****************************************************************************
  782. //
  783. //! Gets the current I2C Slave interrupt status.
  784. //!
  785. //! \param ulBase is the base address of the I2C Slave module.
  786. //! \param bMasked is false if the raw interrupt status is requested and
  787. //! true if the masked interrupt status is requested.
  788. //!
  789. //! This returns the interrupt status for the I2C Slave module. Either the raw
  790. //! interrupt status or the status of interrupts that are allowed to reflect to
  791. //! the processor can be returned.
  792. //!
  793. //! \return Returns the current interrupt status, enumerated as a bit field of
  794. //! values described in I2CSlaveIntEnableEx().
  795. //
  796. //*****************************************************************************
  797. unsigned long
  798. I2CSlaveIntStatusEx(unsigned long ulBase, tBoolean bMasked)
  799. {
  800. unsigned long ulValue;
  801. //
  802. // Check the arguments.
  803. //
  804. ASSERT(I2CSlaveBaseValid(ulBase));
  805. //
  806. // Return either the interrupt status or the raw interrupt status as
  807. // requested.
  808. //
  809. if(bMasked)
  810. {
  811. //
  812. // Workaround for I2C slave masked interrupt status register errata
  813. // (7.1) for Dustdevil Rev A0 devices.
  814. //
  815. if(CLASS_IS_DUSTDEVIL && REVISION_IS_A0)
  816. {
  817. ulValue = HWREG(ulBase + I2C_O_SRIS);
  818. return(ulValue & HWREG(ulBase + I2C_O_SIMR));
  819. }
  820. else
  821. {
  822. return(HWREG(ulBase + I2C_O_SMIS));
  823. }
  824. }
  825. else
  826. {
  827. return(HWREG(ulBase + I2C_O_SRIS));
  828. }
  829. }
  830. //*****************************************************************************
  831. //
  832. //! Clears I2C Master interrupt sources.
  833. //!
  834. //! \param ulBase is the base address of the I2C Master module.
  835. //!
  836. //! The I2C Master interrupt source is cleared, so that it no longer asserts.
  837. //! This must be done in the interrupt handler to keep it from being called
  838. //! again immediately upon exit.
  839. //!
  840. //! \note Because there is a write buffer in the Cortex-M3 processor, it may
  841. //! take several clock cycles before the interrupt source is actually cleared.
  842. //! Therefore, it is recommended that the interrupt source be cleared early in
  843. //! the interrupt handler (as opposed to the very last action) to avoid
  844. //! returning from the interrupt handler before the interrupt source is
  845. //! actually cleared. Failure to do so may result in the interrupt handler
  846. //! being immediately reentered (because the interrupt controller still sees
  847. //! the interrupt source asserted).
  848. //!
  849. //! \return None.
  850. //
  851. //*****************************************************************************
  852. void
  853. I2CMasterIntClear(unsigned long ulBase)
  854. {
  855. //
  856. // Check the arguments.
  857. //
  858. ASSERT(I2CMasterBaseValid(ulBase));
  859. //
  860. // Clear the I2C master interrupt source.
  861. //
  862. HWREG(ulBase + I2C_O_MICR) = I2C_MICR_IC;
  863. //
  864. // Workaround for I2C master interrupt clear errata for rev B Stellaris
  865. // devices. For later devices, this write is ignored and therefore
  866. // harmless (other than the slight performance hit).
  867. //
  868. HWREG(ulBase + I2C_O_MMIS) = I2C_MICR_IC;
  869. }
  870. //*****************************************************************************
  871. //
  872. //! Clears I2C Master interrupt sources.
  873. //!
  874. //! \param ulBase is the base address of the I2C Master module.
  875. //! \param ulIntFlags is a bit mask of the interrupt sources to be cleared.
  876. //!
  877. //! The specified I2C Master interrupt sources are cleared, so that they no
  878. //! longer assert. This must be done in the interrupt handler to keep it from
  879. //! being called again immediately upon exit.
  880. //!
  881. //! The \e ulIntFlags parameter has the same definition as the \e ulIntFlags
  882. //! parameter to I2CMasterIntEnableEx().
  883. //!
  884. //! \note Because there is a write buffer in the Cortex-M3 processor, it may
  885. //! take several clock cycles before the interrupt source is actually cleared.
  886. //! Therefore, it is recommended that the interrupt source be cleared early in
  887. //! the interrupt handler (as opposed to the very last action) to avoid
  888. //! returning from the interrupt handler before the interrupt source is
  889. //! actually cleared. Failure to do so may result in the interrupt handler
  890. //! being immediately reentered (because the interrupt controller still sees
  891. //! the interrupt source asserted).
  892. //!
  893. //! \return None.
  894. //
  895. //*****************************************************************************
  896. void
  897. I2CMasterIntClearEx(unsigned long ulBase, unsigned long ulIntFlags)
  898. {
  899. //
  900. // Check the arguments.
  901. //
  902. ASSERT(I2CMasterBaseValid(ulBase));
  903. //
  904. // Clear the I2C master interrupt source.
  905. //
  906. HWREG(ulBase + I2C_O_MICR) = ulIntFlags;
  907. }
  908. //*****************************************************************************
  909. //
  910. //! Clears I2C Slave interrupt sources.
  911. //!
  912. //! \param ulBase is the base address of the I2C Slave module.
  913. //!
  914. //! The I2C Slave interrupt source is cleared, so that it no longer asserts.
  915. //! This must be done in the interrupt handler to keep it from being called
  916. //! again immediately upon exit.
  917. //!
  918. //! \note Because there is a write buffer in the Cortex-M3 processor, it may
  919. //! take several clock cycles before the interrupt source is actually cleared.
  920. //! Therefore, it is recommended that the interrupt source be cleared early in
  921. //! the interrupt handler (as opposed to the very last action) to avoid
  922. //! returning from the interrupt handler before the interrupt source is
  923. //! actually cleared. Failure to do so may result in the interrupt handler
  924. //! being immediately reentered (because the interrupt controller still sees
  925. //! the interrupt source asserted).
  926. //!
  927. //! \return None.
  928. //
  929. //*****************************************************************************
  930. void
  931. I2CSlaveIntClear(unsigned long ulBase)
  932. {
  933. //
  934. // Check the arguments.
  935. //
  936. ASSERT(I2CSlaveBaseValid(ulBase));
  937. //
  938. // Clear the I2C slave interrupt source.
  939. //
  940. HWREG(ulBase + I2C_O_SICR) = I2C_SICR_DATAIC;
  941. }
  942. //*****************************************************************************
  943. //
  944. //! Clears I2C Slave interrupt sources.
  945. //!
  946. //! \param ulBase is the base address of the I2C Slave module.
  947. //! \param ulIntFlags is a bit mask of the interrupt sources to be cleared.
  948. //!
  949. //! The specified I2C Slave interrupt sources are cleared, so that they no
  950. //! longer assert. This must be done in the interrupt handler to keep it from
  951. //! being called again immediately upon exit.
  952. //!
  953. //! The \e ulIntFlags parameter has the same definition as the \e ulIntFlags
  954. //! parameter to I2CSlaveIntEnableEx().
  955. //!
  956. //! \note Because there is a write buffer in the Cortex-M3 processor, it may
  957. //! take several clock cycles before the interrupt source is actually cleared.
  958. //! Therefore, it is recommended that the interrupt source be cleared early in
  959. //! the interrupt handler (as opposed to the very last action) to avoid
  960. //! returning from the interrupt handler before the interrupt source is
  961. //! actually cleared. Failure to do so may result in the interrupt handler
  962. //! being immediately reentered (because the interrupt controller still sees
  963. //! the interrupt source asserted).
  964. //!
  965. //! \return None.
  966. //
  967. //*****************************************************************************
  968. void
  969. I2CSlaveIntClearEx(unsigned long ulBase, unsigned long ulIntFlags)
  970. {
  971. //
  972. // Check the arguments.
  973. //
  974. ASSERT(I2CSlaveBaseValid(ulBase));
  975. //
  976. // Clear the I2C slave interrupt source.
  977. //
  978. HWREG(ulBase + I2C_O_SICR) = ulIntFlags;
  979. }
  980. //*****************************************************************************
  981. //
  982. //! Sets the address that the I2C Master will place on the bus.
  983. //!
  984. //! \param ulBase is the base address of the I2C Master module.
  985. //! \param ucSlaveAddr 7-bit slave address
  986. //! \param bReceive flag indicating the type of communication with the slave
  987. //!
  988. //! This function will set the address that the I2C Master will place on the
  989. //! bus when initiating a transaction. When the \e bReceive parameter is set
  990. //! to \b true, the address will indicate that the I2C Master is initiating a
  991. //! read from the slave; otherwise the address will indicate that the I2C
  992. //! Master is initiating a write to the slave.
  993. //!
  994. //! \return None.
  995. //
  996. //*****************************************************************************
  997. void
  998. I2CMasterSlaveAddrSet(unsigned long ulBase, unsigned char ucSlaveAddr,
  999. tBoolean bReceive)
  1000. {
  1001. //
  1002. // Check the arguments.
  1003. //
  1004. ASSERT(I2CMasterBaseValid(ulBase));
  1005. ASSERT(!(ucSlaveAddr & 0x80));
  1006. //
  1007. // Set the address of the slave with which the master will communicate.
  1008. //
  1009. HWREG(ulBase + I2C_O_MSA) = (ucSlaveAddr << 1) | bReceive;
  1010. }
  1011. //*****************************************************************************
  1012. //
  1013. //! Reads the state of the SDA and SCL pins.
  1014. //!
  1015. //! \param ulBase is the base address of the I2C Master module.
  1016. //!
  1017. //! This function returns the state of the I2C bus by providing the real time
  1018. //! values of the SDA and SCL pins.
  1019. //!
  1020. //! \return Returns the state of the bus with SDA in bit position 1 and SCL in
  1021. //! bit position 0.
  1022. //
  1023. //*****************************************************************************
  1024. unsigned long
  1025. I2CMasterLineStateGet(unsigned long ulBase)
  1026. {
  1027. //
  1028. // Check the arguments.
  1029. //
  1030. ASSERT(I2CMasterBaseValid(ulBase));
  1031. //
  1032. // Return the line state.
  1033. //
  1034. return(HWREG(ulBase + I2C_O_MBMON));
  1035. }
  1036. //*****************************************************************************
  1037. //
  1038. //! Indicates whether or not the I2C Master is busy.
  1039. //!
  1040. //! \param ulBase is the base address of the I2C Master module.
  1041. //!
  1042. //! This function returns an indication of whether or not the I2C Master is
  1043. //! busy transmitting or receiving data.
  1044. //!
  1045. //! \return Returns \b true if the I2C Master is busy; otherwise, returns
  1046. //! \b false.
  1047. //
  1048. //*****************************************************************************
  1049. tBoolean
  1050. I2CMasterBusy(unsigned long ulBase)
  1051. {
  1052. //
  1053. // Check the arguments.
  1054. //
  1055. ASSERT(I2CMasterBaseValid(ulBase));
  1056. //
  1057. // Return the busy status.
  1058. //
  1059. if(HWREG(ulBase + I2C_O_MCS) & I2C_MCS_BUSY)
  1060. {
  1061. return(true);
  1062. }
  1063. else
  1064. {
  1065. return(false);
  1066. }
  1067. }
  1068. //*****************************************************************************
  1069. //
  1070. //! Indicates whether or not the I2C bus is busy.
  1071. //!
  1072. //! \param ulBase is the base address of the I2C Master module.
  1073. //!
  1074. //! This function returns an indication of whether or not the I2C bus is busy.
  1075. //! This function can be used in a multi-master environment to determine if
  1076. //! another master is currently using the bus.
  1077. //!
  1078. //! \return Returns \b true if the I2C bus is busy; otherwise, returns
  1079. //! \b false.
  1080. //
  1081. //*****************************************************************************
  1082. tBoolean
  1083. I2CMasterBusBusy(unsigned long ulBase)
  1084. {
  1085. //
  1086. // Check the arguments.
  1087. //
  1088. ASSERT(I2CMasterBaseValid(ulBase));
  1089. //
  1090. // Return the bus busy status.
  1091. //
  1092. if(HWREG(ulBase + I2C_O_MCS) & I2C_MCS_BUSBSY)
  1093. {
  1094. return(true);
  1095. }
  1096. else
  1097. {
  1098. return(false);
  1099. }
  1100. }
  1101. //*****************************************************************************
  1102. //
  1103. //! Controls the state of the I2C Master module.
  1104. //!
  1105. //! \param ulBase is the base address of the I2C Master module.
  1106. //! \param ulCmd command to be issued to the I2C Master module
  1107. //!
  1108. //! This function is used to control the state of the Master module send and
  1109. //! receive operations. The \e ucCmd parameter can be one of the following
  1110. //! values:
  1111. //!
  1112. //! - \b I2C_MASTER_CMD_SINGLE_SEND
  1113. //! - \b I2C_MASTER_CMD_SINGLE_RECEIVE
  1114. //! - \b I2C_MASTER_CMD_BURST_SEND_START
  1115. //! - \b I2C_MASTER_CMD_BURST_SEND_CONT
  1116. //! - \b I2C_MASTER_CMD_BURST_SEND_FINISH
  1117. //! - \b I2C_MASTER_CMD_BURST_SEND_ERROR_STOP
  1118. //! - \b I2C_MASTER_CMD_BURST_RECEIVE_START
  1119. //! - \b I2C_MASTER_CMD_BURST_RECEIVE_CONT
  1120. //! - \b I2C_MASTER_CMD_BURST_RECEIVE_FINISH
  1121. //! - \b I2C_MASTER_CMD_BURST_RECEIVE_ERROR_STOP
  1122. //!
  1123. //! \return None.
  1124. //
  1125. //*****************************************************************************
  1126. void
  1127. I2CMasterControl(unsigned long ulBase, unsigned long ulCmd)
  1128. {
  1129. //
  1130. // Check the arguments.
  1131. //
  1132. ASSERT(I2CMasterBaseValid(ulBase));
  1133. ASSERT((ulCmd == I2C_MASTER_CMD_SINGLE_SEND) ||
  1134. (ulCmd == I2C_MASTER_CMD_SINGLE_RECEIVE) ||
  1135. (ulCmd == I2C_MASTER_CMD_BURST_SEND_START) ||
  1136. (ulCmd == I2C_MASTER_CMD_BURST_SEND_CONT) ||
  1137. (ulCmd == I2C_MASTER_CMD_BURST_SEND_FINISH) ||
  1138. (ulCmd == I2C_MASTER_CMD_BURST_SEND_ERROR_STOP) ||
  1139. (ulCmd == I2C_MASTER_CMD_BURST_RECEIVE_START) ||
  1140. (ulCmd == I2C_MASTER_CMD_BURST_RECEIVE_CONT) ||
  1141. (ulCmd == I2C_MASTER_CMD_BURST_RECEIVE_FINISH) ||
  1142. (ulCmd == I2C_MASTER_CMD_BURST_RECEIVE_ERROR_STOP));
  1143. //
  1144. // Send the command.
  1145. //
  1146. HWREG(ulBase + I2C_O_MCS) = ulCmd;
  1147. }
  1148. //*****************************************************************************
  1149. //
  1150. //! Gets the error status of the I2C Master module.
  1151. //!
  1152. //! \param ulBase is the base address of the I2C Master module.
  1153. //!
  1154. //! This function is used to obtain the error status of the Master module send
  1155. //! and receive operations.
  1156. //!
  1157. //! \return Returns the error status, as one of \b I2C_MASTER_ERR_NONE,
  1158. //! \b I2C_MASTER_ERR_ADDR_ACK, \b I2C_MASTER_ERR_DATA_ACK, or
  1159. //! \b I2C_MASTER_ERR_ARB_LOST.
  1160. //
  1161. //*****************************************************************************
  1162. unsigned long
  1163. I2CMasterErr(unsigned long ulBase)
  1164. {
  1165. unsigned long ulErr;
  1166. //
  1167. // Check the arguments.
  1168. //
  1169. ASSERT(I2CMasterBaseValid(ulBase));
  1170. //
  1171. // Get the raw error state
  1172. //
  1173. ulErr = HWREG(ulBase + I2C_O_MCS);
  1174. //
  1175. // If the I2C master is busy, then all the other bit are invalid, and
  1176. // don't have an error to report.
  1177. //
  1178. if(ulErr & I2C_MCS_BUSY)
  1179. {
  1180. return(I2C_MASTER_ERR_NONE);
  1181. }
  1182. //
  1183. // Check for errors.
  1184. //
  1185. if(ulErr & (I2C_MCS_ERROR | I2C_MCS_ARBLST))
  1186. {
  1187. return(ulErr & (I2C_MCS_ARBLST | I2C_MCS_DATACK | I2C_MCS_ADRACK));
  1188. }
  1189. else
  1190. {
  1191. return(I2C_MASTER_ERR_NONE);
  1192. }
  1193. }
  1194. //*****************************************************************************
  1195. //
  1196. //! Transmits a byte from the I2C Master.
  1197. //!
  1198. //! \param ulBase is the base address of the I2C Master module.
  1199. //! \param ucData data to be transmitted from the I2C Master
  1200. //!
  1201. //! This function will place the supplied data into I2C Master Data Register.
  1202. //!
  1203. //! \return None.
  1204. //
  1205. //*****************************************************************************
  1206. void
  1207. I2CMasterDataPut(unsigned long ulBase, unsigned char ucData)
  1208. {
  1209. //
  1210. // Check the arguments.
  1211. //
  1212. ASSERT(I2CMasterBaseValid(ulBase));
  1213. //
  1214. // Write the byte.
  1215. //
  1216. HWREG(ulBase + I2C_O_MDR) = ucData;
  1217. }
  1218. //*****************************************************************************
  1219. //
  1220. //! Receives a byte that has been sent to the I2C Master.
  1221. //!
  1222. //! \param ulBase is the base address of the I2C Master module.
  1223. //!
  1224. //! This function reads a byte of data from the I2C Master Data Register.
  1225. //!
  1226. //! \return Returns the byte received from by the I2C Master, cast as an
  1227. //! unsigned long.
  1228. //
  1229. //*****************************************************************************
  1230. unsigned long
  1231. I2CMasterDataGet(unsigned long ulBase)
  1232. {
  1233. //
  1234. // Check the arguments.
  1235. //
  1236. ASSERT(I2CMasterBaseValid(ulBase));
  1237. //
  1238. // Read a byte.
  1239. //
  1240. return(HWREG(ulBase + I2C_O_MDR));
  1241. }
  1242. //*****************************************************************************
  1243. //
  1244. //! Sets the Master clock timeout value.
  1245. //!
  1246. //! \param ulBase is the base address of the I2C Master module.
  1247. //! \param ulValue is the number of I2C clocks before the timeout is asserted.
  1248. //!
  1249. //! This function enables and configures the clock low timeout feature in the
  1250. //! I2C peripheral. This feature is implemented as a 12-bit counter, with the
  1251. //! upper 8-bits being programmable. For example, to program a timeout of 20ms
  1252. //! with a 100kHz SCL frequency, \e ulValue would be 0x7d.
  1253. //!
  1254. //! \return None.
  1255. //
  1256. //*****************************************************************************
  1257. void
  1258. I2CMasterTimeoutSet(unsigned long ulBase, unsigned long ulValue)
  1259. {
  1260. //
  1261. // Check the arguments.
  1262. //
  1263. ASSERT(I2CMasterBaseValid(ulBase));
  1264. //
  1265. // Write the timeout value.
  1266. //
  1267. HWREG(ulBase + I2C_O_MCLKOCNT) = ulValue;
  1268. }
  1269. //*****************************************************************************
  1270. //
  1271. //! Configures ACK override behavior of the I2C Slave.
  1272. //!
  1273. //! \param ulBase is the base address of the I2C Slave module.
  1274. //! \param bEnable enables or disables ACK override.
  1275. //!
  1276. //! This function enables or disables ACK override, allowing the user
  1277. //! application to drive the value on SDA during the ACK cycle.
  1278. //!
  1279. //! \return None.
  1280. //
  1281. //*****************************************************************************
  1282. void
  1283. I2CSlaveACKOverride(unsigned long ulBase, tBoolean bEnable)
  1284. {
  1285. //
  1286. // Check the arguments.
  1287. //
  1288. ASSERT(I2CSlaveBaseValid(ulBase));
  1289. //
  1290. // Enable or disable based on bEnable.
  1291. //
  1292. if(bEnable)
  1293. {
  1294. HWREG(ulBase + I2C_O_SACKCTL) |= I2C_SACKCTL_ACKOEN;
  1295. }
  1296. else
  1297. {
  1298. HWREG(ulBase + I2C_O_SACKCTL) &= ~I2C_SACKCTL_ACKOEN;
  1299. }
  1300. }
  1301. //*****************************************************************************
  1302. //
  1303. //! Writes the ACK value.
  1304. //!
  1305. //! \param ulBase is the base address of the I2C Slave module.
  1306. //! \param bACK chooses whether to ACK (true) or NACK (false) the transfer.
  1307. //!
  1308. //! This function puts the desired ACK value on SDA during the ACK cycle. The
  1309. //! value written is only valid when ACK override is enabled using
  1310. //! I2CSlaveACKOverride().
  1311. //!
  1312. //! \return None.
  1313. //
  1314. //*****************************************************************************
  1315. void
  1316. I2CSlaveACKValueSet(unsigned long ulBase, tBoolean bACK)
  1317. {
  1318. //
  1319. // Check the arguments.
  1320. //
  1321. ASSERT(I2CSlaveBaseValid(ulBase));
  1322. //
  1323. // ACK or NACK based on the value of bACK.
  1324. //
  1325. if(bACK)
  1326. {
  1327. HWREG(ulBase + I2C_O_SACKCTL) &= ~I2C_SACKCTL_ACKOVAL;
  1328. }
  1329. else
  1330. {
  1331. HWREG(ulBase + I2C_O_SACKCTL) |= I2C_SACKCTL_ACKOVAL;
  1332. }
  1333. }
  1334. //*****************************************************************************
  1335. //
  1336. //! Gets the I2C Slave module status
  1337. //!
  1338. //! \param ulBase is the base address of the I2C Slave module.
  1339. //!
  1340. //! This function will return the action requested from a master, if any.
  1341. //! Possible values are:
  1342. //!
  1343. //! - \b I2C_SLAVE_ACT_NONE
  1344. //! - \b I2C_SLAVE_ACT_RREQ
  1345. //! - \b I2C_SLAVE_ACT_TREQ
  1346. //! - \b I2C_SLAVE_ACT_RREQ_FBR
  1347. //! - \b I2C_SLAVE_ACT_OWN2SEL
  1348. //! - \b I2C_SLAVE_ACT_QCMD
  1349. //! - \b I2C_SLAVE_ACT_QCMD_DATA
  1350. //!
  1351. //! \return Returns \b I2C_SLAVE_ACT_NONE to indicate that no action has been
  1352. //! requested of the I2C Slave module, \b I2C_SLAVE_ACT_RREQ to indicate that
  1353. //! an I2C master has sent data to the I2C Slave module, \b I2C_SLAVE_ACT_TREQ
  1354. //! to indicate that an I2C master has requested that the I2C Slave module send
  1355. //! data, \b I2C_SLAVE_ACT_RREQ_FBR to indicate that an I2C master has sent
  1356. //! data to the I2C slave and the first byte following the slave's own address
  1357. //! has been received, \b I2C_SLAVE_ACT_OWN2SEL to indicate that the second I2C
  1358. //! slave address was matched, \b I2C_SLAVE_ACT_QCMD to indicate that a quick
  1359. //! command was received, and \b I2C_SLAVE_ACT_QCMD_DATA to indicate that the
  1360. //! data bit was set when the quick command was received.
  1361. //
  1362. //*****************************************************************************
  1363. unsigned long
  1364. I2CSlaveStatus(unsigned long ulBase)
  1365. {
  1366. //
  1367. // Check the arguments.
  1368. //
  1369. ASSERT(I2CSlaveBaseValid(ulBase));
  1370. //
  1371. // Return the slave status.
  1372. //
  1373. return(HWREG(ulBase + I2C_O_SCSR));
  1374. }
  1375. //*****************************************************************************
  1376. //
  1377. //! Transmits a byte from the I2C Slave.
  1378. //!
  1379. //! \param ulBase is the base address of the I2C Slave module.
  1380. //! \param ucData data to be transmitted from the I2C Slave
  1381. //!
  1382. //! This function will place the supplied data into I2C Slave Data Register.
  1383. //!
  1384. //! \return None.
  1385. //
  1386. //*****************************************************************************
  1387. void
  1388. I2CSlaveDataPut(unsigned long ulBase, unsigned char ucData)
  1389. {
  1390. //
  1391. // Check the arguments.
  1392. //
  1393. ASSERT(I2CSlaveBaseValid(ulBase));
  1394. //
  1395. // Write the byte.
  1396. //
  1397. HWREG(ulBase + I2C_O_SDR) = ucData;
  1398. }
  1399. //*****************************************************************************
  1400. //
  1401. //! Receives a byte that has been sent to the I2C Slave.
  1402. //!
  1403. //! \param ulBase is the base address of the I2C Slave module.
  1404. //!
  1405. //! This function reads a byte of data from the I2C Slave Data Register.
  1406. //!
  1407. //! \return Returns the byte received from by the I2C Slave, cast as an
  1408. //! unsigned long.
  1409. //
  1410. //*****************************************************************************
  1411. unsigned long
  1412. I2CSlaveDataGet(unsigned long ulBase)
  1413. {
  1414. //
  1415. // Check the arguments.
  1416. //
  1417. ASSERT(I2CSlaveBaseValid(ulBase));
  1418. //
  1419. // Read a byte.
  1420. //
  1421. return(HWREG(ulBase + I2C_O_SDR));
  1422. }
  1423. //*****************************************************************************
  1424. //
  1425. // Close the Doxygen group.
  1426. //! @}
  1427. //
  1428. //*****************************************************************************