金牌会员
 
- 积分
- 1898
- 金钱
- 1898
- 注册时间
- 2012-10-28
- 在线时间
- 357 小时
|

楼主 |
发表于 2017-1-4 23:50:37
|
显示全部楼层
/*
********************************************************************************************************
函数名称:NAND_FlashFatfsDemo
函数功能: 测试 nand flash 读写功能,带 fatfs 文件管理系统操作
参数: 无
返回值: 无
********************************************************************************************************
*/
void NAND_FlashFatfsDemo(void)
{
// static u8 nand_tx_buff[8192*4], nand_rx_buff[8192*4];
uint32_t RWDataSize = 8192 * 4;
uint8_t * nand_tx_buff = (uint8_t *)mymalloc(SRAMEX, RWDataSize );
uint8_t * nand_rx_buff = (uint8_t *)mymalloc(SRAMEX, RWDataSize );
uint8_t res;
FATFS Nand_fs;//逻辑磁盘工作区.
FIL file; //文件1
UINT bw; //读写变量
uint32_t cc;
uint8_t i = 0;
u8 work[_MAX_SS]; /* Work area (larger is better for processing time) */
// FILINFO fileinfo; //文件信息
// DIR DirInf; //目录
// FIL ftemp; //文件2.
if(( !nand_tx_buff) || ( !nand_rx_buff ))
{
#if NAND_FLASH_DEBUG_SWITCH > 0U
printf("NAND SDRAM mymalloc Error ... ... \r\n");
#endif
}
for(cc=0;cc < ( RWDataSize );cc++)
{
nand_tx_buff[cc] = i * 1;
nand_rx_buff[cc] = 0 ;
i ++;
}
res = f_mount( &Nand_fs,"2:",1); //挂载FLASH.
if(FR_OK != res)
{
printf("f_mount NAND 失败 ... ... 0x%x\r\n", res);
if(0x0D == res)
{
if(FTL_Format()) // 使用 fatfs 格式化函数前,先 擦除nand flash 所以的好块
{
printf("NAND Flash Format Error ... ... \r\n");
myfree(SRAMEX, nand_tx_buff);
myfree(SRAMEX, nand_rx_buff);
return ;
}
else
{
res = f_mkfs("2:", 1, 4096, work, sizeof(work) ); //格式化FLASH,2,盘符;1,不需要引导区,8个扇区为1个簇
if(res == 0)
{
res = f_setlabel((const TCHAR *)"2:NANDDISK"); //设置Flash磁盘的名字为:NANDDISK
if(FR_OK == res)
{
printf("NAND Flash 磁盘设置成功 ... ... \r\n");
}
printf("NAND Flash Format Finish ... ... \r\n"); //格式化完成
}
else
printf("NAND Flash Format Error ... ... %d \r\n", res); //格式化失败
}
}
}
else
printf("f_mount NAND 成功 ... ... \r\n");
// res = f_opendir(&DirInf, "2:"); /* 2: 表示盘符 ,在读写文件前必须先打开目录 */
res = f_open(&file, "2:/NAND_1.txt", FA_CREATE_ALWAYS | FA_WRITE );
res = f_write(&file, &nand_tx_buff[0], RWDataSize, &bw);
res = f_close(&file);
// res = f_opendir(&DirInf, "2:"); /* 2: 表示盘符 文件关闭后需要重新打开 根目录,否则读取失败 */
res = f_open(&file, "2:/NAND_1.txt", FA_READ);
res = f_read (&file, &nand_rx_buff[0], RWDataSize, &bw );
res = f_mount( NULL, "2:", 1);
if( Buffercmp_8(&nand_tx_buff[0], &nand_rx_buff[0], RWDataSize ) == 0)
printf("NAND Flash Fatfs Read Write 8192 * 4 Test OK .. DD \r\n\r\n");
else
printf("NAND Flash Fatfs Read Write 8192 * 4 Test Error .. EE \r\n\r\n");
myfree(SRAMEX, nand_tx_buff);
myfree(SRAMEX, nand_rx_buff);
} |
|