stm32f4 hal 内部flash
uint16_t MEM_If_Init_FS(void)
-
{
-
-
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);
-
-
}
-
uint16_t MEM_If_DeInit_FS(void)
-
{
-
HAL_FLASH_Lock();
-
}
-
flash 的檫除操作
-
uint16_t MEM_If_Erase_FS(uint32_t start_Add,uint32_t end_Add)
-
{
-
/* USER CODE BEGIN 3 */
-
uint32_t UserStartSector;
-
uint32_t SectorError;
-
FLASH_EraseInitTypeDef pEraseInit;
-
-
/* Unlock the Flash to enable the flash control register access *************/
-
MEM_If_Init_FS();
-
-
/* Get the sector where start the user flash area */
-
UserStartSector = GetSector(start_Add);
-
-
pEraseInit.TypeErase = TYPEERASE_SECTORS;
-
pEraseInit.Sector = UserStartSector;
-
pEraseInit.NbSectors = GetSector(end_Add)-UserStartSector+1 ;
-
pEraseInit.VoltageRange = VOLTAGE_RANGE_3;
-
-
if (HAL_FLASHEx_Erase(&pEraseInit, &SectorError) != HAL_OK)
-
{
-
/* Error occurred while page erase */
-
return (1);
-
}
-
-
return (USBD_OK);
-
/* USER CODE END 3 */
-
}
擦除操作,先解锁,然后清空所需flash所在sector。注意这里的sector使用GetSector()函数获取的,是一个整型数字(对于F4就是0~11)。
其中的NbSectors是需要清除的sector的个数。
下面是获取sector的编号
我们先按照用户手册上写的在文件usbd_dfu_if.h中define一些需要用到的sector起始地址
再在usbd_dfu_if.c文件末尾添加函数:
-
/**
-
-
-
-
*/
-
static uint32_t GetSector(uint32_t Address)
-
{
-
uint32_t 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;
-
}
-
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
-
{
-
sector = FLASH_SECTOR_11;
-
}
-
-
return sector;
-
}
flash写入操作
-
uint16_t MEM_If_Write_FS(uint8_t *src, uint8_t *dest, uint32_t Len)
-
{
-
/* USER CODE BEGIN 3 */
-
uint32_t i = 0;
-
-
for(i = 0; i < Len; i+=4)
-
{
-
/* Device voltage range supposed to be [2.7V to 3.6V], the operation will
-
be done by byte */
-
if(HAL_FLASH_Program(FLASH_TYPEPROGRAM_WORD, (uint32_t)(dest+i), *(uint32_t*)(src+i)) == HAL_OK)
-
{
-
/* Check the written value */
-
if(*(uint32_t *)(src + i) != *(uint32_t*)(dest+i))
-
{
-
/* Flash content doesn't match SRAM content */
-
return 2;
-
}
-
}
-
else
-
{
-
/* Error occurred while writing data in Flash memory */
-
return 1;
-
}
-
}
-
return (HAL_OK);
-
/* USER CODE END 3 */
-
}
flash读出操作
-
uint8_t *MEM_If_Read_FS (uint8_t *src, uint8_t *dest, uint32_t Len)
-
{
-
/* Return a valid address to avoid HardFault */
-
/* USER CODE BEGIN 4 */
-
uint32_t i = 0;
-
uint8_t *psrc = src;
-
-
for(i = 0; i < Len; i++)
-
{
-
dest[i] = *psrc++;
-
}
-
return HAL_OK;
-
-
/* USER CODE END 4 */
-
}