5 Replies Latest reply on Feb 13, 2019 8:17 AM by niprc_3742601

    variable protection while using interrupts

    niprc_3742601

      Hello,

       

      In OS world, if one thread writes to a variable and second thread reads the same variable then you need to protect the variable with a mutex, let's say.

      But how does this suppose to work in the PSoC world?  I am planning to use Timer Counter with Clock to produce interrupts at certain rate.  My interrupt handler function will update the variable that keeps track of time elapsed.  The main processing loop reads this variable occasionally.  Do I need to protect this variable?  If I do then what's the mechanism?

       

      Thanks!

      Nikolay

        • 1. Re: variable protection while using interrupts
          MoTa_728816

          Hi,

           

          By the word "PSoC World",

          if you are meaning some kind of multi-task OS (RTOS), you need same degree of care with "OS world".

          And if you are talking about "OS-less" program,an interrupt routine is only preempt-ed by higher priority interrupt(s).

           

          Usual flow, such as "main" may only access the value before or after interrupt service routine (ISR),

          so the value must be stable from the view point of the usual flow.

          IMHO, we don't have to protect a variable in such scenario.

          (Note in case you touch same variable from different ISRs, you may have to protect it.)

           

          moto

          • 2. Re: variable protection while using interrupts
            JoMe_264151

            There are "EnterCriticalSection" and "ExitCriticalSection" to control nested interrupt flows. Have a look (search) into the "SystemReference Guide" from Creator Help menu.

             

            Bob

            2 of 2 people found this helpful
            • 3. Re: variable protection while using interrupts
              MoTa_728816

              Hi,

               

              After posting the previous message I noticed that there can be a situation

              when main reads a variable and modify it and before writing it back an ISR is called

              and modified the same variable, this can be a problem.

               

              In such a critical case, I would disable the interrupt during the read/modify/write then enable that interrupt again. But if the expected interrupt is a periodical timer and process can be done within the timer period or main is only referencing the variable, usually we don't have to worry about this much (I hope).

               

              moto

              1 of 1 people found this helpful
              • 4. Re: variable protection while using interrupts
                BoTa_264741

                Nikolay,

                Every global variable that is changed in an interrupt handler must be declared as volatile, e.g.

                volatile uint8 flag_myTimer=0;

                Global Variables Not working When Used in CY_ISR - KBA224452

                /odissey1

                2 of 2 people found this helpful
                • 5. Re: variable protection while using interrupts
                  niprc_3742601

                  Hello,

                   

                  thank you, everybody, for your help!  I use Critical Section to protect my variable and seems to be working fine.

                   

                  CY_ISR( InterruptHandler )

                  {

                      //  Clear the TCPWM terminal count interrupt

                      //

                      Timer_ClearInterrupt( Timer_INTR_MASK_TC );

                     

                      uint8 interrupt_state = CyEnterCriticalSection();

                   

                   

                   

                      //  the interrupt handler is called once every millisecond

                      //

                      time_elapsed_msecs += 1;

                   

                   

                   

                      CyExitCriticalSection( interrupt_state );

                  }

                   

                  and

                   

                  uint32

                  get_time_elapsed_msecs()

                  {

                      uint8 interrupt_state = CyEnterCriticalSection();

                      uint32 v = time_elapsed_msecs;   

                      CyExitCriticalSection( interrupt_state );

                   

                   

                      return( v );

                  }

                   

                   

                  Thanks!

                  Nikolay