title: Temporarily disable timer interrupt
body: <p>I'm working on an embedded project in C on a stm32f4xx uC.</p>
  
  <p>I have a portion of a code that does an loop-operation XYZ continuously, and from time to time a TIM4 interrupt changes some global parameters and causes the operation XYZ to restart.</p>
  
  <p>code is something like this:</p>
  
  <pre><code>for (;;) {
         //line A
      XYZ;
         //line B
  }
  </code></pre>
  
  <p>XYZ is a complex operation involving tranfers of data between buffers and others.</p>
  
  <p>The TIM4 interrupt handler does this: stops XYZ &amp; changes some globals that afect XYZ operations.</p>
  
  <p>So basically I want XYZ to execute repeatedly and TIM4 interrupt to stop XYZ, change the parameters and <strong>then the loop must restart by restarting XYZ with the new global parameters.</strong></p>
  
  <p><strong>PROBLEM IS:</strong> Since XYZ has many instructions, TIM4 IRQ may come right in the middle of it and, after the IRQHandler changes the globals, the operations resume from the middle of XYZ which ruins the program.</p>
  
  <p><strong>MY INITIAL SOLUTION:</strong> Disable interrupts on line A with __disable_irq() and restore them on line B with __enable_irq()</p>
  
  <p><strong>Fails</strong> because the XYZ complex operation must use other interrupts (other than TIM4).</p>
  
  <p><strong>NEXT SOLUTION</strong> Disable only TIM4 interrupt on line A with: </p>
  
  <pre><code>TIM_ITConfig(TIM4, TIM_IT_Update , DISABLE) 
  </code></pre>
  
  <p>and enable it back on line B with: </p>
  
  <pre><code>TIM_ITConfig(TIM4, TIM_IT_Update , ENABLE)
  </code></pre>
  
  <p><strong>Fails</strong> because I am losing the interrupt: when the int is restored, the interrupt that arrived during XYZ is ignored. This is a big problem (one of the reasons is that TIM4 IRQHandler changes the globals and then activates the TIM4 again to give an interrupt later, I do this because the period between interrupts varies).</p>
  
  <p>Can anyone give me a solution to this problem? Is there a better way to disable/restore TIM4 IRQ and <strong>NOT lose any interrupt?</strong></p>