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.

MSP430FR5994: Code is working with breakpoints but not working without breakpoints

Part Number: MSP430FR5994


Hi community,

I am trying to set values certain registers of my sensor(I2C slave) through i2c protocol.But the issueis when i run my program with breakpoints everything is well and good but when i try the same without any breakpoints it just sending the values to sensor but there is no response in the sensor. I will share my code below guys.please drop your suggestions

Code:

#include <msp430.h> 
#include <stdio.h>
#include <stdint.h>
#include <stdbool.h>
#include <I2C.h>
//#include <algorithm.h>


/**
 * main.c
 */

#define BMP280_ADDRESS             0X57
#define PART_ID                 0XFF
#define MEAN_FILTER_SIZE        15
//Registers

#define Mode_Register 0x06
#define MAX30100_REG_SPO2_CONFIGURATION 0x07
#define MAX30100_REG_LED_CONFIGURATION  0x09
#define MAX30100_REG_FIFO_WRITE_POINTER 0x02
#define MAX30100_REG_FIFO_READ_POINTER 0x04
#define MAX30100_REG_FIFO_DATA 0x05
#define MAX30100_INTR 0X01

//Modes

#define HR_Mode 0x02                           //HR Mode
#define Spo2_Mode 0x03                        // Spo2 Mode
#define Spo2_config 0x05
#define redLedCurrent  0x02
#define irLedCurrent  0x02
#define deviceaddr 0x57
#define Intr_enable 0x30                    //Enable Spo2 and HR ready
#define ALPHA 0.95  //dc filter alpha value


uint8_t chip_ID=0;

uint8_t mBuffer[];
uint8_t rxData=0;
uint8_t i;
uint8_t nack_received;

uint8_t rxCount;
uint8_t *rxBuffer;

uint8_t txCount;
uint8_t *txBuffer;

//Variables

uint8_t P_LED_PULSE_WIDTH=0;        //Prev values
uint8_t P_SAMPLING_RATE=0;

uint8_t Read_Pointer=1;
uint8_t Write_Pointer=1;


uint8_t i=0;
uint8_t P_Spo_2_config=0;            //Prev values of SPo2_Reg


uint8_t receive_initiated=0;
uint8_t transmit_initiated=0;

uint8_t clock_timeout=0;

uint32_t ir_Array[100];
uint32_t red_Array[100];


uint8_t Rx_Buffer[64];                          // Transfer the values between Receive inteerupt and R_read



uint32_t un_temp;
uint32_t IR;
uint32_t RED;
uint8_t led[4]={0,0,0,0};
uint8_t RxByteCtr;



int32_t n_spo2;  //SPO2 value
int8_t h_spo2_valid;  //indicator to show if the SPO2 calculation is valid
int32_t n_heart_rate; //heart rate value
int8_t  ch_hr_valid;  //indicator to show if the heart rate calculation is valid
uint8_t uch_dummy;


//--------------------------------------------------------------------------------|
//                          Filters                                               |
//--------------------------------------------------------------------------------|
float meanDiffResIR=0;

struct dcFilter_t {
  float w;
  float result;
};

//struct butterworthFilter_t
//{
//  float v[2];
//  float result;
//};

struct meanDiffFilter_t
{
  float values[MEAN_FILTER_SIZE];
  uint8_t index;
  float sum;
  uint8_t count;
};

struct butterworthFilter_t
{
  float v[2];
  float result;
};

struct result1
{
    uint8_t IR;
    uint8_t RED;
};

struct dcFilter_t dcFilterIR = {0,0};

struct dcFilter_t dcFilterRed = {0,0};

struct meanDiffFilter_t meanDiffIR={0,0,0};

struct result1 result1={0,0};

void intialize()
{
    //Configure GPIO
       P1OUT &= ~BIT0;
       P1DIR |=BIT0;

       //--setup ports
       P7SEL1 &= ~BIT1;             //P7.1 = SCL
       P7SEL0 |=  BIT1;

       P7SEL1 &= ~BIT0;             //P7.0 = SDA
       P7SEL0 |= BIT0;

       P7DIR  &= ~(BIT0 | BIT1);

       PM5CTL0 &= ~LOCKLPM5;        //Disable GPIO power-on high impedance mode
       __delay_cycles(50);
       __bis_SR_register(GIE);

}

