aboutsummaryrefslogtreecommitdiffstats
path: root/Software/Embedded_SW/Embedded/Drivers/I2C_Communication/test_fifo_dma.c
blob: 75028fc4af2124f9ff8482ad3717992c4d8ccb4b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
/*
 * test_fifo_dma.c
 *
 *  Created on: Jul 1, 2020
 *      Author: avi
 */

//*****************************************************************************
//
// ektm4c129_i2c_master_udma_fifo.c - I2C Master with UDMA and FIFO Data Transfer
//
// Copyright (c) 2013-2015 Texas Instruments Incorporated.  All rights reserved.
// Software License Agreement
//
// Texas Instruments (TI) is supplying this software for use solely and
// exclusively on TI's microcontroller products. The software is owned by
// TI and/or its suppliers, and is protected under applicable copyright
// laws. You may not combine this software with "viral" open-source
// software in order to form a larger program.
//
// THIS SOFTWARE IS PROVIDED "AS IS" AND WITH ALL FAULTS.
// NO WARRANTIES, WHETHER EXPRESS, IMPLIED OR STATUTORY, INCLUDING, BUT
// NOT LIMITED TO, IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
// A PARTICULAR PURPOSE APPLY TO THIS SOFTWARE. TI SHALL NOT, UNDER ANY
// CIRCUMSTANCES, BE LIABLE FOR SPECIAL, INCIDENTAL, OR CONSEQUENTIAL
// DAMAGES, FOR ANY REASON WHATSOEVER.
//
// This is part of revision 2.1.1.71 of the EK-TM4C1294XL Firmware Package.
//
//*****************************************************************************
#include <stdint.h>
#include <stdbool.h>
#include "inc/hw_types.h"
#include "inc/hw_i2c.h"
#include "inc/hw_ints.h"
#include "inc/hw_memmap.h"
#include "inc/hw_udma.h"
#include "driverlib/gpio.h"
#include "driverlib/i2c.h"
#include "driverlib/interrupt.h"
#include "driverlib/pin_map.h"
#include "driverlib/sysctl.h"
#include "driverlib/uart.h"
#include "driverlib/udma.h"
#include "utils/random.h"
#include "utils/uartstdio.h"

//*****************************************************************************
//
//! \addtogroup example_list
//! <h1>ektm4c129_i2c_master_udma_fifo (ektm4c129_i2c_master_udma_fifo)</h1>
//!
//! A Simple I2C Master Code for Performing Data Transfer with UDMA and
//! I2CFIFODATA register
//
//*****************************************************************************

//*****************************************************************************
//
// Define for I2C Module
//
//*****************************************************************************
uint8_t SLAVE_ADDRESS_EXT;
#define NUM_OF_I2CBYTES   255

//*****************************************************************************
//
// Enumerated Data Types for Master State Machine
//
//*****************************************************************************
enum I2C_MASTER_STATE
{
    I2C_OP_IDLE = 0,
    I2C_OP_TXADDR,
    I2C_OP_FIFO,
    I2C_OP_TXDATA,
    I2C_OP_RXDATA,
    I2C_OP_STOP,
    I2C_ERR_STATE
};

//*****************************************************************************
//
// Global variable for Delay Count
//
//*****************************************************************************
volatile uint16_t g_ui16SlaveWordAddress;
uint8_t  g_ui8MasterTxData[NUM_OF_I2CBYTES];
uint8_t  g_ui8MasterRxData[NUM_OF_I2CBYTES];
volatile uint8_t  g_ui8MasterCurrState;
volatile uint8_t  g_ui8MasterPrevState;
volatile bool     g_bI2CDirection;
volatile bool     g_bI2CRepeatedStart;
volatile uint8_t  g_ui8MasterBytes       = NUM_OF_I2CBYTES;
volatile uint8_t  g_ui8MasterBytesLength = NUM_OF_I2CBYTES;


uint32_t ui32TxArbSize, ui32RxArbSize;

