N32L43x Flash读写擦除操作总结

2022-09-07 16:45:16 浏览数 (1)

1、flash规格

本文使用N32L43XRL_STB官方开发板展开说明:

本文使用的主芯片为N32L436RBL7,flash大小为128k;

flash分为64个page,编号0-63,每个page为2k。

2、flash操作注意事项

(1)写、擦除操作必须打开HSI时钟;

(2)写操作仅支持32位,擦除至少以1个page位单位进行;

(3)等待周期,本文配置的HCLK为108MHZ,所以设置等待周期为3。

3、代码实现

代码语言:javascript复制
//PAGE 0-63 0x0800 0000 - 0x0801 FFFF
#define FLASH_PAGE_SIZE 0x800 //2k
void FlashWrite(uint16_t len,uint8_t *data,uint32_t addr_start)
{
uint16_t i=0;
uint32_t temp=0;
uint32_t addr=addr_start;
FLASH_STS status = FLASH_COMPL;
while(FLASH_HSICLOCK_ENABLE != FLASH_ClockInit());
FLASH_Unlock();
for(i=0;i<len/4;i  )
{
temp = (data[0]<<0) (data[1]<<8) (data[2]<<16) (data[3]<<24);
status=FLASH_ProgramWord(addr, temp);
if(status!=FLASH_COMPL)
{
return;
}
data  = 4;
addr  = 4;
}
if((len % 4)==3)
{
temp = (data[0]<<0) (data[1]<<8) (data[2]<<16);
temp = temp | 0xff000000;
status=FLASH_ProgramWord(addr,temp);
if(status!=FLASH_COMPL)
{
return;
}
}
else
{
if((len % 4)==2)
{
temp = (data[0]<<0) (data[1]<<8);
temp = temp | 0xffff0000;
status=FLASH_ProgramWord(addr,temp);
}
else
{
if((len % 4)==1)
{
temp = (data[0]<<0);
temp = temp | 0xffffff00 ;
status=FLASH_ProgramWord(addr,temp);
if(status!=FLASH_COMPL)
{
return;
}
}
}
}
FLASH_Lock();
}
void FlashRead(uint16_t len,uint8_t *outdata,uint32_t addr_start)
{
uint32_t addr;
uint16_t i;
addr = addr_start;
for(i=0;i<len;i  )
{
*outdata = *(uint8_t*) addr;
addr = addr   1;
outdata  ;
}
}
void FlashErase(uint32_t start, uint32_t end)
{
uint8_t index;
FLASH_STS status = FLASH_COMPL;
while(FLASH_HSICLOCK_ENABLE != FLASH_ClockInit()) {};
FLASH_Unlock();
/* erase the flash pages */
while(start < end)
{
index = start/FLASH_PAGE_SIZE;
status=FLASH_EraseOnePage(index*FLASH_PAGE_SIZE);
if(status!=FLASH_COMPL)
{
return;
}
start  = FLASH_PAGE_SIZE;
}
FLASH_Lock();
}

4、测试

flash测试空间为最后一个page

代码语言:javascript复制
#define APP_DATA_SATRT_ADDR 0x0801F800 //2k
#define TEST_DATA_LEN 11
uint8_t WriteData[TEST_DATA_LEN]={1,2,3,4,5,6,7,8,9,10,11};
uint8_t ReadData[TEST_DATA_LEN]={0,0,0,0,0,0,0,0,0, 0,0};
void FlashTest(void)
{
FLASH_SetLatency(FLASH_LATENCY_3);
FlashErase(APP_DATA_SATRT_ADDR, APP_DATA_SATRT_ADDR FLASH_PAGE_SIZE);
FlashWrite(TEST_DATA_LEN,WriteData,APP_DATA_SATRT_ADDR);
FlashRead(TEST_DATA_LEN,ReadData, APP_DATA_SATRT_ADDR);
printf("Write Data[");
for(uint8_t i=0;i<TEST_DATA_LEN;i  )
{
printf(" %d",WriteData);
}
printf("]rn");
printf("Read Data[");
for(uint8_t i=0;i<TEST_DATA_LEN;i  )
{
printf(" %d",ReadData);
}
printf("]rn");
}

5、现象

可以看出,写进去的和读出来的一致。

0 人点赞