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.

Sending ADC Data through I2C Communication between TM4C123GHPM launchpad & TM4C129XNCZAD Microcontroller

Other Parts Discussed in Thread: TM4C129XNCZAD, TM4C123GH6PM

Hi, Iam trying to send the ADC Data of TM4C123 launchpad through I2C Communication on TM4C129XNCZAD microcontroller and want to display it on my touch TFT (TFT is driven by TM4C129XNCZAD microcontroller). I've made the TFT as the Master & the launchpad as the Slave. Master will ask the data from the slave when required. Iam not able to workout the code for proper communication. below is my code for both master and slave. Please help.

*******************************Master code**************************


#include <stdint.h>
#include <stdbool.h>
#include <stdlib.h>

#include "grlib/grlib.h"
#include "grlib/widget.h"
#include "grlib/canvas.h"
#include "grlib/pushbutton.h"
#include "grlib/radiobutton.h"
#include "drivers/frame.h"
#include "drivers/pinout.h"
#include "drivers/touch.h"

/* Raster Specific Changes */
#include "drivers/grlib_raster_driver_16bpp.h"
#include "drivers/raster_displays.h"
#include "drivers/sdram.h"

#include "driverlib/rom.h"
#include "driverlib/sysctl.h"
#include "driverlib/systick.h"
#include "driverlib/uart.h"
#include "driverlib/debug.h"
#include "driverlib/gpio.h"
#include "driverlib/interrupt.h"
#include "driverlib/pin_map.h"
#include "driverlib/timer.h"
#include "driverlib/eeprom.h"
#include "driverlib/rom_map.h"
#include "driverlib/udma.h"
#include "driverlib/interrupt.h"
#include "driverlib/lcd.h"
#include "driverlib/i2c.h"
#include "inc/hw_types.h"
#include "inc/hw_sysctl.h"
#include "inc/hw_memmap.h"
#include "inc/hw_gpio.h"
#include "inc/hw_ints.h"
#include "inc/hw_lcd.h"

#include "utils/uartstdio.h"
#include "utils/ustdlib.h"

/* fatfs */
#include "fatfs/src/ff.h"
#include "fatfs/src/diskio.h"

/* custom */
#include "homeUI.h"

/* local RTC */
#include <time.h>


extern volatile uint32_t receive;
int main(Void)
{

    /* Setup System Clock */

    ui32SysClock = SysCtlClockFreqSet((SYSCTL_OSC_INT | SYSCTL_USE_PLL |
                                         g_psDisplayMode->ui32VCOFrequency),
                                        g_psDisplayMode->ui32SysClockFrequency);
    g_ui32SysClock = ui32SysClock;


    SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOG);
     while(!(SysCtlPeripheralReady(SYSCTL_PERIPH_GPIOG)));

    SysCtlPeripheralDisable(SYSCTL_PERIPH_I2C2);
    SysCtlPeripheralReset(SYSCTL_PERIPH_I2C2);
    SysCtlPeripheralEnable(SYSCTL_PERIPH_I2C2);
    while(!(SysCtlPeripheralReady(SYSCTL_PERIPH_I2C2)));

        GPIOPinConfigure(GPIO_PG2_I2C2SCL);

        GPIOPinConfigure(GPIO_PG3_I2C2SDA);

        GPIOPinTypeI2CSCL(GPIO_PORTG_BASE, GPIO_PIN_2);

        GPIOPinTypeI2C(GPIO_PORTG_BASE, GPIO_PIN_3);

        I2CMasterInitExpClk(I2C2_BASE, SysCtlClockGet(), false);

        IntEnable(INT_I2C2);
        I2CMasterIntEnableEx(I2C2_BASE, I2C_MASTER_INT_DATA);
        IntMasterEnable();

        I2CMasterSlaveAddrSet(I2C2_BASE, 0x3C, true);

        I2CMasterControl(I2C2_BASE, I2C_MASTER_CMD_SINGLE_RECEIVE);

        while(1)
        {
                I2CMasterSlaveAddrSet(I2C2_BASE, SLAVE_ADDRESS, true);
                I2CMasterControl(I2C2_BASE, I2C_MASTER_CMD_SINGLE_RECEIVE);
                while(I2CMasterBusy(I2C2_BASE));
        }

    /* Initialize Peripherals */
    PinoutSet();

    /* Configure Debug UART */
    //configureDebugUart();

    /*  Initialization of SD Card Stack */
    /*
    SysTickPeriodSet(ui32SysClock / 100);
    SysTickEnable();
    SysTickIntEnable();
    */

    g_pui16SDRAM = SDRAMInit(ui32SysClock);

    if(!g_pui16SDRAM)
    {
       UARTprintf("Application requires SDRAM but this is not present!\n");
       while(1);
    }

    g_pui32DisplayBuffer = (uint32_t *)g_pui16SDRAM;
    g_pui16Palette = (uint16_t *)g_pui16SDRAM;


    /* Launch GUI */
    gui_Init();  /* function for customized GUI*/


    /* Super Loop */
    while(1)
    {
        /* Check for System Status Icons */

        /* Take a break, Have a KitKat */
        SysCtlDelay(ui32SysClock/100);

        /* Handle UI Inputs */
        WidgetMessageQueueProcess();
    }
}
void I2C2MasterIntHandler(void)
{

    // Clear the I2C0 interrupt flag.
        I2CMasterIntClear(I2C2_BASE);
        // Read the data from the slave.
        receive = I2CMasterDataGet(I2C2_BASE);
    }