void i2cConfig()
{
    UCB2CTLW0 = UCSWRST;        // Reset I2C interface for config

    UCB2CTLW0 =                 /* USCI - B2 configuration register */
                    UCMST         | // Master mode
                    UCSYNC        | // Synchronous mode
                    UCMODE_3      | // I2C mode
                    UCSSEL__SMCLK | // Select SMCLK
                    UCTR          | // Transmitter mode
                    UCSWRST       | // Don't release reset (yet)
                    0;

        UCB2CTLW1 =
                    UCCLTO_1      | // Clock low time-out select (28ms)
                    UCASTP_2      | // Automatic STOP condition generation (enabled)
                    UCGLIT_0      | // Deglitch time (50ns)
                    0;

        UCB2BRW = 10;               // Bit clock divider 1M/10 = 100kHz

        UCB2CTLW0 &= ~UCSWRST;      // Clear reset bit to start operation

        UCB2IE = UCNACKIE         | // Enable Nack interrupt
                 UCTXIE           | // Enable TX interrupt
                 UCRXIE           | // Enable RX interrupt
                 UCCLTOIE         | // Enable clock low time-out interrupt
                 0;
}

uint8_t read(uint8_t reg)
{
    i2c_start(BMP280_ADDRESS,WRITE);
    i2c_write(reg);
    i2c_repeated_start(BMP280_ADDRESS,READ);
    i2c_read(led,2);
    chip_ID=led[1];
    return chip_ID;
}

void write(uint8_t reg,uint8_t data)
{
    i2c_start(BMP280_ADDRESS,WRITE);
    i2c_write(reg);
    i2c_write(data);
    STOP_I2C;
}

void set_mode()
{
//        uint8_t temp=read(0x06);
//        temp = temp & 0x00;
//        temp = temp|mode;
//        write(0x06,0x02); // Mode_Reg, HR_MODE
//        i2c_stop();
//        write(0x06,0x00); // Mode_Reg, HR_MODE
//        write(0x06,0x02); // Mode_Reg, HR_MODE
//
    i2c_start(BMP280_ADDRESS,WRITE);
    i2c_write(0x06);
    i2c_write(0x03); // Mode_Reg, HR_MODE
    __delay_cycles(5);
    i2c_stop ();
    chip_ID=read(0x06);
}

void spo2_config()
{
    uint8_t P_Spo_2_config = read(MAX30100_REG_SPO2_CONFIGURATION);
    write(MAX30100_REG_SPO2_CONFIGURATION, (P_Spo_2_config & 0x00) | Spo2_config);
    i2c_stop();
    chip_ID=read(MAX30100_REG_SPO2_CONFIGURATION);
}

void LED_CURRENT()
{
  write(MAX30100_REG_LED_CONFIGURATION, redLedCurrent << 4 | irLedCurrent);
  i2c_stop();
}


uint8_t init_device()
{
    chip_ID=read(0xFF);
    if(chip_ID==17)
    {
        return 0;
    }
    else
    {
        return 1;
    }
}



//-----------------------------------------------------------------------------------------------------------------------------------------------
//
//-------------------------------------------------------------------------------------------------------------------------------------------------


//-----------------------------------------------------------------------------------------------------------------------------------------------------------|
//                                                      FILTERS                                                                                                                                                           |
//-----------------------------------------------------------------------------------------------------------------------------------------------------------|

int main(void)
{
    WDTCTL = WDTPW | WDTHOLD;   // stop watchdog timer
    intialize();
    i2cConfig();
    initI2C();
    while(init_device())
    {
        printf("Couldn't able to establish I2C connection with MAX30100");
    }

    set_mode();
    spo2_config();
    LED_CURRENT();

}

i2c header file

/*
 * i2c.h
 *
 *  Created on: 7 Dec 2020
 *      Author: forat
 */

#ifndef I2C_H_
#define I2C_H_
#include <stdint.h>

#define READ        0
#define WRITE       1

#define STOP_I2C        (UCB2CTLW0 |= UCTXSTP)

void initI2C (void);

void i2c_start (uint8_t,unsigned int);

void i2c_stop (void);

void i2c_repeated_start(uint8_t,unsigned int);

void i2c_write (uint8_t);//write data or reg_address

void i2c_read (uint8_t*,unsigned int);

#endif /* I2C_H_ */

i2c.c

#include <msp430.h>
#include <stdint.h>

#include "i2c.h"




