title: HAL drivers erase/read/write flash on STM32F4 nucleo
body: <pre><code>uint32_t PAGEError = 0;
  FLASH_EraseInitTypeDef EraseInitStruct;
  EraseInitStruct.TypeErase   = FLASH_TYPEERASE_SECTORS ;
  EraseInitStruct.Sector   = FLASH_SECTOR_0;
  EraseInitStruct.VoltageRange = FLASH_VOLTAGE_RANGE_3;
  
  HAL_FLASH_Unlock();
  __HAL_FLASH_CLEAR_FLAG(FLASH_FLAG_EOP | FLASH_FLAG_OPERR | FLASH_FLAG_WRPERR | FLASH_FLAG_PGAERR | FLASH_FLAG_PGPERR | FLASH_FLAG_PGSERR);
  HAL_FLASHEx_Erase(&amp;EraseInitStruct, &amp;PAGEError);
  HAL_FLASH_Program(FLASH_TYPEPROGRAM_WORD, 0x08000000, counter)
  HAL_FLASH_Lock();
  
  counter2 = *(__IO uint32_t *)0x08000000;
  counter3 = *(__IO uint32_t *)0x08000001;
  counter4 = *(__IO uint32_t *)0x08000002;
  
  sprintf(buf, "%d", counter2); //gets send to the OLED with I2C
  sprintf(buf2, "%d", counter3);
  sprintf(buf3, "%d", counter4);
  </code></pre>
  
  <p>I want to write the variable counter to the flash and then read it as <code>counter2</code>.
  The first flash sector starts at <code>0x08000000</code>.</p>
  
  <p><code>counter2</code>, <code>3</code> and <code>4</code> are display trough an OLED screen. 
  Displaying <code>counter2</code> works and shows me the value of <code>counter-1</code>, but it works only once. If I write again to the flash nothing seems to happen. </p>
  
  <p><code>counter3</code> and <code>counter4</code> don't work at all.</p>
  
  <p>Output on the OLED when <code>counter=0x00000008</code> after I have erased the flash but not written anything:</p>
  
  <pre><code>counter2: 536873624
  counter3: -652214262
  counter4: 31006720
  </code></pre>
  
  <p>And after writting and ressetting:</p>
  
  <pre><code>counter2: 8
  counter3: -654311424
  counter4: 30998528
  </code></pre>
  
  <p>What is going on here? Can someone tell me why all variables change?
  Do I have to configure the linker?</p>