This thread has been locked.

If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.

TM4C129ENCPDT: I2C Loop-back in Loop

Part Number: TM4C129ENCPDT
Other Parts Discussed in Thread: EK-TM4C129EXL

Hi,

I am working off of the example code for the I2C peripheral  provided in TivaWare_C_Series-2.2.0.295 with a EK-TM4C129EXL Rev A development board. I have copied the code into a project and have ran it successfully without modification to completion. I was even able to use the terminal to inspect the UARTPrint commands. I then modified the code in a way where it would continuously be sending the repeated "I2C" characters to itself. To do this I simply put it in a while(1) loop and moved the part of the code that defines the I2CMasterSlaveAddrSet within the loop so that the loop back would not print multiple times and the init console can remain the same. The first pass works perfectly fine but when the loop happens and we return the master to write mode it gets stuck trying to acknowledge the RREQ at the slave status.

During debugging I noticed that the state of MCS is not idle when returning to the start of the while loop. The busy and run bits are stile active unlike my first pass of the loop that I noticed. I tried reviewing the procedure in the documentation for Master Single Receive but am puzzled given the interaction of the loop back. I also noticed that the TREQ and TXFIFO are still enabled.

Can I please get some clarification on these issues. I would like to go between reading and writing seamlessly using this single read and write procedure.

Best regards,

