0
votes

I am trying to build communication between a sensor and MSP430F5438a over I2C. Trying to write a code from scratch. Not using any interrupt at this point. Now to read PartID from the sensor I'm following the steps below:

  1. Master issues start condition, sends slave address for write(0b 1010 1110)
  2. Master sends Register address (e.g 0xFF for part id).
  3. Master issues repeated start condition, sends slave address for reading (0b 1010 1111)
  4. Master reads data byte returned from the slave. (Supposed to get back 0x15 which is the PartID).

My issue is I am not getting anything back in my receive buffer. I'm probably doing something wrong in my code. But actually, don't understand where I am doing wrong. Anyone can help in it? My hardware connection is fine I hope (Checked with the sensor provider).

I'm attaching my code below: Wrote it in Code Composer Studio.

#include <msp430.h> 
#include <string.h>

#define MAX30101_I2C_ADDRESS        0x57

void Clock_setup(); // default
void I2C_setup();

int readByte(char register_add);

int ID;

void main()
{
    WDTCTL = WDTPW + WDTHOLD;
    Clock_setup();
    I2C_setup();
    ID = readByte(0xFF);

}

void Clock_setup(){
    P11DIR |= BIT2; // check smclk, 1MHz default
    P11SEL |= BIT2; // check smclk, 1MHz default
    P11DIR |= BIT0; // check aclk, 32.8KHz default
    P11SEL |= BIT0; // check aclk, 32.8KHz default
}

void I2C_setup() {

    P3SEL |= BIT7; // 3.7 UCB1_SDA, 5.4 UCB1_SCL
    P5SEL |= BIT4;
    UCB1CTL1 |= UCSWRST; // reset enable
    UCB1CTL0 = UCMST + UCMODE_3 + UCSYNC; // master + I2C mode + Sync
    UCB1CTL1 = UCSSEL_2 + UCSWRST; //use SMCLK + still reset
    UCB1BR0 = 10; // default SMCLK 1M/10 = 100KHz
    UCB1BR1 = 0; //
    UCB1I2CSA = MAX30101_I2C_ADDRESS; // MAX30101 7 bit address 0x57
    UCB1CTL1 &= ~UCSWRST; // reset clear

}

int readByte(char register_add){
    int rx_byte;

    UCB1CTL1 |= UCTXSTT+ UCTR; // Generating START + I2C transmit (write)
    UCB1I2CSA = 0xAE; // MAX30101 7 bit address 0x57
    UCB1TXBUF = register_add; // sending 0xFF to read the PartID
    while(!(UCB1IFG & UCTXIFG)); //wait until reg address got sent
    while( UCB1CTL1 & UCTXSTT); //wait till START condition is cleared
    UCB1CTL1 |= UCTXSTT; //generate RE-START
    UCB1I2CSA = 0xAF; // MAX30101 7 bit address 0x57
    UCB1CTL1 &=~ UCTR; //receive mode
    while( UCB1CTL1 & UCTXSTT); //wait till START condition is cleared
    rx_byte = UCB1RXBUF; //read byte
    //while(!(UCB1IFG & UCRXIFG)); //wait while the Byte has being read
    UCB1CTL1 |= UCTXNACK; //generate a NACK
    UCB1CTL1 |= UCTXSTP; //generate stop condition
    while(UCB1CTL1 & UCTXSTP); //wait till stop condition got sent

    return rx_byte;
}
1

1 Answers

0
votes

I²C slave addresses have 7 bits; 0xAF or 0xAE cannot be valid. The comments already say that 0x57 is the correct value.

And you must wait for the received byte to be available (UCRXIFG) before trying to read it.

And this code lacks all error handling; you should at least check for NACK.