#define FLASH_USER (FLASH_BASE+0x00100000U)
void StartDefaultTask(void const * argument)
{
/* USER CODE BEGIN 5 */
uint32_t i = 0;
uint32_t * pFlash_Data = (uint32_t *)FLASH_USER;
/* Infinite loop */
for(;;)
{
osDelay(5000);
HAL_FLASH_Unlock();
HAL_FLASH_Program(FLASH_PSIZE_WORD,FLASH_USER, (uint64_t)i++);
HAL_FLASH_Lock();
FLASH_WaitForLastOpera
tion(100);
printf("Hello world%d
",*pFlash_Data);
HAL_GPIO_WritePin(GPIOB, LD1_GREEN_Pin, i&0x00000001);
//HAL_UART_Transmit(&huart3, "a", 1, 0xFFFF);
}
/* USER CODE END 5 */
}
以上代码写入后读出测试失败,大侠给个例子吧。
#include "IAP_IN_MCU.h"
#include <string.h>
/* -------------------------------------------------------------*/
//const unsigned char Flash_Var[1024*16*2] __attribute__ ((at(FLASH_USER_START_ADDR)));
//******************************************************************************************
const unsigned int PHY_SECTOR_ADDR[]={
ADDR_FLASH_SECTOR_7,ADDR_FLASH_SECTOR_8,ADDR_FLASH_SECTOR_9,ADDR_FLASH_SECTOR_10,
};
const unsigned int ERASE_PHY_SECTOR_ADDR[]={
FLASH_Sector_7,FLASH_Sector_8,FLASH_Sector_9,FLASH_Sector_10,
};
//计算扇区的物理地址
unsigned int MCU_Get_LB_Sector(unsigned int LB_sector,unsigned int *blk,unsigned int *page,unsigned int *EraseBlk){
unsigned int sectorNo,offset;
offset =LB_sector / MAX_BLOCK_CNT;
sectorNo =LB_sector % MAX_BLOCK_CNT;
*blk= sectorNo;//PHY_SECTOR_ADDR[sectorNo];
*page= offset;
*EraseBlk=ERASE_PHY_SECTOR_ADDR[sectorNo];
if(offset >=MAX_BLOCK_SECTOR) return 1;
return 0;
}
/* Private function prototypes -----------------------------------------------*/
//删除扇区 SECTOR=8,9,10,11#
unsigned char MCU_Flash_Earse(unsigned int PHYsector){
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 */
if(FLASH_EraseSector(PHYsector, VoltageRange_3) != FLASH_COMPLETE) return 1;
return 0;
}
//*************************************************************************************************
//MCU 读取一个页 cnt=字节数
unsigned int MCU_Flash_Read(unsigned int page,unsigned int block,unsigned char *buf,unsigned int cnt){
unsigned int adr;
adr=block*MAX_BLOCK_SIZES+page*MAX_SECTOR_SZIES+START_FLASH_SECTOR;
memcpy(buf,(unsigned char *)adr,cnt);
return 0;
}
//*************************************************************************************************
//MCU 读取一个页 cnt=字节数
unsigned int MCU_Flash_Read_to_BufCmp(unsigned int page,unsigned int block,unsigned char *buf,unsigned int cnt){
volatile unsigned char *adr;
adr=(volatile unsigned char *)(block*MAX_BLOCK_SIZES+page*MAX_SECTOR_SZIES+START_FLASH_SECTOR);
while(cnt--){
if(*adr++ != *buf++ ) return 1;
}
return 0;
}
//*************************************************************************************************
//MCU 读取一个页 cnt=字节数
unsigned int MCU_Flash_Read_to_ByteCmp(unsigned int page,unsigned int block,unsigned char Byte,unsigned int cnt){
volatile unsigned char *adr;
adr=(volatile unsigned char *)(block*MAX_BLOCK_SIZES+page*MAX_SECTOR_SZIES+START_FLASH_SECTOR);
while(cnt--){
if(*adr++ != Byte ) return 1;
}
return 0;
}
//*************************************************************************************************
//写入字节 cnt=字节数
//*************************************************************************************************
unsigned int MCU_Flash_Program(unsigned int page,unsigned int block,unsigned char *buf,unsigned int cnt){
unsigned int adr;
FLASH_ClearFlag(FLASH_FLAG_EOP | FLASH_FLAG_OPERR | FLASH_FLAG_WRPERR |
FLASH_FLAG_PGAERR | FLASH_FLAG_PGPERR | FLASH_FLAG_PGSERR);
#ifdef FLASH_WORD_BYTE
unsigned int *p;
p=(unsigned int *)buf;
cnt >>=2;
adr=block*MAX_BLOCK_SIZES+page*MAX_SECTOR_SZIES+START_FLASH_SECTOR;
while(cnt--){
if(FLASH_ProgramWord (adr,*p++) != FLASH_COMPLETE) break;
adr +=4;
}
#else
adr=block*MAX_BLOCK_SIZES+page*MAX_SECTOR_SZIES+START_FLASH_SECTOR;
while(cnt--){
if(FLASH_ProgramByte (adr++,*buf++) != FLASH_COMPLETE) break;
}
#endif
return 0;
}
//*******************************************************************************************
//优化处理 cnt=扇区数!
unsigned int MCU_Optimized( unsigned int sector, unsigned char *buf ,unsigned int cnt){
unsigned int block,page,eraseBlk;
cnt *=MAX_SECTOR_SZIES;
if(MCU_Get_LB_Sector(sector,&block,&page,&eraseBlk)) return 1;
if(MCU_Flash_Read_to_BufCmp(page,block,buf,cnt)==0) return 0;
if(MCU_Flash_Read_to_ByteCmp(page,block,0xff,cnt)) return 1;
FLASH_Unlock();
//FLASH_PrefetchBufferCmd(DISABLE);
MCU_Flash_Program(page,block,buf,cnt);
//FLASH_PrefetchBufferCmd(ENABLE);
FLASH_Lock();
return 0;
}
//#define USE_SDRAM
//***************************************************************************************
//COPY 数据到任意位置 未先删除处理的扇区亦可,不跨块写!! cnt=要写入的页数!
//***************************************************************************************
unsigned int MCU_SWAP_Wirte(unsigned int sector, unsigned char *buf ,unsigned int cnt){
unsigned int block,page,eraseBlk,n;
unsigned char *p;
//***********************************************************
//处理交换块
//***********************************************************
if(MCU_Get_LB_Sector(sector,&block,&page,&eraseBlk)) return 1;
FLASH_Unlock();
//FLASH_PrefetchBufferCmd(DISABLE);
//-----------------------------------------------------------
#ifndef USE_SDRAM
MCU_Flash_Earse(SWAP_FLASH_SECTOR); //删除了交换快
MCU_Flash_Program(0,LB_SWAP_FLASH_ADDR,(unsigned char *)PHY_SECTOR_ADDR[block],MAX_BLOCK_SIZES);
p=(unsigned char *)SWAP_FLASH_ADDR;
#else
p=(unsigned char *)0xd0000000;
memcpy(p,(unsigned char *)PHY_SECTOR_ADDR[block], MAX_BLOCK_SIZES);
#endif
MCU_Flash_Earse(eraseBlk); //删除了原数据块
//***********************************************************
//如何写回一个块数据 从0扇区开始到要插入的页前!
//***********************************************************
//如何写回一个块数据
//***********************************************************
//如何写回一个块数据 从0扇区开始到要插入的页前!
n=page * MAX_SECTOR_SZIES;
MCU_Flash_Program(0,block,p,n );
//------------------- 写入新数据 --------------------------------------
MCU_Flash_Program(page,block,buf, cnt * MAX_SECTOR_SZIES);
n +=cnt * MAX_SECTOR_SZIES;
MCU_Flash_Program(page+cnt,block,p+n, MAX_BLOCK_SIZES-n);
//FLASH_PrefetchBufferCmd(ENABLE);
FLASH_Lock();
return 0;
}
//**************************************************************************************************
//写数据 4K一个扇区读取! sector=0,...n 逻辑地址!
//***********************************************************************************************
unsigned char MCU_Flash_Sector_write(unsigned int sector,unsigned char *buf,unsigned int cnt){
while(cnt--){
//********************************************************************
//优化处理
if(MCU_Optimized(sector,buf,1)) MCU_SWAP_Wirte(sector,buf,1);
buf +=MAX_SECTOR_SZIES;
sector++;
}
return 0;
}
//***********************************************************************************************
//读取数据 一个扇区读取! sector=0,...n 逻辑地址!
//***********************************************************************************************
unsigned char MCU_Flash_Sector_Read(unsigned int sector,unsigned char *buf,unsigned int cnt){
unsigned int block,page,eraseBlk;
while(cnt--){
if(MCU_Get_LB_Sector(sector++,&block,&page,&eraseBlk)) return 1;
MCU_Flash_Read(page,block,buf,MAX_SECTOR_SZIES); //最多读取一个页的数据!
buf +=MAX_SECTOR_SZIES;
}
return 0;
}
一周热门 更多>