Stephen G.

  • Hi,

      Can you post your modified code?

  • //*****************************************************************************
    //
    // master_slave_loopback.c - Example demonstrating a simple I2C message
    //                           transmission and reception.
    //
    // Copyright (c) 2010-2020 Texas Instruments Incorporated.  All rights reserved.
    // Software License Agreement
    // 
    //   Redistribution and use in source and binary forms, with or without
    //   modification, are permitted provided that the following conditions
    //   are met:
    //
    //   Redistributions of source code must retain the above copyright
    //   notice, this list of conditions and the following disclaimer.
    //
    //   Redistributions in binary form must reproduce the above copyright
    //   notice, this list of conditions and the following disclaimer in the
    //   documentation and/or other materials provided with the
    //   distribution.
    //
    //   Neither the name of Texas Instruments Incorporated nor the names of
    //   its contributors may be used to endorse or promote products derived
    //   from this software without specific prior written permission.
    // 
    // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
    // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
    // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
    // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
    // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
    // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
    // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
    // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
    // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
    // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
    // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
    // 
    // This is part of revision 2.2.0.295 of the Tiva Firmware Development Package.
    //
    //*****************************************************************************
    
    #include <stdbool.h>
    #include <stdint.h>
    #include "inc/hw_i2c.h"
    #include "inc/hw_memmap.h"
    #include "inc/hw_types.h"
    #include "driverlib/gpio.h"
    #include "driverlib/i2c.h"
    #include "driverlib/pin_map.h"
    #include "driverlib/sysctl.h"
    #include "driverlib/uart.h"
    #include "utils/uartstdio.h"
    
    //*****************************************************************************
    //
    //! \addtogroup i2c_examples_list
    //! <h1>I2C Master Loopback (i2c_master_slave_loopback)</h1>
    //!
    //! This example shows how to configure the I2C0 module for loopback mode.
    //! This includes setting up the master and slave module.  Loopback mode
    //! internally connects the master and slave data and clock lines together.
    //! The address of the slave module is set in order to read data from the
    //! master.  Then the data is checked to make sure the received data matches
    //! the data that was transmitted.  This example uses a polling method for
    //! sending and receiving data.
    //!
    //! This example uses the following peripherals and I/O signals.  You must
    //! review these and change as needed for your own board:
    //! - I2C0 peripheral
    //! - GPIO Port B peripheral (for I2C0 pins)
    //! - I2C0SCL - PB2
    //! - I2C0SDA - PB3
    //!
    //! The following UART signals are configured only for displaying console
    //! messages for this example.  These are not required for operation of I2C.
    //! - UART0 peripheral
    //! - GPIO Port A peripheral (for UART0 pins)
    //! - UART0RX - PA0
    //! - UART0TX - PA1
    //!
    //! This example uses the following interrupt handlers.  To use this example
    //! in your own application you must add these interrupt handlers to your
    //! vector table.
    //! - None.
    //
    //*****************************************************************************
    
    //*****************************************************************************
    //
    // Number of I2C data packets to send.
    //
    //*****************************************************************************
    #define NUM_I2C_DATA 3
    
    //*****************************************************************************
    //
    // Set the address for slave module. This is a 7-bit address sent in the
    // following format:
    //                      [A6:A5:A4:A3:A2:A1:A0:RS]
    //
    // A zero in the "RS" position of the first byte means that the master
    // transmits (sends) data to the selected slave, and a one in this position
    // means that the master receives data from the slave.
    //
    //*****************************************************************************
    #define SLAVE_ADDRESS 0x3C
    
    #define I2C_BASE                I2C1_BASE
    #define I2C_SYSCTL              SYSCTL_PERIPH_I2C1
    
    #define I2C_SCL_SYSCTL_PERIPH   SYSCTL_PERIPH_GPIOG
    #define I2C_SCL_GPIO_PORT_BASE  GPIO_PORTG_BASE
    #define I2C_SCL                 GPIO_PIN_0
    #define I2C_SCL_CFG             GPIO_PG0_I2C1SCL
    
    #define I2C_SDA_SYSCTL_PERIPH   SYSCTL_PERIPH_GPIOG
    #define I2C_SDA_GPIO_PORT_BASE  GPIO_PORTG_BASE
    #define I2C_SDA                 GPIO_PIN_1
    #define I2C_SDA_CFG             GPIO_PG1_I2C1SDA
    
    //*****************************************************************************
    //
    // 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);
    }
    
    //*****************************************************************************
    //
    // Configure the I2C0 master and slave and connect them using loopback mode.
    //
    //*****************************************************************************
    int
    main(void)
    {
    #if defined(TARGET_IS_TM4C129_RA0) ||                                         \
        defined(TARGET_IS_TM4C129_RA1) ||                                         \
        defined(TARGET_IS_TM4C129_RA2)
        uint32_t ui32SysClock;
    #endif
        uint32_t pui32DataTx[NUM_I2C_DATA];
        uint32_t pui32DataRx[NUM_I2C_DATA];
        uint32_t ui32Index;
    
        //
        // Set the clocking to run directly from the external crystal/oscillator.
        // TODO: The SYSCTL_XTAL_ value must be changed to match the value of the
        // crystal on your board.
        //
    #if defined(TARGET_IS_TM4C129_RA0) ||                                         \
        defined(TARGET_IS_TM4C129_RA1) ||                                         \
        defined(TARGET_IS_TM4C129_RA2)
        ui32SysClock = SysCtlClockFreqSet((SYSCTL_XTAL_25MHZ |
                                           SYSCTL_OSC_MAIN |
                                           SYSCTL_USE_OSC), 25000000);
    #else
        SysCtlClockSet(SYSCTL_SYSDIV_1 | SYSCTL_USE_OSC | SYSCTL_OSC_MAIN |
                       SYSCTL_XTAL_16MHZ);
    #endif
    
        //
        // The I2C0 peripheral must be enabled before use.
        //
        SysCtlPeripheralEnable(I2C_SYSCTL);
        SysCtlPeripheralReset(I2C_SYSCTL);
    
        //
        // For this example I2C0 is used with PortB[3:2].  The actual port and
        // pins used may be different on your part, consult the data sheet for
        // more information.  GPIO port B needs to be enabled so these pins can
        // be used.
        // TODO: change this to whichever GPIO port you are using.
        //
        SysCtlPeripheralEnable(I2C_SCL_SYSCTL_PERIPH);
        SysCtlPeripheralEnable(I2C_SDA_SYSCTL_PERIPH);
        while(!SysCtlPeripheralReady(I2C_SCL_SYSCTL_PERIPH));
        while(!SysCtlPeripheralReady(I2C_SDA_SYSCTL_PERIPH));
    
        //
        // Configure the pin muxing for I2C0 functions on port B2 and B3.
        // 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(I2C_SCL_CFG);
        GPIOPinConfigure(I2C_SDA_CFG);
    
        //
        // Select the I2C function for these pins.  This function will also
        // configure the GPIO pins pins for I2C operation, setting them to
        // open-drain operation with weak pull-ups.  Consult the data sheet
        // to see which functions are allocated per pin.
        // TODO: change this to select the port/pin you are using.
        //
        GPIOPinTypeI2CSCL(I2C_SCL_GPIO_PORT_BASE, I2C_SCL);
        GPIOPinTypeI2C(I2C_SDA_GPIO_PORT_BASE, I2C_SDA);
    
        //
        // Enable loopback mode.  Loopback mode is a built in feature that is
        // useful for debugging I2C operations.  It internally connects the I2C
        // master and slave terminals, which effectively let's you send data as
        // a master and receive data as a slave.
        // NOTE: For external I2C operation you will need to use external pullups
        // that are stronger than the internal pullups.  Refer to the datasheet for
        // more information.
        //
        I2CLoopbackEnable(I2C_BASE);
    
        //
        // Enable and initialize the I2C0 master module.  Use the system clock for
        // the I2C0 module.  The last parameter sets the I2C data transfer rate.
        // If false the data rate is set to 100kbps and if true the data rate will
        // be set to 400kbps.  For this example we will use a data rate of 100kbps.
        //
    #if defined(TARGET_IS_TM4C129_RA0) ||                                         \
        defined(TARGET_IS_TM4C129_RA1) ||                                         \
        defined(TARGET_IS_TM4C129_RA2)
        I2CMasterInitExpClk(I2C_BASE, ui32SysClock, false);
    #else
        I2CMasterInitExpClk(I2C_BASE, SysCtlClockGet(), false);
    #endif
    
        //
        // Enable the I2C0 slave module. This module is enabled only for testing
        // purposes.  It does not need to be enabled for proper operation of the
        // I2Cx master module.
        //
        I2CSlaveEnable(I2C_BASE);
    
        //
        // Set the slave address to SLAVE_ADDRESS.  In loopback mode, it's an
        // arbitrary 7-bit number (set in a macro above) that is sent to the
        // I2CMasterSlaveAddrSet function.
        //
        I2CSlaveInit(I2C_BASE, SLAVE_ADDRESS);
    
        //
        // Set up the serial console to use for displaying messages.  This is
        // just for this example program and is not needed for I2C operation.
        //
        InitConsole();
    
        //
        // Display the example setup on the console.
        //
        UARTprintf("I2C Loopback Example ->");
        UARTprintf("\n   Module = I2C0");
        UARTprintf("\n   Mode = Single Send/Receive");
        UARTprintf("\n   Rate = 100kbps\n\n");
    
        while(1){
            //
            // Tell the master module what address it will place on the bus when
            // communicating with the slave.  Set the address to SLAVE_ADDRESS
            // (as set in the slave module).  The receive parameter is set to false
            // which indicates the I2C Master is initiating a writes to the slave.  If
            // true, that would indicate that the I2C Master is initiating reads from
            // the slave.
            //
            I2CMasterSlaveAddrSet(I2C_BASE, SLAVE_ADDRESS, false);
    
            //
            // Initalize the data to send.
            //
            pui32DataTx[0] = 'I';
            pui32DataTx[1] = '2';
            pui32DataTx[2] = 'C';
    
            //
            // Initalize the receive buffer.
            //
            for(ui32Index = 0; ui32Index < NUM_I2C_DATA; ui32Index++)
            {
                pui32DataRx[ui32Index] = 0;
            }
    
            //
            // Indicate the direction of the data.
            //
            UARTprintf("Tranferring from: Master -> Slave\n");
    
            //
            // Send 3 peices of I2C data from the master to the slave.
            //
            for(ui32Index = 0; ui32Index < NUM_I2C_DATA; ui32Index++)
            {
                //
                // Display the data that the I2C0 master is transferring.
                //
                UARTprintf("  Sending: '%c'  . . .  ", pui32DataTx[ui32Index]);
    
                //
                // Place the data to be sent in the data register
                //
                I2CMasterDataPut(I2C_BASE, pui32DataTx[ui32Index]);
    
                //
                // Initiate send of data from the master.  Since the loopback
                // mode is enabled, the master and slave units are connected
                // allowing us to receive the same data that we sent out.
                //
                I2CMasterControl(I2C_BASE, I2C_MASTER_CMD_SINGLE_SEND);
    
                //
                // Wait until the slave has received and acknowledged the data.
                //
                while(!(I2CSlaveStatus(I2C_BASE) & I2C_SLAVE_ACT_RREQ))
                {
                }
    
                //
                // Read the data from the slave.
                //
                pui32DataRx[ui32Index] = I2CSlaveDataGet(I2C_BASE);
    
                //
                // Wait until master module is done transferring.
                //
                while(I2CMasterBusy(I2C_BASE))
                {
                }
    
                //
                // Display the data that the slave has received.
                //
                UARTprintf("Received: '%c'\n", pui32DataRx[ui32Index]);
            }
    
            //
            // Reset receive buffer.
            //
            for(ui32Index = 0; ui32Index < NUM_I2C_DATA; ui32Index++)
            {
                pui32DataRx[ui32Index] = 0;
            }
    
            //
            // Indicate the direction of the data.
            //
            UARTprintf("\n\nTranferring from: Slave -> Master\n");
    
            //
            // Modifiy the data direction to true, so that seeing the address will
            // indicate that the I2C Master is initiating a read from the slave.
            //
            I2CMasterSlaveAddrSet(I2C_BASE, SLAVE_ADDRESS, true);
    
            for(ui32Index = 0; ui32Index < NUM_I2C_DATA; ui32Index++)
            {
                //
                // Display the data that I2C0 slave module is transferring.
                //
                UARTprintf("  Sending: '%c'  . . .  ", pui32DataTx[ui32Index]);
    
                //
                // Tell the master to read data.
                //
                I2CMasterControl(I2C_BASE, I2C_MASTER_CMD_SINGLE_RECEIVE);
    
    
                //
                // Wait until the slave is done sending data.
                //
                while(!(I2CSlaveStatus(I2C_BASE) & I2C_SLAVE_ACT_TREQ))
                {
                }
    
                //
                // Place the data to be sent in the data register
                //
                I2CSlaveDataPut(I2C_BASE, pui32DataTx[ui32Index]);
    
                while(I2CMasterBusy(I2C_BASE))
                    ;
    
                //
                // Read the data from the master.
                //
                pui32DataRx[ui32Index] = I2CMasterDataGet(I2C_BASE);
    
                //
                // Display the data that the slave has received.
                //
                UARTprintf("Received: '%c'\n", pui32DataRx[ui32Index]);
            }
    
    
            //
            // Tell the user that the test is done.
            //
            UARTprintf("\nDone.\n\n");
    
            SysCtlDelay(ui32SysClock/6);
        }
    
        //
        // Return no errors
        //
        return(0);
    }
    

    This is the full code. I have also added the reset to the peripheral to allow for me to run it in debug mode with initial conditions. Otherwise, the state remains in debug mode. I have optimizations off. 

  • Ok adding an additional I2CSlaveDataPut after the send loop allows the clearing of these bits and the loop resumes running fully. Can you please explain the interaction between the master and slave module in this case.

    This seems to be some sort of interaction between the dummy receive the SlaveDataPut and the following Receive. Since in the dummy command the Master sends to the Slave the address and the wait is specifically meant to wait for the slave to extend the clock period to respond and only does so when the I2CSlaveData is put. Does that finish that transaction? Or does the following SINGLE_RECEIVE get that transaction? 

  • Hi,

      Looks like you got it working. I2CSlaveDataPut() merely put the data into the data register. It is the I2CMasterControl() that starts either the read or write transaction. 

  • Yes, exactly but if in the send loop I have both the SlaveDataPut and then the Master Single Receive that does the addressing and then clocks for data read why is a second put clearing my error. Given that the wait command is also waiting for the TREQ to be 1 meaning its extending the clock for the response until the data is put in the DR. Should the put of data come after the wait for TREQ ? And then maybe a wait for bus busy ?

  • Modified code for no dummy and running without additional DataPut at the end of the loop

    //*****************************************************************************
    //
    // master_slave_loopback.c - Example demonstrating a simple I2C message
    //                           transmission and reception.
    //
    // Copyright (c) 2010-2020 Texas Instruments Incorporated.  All rights reserved.
    // Software License Agreement
    // 
    //   Redistribution and use in source and binary forms, with or without
    //   modification, are permitted provided that the following conditions
    //   are met:
    //
    //   Redistributions of source code must retain the above copyright
    //   notice, this list of conditions and the following disclaimer.
    //
    //   Redistributions in binary form must reproduce the above copyright
    //   notice, this list of conditions and the following disclaimer in the
    //   documentation and/or other materials provided with the
    //   distribution.
    //
    //   Neither the name of Texas Instruments Incorporated nor the names of
    //   its contributors may be used to endorse or promote products derived
    //   from this software without specific prior written permission.
    // 
    // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
    // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
    // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
    // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
    // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
    // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
    // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
    // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
    // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
    // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
    // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
    // 
    // This is part of revision 2.2.0.295 of the Tiva Firmware Development Package.
    //
    //*****************************************************************************
    
    #include <stdbool.h>
    #include <stdint.h>
    #include "inc/hw_i2c.h"
    #include "inc/hw_memmap.h"
    #include "inc/hw_types.h"
    #include "driverlib/gpio.h"
    #include "driverlib/i2c.h"
    #include "driverlib/pin_map.h"
    #include "driverlib/sysctl.h"
    #include "driverlib/uart.h"
    #include "utils/uartstdio.h"
    
    //*****************************************************************************
    //
    //! \addtogroup i2c_examples_list
    //! <h1>I2C Master Loopback (i2c_master_slave_loopback)</h1>
    //!
    //! This example shows how to configure the I2C0 module for loopback mode.
    //! This includes setting up the master and slave module.  Loopback mode
    //! internally connects the master and slave data and clock lines together.
    //! The address of the slave module is set in order to read data from the
    //! master.  Then the data is checked to make sure the received data matches
    //! the data that was transmitted.  This example uses a polling method for
    //! sending and receiving data.
    //!
    //! This example uses the following peripherals and I/O signals.  You must
    //! review these and change as needed for your own board:
    //! - I2C0 peripheral
    //! - GPIO Port B peripheral (for I2C0 pins)
    //! - I2C0SCL - PB2
    //! - I2C0SDA - PB3
    //!
    //! The following UART signals are configured only for displaying console
    //! messages for this example.  These are not required for operation of I2C.
    //! - UART0 peripheral
    //! - GPIO Port A peripheral (for UART0 pins)
    //! - UART0RX - PA0
    //! - UART0TX - PA1
    //!
    //! This example uses the following interrupt handlers.  To use this example
    //! in your own application you must add these interrupt handlers to your
    //! vector table.
    //! - None.
    //
    //*****************************************************************************
    
    //*****************************************************************************
    //
    // Number of I2C data packets to send.
    //
    //*****************************************************************************
    #define NUM_I2C_DATA 3
    
    //*****************************************************************************
    //
    // Set the address for slave module. This is a 7-bit address sent in the
    // following format:
    //                      [A6:A5:A4:A3:A2:A1:A0:RS]
    //
    // A zero in the "RS" position of the first byte means that the master
    // transmits (sends) data to the selected slave, and a one in this position
    // means that the master receives data from the slave.
    //
    //*****************************************************************************
    #define SLAVE_ADDRESS 0x3C
    
    #define I2C_BASE                I2C1_BASE
    #define I2C_SYSCTL              SYSCTL_PERIPH_I2C1
    
    #define I2C_SCL_SYSCTL_PERIPH   SYSCTL_PERIPH_GPIOG
    #define I2C_SCL_GPIO_PORT_BASE  GPIO_PORTG_BASE
    #define I2C_SCL                 GPIO_PIN_0
    #define I2C_SCL_CFG             GPIO_PG0_I2C1SCL
    
    #define I2C_SDA_SYSCTL_PERIPH   SYSCTL_PERIPH_GPIOG
    #define I2C_SDA_GPIO_PORT_BASE  GPIO_PORTG_BASE
    #define I2C_SDA                 GPIO_PIN_1
    #define I2C_SDA_CFG             GPIO_PG1_I2C1SDA
    
    //*****************************************************************************
    //
    // 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);
    }
    
    //*****************************************************************************
    //
    // Configure the I2C0 master and slave and connect them using loopback mode.
    //
    //*****************************************************************************
    int
    main(void)
    {
    #if defined(TARGET_IS_TM4C129_RA0) ||                                         \
        defined(TARGET_IS_TM4C129_RA1) ||                                         \
        defined(TARGET_IS_TM4C129_RA2)
        uint32_t ui32SysClock;
    #endif
        uint32_t pui32DataTx[NUM_I2C_DATA];
        uint32_t pui32DataRx[NUM_I2C_DATA];
        uint32_t ui32Index;
    
        //
        // Set the clocking to run directly from the external crystal/oscillator.
        // TODO: The SYSCTL_XTAL_ value must be changed to match the value of the
        // crystal on your board.
        //
    #if defined(TARGET_IS_TM4C129_RA0) ||                                         \
        defined(TARGET_IS_TM4C129_RA1) ||                                         \
        defined(TARGET_IS_TM4C129_RA2)
        ui32SysClock = SysCtlClockFreqSet((SYSCTL_XTAL_25MHZ |
                                           SYSCTL_OSC_MAIN |
                                           SYSCTL_USE_OSC), 25000000);
    #else
        SysCtlClockSet(SYSCTL_SYSDIV_1 | SYSCTL_USE_OSC | SYSCTL_OSC_MAIN |
                       SYSCTL_XTAL_16MHZ);
    #endif
    
        //
        // The I2C0 peripheral must be enabled before use.
        //
        SysCtlPeripheralEnable(I2C_SYSCTL);
        SysCtlPeripheralReset(I2C_SYSCTL);
    
        //
        // For this example I2C0 is used with PortB[3:2].  The actual port and
        // pins used may be different on your part, consult the data sheet for
        // more information.  GPIO port B needs to be enabled so these pins can
        // be used.
        // TODO: change this to whichever GPIO port you are using.
        //
        SysCtlPeripheralEnable(I2C_SCL_SYSCTL_PERIPH);
        SysCtlPeripheralEnable(I2C_SDA_SYSCTL_PERIPH);
        while(!SysCtlPeripheralReady(I2C_SCL_SYSCTL_PERIPH));
        while(!SysCtlPeripheralReady(I2C_SDA_SYSCTL_PERIPH));
    
        //
        // Configure the pin muxing for I2C0 functions on port B2 and B3.
        // 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(I2C_SCL_CFG);
        GPIOPinConfigure(I2C_SDA_CFG);
    
        //
        // Select the I2C function for these pins.  This function will also
        // configure the GPIO pins pins for I2C operation, setting them to
        // open-drain operation with weak pull-ups.  Consult the data sheet
        // to see which functions are allocated per pin.
        // TODO: change this to select the port/pin you are using.
        //
        GPIOPinTypeI2CSCL(I2C_SCL_GPIO_PORT_BASE, I2C_SCL);
        GPIOPinTypeI2C(I2C_SDA_GPIO_PORT_BASE, I2C_SDA);
    
        //
        // Enable loopback mode.  Loopback mode is a built in feature that is
        // useful for debugging I2C operations.  It internally connects the I2C
        // master and slave terminals, which effectively let's you send data as
        // a master and receive data as a slave.
        // NOTE: For external I2C operation you will need to use external pullups
        // that are stronger than the internal pullups.  Refer to the datasheet for
        // more information.
        //
        I2CLoopbackEnable(I2C_BASE);
    
        //
        // Enable and initialize the I2C0 master module.  Use the system clock for
        // the I2C0 module.  The last parameter sets the I2C data transfer rate.
        // If false the data rate is set to 100kbps and if true the data rate will
        // be set to 400kbps.  For this example we will use a data rate of 100kbps.
        //
    #if defined(TARGET_IS_TM4C129_RA0) ||                                         \
        defined(TARGET_IS_TM4C129_RA1) ||                                         \
        defined(TARGET_IS_TM4C129_RA2)
        I2CMasterInitExpClk(I2C_BASE, ui32SysClock, false);
    #else
        I2CMasterInitExpClk(I2C_BASE, SysCtlClockGet(), false);
    #endif
    
        //
        // Enable the I2C0 slave module. This module is enabled only for testing
        // purposes.  It does not need to be enabled for proper operation of the
        // I2Cx master module.
        //
        I2CSlaveEnable(I2C_BASE);
    
        //
        // Set the slave address to SLAVE_ADDRESS.  In loopback mode, it's an
        // arbitrary 7-bit number (set in a macro above) that is sent to the
        // I2CMasterSlaveAddrSet function.
        //
        I2CSlaveInit(I2C_BASE, SLAVE_ADDRESS);
    
        //
        // Set up the serial console to use for displaying messages.  This is
        // just for this example program and is not needed for I2C operation.
        //
        InitConsole();
    
        //
        // Display the example setup on the console.
        //
        UARTprintf("I2C Loopback Example ->");
        UARTprintf("\n   Module = I2C0");
        UARTprintf("\n   Mode = Single Send/Receive");
        UARTprintf("\n   Rate = 100kbps\n\n");
    
        while(1){
            //
            // Tell the master module what address it will place on the bus when
            // communicating with the slave.  Set the address to SLAVE_ADDRESS
            // (as set in the slave module).  The receive parameter is set to false
            // which indicates the I2C Master is initiating a writes to the slave.  If
            // true, that would indicate that the I2C Master is initiating reads from
            // the slave.
            //
            I2CMasterSlaveAddrSet(I2C_BASE, SLAVE_ADDRESS, false);
    
            //
            // Initalize the data to send.
            //
            pui32DataTx[0] = 'I';
            pui32DataTx[1] = '2';
            pui32DataTx[2] = 'C';
    
            //
            // Initalize the receive buffer.
            //
            for(ui32Index = 0; ui32Index < NUM_I2C_DATA; ui32Index++)
            {
                pui32DataRx[ui32Index] = 0;
            }
    
            //
            // Indicate the direction of the data.
            //
            UARTprintf("Tranferring from: Master -> Slave\n");
    
            //
            // Send 3 peices of I2C data from the master to the slave.
            //
            for(ui32Index = 0; ui32Index < NUM_I2C_DATA; ui32Index++)
            {
                //
                // Display the data that the I2C0 master is transferring.
                //
                UARTprintf("  Sending: '%c'  . . .  ", pui32DataTx[ui32Index]);
    
                //
                // Place the data to be sent in the data register
                //
                I2CMasterDataPut(I2C_BASE, pui32DataTx[ui32Index]);
    
                //
                // Initiate send of data from the master.  Since the loopback
                // mode is enabled, the master and slave units are connected
                // allowing us to receive the same data that we sent out.
                //
                I2CMasterControl(I2C_BASE, I2C_MASTER_CMD_SINGLE_SEND);
    
                //
                // Wait until the slave has received and acknowledged the data.
                //
                while(!(I2CSlaveStatus(I2C_BASE) & I2C_SLAVE_ACT_RREQ))
                {
                }
    
                //
                // Read the data from the slave.
                //
                pui32DataRx[ui32Index] = I2CSlaveDataGet(I2C_BASE);
    
                //
                // Wait until master module is done transferring.
                //
                while(I2CMasterBusy(I2C_BASE))
                {
                }
    
                //
                // Display the data that the slave has received.
                //
                UARTprintf("Received: '%c'\n", pui32DataRx[ui32Index]);
            }
    
            //
            // Reset receive buffer.
            //
            for(ui32Index = 0; ui32Index < NUM_I2C_DATA; ui32Index++)
            {
                pui32DataRx[ui32Index] = 0;
            }
    
            //
            // Indicate the direction of the data.
            //
            UARTprintf("\n\nTranferring from: Slave -> Master\n");
    
            //
            // Modifiy the data direction to true, so that seeing the address will
            // indicate that the I2C Master is initiating a read from the slave.
            //
            I2CMasterSlaveAddrSet(I2C_BASE, SLAVE_ADDRESS, true);
    
            for(ui32Index = 0; ui32Index < NUM_I2C_DATA; ui32Index++)
            {
                //
                // Display the data that I2C0 slave module is transferring.
                //
                UARTprintf("  Sending: '%c'  . . .  ", pui32DataTx[ui32Index]);
    
                //
                // Tell the master to read data.
                //
                I2CMasterControl(I2C_BASE, I2C_MASTER_CMD_SINGLE_RECEIVE);
    
    
                //
                // Wait until the slave is done sending data.
                //
                while(!(I2CSlaveStatus(I2C_BASE) & I2C_SLAVE_ACT_TREQ))
                {
                }
    
                //
                // Place the data to be sent in the data register
                //
                I2CSlaveDataPut(I2C_BASE, pui32DataTx[ui32Index]);
    
                while(I2CMasterBusy(I2C_BASE))
                    ;
    
                //
                // Read the data from the master.
                //
                pui32DataRx[ui32Index] = I2CMasterDataGet(I2C_BASE);
    
                //
                // Display the data that the slave has received.
                //
                UARTprintf("Received: '%c'\n", pui32DataRx[ui32Index]);
            }
    
    
            //
            // Tell the user that the test is done.
            //
            UARTprintf("\nDone.\n\n");
    
            SysCtlDelay(ui32SysClock/6);
        }
    
        //
        // Return no errors
        //
        return(0);
    }
    

  • Hi Stephen,

      Glad that you resolved the issue. When running your original code I was able to see the code hang on the below line. With your latest code I can see it continue running. I need to understand myself what is causing the slave wait indefinitely . Perhaps there is some timing issue. I will bookmark your reply so I can refer to others for the same question. 

    while(!(I2CSlaveStatus(I2C_BASE) & I2C_SLAVE_ACT_RREQ));