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.

MSP432E401Y: REGARDING I2C WRITE OPERATION

Part Number: MSP432E401Y

Tool/software:

Greetings... I am trying to write 16 bit data to the specific register address through I2C. Iam not getting any errors while writing the data into the register. Then I read it back from that register the values are not changing. Here is my code snippet.

Please help. Thanks in advance.

void I2C_WriteRegister(uint8_t regAddress, uint8_t *data, uint8_t length) {


MAP_I2CMasterSlaveAddrSet(I2C3_BASE, SLAVE_ADDRESS, false);
MAP_I2CMasterDataPut(I2C3_BASE, regAddress);
MAP_I2CMasterControl(I2C3_BASE, I2C_MASTER_CMD_BURST_SEND_START);

timeoutCounter = 0;
while (MAP_I2CMasterBusy(I2C3_BASE)) {
MAP_SysCtlDelay(1000);
if (++timeoutCounter > TIMEOUT) {
UARTprintf("TRANSFER TIMEOUT\n");
return;
}
}

if (MAP_I2CMasterErr(I2C3_BASE) != I2C_MASTER_ERR_NONE) {
UARTprintf("TRANSFER ERROR\n");
return;
}

for (uint8_t i = 0; i < length; i++) {
MAP_I2CMasterDataPut(I2C3_BASE, data[i]);
if (i == length - 1) {
MAP_I2CMasterControl(I2C3_BASE, I2C_MASTER_CMD_BURST_SEND_FINISH);
} else {
MAP_I2CMasterControl(I2C3_BASE, I2C_MASTER_CMD_BURST_SEND_CONT);
}

timeoutCounter = 0;
while (MAP_I2CMasterBusy(I2C3_BASE)) {
MAP_SysCtlDelay(1000);
if (++timeoutCounter > TIMEOUT) {
UARTprintf("TRANSFER TIMEOUT\n");
return;
}
}

if (MAP_I2CMasterErr(I2C3_BASE) != I2C_MASTER_ERR_NONE) {
UARTprintf("TRANSFER ERROR\n");
return;
}
}
}

int main(){

...

uint8_t writeData[2] = {0xFF, 0xFF};
I2C_WriteRegister(registerAddress, writeData, 2);

UARTprintf("Data written to Register 0x%02X: 0x%02X 0x%02X\n", registerAddress, writeData[0], writeData[1]);

..

}

  • Hi,

      Can you show a logic analyzer capture of the I2C waveform when you write and read?

    I see that you have while (MAP_I2CMasterBusy(I2C3_BASE)) followed by a wait a SysCtlDelay(1000). In stead of using delay, please at everywhere you have the while (MAP_I2CMasterBusy), change to below.

    Change from:

    while (MAP_I2CMasterBusy(I2C3_BASE))

    To:

    while (!MAP_I2CMasterBusy(I2C3_BASE)) // Add this new line everywhere you use the below line. 

    while (MAP_I2CMasterBusy(2C3_BASE))

  • Thanks for your reply...But I don't have logic analyzer with me. I changed while (MAP_I2CMasterBusy(I2C3_BASE)) to while (!MAP_I2CMasterBusy(I2C3_BASE)).

    But now also iam not getting any errors. But the value is not written to the specific register(checked with readI2C function). Please help me. Thanks in advance.

    void I2C_WriteRegister(uint8_t regAddress, uint8_t *data, uint8_t length) {
    MAP_I2CMasterSlaveAddrSet(I2C3_BASE, SLAVE_ADDRESS, false);
    MAP_I2CMasterDataPut(I2C3_BASE, regAddress);

     MAP_I2CMasterControl(I2C3_BASE, I2C_MASTER_CMD_BURST_SEND_START);

    timeoutCounter = 0;
    while (!MAP_I2CMasterBusy(I2C3_BASE)) {
    if (++timeoutCounter > TIMEOUT) {
    UARTprintf("TRANSFER TIMEOUT\n");
    return;
    }
    }

    if (MAP_I2CMasterErr(I2C3_BASE) != I2C_MASTER_ERR_NONE) {
    UARTprintf("TRANSFER ERROR\n");
    return;
    }

    for (uint8_t i = 0; i < length; i++) {
    MAP_I2CMasterDataPut(I2C3_BASE, data[i]);
    if (i == length - 1) {
    MAP_I2CMasterControl(I2C3_BASE, I2C_MASTER_CMD_BURST_SEND_FINISH);
    } else {
    MAP_I2CMasterControl(I2C3_BASE, I2C_MASTER_CMD_BURST_SEND_CONT);
    }

    timeoutCounter = 0;
    while (!MAP_I2CMasterBusy(I2C3_BASE)) {
    if (++timeoutCounter > TIMEOUT) {
    UARTprintf("TRANSFER TIMEOUT\n");
    return;
    }
    }

    if (MAP_I2CMasterErr(I2C3_BASE) != I2C_MASTER_ERR_NONE) {
    UARTprintf("TRANSFER ERROR\n");
    return;
    }
    }
    }

     int main(){

    ...

    uint8_t writeData[2] = {0xED, 0x97};
    I2C_WriteRegister(registerAddress, writeData, 2);

    UARTprintf("Data written to Register 0x%02X: 0x%02X 0x%02X\n", registerAddress, writeData[0], writeData[1]);

    ...

    }

    output from putty :

    Data written to Register 0x00: 0xed 0x97

  • Hi,

      If you don't have a logic analyzer, then do you have a scope? A logic analyzer or a scope will be extremely helpful to diagnose what the problem is. 

      Do you have the correct slave address?

      Do you have the pullup resistor on SCL and SDA buses?

      Looking at your below code, I think you need to insert the the busy checking like below. Try to see if that makes a difference. 

    for (uint8_t i = 0; i < length; i++) {
    MAP_I2CMasterDataPut(I2C3_BASE, data[i]);
    if (i == length - 1) {
    MAP_I2CMasterControl(I2C3_BASE, I2C_MASTER_CMD_BURST_SEND_FINISH);

    while (!MAP_I2CMasterBusy(I2C3_BASE)); // Add this new line everywhere you use the below line. 

    while (MAP_I2CMasterBusy(2C3_BASE));


    } else {
    MAP_I2CMasterControl(I2C3_BASE, I2C_MASTER_CMD_BURST_SEND_CONT);

    while (!MAP_I2CMasterBusy(I2C3_BASE)); // Add this new line everywhere you use the below line. 

    while (MAP_I2CMasterBusy(2C3_BASE));
    }

      Also take a look at this post. I think it will be helpful. 

    https://e2e.ti.com/support/microcontrollers/arm-based-microcontrollers-group/arm-based-microcontrollers/f/arm-based-microcontrollers-forum/1356707/tm4c1290ncpdt-having-problem-reading-i2c-data/5176124#5176124