//*****************************************************************************
//
// The control table used by the uDMA controller.  This table must be aligned
// to a 1024 byte boundary.
//
//*****************************************************************************
#if defined(ewarm)
#pragma data_alignment=1024
uint8_t pui8ControlTable[1024];
#elif defined(ccs)
#pragma DATA_ALIGN(pui8ControlTable, 1024)
uint8_t pui8ControlTable[1024];
#else
uint8_t pui8ControlTable[1024] __attribute__ ((aligned(1024)));
#endif

//*****************************************************************************
//
// Interrupt Handler for I2C Master Interface
//
//*****************************************************************************
void
I2C3IntHandler(void)
{
    uint32_t ui32I2CMasterInterruptStatus;

    //
    // Toggle PL4 High to Indicate Entry to ISR
    //
    ///GPIOPinWrite(GPIO_PORTL_BASE, GPIO_PIN_4, GPIO_PIN_4);

    //
    // Get the masked interrupt status and clear the flags
    //
    ui32I2CMasterInterruptStatus = I2CMasterIntStatusEx(I2C3_BASE, true);
    I2CMasterIntClearEx(I2C3_BASE, ui32I2CMasterInterruptStatus);

    //
    // Execute the State Machine
    //
    switch (g_ui8MasterCurrState) {
    case I2C_OP_IDLE:
        //
        // Move from IDLE to Transmit Address State
        //
        g_ui8MasterPrevState = g_ui8MasterCurrState;
        g_ui8MasterCurrState = I2C_OP_TXADDR;

        //
        // Write the upper bits of the page to the Slave
        //
        I2CMasterSlaveAddrSet(I2C3_BASE, SLAVE_ADDRESS_EXT, false);
        I2CMasterDataPut(I2C3_BASE, (g_ui16SlaveWordAddress >> 8));
        I2CMasterControl(I2C3_BASE, I2C_MASTER_CMD_BURST_SEND_START);
        break;

    case I2C_OP_TXADDR:
        //
        // Assign the current state to the previous state
        //
        g_ui8MasterPrevState = g_ui8MasterCurrState;

        //
        // If Address has been NAK'ed then go to stop state
        // else go the FIFO Priming State
        //
        if(ui32I2CMasterInterruptStatus & I2C_MASTER_INT_NACK)
        {
            g_ui8MasterCurrState = I2C_OP_STOP;
        }
        else
        {
            g_ui8MasterCurrState = I2C_OP_FIFO;
        }

        //
        // Write the lower bits of the page to the Slave if
        // Address has been ACK-ed
        //
        I2CMasterDataPut(I2C3_BASE, (g_ui16SlaveWordAddress >> 0));
        I2CMasterControl(I2C3_BASE, I2C_MASTER_CMD_BURST_SEND_CONT);
        break;

    case I2C_OP_FIFO:
        //
        // If Last Data has been NAK'ed then go to stop state
        //
        if(ui32I2CMasterInterruptStatus & I2C_MASTER_INT_NACK)
        {
            g_ui8MasterCurrState = I2C_OP_STOP;
        }
        //
        // Based on the direction move to the appropriate state
        // of Transmit or Receive. Also send the BURST command
        // for FIFO Operations.
        //
        else if(!g_bI2CDirection)
        {
            g_ui8MasterCurrState = I2C_OP_TXDATA;
            I2CMasterControl(I2C3_BASE, I2C_MASTER_CMD_FIFO_BURST_SEND_FINISH);
        }
        else
        {
            g_ui8MasterCurrState = I2C_OP_RXDATA;
            I2CMasterSlaveAddrSet(I2C3_BASE, SLAVE_ADDRESS_EXT, true);
            I2CMasterControl(I2C3_BASE, I2C_MASTER_CMD_FIFO_SINGLE_RECEIVE);
        }
        break;

    case I2C_OP_TXDATA:
        //
        // Move the current state to the previous state
        // Else continue with the transmission till last byte
        //
        g_ui8MasterPrevState = g_ui8MasterCurrState;

        //
        // If Address or Data has been NAK'ed then go to stop state
        // If a Stop condition is seen due to number of bytes getting
        // done then move to STOP state
        //
        if(ui32I2CMasterInterruptStatus & I2C_MASTER_INT_NACK)
        {
            g_ui8MasterCurrState = I2C_OP_STOP;
        }
        else if(ui32I2CMasterInterruptStatus & I2C_MASTER_INT_STOP)
        {
            g_ui8MasterCurrState = I2C_OP_STOP;
        }
        else if(ui32I2CMasterInterruptStatus & I2C_MASTER_INT_TX_DMA_DONE)
        {
            g_ui8MasterCurrState = I2C_OP_STOP;
        }
        else
        {
            g_ui8MasterCurrState = I2C_ERR_STATE;
        }
        break;

    case I2C_OP_RXDATA:
        //
        // Move the current state to the previous state
        // Else continue with the transmission till last byte
        //
        g_ui8MasterPrevState = g_ui8MasterCurrState;

        //
        // If Address has been NAK'ed then go to stop state
        // If a Stop condition is seen due to number of bytes getting
        // done then move to STOP state and read the last data byte
        //
        if(ui32I2CMasterInterruptStatus & I2C_MASTER_INT_NACK)
        {
            g_ui8MasterCurrState = I2C_OP_STOP;
        }
        else if(ui32I2CMasterInterruptStatus & I2C_MASTER_INT_STOP)
        {
            g_ui8MasterCurrState = I2C_OP_STOP;
        }
        else if(ui32I2CMasterInterruptStatus & I2C_MASTER_INT_RX_DMA_DONE)
        {
            g_ui8MasterCurrState = I2C_OP_STOP;
        }
        break;

    case I2C_OP_STOP:
        //
        // Move the current state to the previous state
        // Else continue with the transmission till last byte
        //
        g_ui8MasterPrevState = g_ui8MasterCurrState;
        break;

    case I2C_ERR_STATE:
        g_ui8MasterCurrState = I2C_ERR_STATE;
        break;

    default:
        g_ui8MasterCurrState = I2C_ERR_STATE;
        break;
    }

    //
    // Toggle PL4 Low to Indicate Exit from ISR
    //
    ///GPIOPinWrite(GPIO_PORTL_BASE, GPIO_PIN_4, 0x0);
}

