/**
  ******************************************************************************
  * @file    flash_if.c 
  * @author  MCD Application Team
  * @version V1.0.0
  * @date    31-October-2011
  * @brief   This file provides high level routines to manage internal Flash 
  *          programming (erase and write). 
  ******************************************************************************
  * @attention
  *
  * THE PRESENT FIRMWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS
  * WITH CODING INFORMATION REGARDING THEIR PRODUCTS IN ORDER FOR THEM TO SAVE
  * TIME. AS A RESULT, STMICROELECTRONICS SHALL NOT BE HELD LIABLE FOR ANY
  * DIRECT, INDIRECT OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING
  * FROM THE CONTENT OF SUCH FIRMWARE AND/OR THE USE MADE BY CUSTOMERS OF THE
  * CODING INFORMATION CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS.
  *
  * <h2><center>&copy; COPYRIGHT 2011 STMicroelectronics</center></h2>
  ******************************************************************************
  */

/* Includes ------------------------------------------------------------------*/
#include "flash_if.h"
#include "httpserver.h"
#include "common_config.h"
#include "wdg.h"

#include "tinystdio.h"

/**
  * @brief  Чтение данных из flash
  * @param  address
  * @retval data
  */
uint32_t flash_read(uint32_t address)
{
  return (*(__IO uint32_t*) address);
}

/**
  * @brief  Unlocks Flash for write access
  * @param  None
  * @retval None
  */
void FLASH_If_Init(void)
{ 
  FLASH_Unlock(); 
}

/**
  * @brief  This function does an erase of all user flash area
  * @param  StartSector: start of user flash area
  * @retval 0: user flash area successfully erased
  *         1: error occurred
  */
int8_t FLASH_If_Erase(uint32_t StartSector)
{
  uint32_t FlashAddress;
  
  FlashAddress = StartSector;

  /* Device voltage range supposed to be [2.7V to 3.6V], the operation will
     be done by word */ 
 
  if (FlashAddress <= (uint32_t) USER_FLASH_LAST_PAGE_ADDRESS)
  {
	FLASH_Unlock();
	GPIOE->ODR ^= WDT_PIN;
    FLASH_EraseSector(FLASH_Sector_5, VoltageRange_3); /* 128 Kbyte */
	GPIOE->ODR ^= WDT_PIN;
    FLASH_EraseSector(FLASH_Sector_6, VoltageRange_3); /* 128 Kbyte */
	GPIOE->ODR ^= WDT_PIN;
	FLASH_EraseSector(FLASH_Sector_7, VoltageRange_3); /* 128 Kbyte */
	GPIOE->ODR ^= WDT_PIN;
	/* Check if we use more than 512K flash */
	if (USER_FLASH_END_ADDRESS > 0x08000000 + 0x7FFFF) {
        FLASH_EraseSector(FLASH_Sector_8, VoltageRange_3); /* 128 Kbyte */
        GPIOE->ODR ^= WDT_PIN;
        FLASH_EraseSector(FLASH_Sector_9, VoltageRange_3); /* 128 Kbyte */
        GPIOE->ODR ^= WDT_PIN;
	}
	FLASH_Lock();
  }
  else
  {
    return (1);
  }

  return (0);
}
/**
  * @brief  This function writes a data buffer in flash (data are 32-bit aligned).
  * @note   After writing data buffer, the flash content is checked.
  * @param  FlashAddress: start address for writing data buffer
  * @param  Data: pointer on data buffer
  * @param  DataLength: length of data buffer (unit is 32-bit word)   
  * @retval 0: Data successfully written to Flash memory
  *         1: Error occurred while writing data in Flash memory
  *         2: Written Data in flash memory is different from expected one
  */
uint32_t FLASH_If_Write(__IO uint32_t* FlashAddress, uint32_t* Data ,uint16_t DataLength)
{
  uint32_t i = 0;
  uint32_t delta;
  /*
  delta = 0x0805FFFF - *FlashAddress; // ReturnFlashWriteAddress();
  printf("%i", delta);
  
  if ((delta < 1460) && (DataLength != 1))
	DataLength = delta / 4;
  */
  for (i = 0; (i < DataLength) && (*FlashAddress <= (USER_FLASH_END_ADDRESS)); i++)
  {
    /* Device voltage range supposed to be [2.7V to 3.6V], the operation will
       be done by word */ 
    if (FLASH_ProgramWord(*FlashAddress, *(uint32_t*)(Data+i)) == FLASH_COMPLETE)
    {
     /* Check the written value */
      if (*(uint32_t*)*FlashAddress != *(uint32_t*)(Data+i))
      {
        /* Flash content doesn't match SRAM content */
        return(2);
      }
      /* Increment FLASH destination address */
      *FlashAddress += 4;
    }
    else
    {
      /* Error occurred while writing data in Flash memory */
      return (1);
    }
  }

  return (0);
}

/******************* (C) COPYRIGHT 2011 STMicroelectronics *****END OF FILE****/