天天看點

STM32F7xx —— 内部flash                          STM32F7xx —— 内部flash

                          STM32F7xx —— 内部flash

這個就沒什麼好說的了,直接上代碼了,主要封裝了三個函數,擦除,寫flash,讀flash。

// STM32F767IGT6: 1M flash
// STM32F767ZIT6: 2M flash
#define ADDR_FLASH_SECTOR_0     ((uint32_t)0x08000000) /* Base @ of Sector 0, 32 Kbytes */
#define ADDR_FLASH_SECTOR_1     ((uint32_t)0x08008000) /* Base @ of Sector 1, 32 Kbytes */
#define ADDR_FLASH_SECTOR_2     ((uint32_t)0x08010000) /* Base @ of Sector 2, 32 Kbytes */
#define ADDR_FLASH_SECTOR_3     ((uint32_t)0x08018000) /* Base @ of Sector 3, 32 Kbytes */
#define ADDR_FLASH_SECTOR_4     ((uint32_t)0x08020000) /* Base @ of Sector 4, 128 Kbytes */
#define ADDR_FLASH_SECTOR_5     ((uint32_t)0x08040000) /* Base @ of Sector 5, 256 Kbytes */
#define ADDR_FLASH_SECTOR_6     ((uint32_t)0x08080000) /* Base @ of Sector 6, 256 Kbytes */
#define ADDR_FLASH_SECTOR_7     ((uint32_t)0x080C0000) /* Base @ of Sector 7, 256 Kbytes */
#ifdef STM32F767ZIT6
#define ADDR_FLASH_SECTOR_8     ((uint32_t)0x08100000) /* Base @ of Sector 8, 256 Kbytes */
#define ADDR_FLASH_SECTOR_9     ((uint32_t)0x08140000) /* Base @ of Sector 9, 256 Kbytes */
#define ADDR_FLASH_SECTOR_10    ((uint32_t)0x08180000) /* Base @ of Sector 10, 256 Kbytes */
#define ADDR_FLASH_SECTOR_11    ((uint32_t)0x081C0000) /* Base @ of Sector 11, 256 Kbytes */
#endif
           
// 擷取要擦除扇區編号
static uint32_t flash_sector_number(uint32_t address)
{
  uint32_t sector = 0;

  if((address < ADDR_FLASH_SECTOR_1) && (address >= ADDR_FLASH_SECTOR_0))
  {
    sector = FLASH_SECTOR_0;
  }
  else if((address < ADDR_FLASH_SECTOR_2) && (address >= ADDR_FLASH_SECTOR_1))
  {
    sector = FLASH_SECTOR_1;
  }
  else if((address < ADDR_FLASH_SECTOR_3) && (address >= ADDR_FLASH_SECTOR_2))
  {
    sector = FLASH_SECTOR_2;
  }
  else if((address < ADDR_FLASH_SECTOR_4) && (address >= ADDR_FLASH_SECTOR_3))
  {
    sector = FLASH_SECTOR_3;
  }
  else if((address < ADDR_FLASH_SECTOR_5) && (address >= ADDR_FLASH_SECTOR_4))
  {
    sector = FLASH_SECTOR_4;
  }
  else if((address < ADDR_FLASH_SECTOR_6) && (address >= ADDR_FLASH_SECTOR_5))
  {
    sector = FLASH_SECTOR_5;
  }
  else if((address < ADDR_FLASH_SECTOR_7) && (address >= ADDR_FLASH_SECTOR_6))
  {
    sector = FLASH_SECTOR_6;
  }
#ifdef STM32F767ZIT6
  else if((address < ADDR_FLASH_SECTOR_8) && (address >= ADDR_FLASH_SECTOR_7))
  {
    sector = FLASH_SECTOR_7;
  }
  else if((address < ADDR_FLASH_SECTOR_9) && (address >= ADDR_FLASH_SECTOR_8))
  {
    sector = FLASH_SECTOR_8;
  }
  else if((address < ADDR_FLASH_SECTOR_10) && (address >= ADDR_FLASH_SECTOR_9))
  {
    sector = FLASH_SECTOR_9;
  }
  else if((address < ADDR_FLASH_SECTOR_11) && (address >= ADDR_FLASH_SECTOR_10))
  {
    sector = FLASH_SECTOR_10;
  }
  else if((address < 0x081C0000) && (address >= ADDR_FLASH_SECTOR_11))
  {
    sector = FLASH_SECTOR_11;
  }
#else
  else if(address >= ADDR_FLASH_SECTOR_7)
  {
    sector = FLASH_SECTOR_6;
  }
#endif

  return sector;
}

// 擦除指定扇區資料 仔細的朋友會發現count沒有用到,這裡可以任意更改,我是為了相容F1的命名。
void SocFlashErase(uint32_t addr, uint8_t count)
{
  FLASH_EraseInitTypeDef flash_erase;
  uint32_t flash_error;

  HAL_FLASH_Unlock();

  flash_erase.TypeErase = FLASH_TYPEERASE_SECTORS;  // 擦除類型,扇區擦除
  flash_erase.Sector = flash_sector_number(addr);   // 要擦除的扇區
  flash_erase.NbSectors = 1;                        // 一次隻擦除一個扇區 這裡可以使用參數的count 為了防止錯誤操作這裡寫成1
  flash_erase.VoltageRange = FLASH_VOLTAGE_RANGE_3; // 電壓範圍,VCC=2.7~3.6V之間!!

  __HAL_FLASH_CLEAR_FLAG(FLASH_FLAG_EOP | FLASH_FLAG_OPERR | FLASH_FLAG_WRPERR |
                         FLASH_FLAG_PGAERR | FLASH_FLAG_PGPERR | FLASH_FLAG_ERSERR | FLASH_FLAG_BSY);

  HAL_FLASHEx_Erase(&flash_erase, &flash_error);

  FLASH_WaitForLastOperation(50000);

  HAL_FLASH_Lock();
}

uint8_t SocFlashWrite(uint32_t addr, uint8_t *buffer, uint32_t length)
{
  uint32_t i;
  uint16_t data = 0;

  if (length == 0)
  {
    return 0;
  }

  HAL_FLASH_Unlock();

  __HAL_FLASH_CLEAR_FLAG(FLASH_FLAG_EOP | FLASH_FLAG_OPERR | FLASH_FLAG_WRPERR |
                         FLASH_FLAG_PGAERR | FLASH_FLAG_PGPERR | FLASH_FLAG_ERSERR | FLASH_FLAG_BSY);

  // 按半字程式設計
  for (i = 0; i < length; i += 2)
  {
    data = (*(buffer + i + 1) << 8) + (*(buffer + i));
    HAL_FLASH_Program(FLASH_TYPEPROGRAM_HALFWORD, (uint32_t)(addr + i), data);
  }

  HAL_FLASH_Lock();

  return 0;
}

uint32_t SocFlashRead(uint32_t addr, uint8_t *buffer, uint32_t length)
{
  memcpy(buffer, (void *)addr, length);

  return length;
}
           

F1内部flash