我在SD卡上构建dosfs文件系统是总是出错:
代码如下
#include "copyright_wrs.h"
#include "vxWorks.h"
#include "taskLib.h"
#include "blkIo.h"
#include "stdlib.h"
#include "stdio.h"
#include "string.h"
#include "dosFsLib.h"
#include "usrFdiskPartLib.h"
#include "private/dosFsLibP.h" /* for byte swapping macros */
#include "dpartCbio.h"
#include "cbioLib.h"
#include "semLib.h"
#include "fat.h"
#define FD_DEBUG
/*******************************************/
static SEM_ID McSem;BcSem;
static int sdDrvInstalled = 0;
/*******************************************/
LOCAL STATUS sdBlkRd (SD_DEV *pFdDev, int startBlk, int nBlks, char *pBuf);
LOCAL STATUS sdBlkWrt (SD_DEV *pFdDev, int startBlk, int nBlks, char *pBuf);
LOCAL STATUS sdIoctl (SD_DEV *pFdDev, int function, int arg);
LOCAL void sdInit(void);
LOCAL STATUS sdFormat(SD_DEV *pFdDev,int ty);
/******************************************************************/
/******************************************************************/
int n = 0;
/******************* 487424 ********************/
int test(int m)
{
FILE *fp;
char *mm="fsasdfafuiyfhrlfjvifgyruigjfklghidfhgidfjghihydirgj";
if(n==0)
{
sdDrv();
printf("\nCreate FILE system 0 ``````````````````````````````\n");
usrSdConfig(0, m*2048, 0, "/f");//创建文件系统
dosFsShow ("/f", 1);
n=1;
fp = fopen("/f/b.txt","wb+");//创建文件
if(fp==NULL)
printf("can not Create file :b.txt\n");
fread(mm,25,1,fp);//写入数据
fclose(fp);
}
else
{
printf("\nCreate FILE system 1 ``````````````````````````````\n");
usrSdConfig(1, m*2048, 102400, "/ff");//dosFsInit (20);487424
dosFsShow ("/ff", 1);
}
}
/******************************************************************/
/******************************************************************/
STATUS usrSdConfig(int drive,int nBlocks, int blkOffset, char *fileName)
{
BLK_DEV * pBlkDev;
DOS_VOL_DESC * pVolDesc = NULL;
char devName [16];
if ((UINT)drive >= SD_MAX_DRIVES)
{
printErr ("drive is out of range (0-%d).\n", SD_MAX_DRIVES - 1);
return (ERROR);
}
if ((pBlkDev = sdDevCreate (drive, nBlocks, blkOffset,fileName)) == NULL)
{
printErr ("sdDevCreate failed.\n");
return (ERROR);
}
devSplit( fileName, devName);// Make DOS file system
pVolDesc = dosFsDevInit(devName, pBlkDev, NULL);
if (pVolDesc == NULL)
{
printErr ("Error during dosFsDevInit in usrSdConfig: %x\n", errno);
return (ERROR);
}
if (dosFsVolFormat (devName, DOS_OPT_FAT16, NULL) != OK)//以FAT16格式创建文件系统
return ERROR;
//if (dosFsVolFormat (devName, DOS_OPT_VXLONGNAMES, NULL) != OK)
// return ERROR;
//dosFsMkfs(devName,pBlkDev);
return OK;
}
/**************************************************************************/
/**************************************************************************/
STATUS sdDrv(void)
{
if (!sdDrvInstalled)
{
BcSem = semBCreate(SEM_Q_PRIORITY, SEM_EMPTY);
McSem = semMCreate(SEM_Q_PRIORITY);//创建互斥信号量
if( (BcSem==NULL)||(McSem==NULL) )
return ERROR;
sdInit ();
sdDrvInstalled = TRUE;
}
return (OK);
}
/****************************************************************************/
/****************************************************************************/
BLK_DEV *sdDevCreate(int drive,int nBlocks,int blkOffset,char *driName)
{
SD_DEV *pSdDev;
BLK_DEV *pBlkDev;
unsigned int Blocks;
if (!sdDrvInstalled)
return (NULL);
if (nBlocks == 0)
Blocks = 2048*4;
else Blocks = nBlocks;
if ((UINT)drive >= SD_MAX_DRIVES)
return (NULL);
if ((pSdDev = (SD_DEV *)calloc(sizeof (SD_DEV), 1)) == NULL)
return (NULL);
pBlkDev = &pSdDev->blkDev;
pBlkDev->bd_nBlocks = Blocks; //设备的块数
pBlkDev->bd_bytesPerBlk = SD_BLOCK_SIZE; //每块的字节数512
pBlkDev->bd_blksPerTrack = 487424; //0X3F; //每磁道(扇区)的块数//Blocks 4096-16 2048-32 63;
pBlkDev->bd_nHeads = 1;//0XFF;//1; //磁头数 255
pBlkDev->bd_removable = TRUE; //FALSE;//是否可移动标志
pBlkDev->bd_retry = 2; //出错重试次数
pBlkDev->bd_mode = O_RDWR; //O_WRONLY|O_RDONLY|O_RDWR;设备初始模式
pBlkDev->bd_readyChanged = TRUE;
pBlkDev->bd_blkRd = sdBlkRd;
pBlkDev->bd_blkWrt = sdBlkWrt;
pBlkDev->bd_ioctl = sdIoctl;
pBlkDev->bd_reset = NULL; //sdReset;
pBlkDev->bd_statusChk = NULL; //sdStatusChk;
pSdDev->drive = drive;
pSdDev->driName = driName;
pSdDev->blkOffset = blkOffset;
pSdDev->driveSize = Blocks * SD_BLOCK_SIZE;
return (&pSdDev->blkDev);
}
/***************************************************************************/
/****************************************************************************/
LOCAL STATUS sdBlkRd(SD_DEV *pSdDev,int startBlk,int nBlks,char *pBuf)
{
int status = ERROR;
#ifdef FD_DEBUG
printf("read: startBlk = %d nBlks = %d\n ",startBlk+pSdDev->blkOffset, nBlks);
#endif
semTake(McSem,WAIT_FOREVER);//获得信号量
status = SDReadSector(startBlk+pSdDev->blkOffset,pBuf,nBlks);//SD 卡连续读 n 快的数据
semGive(McSem);
return (OK);
}
/*******************************************************************************
/****************************************************************************/
LOCAL STATUS sdBlkWrt(SD_DEV *pSdDev,int startBlk,int nBlks,char *pBuf)
{
int status = ERROR;
#ifdef FD_DEBUG
printf("write: startBlk = %d nBlks = %d\n ",startBlk+pSdDev->blkOffset, nBlks);
#endif
semTake(McSem,WAIT_FOREVER);
status = SDWriteSector(startBlk+pSdDev->blkOffset,pBuf,nBlks);//SD 卡连续写 n 快的数据
semGive(McSem);
return (OK);
}
/*******************************************************************************
/****************************************************************************/
LOCAL STATUS sdIoctl(SD_DEV *pSdDev,int function,int arg)
{
FAST int status = ERROR;
switch (function)
{
case FIODISKFORMAT:
#ifdef FD_DEBUG
printf("dosFsVolFormat DOS_OPT_FAT16\n ");
#endif
dosFsDevInit(pSdDev->driName, &pSdDev->blkDev, NULL);
if(sdFormat(pSdDev, DOS_OPT_FAT16) != OK)
(void) errnoSet (S_ioLib_DEVICE_ERROR);
status = OK;
break;
case FIODISKINIT:
#ifdef FD_DEBUG
printf("dosFsDevInit DOS_OPT_FAT16\n ");
#endif
if( dosFsDevInit(pSdDev->driName, &pSdDev->blkDev, NULL) == NULL )
{
printf("Error during dosFsDevInit\n");
return (ERROR);
}
sdFormat(pSdDev, DOS_OPT_FAT16);
status = OK;
break;
default: printf("nothing\n ");
(void) errnoSet (S_ioLib_UNKNOWN_REQUEST);
}
return (status);
}
/*******************************************************************************
/****************************************************************************/
LOCAL void sdInit (void)
{
semTake(McSem,WAIT_FOREVER);
SDWtReInit();
semGive(McSem);
#ifdef FD_DEBUG
printErr ("sdInit\n");
#endif /* FD_DEBUG */
}
/*******************************************************************************
/****************************************************************************/
LOCAL STATUS sdFormat(SD_DEV *pFdDev,int ty)
{
semTake(McSem,WAIT_FOREVER);
if(dosFsVolFormat (pFdDev->driName, ty, NULL) != OK)
return ERROR;
semGive(McSem);
#ifdef FD_DEBUG
printErr ("fdFormat: ");
printErr ("drive: %d\n ", pFdDev->drive);
printErr ("name: %s\n ", pFdDev->driName);
printErr ("size: %d\n ", pFdDev->driveSize);
printErr ("blkOffset:%d\n ", pFdDev->blkOffset);
#endif /* FD_DEBUG */
return (OK);
}
出错的应该是在红色表示的地方
主函数 text 输入的参数只能小于等于 4 就能创建小于等于 4MB的系统空间 不然就没法创建,在创建了 4 MB空间后能创建 b.txt 文件 但是但执行fread函数写入字符串时有些不进去 但电脑里打开这个文件发现没把应该写进来的字符串写进来,我在 sdDevCreate 函数 红色区域尝试了多种参数设置还是不行,就只能创建 4MB 空间 能创建文件 而不能写文件的数据;我的SD卡是256MB的;还请高手指点指点,我把 dosfs 需要的库都添加了;当创建大于 4MB空间是就会出现如下错误:
problem finding volume data ,trying to use the next block as boot block
ensure this device is formatted and partitions are properly handled
具体是说 0 扇区数据畸形啊什么的 就起始扇区格式不对;