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.

MSPM0C1104: MSPM0C1104 I2c not working

Part Number: MSPM0C1104


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

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

       /* Send a read request to Target */
       DL_I2C_startControllerTransfer(I2C_INST, I2C_TARGET_ADDRESS,
           DL_I2C_CONTROLLER_DIRECTION_RX, I2C_RX_PACKET_SIZE);

       /*
        * Receive all bytes from target. LED will remain high if not all bytes
        * are received
        */
       for (uint8_t i = 0; i < I2C_RX_PACKET_SIZE; i++) {
           while (DL_I2C_isControllerRXFIFOEmpty(I2C_INST))
               ;
           gRxPacket[i] = DL_I2C_receiveControllerData(I2C_INST);

       }
}

void I2C_Write(uint8_t *DataBuffer){
    /*
      * Fill FIFO with data. This example will send a MAX of 4 bytes since it
      * doesn't handle the case where FIFO is full
      */

     DL_I2C_fillControllerTXFIFO(I2C_INST, &gTxPacket[0], I2C_TX_PACKET_SIZE);

     /* 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, I2C_TARGET_ADDRESS,
         DL_I2C_CONTROLLER_DIRECTION_TX, I2C_TX_PACKET_SIZE);

     /* Poll until the Controller writes all bytes */
     while (DL_I2C_getControllerStatus(I2C_INST) &
            DL_I2C_CONTROLLER_STATUS_BUSY_BUS)
         ;

     /* 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);
     }
}
I am using following code for I2C write and read. Write is working but I2C read is not working.
In .syscfg controller mode is enabled and target mode is disabled.
Can you help me with solution for success of I2C read.
  • Tx package is 3 bytes and RX package is 4 bytes.

    Tx ( I2C slave address, register address, data to write on register)
    Rx package (startbit I2C Slave address ACK, register address ACK, Start bit slave address Ack , readdata ACK stop bit.

    Above is the frame of read data.

  • void I2C_Read(uint8_t reg_address){


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

    DL_I2C_startControllerTransfer(I2C_INST, I2C_TARGET_ADDRESS,
    DL_I2C_CONTROLLER_DIRECTION_TX, reg_address);
    /* Send a read request to Target */
    DL_I2C_startControllerTransfer(I2C_INST, I2C_TARGET_ADDRESS,
    DL_I2C_CONTROLLER_DIRECTION_RX, I2C_RX_PACKET_SIZE);

    /*
    * Receive all bytes from target. LED will remain high if not all bytes
    * are received
    */
    for (uint8_t i = 0; i < I2C_RX_PACKET_SIZE; i++) {
    while (DL_I2C_isControllerRXFIFOEmpty(I2C_INST))
    ;
    gRxPacket[i] = DL_I2C_receiveControllerData(I2C_INST);

    }
    }

    Following is the updated void I2C_Read(uint8_t reg_address) function. 

    Program has stuck in while (DL_I2C_isControllerRXFIFOEmpty(I2C_INST))
    ;

    in the loop. Please suggest solution.

  • Hi Vaishak,

    for (uint8_t i = 0; i < I2C_RX_PACKET_SIZE; i++) {
    while (DL_I2C_isControllerRXFIFOEmpty(I2C_INST))
    ;
    gRxPacket[i] = DL_I2C_receiveControllerData(I2C_INST);

    }

    The logic looks abnormal. I prefer belows:

    for (uint8_t i = 0; i < I2C_RX_PACKET_SIZE; i++) {

    if (!DL_I2C_isControllerRXFIFOEmpty(I2C_INST)) {

    gRxPacket[i] = DL_I2C_receiveControllerData(I2C_INST);

    }

    else {

    break;

    }

    }

    B.R.

    Sal

  • Thanks Sal Ye, The Query has resolved.