/*--------------------------------------------------------------------------------------
*  @file     FlashSetup.c
*  @author   ZhangJing
*  @version  base on stm32f0x   
*  @date     2015.09.11
*  @brief    芯片内部flash驱动
---------------------------------------------------------------------------------------*/

#include "stm32f10x_gpio.h"
#include "stm32f10x_flash.h"
#include "TypeDefine.h"
#include "FlashSetup.h"

/*********************************************************************************
*  Function:	FlashWriteOperate						                                                           
*  Object:		flash写操作
*  输入:			uint8_t *writeDataBuf,uint16_t writeLen,uint32_t writeAddress
*  输出:			无	                                     
*  备注:			内部flash一次写两个字节，先根据相应地址进行擦除，再写入              
**********************************************************************************/
void FlashWriteOperate( uint8_t *writeDataBuf,uint16_t writeLen,uint32_t writeAddress )
{
	uint16_t 	i;
	uint16_t	tempWriteData;
	FLASH_Unlock();
	FLASH_ClearFlag( FLASH_FLAG_EOP | FLASH_FLAG_PGERR | FLASH_FLAG_WRPRTERR );
	//if(( writeAddress == HISTORY_RECORDQTY_ADDR) || (writeAddress == PARAMSET_DATA_START_ADDR ) || (writeAddress == HISTORY_DATA_START_ADDR ))
	{
		FLASH_ErasePage(writeAddress);
	}
	for( i = 0;i < writeLen / 2;i++ )
	{
		tempWriteData = writeDataBuf[i*2];
		tempWriteData <<= 8;
		tempWriteData += writeDataBuf[i*2+1];
		FLASH_ProgramHalfWord((writeAddress + i*2),tempWriteData);
	}
	FLASH_Lock();
}

/*********************************************************************************
*  Function:	FlashReadOperate						                                                           
*  Object:		flash读操作
*  输入:			uint8_t *readDataBuf,uint16_t readLen,uint32_t readAddress
*  输出:			无	                                     
*  备注:			内部flash可以按单个字节读取              
**********************************************************************************/
void FlashReadOperate( uint8_t *readDataBuf,uint16_t readLen,uint32_t readAddress)
{
	uint16_t i;
	uint16_t	tempData = 0;//临时变量
	for( i = 0;i < readLen / 2;i++ )
	{
		tempData = *(uint16_t *)( readAddress + i * 2 );
		readDataBuf[i * 2] = tempData / 256;
		readDataBuf[i * 2 + 1] = tempData % 256;
	}
}

