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 slave issue

Other Parts Discussed in Thread: MSP430G2553

Raspberry is I2C master and sends predefined packet to the slave: {0x06 0x08 0x04 0x00 0x02 0x10 0x0D}

MSP430G2553 receives and should send back an answer if the packet matches (first and last byte, 0x06 and 0x0D accordingly). I am able to save received bytes into array but if I try to send back the answer, then it sends: {0x06 0x04 0x02 0x10 0x06 0x04 0x02 0x10}. Basically it is skipping one byte after each and starts again from the beginning. Thee correct byte order should be as received order. I suspect that it is increasing the counter twice the speed as it should by calling out transmit_cb() but I don't understand why.

In short, how to send back received (and saved into array with slight modifications) data back to master from array?

Slave code:

#define P_SYNC         0x06

#define CTRL_REQ     0x08
#define CTRL_ANS     0x12

#define P_FOOTER    0x0D

/***************************************************************************************************
 *            Global Variable section                                                                 *
 ***************************************************************************************************/
unsigned char flag      = 0;
unsigned char flag1  = 0;
unsigned char TXData = 0;
unsigned char RXData = 0;

unsigned char in_array[64] = {0};
unsigned char save_flag     = 0;
unsigned char counter         = 0;
unsigned char txCount = 0;

/***************************************************************************************************
 *            Main                                                                                        *
 ***************************************************************************************************/
int main(void) {
    WDTCTL = WDTPW + WDTHOLD;                 // Stop WDT
    TI_USCI_I2C_slaveinit(start_cb, transmit_cb, receive_cb, SLAVE_ADDR); // init the slave
    _EINT();

    BCSCTL1 = CALBC1_16MHZ;
    DCOCTL = CALDCO_16MHZ;
    LPM0;                                                // Enter LPM0.

}

void start_cb() {
  RXData = 0;
}

void receive_cb(unsigned char receive) {
    //RXData = receive;
    Save_Array(receive);            // Save new data into array

    RXData = Check_Bytes(receive);        // Check packet header and footer
    //flag1++;
}

void transmit_cb(unsigned char volatile *byte) {
  //*byte = TXData;

    if (txCount > 7)
        txCount = 0;
    TXData = in_array[txCount++];
    *byte = TXData;
}


unsigned char Check_Bytes(unsigned char in_byte) {
    switch (in_byte) {
    case P_SYNC:
        save_flag = 1;                break;
    case P_FOOTER:
        /* TXData = P_FOOTER; */     break;
    }
    return TXData;
}

// *************************************************************************************************
// @fn          Save_Array
// @brief       Save received bytes into array
// @param       uint8 in_byte        Receiving byte
// @return      None
// *************************************************************************************************
void Save_Array(unsigned char in_byte) {
    // Reset counter if we find PACKET HEADER
    if (in_byte == P_SYNC)
        counter = 0;

    // Replace REQUEST packet type with ANSWER
    if ((in_byte == CTRL_REQ) && (counter == 1))
            in_byte = CTRL_ANS;

    in_array[counter] = in_byte;        // Save new data into array
    counter++;                            // Increase counter value

    // If we see PACKET FOOTER then disable saving into array
    if (in_byte == P_FOOTER)
        save_flag = 0;
}

**Attention** This is a public forum