//*****************************************************************************
//
// This function sets up UDMA Channel Control Structures for TX
//
//*****************************************************************************
void
ConfigureuDMATX(uint8_t ui8Length, uint32_t ui32ArbSize)
{
    //
    // Put the attributes in a known state for the uDMA channel.
    // These should already be disabled by default.
    //
    uDMAChannelAttributeDisable(UDMA_CH19_I2C3TX,
                                UDMA_ATTR_USEBURST | UDMA_ATTR_ALTSELECT |
                                (UDMA_ATTR_HIGH_PRIORITY |
                                UDMA_ATTR_REQMASK));

    //
    // Configure the control parameters for the I2C2 TX channel.  The channel
    // will be used to transfer between memory buffer and I2CFIFODATA register
    // 8 bits at a time. Therefore the data size is 8 bits, and the address
    // increment is 8 bits for source. The destination increment is none.
    // The arbitration size will be set based on the TX FIFO Threshold, which
    // causes the uDMA controller to rearbitrate after N items are transferred.
    // This keeps this channel from hogging the uDMA controller once the transfer
    // is started and allows other channels cycles if they are higher priority.
    //
    uDMAChannelControlSet(UDMA_CH19_I2C3TX | UDMA_PRI_SELECT,
                          UDMA_SIZE_8 | UDMA_SRC_INC_8 | UDMA_DST_INC_NONE |
                          ui32ArbSize);

    //
    // Set up the transfer parameters for the hardware channel.  This will
    // configure the transfer buffers and the transfer size.  Basic mode
    // is used in this example
    //
    uDMAChannelTransferSet(UDMA_CH19_I2C3TX | UDMA_PRI_SELECT,
                           UDMA_MODE_BASIC, g_ui8MasterTxData,
                           (void *)(I2C3_BASE + I2C_O_FIFODATA),
                           ui8Length);

    //
    // The channel must be enabled.  For hardware based transfers, a request
    // must be issued by the peripheral.  After this, the uDMA memory transfer begins.
    //
    uDMAChannelEnable(UDMA_CH19_I2C3TX);

}

