#ifndef __STMFLASH_H
#define __STMFLASH_H

#ifdef __cplusplus
	extern "C" {
#endif

typedef enum 
{
    STM32_F1XX,
    STM32_F4XX,
} STM32_FSeries_TypeDef;

typedef enum 
{
    STM_FALSH_IGT6,
    STM_FALSH_ZGT6,
    STM_FALSH_ZET6,
    STM_FALSH_VET6,
    STM_FALSH_RET6,
    STM_FALSH_RBT6,
    STM_FALSH_RCT6,
    STM_FALSH_C8T6,
} STM_FALSH_TypeDef;

#define  STM32_FSeries_TYPE STM32_F4XX
#define  STM_FALSH_TYPE STM_FALSH_ZGT6

 
#include "main.h"

//FLASH起始地址
#define STM32_FLASH_BASE    0x08000000 	//STM32 FLASH的起始地址
#define FLASH_WAITETIME     50000          //FLASH等待超时时间

#if STM_FALSH_TYPE == STM_FALSH_ZGT6 || STM_FALSH_TYPE == STM_FALSH_IGT6 || STM_FALSH_TYPE == STM_FALSH_ZET6
#define STM32_FLASH_SIZE    0x00100000 	//STM32 FLASH的起始地址  1M
#define STM32_PAGE_SIZE     0x00000800 	//STM32 页大小 2K
#define STM32_LAST_SECTOR_ADDR     0x080E0000 	//STM32 页大小 2K
#define STM32_LAST_SECTOR_SIZE     0x00020000 	//STM32 页大小 128K
#endif

//FLASH 扇区的起始地址
#define ADDR_FLASH_SECTOR_0     ((uint32_t)0x08000000) 	//扇区0起始地址, 16 Kbytes  
#define ADDR_FLASH_SECTOR_1     ((uint32_t)0x08004000) 	//扇区1起始地址, 16 Kbytes  
#define ADDR_FLASH_SECTOR_2     ((uint32_t)0x08008000) 	//扇区2起始地址, 16 Kbytes  
#define ADDR_FLASH_SECTOR_3     ((uint32_t)0x0800C000) 	//扇区3起始地址, 16 Kbytes  
#define ADDR_FLASH_SECTOR_4     ((uint32_t)0x08010000) 	//扇区4起始地址, 64 Kbytes  
#define ADDR_FLASH_SECTOR_5     ((uint32_t)0x08020000) 	//扇区5起始地址, 128 Kbytes  
#define ADDR_FLASH_SECTOR_6     ((uint32_t)0x08040000) 	//扇区6起始地址, 128 Kbytes  
#define ADDR_FLASH_SECTOR_7     ((uint32_t)0x08060000) 	//扇区7起始地址, 128 Kbytes  
#define ADDR_FLASH_SECTOR_8     ((uint32_t)0x08080000) 	//扇区8起始地址, 128 Kbytes  
#define ADDR_FLASH_SECTOR_9     ((uint32_t)0x080A0000) 	//扇区9起始地址, 128 Kbytes  
#define ADDR_FLASH_SECTOR_10    ((uint32_t)0x080C0000) 	//扇区10起始地址,128 Kbytes  
#define ADDR_FLASH_SECTOR_11    ((uint32_t)0x080E0000) 	//扇区11起始地址,128 Kbytes 

 
uint32_t STMFLASH_ReadWord(uint32_t faddr);		  	//读出字  
void STMFLASH_Write(uint32_t WriteAddr,uint32_t *pBuffer,uint32_t NumToWrite);		//从指定地址开始写入指定长度的数据
void STMFLASH_Read(uint32_t ReadAddr,uint32_t *pBuffer,uint32_t NumToRead);   		//从指定地址开始读出指定长度的数据
int STMFLASH_ErasePage(uint32_t Page_Address);   		//从指定地址开始擦除页

//测试写入
void Test_Write(uint32_t WriteAddr,uint32_t WriteData);	
 
 

#ifdef __cplusplus
	}
#endif
#endif

