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.

I2C polling method

Other Parts Discussed in Thread: MSP430F5335, BQ40Z50

Hello everyone,

 I am using MSP430F5335 to communicate with battery bq40z50 through i2c communication using pollin method. I have implemented my code as follows

uint8_t data = 0x09;
void i2c_init()
{
    UCB0CTL1 |= UCSWRST;                   // Enable SW reset
    UCB0CTL0 = UCMST + UCMODE_3 + UCSYNC; // I2C Master, synchronous mode
    UCB0CTL1 = UCSSEL_2 + UCSWRST ;            // Use SMCLK, keep SW reset
    UCB0BR0 = 10;                             // fSCL = SMCLK/10 = 100kHz
    UCB0BR1 = 0; 
    UCB0CTL1 &= ~UCSWRST;                     // Clear SW reset, resume operation
}
void i2c_write()
{
    UCB0I2CSA = 0x0b;                         // Slave Address is 0bh
    UCB0CTL1 &= ~UCTXSTP;                     //clear stop condition
    write();
}
void write()
{
   UCB0CTL1 |= UCTR;          // Enable transmit
   UCB0CTL1 |= UCTXSTT;       // i2c start
   
  while(UCB0IFG & UCNACKIFG)    //if nack rx
  {
    UCB0IFG &= ~UCNACKIFG;
    UCB0CTL1 |= UCTXSTP;       //stop condition
  }
  while(!(UCB0IFG & UCB0TXIFG)); //wait for tx begun (buffer ready)
  UCB0TXBUF = data;    
}

in this implementation i always get NACK from the slave. I also checked in the BSP code its working fine, from i concluded there is no hardware problem. in interrupt method also i tested its working fine, but my requirement is polling method only. If i put the delay in before sending byte to Tx buff its working fine. Is it correct way? Also while receiving time NACK set means whether the slave not send proper data or else problem with master.

Thanks in advance

  • Are you sure the slave address is 0x0b? The USCI slave address does not include the R/W bit.

    However, your transmit sequence is wrong. DO it this way:

    Set UCTR and UCTXSTT
    write first byte to TXBUF (yes, right now)
    Wait for UCTXSTT being clear (won't ever happen if TXBUF is empty in transmit mode)
    Check UCNACKIFG
    Proceed with sending more bytes (checking TXIFG) or bail out (with setting TXSTP)

    Also, right after setting TXSTT, nothing has been sent at all, so it is futile to immediately check for NACKIFG (as you do inyour code)

    Another thing: if is pointless to manually clear TXSTP. If this works, you will prevent the proper stop condition to be sent, leaving the bus blocked. If not, then it is superfluous trying it. You should simply wait for it to clear by itself. Maybe with a timeout, in case a slave bocks the bus for some reason.

**Attention** This is a public forum