“玩板”+于振南的STM32开发板实验3
<div class='showpostmsg'><p><br /><br />
<br />
<b><font color="#5E7384">此内容由EEWORLD论坛网友<font size="3">chenbingjy</font>原创,如需转载或用于商业用途需征得作者同意并注明出处</font></b><br />
第7章层递删截,通盘格空:文件,目录的删除及磁盘格式化。</p>
<p>文件删除的实现;</p>
<p></p>
<p>#ifdef ZNFAT_DELETE_FILE<br />
UINT8 znFAT_Delete_File(INT8 *filepath) <br />
{<br />
UINT8 res=0;<br />
struct FileInfo fi; </p>
<p> struct FDIesInSEC *pitems; //Ö¸ÏòÎļþĿ¼ÏîÉÈÇøÊý¾ÝµÄÖ¸Õë<br />
struct FDI *pitem; //Ö¸ÏòÎļþĿ¼ÏîÊý¾ÝµÄÖ¸Õë</p>
<p> res=znFAT_Open_File(&fi,filepath,0,BOOL_TRUE);<br />
if(res) return res;</p>
<p> while(!res) //´ò¿ªÎļþ³É¹¦<br />
{<br />
znFAT_Device_Read_Sector(fi.FDI_Sec,znFAT_Buffer); //¶ÁÈ¡ÎļþµÄÎļþĿ¼ÏîËùÔÚÉÈÇø<br />
pitems=(struct FDIesInSEC *)znFAT_Buffer;<br />
pitem=(pitems->FDIes)+fi.nFDI;</p>
<p> if(0!=fi.File_StartClust) Destroy_FAT_Chain(fi.File_StartClust); //Ïú»ÙÕûÌõ´ØÁ´</p>
<p> znFAT_Device_Read_Sector(fi.FDI_Sec,znFAT_Buffer); //¶ÁÈ¡ÎļþĿ¼ÏîËùÔÚµÄÉÈÇø</p>
<p> pitem->Name=0XE5; //¸øÎļþĿ¼Ïî´òÉÏ"ÒÑɾ³ý"µÄ±ê¼Ç<br />
pitem->HighClust=pitem->HighClust=0; //¿ªÊ¼´ØµÄ¸ß×ÖÇå0</p>
<p> znFAT_Device_Write_Sector(fi.FDI_Sec,znFAT_Buffer); //»ØдÉÈÇø</p>
<p> res=znFAT_Open_File(&fi,filepath,0,BOOL_TRUE);<br />
}</p>
<p> znFAT_Close_File(&fi);</p>
<p> #ifdef RT_UPDATE_FSINFO<br />
Update_FSINFO();<br />
#endif</p>
<p> return ERR_SUCC; <br />
}<br />
#endif</p>
<p>删除的是dh.mp3。</p>
<p> </p>
</div><script> var loginstr = '<div class="locked">查看本帖全部内容,请<a href="javascript:;" style="color:#e60000" class="loginf">登录</a>或者<a href="https://bbs.eeworld.com.cn/member.php?mod=register_eeworld.php&action=wechat" style="color:#e60000" target="_blank">注册</a></div>';
if(parseInt(discuz_uid)==0){
(function($){
var postHeight = getTextHeight(400);
$(".showpostmsg").html($(".showpostmsg").html());
$(".showpostmsg").after(loginstr);
$(".showpostmsg").css({height:postHeight,overflow:"hidden"});
})(jQuery);
} </script><script type="text/javascript">(function(d,c){var a=d.createElement("script"),m=d.getElementsByTagName("script"),eewurl="//counter.eeworld.com.cn/pv/count/";a.src=eewurl+c;m.parentNode.insertBefore(a,m)})(document,523)</script> <p>第二,删除目录:</p>
<p>删除目录的实现过程主要有三种操作:</p>
<p>1.在当前目录下一次搜索,遇到目录就进入,遇到文件就删除。</p>
<p>2.如果目录为空,则直接返回上一级目录。</p>
<p>3.知道返回顶层目录,将其删除,目录删除操作完成。</p>
<p></p>
<p>#ifdef ZNFAT_DELETE_DIR<br />
UINT8 znFAT_Delete_Dir(INT8 *dirpath) <br />
{<br />
UINT32 top_dir_first_cluster=0,sub_dir_first_cluster=0;<br />
UINT8 res=0;</p>
<p> struct FDIesInSEC *pitems; //Ö¸ÏòÎļþĿ¼ÏîÉÈÇøÊý¾ÝµÄÖ¸Õë</p>
<p> struct FileInfo fi;</p>
<p> res=znFAT_Open_File(&fi,dirpath,0,BOOL_FALSE); //³¢ÊÔ´ò¿ªÄ¿Â¼</p>
<p> if(res) return res; //Èç¹û´ò¿ªÄ¿Â¼Ê§°Ü£¬ÔòÖ±½Ó·µ»Ø´íÎóÂë</p>
<p> while(!res) //Ŀ¼´ò¿ª³É¹¦<br />
{<br />
top_dir_first_cluster=fi.File_StartClust; //¶¥²ãĿ¼µÄÊ×´Ø<br />
sub_dir_first_cluster=top_dir_first_cluster;</p>
<p> //ÒÔÏ´úÂ뽫¶¥¼¶Ä¿Â¼ÏÂËùÓÐÄÚÈÝ(Îļþ¡¢×ÓĿ¼¼°×ÓĿ¼ÖеÄÄÚÈÝ£¬º¬µÝ¹é)Ïú»Ù</p>
<p> Enter_Deep_Ahead_Dir(&sub_dir_first_cluster); //»ñÈ¡×îÉî×î¿¿Ç°µÄĿ¼Ê×´Ø</p>
<p> while(sub_dir_first_cluster!=top_dir_first_cluster) //Èç¹û×îÉî×ǰĿ¼Ê״ؾÍÊÇҪɾ³ýµÄ¶¥¼¶Ä¿Â¼<br />
{ //Ôò˵Ã÷¶¥¼¶Ä¿Â¼ÏµÄËùÓÐÄÚÈݶ¼ÒѾ-Çå¿Õ<br />
Get_Upper_Dir(&sub_dir_first_cluster); //»ñÈ¡ÉÏÒ»²ãĿ¼Ê×´Ø <br />
<br />
Have_Any_SubDir_With_Del_ForeFile(&sub_dir_first_cluster,BOOL_TRUE); //°ÑÒÑÇå¿ÕÆäÄÚÈݵÄ×ÓĿ¼Ïú»Ù </p>
<p> Enter_Deep_Ahead_Dir(&sub_dir_first_cluster); //»ñÈ¡×îÉî×î¿¿Ç°µÄĿ¼Ê×´Ø<br />
} </p>
<p> //Ïú»Ù¶¥¼¶Ä¿Â¼¶ÔÓ¦µÄÎļþĿ¼Ïî¼°Æä´ØÁ´</p>
<p> znFAT_Device_Read_Sector(fi.FDI_Sec,znFAT_Buffer); //¶ÁÈ¡ÎļþĿ¼ÏîËùÔÚµÄÉÈÇø<br />
pitems=(struct FDIesInSEC *)znFAT_Buffer;</p>
<p> Destroy_FDI((pitems->FDIes)+fi.nFDI); //Ïú»Ù¶¥¼¶Ä¿Â¼</p>
<p> znFAT_Device_Read_Sector(fi.FDI_Sec,znFAT_Buffer); //¶ÁÈ¡ÎļþĿ¼ÏîËùÔÚµÄÉÈÇø<br />
(pitems->FDIes).Name=0XE5;<br />
(pitems->FDIes).HighClust=(pitems->FDIes).HighClust=0;</p>
<p> znFAT_Device_Write_Sector(fi.FDI_Sec,znFAT_Buffer); //»ØдÉÈÇø</p>
<p> res=znFAT_Open_File(&fi,dirpath,0,BOOL_FALSE); //³¢ÊÔ´ò¿ªÄ¿Â¼<br />
}<br />
<br />
znFAT_Close_File(&fi);</p>
<p> #ifdef RT_UPDATE_FSINFO<br />
Update_FSINFO();<br />
#endif</p>
<p> return ERR_SUCC; <br />
}<br />
#endif<br />
工程:</p>
<p></p>
<p><br />
磁盘格式化:</p>
<p>格式化的核心工作是对主要的功能部分进行构造,构造的核心是在于DBR(BPB)。</p>
<p>DBR中的BPB记录了FAT32的重要参数,主要有三项:有效总扇区数,每簇扇区数,FAT表扇区数。</p>
<p></p>
<p>#ifdef ZNFAT_MAKE_FS<br />
UINT8 znFAT_Make_FS(UINT32 tt_sec,UINT16 clu_sz) //¸ñʽ»¯ tt_sec ×ÜÉÈÇøÊý clu_sz Ö¸¶¨µÄ´Ø´óС<br />
{ //ÈôΪ0Ôò°´´ÅÅÌ´óСѡ¶¨Ä¬ÈÏÖµ ¸ñʽ»¯²ßÂÔ²ÉÓñȽϼòµ¥µÄSFD²ßÂÔ<br />
//ÎÞMBR£¬²»Ö§³Ö·ÖÇø£¬Ö±½Ó´ÓDBR¿ªÊ¼<br />
struct DBR *pdbr;<br />
struct FSInfo *pfsinfo;</p>
<p> UINT32 temp=0,temp1=0,temp2=0;</p>
<p> tt_sec/=(UINT32)(NSECPERCYLINDER); <br />
tt_sec*=(UINT32)(NSECPERCYLINDER);//ÉáÈ¥¡°Ê£ÓàÉÈÇø¡±£¬Ê£ÓàÉÈÇøÊÇÖ¸²»×ãÒ»¸öÖùÃæµÄÉÈÇøÊý</p>
<p> //=================ºÏ³ÉDBRÉÈÇøÊý¾Ý=============================================================<br />
PGM2RAM(znFAT_Buffer,_dbr,512); //´ÓÄ£°æÊý×éÖаÑÊý¾Ý°áµ½ÄÚ²¿»º³åÇø<br />
pdbr=(struct DBR *)znFAT_Buffer;</p>
<p> pdbr->BPB_SecPerClus=(UINT8)(clu_sz/512); //ÿ´ØÉÈÇøÊý<br />
if(0==pdbr->BPB_SecPerClus) pdbr->BPB_SecPerClus=Get_Recmd_szClu(tt_sec);<br />
if(0==pdbr->BPB_SecPerClus) return ERR_FMT_TOO_LOW_VOLUME; //ÈÝÁ¿Ì«Ð¡£¬²»ÄÜÓÃFAT32½øÐиñʽ»¯</p>
<p> temp1=pdbr->BPB_SecPerClus;</p>
<p> pdbr->BPB_TotSec32=(UINT8)((tt_sec) &0X000000FF);<br />
pdbr->BPB_TotSec32=(UINT8)((tt_sec>>8) &0X000000FF);<br />
pdbr->BPB_TotSec32=(UINT8)((tt_sec>>16)&0X000000FF);<br />
pdbr->BPB_TotSec32=(UINT8)((tt_sec>>24)&0X000000FF); //¸Ã·ÖÇøµÄ×ÜÉÈÇøÊý</p>
<p> temp=(tt_sec-32)/(((UINT32)NITEMSINFATSEC)*((UINT32)(pdbr->BPB_SecPerClus)));<br />
if((tt_sec-32)%((UINT32)NITEMSINFATSEC)*((UINT32)pdbr->BPB_SecPerClus)) temp++; //((tt_sec-32)-2*FATsz)/(SecPerClus*128)=FATsz ½âÕâ¸öµÈʽ<br />
temp2=temp;<br />
<br />
pdbr->BPB_FATSz32=(UINT8)((temp) &0X000000FF);<br />
pdbr->BPB_FATSz32=(UINT8)((temp>>8) &0X000000FF);<br />
pdbr->BPB_FATSz32=(UINT8)((temp>>16)&0X000000FF);<br />
pdbr->BPB_FATSz32=(UINT8)((temp>>24)&0X000000FF); //FAT±íµÄÉÈÇøÊý</p>
<p> znFAT_Device_Write_Sector(0,znFAT_Buffer); //½«ºÏ³ÉºÃµÄDBRÊý¾ÝдÈëµ½0ÉÈÇøÖÐÈ¥</p>
<p> //===============================ÒÔÉÏ´úÂëÍê³É¶ÔDBRÉÈÇøÊý¾ÝµÄºÏ³É===============================</p>
<p> //===================================ÒÔÏ´úÂëÍê³É¶ÔFSINFOÉÈÇøÊý¾ÝµÄºÏ³É========================</p>
<p> Memory_Set(znFAT_Buffer,ZNFAT_BUF_SIZE,0); //½«ÄÚ²¿»º³åÇøÇå0<br />
PGM2RAM(znFAT_Buffer,_fsinfo_1,4); //½«FSINFOÄ£°åÊý¾ÝµÄµÚÒ»²¿·Ö°á¹ýÀ´<br />
PGM2RAM(znFAT_Buffer+484,_fsinfo_2,28); //½«FSINFOÄ£°åÊý¾ÝµÄµÚ¶þ²¿·Ö°á¹ýÀ´<br />
//×¢£ºFSINFOÄ£°åÊý¾Ý·ÖΪÁ½²¿·Ö£¬Ö÷ÒªÊÇÒòΪÆäÖÐÓоø´ó<br />
//²¿·ÖÊÇ0£¬ÎªÁ˽ÚÊ¡¹Ì»¯Êý¾ÝÁ¿£¬¼õÉÙFLASHROM¿Õ¼äµÄʹÓÃÁ¿<br />
pfsinfo=(struct FSInfo *)znFAT_Buffer;<br />
<br />
temp=(tt_sec-32-2*temp)/((UINT32)(temp1))-1; //×Ü´ØÊý-1£¬ÒòΪµÚ2´ØΪÊ×Ŀ¼£¬ÒѾ-±»¾í±êÕ¼ÓÃ<br />
pfsinfo->Free_Cluster=(UINT8)((temp) &0X000000FF);<br />
pfsinfo->Free_Cluster=(UINT8)((temp>>8) &0X000000FF);<br />
pfsinfo->Free_Cluster=(UINT8)((temp>>16)&0X000000FF);<br />
pfsinfo->Free_Cluster=(UINT8)((temp>>24)&0X000000FF); //Ê£Óà¿Õ´ØÊý</p>
<p> znFAT_Device_Write_Sector(1,znFAT_Buffer); //½«ºÏ³ÉºÃµÄDBRÊý¾ÝдÈëµ½64ÉÈÇøÖÐÈ¥£¬¼´DBRÉÈÇøµÄºóÒ»¸öÉÈÇø</p>
<p> //=====================================ÒÔÉÏ´úÂëÍê³É¶ÔFSINFOÉÈÇøÊý¾ÝµÄºÏ³É=====================</p>
<p> //=====================================ÒÔÏ´úÂëÍê³É¶ÔFAT±íµÄ³õʼ»¯============================</p>
<p> znFAT_Device_Clear_nSector(temp1,32+2*temp2);<br />
znFAT_Device_Clear_nSector(temp2-1,33);<br />
znFAT_Device_Clear_nSector(temp2-1,33+temp2);</p>
<p> PGM2RAM(znFAT_Buffer,_fatsec,12); //½«FAT±íÄ£°æÊý¾Ý°áµ½ÄÚ²¿»º³åÇø<br />
znFAT_Device_Write_Sector(32,znFAT_Buffer); //ÏòFAT±í1ÖÐдÈë0<br />
znFAT_Device_Write_Sector(32+temp2,znFAT_Buffer); //ÏòFAT±í2ÖÐдÈë0</p>
<p> //=====================================ÒÔÉÏ´úÂëÍê³É¶ÔFAT±íµÄ³õʼ»¯============================</p>
<p> //=====================================ÒÔÏ´úÂë¶ÔÊý¾ÝÇøÊ×ÉÈÇø½øÐгõʼ»¯£¬Ð´Èë¾í±ê=============</p>
<p> PGM2RAM(znFAT_Buffer,_1stsec,26); <br />
znFAT_Device_Write_Sector(32+2*temp2,znFAT_Buffer); //ÏòÊý¾ÝÇøµÚÒ»¸öÉÈÇøдÈëÊý¾Ý</p>
<p> //=====================================ÒÔÉÏ.....==============================================<br />
return ERR_SUCC;<br />
} <br />
#endif</p>
<p>试的时候,感觉格式化时间比较长。</p>
<p><img height="50" src="https://bbs.eeworld.com.cn/static/editor/plugins/hkemoji/sticker/onion/Onion--12.gif" width="50" /></p>
<p>感谢分享!楼主继续!</p>
页:
[1]