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