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