11 Replies Latest reply on Aug 5, 2014 6:27 AM by muneeb.zafar

    Code for interfacing I2C between two PSOC3 CY8C38 development kit

    mutee.urrehman

       I am trying to interfacing I2C between two PSOC3. I have written the following code 

         

      for MASTER 

         
          
           
            
             
              
               
                
                 
                  

      /*******************************************************************************
      * File Name: main.c
      *
      * Version: 1.0
      *
      * Description:
      *  This is source code for Example Project: 8-ch DelSig ADC with I2C Master.
      *
      * Parameters used:
      *  I2C Master
      *   Implementation        Fixed function
      *   Data rate            100kbps
      *   SDA SCL pin config  Open drain, drives low
      *   Pull-up resistors    2.67k each
      *
      *  Delta Sigma ADC
      *      Conversion mode     1-Multi Sample
      *    Resolution            16 bits
      *    Conversion Rate        5000 SPS
      *    Input mode            Differential
      *
      *  IDAC
      *    Polarity            Positive
      *    Range                0-31.875 uA
      *    Speed                Slow
      *    Current output        10 uA
      *
      *  Sequencing Max
      *    Mux type            Differential
      *    Channels            8
      *
      * In this project, data converted by the 8-channel, sequenced ADC is sent via
      * a I2C Master. The project is tested with a separate project containing a I2C
      * slave. The data output by the ADC is made visible on an Character LCD. This
      * can be used to verify the data received by the I2C slave which is also
      * displayed on an LCD screen.
      *
      ********************************************************************************
      * Copyright 2012, Cypress Semiconductor Corporation. All rights reserved.
      * This software is owned by Cypress Semiconductor Corporation and is protected
      * by and subject to worldwide patent and copyright laws and treaties.
      * Therefore, you may use this software only as provided in the license agreement
      * accompanying the software package from which you obtained this software.
      * CYPRESS AND ITS SUPPLIERS MAKE NO WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
      * WITH REGARD TO THIS SOFTWARE, INCLUDING, BUT NOT LIMITED TO, NONINFRINGEMENT,
      * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
      *******************************************************************************/

      #include <device.h>

      /* The I2C Slave address by default in a PSoC device is 8 */
      #define I2C_SLAVE_ADDRESS    (8u)
      /* Set the write buffer length to be 16 bits or 2 bytes */
      #define WR_BUFFER_SIZE       (5u)


      /*******************************************************************************
      * Function Name: main
      ********************************************************************************
      *
      * Summary:
      *  main function performs following functions:
      *   1. Starts all used components.
      *   2. Controls sequencing of ADC inputs.
      *   3. Reads ADC converted data, sends this data via a I2C Master.
      *   4. Displays ADC converted data on Character LCD.
      *
      * Parameters:
      *  None.
      *
      * Return:
      *  None.
      *
      *******************************************************************************/
      int main()
      {
          uint8 i;
          uint8 temp;
          uint8 sample_segment[WR_BUFFER_SIZE];
          for (i = 0; i < WR_BUFFER_SIZE; i++) 
          {
              sample_segment[i] = i;
          }
          LCD_Char_Start();
          LCD_Char_PrintString("Start1");
          
          I2CM_Start();
        

          /* The Start API for Mux sets it up, but disconnects all the channels.
           * To connect the first channel, call the 'AMuxSeq_Next' function.
           */
          

          /* The LCD will display the values read from ADC in hex */
          
          

          

          /* Enable global interrupts - required for I2C */
          CyGlobalIntEnable;
          //for(;;)
         // {
            

              /* Attempt to initiate communication with the slave until the function
               * completes without error.
               */
              do
              {
                  /* The syntax below automatically writes a buffer of data to a slave
                   * device from start to stop.
                    */
                  temp = I2CM_MasterWriteBuf(I2C_SLAVE_ADDRESS, (uint8 *)sample_segment,
                                              WR_BUFFER_SIZE, I2CM_MODE_COMPLETE_XFER);
              }
              while (temp != I2CM_MSTR_NO_ERROR);
              LCD_Char_ClearDisplay();
              LCD_Char_PrintString("Stop1! ");//debugging
              /* Wait for the data transfer to complete */
              
              while(I2CM_MasterStatus() & I2CM_MSTAT_XFER_INP);
              LCD_Char_PrintString("Stop2! ");//debugging
              temp = I2CM_MasterClearStatus();

              LCD_Char_ClearDisplay();

              /* If there is an error while transferring data */
              if(temp & I2CM_MSTAT_ERR_XFER)
              {
                  /* Indicate the error */
                  LCD_Char_PrintString("I2C Error! ");
                  /* Place error handling code here */
              }
              else /* Write completed without error */
              {
                  /* For verification purposes, display the adcReading on the LCD */
                  LCD_Char_PrintString("Transfer complete");
                  CyDelay(1000u/*ms*/);
                      LCD_Char_ClearDisplay();
                   for (i = 0; i < WR_BUFFER_SIZE; i++) 
                  {
                      LCD_Char_PrintInt8(sample_segment[i]);
                  }
                          
              }

              /* Delay introduced for ease of reading LCD */
              CyDelay(10000u/*ms*/);

          }  /* End forever loop */
      //} /* End main */

      /* [] END OF FILE */

                  

      //and for SLAVE 

                  

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

                  
                   ******************************              ********************            
                  

      * File Name: main.c
      *
      * Version: 1.0
      *
      * Description:
      *  This is source code for the 8-ch DelSig ADC and I2C Slave Example Project.
      *
      * Parameters used:
      *  I2C Slave
      *   Implementation        Fixed function
      *   Data rate            100kbps
      *   SDA SCL config         Open drain, drives low
      *   Pull-up resistors    2.67k each
      *    Address                8
      *    Address decode        Hardware
      *
      *  Delta Sigma ADC
      *      Conversion mode     1-Multi Sample
      *    Resolution            16 bits
      *    Conversion Rate        5000 SPS
      *    Input mode            Differential
      *
      *  IDAC
      *    Polarity            Positive
      *    Range                0-31.875 uA
      *    Speed                Slow
      *    Output Current        10uA
      *
      *  Sequencing Max
      *    Mux type            Differential
      *    Channels            8
      *
      * In this project, data converted by the 8-channel, sequenced ADC is sent via
      * I2C. The main project containing an I2C slave is tested with a separate
      * project containing an I2C master. The data output by the ADC is made
      * visible on an Character LCD. This can be used to verify the data
      * received by the I2C master which is also displayed on an LCD screen.
      *
      * The communication flow is as follows:
      * When the device is reset, all components are started and an initial ADC
      * reading is taken. This is stored in the read buffer of the I2C slave and
      * the ADC is stopped. Then the infinite 'for' loop begins. Only if the correct
      * number of bytes of the read buffer are read by the I2C master, a new reading
      * is taken from the ADC and the loop continues.
      *
      ******************************

                  
                   ******************************              ********************             
      * Copyright 2012, Cypress Semiconductor Corporation. All rights reserved.             
      * This software is owned by Cypress Semiconductor Corporation and is protected             
      * by and subject to worldwide patent and copyright laws and treaties.             
      * Therefore, you may use this software only as provided in the license agreement             
      * accompanying the software package from which you obtained this software.             
      * CYPRESS AND ITS SUPPLIERS MAKE NO WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,             
      * WITH REGARD TO THIS SOFTWARE, INCLUDING, BUT NOT LIMITED TO, NONINFRINGEMENT,             
      * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.             
      ******************************              ******************************              *******************/             
      #include <device.h>             
                   
      /* Set the write buffer length to be 16 bites or 2 bytes */             
      #define RD_BUFFER_SIZE        (10u)             
                   
      uint8 wrBuf[RD_BUFFER_SIZE];             
      uint8 userArray[RD_BUFFER_SIZE];             
      uint8 byteCnt;             
      uint8 i;             
      /*****************************              ******************************              ********************             
      * Function Name: main             
      ******************************              ******************************              ********************             
      *             
      * Summary:             
      *  main function performs following functions:             
      *   1. Starts all used components.             
      *   2. Controls sequencing of ADC inputs.             
      *   3. Reads ADC converted data, puts this in a read buffer to be read by the             
      *       I2C Master.             
      *   4. Displays ADC converted data on Character LCD.             
      *             
      * Parameters:             
      *  None.             
      *             
      * Return:             
      *  None.             
      *             
      ******************************              ******************************              *******************/             
      int main()             
      {             
          /* To conserve memory space, set up temporary variable for storing ADC             
           * converted values and the write buffer as a union.             
           */             
                   
          LCD_Char_Start();             
          LCD_Char_PrintString("Slave Start! ");             
          /* Initialize write buffer before call I2C_Start */             
          I2CS_SlaveInitWriteBuf((uint8 *) wrBuf, RD_BUFFER_SIZE);             
          /* Start I2C Slave operation */             
          I2CS_Start();             
      CyGlobalIntEnable;             
          /* Wait for I2C master to complete a write */             
          for(;;) /* loop forever             
          */             
          {             
          /* Wait for I2C master to complete a write */             
              if(0u != (I2CS_SlaveStatus() & I2CS_SSTAT_WR_CMPLT))             
              {             
                  byteCnt = I2CS_SlaveGetWriteBufSize();             
                  I2CS_SlaveClearWriteStatus();             
                  for(i=0; i < byteCnt; i++)             
                  {             
                      userArray[i] = wrBuf[i]; /* Transfer data */             
                  }             
                  LCD_Char_PrintString("              Transmitted! ");             
                  I2CS_SlaveClearWriteBuf();             
               }             
          }             
      } /* End of main */             
                   
                   
      /* [] END OF FILE */            
                  
                                
                  
                                
                  
                                
                  
                   The code gets compiled and also burned on Hardware but donot work properly. The master PSOC shows "I2C ERROR". Probably it is not sending  data  properly. How can I fix it ?