一.在文件stm32f4xx_hal_flash_ex.h中有对应的文件标号定义如下:
#if defined(STM32F405xx) || defined(STM32F415xx) || defined(STM32F407xx) || defined(STM32F417xx) || defined(STM32F412Zx) ||\
defined(STM32F412Vx) || defined(STM32F412Rx) || defined(STM32F412Cx)
#define OB_WRP_SECTOR_0 0x00000001U
#define OB_WRP_SECTOR_1 0x00000002U
#define OB_WRP_SECTOR_2 0x00000004U
#define OB_WRP_SECTOR_3 0x00000008U
#define OB_WRP_SECTOR_4 0x00000010U
#define OB_WRP_SECTOR_5 0x00000020U
#define OB_WRP_SECTOR_6 0x00000040U
#define OB_WRP_SECTOR_7 0x00000080U
#define OB_WRP_SECTOR_8 0x00000100U
#define OB_WRP_SECTOR_9 0x00000200U
#define OB_WRP_SECTOR_10 0x00000400U
#define OB_WRP_SECTOR_11 0x00000800U
#define OB_WRP_SECTOR_All 0x00000FFFU
#endif
对于头文件的定义internal_flash.h用户自定义,以1mb的内部flash的stm32为例:
#ifndef _INTERNAL_FLASH_H
#define _INTERNAL_FLASH_H
#include "main.h"
#define ADDR_FLASH_SECTOR_0 ((uint32_t)0x08000000)
#define ADDR_FLASH_SECTOR_1 ((uint32_t)0x08004000)
#define ADDR_FLASH_SECTOR_2 ((uint32_t)0x08008000)
#define ADDR_FLASH_SECTOR_3 ((uint32_t)0x0800C000)
#define ADDR_FLASH_SECTOR_4 ((uint32_t)0x08010000)
#define ADDR_FLASH_SECTOR_5 ((uint32_t)0x08020000)
#define ADDR_FLASH_SECTOR_6 ((uint32_t)0x08040000)
#define ADDR_FLASH_SECTOR_7 ((uint32_t)0x08060000)
#define ADDR_FLASH_SECTOR_8 ((uint32_t)0x08080000)
#define ADDR_FLASH_SECTOR_9 ((uint32_t)0x080A0000)
#define ADDR_FLASH_SECTOR_10 ((uint32_t)0x080C0000)
#define ADDR_FLASH_SECTOR_11 ((uint32_t)0x080E0000)
#define ADDR_FLASH_SECTOR_12 ((uint32_t)0x08100000)
#define NORMAL_APP_EXIST_FLAG_ADDR ADDR_FLASH_SECTOR_7
#define NORMAL_APP_EXIST_FLAG_SIZE 4
#define NORMAL_APP_Version_ADDR NORMAL_APP_EXIST_FLAG_ADDR+NORMAL_APP_EXIST_FLAG_SIZE
#define NORMAL_APP_Version_SIZE 4
#define NORMAL_APP_CRC_ADDR NORMAL_APP_Version_ADDR+NORMAL_APP_Version_SIZE
#define NORMAL_APP_CRC_SIZE 4
#define NORMAL_APP_LEN_ADDR NORMAL_APP_CRC_ADDR+NORMAL_APP_CRC_SIZE
#define NORMAL_APP_LEN_SIZE 4
#define NORMAL_APP_DATA_ADDR NORMAL_APP_LEN_ADDR+NORMAL_APP_LEN_SIZE
#define SELCHECK_APP_EXIST_FLAG_ADDR ADDR_FLASH_SECTOR_6
#define SELCHECK_APP_EXIST_FLAG_SIZE 4
#define SELCHECK_APP_Version_ADDR SELCHECK_APP_EXIST_FLAG_ADDR+SELCHECK_APP_EXIST_FLAG_SIZE
#define SELCHECK_APP_Version_SIZE 4
#define SELCHECK_APP_CRC_ADDR SELCHECK_APP_Version_ADDR+SELCHECK_APP_Version_SIZE
#define SELCHECK_APP_CRC_SIZE 4
#define SELCHECK_APP_LEN_ADDR SELCHECK_APP_CRC_ADDR+SELCHECK_APP_CRC_SIZE
#define SELCHECK_APP_LEN_SIZE 4
#define SELCHECK_APP_DATA_ADDR SELCHECK_APP_LEN_ADDR+SELCHECK_APP_LEN_SIZE
#define STM32_FLASH_BASE 0x08000000
#define FLASH_WAITETIME 50000
void Flash_Write(uint32_t WriteAddr,uint32_t *pBuffer,uint32_t NumToWrite);
void Flash_Read(uint32_t ReadAddr,uint32_t *pBuffer,uint32_t NumToRead);
void Flash_Test(void);
#endif
internal_flash.c文件的定义如下:
#include "internal_flash.h"
#if defined(flash_CONN_LOG)
#define flash_log_print(format, ...) printf(format, ##__VA_ARGS__)
#else
#define flash_log_print(format, ...)
#endif
static uint8_t Flash_GetFlashSector(uint32_t addr)
{
if(addr<ADDR_FLASH_SECTOR_1)return 0;
else if(addr<ADDR_FLASH_SECTOR_2)return 1;
else if(addr<ADDR_FLASH_SECTOR_3)return 2;
else if(addr<ADDR_FLASH_SECTOR_4)return 3;
else if(addr<ADDR_FLASH_SECTOR_5)return 4;
else if(addr<ADDR_FLASH_SECTOR_6)return 5;
else if(addr<ADDR_FLASH_SECTOR_7)return 6;
else if(addr<ADDR_FLASH_SECTOR_8)return 7;
else if(addr<ADDR_FLASH_SECTOR_9)return 8;
else if(addr<ADDR_FLASH_SECTOR_10)return 9;
else if(addr<ADDR_FLASH_SECTOR_11)return 10;
else return 10;
}
static uint32_t Flash_ReadWord(uint32_t faddr)
{
return *(int32_t*)faddr;
}
void Flash_Read(uint32_t ReadAddr,uint32_t *pBuffer,uint32_t NumToRead)
{
uint32_t i;
for(i=0;i<NumToRead;i++)
{
pBuffer[i]=Flash_ReadWord(ReadAddr);
ReadAddr+=4;
}
}
void Flash_Write(uint32_t WriteAddr,uint32_t *pBuffer,uint32_t NumToWrite)
{
FLASH_EraseInitTypeDef FlashEraseInit;
HAL_StatusTypeDef FlashStatus=HAL_OK;
uint32_t SectorError=0;
uint32_t addrx=0,endaddr=0;
if(WriteAddr<STM32_FLASH_BASE||WriteAddr%4)return;
HAL_FLASH_Unlock();
addrx=WriteAddr;
endaddr=WriteAddr+NumToWrite*4;
if(addrx<ADDR_FLASH_SECTOR_12)
{
while(addrx<endaddr)
{
if(Flash_ReadWord(addrx)!=0XFFFFFFFF)
{
FlashEraseInit.TypeErase=FLASH_TYPEERASE_SECTORS;
FlashEraseInit.Sector=Flash_GetFlashSector(addrx);
FlashEraseInit.NbSectors=1;
FlashEraseInit.VoltageRange=FLASH_VOLTAGE_RANGE_3;
if(HAL_FLASHEx_Erase(&FlashEraseInit,&SectorError)!=HAL_OK)
{
break;
}
}
else
addrx+=4;
FLASH_WaitForLastOperation(FLASH_WAITETIME);
}
}
FlashStatus=FLASH_WaitForLastOperation(FLASH_WAITETIME);
if(FlashStatus==HAL_OK)
{
while(WriteAddr<endaddr)
{
if(HAL_FLASH_Program(FLASH_TYPEPROGRAM_WORD,WriteAddr,*pBuffer)!=HAL_OK)
{
break;
}
WriteAddr+=4;
pBuffer++;
}
}
HAL_FLASH_Lock();
}
void Flash_Test(void)
{
uint32_t test = 0x01020304;
uint32_t test1 = 0xaabbccdd;
uint32_t test2 = 0xaabb4098;
Flash_Write(SELCHECK_APP_EXIST_FLAG_ADDR, &test, 1);
Flash_Read(SELCHECK_APP_EXIST_FLAG_ADDR, &test, 1);
flash_log_print("test:%x\n",test);
Flash_Write(SELCHECK_APP_Version_ADDR, &test1, 1);
Flash_Read(SELCHECK_APP_Version_ADDR, &test1, 1);
flash_log_print("test1:%x\n",test1);
Flash_Write(NORMAL_APP_CRC_ADDR, &test2, 1);
Flash_Read(NORMAL_APP_CRC_ADDR, &test2, 1);
flash_log_print("test2:%x\n",test2);
}
|