2 Replies Latest reply on Oct 15, 2018 9:05 AM by user_80002741

    I2C Master/Slave Example

    user_80002741

      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);

      }