I2C Master/Slave Example

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

cross mob
lock attach
Attachments are accessible only for community members.
joli_601446
Level 5
Level 5
25 sign-ins 10 likes given 10 sign-ins

Hello, I'm trying to write a simple I2C program on a CY8CKIT-059. I've snapped off the KitProg and using a MiniProg to Debug my code. Here is a snap shot of the I2C Master to Slave comms:

I2C_view.jpg

My program has one Master and one EZI2C Slave module. I want the Master to transmit one byte to the Slave Buffer and the data will control and LED using the PWM module.  From the image above I see the SLAVE is ACK'ing back on the Address and the Register address but not the data word. Also, the Slave buffer is not being fill with the transmit data from the Master. I would like anyone to look over my code and maybe see something that would explain why the Slave buffer is not being filled.

#include "project.h"

#include "header.h"

#define RD_SLAVE_BUF_SIZE (0x2u)

#define BUFFER_RW_AREA_SIZE (0x2u)

#define MASTER_BUF_SIZE RD_SLAVE_BUF_SIZE

#define Wait(x)                          // Just for readability

uint8 slaveaddress = 0x8;

uint8 slavebuff[RD_SLAVE_BUF_SIZE] = {0,0};  // Buffer for EzI2C slave

uint8  masterbuff[MASTER_BUF_SIZE] = {0,0};   // Buffer for I2CM Master

uint32 master_status = 0;

void InitializeSystem(void)

{

    uint8 i = 0;

    CyGlobalIntEnable;

   

    PWM_1_Start();

    PWM_1_WriteCompare1(1);

   

    I2CS_SetBuffer1(RD_SLAVE_BUF_SIZE,  BUFFER_RW_AREA_SIZE,   slavebuff);

   

    I2CM_Start();  // start master

    I2CS_Start();   // start slave

    CyDelay(500);

   

}

int main()

{

    uint8 compare = 0;

    InitializeSystem();

    WriteCommandPacket();    

   

    if(compare != slavebuff[0])

    {

        PWM_1_WriteCompare1(slavebuff[0]);

        compare = slavebuff[0];

    }

   return 0;

}

/*******************************************************************************

* WriteCommandPacket(): Writes command packet to I2C slave

*******************************************************************************/

uint32 WriteCommandPacket(void)

{

    /* Initialize buffer with packet */

    masterbuff[0] = 0;            //    Address within EzI2C buffer

    masterbuff[1] = 200;        //    Data

    I2CM_MasterClearStatus();   

    I2CM_MasterWriteBuf(slaveaddress,  masterbuff, MASTER_BUF_SIZE, I2CM_MODE_COMPLETE_XFER);

   

    //  Waits until master completes write transfer

    while(0u == ( I2CM_MasterStatus() & I2CM_MSTAT_WR_CMPLT) )

            Wait();    //    Wait until transfer completed  

    master_status = I2CM_MasterStatus();

    I2CM_MasterClearStatus();                                            //    Usually needed only when there was an error

    return (master_status);

}

0 Likes
1 Solution
BragadeeshV
Moderator
Moderator
Moderator
First question asked 1000 replies posted 750 replies posted

Hi Joe,

I2CS_SetBuffer1 API should be given after starting the component.

I2CS_Start();

I2CS_SetBuffer1(RD_SLAVE_BUF_SIZE,  BUFFER_RW_AREA_SIZE,   slavebuff);

I tried this and it is working at my end. Please check it at your end and confirm it here.

Regards,

Bragadeesh

Regards,
Bragadeesh

View solution in original post

0 Likes
2 Replies
BragadeeshV
Moderator
Moderator
Moderator
First question asked 1000 replies posted 750 replies posted

Hi Joe,

I2CS_SetBuffer1 API should be given after starting the component.

I2CS_Start();

I2CS_SetBuffer1(RD_SLAVE_BUF_SIZE,  BUFFER_RW_AREA_SIZE,   slavebuff);

I tried this and it is working at my end. Please check it at your end and confirm it here.

Regards,

Bragadeesh

Regards,
Bragadeesh
0 Likes
lock attach
Attachments are accessible only for community members.

Hello, and thank you for responding to my post. Your suggestion solved my problem. Thank you very much.

Here's my complete program for others to see.

Thanks everyone for helping me along the way.

Joe

0 Likes