Code for interfacing I2C between two PSOC3 CY8C38 development kit

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

cross mob
Anonymous
Not applicable

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

        /* 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 = wrBuf; /* 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 ?            
            

 

            

 

            
                          
           
          
         
         
                    
        
        
                  
       
       
        
         
                    
        
       
      
     
    
   
   
    
     
      
       
        
         
                    
        
       
      
     
    
   
0 Likes
11 Replies
Anonymous
Not applicable

What are you expecting from the I2C when compareing with  (tenp != I2CM_MSTR_NO_ERROR) in your master main.c?

   

   

}while (temp != I2CM_MSTR_NO_ERROR);
 

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

As a general rule of thumb: It is always advisable for us to have the complete project(s) here to check for any errors. Assume you have choosen different transfer rates for the two I2Cs, we will never see that. On the other hand, some of us might be able to compile and check your program with minimum efford, quickly getting near the target. To upload a complete project , use Creator -> File -> Create Workspace Bundle (minimal) and then upload the resulting file. Do NOT use chrome, will not work, use ie instead.

   

 

   

Bob

0 Likes
Anonymous
Not applicable
        Im Mutee's partner and this is the shorter version (minimal workspace) of the code sent by Mutee. I want to tell that i am totally new at this and this is my first task to communicate two PSOC's with each other using I2C interface. I also do not know if i have written the code correctly or not, i used help from internet and your forums. Talking about the software part, the master code runs and ends up at displaying on the LCD "I2C Error" as will be seen in the code. The slave if believe does not even enter the forever for loop as LCD keeps on displaying "Slave Start". For the hardware, i am connecting P12[0] and P12[1] of both PSOC's together with pull up resistors in between and i am giving the masters PSOC 5V port as Vcc to the SDA SCL lines. Please help to what i am doing wrong.   
0 Likes
Anonymous
Not applicable
0 Likes
Anonymous
Not applicable
0 Likes
Anonymous
Not applicable

Sorry for the blank posts. I was trying to upload with IE but it didn't work.

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

Try to switch on compatibility mode in IE to get the "normal" view of the forum post window.

   

 

   

Bob

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

Look at your I2C datasheet Slave Functions page 14. I think you mixed up Read- and Write buffers.

   

 

   

Bob

0 Likes
Anonymous
Not applicable

Thank you for the help, I did edit the buffers and I did have success once, the slave received successfully but soon as I changed the delays, the code did not work again and it is not working since. Again Master ends up at "I2C Error" and Slave only displays "Slave Start".

0 Likes
Anonymous
Not applicable
0 Likes