uC/FS调试笔记

本文详细记录了在调试uC/FSFAT16文件系统过程中遇到的问题,包括接口函数定义、初始化、读写操作、坏块处理等方面,并提供了针对性的解决方法。重点强调了heap区大小对系统稳定性的关键作用,以及如何正确地进行NAND闪存的读写操作,避免数据丢失和系统崩溃。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

最近调试了uC/FS FAT16的文件系统,发现uC/FS在网上的基本上只有1.34版本的可以用,

调试时发现几个问题,

(1)定义接口函数

const FS__device_type FS__flashdevice_driver = {
  "FLASH device",
  _FS_FLASH_DevStatus,
  _FS_FLASH_DevRead,
  _FS_FLASH_DevWrite,
  _FS_FLASH_DevIoCtl
};

其实也就read , write两个有用,其它return ok就好了

(2)flash_x_hw.c

int FS__FLASH_Init(FS_u32 Unit){
    return Nand_init();
}


int FS__FLASH_ReadSector(FS_u32 Unit,unsigned long Sector,unsigned char *pBuffer){
 return  Nand_io(Sector,(unsigned int *)pBuffer,1);
}

 

int FS__FLASH_WriteSector(FS_u32 Unit,unsigned long Sector,unsigned char *pBuffer){    
 return  Nand_io(Sector,(unsigned int *)pBuffer,0);
}

(3)最重要的是,heap区应足够大,要大于16K,因为分区交换时要用

int NandReadBlock(unsigned int buffer,unsigned int block)
{
 int i;
 unsigned int buffer_ptr,status,page;
 
 page = block*32;
 
 buffer_ptr = buffer;
 
 for(i=0; i<32; i++)
 {
  status = NFReadPage(page, (unsigned char *)buffer_ptr);
  if(status != 0)  return -1;  
  page ++;
  buffer_ptr += 512;
 }

 return 0;
 
}


int NandWriteBlock(unsigned int* buffer,unsigned int block)
{
 int i;
 unsigned int buffer_ptr,status,page;
 
 page = block*32;
  
 buffer_ptr = (unsigned int)buffer;
 
 for(i=0; i<32; i++)
 {
  status = NFWritePage(page, (unsigned char *)buffer_ptr);  
  if(status!= 0) return -1;
  page ++;
  buffer_ptr += 512;
 }

 return 0;
 
}

int NandBlockToTemp(unsigned int *buffer,unsigned int block)
{
 int i;
 unsigned int status,page,buffer_ptr;
 
 page = block*32;
 
 buffer_ptr = (unsigned int)buffer;
 
 for(i=0;i<32;i++)
 {
  status = NFReadPage(page, (unsigned char *)buffer_ptr);
  if(status!= 0) return  -1;
  page++;
  buffer_ptr += 512;
 }
  
 return 0;
 

int Nand_io(unsigned int page, unsigned int *buffer, int reading)
{
 unsigned int i,pageoffset,blocknum;
 unsigned int *swap_block,*swap_ptr;
 
 if(reading == 1)
  NFReadPage(page, (unsigned char *)buffer); 
 else if(reading == 0)
 {
  swap_block = (unsigned int *)malloc(512*32);
  blocknum = page/32;
  pageoffset = page %32;
  swap_ptr = swap_block + 0x200*pageoffset/4;
  
  NandBlockToTemp(swap_block,blocknum);       //保存该页所在块到交换块
  memcpy(swap_ptr,buffer,512);    //写页数据到交换块相应的位置
  NFEraseBlock(page);
  for(i = 0;i < 3000; i++);
  NandWriteBlock(swap_block,blocknum);
  free(swap_block);
 
 }
 else return 1;
 
 return 0;
}

(4)格式化一次就可以了,

 

FS_IoCtl("flash:",FS_CMD_FORMAT_MEDIA,FS_MEDIA_SMC_16MB,0);

 

因为我的NAND只有16M,而FS_MEDIA_NAND_xxMB只有64M的,所以就借用了一个SMC的,

(5)主要的是从网上下载下来的代码,很经典的,因为uC/FS(FAT)对于坏块的管理并没有做,所以出了坏块就要加入坏块处理代码,这部分现在还没有做好,可恶的是现在居然找不到一个带坏块的NAND,只能等等待了


µC/FS µC/FS is a compact, reliable, high-performance file system. It offers full-featured file and directory access with flexible device and volume management including support for partitions. Source Code: µC/FS is provided in ANSI-C source to licensees. The source code is written to an exacting coding standard that emphasizes cleanness and readability. Moreover, extensive comments pepper the code to elucidate its logic and describe global variables and functions. Where appropriate, the code directly references standards and supporting documents. Device Drivers: Device drivers are available for most common media including SD/MMC cards, NAND flash, NOR flash. Each of these is written with a clear, layered structure so that it can easily be ported to your hardware. The device driver structure is simple—basically just initialization, read and write functions—so that µC/FS can easily be ported to a new medium. Devices and Volumes: Multiple media can be accessed simultaneously, including multiple instances of the same type of medium (since all drivers are re-entrant). DOS partitions are supported, so more than one volume can be located on a device. In addition, the logical device driver allows a single volume to span several (typically identical) devices, such as a bank of flash chips. FAT: All standard FAT variants and features are supported including FAT12/FAT16/FAT32 and long file names, which encompasses Unicode file names. Files can be up to 4-GB and volumes up to 8-TB (the standard maximum). An optional journaling module provides total power fail-safety to the FAT system driver. Application Programming Interface (API): µC/FS provides two APIs for file and directory access. A proprietary API with parallel argument placement and meaningful return error codes is provided, with functions like FSFile_Wr(), FSFile_Rd() and FSFile_PosSet(). Alternatively, a standard POSIX-like API is provided, including functions like fs_fwrite(), fs_fread() and fs_fsetpos
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值