//*****************************************************************************
//
// This function sets up UDMA Channel Control Structures for RX
//
//*****************************************************************************
void
ConfigureuDMARX(uint8_t ui8Length, uint32_t ui32ArbSize)
{
    //
    // Put the attributes in a known state for the uDMA software channel.
    // These should already be disabled by default.
    //
    uDMAChannelAttributeDisable(UDMA_CH18_I2C3RX,
                                UDMA_ATTR_USEBURST | UDMA_ATTR_ALTSELECT |
                                (UDMA_ATTR_HIGH_PRIORITY |
                                UDMA_ATTR_REQMASK));

    //
    // Configure the control parameters for the I2C2 RX channel.  The channel
    // will be used to transfer between I2CFIFODATA register and memory buffers,
    // 8 bits at a time. Therefore the data size is 8 bits, and the address
    // increment is 8 bits for destination. The source increment is none.
    // The arbitration size will be set based on the RX FIFO Threshold, which
    // causes the uDMA controller to rearbitrate after N items are transferred.
    // This keeps this channel from hogging the uDMA controller once the transfer
    // is started and allows other channels cycles if they are higher priority.
    //
    uDMAChannelControlSet(UDMA_CH18_I2C3RX | UDMA_PRI_SELECT,
                          UDMA_SIZE_8 | UDMA_SRC_INC_NONE | UDMA_DST_INC_8 |
                          ui32ArbSize);

    //
    // Set up the transfer parameters for the hardware channel.  This will
    // configure the transfer buffers and the transfer size.  Basic mode
    // is used in this example
    //
    uDMAChannelTransferSet(UDMA_CH18_I2C3RX | UDMA_PRI_SELECT,
                           UDMA_MODE_BASIC, (void *)(I2C3_BASE + I2C_O_FIFODATA),
                           g_ui8MasterRxData,
                           ui8Length);

    //
    // The channel must be enabled.  For hardware based transfers, a request
    // must be issued by the peripheral.  After this, the uDMA memory transfer begins.
    //
    uDMAChannelEnable(UDMA_CH18_I2C3RX);

}

