一、环境与目的
AT32F403AVGT7,FLASH从0x80e0000到最后,共128K。扇区大小为512。
注意:Flash 的扇区大小为2KB。
fatfs 80286 /* Revision ID */
目标在于利用单片机1MBflash后面的一小部分,以方便应用程序存储系统参数。
1)diskio.c里ioctl下:
case DEV_SYS :
switch(cmd){
case CTRL_SYNC:
result = RES_OK;
break;
case GET_SECTOR_SIZE:
*(DWORD*)buff = 512;
result = RES_OK;
break;
case GET_SECTOR_COUNT:
//*(DWORD*)buff = ABLINKFLASH_SIZE/2048;
*(DWORD*)buff = 256;
result = RES_OK;
break;
case GET_BLOCK_SIZE:
*(DWORD*)buff = 1;
result = RES_OK;
break;
default:
result = RES_PARERR;
break;
}
2) 读写函数:
void flash_write_byte_nocheck(uint32_t write_addr, uint8_t *p_buffer, uint32_t num_write){
uint32_t i;
uint32_t writeNum = num_write*512;
uint32_t headAddr, tailAddr;
write_addr = write_addr*512+ABLINKFLASH_START_ADDR;
while(writeNum){
headAddr = write_addr - write_addr%2048;//(write_addr>>11)<<11;
tailAddr = headAddr+2048;
n_ReadSector(headAddr);
for(i=write_addr; i<tailAddr; i++){
flushSectorBuff[i-headAddr] = p_buffer[i-write_addr];
writeNum--;
if(0==writeNum){
break;
}
}
n_WriteSector(headAddr);
}
}
void flash_read_byte(uint32_t read_addr, uint8_t *p_buffer, uint32_t num_read){
uint32_t i;
uint32_t readCount = num_read*512;
uint32_t sectora = read_addr<<9;
uint8_t* pu8T_Temp;
for(i = 0; i < readCount; i++){
pu8T_Temp = (uint8_t*)(sectora+ABLINKFLASH_START_ADDR+i);
p_buffer[i] = *pu8T_Temp;
}
}
二、解析如下:
三、验证代码如下:
res = f_mkfs("0:", 0, buffer, 1024);
if(FR_OK==res){
aBlink.uart_usb->send("f_mkfs ok...\r\n", 14);
}else{
aBlink.uart_usb->send("f_mkfs error...\r\n", 17);
}
res = f_mount(&fatfs, "0:", 1);
if(FR_OK==res){
aBlink.uart_usb->send("fatfs mount success.\r\n", 22);
res = f_open(&fil, "0:123456.txt", FA_OPEN_ALWAYS);
if(FR_OK==res){
aBlink.uart_usb->send("fatfs open success...\r\n", 23);
res = f_close(&fil);
if(FR_OK==res){
aBlink.uart_usb->send("fatfs close success...\r\n", 24);
}else{
aBlink.uart_usb->send("fatfs close error...\r\n", 24);
}
}else{
aBlink.uart_usb->send("fatfs open error...\r\n", 23);
}
}else{
aBlink.uart_usb->send("fatfs mount error.\r\n", 22);
}