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.

MSPM0G1106: I2C STAND LOOP

Part Number: MSPM0G1106

Tool/software:

In the code line below, I can read the DS1307 time chip without any problems, but in case of any hangup or sudden processor reset, while (DL_I2C_isControllerRXFIFOEmpty(I2C_INST)); It is waiting within the function, which stops my system. In this case, what software scenario would you recommend?

uint8_t readDS1307Register(uint8_t regAddr) {
    uint8_t data;


    DL_I2C_fillControllerTXFIFO(I2C_INST, &regAddr, 1);

       /* Wait for I2C to be Idle */
       while (!(
           DL_I2C_getControllerStatus(I2C_INST) & DL_I2C_CONTROLLER_STATUS_IDLE))
           ;

       /* Send the packet to the controller.
        * This function will send Start + Stop automatically.
        */
       DL_I2C_startControllerTransfer(I2C_INST, DS1307_ADDRESS,
           DL_I2C_CONTROLLER_DIRECTION_TX, 1);

       /* Trap if there was an error */
       if (DL_I2C_getControllerStatus(I2C_INST) &
           DL_I2C_CONTROLLER_STATUS_ERROR) {
           /* LED will remain high if there is an error */
           __BKPT(0);
       }

       /* Wait for I2C to be Idle */
       while (!(
           DL_I2C_getControllerStatus(I2C_INST) & DL_I2C_CONTROLLER_STATUS_IDLE))
           ;

       /* Add delay between transfers */
       delay_cycles(100);

       /* Send a read request to Target */
       DL_I2C_startControllerTransfer(I2C_INST, DS1307_ADDRESS,
           DL_I2C_CONTROLLER_DIRECTION_RX, 1);




    while (DL_I2C_isControllerRXFIFOEmpty(I2C_INST));
    data = DL_I2C_receiveControllerData(I2C_INST);


    if (DL_I2C_getControllerStatus(I2C_INST) & DL_I2C_CONTROLLER_STATUS_ERROR) {
        printf("I2C Okuma Hatasi!\n");
    }

    return bcdToDec(data);
}

  • Hi,

    1. A system reset may interrupt the i2c write command transferring progress, which causes i2c target device could not respond to i2c controller. 

    2. To avoid program blocking in the  while (DL_I2C_isControllerRXFIFOEmpty(I2C_INST)); , a timeout parameter could be used in this problem. Here is a example for reference:

    int i2c_read(I2C_Regs *i2c, uint32_t targetAddr, uint8_t *buffer, uint16_t length, uint32_t timeOut)
    {
        /* Wait for I2C to be Idle */
        while (!(
            DL_I2C_getControllerStatus(i2c) & DL_I2C_CONTROLLER_STATUS_IDLE))
            ;
    
        /* Send a read request to Target */
        DL_I2C_startControllerTransfer(i2c, targetAddr,
            DL_I2C_CONTROLLER_DIRECTION_RX, length);
    
        /*
         * Receive all bytes from target. LED will remain high if not all bytes
         * are received
         */
        for (uint8_t i = 0; i < length; i++) {
            uint32_t tmpCnt = timeOut;
            while (DL_I2C_isControllerRXFIFOEmpty(i2c)){
                tmpCnt --;
                /* Receiving time out */
                if (tmpCnt == 0)
                    return -1;
            }
            buffer[i] = DL_I2C_receiveControllerData(i2c);
        }
    
        return 0;
    }

    Regards,

    Pengfei