5 Replies Latest reply on Dec 8, 2019 7:47 PM by RakshithM_16

    I2C for reading from PSoC by raspberry pi - python

    UlMo_4589691

      Reading from raspberry pi via I2C - how can I reset the read pointer to the first byte of the read buffer by my raspberry? I am using python on the raspberry.

        • 1. Re: I2C for reading from PSoC by raspberry pi - python
          RakshithM_16

          Hi UlMo_4589691,

           

          Can you please provide more information regarding your application?

           

          According to my understanding you want to reset the buffer pointer of the read buffer of your I2C slave device (PSoC device) through Raspberry Pi device. Please correct me if I am wrong.

           

          You can define a particular value as RESET command and when the I2C slave gets the RESET command (from RPi) you can reset the buffer pointer in your slave firmware.

           

          Hope this helps,

           

          Thanks and Regards,

          Rakshith M B

          • 2. Re: I2C for reading from PSoC by raspberry pi - python
            UlMo_4589691

            Hi Rakshith,

            I have to read out about 5 Touchsliders and 20 Touch fields. This is too much for one PSoC4000 (CY8C4248LTI-L485). Therefore I connect the PSoC with a raspberry pi via I2C and read the sensor status from their read buffes. Now I reset the buffer pointer before updating the sensor values. The time for udating should be faster for each PSoC4000 than for the pi. Hence by reading the buffer pointers are always 0.

             

            Do you think that works?

            #include "project.h"

            #include "CapSense.h"

            uint8 bufSize;

            uint8 readBuffer[] = {1,2,3,4,5,6,7,8,9,10};

            uint8 writeBuffer[10];

            uint8 byteCnt;

            uint8 userArray[10];

            uint32 status;

             

            /* Boolean constants */

            #define LED_ON                        (0u)

            #define LED_OFF                        (1u)

             

            /* Finite state machine states for device operating states

                SENSOR_SCAN - Sensors are scanned in this state

                WAIT_FOR_SCAN_COMPLETE - CPU is put to sleep in this state

                PROCESS_DATA - Sensor data is processed, LEDs are controlled,

                              and I2C buffer is updated in this state */

            typedef enum

            {

                SENSOR_SCAN = 0x01u,              

                WAIT_FOR_SCAN_COMPLETE = 0x02u,  

                PROCESS_DATA = 0x03u,            

            } DEVICE_STATE;

             

            /*Defining step size for LED control based on centroid position of slider*/

            #define STEP_SIZE (CapSense_SLD_X_RESOLUTION/CapSense_SLD_NUM_SENSORS)

             

            int main()

            {    //Start I2C

                CyGlobalIntEnable;

                I2C_1_I2CSlaveInitReadBuf(readBuffer, 10);

                I2C_1_I2CSlaveInitWriteBuf(writeBuffer, 10);

                I2C_1_Start();

              

             

              

                /* Start CapSense block */

                CapSense_Start();

              

                for(;1;)

                {

                    CapSense_ScanAllWidgets();

                    CapSense_ProcessAllWidgets();

                  

                    uint32 centroid;

                    /*Get the touch position(centroid) of CapSense Linear Slider*/

                    centroid = CapSense_GetCentroidPos(CapSense_SLD_WDGT_ID);

                    int LED9wert=CapSense_IsWidgetActive(CapSense_BTN0_WDGT_ID);

                    int LED10wert=CapSense_IsWidgetActive(CapSense_BTN1_WDGT_ID);

                    int LED11wert=CapSense_IsWidgetActive(CapSense_BTN2_WDGT_ID);

                  

                    /*Turn ON/OFF LEDs based on the status of the corresponding CapSense buttons*/

                    LED9_Write(LED9wert ? LED_ON : LED_OFF );

                    LED10_Write(LED10wert ? LED_ON : LED_OFF );

                    LED11_Write(LED11wert ? LED_ON : LED_OFF );

             

                    /*Turn ON/OFF LEDs based on the finger position (centroid) on the CapSense Linear slider*/

                    if(CapSense_IsWidgetActive(CapSense_SLD_WDGT_ID))

                    {      

                        LED4_Write( ( (centroid > 0) || (centroid == 0)) ? LED_ON : LED_OFF );

                        LED5_Write( ( centroid > ( 1 * STEP_SIZE ) ) ? LED_ON : LED_OFF);

                        LED6_Write( ( centroid > ( 2 * STEP_SIZE ) ) ? LED_ON : LED_OFF);

                        LED7_Write( ( centroid > ( 3 * STEP_SIZE ) ) ? LED_ON : LED_OFF);

                        LED8_Write( ( centroid > ( 4 * STEP_SIZE ) ) ? LED_ON : LED_OFF);

                    }

                    else

                    {

                        LED4_Write(LED_OFF);

                        LED5_Write(LED_OFF);

                        LED6_Write(LED_OFF);

                        LED7_Write(LED_OFF);

                        LED8_Write(LED_OFF);

                    }

                      

                  

                    //Nachricht an Master ----------------------------------------------------------------------------------------------------------  

                    status=I2C_1_I2CSlaveStatus();

                    if(0u!= (status & !I2C_1_I2C_SSTAT_RD_CMPLT))  

                    {

                        readBuffer[0]=centroid;

                        readBuffer[1]=LED9wert;

                        readBuffer[2]=LED10wert;

                        readBuffer[3]=LED11wert;

                      

                        I2C_1_I2CSlaveClearReadBuf();  //sets pointer to zero

                        I2C_1_I2CSlaveClearReadBuf();

                    }

                  

                    //Nachricht von Master

                    status=I2C_1_I2CSlaveStatus();

                    if(I2C_1_I2CSlaveStatus() & I2C_1_I2C_SSTAT_RD_CMPLT)

                    {

                        byteCnt=I2C_1_I2CSlaveGetWriteBufSize();

                        for (uint8 i=0; i<byteCnt;i++)

                        {

                            userArray[i]=writeBuffer[i];

                        }

                        I2C_1_I2CSlaveClearWriteStatus();

                        I2C_1_I2CSlaveClearWriteBuf();

                    }

                }

            }

            • 3. Re: I2C for reading from PSoC by raspberry pi - python
              RakshithM_16

              Hi UlMo_4589691,

               

              Thank you for sharing your code.

               

              There were a few issues that I found in your code.

               

              >CapSense_ScanAllWidgets();

              This function is not a polling function. So, execution continues even though the scan is not completed. The next step processes the widgets though the scan is not completed. You can refer to any CapSense code example for more information.

               

              I also changed the I2C status check conditions according to the code examples.

               

              So please change your code to -

               

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

              * After CapSense_Start();

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

              uint32 centroid;

              for(;1;)

              {

                   if(0u == CapSense_IsBusy())

                   {

                        CapSense_ProcessAllWidgets();

                                 

                                  /*Get the touch position(centroid) of CapSense Linear Slider*/

                                   centroid = CapSense_GetCentroidPos(CapSense_SLD_WDGT_ID);

                                   int LED9wert=CapSense_IsWidgetActive(CapSense_BTN0_WDGT_ID);

                                   int LED10wert=CapSense_IsWidgetActive(CapSense_BTN1_WDGT_ID);

                                   int LED11wert=CapSense_IsWidgetActive(CapSense_BTN2_WDGT_ID);

               

                                    /*Turn ON/OFF LEDs based on the status of the corresponding CapSense buttons*/

                                   LED9_Write(LED9wert ? LED_ON : LED_OFF );

                                   LED10_Write(LED10wert ? LED_ON : LED_OFF );

                                   LED11_Write(LED11wert ? LED_ON : LED_OFF );

               

               

                                    /*Turn ON/OFF LEDs based on the finger position (centroid) on the CapSense Linear slider*/

                                   if(CapSense_IsWidgetActive(CapSense_SLD_WDGT_ID))

                                   {     

                                             LED4_Write( ( (centroid > 0) || (centroid == 0)) ? LED_ON : LED_OFF );

                                             LED5_Write( ( centroid > ( 1 * STEP_SIZE ) ) ? LED_ON : LED_OFF);

                                             LED6_Write( ( centroid > ( 2 * STEP_SIZE ) ) ? LED_ON : LED_OFF);

                                             LED7_Write( ( centroid > ( 3 * STEP_SIZE ) ) ? LED_ON : LED_OFF);

                                             LED8_Write( ( centroid > ( 4 * STEP_SIZE ) ) ? LED_ON : LED_OFF);

                                   }

                                   else

                                   {

                                             LED4_Write(LED_OFF);

                                             LED5_Write(LED_OFF);

                                             LED6_Write(LED_OFF);

                                             LED7_Write(LED_OFF);

                                             LED8_Write(LED_OFF);

                                  }

                                  CapSense_ScanAllWidgets();

                        }

                       

                        //Nachricht an Master ---------------------------------------------------------------------------------------------------------- 

                       

                        if(0u != (I2CS_I2CSlaveStatus() & I2CS_I2C_SSTAT_RD_CMPLT)) 

                        {

                                  I2C_1_I2CSlaveClearReadStatus();

                                  I2C_1_I2CSlaveClearReadBuf();

                                  readBuffer[0]=centroid;

                                  readBuffer[1]=LED9wert;

                                  readBuffer[2]=LED10wert;

                                  readBuffer[3]=LED11wert;

                        }

                   

                        //Nachricht von Master

                       

                        if(0u != (I2CS_I2CSlaveStatus() & I2CS_I2C_SSTAT_WR_CMPLT))

                        {

                                byteCnt=I2C_1_I2CSlaveGetWriteBufSize();

                                for (uint8 i=0; i<byteCnt;i++)

                                {

                                         userArray[i]=writeBuffer[i];

                                }

                                I2C_1_I2CSlaveClearWriteStatus();

                                I2C_1_I2CSlaveClearWriteBuf();

                        }

              }

               

              Please check if this code works for you. If not please attach both the PSoC Project and the RPi code so that it will be easier for us to debug.

               

               

              Thanks and Regards,

              Rakshith M B

              • 4. Re: I2C for reading from PSoC by raspberry pi - python
                UlMo_4589691

                Dear Rakshith,

                thank you for your help, but

                - there is no CapSense_CSD_IsBusy()

                - if I remove CapSense_ScanAllWidgets(); then the senor is not working

                 

                May you help me please

                • 5. Re: I2C for reading from PSoC by raspberry pi - python
                  RakshithM_16

                  Hi UlMo_4589691,

                   

                  I apologize for the error. CapSense_CSD_IsBusy() is for an old component and it is changed to CapSense_IsBusy(). Please change CapSense_CSD_IsBusy() to CapSense_IsBusy(). I will editing my previous reply so that anyone who views the code is not confused.

                   

                  For your second question -

                  - if I remove CapSense_ScanAllWidgets(); then the senor is not working

                  I have not removed CapSense_ScanAllWidgets(); I have put it at the end of the if condition.

                   

                  Hope this helps,

                   

                  Regards,

                  Rakshith M B