//*****************************************************************************
//
// This function sets up UART0 to be used for a console to display information
// as the example is running.
//
//*****************************************************************************
/*
void
InitConsole(void)
{
    //
    // Enable GPIO port A which is used for UART0 pins.
    // TODO: change this to whichever GPIO port you are using.
    //
    SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOA);

    //
    // Configure the pin muxing for UART0 functions on port A0 and A1.
    // This step is not necessary if your part does not support pin muxing.
    // TODO: change this to select the port/pin you are using.
    //
    GPIOPinConfigure(GPIO_PA0_U0RX);
    GPIOPinConfigure(GPIO_PA1_U0TX);

    //
    // Enable UART0 so that we can configure the clock.
    //
    SysCtlPeripheralEnable(SYSCTL_PERIPH_UART0);

    //
    // Use the internal 16MHz oscillator as the UART clock source.
    //
    UARTClockSourceSet(UART0_BASE, UART_CLOCK_PIOSC);

    //
    // Select the alternate (UART) function for these pins.
    // TODO: change this to select the port/pin you are using.
    //
    GPIOPinTypeUART(GPIO_PORTA_BASE, GPIO_PIN_0 | GPIO_PIN_1);

    //
    // Initialize the UART for console I/O.
    //
    UARTStdioConfig(0, 115200, 16000000);
}
*/
//*****************************************************************************
//
// Main Program to Configure and Use the I2C Master
//
//*****************************************************************************
int
InitI2C3_udma_fifo(void)
{
    uint32_t ui32SysClock;
    uint8_t  /*ui8Count, */ui8MasterBytesLength;
    //


    //
    // Set up the serial console to use for displaying messages.  This is
    // just for this example program and is not needed for EPI operation.
    //
    ///InitConsole();

    //
    // Display the setup on the console.
    //
    ///UARTprintf("\033[2J\033[H");
    ///UARTprintf("\r\nExample Code for I2C Master with");
    ///UARTprintf("\nUDMA and FIFO Data Transfer\n\n");

    //
    // Stop the Clock, Reset and Enable UDMA Module
    // in Master Function
    //
    SysCtlPeripheralDisable(SYSCTL_PERIPH_UDMA);
    SysCtlPeripheralReset(SYSCTL_PERIPH_UDMA);
    SysCtlPeripheralEnable(SYSCTL_PERIPH_UDMA);

    //
    // Wait for the Peripheral to be ready for programming
    //
    while(!SysCtlPeripheralReady(SYSCTL_PERIPH_UDMA));

    //
    // Enable the uDMA controller.
    //
    uDMAEnable();

    //
    // Point at the control table to use for channel control structures.
    //
    uDMAControlBaseSet(pui8ControlTable);

    //
    // Assign the UDMA Channel for I2C2 RX and TX DMA Request
    //
    uDMAChannelAssign(UDMA_CH18_I2C3RX);
    uDMAChannelAssign(UDMA_CH19_I2C3TX);

    //
    // Enable GPIO for Configuring the I2C Interface Pins
    //
    SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOR);

    //
    // Wait for the Peripheral to be ready for programming
    //
    while(!SysCtlPeripheralReady(SYSCTL_PERIPH_GPIOR));

    //
    // Configure Pins for I2C2 Master Interface
    //
    GPIOPinConfigure(GPIO_PR4_I2C3SCL);
    GPIOPinConfigure(GPIO_PR5_I2C3SDA);
    GPIOPinTypeI2C(GPIO_PORTR_BASE, GPIO_PIN_5);
    GPIOPinTypeI2CSCL(GPIO_PORTR_BASE, GPIO_PIN_4);

    //
    // Configure GPIO Pin PL4 for Interrupt Time Processing
    //
    ///GPIOPinTypeGPIOOutput(GPIO_PORTL_BASE, GPIO_PIN_4);
    ///GPIOPinWrite(GPIO_PORTL_BASE, GPIO_PIN_4, 0x0);

    //
    // Setup System Clock for 120MHz
    //
    ui32SysClock = SysCtlClockFreqSet((SYSCTL_OSC_MAIN | SYSCTL_USE_PLL | SYSCTL_XTAL_25MHZ |
                        SYSCTL_CFG_VCO_480), 120000000);

    //
    // Stop the Clock, Reset and Enable I2C Module
    // in Master Function
    //
    SysCtlPeripheralDisable(SYSCTL_PERIPH_I2C3);
    SysCtlPeripheralReset(SYSCTL_PERIPH_I2C3);
    SysCtlPeripheralEnable(SYSCTL_PERIPH_I2C3);

    //
    // Wait for the Peripheral to be ready for programming
    //
    while(!SysCtlPeripheralReady(SYSCTL_PERIPH_I2C3));

    //
    // Initialize and Configure the Master Module
    //
    I2CMasterInitExpClk(I2C3_BASE, ui32SysClock, true);

    //
    // Assign the Transmit and Receive FIFO to the Master
    // Transmit threshold of 2 means that when there are less
    // than or equal to 2 bytes in the TX FIFO then generate
    // an interrupt
    // Receive threshold of 6 means that when there are more
    // than or qual to 6 bytes in the RX FIFO then generate
    // an interrupt
    //
    I2CTxFIFOConfigSet(I2C3_BASE, I2C_FIFO_CFG_TX_MASTER_DMA | I2C_FIFO_CFG_TX_TRIG_2);
    I2CRxFIFOConfigSet(I2C3_BASE, I2C_FIFO_CFG_RX_MASTER_DMA | I2C_FIFO_CFG_RX_TRIG_3);

    //
    // Transmit uDMA Arbitrartion size is calculated as the
    // smaller mod 2 which can write to the TXFIFO without
    // overrun
    // TX TRIGGER               ARB
    // I2C_FIFO_CFG_TX_NO_TRIG  UDMA_ARB_8
    // I2C_FIFO_CFG_TX_TRIG_1   UDMA_ARB_4
    // I2C_FIFO_CFG_TX_TRIG_2   UDMA_ARB_4
    // I2C_FIFO_CFG_TX_TRIG_3   UDMA_ARB_4
    // I2C_FIFO_CFG_TX_TRIG_4   UDMA_ARB_4
    // I2C_FIFO_CFG_TX_TRIG_5   UDMA_ARB_2
    // I2C_FIFO_CFG_TX_TRIG_6   UDMA_ARB_2
    // I2C_FIFO_CFG_TX_TRIG_7   UDMA_ARB_1
    //
    ui32TxArbSize = UDMA_ARB_4;

    //
    // Receive uDMA Arbitrartion size is calculated as the
    // Trigger Level to read back data from RXFIFO without
    // an underrun
    // RX TRIGGER               ARB
    // I2C_FIFO_CFG_RX_NO_TRIG  INVALID
    // I2C_FIFO_CFG_RX_TRIG_1   UDMA_ARB_1
    // I2C_FIFO_CFG_RX_TRIG_2   UDMA_ARB_2
    // I2C_FIFO_CFG_RX_TRIG_3   UDMA_ARB_2
    // I2C_FIFO_CFG_RX_TRIG_4   UDMA_ARB_4
    // I2C_FIFO_CFG_RX_TRIG_5   UDMA_ARB_4
    // I2C_FIFO_CFG_RX_TRIG_6   UDMA_ARB_4
    // I2C_FIFO_CFG_RX_TRIG_7   UDMA_ARB_4
    //
    ui32RxArbSize = UDMA_ARB_2;

    //
    // Flush any existing data in the FIFO
    //
    I2CTxFIFOFlush(I2C3_BASE);
    I2CRxFIFOFlush(I2C3_BASE);

    //
    // Enable Interrupts for Arbitration Lost, Stop, NAK,
    // Clock Low Timeout and Data.
    //
    I2CMasterIntEnableEx(I2C3_BASE, (I2C_MASTER_INT_ARB_LOST |
            I2C_MASTER_INT_STOP | I2C_MASTER_INT_NACK |
            I2C_MASTER_INT_TIMEOUT | I2C_MASTER_INT_DATA));

    //
    // Enable the Interrupt in the NVIC from I2C Master
    //
    IntEnable(INT_I2C3);

    //
    // Enable the Glitch Filter. Writting a value 0 will
    // disable the glitch filter
    // I2C_MASTER_GLITCH_FILTER_DISABLED
    // I2C_MASTER_GLITCH_FILTER_1
    // I2C_MASTER_GLITCH_FILTER_2 : Ideal Value when in HS Mode
    //                              for 120MHz clock
    // I2C_MASTER_GLITCH_FILTER_4
    // I2C_MASTER_GLITCH_FILTER_8 : Ideal Value when in Std,
    //                              Fast, Fast+ for 120MHz clock
    // I2C_MASTER_GLITCH_FILTER_16
    // I2C_MASTER_GLITCH_FILTER_32
    //
    I2CMasterGlitchFilterConfigSet(I2C3_BASE, I2C_MASTER_GLITCH_FILTER_8);

    //
    // Initialize and Configure the Master Module State Machine
    //
    g_ui8MasterCurrState = I2C_OP_IDLE;

    return 0;

}

