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.

MSP432P401R: How to setup the code for i2c from srcatch?

Part Number: MSP432P401R

Hi,

I'm trying to set up the code format for i2c communication with the master to slave sensor.

I didn't get the idea how to initialize and how to start the code. 

Can anyone help me? 

  • Chethan,

     There are several good examples to illustrate how to configure the I2C for communication. The table below shows the right ones to use for your master/slave pair for your choice of OS (either non-RTOS (driverlib) or TI-RTOS). Note that you'll need TWO LaunchPads to run these examples (1 master, 1 slave)

    driverlib/ non-RTOS

    TI-RTOS
    Master i2c_master_w_multibyte-master_code i2cmasterexample1
    Slave i2c_master_w_multibyte-slave_code i2cslaveexample1

    For the driverlib/non-RTOS Master example, you'll find near the top of the code the following data structure:

    const eUSCI_I2C_MasterConfig i2cConfig = {
      EUSCI_B_I2C_CLOCKSOURCE_SMCLK, // SMCLK Clock Source
      3000000, // SMCLK = 3MHz
      EUSCI_B_I2C_SET_DATA_RATE_400KBPS, // Desired I2C Clock of 400khz
      0, // No byte counter threshold
      EUSCI_B_I2C_NO_AUTO_STOP // No Autostop
    };

    You'll likely want to configure the first 3 parameters to suit your design- namely the SMCLK source and rate, and the data rate for your I2C channel.  Make sure that your slave is operating at the same rate, of course.

    -Bob L.

  • Hi,
    thanks for replied. I need code for non-interrupts read-write code for i2c communication. we are reading the sensor reading.
  • Hi, pleae check what is the mistake in the below code

    /*******************************************************************************
    int main(void) {
    int i;

    printf("PS sensor\n");
    MAP_GPIO_setAsPeripheralModuleFunctionInputPin(GPIO_PORT_P1, GPIO_PIN6 + GPIO_PIN7, GPIO_PRIMARY_MODULE_FUNCTION);
    UCB0BR0 = 12; // fSCL = SMCLK/12 = ~100kHz
    UCB0BR1 = 0;
    P1REN = 1;
    MAP_I2C_initMaster(EUSCI_B0_BASE, &i2cConfig);
    MAP_I2C_enableModule(EUSCI_B0_BASE);

    while(1) {
    UCTXSTT=1; //start bit
    MAP_I2C_setSlaveAddress(EUSCI_B1_BASE, SLAVE_ADDRESS);
    MAP_I2C_setMode(EUSCI_B1_BASE, EUSCI_B_I2C_TRANSMIT_MODE);
    while (MAP_I2C_isBusBusy(EUSCI_B0_BASE) == EUSCI_B_I2C_BUS_BUSY);

    MAP_I2C_masterSendMultiByteStart(EUSCI_B1_BASE, 0x08);
    while (MAP_I2C_isBusBusy(EUSCI_B0_BASE) == EUSCI_B_I2C_BUS_BUSY);
    MAP_I2C_masterSendMultiByteStart(EUSCI_B1_BASE, 0x08);
    MAP_I2C_masterSendMultiByteStop(EUSCI_B1_BASE);

    // / Making sure the last transaction has been completely sent out /
    while (MAP_I2C_masterIsStopSent(EUSCI_B1_BASE) == EUSCI_B_I2C_SENDING_STOP);
    while (MAP_I2C_isBusBusy(EUSCI_B0_BASE) == EUSCI_B_I2C_BUS_BUSY);

    UCTXSTT=1; //start bit
    MAP_I2C_setSlaveAddress(EUSCI_B1_BASE, SLAVE_ADDRESS);
    MAP_I2C_setMode(EUSCI_B1_BASE, EUSCI_B_I2C_RECEIVE_MODE);
    while (MAP_I2C_isBusBusy(EUSCI_B0_BASE) == EUSCI_B_I2C_BUS_BUSY);

    MAP_I2C_masterReceiveStart(EUSCI_B1_BASE);
    while (MAP_I2C_isBusBusy(EUSCI_B0_BASE) == EUSCI_B_I2C_BUS_BUSY);
    uint8_t msb = MAP_I2C_masterReceiveMultiByteNext(EUSCI_B1_BASE);

    while (MAP_I2C_isBusBusy(EUSCI_B0_BASE) == EUSCI_B_I2C_BUS_BUSY);
    uint8_t lsb = MAP_I2C_masterReceiveMultiByteNext(EUSCI_B1_BASE);

    while (MAP_I2C_isBusBusy(EUSCI_B0_BASE) == EUSCI_B_I2C_BUS_BUSY);
    MAP_I2C_masterReceiveMultiByteFinish(EUSCI_B1_BASE);
    UCTXSTP=0; //stop bit
    while (MAP_I2C_isBusBusy(EUSCI_B0_BASE) == EUSCI_B_I2C_BUS_BUSY);
    // Making sure the last transaction has been completely sent out /
    // while (MAP_I2C_masterIsStopSent(EUSCI_B1_BASE) == EUSCI_B_I2C_SENDING_STOP);

    uint16_t data = ((uint16_t)msb << 8) | lsb;

    printf("PS = %d\n%d\n%d\n",msb,lsb,data);
    for(i=100000; i>0; i--);
    }
    }
  • Perhaps you could describe what is not working? What error messages are you seeing?
    -Bob L.
  • Closing this due to no response - Please feel free to submit another post if you are still having issues with your code.
    -Bob L.

**Attention** This is a public forum