Cap touch code enter critical section

Tip / Sign in to post questions, reply, level up, and achieve exciting badges. Know more

cross mob
LiFi_1601451
Level 2
Level 2

Below is my firmware, and it has watchdog and ADC interrupt. I have to add enter and exit critical section function so that the cap touch process will not be interrupted. Please tell me 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)
{

    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;


    }
    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);

    }
    else
    {

        CapTouchOutput_Write(0u);
        press_print = 0;
    }
}


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

    Vbat_EN_Write(1u);

    if(ADCdataReady != 0u)
    {

        adcVal = ADC_GetResult16(CHANNEL_1);
       

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

           BatteryOutOfRange = 1;

  
        }
        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;

    
    }
}

0 Likes
1 Solution
LinglingG_46
Moderator
Moderator
Moderator
500 solutions authored 1000 replies posted 10 questions asked

please refer to the below codes:

You can't disable the interrupt while scanning, because we should use the CSD ISR to report that scanning is done.

We only need to disable the interrupts while checking the capsense scan status.

If you want to avoid that the CSD scan is disturbed by other interrupts, you only to set the CSD ISR as the highest priority in your system.

uint8 interruptState;

  if(BatteryOutOfRange==0)

        {

/* Disable interrupts, so that ISR is not serviced while

                *  checking for CapSense scan status.

                */

      interruptState = CyEnterCriticalSection();

            /* Do this only when a scan is done */

            if(CapSense_NOT_BUSY == CapSense_IsBusy())

            {

       /* Enable interrupts for servicing ISR */

       CyExitCriticalSection(interruptState);

                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 */

            }

       /* Enable interrupts for servicing ISR */

           CyExitCriticalSection(interruptState);

            /* Display CapSense state using LEDs */
                

        }
        else CapTouchOutput_Write(0u);

View solution in original post

0 Likes
1 Reply
LinglingG_46
Moderator
Moderator
Moderator
500 solutions authored 1000 replies posted 10 questions asked

please refer to the below codes:

You can't disable the interrupt while scanning, because we should use the CSD ISR to report that scanning is done.

We only need to disable the interrupts while checking the capsense scan status.

If you want to avoid that the CSD scan is disturbed by other interrupts, you only to set the CSD ISR as the highest priority in your system.

uint8 interruptState;

  if(BatteryOutOfRange==0)

        {

/* Disable interrupts, so that ISR is not serviced while

                *  checking for CapSense scan status.

                */

      interruptState = CyEnterCriticalSection();

            /* Do this only when a scan is done */

            if(CapSense_NOT_BUSY == CapSense_IsBusy())

            {

       /* Enable interrupts for servicing ISR */

       CyExitCriticalSection(interruptState);

                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 */

            }

       /* Enable interrupts for servicing ISR */

           CyExitCriticalSection(interruptState);

            /* Display CapSense state using LEDs */
                

        }
        else CapTouchOutput_Write(0u);

0 Likes