Working with I2C

Tip / Sign in to post questions, reply, level up, and achieve exciting badges. Know more

cross mob
Anonymous
Not applicable

 This is my first I2C project. Seems like something going wrong. Slave is not receiving correct data. The code is provided below for both master and slave. Can  anyone debug

   

Master code

   

   

*/

   

#include <device.h>

   

uint8 wrData[2]={0xAA, 0xBB};

   

extern uint8 flag;

   

 

   

void main()

   

{

   

    /* Place your initialization/startup code here (e.g. MyInst_Start()) */

   

    CyGlobalIntEnable; 

   

    isr_Tx_Start();

   

    LCD_Start();

   

    I2C_Start();

   

    /* Uncomment this line to enable global interrupts. */

   

    for(;;)

   

    {

   

        if(flag==1)

   

        {

   

            flag=0;

   

           I2C_MasterClearStatus(); /* Clear any previous status */

   

            I2C_MasterWriteBuf(0x08, (uint8 *) wrData, 2, I2C_MODE_COMPLETE_XFER);

   

            for(;;)

   

            {

   

               if(0u != (I2C_MasterStatus() & I2C_MSTAT_WR_CMPLT))

   

                {

   

                /* Transfer complete. Check Master status to make sure that transfer

   

                completed without errors. */

   

                break;

   

                }

   

            } 

   

              LCD_PrintInt8(wrData[0]);  

   

          

   

        }

   

        /* Place your application code here. */

   

    }

   

}

   

 

   

/* [] END OF FILE */

   

 

   

Slave code

   

 

   

   

#include <device.h>

   

 

   

void main()

   

{

   

uint8 i;

   

uint8 wrBuf[10];

   

uint8 userArray[10];

   

uint8 byteCnt;

   

    CyGlobalIntEnable;

   

    /* Initialize write buffer before call I2C_Start */

   

    I2C_SlaveInitWriteBuf((uint8 *) wrBuf, 10);

   

    LCD_Start();

   

    /* Start I2C Slave operation */

   

    I2C_Start();

   

    /* Wait for I2C master to complete a write */

   

    for(;;) /* loop forever */

   

    {

   

        /* Wait for I2C master to complete a write */

   

        if(0u != (I2C_SlaveStatus() & I2C_SSTAT_WR_CMPLT))

   

        {

   

            byteCnt = I2C_SlaveGetWriteBufSize();

   

            I2C_SlaveClearWriteStatus();

   

            for(i=0; i < byteCnt; i++)

   

            {

   

                 userArray = wrBuf; /* Transfer data */

   

            }

   

            I2C_SlaveClearWriteBuf();

   

            LCD_PrintInt8(userArray[0]);

   

        }

   

    }

   

}

   

/* [] END OF FILE */

0 Likes
10 Replies
Bob_Marlowe
Level 10
Level 10
First like given 50 questions asked 10 questions asked

What do you mean with "correct" data? Are you receiving anything at all? What voltages are master and slave running with and what  are the pull-up resistor values?

   

By the way: I would initialize the buffers AFTER initializing the component which does not seem to be the error but sounds more logically.

   

 

   

Bob

0 Likes
Anonymous
Not applicable

The only data i receive is FF. But actiually I amtransmitting AA. I tested both master and Slave at 3.3 V and 5V. Nothing different. Resistor value usedf is 2.2k. I did probe both SCL and SDA line they show some sort of transmission but I am not sure with that. 

   

And the code is same as the one given in the data sheet of I2C component.

0 Likes
Bob_Marlowe
Level 10
Level 10
First like given 50 questions asked 10 questions asked

I would suggest to double check the transfer frequency and the IO-pin connections. And: You are transmitting TWO bytes and you should receive both of them.

   

 

   

Bob

0 Likes
Anonymous
Not applicable

I think i should be attaching my projects here, so that it will be easy for you guys to just quickly go through and debug the problem

0 Likes
lock attach
Attachments are accessible only for community members.
Anonymous
Not applicable
0 Likes
lock attach
Attachments are accessible only for community members.
Anonymous
Not applicable

I think so there is some sort of rivalry between Google chrome and Cypress forum...Nothing gets attached ........

   

Firefor is for my rescue

0 Likes
Bob_Marlowe
Level 10
Level 10
First like given 50 questions asked 10 questions asked

There is always the opportunity to use ms iinternet explorer. I suppose every function is at least tested with that browser independant of likes and dislikes.

   

 

   

Bob

0 Likes
Bob_Marlowe
Level 10
Level 10
First like given 50 questions asked 10 questions asked

I cannot see anything wrong.

   

 

   

Can you check (display) the master-status after a completed transfer.

   

instead of

   

if(0u != (I2C_MasterStatus() & I2C_MSTAT_WR_CMPLT))

   

you may write

   

uint8 Status;

   

if((Status= I2C_Masterstatus()) & I2C_MSTAT_WR_CMPLT) ...

   

and later print the var Status

   

 

   

Bob

0 Likes
Anonymous
Not applicable

   

   

Make sure that Master is writing the data after you call the MasterWriteBuf() API by using the following code snipped:

   

   

   

do

   

{

   

    temp = I2CM_MasterWriteBuf(I2C_SLAVE_ADDRESS, (uint8 *)wrBuf, sizeof(wrBuf), I2CM_MODE_COMPLETE_XFER);

   

}while (temp != I2CM_MSTR_NO_ERROR);

   

If you have any Protocol analyzer, make sure that data is sent out of I2C Master. And then we can debug the I2C Slave.

0 Likes
Anonymous
Not applicable

Were you able to obtain correct data at the slave point???

0 Likes