I am using an MSP430FR5735 Microcontroller to talk to a battery charger IC. The process is simple, send a data register byte for the location that you want to read from, then read one byte.
I call the function charger_read_reg and send the register location that I want to read.
charger_read_reg(0x0C); // read from the charger at the defined address
//*****************************************************************************
// initialize I2C TX
//*****************************************************************************
void init_I2C_TX(void)
{
EUSCI_B_I2C_initMasterParam param = {0};// TX
param.selectClockSource = EUSCI_B_I2C_CLOCKSOURCE_SMCLK;
param.i2cClk = CS_getSMCLK();
param.dataRate = EUSCI_B_I2C_SET_DATA_RATE_100KBPS;
param.byteCounterThreshold = 1;
param.autoSTOPGeneration = EUSCI_B_I2C_NO_AUTO_STOP;
EUSCI_B_I2C_initMaster(EUSCI_B0_BASE, ¶m);
}
//*****************************************************************************
// initialize I2C RX
//*****************************************************************************
void init_I2C_RX(void)
{
EUSCI_B_I2C_initMasterParam param = {0};// TX
param.selectClockSource = EUSCI_B_I2C_CLOCKSOURCE_SMCLK;
param.i2cClk = CS_getSMCLK();
param.dataRate = EUSCI_B_I2C_SET_DATA_RATE_100KBPS;
param.byteCounterThreshold = 1;
param.autoSTOPGeneration = EUSCI_B_I2C_SEND_STOP_AUTOMATICALLY_ON_BYTECOUNT_THRESHOLD;
EUSCI_B_I2C_initMaster(EUSCI_B0_BASE, ¶m);
}
//*******************************************************************************************************************
// Charger read comms
//*******************************************************************************************************************
void charger_read_reg(C_REG_ADDR) // read register routine for the charger
{
init_I2C_TX();
EUSCI_B_I2C_setSlaveAddress(EUSCI_B0_BASE, CHARGER_SLAVE_READ_ADDRESS); // Specify slave address
EUSCI_B_I2C_setMode(EUSCI_B0_BASE, EUSCI_B_I2C_TRANSMIT_MODE); // Set to transmit mode
EUSCI_B_I2C_enable(EUSCI_B0_BASE); // Enable I2C Module to start operations
EUSCI_B_I2C_masterSendSingleByte(EUSCI_B0_BASE, C_REG_ADDR); //Send single byte data.(address of register to read)
while(EUSCI_B_I2C_isBusBusy(EUSCI_B0_BASE))
{
;
}
init_I2C_RX();
EUSCI_B_I2C_setSlaveAddress(EUSCI_B0_BASE, CHARGER_SLAVE_READ_ADDRESS); //Specify slave address
EUSCI_B_I2C_setMode(EUSCI_B0_BASE, EUSCI_B_I2C_RECEIVE_MODE); //Set Master to receive mode
EUSCI_B_I2C_enable(EUSCI_B0_BASE); //Enable I2C Module to start operations
c_0x0C_r = EUSCI_B_I2C_masterReceiveSingleByte(EUSCI_B0_BASE);
}
What's happening is that the register data is sent, a long 1mS delay ( I don't know why there is a delay) then the read address is sent and I never get a NACK or data returned.
I am not sure that I am switching between TX and RX I2C modes correctly. Any Ideas?