uint32_t I2C_Write_udmaFifo(uint32_t base, uint8_t addr, uint8_t *data, uint32_t len)
{
    uint8_t  ui8Count;
    //uint32_t ui32TxArbSize;
    SLAVE_ADDRESS_EXT = addr;
    uint8_t  ui8MasterBytesLength;
    //
    // Check if the Bus is Busy or not
    //
    while(I2CMasterBusBusy(base));

    //
    // Randomly Initialize the Transmit buffer and
    // set the receive buffer to 0xFF
    //
    for(ui8Count=0 ; ui8Count < len ; ui8Count++)
    {
        g_ui8MasterTxData[ui8Count]   = data[ui8Count];

        //
        // Change the Random Value for the next
        // iteration..
        //
        ///RandomAddEntropy(RandomSeed());

        //
        // Init the receive buffers with the value
        // 0xFF
        //
        ////g_ui8MasterRxData[ui8Count]   = 0xFF;
    }

    //
    // When sending data to slave make sure
    // RX DMA DONE interrupt bit is masked and
    // TX DMA DONE interrupt bit is unmasked
    //
    I2CMasterIntEnableEx(base, I2C_MASTER_INT_TX_DMA_DONE);
    I2CMasterIntDisableEx(base, I2C_MASTER_INT_RX_DMA_DONE);

    //
    // Set the I2CMBLEN register and also initialize
    // the internal flag
    //
    ui8MasterBytesLength = 32;

    I2CMasterBurstLengthSet(base, ui8MasterBytesLength);

    //
    // Configure the uDMA Control Channel Structure for TX
    //
    ConfigureuDMATX(ui8MasterBytesLength, ui32TxArbSize);

    //
    // Set Transmit Flag and set the Page Address in
    // external slave to 0x0000
    //
    g_bI2CDirection        = false;
    g_ui16SlaveWordAddress = 0x0;
    g_ui8MasterBytes       = 0;

    //
    // Print Message before sending data
    //
    ///UARTprintf("Transmit %d bytes to external Slave...\n\n",ui8MasterBytesLength);

    //
    // Trigger the Transfer using Software Interrupt
    //
    g_ui8MasterCurrState = I2C_OP_IDLE;
    IntTrigger(INT_I2C3);
    while(g_ui8MasterCurrState != I2C_OP_STOP);

    return 0;
}

