6 Replies Latest reply on May 29, 2017 12:46 PM by user_419482461

    I2C_MasterReadBuf and related

    user_419482461

      I am trying to understand more about I2C and the use of it in PSoC.

         

      At this moment i am working with a MPU-6050. There is a library for this component and it works all fine. 

         

      But..... its to easy for me to just use a library, copy/past it and ready. 

         

      At the moment i have code that works with the I2C_MasterReadByte() function. 

         

      Is there a way to use the I2C_MasterReadBuf() function?

         

      And is there a way to shorten this piece of code?

         

      Best Regards,

         

      Frank

        • 1. Re: I2C_MasterReadBuf and related
          user_1377889

          You need to use I2C_MasterWriteBuf() at first 

             

          The high-level APIs must be used in this way:

             

          Writing to slave Count bytes
          I2C_MasterWriteBuf(SlaveAddress,DataPtr,Count,I2C_MODE_COMPLETE_XFER);

             

          Reading from Slave sending register/command byte first:
          I2C_MasterWriteBuf(SlaveAddress,&RegAddress,1,I2C_MODE_NO_STOP);
          I2C_MasterReadBuf(SlaveAddress,DataPtr,Count,I2C_MODE_REPEAT_START);

             


          Bob

          • 2. Re: I2C_MasterReadBuf and related
            user_419482461
                    Thanks for the response, despite it still doesn't work. I made the code like below. Where wBuf is an array: uint8 wBuf[1] and wBuf[0] = 0x3B Where rBuf is an array: uint8 rBuf[14] Where status is uint8 I2C_MasterWriteBuf(0x68,wBuf,1,I2C_MODE_NO_STOP); I2C_MasterClearStatus(); I2C_MasterReadBuf(0x68,rBuf,14,I2C_MODE_REPEAT_START); status=I2C_MasterStatus(); When i debug the program i will find status = 4 (I2C_MSTAT_XFER_INP) transfer in progress But de rBuf array isn't filled with any value.   
            • 3. Re: I2C_MasterReadBuf and related
              user_1377889

              Try to remove the line I2C_MasterClearStatus();

                 

              Ensure that global interrupts are enabled

                 

               

                 

              Bob

              • 4. Re: I2C_MasterReadBuf and related
                user_419482461
                        Hi Bob, I tryed with removed I2C_MasterClearStatus() but it didn't work either. The global interrupts are enabled. De code below works, its with I2C_masterSendStart(), I2C_MasterWriteByte(), I2C_MasterSendRestart(), I2C_MasterReadByte() and I2C_MasterSendStop(). So the I2C setup works and have data in my rBuf array. Its now just that i want to understand how to use the read and write with API I2C_MasterReadBuf() and I2C_MasterWriteBuf() //Read data from MPU-6050 uint8_t i=0; I2C_MasterSendStart(0x68, I2C_WRITE_XFER_MODE); I2C_MasterWriteByte(0x3B); //Default 0x3B I2C_MasterSendRestart(0x68, I2C_READ_XFER_MODE); while (i++ < (14-1)) { rBuf[i-1] = I2C_MasterReadByte(I2C_ACK_DATA); } rBuf[13] = I2C_MasterReadByte(I2C_NAK_DATA); I2C_MasterSendStop();   
                • 5. Re: I2C_MasterReadBuf and related
                  user_1377889

                  My I2C interface works (usually). ;-)

                     

                  Can you upload 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

                  • 6. Re: I2C_MasterReadBuf and related
                    user_419482461

                    Hi Bob,

                       

                    So after reading some stuff on the internet i found out that the I2C uses its own interrupt. I use some interrupts myself in the application. It looks like the I2C component does'nt like that at all. Yep and after checking the interrupts priority i found out they where all on the same(1).  I changed that in the right one for the I2C_I2C_IRQ to priority (0). And the other onces in order from priority, its logical.

                       

                    Despite it did'nt work direct. I also had to add I2C_MasterClearWriteBuf() and I2C_MasterClearReadBuf(). After that it worked all perfectly.

                       

                    So to use the I2C_MasterWriteBuf() and I2C_MasterReadBuf() you have to do it in following order.

                       

                    Look after your priority from the interrupt when you use other interrupts in your application.

                       

                    And than code like:

                       

                    wBuf[1];

                       

                    rBuf[14]; //Number of bytes you want to read

                       

                    wBuf[0] = cmdAdress;

                       

                    I2C_MasterClearWriteBuf();
                        I2C_MasterWriteBuf(devAdress,wBuf,1,I2C_MODE_NO_STOP);
                        while (0u == (I2C_MasterStatus() & I2C_MSTAT_WR_CMPLT)) //Waits until master completes write transfer 
                               
                        if (0u == (I2C_MSTAT_ERR_XFER & I2C_MasterStatus())) //Check transfer status 
                        {
                            if(I2C_MasterGetWriteBufSize() == 1) //Check if all bytes was written 
                            {
                                //What ever you want to do after writing the byte
                            }
                        }
                        else
                        {
                            //What ever you want to do when something wrong
                        }
                            
                        I2C_MasterClearReadBuf();
                        I2C_MasterReadBuf(devAdress,rBuf,14,I2C_MODE_REPEAT_START);
                        while (0u == (I2C_MasterStatus() & I2C_MSTAT_RD_CMPLT)) //Waits until master complete read transfer 
                        
                        if (0u == (I2C_MSTAT_ERR_XFER & I2C_MasterStatus())) //Displays transfer status 
                        {
                            if(I2C_MasterGetReadBufSize() == 14) // Check if all bytes was written 
                            {
                                //What ever you want to do after reading all the bytes
                             }
                        }
                        else
                        {
                            //What ever you want to do when something wrong
                        }

                       

                    Thanks Bob for the hints.

                       

                    Happy coding,

                       

                    Frank