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.

Unable to get proper data out from I2C (TM4C123GH6PZ)

Other Parts Discussed in Thread: TM4C123GH6PM

Hi, 

I have tried several times altering the I2C code in many possible ways

but i am still not able to get it to communicate with another tiva board TM4C123GH6PM (slave).

Then i tried receiving the SDA and SCL with an oscilloscope, but the SDA line copies the SCL.

Any idea why this is happening ?

The master code is embedded below.

Thanks in advance.

#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"

// Number of I2C data packets to send.
#define NUM_I2C_DATA 3

#define SLAVE_ADDRESS 0x3C


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);
}



int main()
{

	uint32_t pui32DataRx[NUM_I2C_DATA];
	uint32_t reverse[NUM_I2C_DATA];
	uint32_t ui32Index, i;
	SysCtlClockSet(SYSCTL_SYSDIV_1 | SYSCTL_USE_OSC | SYSCTL_OSC_MAIN |
                   SYSCTL_XTAL_16MHZ);

	SysCtlPeripheralEnable(SYSCTL_PERIPH_I2C0);

	SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOB);

	GPIOPinConfigure(GPIO_PB2_I2C0SCL);
    GPIOPinConfigure(GPIO_PB3_I2C0SDA);

	GPIOPinTypeI2CSCL(GPIO_PORTB_BASE, GPIO_PIN_2);
    GPIOPinTypeI2C(GPIO_PORTB_BASE, GPIO_PIN_3);

	//I2CSlaveInitExpClk(I2C0_BASE, SysCtlClockGet(), false);

	//I2CMasterInitExpClk(I2C0_BASE, SysCtlClockGet(), false);

	I2CSlaveEnable(I2C0_BASE);

	I2CSlaveInit(I2C0_BASE, SLAVE_ADDRESS);

	InitConsole();



while(1)
{
	UARTprintf("1\n");
	//while(!(I2CSlaveStatus(I2C0_BASE) & I2C_SLAVE_ACT_RREQ))
      // ;

	for(ui32Index = 0; ui32Index < NUM_I2C_DATA; ui32Index++)
	{
		UARTprintf("3\n");
		while(!(I2CSlaveStatus(I2C0_BASE)))
		{
			SysCtlDelay(1000000);

				UARTprintf(".");

		}
		pui32DataRx[ui32Index] = I2CSlaveDataGet(I2C0_BASE);

		UARTprintf("Received: '%c'\n", pui32DataRx[ui32Index]);
	}

	//	reverse
	for(i = 0; i < NUM_I2C_DATA; i++)
		reverse[i] = pui32DataRx[NUM_I2C_DATA - i];

	UARTprintf("Reversed String: %s\n ", reverse);

	UARTprintf("5\n");

	//	dummy receive
	//I2CMasterControl(I2C0_BASE, I2C_MASTER_CMD_SINGLE_RECEIVE);

	//UARTprintf("2\n");
	while(!(I2CSlaveStatus(I2C0_BASE) & I2C_SLAVE_ACT_TREQ))
		;
	UARTprintf("6\n");

	for(ui32Index = 0; ui32Index < NUM_I2C_DATA; ui32Index++)
    {
		UARTprintf("4\n");
		UARTprintf("  Sending: '%c'  . . .  ", reverse[ui32Index]);

		I2CSlaveDataPut(I2C0_BASE, reverse[ui32Index]);

		while(!(I2CSlaveStatus(I2C0_BASE)))
		;
	}
}
}