title: Which variable types/sizes are atomic on STM32 microcontrollers?
body: <p>Here are the data types on STM32 microcontrollers: <a href="http://www.keil.com/support/man/docs/armcc/armcc_chr1359125009502.htm" rel="nofollow noreferrer">http://www.keil.com/support/man/docs/armcc/armcc_chr1359125009502.htm</a>.</p>
  <p>These microcontrollers use 32-bit ARM core processors.</p>
  <p>Which data types have automatic atomic read and atomic write access?</p>
  <p>I'm pretty sure all 32-bit data types do (since the processor is 32-bits), and all 64-bit data types do NOT (since it would take at least 2 processor operations to read or write a 64-bit word), but what about <code>bool</code> (1 byte), and <code>uint16_t</code>/<code>int16_t</code> (2 bytes)?</p>
  <p>Context: I'm sharing variables between multiple threads (single core, but multiple threads, or &quot;tasks&quot; as they are called, in <a href="https://www.freertos.org/" rel="nofollow noreferrer">FreeRTOS</a>) on the STM32 and need to know if I need to enforce atomic access by turning off interrupts, using mutexes, etc.</p>
  <p>UPDATE:</p>
  <p>Refering to this sample code:</p>
  <pre class="lang-c prettyprint-override"><code>volatile bool shared_bool;
  volatile uint8_t shared u8;
  volatile uint16_t shared_u16;
  volatile uint32_t shared_u32;
  volatile uint64_t shared_u64;
  volatile float shared_f; // 32-bits
  volatile double shared_d; // 64-bits
  
  // Task (thread) 1
  while (true)
  {
      // Write to the values in this thread.
      //
      // What I write to each variable will vary. Since other threads are reading
      // these values, I need to ensure my *writes* are atomic, or else I must
      // use a mutex to prevent another thread from reading a variable in the
      // middle of this thread's writing.
      shared_bool = true;
      shared_u8 = 129;
      shared_u16 = 10108;
      shared_u32 = 130890;
      shared_f = 1083.108;
      shared_d = 382.10830;
  }
  
  // Task (thread) 2
  while (true)
  {
      // Read from the values in this thread.
      //
      // What thread 1 writes into these values can change at any time, so I need
      // to ensure my *reads* are atomic, or else I'll need to use a mutex to
      // prevent the other thread from writing to a variable in the midst of
      // reading it in this thread.
      if (shared_bool == whatever)
      {
          // do something
      }
      if (shared_u8 == whatever)
      {
          // do something
      }
      if (shared_u16 == whatever)
      {
          // do something
      }
      if (shared_u32 == whatever)
      {
          // do something
      }
      if (shared_u64 == whatever)
      {
          // do something
      }
      if (shared_f == whatever)
      {
          // do something
      }
      if (shared_d == whatever)
      {
          // do something
      }
  }
  </code></pre>
  <p><strong>In the code above, which variables can I do this for without using a mutex?</strong> My suspicion is as follows:</p>
  <ol>
  <li><code>volatile bool</code>: safe--no mutex required</li>
  <li><code>volatile uint8_t</code>: safe--no mutex required</li>
  <li><code>volatile uint16_t</code>: safe--no mutex required</li>
  <li><code>volatile uint32_t</code>: safe--no mutex required</li>
  <li><code>volatile uint64_t</code>: UNSAFE--YOU MUST USE A Critical section or MUTEX!</li>
  <li><code>volatile float</code>: safe--no mutex required</li>
  <li><code>volatile double</code>: UNSAFE--YOU MUST USE A Critical section or MUTEX!</li>
  </ol>
  <p>Example critical section with FreeRTOS:</p>
  <ul>
  <li><p><a href="https://www.freertos.org/taskENTER_CRITICAL_taskEXIT_CRITICAL.html" rel="nofollow noreferrer">https://www.freertos.org/taskENTER_CRITICAL_taskEXIT_CRITICAL.html</a></p>
  <p>// Force atomic access with these critical section atomic access guards.
  taskENTER_CRITICAL();
  // do the (now guaranteed to be safe) read or write here
  taskEXIT_CRITICAL();</p>
  </li>
  </ul>
  <h1>Related, but not answering my question:</h1>
  <ol>
  <li><a href="https://stackoverflow.com/questions/11894059/atomic-operations-in-arm">Atomic operations in ARM</a></li>
  <li><a href="https://stackoverflow.com/questions/9399026/arm-is-writing-reading-from-int-atomic">ARM: Is writing/reading from int atomic?</a></li>
  <li>(My own question and answer on atomicity in 8-bit AVR [and Arduino] microcontrollers): <a href="https://stackoverflow.com/a/39693278/4561887">https://stackoverflow.com/a/39693278/4561887</a></li>
  <li><a href="https://stm32f4-discovery.net/2015/06/how-to-properly-enabledisable-interrupts-in-arm-cortex-m/" rel="nofollow noreferrer">https://stm32f4-discovery.net/2015/06/how-to-properly-enabledisable-interrupts-in-arm-cortex-m/</a></li>
  </ol>