2019-12-27 18:35发布
stm32f103f4 发表于 2014-10-30 12:28 flash块大小一样吗
fengzhong 发表于 2014-10-30 15:19 把mass_mal.c这个文件里面的读写函数改一下试试: uint16_t MAL_Write(uint8_t lun, uint32_t Memory_Offse ...
最多设置5个标签!
移植到103VC上时,mass_mal.c中的
#define FLASH_START_ADDR 0x08003000 // Flash start address
#define FLASH_SIZE 0xD000
#define FLASH_PAGE_SIZE 0x400 // 1K per page
#define FLASH_WAIT_TIMEOUT 100000
这部分我没有做修改。我想103VC的FLASH是256K,103C8是64K,不做修改的话用的是前64K。不知道这样的想法对不对?
uint16_t MAL_Write(uint8_t lun, uint32_t Memory_Offset, uint32_t *Writebuff, uint16_t Transfer_Length)
{
uint16_t i,lenBackup=0;
static uint32_t Readbuff[FLASH_PAGE_SIZE>>2];//一个uint32_t占4个字节;数组比较大,用static修饰放在静态区避免堆栈溢出
uint32_t PageAddr,StartWriteOffset,EndWriteOffset;//页起始地址,页内需要修改的起始偏移数,页内需要修改的结束偏移数
StartWriteOffset=(FLASH_START_ADDR + Memory_Offset)%FLASH_PAGE_SIZE;
EndWriteOffset=FLASH_PAGE_SIZE;
PageAddr=FLASH_START_ADDR + Memory_Offset-StartWriteOffset;
if((lun!=0) && (lun!=1)) return MAL_FAIL;
while(PageAddr < FLASH_START_ADDR + Memory_Offset + Transfer_Length)
{
//printf(" PageAddr:0x%08x PageAddr - FLASH_START_ADDR:0x%04x",PageAddr,PageAddr - FLASH_START_ADDR);
MAL_Read(0,PageAddr - FLASH_START_ADDR,Readbuff,FLASH_PAGE_SIZE);//读一页
// for(i=0; i < (FLASH_PAGE_SIZE>>2); i++) //显示修改前的数据
// printf(" <-:0x%04x data:0x%08x",i<<2,Readbuff[i]);
if(PageAddr + FLASH_PAGE_SIZE > FLASH_START_ADDR+Memory_Offset+Transfer_Length) //计算是否最后一页
EndWriteOffset=(FLASH_START_ADDR + Memory_Offset + Transfer_Length)%FLASH_PAGE_SIZE;
//printf(" StartWriteOffset:0x%x EndWriteOffset:0x%x",StartWriteOffset,EndWriteOffset);
for(i=StartWriteOffset; i<EndWriteOffset; i+=4 )//修改需要修改的数据
Readbuff[i>>2]=Writebuff[lenBackup++];
// for(i=0; i < (FLASH_PAGE_SIZE>>2); i++) //显示修改后的数据
// printf(" ->:0x%04x data:0x%08x",i<<2,Readbuff[i]);
FLASH_ErasePage(PageAddr); //擦除
for(i=0; i<(FLASH_PAGE_SIZE>>2); i++)//写一页
{
if( FLASH_WaitForLastOperation(FLASH_WAIT_TIMEOUT) != FLASH_TIMEOUT )
{
FLASH_ClearFlag(FLASH_FLAG_EOP|FLASH_FLAG_PGERR|FLASH_FLAG_WRPRTERR);
}
if(Readbuff[i]!=0xffffffff)//0xffffffff说明原来就没有数据;这可以减少写flash的次数;
{
//printf(" i:%04x wAddr:0x%08x wdata:0x%08x",i,PageAddr+(i<<2),Readbuff[i]);
FLASH_ProgramWord(PageAddr+(i<<2), Readbuff[i]);
}
}
// MAL_Read(0,PageAddr - FLASH_START_ADDR,Readbuff,FLASH_PAGE_SIZE);//读一页
// for(i = 0; i <FLASH_PAGE_SIZE; i+=4)//读显示
// printf(" bias:0x%04x data:0x%08x",i,Readbuff[i>>2]);
PageAddr=PageAddr+FLASH_PAGE_SIZE;
StartWriteOffset=0;
}
return MAL_OK;
}
#define FLASH_SIZE 0xD000
#define FLASH_PAGE_SIZE 0x400 // 1K per page
这里需要修改吗?
一周热门 更多>