void initI2C()
{
    //config P1.6 SDA P1.7 SCL
    UCB2CTLW0 &=0x00;
    UCB2CTLW1 &=0x00;
    UCB2TBCNT =0x00;

    P1SEL1 &= ~ (BIT6 | BIT7);
    P1SEL0 |= (BIT6 | BIT7);

    UCB2CTLW0 = UCSWRST;                      // Enable SW reset
    UCB2CTLW0 |= UCMODE_3 | UCMST | UCSSEL__SMCLK | UCSYNC; // I2C master mode, SMCLK
    UCB2BRW = 20;//3;//10;                            // fSCL = SMCLK/10 = ~100kHz
    //UCB2I2CSA = SLAVE_ADDR;                   // Slave Address
    UCB2CTLW0 &= ~UCSWRST;                    // Clear SW reset, resume operation
    UCB2IE |= UCNACKIE;//enable NACK ISR (TX and RX?)
    UCB2IE &=  ~UCTXIE;
    UCB2IE &=  ~UCRXIE;
    UCB2IE &=  ~UCCLTOIE;
}

void i2c_start (uint8_t dev_addr,unsigned int RW)
{
    while(UCB2STAT & UCBBUSY);//check if SDA and SCL are idle

    UCB2I2CSA = dev_addr;

    if(RW == READ){UCB2CTLW0 &= ~UCTR;}
    else{UCB2CTLW0 |= UCTR;}

    UCB2CTLW0 |= UCTXSTT;

    while (UCB2CTLW0 & UCTXSTT);//wait till the whole address has been sent and ACK'ed
}

void i2c_stop (void)
{
    UCB2CTLW0 |= UCTXSTP;//stop
    while(UCB2CTLW0 & UCTXSTP);//wait for a stop to happen
}

void i2c_repeated_start(uint8_t dev_addr, unsigned int RW)
{
    UCB2I2CSA = dev_addr;

    if(RW == READ){UCB2CTLW0 &= ~UCTR;}
    else{UCB2CTLW0 |= UCTR;}

    UCB2CTLW0 |= UCTXSTT;

    while (UCB2CTLW0 & UCTXSTT);//wait till the whole address has been sent and ACK'ed
}

void i2c_write (uint8_t data)
{
    UCB2TXBUF = data;
    while(UCB2CTLW0 & UCTXIFG0);//1 means data is sent completely
}

void i2c_read (uint8_t * led,unsigned int RxByteCtr)
{
    int i = 0;
    for(i = 0; i < RxByteCtr;i++)
    {
        while(!(UCB2IFG & UCRXIFG0));//make sure rx buffer got data

        if(i == RxByteCtr - 1)
            STOP_I2C;

        led[i] = UCB2RXBUF;
    }

    while(UCB2CTLW0 & UCTXSTP);//wait for a stop to happen
}



