title: freertos global variable inside isr
body: <p>I'm new to RTOS and having some troubles understanding a strange behavior:</p>
  
  <p>I have a STM32 microcontroller running FreeRTOS and an RTC interrupt running, too.
  The RTC interrupt just updates a <code>volatile uint32_t</code> variable named <code>SystemTime</code>:</p>
  
  <pre><code>void HAL_RTC_AlarmAEventCallback(RTC_HandleTypeDef *hrtc) {
    UNUSED(hrtc);
    SystemTime++;
  }
  </code></pre>
  
  <p>Additionally, I have another task that runs every 100 mS.
  It just prints the <code>SystemTime</code> value if it has changed.</p>
  
  <pre><code>static void ToggleLEDThread(void const *argument) {
    (void) argument;
    static uint32_t ost;
    uint32_t tst;
  
    for (;;) {
  #if 0
      tst = SystemTime;
      if (ost != tst) {
        xprintf("&lt;%d&gt;\n", tst);
        ost = tst;
      }
  #else
      if (ost != SystemTime) {
        xprintf("&lt;%d&gt;\n", SystemTime);
        ost = SystemTime;
      }
  #endif
      vTaskDelay(100);
    }
  }
  </code></pre>
  
  <p>It works as expected if <code>#if 1</code> (using a temporary variable), but when <code>#if 0</code>, the code runs ok for some time before it stops printing and after a few seconds starts to print again.</p>
  
  <p>There is another task that prints some other values once a second.</p>
  
  <p>Output when it's working:</p>
  
  <pre><code>V:1139  O:1091
  &lt;35&gt;
  V:1139  O:1123
  &lt;36&gt;
  V:1140  O:1154
  &lt;37&gt;
  V:1140  O:1186
  &lt;38&gt;
  V:1139  O:1218
  &lt;39&gt;
  V:1139  O:1249
  &lt;40&gt;
  V:1139  O:1281
  &lt;41&gt;
  V:1139  O:1313
  &lt;42&gt;
  V:1139  O:1344
  &lt;43&gt;
  V:1139  O:1376
  &lt;44&gt;
  V:1139  O:1408
  &lt;45&gt;
  V:1139  O:1439
  &lt;46&gt;
  V:1140  O:1471
  &lt;47&gt;
  V:1139  O:1503
  &lt;48&gt;
  V:1139  O:1535
  &lt;49&gt;
  V:1139  O:1566
  &lt;50&gt;
  V:1140  O:1598
  &lt;51&gt;
  V:1139  O:1630
  &lt;52&gt;
  V:1139  O:1661
  </code></pre>
  
  <p>Output when the problem happens:</p>
  
  <pre><code>V:1139  O:1091
  &lt;35&gt;
  V:1139  O:1123
  &lt;36&gt;
  V:1140  O:1154
  &lt;37&gt;
  V:1140  O:1186
  &lt;38&gt;
  V:1139  O:1218
  &lt;39&gt;
  V:1139  O:1249
  &lt;40&gt;
  V:1139  O:1281
  &lt;41&gt;
  V:1139  O:1313
  &lt;42&gt;
  V:1139  O:1344
  V:1139  O:1376
  V:1139  O:1408
  V:1139  O:1439
  V:1140  O:1471
  V:1139  O:1503
  V:1139  O:1535
  V:1139  O:1566
  &lt;50&gt;
  V:1140  O:1598
  &lt;51&gt;
  V:1139  O:1630
  &lt;52&gt;
  V:1139  O:1661
  </code></pre>
  
  <p>Any ideas?</p>