6 Replies Latest reply on Jan 25, 2019 12:47 AM by RuzheZ_36

    PSOC 4 cap sense example

    LiFi_1601451

      Inside the CE210291_CapSense_P4_One_Button02 example, there are following codes.

       

                      interruptState = CyEnterCriticalSection();

                     

                      /* Check if CapSense scanning is complete */

                      if(CapSense_NOT_BUSY != CapSense_IsBusy())

                      {

                          /* If CapSense scanning is in progress, put CPU to sleep */

                          CySysPmSleep();

                      }

                      /* If CapSense scanning is complete, process the CapSense data */

                      else

                      {

                          currentState = PROCESS_DATA;

                      }

                      /* Enable interrupts for servicing ISR */

                      CyExitCriticalSection(interruptState);

       

      The question is why we need to enter critical section. In my application, I have following code. Do I need to add enter critical section codes

       

          while(1u)
          {
              ADCHandler();
              CySysWdtResetCounters(CY_SYS_WDT_COUNTER1_RESET);
             
              if(BatteryOutOfRange==0)
              {
                  /* Do this only when a scan is done */
                  if(CapSense_NOT_BUSY == CapSense_IsBusy())
                  {
                      CapSense_ProcessAllWidgets(); /* Process all widgets */
                      #ifdef USER_CAP_SENSE_TUNE_ENABLE
                    //      LED_Write(1u);
                            CapSense_RunTuner(); /* To sync with Tuner application */
                     //     LED_Write(0u);
                      #endif // cap sense tune enable
                      CapSense_CheckState();
                      CapSense_ScanAllWidgets(); /* Start next scan */
                  }

                  /* Display CapSense state using LEDs */
                        

              }
              else CapTouchOutput_Write(0u);
                 
          }

       

      void CapSense_CheckState(void)

      {

      //    int16 i;

          static uint16_t guardReleaseCount = 0, SensorTouchCount = 0,guardState = 1, sensorState=0;

          static uint8_t press_print = 0;

         

          if (CapSense_IsWidgetActive(CapSense_GUARD_WDGT_ID))

          {

              guardState=1;

              guardReleaseCount = 0;

         //     User_UART_UartPutString("GuardProtected\r\n");

            //              for(i=0;i<3000;i++)

          //          ;

             

          }

          else

          {

              if(++guardReleaseCount >= GUARD_RELEASE_COUNT_TH)

              {

                  guardState=0;

                  guardReleaseCount = 0;

              }

          }

          if (CapSense_IsWidgetActive(CapSense_BUTTON0_WDGT_ID))

          {

              if(++SensorTouchCount>=SENSOR_TOUCH_COUNT_TH)

              {

                  sensorState=1;

                  SensorTouchCount=0;

              }

             

          }

          else

          {

              sensorState=0;

              SensorTouchCount = 0;

          }

        

         

          if(guardState ==0 && sensorState==1)

          {

              if(press_print==0)

              {

                  User_UART_UartPutString("Button Pressed\r\n");

                  press_print = 1;

              }

              CapTouchOutput_Write(1u);

         //     LED_Write(1u);

          }

          else

          {

        //      LED_Write(0u);

              CapTouchOutput_Write(0u);

              press_print = 0;

          }

      }

        • 1. Re: PSOC 4 cap sense example
          RuzheZ_36

          Hi Lin,

           

          That is used to guarantee CapSense hardware scan won't be interrupted by other interrupt source.

          If you have checked your project and confirmed there's no other interrupt would happen during CapSense scan, you don't have to to enter critical section.

           

          Thanks,

          Ryan

          • 2. Re: PSOC 4 cap sense example
            LiFi_1601451

            My firmware has watchdog and ADC interrupt. Where can I insert at below code the enter and exit critical section function

             

                while(1u)
                {
                    ADCHandler();
                    CySysWdtResetCounters(CY_SYS_WDT_COUNTER1_RESET);
                   
                    if(BatteryOutOfRange==0)
                    {
                        /* Do this only when a scan is done */
                        if(CapSense_NOT_BUSY == CapSense_IsBusy())
                        {
                            CapSense_ProcessAllWidgets(); /* Process all widgets */
                            #ifdef USER_CAP_SENSE_TUNE_ENABLE
                                  CapSense_RunTuner(); /* To sync with Tuner application */
                            #endif // cap sense tune enable
                            CapSense_CheckState();
                            CapSense_ScanAllWidgets(); /* Start next scan */
                        }

                        /* Display CapSense state using LEDs */
                              

                    }
                    else CapTouchOutput_Write(0u);
                       
                }

             

            void CapSense_CheckState(void)
            {
            //    int16 i;
                static uint16_t guardReleaseCount = 0, SensorTouchCount = 0,guardState = 1, sensorState=0;
                static uint8_t press_print = 0;
               
                if (CapSense_IsWidgetActive(CapSense_GUARD_WDGT_ID))
                {
                    guardState=1;
                    guardReleaseCount = 0;
               //     User_UART_UartPutString("GuardProtected\r\n");
                  //              for(i=0;i<3000;i++)
                //          ;
                   
                }
                else
                {
                    if(++guardReleaseCount >= GUARD_RELEASE_COUNT_TH)
                    {
                        guardState=0;
                        guardReleaseCount = 0;
                    }
                }
                if (CapSense_IsWidgetActive(CapSense_BUTTON0_WDGT_ID))
                {
                    if(++SensorTouchCount>=SENSOR_TOUCH_COUNT_TH)
                    {
                        sensorState=1;
                        SensorTouchCount=0;
                    }
                   
                }
                else
                {
                    sensorState=0;
                    SensorTouchCount = 0;
                }
              
               
                if(guardState ==0 && sensorState==1)
                {
                    if(press_print==0)
                    {
                        User_UART_UartPutString("Button Pressed\r\n");
                        press_print = 1;
                    }
                    CapTouchOutput_Write(1u);
               //     LED_Write(1u);
                }
                else
                {
              //      LED_Write(0u);
                    CapTouchOutput_Write(0u);
                    press_print = 0;
                }
            }

             


            void ADCHandler(void)
            {
                uint8 channel = CHANNEL_1;
                int16 adcVal;
                int32 mVolts;
                int32 BatteryVolt;
                static uint8 FirstBoot=1;
            //   int16 i;

                Vbat_EN_Write(1u);

                if(ADCdataReady != 0u)
                {

                    adcVal = ADC_GetResult16(CHANNEL_1);
                   

                   
                  /* Check for ADC window limit interrupt */
                    if(ADCwindowFlag != 0u)
                    {

                       BatteryOutOfRange = 1;
            /*
                       mVolts = (int32)ADC_CountsTo_mVolts(channel, adcVal);
                       BatteryVolt = mVolts * BATTERY_VOLTAGE_GAIN + BATTERY_OFFSET;
                       User_UART_UartPutString("Battery voltage output of range-interrupt flag set\r\n");
            */
                        /* Note: If LED is active HIGH, then replace "LOW" with "HIGH" */
                    }
                    else
                    {

                        /* Convert the ADC counts of active channel to mVolts */
                        mVolts = (int32)ADC_CountsTo_mVolts(channel, adcVal);
                        BatteryVolt = mVolts * BATTERY_VOLTAGE_GAIN + BATTERY_OFFSET;

                        if(FirstBoot)
                        {
                            if(BatteryVolt>=BATTERY_VOLTAGE_LOWER_LIMIT && BatteryVolt <=BATTERY_VOLTAGE_UPPER_LIMIT)
                                BatteryOutOfRange = 0;
                        }
                                
                        if(BatteryOutOfRange)
                        {
                            if(BatteryVolt>=BATTERY_VOLTAGE_LOWER_HYSTERESIS && BatteryVolt <=BATTERY_VOLTAGE_HIGHER_HYSTERESIS)
                                BatteryOutOfRange = 0;
                        }
                        else
                        {
                            if(BatteryVolt>=BATTERY_VOLTAGE_UPPER_LIMIT || BatteryVolt<=BATTERY_VOLTAGE_LOWER_LIMIT)
                                BatteryOutOfRange = 1;
                        }
                       

                    }
                    FirstBoot = 0;
                    ADCdataReady = 0u;
            /*      
                   
                    if(BatteryOutOfRange)
                        User_UART_UartPutString("Battery Protected\r\n");
                    else User_UART_UartPutString("Battery normal\r\n");

                    SendChannelVoltage(channel, mVolts);
                    SendChannelVoltage(channel, BatteryVolt);
                        for(i=0;i<3000;i++)
                          ; 
                    */
                   
                }
            }

            • 3. Re: PSOC 4 cap sense example
              LiFi_1601451

              Dear rzzh,

              Please reply in above issue at your earliest convenience

              Thanks.

              Fiske Lin

              • 4. Re: PSOC 4 cap sense example
                RuzheZ_36

                Hi Fiske,

                 

                why not following the code example architecture, especially to judge scan is busy or not busy?

                 

                Thanks,

                Ryan

                • 5. Re: PSOC 4 cap sense example
                  LiFi_1601451

                  Dear Ryan,

                  My application don’t require to enter sleep mode. Therefore, I just simplify the code. Are there any problems in my code? Are there any better way to do it.

                   

                  Fiske Lin

                  Senior Electronic Engineer

                  Corporate Engineering

                  Defond Group

                   

                  T +852 31237674

                  • 6. Re: PSOC 4 cap sense example
                    RuzheZ_36

                    Fiske,

                     

                    If Deep Sleep is not required. You can use more simply CapSense scan code. For example, the attached project in the KBA:

                    https://community.cypress.com/docs/DOC-15405

                     

                    Thanks,

                    Ryan