title: Why doesn't my Flash Control Register update when I write to it (STM32)?
body: <p>I'm trying to write to update my engineering bytes on the STM32G0 and I'm getting stuck. I need to unlock flash by writing to the FLASH_KEYR register. The manual says this:</p>
  <pre><code>After reset, write into the FLASH control register (FLASH_CR) is not allowed so as to protect the Flash memory against possible unwanted operations due, for example, to electric disturbances. The following sequence unlocks these registers:
  
  1. Write KEY1 = 0x4567 0123 in the FLASH key register (FLASH_KEYR)
  2. Write KEY2 = 0xCDEF 89AB in the FLASH key register (FLASH_KEYR).
  
  Any wrong sequence will lock the FLASH_CR registers until the next system reset. In the case of a wrong key sequence, a bus error is detected and a Hard Fault interrupt is generated.
  The FLASH_CR registers can be locked again by software by setting the LOCK bit in one of these registers.
  
  Note: The FLASH_CR register cannot be written when the BSY1 bit of the FLASH status register (FLASH_SR) is set. Any attempt to write to this register with the BSY1 bit set causes the AHB bus to stall until the BSY1 bit is cleared.
  </code></pre>
  <p>I making sure that my BSY1 bit is clear when I'm trying to write and I believe that's the only condition.</p>
  <p>This is how I'm writing:</p>
  <pre><code>#define KEY1  0x45670123
  #define KEY2  0xCDEF89AB
  
  ... 
  
  FLASH-&gt;KEYR = KEY1;
  FLASH-&gt;KEYR = KEY2;
  </code></pre>
  <p>After this point, my FLASH_KEYR register is still 0x0. Does anyone know why?</p>
  <p>Edit: here's my code</p>
  <pre><code>        #define TEST_FLASH_BYTES    ((uint32_t)0x0801FF00)
  
          ...
  
          FLASH-&gt;CR &amp;= FLASH_CR_LOCK;
          FLASH-&gt;KEYR = KEY1;
          FLASH-&gt;KEYR = KEY2;
  
          while(__HAL_FLASH_GET_FLAG(FLASH_SR_BSY1) != 0) {}
  
          program_flash(TEST_FLASH_BYTES, 0x1234567887654321);
  
          while (__HAL_FLASH_GET_FLAG(FLASH_SR_BSY1) != 0) {}
  
          FLASH-&gt;SR &amp;= FLASH_SR_EOP;
          FLASH-&gt;CR &amp;= FLASH_CR_PG;
  </code></pre>
  <p>here is <code>program_flash</code>:</p>
  <pre><code>static void program_flash(uint32_t address, uint64_t data)
  {
      *(uint32_t *)address = (uint32_t)data;
  
      (independently of compiler optimization behavior) */
      __ISB();
  
      *(uint32_t *)(address + 4U) = (uint32_t)(data &gt;&gt; 32U);
  }
  </code></pre>
  <p>Edit: it's a write only register so that's why I can't read it back. I figured out how to write to my memory successfully.</p>