/*
F103 可以用页擦除， 以页为单位操作。 F407以扇区为单位操作
擦除页函数   FLASH_ErasePage(uint32_t Page_Address);  // F407无此函数
擦除扇区函数  HAL_FLASHEx_Erase(&FlashEraseInit,&SectorError)

擦除操作，以扇区为单位，不是以扇区为单位
写入， 至少16bit, 8bit 写入也是占2个位置，32bit = 2*16bit, 以半字为单位
 


STM32F407ZET6  内部Flash分区  512K
#define ADDR_FLASH_SECTOR_0     ((uint32_t)0x08000000) 	//扇区0起始地址, 16 Kbytes  
#define ADDR_FLASH_SECTOR_1     ((uint32_t)0x08004000) 	//扇区1起始地址, 16 Kbytes  
#define ADDR_FLASH_SECTOR_2     ((uint32_t)0x08008000) 	//扇区2起始地址, 16 Kbytes  
#define ADDR_FLASH_SECTOR_3     ((uint32_t)0x0800C000) 	//扇区3起始地址, 16 Kbytes  
#define ADDR_FLASH_SECTOR_4     ((uint32_t)0x08010000) 	//扇区4起始地址, 64 Kbytes  
#define ADDR_FLASH_SECTOR_5     ((uint32_t)0x08020000) 	//扇区5起始地址, 128 Kbytes  
#define ADDR_FLASH_SECTOR_6     ((uint32_t)0x08040000) 	//扇区6起始地址, 128 Kbytes  
#define ADDR_FLASH_SECTOR_7     ((uint32_t)0x08060000) 	//扇区7起始地址, 128 Kbytes  

STM32F407ZGT6  内部Flash分区   1M
#define ADDR_FLASH_SECTOR_0     ((uint32_t)0x08000000) 	//扇区0起始地址, 16 Kbytes  
#define ADDR_FLASH_SECTOR_1     ((uint32_t)0x08004000) 	//扇区1起始地址, 16 Kbytes  
#define ADDR_FLASH_SECTOR_2     ((uint32_t)0x08008000) 	//扇区2起始地址, 16 Kbytes  
#define ADDR_FLASH_SECTOR_3     ((uint32_t)0x0800C000) 	//扇区3起始地址, 16 Kbytes  
#define ADDR_FLASH_SECTOR_4     ((uint32_t)0x08010000) 	//扇区4起始地址, 64 Kbytes  
#define ADDR_FLASH_SECTOR_5     ((uint32_t)0x08020000) 	//扇区5起始地址, 128 Kbytes  
#define ADDR_FLASH_SECTOR_6     ((uint32_t)0x08040000) 	//扇区6起始地址, 128 Kbytes  
#define ADDR_FLASH_SECTOR_7     ((uint32_t)0x08060000) 	//扇区7起始地址, 128 Kbytes  
#define ADDR_FLASH_SECTOR_8     ((uint32_t)0x08080000) 	//扇区8起始地址, 128 Kbytes  
#define ADDR_FLASH_SECTOR_9     ((uint32_t)0x080A0000) 	//扇区9起始地址, 128 Kbytes  
#define ADDR_FLASH_SECTOR_10    ((uint32_t)0x080C0000) 	//扇区10起始地址,128 Kbytes  
#define ADDR_FLASH_SECTOR_11    ((uint32_t)0x080E0000) 	//扇区11起始地址,128 Kbytes 

STM32F429IGT6  内部Flash分区  -- 同上 



STM32F103C8T6  内部Flash分区  64K， 64*1024Bytes  单个扇区 1K， 0x400
64K (F103C8T6 )  --->>>> 单个扇区 1K 0x400  最后一个扇区地址：0x0800FC00
128K (F103RCT6)  --->>>> 单个扇区 1K 0x400  ，最后一个扇区地址：0x0801FC00
256K (F103RBT6)  --->>> 单个扇区 2K  0x800  ，最后一个扇区地址：0x0803F800
512K (F103RET6),  --->>> 单个扇区 2K  0x800  ，最后一个扇区地址：0x0807F800
512K (F103ZET6),  --->>> 单个扇区 2K  0x800  ，最后一个扇区地址：0x0807F800

STM32F103VET 6  内部Flash分区   512K 
#define ADDR_FLASH_SECTOR_0     ((uint32_t)0x08000000) 	//扇区0起始地址, 16 Kbytes  
#define ADDR_FLASH_SECTOR_1     ((uint32_t)0x08004000) 	//扇区1起始地址, 16 Kbytes  
#define ADDR_FLASH_SECTOR_2     ((uint32_t)0x08008000) 	//扇区2起始地址, 16 Kbytes  
#define ADDR_FLASH_SECTOR_3     ((uint32_t)0x0800C000) 	//扇区3起始地址, 16 Kbytes  
#define ADDR_FLASH_SECTOR_4     ((uint32_t)0x08010000) 	//扇区4起始地址, 64 Kbytes  
#define ADDR_FLASH_SECTOR_5     ((uint32_t)0x08020000) 	//扇区5起始地址, 128 Kbytes  
#define ADDR_FLASH_SECTOR_6     ((uint32_t)0x08040000) 	//扇区6起始地址, 128 Kbytes  
#define ADDR_FLASH_SECTOR_7     ((uint32_t)0x08060000) 	//扇区7起始地址, 128 Kbytes  

STM32F103ZET6  内部Flash分区  512K   每页2K
-->> 调用擦除页函数  FLASH_ErasePage(uint32_t Page_Address);

*/