uint32_t I2C_Read_udmaFifo(uint32_t base, uint8_t addr, uint8_t *data, uint32_t len)
{
    uint8_t  ui8Count;
    bool     bError;
    //uint32_t ui32RxArbSize;
    uint8_t  ui8MasterBytesLength;

    SLAVE_ADDRESS_EXT = addr;
    //
    // When receiving data from slave make sure
    // TX DMA DONE interrupt bit is masked and
    // RX DMA DONE interrupt bit is unmasked
    //
    I2CMasterIntEnableEx(base, I2C_MASTER_INT_RX_DMA_DONE);
    I2CMasterIntDisableEx(base, I2C_MASTER_INT_TX_DMA_DONE);

    //
    // Set the I2CMBLEN register and also initialize
    // the internal flag
    //
    ui8MasterBytesLength = 32;

    I2CMasterBurstLengthSet(base, ui8MasterBytesLength);

    //
    // Configure the uDMA Control Channel Structure for RX
    //
    ConfigureuDMARX(ui8MasterBytesLength,ui32RxArbSize);

    //
    // Set receive Flag and set the Page Address in
    // external slave to 0x0000
    //
    g_bI2CDirection        = true;
    g_bI2CRepeatedStart    = true;
    g_ui16SlaveWordAddress = 0x0;
    g_ui8MasterBytes       = 0;

    //
    // Print Message before receiving data
    //
    ///UARTprintf("Receiving %d bytes from external Slave...\n\n",ui8MasterBytesLength);

    //
    // Trigger the Transfer using Software Interrupt
    //
    g_ui8MasterCurrState = I2C_OP_IDLE;
    IntTrigger(INT_I2C3);
    while(g_ui8MasterCurrState != I2C_OP_STOP);

    //
    // Perform Data Integrity Check...
    //
    bError = false;
    for(ui8Count = 0 ; ui8Count < len ; ui8Count++)
    {
        data[ui8Count] = g_ui8MasterRxData[ui8Count];

    }

    return 0;

}