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.

EK-TM4C1294XL: I2C Master and Slave Communication Issues

Part Number: EK-TM4C1294XL

Hi,

I am Having trouble with connecting my two launchpads together over I2C. I am attempting to pass 3 Bytes from the master to the slave and then have the slave output them to a terminal.

My issue, is the Slave device is requiring two packets to be sent before outputting anything to the terminal, however the output is a combination of the two packets. For example, When I send the values 10, 20, 30 in one packet and 40, 50, 60 in another packet. The slave device will print out 20,30,50, (Ignoring the first byte of each packet).

Here is the code for the initialisation of my I2C Slave device and the ISR:

#define BUFFER_SIZE 3 // Size of the I2C receive buffer

#define I2C_SLAVE_BASE I2C0_BASE
#define I2C_SLAVE_GPIO GPIO_PORTB_BASE
#define I2C_SLAVE_SDA GPIO_PIN_3
#define I2C_SLAVE_SCL GPIO_PIN_2

uint8_t i2c_rx_buffer[BUFFER_SIZE];
volatile uint8_t i2c_rx_buffer_index = 0;
volatile bool i2c_buffer_full = false;

void I2C0_IRQHandler(void); // ISR
void init_I2C()
{
// Enable the I2C0 peripheral
SysCtlPeripheralEnable(SYSCTL_PERIPH_I2C0);

// Enable the GPIOB peripheral
SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOB);

// Configure PB2 and PB3 as I2C pins
GPIOPinConfigure(GPIO_PB2_I2C0SCL);
GPIOPinConfigure(GPIO_PB3_I2C0SDA);
GPIOPinTypeI2CSCL(I2C_SLAVE_GPIO, I2C_SLAVE_SCL);
GPIOPinTypeI2C(I2C_SLAVE_GPIO, I2C_SLAVE_SDA);

// Enable the I2C0 interrupt
IntEnable(INT_I2C0);

// Enable I2C slave receive interrupt
//I2CSlaveIntEnableEx(I2C0_BASE, I2C_SLAVE_INT_DATA); // Interrupt when slave receives data

// Initialize the I2C slave
I2CSlaveInit(I2C0_BASE, 0x3C); // Slave address is 0x3C
I2CSlaveAddressSet(I2C0_BASE, 0, 0x3C);

I2CSlaveEnable(I2C0_BASE); // Enable the I2C slave

I2CSlaveIntEnable(I2C0_BASE); // Enable the I2C slave interrupt
//Set the interrupt vector
IntRegister(INT_I2C0, I2C0_IRQHandler);

// Clear the receive buffer
memset(i2c_rx_buffer, 0x00, BUFFER_SIZE);
}

// I2C0 interrupt service routine

void I2C0_IRQHandler(void)
{
uart_puts("I2C0_IRQHandler");

uint32_t status = I2CSlaveStatus(I2C_SLAVE_BASE); // Get the I2C interrupt status
// Get INT status

//If start condition interrupt occurred

// Check if data received interrupt occurred
if (status & (I2C_SLAVE_ACT_RREQ | I2C_SLAVE_ACT_RREQ_FBR)) // Check if data received
{
i2c_rx_buffer[i2c_rx_buffer_index] = I2CSlaveDataGet(I2C_SLAVE_BASE); // Get data from I2C data register
if (i2c_rx_buffer_index == BUFFER_SIZE - 1)
{
i2c_buffer_full = true;
i2c_rx_buffer_index = 0; // Reset the buffer index
}
else
{
i2c_rx_buffer_index++;
}
}

I2CSlaveIntClearEx(I2C_SLAVE_BASE, status); // Clear the I2C interrupt status
}


Here is the write function from my master:

void I2C_MasterWrite(uint8_t slaveAddress, uint8_t *data, uint8_t len)
{
I2CMasterSlaveAddrSet(I2C_MASTER_BASE, slaveAddress, false); // false = write mode
I2CMasterDataPut(I2C_MASTER_BASE, data[0]); // Place the first byte to be sent in the data register
I2CMasterControl(I2C_MASTER_BASE, I2C_MASTER_CMD_BURST_SEND_START); // Start condition for the first byte
while(I2CMasterBusy(I2C_MASTER_BASE)); // Wait until the current byte has been transferred
//__delay_cycles(1000); // Delay to allow the slave to process the data
for (uint8_t i = 1; i < len; i++)
{

if(i == len - 1)
{
I2CMasterDataPut(I2C_MASTER_BASE, data[i]); // Send the current byte
I2CMasterControl(I2C_MASTER_BASE, I2C_MASTER_CMD_BURST_SEND_FINISH); // Finish condition
while (I2CMasterBusy(I2C_MASTER_BASE)); // Wait until the current byte has been transferred
//__delay_cycles(1000); // Delay to allow the slave to process the data
}
else
{
I2CMasterDataPut(I2C_MASTER_BASE, data[i]); // Send the current byte
I2CMasterControl(I2C_MASTER_BASE, I2C_MASTER_CMD_BURST_SEND_CONT); // Continue condition
while (I2CMasterBusy(I2C_MASTER_BASE)); // Wait until the current byte has been transferred
//__delay_cycles(1000); // Delay to allow the slave to process the data
}
}
}


Let me know if you require anything else.

Thank you