Guys you can leave the filters.i didn't use them.

  • Can you use oscillicope to catch the wave? I think maybe the communication is too fast and the sensor can't response it quickly.

  • Hey Zhou,

    I did tried with delays but still it didn't work.

  • Can you test the wave?It can tell what happens

  • Could you please explain what do u mean by test wave?

  • Where were the breakpoints? A symptom like this suggests a race, where you're doing something before the EUSCI is ready for it.

    This, e.g. stands out:

    UCB2TXBUF = data;
    while(UCB2CTLW0 & UCTXIFG0);//1 means data is sent completely

    which should probably be written

    while(!(UCB2IFG & UCTXIFG0));//1 means data can be sent
    UCB2TXBUF = data;


  • I tried this also, But I am unable to see the values.

    While debugging ( line by line execution) I can see the values. Please help me solve these issues.

    Thanks

  • I don't have your equipment, so all I can do is inspect. 

    One possibility is that you're issuing the repeated-start too early. Try waiting for (the second) UCTXIFG0 before requesting it.

    If you have one, a scope is usually handy in such a case. Also, comparing your sequence with those in TI's Example suite might be useful.

  • Hey Bruce, 

    I have gone through some of your MSP430 Examples and I have change altered my code in good fashioned way. I have attached the code here. But here also i am encountering the same problem. I have attached my new code here please go through it and let me know your suggestions

    //******************************************************************************
    //   MSP430FR599x Demo - eUSCI_B2, I2C Master multiple byte TX/RX
    //
    //   Description: I2C master communicates to I2C slave sending and receiving
    //   3 different messages of different length. I2C master will enter LPM0 mode
    //   while waiting for the messages to be sent/receiving using I2C interrupt.
    //   ACLK = NA, MCLK = SMCLK = DCO 16MHz.
    //
    //                                     /|\ /|\
    //                   MSP430FR5994      4.7k |
    //                 -----------------    |  4.7k
    //            /|\ |             P7.1|---+---|-- I2C Clock (UCB2SCL)
    //             |  |                 |       |
    //             ---|RST          P7.0|-------+-- I2C Data (UCB2SDA)
    //                |                 |
    //                |                 |
    //                |                 |
    //                |                 |
    //                |                 |
    //                |                 |
    //
    //   Nima Eskandari and Ryan Meredith
    //   Texas Instruments Inc.
    //   January 2018
    //   Built with CCS V7.3
    //******************************************************************************
    
    #include <msp430.h> 
    #include <stdint.h>
    #include <stdbool.h>
    
    //******************************************************************************
    // Pin Config ******************************************************************
    //******************************************************************************
    
    #define LED_OUT     P1OUT
    #define LED_DIR     P1DIR
    #define LED0_PIN    BIT0
    #define LED1_PIN    BIT1
    
    //******************************************************************************
    // Example Commands ************************************************************
    //******************************************************************************
    
    #define SLAVE_ADDR  0x53
    
    /* CMD_TYPE_X_SLAVE are example commands the master sends to the slave.
     * The slave will send example SlaveTypeX buffers in response.
     *
     * CMD_TYPE_X_MASTER are example commands the master sends to the slave.
     * The slave will initialize itself to receive MasterTypeX example buffers.
     * */
    
    #define CMD_TYPE_0_SLAVE      0x00
    #define CMD_TYPE_1_SLAVE      1
    #define CMD_TYPE_2_SLAVE      2
    
    #define Power_CTL             0x2D
    #define Data_Rate             0x2C
    #define Data_Format           0x31
    
    #define X0_Reg                0x32
    #define X1_Reg                0x33
    #define Y0_Reg                0x34
    #define Y1_Reg                0x35
    #define Z0_Reg                0x36
    #define Z1_Reg                0x37
    
    
    #define CMD_TYPE_0_MASTER      3
    #define CMD_TYPE_1_MASTER      4
    #define CMD_TYPE_2_MASTER      5
    
    #define TYPE_0_LENGTH   1
    #define TYPE_1_LENGTH   2
    #define TYPE_2_LENGTH   6
    
    #define MAX_BUFFER_SIZE     20
    
    /* MasterTypeX are example buffers initialized in the master, they will be
     * sent by the master to the slave.
     * SlaveTypeX are example buffers initialized in the slave, they will be
     * sent by the slave to the master.
     * */
    
    uint8_t MasterType2 [TYPE_2_LENGTH] = {'F', '4', '1', '9', '2', 'B'};
    uint8_t MasterType1 [TYPE_1_LENGTH] = { 8, 9};
    uint8_t MasterType0 [TYPE_0_LENGTH] = {0x08};
    uint8_t MasterType3 [TYPE_0_LENGTH] = {0x0A};
    uint8_t MasterType4 [TYPE_0_LENGTH] = {0x03};
    
    
    uint8_t SlaveType2 [TYPE_2_LENGTH] = {0};
    uint8_t SlaveType1 [TYPE_1_LENGTH] = {0};
    uint8_t SlaveType0 [TYPE_0_LENGTH] = {0};
    
    //******************************************************************************
    // General I2C State Machine ***************************************************
    //******************************************************************************
    
    typedef enum I2C_ModeEnum{
        IDLE_MODE,
        NACK_MODE,
        TX_REG_ADDRESS_MODE,
        RX_REG_ADDRESS_MODE,
        TX_DATA_MODE,
        RX_DATA_MODE,
        SWITCH_TO_RX_MODE,
        SWITHC_TO_TX_MODE,
        TIMEOUT_MODE
    } I2C_Mode;
    
    
    /* Used to track the state of the software state machine*/
    I2C_Mode MasterMode = IDLE_MODE;
    
    /* The Register Address/Command to use*/
    uint8_t TransmitRegAddr = 0;
    
    /* ReceiveBuffer: Buffer used to receive data in the ISR
     * RXByteCtr: Number of bytes left to receive
     * ReceiveIndex: The index of the next byte to be received in ReceiveBuffer
     * TransmitBuffer: Buffer used to transmit data in the ISR
     * TXByteCtr: Number of bytes left to transfer
     * TransmitIndex: The index of the next byte to be transmitted in TransmitBuffer
     * */
    uint8_t ReceiveBuffer[MAX_BUFFER_SIZE] = {0};
    uint8_t RXByteCtr = 0;
    uint8_t ReceiveIndex = 0;
    uint8_t TransmitBuffer[MAX_BUFFER_SIZE] = {0};
    uint8_t TXByteCtr = 0;
    uint8_t TransmitIndex = 0;
    
    int16_t xf=0;
    int16_t yf=0;
    int16_t zf=0;
    
    uint8_t Data_rate=0;
    uint8_t Data_Res =0;
    uint8_t Dev_ID   =0;
    
    
    
    /* I2C Write and Read Functions */
    
    /* For slave device with dev_addr, writes the data specified in *reg_data
     *
     * dev_addr: The slave device address.
     *           Example: SLAVE_ADDR
     * reg_addr: The register or command to send to the slave.
     *           Example: CMD_TYPE_0_MASTER
     * *reg_data: The buffer to write
     *           Example: MasterType0
     * count: The length of *reg_data
     *           Example: TYPE_0_LENGTH
     *  */
    I2C_Mode I2C_Master_WriteReg(uint8_t dev_addr, uint8_t reg_addr, uint8_t *reg_data, uint8_t count);
    
    /* For slave device with dev_addr, read the data specified in slaves reg_addr.
     * The received data is available in ReceiveBuffer
     *
     * dev_addr: The slave device address.
     *           Example: SLAVE_ADDR
     * reg_addr: The register or command to send to the slave.
     *           Example: CMD_TYPE_0_SLAVE
     * count: The length of data to read
     *           Example: TYPE_0_LENGTH
     *  */
    I2C_Mode I2C_Master_ReadReg(uint8_t dev_addr, uint8_t reg_addr, uint8_t count);
    void CopyArray(uint8_t *source, uint8_t *dest, uint8_t count);
    
    
    I2C_Mode I2C_Master_ReadReg(uint8_t dev_addr, uint8_t reg_addr, uint8_t count)
    {
        /* Initialize state machine */
        MasterMode = TX_REG_ADDRESS_MODE;
        TransmitRegAddr = reg_addr;
        RXByteCtr = count;
        TXByteCtr = 0;
        ReceiveIndex = 0;
        TransmitIndex = 0;
    
        /* Initialize slave address and interrupts */
        UCB2I2CSA = dev_addr;
        UCB2IFG &= ~(UCTXIFG + UCRXIFG);       // Clear any pending interrupts
        UCB2IE &= ~UCRXIE;                       // Disable RX interrupt
        UCB2IE |= UCTXIE;                        // Enable TX interrupt
    
        UCB2CTLW0 |= UCTR + UCTXSTT;             // I2C TX, start condition
        __bis_SR_register(LPM0_bits + GIE);              // Enter LPM0 w/ interrupts
    
        return MasterMode;
    
    }
    
    
    I2C_Mode I2C_Master_WriteReg(uint8_t dev_addr, uint8_t reg_addr, uint8_t *reg_data, uint8_t count)
    {
        /* Initialize state machine */
        MasterMode = TX_REG_ADDRESS_MODE;
        TransmitRegAddr = reg_addr;
    
        //Copy register data to TransmitBuffer
        CopyArray(reg_data, TransmitBuffer, count);
    
        TXByteCtr = count;
        RXByteCtr = 0;
        ReceiveIndex = 0;
        TransmitIndex = 0;
    
        /* Initialize slave address and interrupts */
        UCB2I2CSA = dev_addr;
        UCB2IFG &= ~(UCTXIFG + UCRXIFG);       // Clear any pending interrupts
        UCB2IE &= ~UCRXIE;                       // Disable RX interrupt
        UCB2IE |= UCTXIE;                        // Enable TX interrupt
    
        UCB2CTLW0 |= UCTR + UCTXSTT;             // I2C TX, start condition
        __bis_SR_register(LPM0_bits + GIE);           // Enter LPM0 w/ interrupts
    
        return MasterMode;
    }
    
    void CopyArray(uint8_t *source, uint8_t *dest, uint8_t count)
    {
        uint8_t copyIndex = 0;
        for (copyIndex = 0; copyIndex < count; copyIndex++)
        {
            dest[copyIndex] = source[copyIndex];
        }
    }
    
    
    //******************************************************************************
    // Device Initialization *******************************************************
    //******************************************************************************
    
    
    void initGPIO()
    {
        // Configure GPIO
        LED_OUT &= ~(LED0_PIN | LED1_PIN); // P1 setup for LED & reset output
        LED_DIR |= (LED0_PIN | LED1_PIN);
    
        // I2C pins
        P7SEL0 |= BIT0 | BIT1;
        P7SEL1 &= ~(BIT0 | BIT1);
    
        // Disable the GPIO power-on default high-impedance mode to activate
        // previously configured port settings
        PM5CTL0 &= ~LOCKLPM5;
    }
    
    void initClockTo16MHz()
    {
        // Configure one FRAM waitstate as required by the device datasheet for MCLK
        // operation beyond 8MHz _before_ configuring the clock system.
        FRCTL0 = FRCTLPW | NWAITS_1;
    
        // Clock System Setup
        CSCTL0_H = CSKEY_H;                     // Unlock CS registers
        CSCTL1 = DCOFSEL_0;                     // Set DCO to 1MHz
    
        // Set SMCLK = MCLK = DCO, ACLK = LFXTCLK (VLOCLK if unavailable)
        CSCTL2 = SELA__LFXTCLK | SELS__DCOCLK | SELM__DCOCLK;
    
        // Per Device Errata set divider to 4 before changing frequency to
        // prevent out of spec operation from overshoot transient
        CSCTL3 = DIVA__4 | DIVS__4 | DIVM__4;   // Set all corresponding clk sources to divide by 4 for errata
        CSCTL1 = DCOFSEL_4 | DCORSEL;           // Set DCO to 16MHz
    
        // Delay by ~10us to let DCO settle. 60 cycles = 20 cycles buffer + (10us / (1/4MHz))
        __delay_cycles(60);
        CSCTL3 = DIVA__1 | DIVS__1 | DIVM__1;   // Set all dividers to 1 for 16MHz operation
        CSCTL0_H = 0;                           // Lock CS registers
    }
    
    void initI2C()
    {
        UCB2CTLW0 = UCSWRST;                      // Enable SW reset
        UCB2CTLW0 |= UCMODE_3 | UCMST | UCSSEL__SMCLK | UCSYNC; // I2C master mode, SMCLK
        UCB2BRW = 1600;                            // fSCL = SMCLK/160 = ~100kHz
        UCB2I2CSA = SLAVE_ADDR;                   // Slave Address
        UCB2CTLW0 &= ~UCSWRST;                    // Clear SW reset, resume operation
        UCB2IE |= UCNACKIE;
    }
    
    
    //******************************************************************************
    // Main ************************************************************************
    // Send and receive three messages containing the example commands *************
    //******************************************************************************
    
    int main(void) {
        WDTCTL = WDTPW | WDTHOLD;   // Stop watchdog timer
        initClockTo16MHz();
        initGPIO();
        initI2C();
    
    
       I2C_Master_ReadReg(SLAVE_ADDR, CMD_TYPE_0_SLAVE, TYPE_0_LENGTH);
       CopyArray(ReceiveBuffer, SlaveType0, TYPE_0_LENGTH);
       Dev_ID=ReceiveBuffer[0];
       __delay_cycles(10);
    
        I2C_Master_WriteReg(SLAVE_ADDR, Power_CTL, MasterType0, TYPE_0_LENGTH);
        __delay_cycles(10);
    //
        I2C_Master_WriteReg(SLAVE_ADDR, Data_Rate, MasterType3, TYPE_0_LENGTH);
        __delay_cycles(10);
    //
        I2C_Master_WriteReg(SLAVE_ADDR, Data_Format, MasterType4, TYPE_0_LENGTH);
        __delay_cycles(10);
    //
    //    I2C_Master_ReadReg(SLAVE_ADDR, Data_Rate, TYPE_0_LENGTH);
    //    CopyArray(ReceiveBuffer, SlaveType0, TYPE_0_LENGTH);
    //    Data_rate=ReceiveBuffer[0];
    //
    //    __delay_cycles(10);
    //
    //    I2C_Master_ReadReg(SLAVE_ADDR, Data_Format, TYPE_0_LENGTH);
    //    CopyArray(ReceiveBuffer, SlaveType0, TYPE_0_LENGTH);
    //    Data_Res=ReceiveBuffer[0];
    //
    //    __delay_cycles(10);
    //
    //    I2C_Master_ReadReg(SLAVE_ADDR, X0_Reg, TYPE_1_LENGTH);
    //    CopyArray(ReceiveBuffer, SlaveType0, TYPE_1_LENGTH);
    //    uint8_t x0 = ReceiveBuffer[0];
    //    __delay_cycles(10);
    //
    //    I2C_Master_ReadReg(SLAVE_ADDR, X1_Reg, TYPE_0_LENGTH);
    //    CopyArray(ReceiveBuffer, SlaveType0, TYPE_0_LENGTH);
    //    uint8_t x1 = ReceiveBuffer[0];
    //    __delay_cycles(10);
    //
    //    x1=x1&0x03;
    //    uint16_t x = (x1 << 8) + x0;
    //    xf = x;
    //    if(xf > 511)
    //    {
    //     xf = xf - 1024;
    //    }
    //
    //    I2C_Master_ReadReg(SLAVE_ADDR, Z0_Reg, TYPE_0_LENGTH);
    //    CopyArray(ReceiveBuffer, SlaveType0, TYPE_0_LENGTH);
    //    uint8_t z0 = ReceiveBuffer[0];
    //    __delay_cycles(10);
    //
    //    I2C_Master_ReadReg(SLAVE_ADDR, Z1_Reg, TYPE_0_LENGTH);
    //    CopyArray(ReceiveBuffer, SlaveType0, TYPE_0_LENGTH);
    //    uint8_t z1 = ReceiveBuffer[0];
    //    __delay_cycles(10);
    //
    //    z1=z1&0x03;
    //    uint16_t z = (z1 << 8) + z0;
    //    zf = z;
    //    if(zf > 511)
    //    {
    //     zf = zf - 1024;
    //    }
    //
    //    I2C_Master_ReadReg(SLAVE_ADDR, Y0_Reg, TYPE_0_LENGTH);
    //    CopyArray(ReceiveBuffer, SlaveType0, TYPE_0_LENGTH);
    //    uint8_t y0 = ReceiveBuffer[0];
    //    __delay_cycles(10);
    //
    //    I2C_Master_ReadReg(SLAVE_ADDR, Y1_Reg, TYPE_0_LENGTH);
    //    CopyArray(ReceiveBuffer, SlaveType0, TYPE_0_LENGTH);
    //    uint8_t y1 = ReceiveBuffer[0];
    //    __delay_cycles(10);
    //
    //    y1=y1&0x03;
    //    uint16_t y = (y1 << 8) + y0;
    //    yf = y;
    //    if(yf > 511)
    //    {
    //     yf = yf - 1024;
    //    }
    
    
    //    I2C_Master_ReadReg(SLAVE_ADDR, CMD_TYPE_0_SLAVE, TYPE_0_LENGTH);
    //    CopyArray(ReceiveBuffer, SlaveType0, TYPE_0_LENGTH);
    //
    //    I2C_Master_WriteReg(SLAVE_ADDR, CMD_TYPE_0_MASTER, MasterType0, TYPE_0_LENGTH);
    //    I2C_Master_WriteReg(SLAVE_ADDR, CMD_TYPE_1_MASTER, MasterType1, TYPE_1_LENGTH);
    //    I2C_Master_WriteReg(SLAVE_ADDR, CMD_TYPE_2_MASTER, MasterType2, TYPE_2_LENGTH);
    //
    //    I2C_Master_ReadReg(SLAVE_ADDR, CMD_TYPE_0_SLAVE, TYPE_0_LENGTH);
    //    CopyArray(ReceiveBuffer, SlaveType0, TYPE_0_LENGTH);
    //
    //    I2C_Master_ReadReg(SLAVE_ADDR, CMD_TYPE_1_SLAVE, TYPE_1_LENGTH);
    //    CopyArray(ReceiveBuffer, SlaveType1, TYPE_1_LENGTH);
    //
    //    I2C_Master_ReadReg(SLAVE_ADDR, CMD_TYPE_2_SLAVE, TYPE_2_LENGTH);
    //    CopyArray(ReceiveBuffer, SlaveType2, TYPE_2_LENGTH);
    
        __bis_SR_register(LPM0_bits + GIE);
        return 0;
    }
    
    
    //******************************************************************************
    // I2C Interrupt ***************************************************************
    //******************************************************************************
    
    #if defined(__TI_COMPILER_VERSION__) || defined(__IAR_SYSTEMS_ICC__)
    #pragma vector = USCI_B2_VECTOR
    __interrupt void USCI_B2_ISR(void)
    #elif defined(__GNUC__)
    void __attribute__ ((interrupt(USCI_B2_VECTOR))) USCI_B2_ISR (void)
    #else
    #error Compiler not supported!
    #endif
    {
      //Must read from UCB2RXBUF
      uint8_t rx_val = 0;
      switch(__even_in_range(UCB2IV, USCI_I2C_UCBIT9IFG))
      {
        case USCI_NONE:          break;         // Vector 0: No interrupts
        case USCI_I2C_UCALIFG:   break;         // Vector 2: ALIFG
        case USCI_I2C_UCNACKIFG:                // Vector 4: NACKIFG
          break;
        case USCI_I2C_UCSTTIFG:  break;         // Vector 6: STTIFG
        case USCI_I2C_UCSTPIFG:  break;         // Vector 8: STPIFG
        case USCI_I2C_UCRXIFG3:  break;         // Vector 10: RXIFG3
        case USCI_I2C_UCTXIFG3:  break;         // Vector 12: TXIFG3
        case USCI_I2C_UCRXIFG2:  break;         // Vector 14: RXIFG2
        case USCI_I2C_UCTXIFG2:  break;         // Vector 16: TXIFG2
        case USCI_I2C_UCRXIFG1:  break;         // Vector 18: RXIFG1
        case USCI_I2C_UCTXIFG1:  break;         // Vector 20: TXIFG1
        case USCI_I2C_UCRXIFG0:                 // Vector 22: RXIFG0
            rx_val = UCB2RXBUF;
            if (RXByteCtr)
            {
              ReceiveBuffer[ReceiveIndex++] = rx_val;
              RXByteCtr--;
            }
    
            if (RXByteCtr == 1)
            {
              UCB2CTLW0 |= UCTXSTP;
            }
            else if (RXByteCtr == 0)
            {
              UCB2IE &= ~UCRXIE;
              MasterMode = IDLE_MODE;
              __bic_SR_register_on_exit(CPUOFF);      // Exit LPM0
            }
            break;
        case USCI_I2C_UCTXIFG0:                 // Vector 24: TXIFG0
            switch (MasterMode)
            {
              case TX_REG_ADDRESS_MODE:
                  UCB2TXBUF = TransmitRegAddr;
                  if (RXByteCtr)
                      MasterMode = SWITCH_TO_RX_MODE;   // Need to start receiving now
                  else
                      MasterMode = TX_DATA_MODE;        // Continue to transmision with the data in Transmit Buffer
                  break;
    
              case SWITCH_TO_RX_MODE:
                  UCB2IE |= UCRXIE;              // Enable RX interrupt
                  UCB2IE &= ~UCTXIE;             // Disable TX interrupt
                  UCB2CTLW0 &= ~UCTR;            // Switch to receiver
                  MasterMode = RX_DATA_MODE;    // State state is to receive data
                  UCB2CTLW0 |= UCTXSTT;          // Send repeated start
                  if (RXByteCtr == 1)
                  {
                      //Must send stop since this is the N-1 byte
                      while((UCB2CTLW0 & UCTXSTT));
                      UCB2CTLW0 |= UCTXSTP;      // Send stop condition
                  }
                  break;
    
              case TX_DATA_MODE:
                  if (TXByteCtr)
                  {
                      UCB2TXBUF = TransmitBuffer[TransmitIndex++];
                      TXByteCtr--;
                  }
                  else
                  {
                      //Done with transmission
                      UCB2CTLW0 |= UCTXSTP;     // Send stop condition
                      MasterMode = IDLE_MODE;
                      UCB2IE &= ~UCTXIE;                       // disable TX interrupt
                      __bic_SR_register_on_exit(CPUOFF);      // Exit LPM0
                  }
                  break;
    
              default:
                  __no_operation();
                  break;
            }
            break;
        default: break;
      }
    }
    
    
     

  • Hey guys,

    More updates,I guess i have found out where the problem. On both of my read and write functions i have set the LPM0 bit. So the cpu will be OFF untill any interrupts comes in. Once the appropriate interrupt(Tx or Rx) is occurred I am coming out of that mode  using this following command:   

    bic_SR_register_on_exit(CPUOFF);

    But the issue i am not able exit from the low power mode even after the execution of the above command. So i sense there is some issue in the way i exiting the Low power mode. So guys please drop your suggestion on this problem.

    Note:I am using the same above code

  • How do you tell that it doesn't exit from LPM? Breakpoint? Externally-visible behavior? Does it fail on the first transaction, or a subsequent one?

    I don't have an ADXL345 here, so I enabled the internal pulllups on P7.0/1 and I got a (expected) NACK. When I added a wakeup to the NACK case, it woke up just fine (breakpoint).

  • I ran the code step by step and i saw after the first function is completed instead of  moving forward to second function in the main() function it stuck in this same line forever

    Line: __bis_SR_register(CPUOFF + GIE);

    New issue: Sometimes Tx interrupt is also not get triggered

  • Are you getting to the ISR? Instead of stepping, try setting a breakpoint on the "return" after the CPUOFF setting. 

  • Now I am not getting into ISR itself.After the setting the UCTXSTT bit.the transmit interrupt is not triggered.

  • Not getting the initial TXIFG usually means that the slave is holding SCL (or SDA) low ("forever"). This is not uncommon when debugging, since it's not difficult to find yourself resetting the master (MCU) in the middle of a transaction.

    The ADXL345 doesn't have a Reset pin, so to fix it you need to power-cycle it (maybe power-cycle your whole system).

    ------------

    Unsolicited: The ADXL345 can run on 140uA, so it's feasible to power it using a GPIO (<2mA). Doing this would allow you to reset it under program control.

**Attention** This is a public forum