#include "stm32f4xx.h"
#include "string.h"

/* Base address of the Flash sectors */
#define ADDR_FLASH_SECTOR_0     ((uint32_t)0x08000000) /* Base @ of Sector 0, 16 Kbytes */
#define ADDR_FLASH_SECTOR_1     ((uint32_t)0x08004000) /* Base @ of Sector 1, 16 Kbytes */
#define ADDR_FLASH_SECTOR_2     ((uint32_t)0x08008000) /* Base @ of Sector 2, 16 Kbytes */
#define ADDR_FLASH_SECTOR_3     ((uint32_t)0x0800C000) /* Base @ of Sector 3, 16 Kbytes */
#define ADDR_FLASH_SECTOR_4     ((uint32_t)0x08010000) /* Base @ of Sector 4, 64 Kbytes */
#define ADDR_FLASH_SECTOR_5     ((uint32_t)0x08020000) /* Base @ of Sector 5, 128 Kbytes */
#define ADDR_FLASH_SECTOR_6     ((uint32_t)0x08040000) /* Base @ of Sector 6, 128 Kbytes */
#define ADDR_FLASH_SECTOR_7     ((uint32_t)0x08060000) /* Base @ of Sector 7, 128 Kbytes */
#define ADDR_FLASH_SECTOR_8     ((uint32_t)0x08080000) /* Base @ of Sector 8, 128 Kbytes */
#define ADDR_FLASH_SECTOR_9     ((uint32_t)0x080A0000) /* Base @ of Sector 9, 128 Kbytes */
#define ADDR_FLASH_SECTOR_10    ((uint32_t)0x080C0000) /* Base @ of Sector 10, 128 Kbytes */
#define ADDR_FLASH_SECTOR_11    ((uint32_t)0x080E0000) /* Base @ of Sector 11, 128 Kbytes */

#define FLASH_USER_START_ADDR   ADDR_FLASH_SECTOR_2   /* Start @ of user Flash area */
#define FLASH_USER_END_ADDR     ADDR_FLASH_SECTOR_3   /* End @ of user Flash area */
#define PROGRAM_CNT_ADD         FLASH_USER_START_ADDR
#define PROGRAM_CNT_SIZE        4


void smm_earase_flash(uint32_t StartSector, uint32_t EndSector);
uint32_t GetSector(uint32_t Address);

uint8_t Read_Data = 0;

int main(void){
	uint8_t Write_Data = 0x77;

	smm_earase_flash(GetSector(FLASH_USER_START_ADDR), GetSector(FLASH_USER_END_ADDR));
	
	FLASH_Unlock();
	FLASH_ProgramByte(PROGRAM_CNT_ADD+PROGRAM_CNT_SIZE, Write_Data);
	FLASH_Lock();
	Read_Data = *(uint8_t *)(PROGRAM_CNT_ADD+PROGRAM_CNT_SIZE);
	
	while (1){
	}
}
/******************************************************************************/
void smm_earase_flash(uint32_t StartSector, uint32_t EndSector){
	uint32_t i=0;
	int programCnt = 0;
	programCnt = *(uint32_t *)FLASH_USER_START_ADDR;

  FLASH_Unlock();

	
  FLASH_ClearFlag(FLASH_FLAG_EOP | FLASH_FLAG_OPERR | FLASH_FLAG_WRPERR | 
                  FLASH_FLAG_PGAERR | FLASH_FLAG_PGPERR|FLASH_FLAG_PGSERR); 

  for (i = StartSector; i < EndSector; i += 8){
    /* Device voltage range supposed to be [2.7V to 3.6V], the operation will
       be done by word */ 
    if (FLASH_EraseSector(i, VoltageRange_3) != FLASH_COMPLETE){
      /* Error occurred while sector erase. 
         User can add here some code to deal with this error  */
      while (1){}
    }
  }
	programCnt++;  // Increasing Number of Flash Rewriting
	FLASH_ProgramWord(PROGRAM_CNT_ADD, programCnt);
	FLASH_Lock();
}
/******************************************************************************/
uint32_t GetSector(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;  
  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/*(Address < FLASH_END_ADDR) && (Address >= ADDR_FLASH_SECTOR_11))*/
    sector = FLASH_Sector_11;  

  return sector;
}