.h文件
#ifndef FLASH_H
#define FLASH_H
#include "sys.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 ADDR_FLASH_SECTOR_13 ((uint32_t)0x08104000)
#define ADDR_FLASH_SECTOR_14 ((uint32_t)0x08108000)
#define ADDR_FLASH_SECTOR_15 ((uint32_t)0x0810C000)
#define ADDR_FLASH_SECTOR_16 ((uint32_t)0x08110000)
#define ADDR_FLASH_SECTOR_17 ((uint32_t)0x08120000)
#define ADDR_FLASH_SECTOR_18 ((uint32_t)0x08140000)
#define ADDR_FLASH_SECTOR_19 ((uint32_t)0x08160000)
#define ADDR_FLASH_SECTOR_20 ((uint32_t)0x08180000)
#define ADDR_FLASH_SECTOR_21 ((uint32_t)0x081A0000)
#define ADDR_FLASH_SECTOR_22 ((uint32_t)0x081C0000)
#define ADDR_FLASH_SECTOR_23 ((uint32_t)0x081E0000)
#define FLASH_END_ADDR ((uint32_t)0x08200000)
extern int8_t flash_write_single_address ( uint32_t address, uint32_t * buf, uint32_t len) ;
extern int8_t flash_write_muli_address ( uint32_t start_address, uint32_t end_address, uint32_t * buf, uint32_t len) ;
extern void flash_read ( uint32_t address, uint32_t * buf, uint32_t len) ;
extern void write_protect ( void ) ;
extern void write_relieve_protect ( void ) ;
#endif
.c文件
#include "flash.h"
#include "stm32f4xx.h"
static uint32_t GetSector ( unsigned int Address) ;
static uint32_t Get_Next_Flash_Address ( uint32_t Address) ;
static int8_t FLASH_Erase_Muli_Sector ( uint32_t start_Address, uint32_t end_Address, uint8_t VoltageRange) ;
int8_t flash_write_single_address ( uint32_t Address, uint32_t * buf, uint32_t len)
{
uint32_t uwSector = 0 ;
uint32_t uwAddress = 0 ;
uint32_t uwEndAddress = 0 ;
uint32_t uwSectorCounter = 0 ;
uint32_t * data_buf;
uint32_t data_len;
__IO uint32_t uwData32 = 0 ;
__IO uint32_t uwMemoryProgramStatus = 0 ;
FLASH_Unlock ( ) ;
FLASH_ClearFlag ( FLASH_FLAG_EOP | FLASH_FLAG_OPERR | FLASH_FLAG_WRPERR |
FLASH_FLAG_PGAERR | FLASH_FLAG_PGPERR | FLASH_FLAG_PGSERR) ;
uwSector = GetSector ( Address) ;
uwSectorCounter = uwSector;
if ( FLASH_EraseSector ( uwSectorCounter, VoltageRange_3) != FLASH_COMPLETE)
{
return - 1 ;
}
uwAddress = Address;
uwEndAddress = Get_Next_Flash_Address ( Address) ;
data_buf = buf;
data_len = 0 ;
while ( uwAddress <= uwEndAddress)
{
if ( FLASH_ProgramWord ( uwAddress, * data_buf) == FLASH_COMPLETE)
{
uwAddress = uwAddress + 4 ;
data_buf++ ;
data_len++ ;
if ( data_len == len)
{