i2c.c 46 KB

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