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: I2C issue with MSP432E401Y

Part Number: MSP432E401Y
Other Parts Discussed in Thread: MSP-EXP432E401Y, INA226

Tool/software:

Greetings.... I am struck with I2C communication using MSP432E401YT. There is no example program to start with. Then i found one I2C master example program under "ti/devices/msp432e4/driverlib/driverlib.h".
But this also not even working.

MSP is working fine . I think there is no hardware issue.. UART and analog functionalities is working fine. Please provide some examples to read and write to the particular register through I2C. Iam using Keil uvision5

Thanks in advance...

  • /* DriverLib Includes */
    #include <ti/devices/msp432e4/driverlib/driverlib.h>

    /* Standard Includes */
    #include <stdint.h>
    #include <stdbool.h>

    #include <stdlib.h>
    #include <string.h>
    #include "uartstdio.h"
    #include "pinout.h"
    #include <stdio.h>

    /* Defines for I2C State Machine */
    #define I2C_MASTER_IDLE 0x0
    #define I2C_MASTER_TX 0x1
    #define I2C_MASTER_RX 0x2
    #define I2C_MASTER_ANAK 0x3
    #define I2C_MASTER_DNAK 0x4
    #define I2C_MASTER_ALST 0x5
    #define I2C_MASTER_UNKN 0x6


    /* Defines for I2C bus parameters */
    #define SLAVE_ADDRESS 0x40 //  I2C address
    #define I2C_NUM_DATA 2
    #define TIMEOUT 10000

    uint32_t g_ui32SysClock;
    char str[100];

    // The error routine that is called if the driver library encounters an error.
    #ifdef DEBUG
    void __error__(char *pcFilename, uint32_t ui32Line) {}
    #endif

    /* Variables for I2C data and state machine */
    uint8_t sendData[I2C_NUM_DATA] = {0x04}; // Register address
    uint8_t getData[I2C_NUM_DATA] = {0x00, 0x00}; // Buffer to hold the receiving data...

    /* Timeout counter */
    uint32_t timeoutCounter;

    // Configure the UART and its pins. 
    void ConfigureUART(void)
    {
    // Enable the GPIO Peripheral used by the UART.
    MAP_SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOA);

    // Enable UART0
    MAP_SysCtlPeripheralEnable(SYSCTL_PERIPH_UART0);

    // Configure GPIO Pins for UART mode.
    MAP_GPIOPinConfigure(GPIO_PA0_U0RX);
    MAP_GPIOPinConfigure(GPIO_PA1_U0TX);
    MAP_GPIOPinTypeUART(GPIO_PORTA_BASE, GPIO_PIN_0 | GPIO_PIN_1);

    // Initialize the UART for console I/O.
    UARTStdioConfig(0, 115200, g_ui32SysClock);
    }

    int main(void)
    {


    while(1){

    uint8_t ii;
    uint32_t systemClock;

    /* Configure the system clock for 120 MHz */
    systemClock = MAP_SysCtlClockFreqSet((SYSCTL_XTAL_25MHZ | SYSCTL_OSC_MAIN |
    SYSCTL_USE_PLL | SYSCTL_CFG_VCO_480),
    120000000);
    // 120000000 50000000
    g_ui32SysClock = systemClock;

    // Configure the device pins.
    PinoutSet(false, false);

    // Enable the GPIO pins for the LED D1 (PN1).
    MAP_GPIOPinTypeGPIOOutput(GPIO_PORTN_BASE, GPIO_PIN_0);

    // Initialize the UART.
    ConfigureUART();

    /* Enable clocks to GPIO Port N and configure pins as Output */
    MAP_SysCtlPeripheralEnable(SYSCTL_PERIPH_GPION);
    while(!(MAP_SysCtlPeripheralReady(SYSCTL_PERIPH_GPION)))
    {
    }
    MAP_GPIOPinTypeGPIOOutput(GPIO_PORTN_BASE, GPIO_PIN_0);
    MAP_GPIOPinWrite(GPIO_PORTN_BASE, GPIO_PIN_0, 0);

    /* Enable clocks to GPIO Port K and configure pins as I2C */
    MAP_SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOK);
    while(!(MAP_SysCtlPeripheralReady(SYSCTL_PERIPH_GPIOK)))
    {
    }

    MAP_GPIOPinConfigure(GPIO_PK4_I2C3SCL);
    MAP_GPIOPinConfigure(GPIO_PK5_I2C3SDA);
    MAP_GPIOPinTypeI2C(GPIO_PORTK_BASE, GPIO_PIN_5);
    MAP_GPIOPinTypeI2CSCL(GPIO_PORTK_BASE, GPIO_PIN_4);

    /* Since there are no board pull up's we shall enable the weak internal pull up */
    GPIOK->PUR |= (GPIO_PIN_5 | GPIO_PIN_4);

    /* Enable the clock to I2C-3 module and configure the I2C Master */
    MAP_SysCtlPeripheralEnable(SYSCTL_PERIPH_I2C3);
    while(!(MAP_SysCtlPeripheralReady(SYSCTL_PERIPH_I2C3)))
    {
    }

    /* Configure the I2C Master in standard mode */
    MAP_I2CMasterInitExpClk(I2C3_BASE, systemClock, false);

    /* Initialize the variables for Tx */
    uint8_t setI2CState = I2C_MASTER_TX;
    uint8_t dataIndex = 0;

    /* Put the Slave Address on the bus for Write */
    MAP_I2CMasterSlaveAddrSet(I2C3_BASE, SLAVE_ADDRESS, false); // false for write and true for read

    /* Write the register address to the bus */
    MAP_I2CMasterDataPut(I2C3_BASE, sendData[dataIndex]);
    MAP_I2CMasterControl(I2C3_BASE, I2C_MASTER_CMD_BURST_SEND_START);

    /* Wait for the transmission to complete with timeout */
    timeoutCounter = 0;
    while(MAP_I2CMasterBusy(I2C3_BASE))
    {
    UARTprintf("TRANSFERRING\n");
    MAP_SysCtlDelay(1000); // Adjust delay as needed
    if (++timeoutCounter > TIMEOUT)
    {
    UARTprintf("TRANSFER TIMEOUT\n");
    return 0;
    }
    }

    /* Check for errors */
    if (MAP_I2CMasterErr(I2C3_BASE) != I2C_MASTER_ERR_NONE)
    {
    UARTprintf("TRANSFER ERROR\n");
    return 0;
    }

    /* Short delay */
    MAP_SysCtlDelay(3000);

    /* Initialize the variables for Rx */
    setI2CState = I2C_MASTER_RX;
    dataIndex = 0;

    /* Put the Slave Address on the bus for Read */
    MAP_I2CMasterSlaveAddrSet(I2C3_BASE, SLAVE_ADDRESS, true);

    /* Start the reception of data from the Slave */
    MAP_I2CMasterControl(I2C3_BASE, I2C_MASTER_CMD_BURST_RECEIVE_START);

    /* Wait for the reception to complete with timeout */
    timeoutCounter = 0;
    while(MAP_I2CMasterBusy(I2C3_BASE))
    {
    UARTprintf("TRANSFERRING\n");
    MAP_SysCtlDelay(1000); // Adjust delay as needed
    if (++timeoutCounter > TIMEOUT)
    {
    UARTprintf("TRANSFER TIMEOUT\n");
    return 0;
    }
    }

    /* Check for errors */
    if (MAP_I2CMasterErr(I2C3_BASE) != I2C_MASTER_ERR_NONE)
    {
    UARTprintf("TRANSFER ERROR\n");
    return 0;
    }

    /* Read received data */
    getData[dataIndex++] = MAP_I2CMasterDataGet(I2C3_BASE);

    getData[1] = MAP_I2CMasterDataGet(I2C3_BASE);

    /* Print received data via UART */
    for(ii = 0; ii < I2C_NUM_DATA; ii++)
    {
    UARTprintf("Received byte %d: 0x%02X\n", ii, getData[ii]);
    }


    uint16_t busVoltageRaw = (getData[0] << 8) | getData[1];




    MAP_SysCtlDelay(g_ui32SysClock / 3);


    }
    return 0;
    }



    This is my code in keil uvision 5 ,which reads the low byte correctly but incorrect reading of the high byte.

    Please help ASAP...

    Thanks in advance...

  • I am struck with I2C communication using MSP432E401YT. There is no example program to start with. Then i found one I2C master example program under "ti/devices/msp432e4/driverlib/driverlib.h".
    But this also not even working.

    Hi,

      There are quite a few I2C examples. Can you start with the i2c_mastermode_simple_transfer. The use of the example requires another MSP-EXP432E401Y board to be running the i2c_slavemode_simple_transfer application. Also make sure you have proper pull resistors on SDA and SCL buses. 

  • getData[1] = MAP_I2CMasterDataGet(I2C3_BASE);

    I think you need to precede this with something like:

    > MAP_I2CMasterControl(I2C3_BASE, I2C_MASTER_CMD_BURST_RECEIVE_CONT); // Rx another

    while(MAP_I2CMasterBusy(I2C3_BASE))/*EMPTY*/; // Next Rx byte ready?

    and follow it with

    > MAP_I2CMasterControl(I2C3_BASE, I2C_MASTER_CMD_BURST_RECEIVE_FINISH); // Done

    This sequence appears in the example Charles suggested, but it's buried rather deep.

    [Edit: Ref also TRM (SLAU723A) Fig 19-11]

  • Thank a lot for your reply...Please see my below code , I've made changes as per your suggestion. But then also sometimes I am getting correct results sometimes not. Please help me to solve this. 

    Thanks in advance...

    /* DriverLib Includes */
    #include <ti/devices/msp432e4/driverlib/driverlib.h>

    /* Standard Includes */
    #include <stdint.h>
    #include <stdbool.h>
    #include <stdio.h>
    #include "uartstdio.h"
    #include "pinout.h"

    /* Defines for I2C State Machine */
    #define I2C_MASTER_IDLE 0x0
    #define I2C_MASTER_TX 0x1
    #define I2C_MASTER_RX 0x2
    #define I2C_MASTER_ANAK 0x3
    #define I2C_MASTER_DNAK 0x4
    #define I2C_MASTER_ALST 0x5
    #define I2C_MASTER_UNKN 0x6

    /* Defines for I2C bus parameters */
    #define SLAVE_ADDRESS 0x44 // I2C address
    #define I2C_NUM_DATA 2 // Number of bytes to read
    #define TIMEOUT 10000 // Timeout value in iterations

    uint32_t g_ui32SysClock;

    /* Variables for I2C data and state machine */
    uint8_t sendData[I2C_NUM_DATA] = {0x00}; // Register address
    uint8_t getData[I2C_NUM_DATA] = {0x00}; // Buffer to hold the receiving data

    /* Timeout counter */
    uint32_t timeoutCounter;

    /* Configure the UART and its pins. This must be called before UARTprintf(). */
    void ConfigureUART(void)
    {
    /* Enable the GPIO Peripheral used by the UART. */
    MAP_SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOA);

    /* Enable UART0 */
    MAP_SysCtlPeripheralEnable(SYSCTL_PERIPH_UART0);

    /* Configure GPIO Pins for UART mode. */
    MAP_GPIOPinConfigure(GPIO_PA0_U0RX);
    MAP_GPIOPinConfigure(GPIO_PA1_U0TX);
    MAP_GPIOPinTypeUART(GPIO_PORTA_BASE, GPIO_PIN_0 | GPIO_PIN_1);

    /* Initialize the UART for console I/O. */
    UARTStdioConfig(0, 115200, g_ui32SysClock);
    }

    int main(void)
    {
    uint8_t ii;
    uint32_t systemClock;

    /* Configure the system clock for 120 MHz */
    systemClock = MAP_SysCtlClockFreqSet((SYSCTL_XTAL_25MHZ | SYSCTL_OSC_MAIN |
    SYSCTL_USE_PLL | SYSCTL_CFG_VCO_480),
    120000000);

    while (1){

    g_ui32SysClock = systemClock;

    /* Configure the device pins. */
    PinoutSet(false, false);

    /* Initialize the UART. */
    ConfigureUART();

    /* Enable clocks to GPIO Port N and configure pins as Output */
    MAP_SysCtlPeripheralEnable(SYSCTL_PERIPH_GPION);
    while (!(MAP_SysCtlPeripheralReady(SYSCTL_PERIPH_GPION)));

    MAP_GPIOPinTypeGPIOOutput(GPIO_PORTN_BASE, GPIO_PIN_0);
    MAP_GPIOPinWrite(GPIO_PORTN_BASE, GPIO_PIN_0, 0);

    /* Enable clocks to GPIO Port K and configure pins as I2C */
    MAP_SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOK);
    while (!(MAP_SysCtlPeripheralReady(SYSCTL_PERIPH_GPIOK)));

    MAP_GPIOPinConfigure(GPIO_PK4_I2C3SCL);
    MAP_GPIOPinConfigure(GPIO_PK5_I2C3SDA);
    MAP_GPIOPinTypeI2C(GPIO_PORTK_BASE, GPIO_PIN_5);
    MAP_GPIOPinTypeI2CSCL(GPIO_PORTK_BASE, GPIO_PIN_4);

    /* Enable the clock to I2C-3 module and configure the I2C Master */
    MAP_SysCtlPeripheralEnable(SYSCTL_PERIPH_I2C3);
    while (!(MAP_SysCtlPeripheralReady(SYSCTL_PERIPH_I2C3)));

    /* Configure the I2C Master in standard mode */
    MAP_I2CMasterInitExpClk(I2C3_BASE, systemClock, false);

    /* Initialize the variables for I2C operation */
    uint8_t dataIndex = 0;

    /* Transmit the register address or command */
    MAP_I2CMasterSlaveAddrSet(I2C3_BASE, SLAVE_ADDRESS, false);
    MAP_I2CMasterDataPut(I2C3_BASE, sendData[dataIndex]);
    MAP_I2CMasterControl(I2C3_BASE, I2C_MASTER_CMD_SINGLE_SEND);

    /* Wait for the transmission to complete */
    timeoutCounter = 0;
    while (MAP_I2CMasterBusy(I2C3_BASE))
    {
    MAP_SysCtlDelay(1000); // Adjust delay as needed
    if (++timeoutCounter > TIMEOUT)
    {
    UARTprintf("TRANSFER TIMEOUT\n");
    return 0;
    }
    }

    /* Check for errors */
    if (MAP_I2CMasterErr(I2C3_BASE) != I2C_MASTER_ERR_NONE)
    {
    UARTprintf("TRANSFER ERROR\n");
    return 0;
    }

    /* Short delay */
    MAP_SysCtlDelay(3000);

    /* Receive data from the device */
    MAP_I2CMasterSlaveAddrSet(I2C3_BASE, SLAVE_ADDRESS, true);
    if (I2C_NUM_DATA > 1)
    {
    UARTprintf("SDA has more than single byte ..........\n");
    /* Initiate multi-byte read */
    MAP_I2CMasterControl(I2C3_BASE, I2C_MASTER_CMD_BURST_RECEIVE_START);

    /* Wait for the reception to complete */
    timeoutCounter = 0;
    while (MAP_I2CMasterBusy(I2C3_BASE))
    {
    MAP_SysCtlDelay(1000); // Adjust delay as needed
    if (++timeoutCounter > TIMEOUT)
    {
    UARTprintf("TRANSFER TIMEOUT\n");
    return 0;
    }
    }

    /* Check for errors */
    if (MAP_I2CMasterErr(I2C3_BASE) != I2C_MASTER_ERR_NONE)
    {
    UARTprintf("TRANSFER ERROR\n");
    return 0;
    }

    /* Read received data */
    getData[dataIndex++] = MAP_I2CMasterDataGet(I2C3_BASE);

    // getData[1] = MAP_I2CMasterDataGet(I2C3_BASE);

    /* Continue reading if more than one byte is expected */
    for (ii = 1; ii < I2C_NUM_DATA; ii++)
    {
    MAP_I2CMasterControl(I2C3_BASE, I2C_MASTER_CMD_BURST_RECEIVE_CONT);
    while (MAP_I2CMasterBusy(I2C3_BASE))
    {
    MAP_SysCtlDelay(1000); // Adjust delay as needed
    if (++timeoutCounter > TIMEOUT)
    {
    UARTprintf("TRANSFER TIMEOUT\n");
    return 0;
    }
    }

    /* Check for errors */
    if (MAP_I2CMasterErr(I2C3_BASE) != I2C_MASTER_ERR_NONE)
    {
    UARTprintf("TRANSFER ERROR\n");
    return 0;
    }

    /* Read received data */
    getData[dataIndex++] = MAP_I2CMasterDataGet(I2C3_BASE);
    }

    /* Finish the multi-byte read */
    MAP_I2CMasterControl(I2C3_BASE, I2C_MASTER_CMD_BURST_RECEIVE_FINISH);
    while (MAP_I2CMasterBusy(I2C3_BASE))
    {
    MAP_SysCtlDelay(1000); // Adjust delay as needed
    if (++timeoutCounter > TIMEOUT)
    {
    UARTprintf("TRANSFER TIMEOUT\n");
    return 0;
    }
    }

    /* Check for errors */
    if (MAP_I2CMasterErr(I2C3_BASE) != I2C_MASTER_ERR_NONE)
    {
    UARTprintf("TRANSFER ERROR\n");
    return 0;
    }

    /* Read received data */
    getData[dataIndex++] = MAP_I2CMasterDataGet(I2C3_BASE);
    }
    else
    {
    UARTprintf("SDA has single byte ..........");
    /* Single-byte read */
    MAP_I2CMasterControl(I2C3_BASE, I2C_MASTER_CMD_SINGLE_RECEIVE);
    while (MAP_I2CMasterBusy(I2C3_BASE))
    {
    MAP_SysCtlDelay(1000); // Adjust delay as needed
    if (++timeoutCounter > TIMEOUT)
    {
    UARTprintf("TRANSFER TIMEOUT\n");
    return 0;
    }
    }

    /* Check for errors */
    if (MAP_I2CMasterErr(I2C3_BASE) != I2C_MASTER_ERR_NONE)
    {
    UARTprintf("TRANSFER ERROR\n");
    return 0;
    }

    /* Read received data */
    getData[dataIndex++] = MAP_I2CMasterDataGet(I2C3_BASE);
    }

    /* Print received data via UART */
    for (ii = 0; ii < I2C_NUM_DATA; ii++)
    {
    UARTprintf("Received byte %d: 0x%02X\n", ii, getData[ii]);
    }

    /* Calculate bus voltage */
    uint16_t busVoltageRaw = (getData[0] << 8) | getData[1];
    float busVoltage = busVoltageRaw * 1.25; // INA226 bus voltage LSB is 1.25 mV

    UARTprintf("Bus Voltage: %.2f mV\n", busVoltage);


    MAP_SysCtlDelay(g_ui32SysClock / 3);

    }

    return 0;
    }

  • Thanks a lot for your reply...
    But I am not having two MSP's....