****** customized gui code***********

#include "common.h"
#include "homeUI.h"
#include "pixbuf.h"
#include "driverlib/sysctl.h"
#include "inc/hw_sysctl.h"
#include "grlib/Container.h"

extern volatile uint32_t ui32SysClock;

extern volatile uint32_t receive;
void gui_Init()
{


    /* Initialize Graphics Driver */
    DisplayInit(ui32SysClock);

    GrRaster16BppDriverInit(g_pui32DisplayBuffer);
    GrContextInit(&guiMainContext, &g_sGrRaster16BppDriver);

    FillScreen(ClrBlack);

    TouchScreenInit(ui32SysClock);
    TouchScreenCallbackSet(WidgetPointerMessage);
    WidgetAdd(WIDGET_ROOT, (tWidget *)&guiBackGround);
    //WidgetAdd(WIDGET_ROOT, (tWidget *)&guiAcquisitionPage);
    WidgetPaint(WIDGET_ROOT);
    WidgetMessageQueueProcess();

    void OnIntroPaint(tWidget *psWidget, tContext *psContext);
   // void OnSignal(tWidget *psWidget);
   // void OnAcquisitionPaint(tWidget *psWidget, tContext *psContext);
}

Canvas(guiBackGround, WIDGET_ROOT, 0, 0, &g_sGrRaster16BppDriver,
       X_OFFSET, Y_OFFSET, 800, 480,
       CANVAS_STYLE_APP_DRAWN,
                                  0, 0, 0, 0, 0, 0, OnIntroPaint);


void OnIntroPaint(tWidget *psWidget, tContext *psContext)
{
    
    char sensorValue[10];

    usprintf(sensorValue,"%2d",receive );

    GrContextFontSet(psContext, g_psFontCmss32b);
                GrContextForegroundSet(psContext, ClrCyan);
                GrStringDraw(psContext, "Heart Rate = ", -1,
                                             250, 250, 0);
                GrStringDraw(psContext, sensorValue, 10,
                                 500, 250, 0);
                WidgetPaint(WIDGET_ROOT);

}

**************slave code*********************

 

/*
 * main.c
 */

#include <stdint.h>
#include <stdbool.h>
#include <stdlib.h>
#include "driverlib/sysctl.h"
#include "driverlib/debug.h"
#include "driverlib/gpio.h"
#include "driverlib/adc.h"
#include "inc/hw_types.h"
#include "inc/hw_sysctl.h"
#include "inc/hw_memmap.h"
#include "inc/hw_ssi.h"
#include "inc/tm4c123gh6pm.h"
#include "inc/hw_adc.h"
#include "inc/hw_gpio.h"
#include "inc/hw_ints.h"
#include "driverlib/i2c.h"
#include "driverlib/pin_map.h"
#include "driverlib/uart.h"
#include "utils/uartstdio.h"
#include "driverlib/ssi.h"
#include "utils/ustdlib.h"
#include "driverlib/interrupt.h"


#ifdef DEBUG
void_error_(char *pcFilename, unsigned long u1Line)
{

}
#endif

extern volatile unsigned long adcResult = 0;


