|
发表于 2017-3-28 12:34:24
|
显示全部楼层
本帖最后由 mon51 于 2017-3-28 12:35 编辑
/**
******************************************************************************
* @file FLASH/Program/main.c
* @author MCD Application Team
* @version V1.0.0
* @date 30-September-2011
* @brief Main program body
******************************************************************************
* @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>© COPYRIGHT 2011 STMicroelectronics</center></h2>
******************************************************************************
*/
/* Includes ------------------------------------------------------------------*/
#include "stm32f2xx.h"
#include <string.h>
/* Private define ------------------------------------------------------------*/
/* 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_BAK_START_ADDR ADDR_FLASH_SECTOR_3 /* Start @ of user Flash area */
#define FLASH_USER_SECTOR_ADDR FLASH_Sector_2 /* End @ of user Flash area */
#define FLASH_BAK_SECTOR_ADDR FLASH_Sector_3 /* End @ of user Flash area */
/* -------------------------------------------------------------*/
const unsigned char Flash_Var[1024*16*2] __attribute__ ((at(FLASH_USER_START_ADDR)));
/* Private function prototypes -----------------------------------------------*/
void wr_FLASH_IAP_MCU(unsigned int sector,unsigned char *p,unsigned int cnt){
unsigned int adr[2];
unsigned int s[2];
if(sector >1 ) return;
s[0]=FLASH_Sector_2;
s[1]=FLASH_Sector_3;
adr[0]=FLASH_USER_START_ADDR;
adr[1]=FLASH_BAK_START_ADDR;
if(0==memcmp(p,(unsigned char *)adr[sector],cnt)) return;
FLASH_Unlock();
FLASH_ClearFlag(FLASH_FLAG_EOP | FLASH_FLAG_OPERR | FLASH_FLAG_WRPERR |
FLASH_FLAG_PGAERR | FLASH_FLAG_PGPERR | FLASH_FLAG_PGSERR);
/* Device voltage range supposed to be [2.7V to 3.6V], the operation will be done by word */
FLASH_PrefetchBufferCmd(DISABLE);
if(FLASH_EraseSector(s[sector], VoltageRange_3) != FLASH_COMPLETE) return;
while(cnt--){
if(FLASH_ProgramByte(adr[sector]++, *p++) != FLASH_COMPLETE) break;
}
FLASH_PrefetchBufferCmd(ENABLE);
FLASH_Lock();
}
//***********************************************************************************************
//读取数据
//***********************************************************************************************
void rd_FLASH_IAP_MCU(unsigned int sector,unsigned char *p,unsigned int cnt){
if(sector==0) memcpy(p,(unsigned char *)FLASH_USER_START_ADDR,cnt);
if(sector==1) memcpy(p,(unsigned char *)FLASH_BAK_START_ADDR,cnt);
}
//**************************************************************************************************
F205,F405是16K的大扇区,有一点浪费,但空闲更浪费。
|
|