9 Replies Latest reply on Dec 18, 2015 8:55 AM by user_1377889

    Example I2C with PSoC 4000.

    brendan_merna_1545636

      I'm trying to set up I2C communication with the PSoC 4000 board and having trouble. I am using an Aardvark to try to read and write data. All the example projects I have seen on PSoC uses EZI2C and the Bridge Control Panel. I don't want to use this.

         

      Does anyone have any example code just using plain I2C?

        • 1. Re: Example I2C with PSoC 4000.
          user_1377889

          Welcome in the forum, brendan.

             

          Funny animal you try to read from. Probably a link to the datasheet could help us. Furthermore can you post your complete project, so that we all can have a look at all of your settings? To do so, use
          Creator->File->Create Workspace Bundle (minimal)
          and attach the resulting file.

             

           

             

          Bob

          • 2. Re: Example I2C with PSoC 4000.
            brendan_merna_1545636

            Hey Bob,

               

            Thanks for the response! Love the profile picture too.

            I put together the bundle and attached. My code basically waits for an I2C interrupt. The IRQ sends the status register value to my transfer function which responds accordingly. I've also tried this without any interrupts and just continually check the status register and called the transfer function in the main loop and it still didn't work.

            I'm using an Aardvark with Control Center on my PC to act as the Master. I'm looking at the data sent on an oscilloscope and that data looks good going into the PSoC, but not coming out.

            • 3. Re: Example I2C with PSoC 4000.
              user_78878863

              When you right-click on a component, you can search for example projects. There should be some for plain I2C.

                 

              When your scope shows that sending I2C data from the PSoC doesn't show up on the I2C data lines, something in your code is wrong (can you put a breakpoint right to where your data should be send and check that this code is called?)

              • 4. Re: Example I2C with PSoC 4000.
                user_1377889

                I still do not know what your "Ardvark" is, google couldn't get me any further. Link to a datasheet would be appreciated.

                   

                What makes me suspicious is that you check the I2C-state at any  exception and while the component handles the state you make changes to the buffer contents. I would suggest to start with something more simple:

                   

                Fill a read-buffer with pre-defined data

                   

                in a loop enable the buffer, wait until master has red the complete buffer successfully

                   

                Next: get that code in an interrupt handler

                   

                Handle with that strategy the master write function

                   

                Combine both

                   

                 

                   

                Bob

                • 5. Re: Example I2C with PSoC 4000.
                  brendan_merna_1545636

                  I got the code some what working up to the point you made Bob. Basically, in the Aardvark you can send a write command with an address followed by a read command. In my code I now fill the transfer buffer with the contents pointed to by the address write and then read it. That works for the first read, but every one after that it reads FF. I'm using the Clear Buffer command to reset my buffer pointer, but it doesn't seem to be doing that.

                  I have a mini prog 3 for debugging but I was having trouble setting that up, so I have been running blind with my code just using LEDs to debug.

                     

                  The Aardvark acts as a master or a slave for an I2C line and sends and displays data on its software called control center. I attached a screenshot too and showed the data from my problem.

                     

                  Write 40 EE -Address Data
                  Write 40 - Address
                  Read EE - Data
                  Write 40 - Address
                  Read 44 - Data
                  Write 40 - Address
                  Read FF - Data
                  http://www.totalphase.com/support/articles/200468316-Aardvark-I2C-SPI-Host-Adapter-User-Manual

                  • 6. Re: Example I2C with PSoC 4000.
                    brendan_merna_1545636

                    Thanks for the tip hli! I looked at an I2C example project turning on and off LEDs. It confirmed I'm doing the write steps to get this going. It looks like this thing is just tough to communicate with if you are not using their own Bridge Control software.

                    • 7. Re: Example I2C with PSoC 4000.
                      user_1377889

                      You do not show the places in your screenshot where you reset the PSoC slave's Tx and Rx buffers by your program.

                         

                      Debugging is quite easy: Under Project->Build settings->Compiler->Optimization set to "none"

                         

                      Click on the beetle-icon and wait for the first C-line showing up. Place a breakpoint wherever you like (clicking into the leftmost area of a line and run to that breakpoint. When interrupts are enabled (there is an icon showing that) remove breakpoint and set a new one elsewere. When interrupts are disabled (or you disable interrupts) you may single-step through your code.

                         

                       

                         

                      Bob

                      • 8. Re: Example I2C with PSoC 4000.
                        brendan_merna_1545636

                        I reset the buffers after detecting a write complete and a read complete in the code.

                               else if(u32Status == I2C_I2C_SSTAT_WR_CMPLT)                 /* Write function complete. Data recieved.*/
                            {
                                slave_buff_addr = g_au8SlvRxData[0];
                                I2C_I2CSlaveClearWriteStatus();
                                for( i=0; i < g_u8SlvDataLen; i++ )
                                {
                                    g_au8SlvData[slave_buff_addr+i]=g_au8SlvRxData[1+i];
                                }
                                g_au8SlvTxData[0]=g_au8SlvData[slave_buff_addr];
                                I2C_I2CSlaveClearReadBuf();
                                g_u8SlvDataLen = 0;
                                I2C_I2CSlaveClearWriteBuf(); //Reset buffer
                            }
                        else if(u32Status == I2C_I2C_SSTAT_RD_CMPLT)                 /* Master has read the slave data. */
                            {
                                I2C_I2CSlaveClearReadStatus();
                                g_u8SlvDataLen = 0;
                                I2C_I2CSlaveClearReadBuf(); //Reset buffer
                            }

                        • 9. Re: Example I2C with PSoC 4000.
                          user_1377889

                          So when you find a write complete there cannot be a read complete?

                             

                          Afaik reading past the readbuffer will transmit a 0xff to the master (including a NAK)

                             

                           

                             

                          Bob