int main(void)
{
    uint32_t ui32ADC0Value[1];
    SysCtlClockSet(SYSCTL_SYSDIV_5|SYSCTL_USE_PLL|SYSCTL_OSC_MAIN|SYSCTL_XTAL_16MHZ);


    SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOB);
    while(!(SysCtlPeripheralReady(SYSCTL_PERIPH_GPIOB)));
    SysCtlPeripheralDisable(SYSCTL_PERIPH_I2C0);
    SysCtlPeripheralReset(SYSCTL_PERIPH_I2C0);
    SysCtlPeripheralEnable(SYSCTL_PERIPH_I2C0);
    while(!(SysCtlPeripheralReady(SYSCTL_PERIPH_I2C0)));


                GPIOPinConfigure(GPIO_PB2_I2C0SCL);
                GPIOPinConfigure(GPIO_PB3_I2C0SDA);

                GPIOPinTypeI2CSCL(GPIO_PORTB_BASE, GPIO_PIN_2);

                    GPIOPinTypeI2C(GPIO_PORTB_BASE, GPIO_PIN_3);

                  I2CSlaveEnable(I2C0_BASE);
                I2CSlaveInit(I2C0_BASE, 0x3C);

    SysCtlPeripheralEnable(SYSCTL_PERIPH_ADC0);
    GPIOPinTypeADC(GPIO_PORTB_BASE,GPIO_PIN_5);
        ADCSequenceDisable(ADC0_BASE, 3);
        ADCSequenceConfigure(ADC0_BASE, 3, ADC_TRIGGER_PROCESSOR, 0);

        ADCSequenceStepConfigure(ADC0_BASE,3,0,ADC_CTL_CH11|ADC_CTL_IE|ADC_CTL_END);

        ADCSequenceEnable(ADC0_BASE, 3);

        
            ADCIntClear(ADC0_BASE, 3);
            while(1)
            {
            ADCProcessorTrigger(ADC0_BASE, 3);
            while(!ADCIntStatus(ADC0_BASE, 3, false))
            {
            }
            ADCSequenceDataGet(ADC0_BASE, 3, ui32ADC0Value);
            adcResult= ui32ADC0Value[0];

            IntEnable(INT_I2C0);

            I2CSlaveIntEnableEx(I2C0_BASE, I2C_SLAVE_INT_DATA);

            IntMasterEnable();

            while(!(I2CSlaveStatus(I2C0_BASE) & I2C_SLAVE_ACT_TREQ));

            I2CSlaveDataPut(I2C0_BASE, adcResult);

            }


}


void I2C0SlaveIntHandler(void)
{
    I2CSlaveIntClear(I2C0_BASE);
        // Read the data from the slave.
    adcResult = I2CSlaveDataGet(I2C0_BASE);

}


  • Sumit,

    Is the I2C communication between 2 TM4C129 MCUs, or between the TM4C129x MCU and the LCD? Does the LCD itself support I2C master? Both your master code and slave code are for TM4C MCU.

    Regards,
    QJ
  • Hi QJ,

    Thanks for replying to my post!!

    The I2C Communication is between the TM4C123 Launchpad & TFT which is based on TM4C129x MCU.
    Iam using one of the TM4C123 MCU of the launchpad as JTAG Debugger for the TFT and Iam using the second TM4C123 MCU of the launchpad as my slave MCU .
    I've purchased the TFT through a vendor which makes these TFT's based on TM4C129x MCU.


    Also I've connected external pullup resistors of 4.7k on both SDA & SCL lines.

  • Hi QJ,

    kindly suggest a solution
  • I was just looking at the master code. In that you are starting off with first slave addr set itself with true. You should first write the slave address (so it is false in I2CMasterSlaveAddrSet. You should also do a data put with the register number in the slave from where you want to extract data. Then you do I2CMasterControl with send start. After grabbing the slave, you can read data. Below is sample code:

    I2CMasterSlaveAddrSet(I2C7_BASE,SLAVE_ADDR,false); // master to slave establish communication
    I2CMasterDataPut(I2C7_BASE,AccelX); // select x-accelerometer register in slave to read
    I2CMasterControl(I2C7_BASE,I2C_MASTER_CMD_BURST_SEND_START); // start and send
    SysCtlDelay(1000);
    while(I2CMasterBusy(I2C7_BASE)); // wait for MCU to finish transaction
    if (I2CMasterErr(I2C7_BASE)){
    UARTprintf("error");
    return;
    }

    I2CMasterSlaveAddrSet(I2C7_BASE,SLAVE_ADDR,true); // ready to receive Accelerometer x-axis data from slave
    I2CMasterControl(I2C7_BASE,I2C_MASTER_CMD_BURST_RECEIVE_START); // receive data
    SysCtlDelay(1000);
    while(I2CMasterBusy(I2C7_BASE)); // wait for MCU to finish transaction
    if (I2CMasterErr(I2C7_BASE)){
    UARTprintf("error");
    return;
    }
    val1 = I2CMasterDataGet(I2C7_BASE); // move Accelerometer x-axis high data to variable
    UARTprintf("X_Hi = %x,", val1);