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.

MSP430g2 i2c - SDA held low

Other Parts Discussed in Thread: MSP430G2553

Hello everyone,

i have a launchpad with MSP430G2553 and two I2C sensors (BMP180 and si7021 - both working as they should, checked with arduino). 

I have connected one sensor at the time, there are 10k pullups on the both SDA and SCK. Sometimes (after changing values to send or an address) is SDA held down by the msp (SCK is high with no impulses). When i swap the sensors, SDA magically goes high and everthing works. When i connect the previous sensor, SDA is low. And then when i swap them is high again. It happens on either of sensors. SDA is then low even when i disconnect the sensor.

But when i disconnect the "working" one, then the disconnected SDA is high.

I hope i explained it clear enough x)

Thank you for any help, used code is below

/*

* I2C LIBRARY FOR MSP430F2132 WITHOUT INTERRUPT
* created by Petus (c) 2013
*
* please, visit the website http://petus.cz
*
*/

#include "msp430g2553.h";

//---------I2C INICIALIZATION----------------------
void i2c_init(unsigned int slave_address)
{
	P1SEL |= BIT6 + BIT7;                     // set of pins
	P1SEL2 |= BIT6 + BIT7;                     // set of pins
	UCB0CTL1 |= UCSWRST;                      // enable SW reset
	UCB0CTL0 = UCMST + UCMODE_3 + UCSYNC;     // I2C Master
	UCB0CTL1 = UCSSEL_2 + UCSWRST;            // SMCLK, keep SW reset
	UCB0BR0 = 12;                             // fSCL = SMCLK/12 = ~100kHz
	UCB0BR1 = 0;
	UCB0I2CSA = slave_address;                // Set slave address
	UCB0CTL1 &= ~UCSWRST;                     // Clear SW reset, resume operation

}
//----------CHANGE OF ADDRESS--------------
void i2c_adr(unsigned int adr)
{
	UCB0I2CSA = adr;
}

//----------SEND DATA WITH A START BIT------
void i2c_tx(unsigned char data)
{
	while((UCB0STAT & BUSY)!= 0);			         // free bus
	while (UCB0CTL1 & UCTXSTP);			           // stop
	UCB0CTL1 |= UCTR;                          // UCTR=1 => Transmit Mode (R/W bit = 0)
	UCB0CTL1 |= UCTXSTT;                       // start condition generation
	while((IFG2  & UCB0TXIFG ) ==0);
	UCB0TXBUF = data;
	__delay_cycles(200);
}

//----------SEND DATA WITHOUT START BIT--------
void i2c_txx(unsigned char data1)
{
	while((IFG2  & UCB0TXIFG ) ==0);		       // buffer is filled
	UCB0TXBUF = data1;
	__delay_cycles(200);
}

//----------SEND STRING WITHOUT START BIT----------
void i2c_retez(const char *s)
{
	while((UCB0STAT & BUSY)!= 0);			         // free bus
	while(*s)
		i2c_txx(*s++);

}


//-----------READ WITH START BIT-------------
unsigned char i2c_rx(void)
{

	while((UCB0STAT & BUSY)!= 0);			         // free bus
	UCB0CTL1 &= ~UCTR;                         // recive
	UCB0CTL1 |= UCTXSTT;
	while(UCB0CTL1 & UCTXSTT);				         // start
	while((IFG2  & UCB0RXIFG ) ==0);		       // char is complete
	return UCB0RXBUF;
}

//-----------READ WITHOUT START BIT------------
unsigned char i2c_rxx(void)
{

	while((UCB0STAT & BUSY)!= 0);			         // free bus

	while((IFG2  & UCB0RXIFG ) ==0);		       // char is complete
	return UCB0RXBUF;
}

//---------------STOP-------------------------
void i2c_stop(void)
{
	while((UCB0STAT & BUSY)!= 0);
	UCB0CTL1 |= UCTXSTP;
	while(UCB0CTL1 & UCTXSTP);
}

//---------STOP WITH NACK-------------
void i2c_stopNACK(void)
{
	while((UCB0STAT & BUSY)!= 0);
	UCB0CTL1 |= UCTXNACK;
}


int main()
{
	_delay_cycles(1000);

while(1){
	i2c_init(0x40);
	i2c_tx(0xfa);
	i2c_rx();
	
	_delay_cycles(400);
}
}

  • Hello,

    The SDA line is most likely held low because the MSP430 is stuck in a flag check loop that is never clearing. Can you use a debugger tool to check the state of the MSP430 while the issue is present? I also do not notice anywhere inside of your code where you actually send the stop condition even though the functions are defined and available. This is most likely the cause of the SDA line remaining pulled low by the MSP430, getting stuck during [while (UCB0CTL1 & UCTXSTP);] of the i2c_tx function. Logic analyzer or oscilloscope screenshots would also help further debug the issue.

    Regards,
    Ryan

**Attention** This is a public forum