3 Replies Latest reply on Jan 3, 2017 9:52 PM by EmHo_296241

    Setting variable from method called by ISR

      I'm still learning the in's and out's of C programing so this maybe a very obvious question. I have a Timer that calls an ISR. In my ISR I have this code: 


          if (clockCounter == 1600) {
              clockCounter = 0;


      So that every second it calls those three methods to increment some other timer counters. My though process is that this is the cleanest way to handle keeping track of the timers in the appropriate other classes. 




      Here is what my Increment_Quadrant_LED_OFF_Timer() function looks like: 


      void Increment_Quadrant_LED_OFF_Timer()
          if (!quadrant_LED_OFF_timer_ENABLED) { 


          if (quadrant_LED_OFF_timer_counter >= (QUADRANT_LED_OFF_TIMER_DURATION / DEBUG_DIVIDER)) {


      Where quadrant_LED_OFF_timer_ENABLED variable and Quadrant_LED_OFF_Timer_Did_Expire() are defined in a class call app_Timers. So this works fine the first time that quadrant_LED_OFF_timer_ENABLED is set to 1 from my main.c.


      Here is what my Quadrant_LED_OFF_Timer_Did_Expire() method looks like: 


      void Quadrant_LED_OFF_Timer_Did_Expire()
          UART_UartPutString("Quadrant_LED_OFF_Timer_Did_Expire \n\r ");
          quadrant_LED_OFF_timer_ENABLED = 0;
          quadrant_LED_OFF_timer_counter = 0;


      So you can see that I set my quadrant_LED_OFF_timer_ENABLED to 0 here.


      If I debug and check quadrant_LED_OFF_timer_ENABLED after it has been set it's correct at 0.  However, continuing in debug if I check quadrant_LED_OFF_timer_ENABLED in this function Increment_Quadrant_LED_OFF_Timer() it's set back to 1. 




      So just to clarify: 


      void Increment_Quadrant_LED_OFF_Timer()


      void Quadrant_LED_OFF_Timer_Did_Expire()


      int quadrant_LED_OFF_timer_ENABLED


      int quadrant_LED_OFF_timer_counter


      Are all declared and defined in app_Timers.c and Increment_Quadrant_LED_OFF_Timer() is being called from Cook_Timer_ISR.c




      My questions are: 

      1. What my take away from the debugging and my other programming experience is that the ISR is calling Increment_Quadrant_LED_OFF_Timer() from a different thread? Which is guess it's not technically a different "thread" but some other location in memory? However if this was the case I'm not sure how I'm getting proper UART output. 
      3. Is calling external methods like this to increment other counters the proper way to do this from an ISR? Or should I have a separate ISR for each timer that I need? It seems logical to have one counter that counts every 1 second then just piggy back off that one. Maybe not. 

      Thanks for any help! 

        • 1. Re: Setting variable from method called by ISR

          It is much easier to follow your code when you provide us with the complete project to look at. To do so, use
          Creator->File->Create Workspace Bundle (minimal)
          and attach the resulting file.


          In terms of secure processing the lines


              if (clockCounter == 1600) {


          should read


              if (clockCounter >= 1600) {


          just in case of that cosmic particle changing a bit...  ;-)





          • 2. Re: Setting variable from method called by ISR

            I wish that I could upload the project but due it's for work and strapped under NDAs and other legal mumbo jumbo so would get in trouble if client found out :-( 

            • 3. Re: Setting variable from method called by ISR


              It is difficult to debug the project without the actual project. So I think you have to do the work at your side. I can give you some suggestions

              1. Don't call long routines inside ISR. This may lead to stack overflow. You may set a flag inside the ISR, and put the corresponding code inside main code itself.

              2. While debugging code you can see the call stack option. You may keep a break point at  quadrant_LED_OFF_timer_ENABLED becoming 1, to find where the function was call was generated from.