Quantcast
Channel: MSP low-power microcontroller forum - Recent Threads
Viewing all articles
Browse latest Browse all 22172

MSP430F5529: Check if Slave is Present

$
0
0
Part Number: MSP430F5529
Other Parts Discussed in Thread: MSP430F2619

Hello,

I have an MSP430F5529 on an I2C bus. During operation of my system, some components can be connected or disconnected from this bus. I would like to check if a certain slave is on the bus or not before talking to the slave. I have found a helpful function (TI_USCI_I2C_slave_present), which was referenced in the document slaa382a and has some TI generated source code on GitHub, albeit for a MSP430F2619. I have been trying to adapt this function for the MSP430F5529, but I am struggling to get this code to work. 

Original code for MSP430F2619

unsigned char TI_USCI_I2C_slave_present(unsigned char slave_address){
  unsigned char ie2_bak, slaveadr_bak, ucb0i2cie, returnValue;
  ucb0i2cie = UCB0I2CIE;                      // restore old UCB0I2CIE
  ie2_bak = IE2;                              // store IE2 register
  slaveadr_bak = UCB0I2CSA;                   // store old slave address
  UCB0I2CIE &= ~ UCNACKIE;                    // no NACK interrupt
  UCB0I2CSA = slave_address;                  // set slave address
  IE2 &= ~(UCB0TXIE + UCB0RXIE);              // no RX or TX interrupts
  __disable_interrupt();
  UCB0CTL1 |= UCTR + UCTXSTT + UCTXSTP;       // I2C TX, start condition
  while (UCB0CTL1 & UCTXSTP);                 // wait for STOP condition
  returnValue = !(UCB0STAT & UCNACKIFG);
  __enable_interrupt();
  IE2 = ie2_bak;                              // restore IE2
  UCB0I2CSA = slaveadr_bak;                   // restore old slave address
  UCB0I2CIE = ucb0i2cie;                      // restore old UCB0CTL1
  return returnValue;                         // return whether or not 
                                              // a NACK occured
}

My current attempt for MSP430F5529 

unsigned char TI_USCI_I2C_slave_present(unsigned char slave_address){
  unsigned char slaveadr_bak, ucb0i2cie, returnValue;
  ucb0i2cie = UCB0IE;                      // restore old UCB0I2CIE
  slaveadr_bak = UCB0I2CSA;                   // store old slave address
  UCB0I2CSA = slave_address;                  // set slave address
  UCB0IFG &= ~(UCTXIFG + UCRXIFG);       // Clear any pending interrupts
  UCB0IE &= ~ ( UCNACKIE + UCRXIE + UCTXIE);  // no NACK, RX, or TX interrupts
  __disable_interrupt();
  UCB0CTL1 |= UCTR + UCTXSTT + UCTXSTP;       // I2C TX, start condition
  while (UCB0CTL1 & UCTXSTP);                 // wait for STOP condition
  // !!! Not sure what registers need to be used here for MSP430F5529 !!!
  returnValue = !(UCB0IV & USCI_I2C_UCNACKIFG);
  //returnValue = !(UCB0IFG & UCNACKIFG); //

  __enable_interrupt();
  UCB0I2CSA = slaveadr_bak;                   // restore old slave address
  UCB0IE = ucb0i2cie;                      // restore old UCB0CTL1
  return returnValue;                         // return whether or not
                                              // a NACK occured
}

Running the debugger, I don't seem to be getting desired values for the variable "returnValue", but I am not sure what I am doing wrong. I have a test setup and a logic analyzer. My goal is to verify that an existing slave on the bus exists, then to verify that a non-existent slave does not exist.

Any help is appreciated!

J


Viewing all articles
Browse latest Browse all 22172

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>