i2c.c 34 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106
  1. //*****************************************************************************
  2. //
  3. // i2c.c - Driver for Inter-IC (I2C) bus block.
  4. //
  5. // Copyright (c) 2005-2010 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 6459 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. //! Initializes the I2C Master block.
  41. //!
  42. //! \param ulBase is the base address of the I2C Master module.
  43. //! \param ulI2CClk is the rate of the clock supplied to the I2C module.
  44. //! \param bFast set up for fast data transfers
  45. //!
  46. //! This function initializes operation of the I2C Master block. Upon
  47. //! successful initialization of the I2C block, this function will have set the
  48. //! bus speed for the master, and will have enabled the I2C Master block.
  49. //!
  50. //! If the parameter \e bFast is \b true, then the master block will be set up
  51. //! to transfer data at 400 kbps; otherwise, it will be set up to transfer data
  52. //! at 100 kbps.
  53. //!
  54. //! The peripheral clock will be the same as the processor clock. This will be
  55. //! the value returned by SysCtlClockGet(), or it can be explicitly hard coded
  56. //! if it is constant and known (to save the code/execution overhead of a call
  57. //! to SysCtlClockGet()).
  58. //!
  59. //! This function replaces the original I2CMasterInit() API and performs the
  60. //! same actions. A macro is provided in <tt>i2c.h</tt> to map the original
  61. //! API to this API.
  62. //!
  63. //! \return None.
  64. //
  65. //*****************************************************************************
  66. void
  67. I2CMasterInitExpClk(unsigned long ulBase, unsigned long ulI2CClk,
  68. tBoolean bFast)
  69. {
  70. unsigned long ulSCLFreq;
  71. unsigned long ulTPR;
  72. //
  73. // Check the arguments.
  74. //
  75. ASSERT((ulBase == I2C0_MASTER_BASE) || (ulBase == I2C1_MASTER_BASE));
  76. //
  77. // Must enable the device before doing anything else.
  78. //
  79. I2CMasterEnable(ulBase);
  80. //
  81. // Get the desired SCL speed.
  82. //
  83. if(bFast == true)
  84. {
  85. ulSCLFreq = 400000;
  86. }
  87. else
  88. {
  89. ulSCLFreq = 100000;
  90. }
  91. //
  92. // Compute the clock divider that achieves the fastest speed less than or
  93. // equal to the desired speed. The numerator is biased to favor a larger
  94. // clock divider so that the resulting clock is always less than or equal
  95. // to the desired clock, never greater.
  96. //
  97. ulTPR = ((ulI2CClk + (2 * 10 * ulSCLFreq) - 1) / (2 * 10 * ulSCLFreq)) - 1;
  98. HWREG(ulBase + I2C_O_MTPR) = ulTPR;
  99. }
  100. //*****************************************************************************
  101. //
  102. //! Initializes the I2C Slave block.
  103. //!
  104. //! \param ulBase is the base address of the I2C Slave module.
  105. //! \param ucSlaveAddr 7-bit slave address
  106. //!
  107. //! This function initializes operation of the I2C Slave block. Upon
  108. //! successful initialization of the I2C blocks, this function will have set
  109. //! the slave address and have enabled the I2C Slave block.
  110. //!
  111. //! The parameter \e ucSlaveAddr is the value that will be compared against the
  112. //! slave address sent by an I2C master.
  113. //!
  114. //! \return None.
  115. //
  116. //*****************************************************************************
  117. void
  118. I2CSlaveInit(unsigned long ulBase, unsigned char ucSlaveAddr)
  119. {
  120. //
  121. // Check the arguments.
  122. //
  123. ASSERT((ulBase == I2C0_SLAVE_BASE) || (ulBase == I2C1_SLAVE_BASE));
  124. ASSERT(!(ucSlaveAddr & 0x80));
  125. //
  126. // Must enable the device before doing anything else.
  127. //
  128. I2CSlaveEnable(ulBase);
  129. //
  130. // Set up the slave address.
  131. //
  132. HWREG(ulBase + I2C_O_SOAR) = ucSlaveAddr;
  133. }
  134. //*****************************************************************************
  135. //
  136. //! Enables the I2C Master block.
  137. //!
  138. //! \param ulBase is the base address of the I2C Master module.
  139. //!
  140. //! This will enable operation of the I2C Master block.
  141. //!
  142. //! \return None.
  143. //
  144. //*****************************************************************************
  145. void
  146. I2CMasterEnable(unsigned long ulBase)
  147. {
  148. //
  149. // Check the arguments.
  150. //
  151. ASSERT((ulBase == I2C0_MASTER_BASE) || (ulBase == I2C1_MASTER_BASE));
  152. //
  153. // Enable the master block.
  154. //
  155. HWREG(ulBase + I2C_O_MCR) |= I2C_MCR_MFE;
  156. }
  157. //*****************************************************************************
  158. //
  159. //! Enables the I2C Slave block.
  160. //!
  161. //! \param ulBase is the base address of the I2C Slave module.
  162. //!
  163. //! This will enable operation of the I2C Slave block.
  164. //!
  165. //! \return None.
  166. //
  167. //*****************************************************************************
  168. void
  169. I2CSlaveEnable(unsigned long ulBase)
  170. {
  171. //
  172. // Check the arguments.
  173. //
  174. ASSERT((ulBase == I2C0_SLAVE_BASE) || (ulBase == I2C1_SLAVE_BASE));
  175. //
  176. // Enable the clock to the slave block.
  177. //
  178. HWREG(ulBase - I2C0_SLAVE_BASE + I2C0_MASTER_BASE + I2C_O_MCR) |=
  179. I2C_MCR_SFE;
  180. //
  181. // Enable the slave.
  182. //
  183. HWREG(ulBase + I2C_O_SCSR) = I2C_SCSR_DA;
  184. }
  185. //*****************************************************************************
  186. //
  187. //! Disables the I2C master block.
  188. //!
  189. //! \param ulBase is the base address of the I2C Master module.
  190. //!
  191. //! This will disable operation of the I2C master block.
  192. //!
  193. //! \return None.
  194. //
  195. //*****************************************************************************
  196. void
  197. I2CMasterDisable(unsigned long ulBase)
  198. {
  199. //
  200. // Check the arguments.
  201. //
  202. ASSERT((ulBase == I2C0_MASTER_BASE) || (ulBase == I2C1_MASTER_BASE));
  203. //
  204. // Disable the master block.
  205. //
  206. HWREG(ulBase + I2C_O_MCR) &= ~(I2C_MCR_MFE);
  207. }
  208. //*****************************************************************************
  209. //
  210. //! Disables the I2C slave block.
  211. //!
  212. //! \param ulBase is the base address of the I2C Slave module.
  213. //!
  214. //! This will disable operation of the I2C slave block.
  215. //!
  216. //! \return None.
  217. //
  218. //*****************************************************************************
  219. void
  220. I2CSlaveDisable(unsigned long ulBase)
  221. {
  222. //
  223. // Check the arguments.
  224. //
  225. ASSERT((ulBase == I2C0_SLAVE_BASE) || (ulBase == I2C1_SLAVE_BASE));
  226. //
  227. // Disable the slave.
  228. //
  229. HWREG(ulBase + I2C_O_SCSR) = 0;
  230. //
  231. // Disable the clock to the slave block.
  232. //
  233. HWREG(ulBase - I2C0_SLAVE_BASE + I2C0_MASTER_BASE + I2C_O_MCR) &=
  234. ~(I2C_MCR_SFE);
  235. }
  236. //*****************************************************************************
  237. //
  238. //! Registers an interrupt handler for the I2C module.
  239. //!
  240. //! \param ulBase is the base address of the I2C Master module.
  241. //! \param pfnHandler is a pointer to the function to be called when the
  242. //! I2C interrupt occurs.
  243. //!
  244. //! This sets the handler to be called when an I2C interrupt occurs. This will
  245. //! enable the global interrupt in the interrupt controller; specific I2C
  246. //! interrupts must be enabled via I2CMasterIntEnable() and
  247. //! I2CSlaveIntEnable(). If necessary, it is the interrupt handler's
  248. //! responsibility to clear the interrupt source via I2CMasterIntClear() and
  249. //! I2CSlaveIntClear().
  250. //!
  251. //! \sa IntRegister() for important information about registering interrupt
  252. //! handlers.
  253. //!
  254. //! \return None.
  255. //
  256. //*****************************************************************************
  257. void
  258. I2CIntRegister(unsigned long ulBase, void (*pfnHandler)(void))
  259. {
  260. unsigned long ulInt;
  261. //
  262. // Check the arguments.
  263. //
  264. ASSERT((ulBase == I2C0_MASTER_BASE) || (ulBase == I2C1_MASTER_BASE));
  265. //
  266. // Determine the interrupt number based on the I2C port.
  267. //
  268. ulInt = (ulBase == I2C0_MASTER_BASE) ? INT_I2C0 : INT_I2C1;
  269. //
  270. // Register the interrupt handler, returning an error if an error occurs.
  271. //
  272. IntRegister(ulInt, pfnHandler);
  273. //
  274. // Enable the I2C interrupt.
  275. //
  276. IntEnable(ulInt);
  277. }
  278. //*****************************************************************************
  279. //
  280. //! Unregisters an interrupt handler for the I2C module.
  281. //!
  282. //! \param ulBase is the base address of the I2C Master module.
  283. //!
  284. //! This function will clear the handler to be called when an I2C interrupt
  285. //! occurs. This will also mask off the interrupt in the interrupt controller
  286. //! so that the interrupt handler no longer is called.
  287. //!
  288. //! \sa IntRegister() for important information about registering interrupt
  289. //! handlers.
  290. //!
  291. //! \return None.
  292. //
  293. //*****************************************************************************
  294. void
  295. I2CIntUnregister(unsigned long ulBase)
  296. {
  297. unsigned long ulInt;
  298. //
  299. // Check the arguments.
  300. //
  301. ASSERT((ulBase == I2C0_MASTER_BASE) || (ulBase == I2C1_MASTER_BASE));
  302. //
  303. // Determine the interrupt number based on the I2C port.
  304. //
  305. ulInt = (ulBase == I2C0_MASTER_BASE) ? INT_I2C0 : INT_I2C1;
  306. //
  307. // Disable the interrupt.
  308. //
  309. IntDisable(ulInt);
  310. //
  311. // Unregister the interrupt handler.
  312. //
  313. IntUnregister(ulInt);
  314. }
  315. //*****************************************************************************
  316. //
  317. //! Enables the I2C Master interrupt.
  318. //!
  319. //! \param ulBase is the base address of the I2C Master module.
  320. //!
  321. //! Enables the I2C Master interrupt source.
  322. //!
  323. //! \return None.
  324. //
  325. //*****************************************************************************
  326. void
  327. I2CMasterIntEnable(unsigned long ulBase)
  328. {
  329. //
  330. // Check the arguments.
  331. //
  332. ASSERT((ulBase == I2C0_MASTER_BASE) || (ulBase == I2C1_MASTER_BASE));
  333. //
  334. // Enable the master interrupt.
  335. //
  336. HWREG(ulBase + I2C_O_MIMR) = 1;
  337. }
  338. //*****************************************************************************
  339. //
  340. //! Enables the I2C Slave interrupt.
  341. //!
  342. //! \param ulBase is the base address of the I2C Slave module.
  343. //!
  344. //! Enables the I2C Slave interrupt source.
  345. //!
  346. //! \return None.
  347. //
  348. //*****************************************************************************
  349. void
  350. I2CSlaveIntEnable(unsigned long ulBase)
  351. {
  352. //
  353. // Check the arguments.
  354. //
  355. ASSERT((ulBase == I2C0_SLAVE_BASE) || (ulBase == I2C1_SLAVE_BASE));
  356. //
  357. // Enable the slave interrupt.
  358. //
  359. HWREG(ulBase + I2C_O_SIMR) |= I2C_SLAVE_INT_DATA;
  360. }
  361. //*****************************************************************************
  362. //
  363. //! Enables individual I2C Slave interrupt sources.
  364. //!
  365. //! \param ulBase is the base address of the I2C Slave module.
  366. //! \param ulIntFlags is the bit mask of the interrupt sources to be enabled.
  367. //!
  368. //! Enables the indicated I2C Slave interrupt sources. Only the sources that
  369. //! are enabled can be reflected to the processor interrupt; disabled sources
  370. //! have no effect on the processor.
  371. //!
  372. //! The \e ulIntFlags parameter is the logical OR of any of the following:
  373. //!
  374. //! - \b I2C_SLAVE_INT_STOP - Stop condition detected interrupt
  375. //! - \b I2C_SLAVE_INT_START - Start condition detected interrupt
  376. //! - \b I2C_SLAVE_INT_DATA - Data interrupt
  377. //!
  378. //! \return None.
  379. //
  380. //*****************************************************************************
  381. void
  382. I2CSlaveIntEnableEx(unsigned long ulBase, unsigned long ulIntFlags)
  383. {
  384. //
  385. // Check the arguments.
  386. //
  387. ASSERT((ulBase == I2C0_SLAVE_BASE) || (ulBase == I2C1_SLAVE_BASE));
  388. //
  389. // Enable the slave interrupt.
  390. //
  391. HWREG(ulBase + I2C_O_SIMR) |= ulIntFlags;
  392. }
  393. //*****************************************************************************
  394. //
  395. //! Disables the I2C Master interrupt.
  396. //!
  397. //! \param ulBase is the base address of the I2C Master module.
  398. //!
  399. //! Disables the I2C Master interrupt source.
  400. //!
  401. //! \return None.
  402. //
  403. //*****************************************************************************
  404. void
  405. I2CMasterIntDisable(unsigned long ulBase)
  406. {
  407. //
  408. // Check the arguments.
  409. //
  410. ASSERT((ulBase == I2C0_MASTER_BASE) || (ulBase == I2C1_MASTER_BASE));
  411. //
  412. // Disable the master interrupt.
  413. //
  414. HWREG(ulBase + I2C_O_MIMR) = 0;
  415. }
  416. //*****************************************************************************
  417. //
  418. //! Disables the I2C Slave interrupt.
  419. //!
  420. //! \param ulBase is the base address of the I2C Slave module.
  421. //!
  422. //! Disables the I2C Slave interrupt source.
  423. //!
  424. //! \return None.
  425. //
  426. //*****************************************************************************
  427. void
  428. I2CSlaveIntDisable(unsigned long ulBase)
  429. {
  430. //
  431. // Check the arguments.
  432. //
  433. ASSERT((ulBase == I2C0_SLAVE_BASE) || (ulBase == I2C1_SLAVE_BASE));
  434. //
  435. // Disable the slave interrupt.
  436. //
  437. HWREG(ulBase + I2C_O_SIMR) &= ~I2C_SLAVE_INT_DATA;
  438. }
  439. //*****************************************************************************
  440. //
  441. //! Disables individual I2C Slave interrupt sources.
  442. //!
  443. //! \param ulBase is the base address of the I2C Slave module.
  444. //! \param ulIntFlags is the bit mask of the interrupt sources to be disabled.
  445. //!
  446. //! Disables the indicated I2C Slave interrupt sources. Only the sources that
  447. //! are enabled can be reflected to the processor interrupt; disabled sources
  448. //! have no effect on the processor.
  449. //!
  450. //! The \e ulIntFlags parameter has the same definition as the \e ulIntFlags
  451. //! parameter to I2CSlaveIntEnableEx().
  452. //!
  453. //! \return None.
  454. //
  455. //*****************************************************************************
  456. void
  457. I2CSlaveIntDisableEx(unsigned long ulBase, unsigned long ulIntFlags)
  458. {
  459. //
  460. // Check the arguments.
  461. //
  462. ASSERT((ulBase == I2C0_SLAVE_BASE) || (ulBase == I2C1_SLAVE_BASE));
  463. //
  464. // Disable the slave interrupt.
  465. //
  466. HWREG(ulBase + I2C_O_SIMR) &= ~ulIntFlags;
  467. }
  468. //*****************************************************************************
  469. //
  470. //! Gets the current I2C Master interrupt status.
  471. //!
  472. //! \param ulBase is the base address of the I2C Master module.
  473. //! \param bMasked is false if the raw interrupt status is requested and
  474. //! true if the masked interrupt status is requested.
  475. //!
  476. //! This returns the interrupt status for the I2C Master module. Either the
  477. //! raw interrupt status or the status of interrupts that are allowed to
  478. //! reflect to the processor can be returned.
  479. //!
  480. //! \return The current interrupt status, returned as \b true if active
  481. //! or \b false if not active.
  482. //
  483. //*****************************************************************************
  484. tBoolean
  485. I2CMasterIntStatus(unsigned long ulBase, tBoolean bMasked)
  486. {
  487. //
  488. // Check the arguments.
  489. //
  490. ASSERT((ulBase == I2C0_MASTER_BASE) || (ulBase == I2C1_MASTER_BASE));
  491. //
  492. // Return either the interrupt status or the raw interrupt status as
  493. // requested.
  494. //
  495. if(bMasked)
  496. {
  497. return((HWREG(ulBase + I2C_O_MMIS)) ? true : false);
  498. }
  499. else
  500. {
  501. return((HWREG(ulBase + I2C_O_MRIS)) ? true : false);
  502. }
  503. }
  504. //*****************************************************************************
  505. //
  506. //! Gets the current I2C Slave interrupt status.
  507. //!
  508. //! \param ulBase is the base address of the I2C Slave module.
  509. //! \param bMasked is false if the raw interrupt status is requested and
  510. //! true if the masked interrupt status is requested.
  511. //!
  512. //! This returns the interrupt status for the I2C Slave module. Either the raw
  513. //! interrupt status or the status of interrupts that are allowed to reflect to
  514. //! the processor can be returned.
  515. //!
  516. //! \return The current interrupt status, returned as \b true if active
  517. //! or \b false if not active.
  518. //
  519. //*****************************************************************************
  520. tBoolean
  521. I2CSlaveIntStatus(unsigned long ulBase, tBoolean bMasked)
  522. {
  523. //
  524. // Check the arguments.
  525. //
  526. ASSERT((ulBase == I2C0_SLAVE_BASE) || (ulBase == I2C1_SLAVE_BASE));
  527. //
  528. // Return either the interrupt status or the raw interrupt status as
  529. // requested.
  530. //
  531. if(bMasked)
  532. {
  533. return((HWREG(ulBase + I2C_O_SMIS)) ? true : false);
  534. }
  535. else
  536. {
  537. return((HWREG(ulBase + I2C_O_SRIS)) ? true : false);
  538. }
  539. }
  540. //*****************************************************************************
  541. //
  542. //! Gets the current I2C Slave interrupt status.
  543. //!
  544. //! \param ulBase is the base address of the I2C Slave module.
  545. //! \param bMasked is false if the raw interrupt status is requested and
  546. //! true if the masked interrupt status is requested.
  547. //!
  548. //! This returns the interrupt status for the I2C Slave module. Either the raw
  549. //! interrupt status or the status of interrupts that are allowed to reflect to
  550. //! the processor can be returned.
  551. //!
  552. //! \return Returns the current interrupt status, enumerated as a bit field of
  553. //! values described in I2CSlaveIntEnableEx().
  554. //
  555. //*****************************************************************************
  556. unsigned long
  557. I2CSlaveIntStatusEx(unsigned long ulBase, tBoolean bMasked)
  558. {
  559. unsigned long ulValue;
  560. //
  561. // Check the arguments.
  562. //
  563. ASSERT((ulBase == I2C0_SLAVE_BASE) || (ulBase == I2C1_SLAVE_BASE));
  564. //
  565. // Return either the interrupt status or the raw interrupt status as
  566. // requested.
  567. //
  568. if(bMasked)
  569. {
  570. //
  571. // Workaround for I2C slave masked interrupt status register errata
  572. // (7.1) for Dustdevil Rev A0 devices.
  573. //
  574. if(CLASS_IS_DUSTDEVIL && REVISION_IS_A0)
  575. {
  576. ulValue = HWREG(ulBase + I2C_O_SRIS);
  577. return(ulValue & HWREG(ulBase + I2C_O_SIMR));
  578. }
  579. else
  580. {
  581. return(HWREG(ulBase + I2C_O_SMIS));
  582. }
  583. }
  584. else
  585. {
  586. return(HWREG(ulBase + I2C_O_SRIS));
  587. }
  588. }
  589. //*****************************************************************************
  590. //
  591. //! Clears I2C Master interrupt sources.
  592. //!
  593. //! \param ulBase is the base address of the I2C Master module.
  594. //!
  595. //! The I2C Master interrupt source is cleared, so that it no longer asserts.
  596. //! This must be done in the interrupt handler to keep it from being called
  597. //! again immediately upon exit.
  598. //!
  599. //! \note Since there is a write buffer in the Cortex-M3 processor, it may take
  600. //! several clock cycles before the interrupt source is actually cleared.
  601. //! Therefore, it is recommended that the interrupt source be cleared early in
  602. //! the interrupt handler (as opposed to the very last action) to avoid
  603. //! returning from the interrupt handler before the interrupt source is
  604. //! actually cleared. Failure to do so may result in the interrupt handler
  605. //! being immediately reentered (since NVIC still sees the interrupt source
  606. //! asserted).
  607. //!
  608. //! \return None.
  609. //
  610. //*****************************************************************************
  611. void
  612. I2CMasterIntClear(unsigned long ulBase)
  613. {
  614. //
  615. // Check the arguments.
  616. //
  617. ASSERT((ulBase == I2C0_MASTER_BASE) || (ulBase == I2C1_MASTER_BASE));
  618. //
  619. // Clear the I2C master interrupt source.
  620. //
  621. HWREG(ulBase + I2C_O_MICR) = I2C_MICR_IC;
  622. //
  623. // Workaround for I2C master interrupt clear errata for rev B Stellaris
  624. // devices. For later devices, this write is ignored and therefore
  625. // harmless (other than the slight performance hit).
  626. //
  627. HWREG(ulBase + I2C_O_MMIS) = I2C_MICR_IC;
  628. }
  629. //*****************************************************************************
  630. //
  631. //! Clears I2C Slave interrupt sources.
  632. //!
  633. //! \param ulBase is the base address of the I2C Slave module.
  634. //!
  635. //! The I2C Slave interrupt source is cleared, so that it no longer asserts.
  636. //! This must be done in the interrupt handler to keep it from being called
  637. //! again immediately upon exit.
  638. //!
  639. //! \note Since there is a write buffer in the Cortex-M3 processor, it may take
  640. //! several clock cycles before the interrupt source is actually cleared.
  641. //! Therefore, it is recommended that the interrupt source be cleared early in
  642. //! the interrupt handler (as opposed to the very last action) to avoid
  643. //! returning from the interrupt handler before the interrupt source is
  644. //! actually cleared. Failure to do so may result in the interrupt handler
  645. //! being immediately reentered (since NVIC still sees the interrupt source
  646. //! asserted).
  647. //!
  648. //! \return None.
  649. //
  650. //*****************************************************************************
  651. void
  652. I2CSlaveIntClear(unsigned long ulBase)
  653. {
  654. //
  655. // Check the arguments.
  656. //
  657. ASSERT((ulBase == I2C0_SLAVE_BASE) || (ulBase == I2C1_SLAVE_BASE));
  658. //
  659. // Clear the I2C slave interrupt source.
  660. //
  661. HWREG(ulBase + I2C_O_SICR) = I2C_SICR_DATAIC;
  662. }
  663. //*****************************************************************************
  664. //
  665. //! Clears I2C Slave interrupt sources.
  666. //!
  667. //! \param ulBase is the base address of the I2C Slave module.
  668. //! \param ulIntFlags is a bit mask of the interrupt sources to be cleared.
  669. //!
  670. //! The specified I2C Slave interrupt sources are cleared, so that they no
  671. //! longer assert. This must be done in the interrupt handler to keep it from
  672. //! being called again immediately upon exit.
  673. //!
  674. //! The \e ulIntFlags parameter has the same definition as the \e ulIntFlags
  675. //! parameter to I2CSlaveIntEnableEx().
  676. //!
  677. //! \note Since there is a write buffer in the Cortex-M3 processor, it may take
  678. //! several clock cycles before the interrupt source is actually cleared.
  679. //! Therefore, it is recommended that the interrupt source be cleared early in
  680. //! the interrupt handler (as opposed to the very last action) to avoid
  681. //! returning from the interrupt handler before the interrupt source is
  682. //! actually cleared. Failure to do so may result in the interrupt handler
  683. //! being immediately reentered (since NVIC still sees the interrupt source
  684. //! asserted).
  685. //!
  686. //! \return None.
  687. //
  688. //*****************************************************************************
  689. void
  690. I2CSlaveIntClearEx(unsigned long ulBase, unsigned long ulIntFlags)
  691. {
  692. //
  693. // Check the arguments.
  694. //
  695. ASSERT((ulBase == I2C0_SLAVE_BASE) || (ulBase == I2C1_SLAVE_BASE));
  696. //
  697. // Clear the I2C slave interrupt source.
  698. //
  699. HWREG(ulBase + I2C_O_SICR) = ulIntFlags;
  700. }
  701. //*****************************************************************************
  702. //
  703. //! Sets the address that the I2C Master will place on the bus.
  704. //!
  705. //! \param ulBase is the base address of the I2C Master module.
  706. //! \param ucSlaveAddr 7-bit slave address
  707. //! \param bReceive flag indicating the type of communication with the slave
  708. //!
  709. //! This function will set the address that the I2C Master will place on the
  710. //! bus when initiating a transaction. When the \e bReceive parameter is set
  711. //! to \b true, the address will indicate that the I2C Master is initiating a
  712. //! read from the slave; otherwise the address will indicate that the I2C
  713. //! Master is initiating a write to the slave.
  714. //!
  715. //! \return None.
  716. //
  717. //*****************************************************************************
  718. void
  719. I2CMasterSlaveAddrSet(unsigned long ulBase, unsigned char ucSlaveAddr,
  720. tBoolean bReceive)
  721. {
  722. //
  723. // Check the arguments.
  724. //
  725. ASSERT((ulBase == I2C0_MASTER_BASE) || (ulBase == I2C1_MASTER_BASE));
  726. ASSERT(!(ucSlaveAddr & 0x80));
  727. //
  728. // Set the address of the slave with which the master will communicate.
  729. //
  730. HWREG(ulBase + I2C_O_MSA) = (ucSlaveAddr << 1) | bReceive;
  731. }
  732. //*****************************************************************************
  733. //
  734. //! Indicates whether or not the I2C Master is busy.
  735. //!
  736. //! \param ulBase is the base address of the I2C Master module.
  737. //!
  738. //! This function returns an indication of whether or not the I2C Master is
  739. //! busy transmitting or receiving data.
  740. //!
  741. //! \return Returns \b true if the I2C Master is busy; otherwise, returns
  742. //! \b false.
  743. //
  744. //*****************************************************************************
  745. tBoolean
  746. I2CMasterBusy(unsigned long ulBase)
  747. {
  748. //
  749. // Check the arguments.
  750. //
  751. ASSERT((ulBase == I2C0_MASTER_BASE) || (ulBase == I2C1_MASTER_BASE));
  752. //
  753. // Return the busy status.
  754. //
  755. if(HWREG(ulBase + I2C_O_MCS) & I2C_MCS_BUSY)
  756. {
  757. return(true);
  758. }
  759. else
  760. {
  761. return(false);
  762. }
  763. }
  764. //*****************************************************************************
  765. //
  766. //! Indicates whether or not the I2C bus is busy.
  767. //!
  768. //! \param ulBase is the base address of the I2C Master module.
  769. //!
  770. //! This function returns an indication of whether or not the I2C bus is busy.
  771. //! This function can be used in a multi-master environment to determine if
  772. //! another master is currently using the bus.
  773. //!
  774. //! \return Returns \b true if the I2C bus is busy; otherwise, returns
  775. //! \b false.
  776. //
  777. //*****************************************************************************
  778. tBoolean
  779. I2CMasterBusBusy(unsigned long ulBase)
  780. {
  781. //
  782. // Check the arguments.
  783. //
  784. ASSERT((ulBase == I2C0_MASTER_BASE) || (ulBase == I2C1_MASTER_BASE));
  785. //
  786. // Return the bus busy status.
  787. //
  788. if(HWREG(ulBase + I2C_O_MCS) & I2C_MCS_BUSBSY)
  789. {
  790. return(true);
  791. }
  792. else
  793. {
  794. return(false);
  795. }
  796. }
  797. //*****************************************************************************
  798. //
  799. //! Controls the state of the I2C Master module.
  800. //!
  801. //! \param ulBase is the base address of the I2C Master module.
  802. //! \param ulCmd command to be issued to the I2C Master module
  803. //!
  804. //! This function is used to control the state of the Master module send and
  805. //! receive operations. The \e ucCmd parameter can be one of the following
  806. //! values:
  807. //!
  808. //! - \b I2C_MASTER_CMD_SINGLE_SEND
  809. //! - \b I2C_MASTER_CMD_SINGLE_RECEIVE
  810. //! - \b I2C_MASTER_CMD_BURST_SEND_START
  811. //! - \b I2C_MASTER_CMD_BURST_SEND_CONT
  812. //! - \b I2C_MASTER_CMD_BURST_SEND_FINISH
  813. //! - \b I2C_MASTER_CMD_BURST_SEND_ERROR_STOP
  814. //! - \b I2C_MASTER_CMD_BURST_RECEIVE_START
  815. //! - \b I2C_MASTER_CMD_BURST_RECEIVE_CONT
  816. //! - \b I2C_MASTER_CMD_BURST_RECEIVE_FINISH
  817. //! - \b I2C_MASTER_CMD_BURST_RECEIVE_ERROR_STOP
  818. //!
  819. //! \return None.
  820. //
  821. //*****************************************************************************
  822. void
  823. I2CMasterControl(unsigned long ulBase, unsigned long ulCmd)
  824. {
  825. //
  826. // Check the arguments.
  827. //
  828. ASSERT((ulBase == I2C0_MASTER_BASE) || (ulBase == I2C1_MASTER_BASE));
  829. ASSERT((ulCmd == I2C_MASTER_CMD_SINGLE_SEND) ||
  830. (ulCmd == I2C_MASTER_CMD_SINGLE_RECEIVE) ||
  831. (ulCmd == I2C_MASTER_CMD_BURST_SEND_START) ||
  832. (ulCmd == I2C_MASTER_CMD_BURST_SEND_CONT) ||
  833. (ulCmd == I2C_MASTER_CMD_BURST_SEND_FINISH) ||
  834. (ulCmd == I2C_MASTER_CMD_BURST_SEND_ERROR_STOP) ||
  835. (ulCmd == I2C_MASTER_CMD_BURST_RECEIVE_START) ||
  836. (ulCmd == I2C_MASTER_CMD_BURST_RECEIVE_CONT) ||
  837. (ulCmd == I2C_MASTER_CMD_BURST_RECEIVE_FINISH) ||
  838. (ulCmd == I2C_MASTER_CMD_BURST_RECEIVE_ERROR_STOP));
  839. //
  840. // Send the command.
  841. //
  842. HWREG(ulBase + I2C_O_MCS) = ulCmd;
  843. }
  844. //*****************************************************************************
  845. //
  846. //! Gets the error status of the I2C Master module.
  847. //!
  848. //! \param ulBase is the base address of the I2C Master module.
  849. //!
  850. //! This function is used to obtain the error status of the Master module send
  851. //! and receive operations.
  852. //!
  853. //! \return Returns the error status, as one of \b I2C_MASTER_ERR_NONE,
  854. //! \b I2C_MASTER_ERR_ADDR_ACK, \b I2C_MASTER_ERR_DATA_ACK, or
  855. //! \b I2C_MASTER_ERR_ARB_LOST.
  856. //
  857. //*****************************************************************************
  858. unsigned long
  859. I2CMasterErr(unsigned long ulBase)
  860. {
  861. unsigned long ulErr;
  862. //
  863. // Check the arguments.
  864. //
  865. ASSERT((ulBase == I2C0_MASTER_BASE) || (ulBase == I2C1_MASTER_BASE));
  866. //
  867. // Get the raw error state
  868. //
  869. ulErr = HWREG(ulBase + I2C_O_MCS);
  870. //
  871. // If the I2C master is busy, then all the other bit are invalid, and
  872. // don't have an error to report.
  873. //
  874. if(ulErr & I2C_MCS_BUSY)
  875. {
  876. return(I2C_MASTER_ERR_NONE);
  877. }
  878. //
  879. // Check for errors.
  880. //
  881. if(ulErr & (I2C_MCS_ERROR | I2C_MCS_ARBLST))
  882. {
  883. return(ulErr & (I2C_MCS_ARBLST | I2C_MCS_DATACK | I2C_MCS_ADRACK));
  884. }
  885. else
  886. {
  887. return(I2C_MASTER_ERR_NONE);
  888. }
  889. }
  890. //*****************************************************************************
  891. //
  892. //! Transmits a byte from the I2C Master.
  893. //!
  894. //! \param ulBase is the base address of the I2C Master module.
  895. //! \param ucData data to be transmitted from the I2C Master
  896. //!
  897. //! This function will place the supplied data into I2C Master Data Register.
  898. //!
  899. //! \return None.
  900. //
  901. //*****************************************************************************
  902. void
  903. I2CMasterDataPut(unsigned long ulBase, unsigned char ucData)
  904. {
  905. //
  906. // Check the arguments.
  907. //
  908. ASSERT((ulBase == I2C0_MASTER_BASE) || (ulBase == I2C1_MASTER_BASE));
  909. //
  910. // Write the byte.
  911. //
  912. HWREG(ulBase + I2C_O_MDR) = ucData;
  913. }
  914. //*****************************************************************************
  915. //
  916. //! Receives a byte that has been sent to the I2C Master.
  917. //!
  918. //! \param ulBase is the base address of the I2C Master module.
  919. //!
  920. //! This function reads a byte of data from the I2C Master Data Register.
  921. //!
  922. //! \return Returns the byte received from by the I2C Master, cast as an
  923. //! unsigned long.
  924. //
  925. //*****************************************************************************
  926. unsigned long
  927. I2CMasterDataGet(unsigned long ulBase)
  928. {
  929. //
  930. // Check the arguments.
  931. //
  932. ASSERT((ulBase == I2C0_MASTER_BASE) || (ulBase == I2C1_MASTER_BASE));
  933. //
  934. // Read a byte.
  935. //
  936. return(HWREG(ulBase + I2C_O_MDR));
  937. }
  938. //*****************************************************************************
  939. //
  940. //! Gets the I2C Slave module status
  941. //!
  942. //! \param ulBase is the base address of the I2C Slave module.
  943. //!
  944. //! This function will return the action requested from a master, if any.
  945. //! Possible values are:
  946. //!
  947. //! - \b I2C_SLAVE_ACT_NONE
  948. //! - \b I2C_SLAVE_ACT_RREQ
  949. //! - \b I2C_SLAVE_ACT_TREQ
  950. //! - \b I2C_SLAVE_ACT_RREQ_FBR
  951. //!
  952. //! \return Returns \b I2C_SLAVE_ACT_NONE to indicate that no action has been
  953. //! requested of the I2C Slave module, \b I2C_SLAVE_ACT_RREQ to indicate that
  954. //! an I2C master has sent data to the I2C Slave module, \b I2C_SLAVE_ACT_TREQ
  955. //! to indicate that an I2C master has requested that the I2C Slave module send
  956. //! data, and \b I2C_SLAVE_ACT_RREQ_FBR to indicate that an I2C master has sent
  957. //! data to the I2C slave and the first byte following the slave's own address
  958. //! has been received.
  959. //
  960. //*****************************************************************************
  961. unsigned long
  962. I2CSlaveStatus(unsigned long ulBase)
  963. {
  964. //
  965. // Check the arguments.
  966. //
  967. ASSERT((ulBase == I2C0_SLAVE_BASE) || (ulBase == I2C1_SLAVE_BASE));
  968. //
  969. // Return the slave status.
  970. //
  971. return(HWREG(ulBase + I2C_O_SCSR));
  972. }
  973. //*****************************************************************************
  974. //
  975. //! Transmits a byte from the I2C Slave.
  976. //!
  977. //! \param ulBase is the base address of the I2C Slave module.
  978. //! \param ucData data to be transmitted from the I2C Slave
  979. //!
  980. //! This function will place the supplied data into I2C Slave Data Register.
  981. //!
  982. //! \return None.
  983. //
  984. //*****************************************************************************
  985. void
  986. I2CSlaveDataPut(unsigned long ulBase, unsigned char ucData)
  987. {
  988. //
  989. // Check the arguments.
  990. //
  991. ASSERT((ulBase == I2C0_SLAVE_BASE) || (ulBase == I2C1_SLAVE_BASE));
  992. //
  993. // Write the byte.
  994. //
  995. HWREG(ulBase + I2C_O_SDR) = ucData;
  996. }
  997. //*****************************************************************************
  998. //
  999. //! Receives a byte that has been sent to the I2C Slave.
  1000. //!
  1001. //! \param ulBase is the base address of the I2C Slave module.
  1002. //!
  1003. //! This function reads a byte of data from the I2C Slave Data Register.
  1004. //!
  1005. //! \return Returns the byte received from by the I2C Slave, cast as an
  1006. //! unsigned long.
  1007. //
  1008. //*****************************************************************************
  1009. unsigned long
  1010. I2CSlaveDataGet(unsigned long ulBase)
  1011. {
  1012. //
  1013. // Check the arguments.
  1014. //
  1015. ASSERT((ulBase == I2C0_SLAVE_BASE) || (ulBase == I2C1_SLAVE_BASE));
  1016. //
  1017. // Read a byte.
  1018. //
  1019. return(HWREG(ulBase + I2C_O_SDR));
  1020. }
  1021. //*****************************************************************************
  1022. //
  1023. // Close the Doxygen group.
  1024. //! @}
  1025. //
  1026. //*****************************************************************************