title: STM32 µC: SysTick delay doesn't work inside interrupt handler
body: <p>So I wrote a program for the <code>STM32F103C8T6</code> microcontroller in C, using the RTC (Real Time Clock) and a display module.</p>
  
  <p>The RTC and the display both work fine, but when I try to update the display from inside the RTC interrupt handler, it doesn't work.</p>
  
  <p>When I write something to the display from within the <code>main()</code>, it works just fine. </p>
  
  <p>The interrupt handler works as well, so I figured that the problem lied within the function that writes to the display.</p>
  
  <p>This function uses small delays to bit-bang the communication with the display controller.</p>
  
  <p>I previously used the <code>SysTick</code> to generate the delays like this:</p>
  
  <pre><code>void delay(uint32_t n){
      uint32_t start =  systick_count;
      while (systick_count - start &lt; n);
      return;
  }
  </code></pre>
  
  <p>But somehow, inside the interrupt handler of the RTC it doesn't work. So I replaced my delay function with this one, not using the SysTick:</p>
  
  <pre><code>for (; i&gt;0; i--) {
      for (int j = 0; j &lt; 72; ++j) {
          __asm__ __volatile__("nop\n\t":::"memory");
      }
  }
  </code></pre>
  
  <p>and now everything works just fine. </p>
  
  <p>I'm trying to understand, why the <code>SysTick</code> apparently doesn't work inside the RTC interrupt handler.</p>
  
  <p>I figured that maybe it's caused by the Interrupt priorities, but according to the datasheet, by default the <code>SysTick Interrupt</code>'s priority is higher than the RTC interrupts priority.</p>
  
  <p>Maybe someone can explain, why this happens?</p>
  
  <p>EDIT1: Ok, so I’ve read a bit more about the interrupt priorities, and it seems that i need to configure the NVIC_IRQChannelPreemptionPriority correctly. I’ll try this as soon as I can...</p>
  
  <p>With regard to the delay inside the interrupt, I know that's not the right way to do It, but I still wanted to understand the program's behaviour</p>
  
  <p>EDIT2: I just tried changing the interrupt priorities in the following way:</p>
  
  <pre><code>// set RTC interrupt priority to 15 (lowest)
  NVIC_SetPriority(RTC_IRQn, 15);
  // set interrupt priority of SysTick to 0 (highest)
  NVIC_SetPriority(SysTick_IRQn, 0);
  </code></pre>
  
  <p>and now the SysTick delay works within the RTC interrupt handler. </p>