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.

Problem implementing I2C on DK-TM4C129X

Other Parts Discussed in Thread: BQ27510

I'm trying to communicate with the FuelTank BoosterPack, which uses I2C communication. It's actually the TI bq27510 fuel gauge chip on the board that handles all the data. The datasheet for the part explains all the commands, and also the I2C addresses for write and read.

I've written a simple program, just to try and get communication working, but when I watch the line with my logic analyzer, I only see one byte sent both times I2CMasterControl is called. I feel like I'm missing something but I don't understand what. Any and all help is much appreciated!

/*
 * main.c
 */
#include <stdint.h>
#include <stdbool.h>
#include "utils/ustdlib.h"
#include "inc/hw_memmap.h"
#include "inc/hw_types.h"
#include "inc/hw_gpio.h"
#include "driverlib/interrupt.h"
#include "driverlib/sysctl.h"
#include "driverlib/pin_map.h"
#include "driverlib/debug.h"
#include "driverlib/i2c.h"
#include "driverlib/gpio.h"
#include "driverlib/rom.h"
#include "driverlib/rom_map.h"

#define WRITEADDR 	0xAA
#define READADDR	0xAB
#define GETTEMP		0
#define GETVOLT		1
#define	GETREMCAP	2
#define TIME2EMPTY	3

volatile uint32_t mstrClk;
uint16_t I2COperation(uint16_t oper);

void I2Csetup(void)
{
	SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOG);
	SysCtlPeripheralEnable(SYSCTL_PERIPH_I2C3);

	GPIOPinTypeI2CSCL(GPIO_PORTG_BASE, GPIO_PIN_4);
	GPIOPinTypeI2C(GPIO_PORTG_BASE, GPIO_PIN_5);

	GPIOPinConfigure(GPIO_PG4_I2C3SCL); // not sure if these need to be called also
	GPIOPinConfigure(GPIO_PG5_I2C3SDA);

	I2CMasterInitExpClk(I2C3_BASE, mstrClk, false); // set up I2C for master, at 100KHz
}

uint16_t I2COperation(volatile uint16_t oper)
{
	volatile uint16_t returndata;

	switch(oper) {
	case 0:

		// get temp value from bq27510 (0x06)
		I2CMasterSlaveAddrSet(I2C3_BASE, WRITEADDR, false);
		I2CMasterDataPut(I2C3_BASE, 0x06);
		I2CMasterControl(I2C3_BASE, I2C_MASTER_CMD_SINGLE_SEND);
		while(I2CMasterBusBusy(I2C3_BASE));
		I2CMasterSlaveAddrSet(I2C3_BASE, READADDR, true);
		I2CMasterControl(I2C3_BASE, I2C_MASTER_CMD_SINGLE_RECEIVE);
		while(I2CMasterBusBusy(I2C3_BASE));
		returndata = I2CMasterDataGet(I2C3_BASE);

		return returndata;

	case 1:

		I2CMasterSlaveAddrSet(I2C3_BASE, WRITEADDR, false);
		I2CMasterDataPut(I2C3_BASE, 0x08);
		I2CMasterControl(I2C3_BASE, I2C_MASTER_CMD_SINGLE_SEND);
		while(I2CMasterBusBusy(I2C3_BASE));
		I2CMasterSlaveAddrSet(I2C3_BASE, READADDR, true);
		I2CMasterControl(I2C3_BASE, I2C_MASTER_CMD_SINGLE_RECEIVE);
		returndata = I2CMasterDataGet(I2C3_BASE);

		return returndata;

	case 2:
		// get remaining capacity from bq27510 (0x10)
		break;

	case 3:
		// get time to empty (0x16)
	break;
	
	default:
	return 0x00;

	}
	return 0x00;
}

void main(void) {

	volatile uint16_t test = 0x00;

	mstrClk = SysCtlClockFreqSet(SYSCTL_OSC_INT|SYSCTL_USE_PLL|SYSCTL_CFG_VCO_480, 120000000);
	SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOQ);
	SysCtlPeripheralEnable(SYSCTL_PERIPH_GPION);
	GPIOPinTypeGPIOOutput(GPIO_PORTQ_BASE, GPIO_PIN_7 | GPIO_PIN_4);
	GPIOPinTypeGPIOOutput(GPIO_PORTN_BASE, GPIO_PIN_5);
	GPIOPinWrite(GPIO_PORTQ_BASE, GPIO_PIN_4|GPIO_PIN_7, 0x00);
	GPIOPinWrite(GPIO_PORTN_BASE, GPIO_PIN_5, 0x00);

	I2Csetup();

	test = I2COperation(0);

	if (test != 0)
	{
		// turn on all (white) LEDs
		GPIOPinWrite(GPIO_PORTQ_BASE, GPIO_PIN_4|GPIO_PIN_7, 0x90);
		GPIOPinWrite(GPIO_PORTN_BASE, GPIO_PIN_5, 0x20);

	}
	else
	{
		GPIOPinWrite(GPIO_PORTN_BASE, GPIO_PIN_5, 0x20); //turn on red LED
	}

	while(1)
	{

	}
}