Petit FatFS在51单片机上的移植

来源:互联网 发布:西门子s7300仿真软件 编辑:程序博客网 时间:2024/05/22 14:41

    一直对MP3的制作报有极大的兴趣,之前己经用沁恒的CH375 + STC12C5A60S2 + VS1003方案实现,但对于底层文件系统和USB通讯协议都是由沁恒提供的库实现的,并且它不开源,同时考虑到一块CH375价格在20价左右,不菲,于是下定决心要直接在SD卡上实现文件系统的管理。

    在网上找了下文件系统,发现几乎没有针对51单片机的文件系统,偶然之间在看到网上看到了Petit FatFS,http://hi.baidu.com/tlptotop/blog/item/21c30b2ae0c9a4f5e7cd40de.html

兴奋之余,立即着手开始移植,由于对SD卡己经有一些的时间的认识于是移植的过程倒是没没有花费多少时间,由于考虑做的是MP3,在移植时并没有移植写函数。

涉及的文件是diskio.c中的disk_readp()函数,修改如下

/*-----------------------------------------------------------------------*/
/* Read Partial Sector                                                   */
/*-----------------------------------------------------------------------*/

DRESULT disk_readp (
 BYTE* dest,   /* Pointer to the destination object */
 DWORD sector,  /* Sector number (LBA) */
 WORD sofs,   /* Offset in the sector */
 WORD count   /* Byte count (bit15:destination) */
)
{
 DRESULT res;
 // Put your code here
 int8u temp; 
 int16u i=0; 
    int8u cmd[]={0x51,0x00,0x00,0x00,0x00,0xff};
 sector=sector<<9;
 cmd[1]=((sector&0xff000000)>>24);
 cmd[2]=((sector&0x00ff0000)>>16);
 cmd[3]=((sector&0x0000ff00)>>8);
 SPI_CS=0;
//写命令
 do{
  temp=SDWriteCmd(cmd);
  i++;
  if(i>100)
   return 0x01;
 }while(temp!=0x00);
 while(SDReadByte()!=0xfe);
 for(i=0;i<sofs;i++)
  SDReadByte();//跳过sofs个字节
 for(i=0;i<count;i++)//读取count个字节
  *(dest+i)=SDReadByte();
 SDReadByte();
 SDReadByte();
 SPI_CS=1;
 SDWriteByte(0xff);
 res=RES_OK;
 return res;
}

原创粉丝点击