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.

CCS/MSP432P401R: problems connecting sensor VL53L0X via I2C

Part Number: MSP432P401R


Tool/software: Code Composer Studio

Hello,
i try to connect the Tof Sensor VL53L0X with MSP432 Launchpad via I2C.
The sensor is connected as follows:
VCC => 3.3V
GND => GND
SCL => P6.5
SDA => P6.4
GPIO1 => not connected
XSHUT => P6.0

I want to read register 0xC0 from the sensor after the sensor has started, but unfortunately I can't get it to work. My code looks like this so far:

#include "driverlib.h"


#define SLAVE_ADDRESS_1 0x29


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

void TofSensor_Init(void)
{
        
    // Configure P6.4 for EUSCI_B1_SPI_I2C EUSCI_B1_SPI_I2C.SCL P6.5 for EUSCI_B1_SPI_I2C EUSCI_B1_SPI_I2C.SDA
         MAP_GPIO_setAsPeripheralModuleFunctionInputPin(GPIO_PORT_P6, GPIO_PIN4 + GPIO_PIN5, GPIO_PRIMARY_MODULE_FUNCTION);
         
        MAP_GPIO_setAsOutputPin(GPIO_PORT_P6, GPIO_PIN0);

        /* Initializing I2C Master to SMCLK at 400khz with no autostop */
        MAP_I2C_initMaster(EUSCI_B1_BASE, &i2cConfig);

        /* Specify slave address */
        MAP_I2C_setSlaveAddress(EUSCI_B1_BASE, SLAVE_ADDRESS_1);

        /* Enable I2C Module to start operations */
        MAP_I2C_enableModule(EUSCI_B1_BASE);
        //Boot sensor
        MAP_GPIO_setOutputHighOnPin(GPIO_PORT_P6,GPIO_PIN0);
        __delay_cycles(1000);// Delay for sensor to be ready
        MAP_I2C_setMode(EUSCI_B1_BASE, EUSCI_B_I2C_TRANSMIT_MODE);
        MAP_I2C_masterReceiveStart(EUSCI_B1_BASE);
        uint8_t address=0xC0;
        MAP_I2C_masterSendSingleByte(EUSCI_B1_BASE,address);
        MAP_I2C_setMode(EUSCI_B1_BASE, EUSCI_B_I2C_RECEIVE_MODE);
        MAP_I2C_masterReceiveMultiByteStop(EUSCI_B1_BASE);

}

Can anyone explain to me how I can read the register? Can someone explain to me how to write data into the register?

I have already tried some entries from the community, but without success.

  • Hi Louis,

    I confused about the slave address you are using.

    You have a #define that uses 0x29 (which is correct), but then in code you use 0xC0?

    Use 0x29.  If you still have trouble do you have any way to probe the I2C SDA and SCL lines (oscilloscope or logic probe)?

    If so, check that you are seeing an ACK from the VL53L0X after sending the slave address.

  • >        MAP_I2C_setMode(EUSCI_B1_BASE, EUSCI_B_I2C_TRANSMIT_MODE);
    >        MAP_I2C_masterReceiveStart(EUSCI_B1_BASE);
    >        uint8_t address=0xC0;
    >        MAP_I2C_masterSendSingleByte(EUSCI_B1_BASE,address);
    >        MAP_I2C_setMode(EUSCI_B1_BASE, EUSCI_B_I2C_RECEIVE_MODE);
    >        MAP_I2C_masterReceiveMultiByteStop(EUSCI_B1_BASE); 

    I'm not sure this sequence is doing what you think. For this particular task (reading register 0xC0 to see if it's ==0xEE), I think all you need is:

    >        uint8_t address=0xC0, regC0;
    >        MAP_I2C_masterSendSingleByte(EUSCI_B1_BASE,address);
    >        regC0 = MAP_I2C_masterReceiveSingleByte(EUSCI_B1_BASE);

    When you get to multi-byte transactions, you'll need a little more. 

  • Hello,

    Thanks for an answer, it worked!

    I thought I had to go like the documentation says:

    The generalprocedurefor a masterto accessa slavedeviceis the following:

    1. Supposea masterwantsto senddatato a slave

    • Master-transmittersendsa STARTconditionand addressesthe slave-receiver
    • Master-transmittersendsdatato slave-receiver
    • Master-transmitterterminatesthe transferwith a STOPcondition

    2. If a masterwantsto receive/readdatafroma slave:

    • Master-receiversendsa STARTconditionand addressesthe slave-transmitter
    • Master-receiversendsthe requestedregisterto readto slave-transmitter
    • Master-receiverreceivesdatafromthe slave-transmitter
    • Master-receiverterminatesthe transferwith a STOPcondition

    but it looks like the function does this for me( i checked the declaration).

    How can i write into a register, use MAP_I2C_masterSendSingleByte twice? first time with the register address and second time with the value?

    Best regards

  • Yes, the SingleByte functions are self-contained, which is handy to get you going.

    The multi-byte sequences are described in the Driverlib User's Guide Sec 12.2. They deal piecewise with the sequences you described:

    http://dev.ti.com/tirex/explore/node?node=AJIAWhC7vhw.P.ggQJeRmw__z-lQYNj__LATEST

    [I'm not quite sure why they have you call setMode during initialization, though it does no harm. The Mode (UCTR) is actually rather fluid, and is managed by the functions listed later.]

  • Hello,

    Thanks again for your answer.
    I will have a look at the documentation (had not found it yet) and mark the entry as solved.

**Attention** This is a public forum