3 Replies Latest reply on Aug 18, 2017 1:58 PM by jaeyeon.kihm

    PSoC4 I2C Communication

    jaeyeon.kihm

      Hello folks,

         

       

         

      I've a question regarding I2C communication in CY8CKIT-042 pioneer board.

         

      What I want to do is a simple I2C communication; Write a byte and read a value from slave. It could be programmed in Arduino like below.

         

      #include <Wire.h>

         

      const int address = 0x40;

         

       

         

      void setup()

         

      {

         

        // open the serial port

         

        Serial.begin(57600);

         

        // Start the I2C module

         

        Wire.begin();

         

      }

         

       

         

      void loop()

         

      {

         

        // start a transmission to the slave

         

        Wire.beginTransmission(address);

         

        // request register 0

         

        Wire.write((byte)0);

         

        // issue the request

         

        Wire.endTransmission();

         

        // prepare to receive on byte

         

        Wire.requestFrom(address, 1);

         

        // wait for the data to be ready

         

        while(!Wire.available());

         

        // print the temperature

         

        Serial.println(Wire.read(),DEC);

         

        delay(1000);

         

      }

         

       

         

       

         

      I started this program with an example project, I2cCommMaster. When I try this project with my commands and slave address, I found that the master couldn't complete write, so my program holds here;

         

      [main.c @ line:85]

         

      while (0u == (I2CM_I2CMasterStatus() & I2CM_I2C_MSTAT_WR_CMPLT))
      {
          /* Waits until master completes write transfer */
      }

         

       

         

      Could you guys help me to make this code run? I attach all my code below for your information.

         

       

         

      Thanks for your help in advance.

         

       

         
            
      1.      
               <main.       c       >     
      2.     
      3.      
                    
      4.     
      5.      
               /*******************************************************************************     
      6.     
      7.      
               * File Name: main.c     
      8.     
      9.      
               *     
      10.     
      11.      
               ********************************************************************************     
      12.     
      13.      
               * Copyright 2014, Cypress Semiconductor Corporation. All rights reserved.     
      14.     
      15.      
               * This software is owned by Cypress Semiconductor Corporation and is protected     
      16.     
      17.      
               * by and subject to worldwide patent and copyright laws and treaties.     
      18.     
      19.      
               * Therefore, you may use this software only as provided in the license agreement     
      20.     
      21.      
               * accompanying the software package from which you obtained this software.     
      22.     
      23.      
               * CYPRESS AND ITS SUPPLIERS MAKE NO WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,     
      24.     
      25.      
               * WITH REGARD TO THIS SOFTWARE, INCLUDING, BUT NOT LIMITED TO, NONINFRINGEMENT,     
      26.     
      27.      
               * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.     
      28.     
      29.      
               *******************************************************************************/     
      30.     
      31.      
                    
      32.     
      33.      
               #include "project.h"     
      34.     
      35.      
               #include "header.h"     
      36.     
      37.      
                    
      38.     
      39.      
                    
      40.     
      41.      
               /*******************************************************************************     
      42.     
      43.      
               * Function Name: main     
      44.     
      45.      
               ********************************************************************************     
      46.     
      47.      
               * Summary:     
      48.     
      49.      
               *  The main function performs the following actions:     
      50.     
      51.      
               *   1. Turns off the RGB LED.     
      52.     
      53.      
               *   2. Starts the I2C master (SCB mode) component.     
      54.     
      55.      
               *   3. Enables global interrupts.     
      56.     
      57.      
               *   4. Sends command and status packets to the slave every 500mS.     
      58.     
      59.      
               *     
      60.     
      61.      
               * Parameters:     
      62.     
      63.      
               *  None     
      64.     
      65.      
               *     
      66.     
      67.      
               * Return:     
      68.     
      69.      
               *  None     
      70.     
      71.      
               *     
      72.     
      73.      
               *******************************************************************************/     
      74.     
      75.      
               void main       (       )     
      76.     
      77.      
               {     
      78.     
      79.      
                         uint8 command        = CMD_MSR_REL_HUM_NO_HOLD_MASTER       ;     
      80.     
      81.      
                    
      82.     
      83.      
                  RGB_LED_OFF       ;     
      84.     
      85.      
                      
      86.     
      87.      
                  I2CM_Start       (       )       ;     
      88.     
      89.      
                      
      90.     
      91.      
                    
      92.     
      93.      
                  CyGlobalIntEnable       ;     
      94.     
      95.      
                    
      96.     
      97.      
                         /***************************************************************************     
      98.     
      99.      
                   * Main polling loop     
      100.     
      101.      
                   ***************************************************************************/     
      102.     
      103.      
                         for       (       ;;       )     
      104.     
      105.      
                         {     
      106.     
      107.      
                          
      108.     
      109.      
                             if        (0u        == WriteCommandPacket       (command       )       )     
      110.     
      111.      
                             {     
      112.     
      113.      
                              
      114.     
      115.      
                                 if        (0u        == ReadStatusPacket       (       )       )     
      116.     
      117.      
                                 {     
      118.     
      119.      
                              RGB_LED_OFF       ;                       /* Turn off status LED  */     
      120.     
      121.      
                                 }     
      122.     
      123.      
                             }     
      124.     
      125.      
                    
      126.     
      127.      
                      CyDelay       (500u       )       ;        /* Send command to the I2C slave every 500mS */     
      128.     
      129.      
                         }     
      130.     
      131.      
               }     
      132.     
      133.      
                    
      134.     
      135.      
                    
      136.     
      137.      
               /*******************************************************************************     
      138.     
      139.      
               * WriteCommandPacket(): Writes command packet to I2C slave     
      140.     
      141.      
               *******************************************************************************/     
      142.     
      143.      
               uint32 WriteCommandPacket       (       uint8 cmd       )     
      144.     
      145.      
               {     
      146.     
      147.      
                         uint8  buffer       [BUFFER_SIZE       ]       ;     
      148.     
      149.      
                         uint32 status        = TRANSFER_ERROR       ;     
      150.     
      151.      
                    
      152.     
      153.      
                         /* Initialize buffer with packet */     
      154.     
      155.      
                  buffer       [PACKET_SOP_POS       ]        = PACKET_SOP       ;     
      156.     
      157.      
                  buffer       [PACKET_CMD_POS       ]        = cmd       ;     
      158.     
      159.      
                  buffer       [PACKET_EOP_POS       ]        = PACKET_EOP       ;     
      160.     
      161.      
                    
      162.     
      163.      
                         (       void       ) I2CM_I2CMasterWriteBuf       (I2C_SLAVE_ADDR       , buffer       , PACKET_SIZE       , I2CM_I2C_MODE_COMPLETE_XFER       )       ;     
      164.     
      165.      
                         //I2CM_I2CMasterReadBuf(I2C_SLAVE_ADDR, buffer, PACKET_SIZE, I2CM_I2C_MODE_COMPLETE_XFER);     
      166.     
      167.      
                  RGB_LED_ON_RED       ;     
      168.     
      169.      
                         while        (0u        ==        (I2CM_I2CMasterStatus       (       )        & I2CM_I2C_MSTAT_WR_CMPLT       )       )     
      170.     
      171.      
                         {     
      172.     
      173.      
                             /* Waits until master completes write transfer */     
      174.     
      175.      
                         }     
      176.     
      177.      
                  RGB_LED_ON_GREEN       ;     
      178.     
      179.      
                    
      180.     
      181.      
                         /* Displays transfer status */     
      182.     
      183.      
                         if        (0u        ==        (I2CM_I2C_MSTAT_ERR_XFER        & I2CM_I2CMasterStatus       (       )       )       )     
      184.     
      185.      
                         {     
      186.     
      187.      
                      RGB_LED_ON_GREEN       ;     
      188.     
      189.      
                    
      190.     
      191.      
                             /* Check if all bytes was written */     
      192.     
      193.      
                             if       (I2CM_I2CMasterGetWriteBufSize       (       )        == BUFFER_SIZE       )     
      194.     
      195.      
                             {     
      196.     
      197.      
                          status        = TRANSFER_CMPLT       ;     
      198.     
      199.      
                             }     
      200.     
      201.      
                         }     
      202.     
      203.      
                         else     
      204.     
      205.      
                         {     
      206.     
      207.      
                      RGB_LED_ON_RED       ;     
      208.     
      209.      
                         }     
      210.     
      211.      
                    
      212.     
      213.      
                         (       void       ) I2CM_I2CMasterClearStatus       (       )       ;     
      214.     
      215.      
                    
      216.     
      217.      
                         return        (status       )       ;     
      218.     
      219.      
               }     
      220.     
      221.      
                    
      222.     
      223.      
                    
      224.     
      225.      
               /*******************************************************************************     
      226.     
      227.      
               * ReadStatusPacket(): Reads status packet from I2C slave     
      228.     
      229.      
               *******************************************************************************/     
      230.     
      231.      
               uint32 ReadStatusPacket       (       void       )     
      232.     
      233.      
               {     
      234.     
      235.      
                         uint8  buffer       [BUFFER_SIZE       ]       ;     
      236.     
      237.      
                         uint32 status        = TRANSFER_ERROR       ;     
      238.     
      239.      
                    
      240.     
      241.      
                         (       void       ) I2CM_I2CMasterReadBuf       (I2C_SLAVE_ADDR       , buffer       , PACKET_SIZE       , I2CM_I2C_MODE_COMPLETE_XFER       )       ;     
      242.     
      243.      
                    
      244.     
      245.      
                         while        (0u        ==        (I2CM_I2CMasterStatus       (       )        & I2CM_I2C_MSTAT_RD_CMPLT       )       )     
      246.     
      247.      
                         {     
      248.     
      249.      
                             /* Waits until master complete read transfer */     
      250.     
      251.      
                         }     
      252.     
      253.      
                    
      254.     
      255.      
                         /* Displays transfer status */     
      256.     
      257.      
                         if        (0u        ==        (I2CM_I2C_MSTAT_ERR_XFER        & I2CM_I2CMasterStatus       (       )       )       )     
      258.     
      259.      
                         {     
      260.     
      261.      
                      RGB_LED_ON_GREEN       ;     
      262.     
      263.      
                    
      264.     
      265.      
                             /* Check packet structure */     
      266.     
      267.      
                             if        (       (I2CM_I2CMasterGetReadBufSize       (       )        == BUFFER_SIZE       )        &&     
      268.     
      269.      
                                 (PACKET_SOP        == buffer       [PACKET_SOP_POS       ]       )        &&     
      270.     
      271.      
                                 (PACKET_EOP        == buffer       [PACKET_EOP_POS       ]       )       )     
      272.     
      273.      
                             {     
      274.     
      275.      
                                 /* Check packet status */     
      276.     
      277.      
                                 if        (STS_CMD_DONE        == buffer       [PACKET_STS_POS       ]       )     
      278.     
      279.      
                                 {     
      280.     
      281.      
                              status        = TRANSFER_CMPLT       ;     
      282.     
      283.      
                                 }     
      284.     
      285.      
                             }     
      286.     
      287.      
                         }     
      288.     
      289.      
                         else     
      290.     
      291.      
                         {     
      292.     
      293.      
                      RGB_LED_ON_RED       ;     
      294.     
      295.      
                      status        = TRANSFER_ERROR       ;     
      296.     
      297.      
                         }     
      298.     
      299.      
                    
      300.     
      301.      
                         (       void       ) I2CM_I2CMasterClearStatus       (       )       ;     
      302.     
      303.      
                    
      304.     
      305.      
                         return        (status       )       ;     
      306.     
      307.      
               }     
      308.     
      309.      
                    
      310.     
      311.      
                    
      312.     
      313.      
               /* [] END OF FILE */     
      314.     
      315.      
                    
      316.     
      317.      
                    
      318.     
      319.      
                    
      320.     
      321.      
                    
      322.     
      323.      
                    
      324.     
      325.      
                    
      326.     
      327.      
                    
      328.     
      329.      
               <header.       h       >     
      330.     
      331.      
               /*******************************************************************************     
      332.     
      333.      
               * File Name: header.h     
      334.     
      335.      
               *     
      336.     
      337.      
               ********************************************************************************     
      338.     
      339.      
               * Copyright 2014, Cypress Semiconductor Corporation. All rights reserved.     
      340.     
      341.      
               * This software is owned by Cypress Semiconductor Corporation and is protected     
      342.     
      343.      
               * by and subject to worldwide patent and copyright laws and treaties.     
      344.     
      345.      
               * Therefore, you may use this software only as provided in the license agreement     
      346.     
      347.      
               * accompanying the software package from which you obtained this software.     
      348.     
      349.      
               * CYPRESS AND ITS SUPPLIERS MAKE NO WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,     
      350.     
      351.      
               * WITH REGARD TO THIS SOFTWARE, INCLUDING, BUT NOT LIMITED TO, NONINFRINGEMENT,     
      352.     
      353.      
               * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.     
      354.     
      355.      
               *******************************************************************************/     
      356.     
      357.      
                    
      358.     
      359.      
               #include <project.h>     
      360.     
      361.      
                    
      362.     
      363.      
               /***************************************     
      364.     
      365.      
               *         Function Prototypes     
      366.     
      367.      
               ****************************************/     
      368.     
      369.      
                    
      370.     
      371.      
               /* Function prototypes */     
      372.     
      373.      
               uint32 CheckSwitchState       (       void       )       ;     
      374.     
      375.      
               uint32 WriteCommandPacket       (       uint8 cmd       )       ;     
      376.     
      377.      
               uint32 ReadStatusPacket       (       void       )       ;     
      378.     
      379.      
                    
      380.     
      381.      
                    
      382.     
      383.      
               /***************************************     
      384.     
      385.      
               *            Constants     
      386.     
      387.      
               ****************************************/     
      388.     
      389.      
                    
      390.     
      391.      
               /* I2C slave address to communicate with */     
      392.     
      393.      
               #define I2C_SLAVE_ADDR  (0x40u) //(0x48u)//     
      394.     
      395.      
                    
      396.     
      397.      
               /* Buffer and packet size */     
      398.     
      399.      
               #define BUFFER_SIZE     (3u)     
      400.     
      401.      
               #define PACKET_SIZE     (BUFFER_SIZE)     
      402.     
      403.      
                    
      404.     
      405.      
               /* Packet positions */     
      406.     
      407.      
               #define PACKET_SOP_POS  (0u)     
      408.     
      409.      
               #define PACKET_CMD_POS  (1u)     
      410.     
      411.      
               #define PACKET_STS_POS  (PACKET_CMD_POS)     
      412.     
      413.      
               #define PACKET_EOP_POS  (2u)     
      414.     
      415.      
                    
      416.     
      417.      
               /* Start and end of packet markers */     
      418.     
      419.      
               #define PACKET_SOP      (0x01u)     
      420.     
      421.      
               #define PACKET_EOP      (0x17u)     
      422.     
      423.      
                    
      424.     
      425.      
               /* Command valid status */     
      426.     
      427.      
               #define STS_CMD_DONE    (0x00u)     
      428.     
      429.      
               #define STS_CMD_FAIL    (0xFFu)     
      430.     
      431.      
                    
      432.     
      433.      
               /* Command valid status */     
      434.     
      435.      
               #define TRANSFER_CMPLT    (0x00u)     
      436.     
      437.      
               #define TRANSFER_ERROR    (0xFFu)     
      438.     
      439.      
                    
      440.     
      441.      
               /* Commands set */     
      442.     
      443.      
               //#define CMD_SET_OFF     (0u)     
      444.     
      445.      
               //#define CMD_SET_RED     (1u)     
      446.     
      447.      
               //#define CMD_SET_GREEN   (2u)     
      448.     
      449.      
               //#define CMD_SET_BLUE    (3u)     
      450.     
      451.      
               #define CMD_MSR_REL_HUM_HOLD_MASTER (0xE5u)     
      452.     
      453.      
               #define CMD_MSR_REL_HUM_NO_HOLD_MASTER (0xF5u)     
      454.     
      455.      
               #define CMD_MSR_TEMP_HOLD_MASTER (0xE3u)     
      456.     
      457.      
               #define CMD_MSR_TEMP_NO_HOLD_MASTER (0xF3u)     
      458.     
      459.      
               #define CMD_RESET (0xFEu)     
      460.     
      461.      
                    
      462.     
      463.      
                    
      464.     
      465.      
               /***************************************     
      466.     
      467.      
               *               Macros     
      468.     
      469.      
               ****************************************/     
      470.     
      471.      
                    
      472.     
      473.      
               /* Set LED RED color */     
      474.     
      475.      
               #define RGB_LED_ON_RED  \     
      476.     
      477.      
                               do{     \     
      478.     
      479.      
                                   LED_RED_Write  (0u); \     
      480.     
      481.      
                                   LED_GREEN_Write(1u); \     
      482.     
      483.      
                               }while(0)     
      484.     
      485.      
                    
      486.     
      487.      
               /* Set LED GREEN color */     
      488.     
      489.      
               #define RGB_LED_ON_GREEN \     
      490.     
      491.      
                               do{      \     
      492.     
      493.      
                                   LED_RED_Write  (1u); \     
      494.     
      495.      
                                   LED_GREEN_Write(0u); \     
      496.     
      497.      
                               }while(0)     
      498.     
      499.      
                    
      500.     
      501.      
               /* Set LED BLUE color */     
      502.     
      503.      
               #define RGB_LED_ON_BLUE \     
      504.     
      505.      
                               do{     \     
      506.     
      507.      
                                   LED_RED_Write  (1u); \     
      508.     
      509.      
                                   LED_GREEN_Write(1u); \     
      510.     
      511.      
                               }while(0)     
      512.     
      513.      
                    
      514.     
      515.      
               /* Set LED TURN OFF */     
      516.     
      517.      
               #define RGB_LED_OFF \     
      518.     
      519.      
                               do{ \     
      520.     
      521.      
                                   LED_RED_Write  (1u); \     
      522.     
      523.      
                                   LED_GREEN_Write(1u); \     
      524.     
      525.      
                               }while(0)     
      526.     
      527.      
                    
      528.     
      529.      
                    
      530.     
      531.      
               /* [] END OF FILE */     
      532.    
        • 1. Re: PSoC4 I2C Communication
          user_14586677

          Generally speaking its better if you post a project archive so

             

          all setup/settings/configuration can be seen.

             

           

             

          Look at I2C clk and data lines to make sure pullups in place

             

          and you see legit logic level signals.

             

           

             

              

             

                    

             

          “File”                                                             Creator

             

          “Create Workspace Bundle”

             

           

             

           

             

          If you are using a Pioneer board be aware of fixed pin assignments

             

          on the board. Also PSOC 4 itself has dedicated pins for various

             

          components. See table in -

             

           

             

              

             

                    

             

          http://www.cypress.com/?rID=93401     AN86439 - PSoC® 4 - Using GPIO Pins

             

           

             

           

             

          Regards, Dana.

          • 2. Re: PSoC4 I2C Communication
            jaeyeon.kihm

             Thanks Dana for your help. I just figured out that there was my misunderstanding at packet allocation. Once I set the packet design, I could successfully communicate with my slave device. I will make sure to attach the archive if I've a question next time. Thanks!

            • 3. Re: PSoC4 I2C Communication
              user_14586677

              You are always welcome!

                 

               

                 

              Dana.