Other Parts Discussed in Thread: MSP430WARE, MSPWARE, BQ27545-G1
Hi
Even i have the same configuration as in linked post -( Dipin's Post )
configuration - MSP430F5340 configured for I2C to communicate with BQ27545 fuel gauge IC.
issue - I2C timeout happens either regularly or while after MSP powerup. highlighted the code part where i get stuck.
below is the code :
#define LOOP_COUNT 10000
void BatteryFuelGuageI2CInit(void)
{
P4SEL |= 0x06; // Assign I2C pins to USCI_B1
UCB1CTL1 |= UCSWRST; // Enable SW reset
UCB1CTL0 = UCMST + UCMODE_3 + UCSYNC; // I2C Master, synchronous mode
UCB1CTL1 = UCSSEL_2 + UCSWRST; // Use SMCLK, keep SW reset
UCB1BR0 = 30; // fSCL = SMCLK/30 = ~33.33kHz
UCB1BR1 = 0;
}
uint16_t BatteryFuelGuageI2CMasterReceive(uint8_t ucSlaveAddress, uint8_t ucCmd, uint8_t ucDataLength)
{
uint8_t ucRxdata[2]={};
// set Slave Address as 0x55
UCB1I2CSA = ucSlaveAddress;
// Clear SW reset, resume operation
UCB1CTL1 &= ~UCSWRST;
//I2C time out flag
g_I2Ctimeoutflag = true;
uint16_t uloopcount = 0;
for (uloopcount = 0; uloopcount < LOOP_COUNT; uloopcount++)
{
if ((UCB1CTL1 & UCTXSTP) == 0)
{
break;
}
}
if (uloopcount == LOOP_COUNT)
{
return 0;
}
// Transmitter, Send start condition
UCB1CTL1 |= UCTR + UCTXSTT;
/* Send command to FuelGuage */
for (uloopcount = 0; uloopcount < LOOP_COUNT; uloopcount++)
{
if (UCB1IFG & UCTXIFG)
{
break;
}
}
if (g_uloopcount == LOOP_COUNT)
{
return 0;
}
// sending cmd
UCB1TXBUF = ucCmd;
// wait until cmd got sent
for (uloopcount = 0; uloopcount < LOOP_COUNT; uloopcount++)
{
if (UCB1IFG & UCTXIFG)
{
break;
}
}
if (g_uloopcount == LOOP_COUNT)
{
return 0;
}
////////////////////////////////////////////////////////////////////////////
// Set master in Receive mode, Send start condition
UCB1CTL1 &= ~UCTR;
UCB1CTL1 |= UCTXSTT; // Send repeated start condition
unsigned int uiCounter = 0;
unsigned int uiRxCounter = 2;
while (uiRxCounter > 0)
{
for (uloopcount = 0; uloopcount < LOOP_COUNT; uloopcount++)
{
if (UCB1IFG & UCRXIFG)
{
break;
}
}
if (uloopcount == LOOP_COUNT)
{
return 0;
}
ucRxdata[uiCounter] = UCB1RXBUF; // Receive data from fuel gauge
if(uiCounter == 0)
{
//Send stop condition.
UCB1CTL1 |= UCTXSTP;
}
uiCounter++;
uiRxCounter--;
}
UCB1IFG &= ~UCRXIFG;
/* Clear USCI_B1 TX int flag */
UCB1IFG &= ~UCTXIFG;
g_uBatteryInfo = (uint16_t) ucRxdata[0] << 8 | ucRxdata[1]; //Transmit received data byte by byte
g_I2Ctimeoutflag = false;
return g_uBatteryInfo;
}
need your suggestions to resolve this issue.
Thanks
Rekha