文件操作与WAV音频播放实验程序解析.docx
《文件操作与WAV音频播放实验程序解析.docx》由会员分享,可在线阅读,更多相关《文件操作与WAV音频播放实验程序解析.docx(46页珍藏版)》请在冰点文库上搜索。
文件操作与WAV音频播放实验程序解析
//文件操作与WAV音频播放实验程序解析
//头文件
#include
#include"inc/hw_memmap.h"
#include"inc/hw_types.h"
#include"driverlib/fpu.h"
#include"driverlib/gpio.h"
#include"driverlib/interrupt.h"
#include"driverlib/rom.h"
#include"driverlib/sysctl.h"
#include"driverlib/systick.h"
#include"driverlib/pin_map.h"
#include"driverlib/ssi.h"
#include"grlib/grlib.h"
#include"utils/cmdline.h"
#include"utils/uartstdio.h"
#include"fatfs/src/ff.h"
#include"fatfs/src/diskio.h"
#include"drivers/cfal96x64x16.h"
#include"drivers/buttons.h"
//*****************************************************************************
//Definesthesizeofthebuffersthatholdthepath,ortemporarydatafrom
//theSDcard.Therearetwobuffersallocatedofthissize.Thebuffersize
//mustbelargeenoughtoholdthelongestexpectedfullpathname,including
//thefilename,andatrailingnullcharacter.
//*****************************************************************************
#definePATH_BUF_SIZE80
//*****************************************************************************
//Definesthesizeofthebufferthatholdsthecommandline.
//*****************************************************************************
#defineCMD_BUF_SIZE64
//*****************************************************************************
//Thisbufferholdsthefullpathtothecurrentworkingdirectory.Initially
//itisroot("/").
//*****************************************************************************
staticcharg_cCwdBuf[PATH_BUF_SIZE]="/";
//*****************************************************************************
//Atemporarydatabufferusedwhenmanipulatingfilepaths,orreadingdata
//fromtheSDcard.
//*****************************************************************************
staticcharg_cTmpBuf[PATH_BUF_SIZE];
//*****************************************************************************
//Aflagtoindicatetheselectbuttonwaspressed.
//*****************************************************************************
staticvolatileunsignedcharbSelectPressed=0;
staticvolatileunsignedcharbUpPressed=0;
staticvolatileunsignedcharbDownPressed=0;
staticvolatileunsignedcharbLeftPressed=0;
staticvolatileunsignedcharbRightPressed=0;
//*****************************************************************************
//Thebufferthatholdsthecommandline.
//*****************************************************************************
staticcharg_cCmdBuf[CMD_BUF_SIZE];
//*****************************************************************************
//ThefollowingaredatastructuresusedbyFatFs.
//*****************************************************************************
staticFATFSg_sFatFs;
staticDIRg_sDirObject;
staticFILINFOg_sFileInfo;
staticFILg_sFileObject;
tRectanglesRect;
chartmp[100];
charcover[20]="";
//*****************************************************************************
//AstructurethatholdsamappingbetweenanFRESULTnumericalcode,anda
//stringrepresentation.FRESULTcodesarereturnedfromtheFatFsFATfile
//systemdriver.
//*****************************************************************************
typedefstruct
{
FRESULTfresult;
char*pcResultStr;
}
tFresultString;
//wav文件头数据类型
typedefstruct_tagMsWavPcmHeader44{
charChunkID[4];//"RIFF";The"RIFF"themainchunk;
unsignedlongChunkSize;//FileSize-8;Thesizefollowingthisdata
charFormat[4];//"WAVE";The"WAVE"formatconsistsoftwosubchunks:
"fmt"and"data"
charSubChunk1ID[4];//"fmt"
unsignedlongSubChunk1Size;//16forPCM.Thisisthesizeoftherestofthesubchunkwhichfollowsthisdata.
unsignedshortAudioFormat;//1forPCM.Linearquantization
unsignedshortNumChannels;//1->Mono,2->stereo,etc..
unsignedlongSampleRate;//8000,11025,16000,44100,48000,etc..
unsignedlongByteRate;//=SampleRate*NumChannels*BitsPerSample/8
unsignedshortBlockAlign;//=NumChannels*BitsPerSample/8
unsignedshortBitsPerSample;//8->8bits,16->16bits,etc..
charSubChunk2ID[4];//"data"
unsignedlongSubChun2Size;//=NumSamples*NumChannels*BitsPerSample/8.Thesizeofdata
}wav_pcm_header44;
//*****************************************************************************
//Amacrotomakeiteasytoaddresultcodestothetable.
//*****************************************************************************
#defineFRESULT_ENTRY(f){(f),(#f)}
//*****************************************************************************
//AtablethatholdsamappingbetweenthenumericalFRESULTcodeandit's
//nameasastring.Thisisusedforlookinguperrorcodesforprintingto
//theconsole.
//*****************************************************************************
tFresultStringg_sFresultStrings[]=
{
FRESULT_ENTRY(FR_OK),
FRESULT_ENTRY(FR_NOT_READY),
FRESULT_ENTRY(FR_NO_FILE),
FRESULT_ENTRY(FR_NO_PATH),
FRESULT_ENTRY(FR_INVALID_NAME),
FRESULT_ENTRY(FR_INVALID_DRIVE),
FRESULT_ENTRY(FR_DENIED),
FRESULT_ENTRY(FR_EXIST),
FRESULT_ENTRY(FR_RW_ERROR),
FRESULT_ENTRY(FR_WRITE_PROTECTED),
FRESULT_ENTRY(FR_NOT_ENABLED),
FRESULT_ENTRY(FR_NO_FILESYSTEM),
FRESULT_ENTRY(FR_INVALID_OBJECT),
FRESULT_ENTRY(FR_MKFS_ABORTED)
};
//*****************************************************************************
//Amacrothatholdsthenumberofresultcodes.
//*****************************************************************************
#defineNUM_FRESULT_CODES(sizeof(g_sFresultStrings)/sizeof(tFresultString))
//*****************************************************************************
//GraphicscontextusedtoshowtextontheCSTNdisplay.
//*****************************************************************************
tContextg_sContext;
//*****************************************************************************
//Thisfunctionreturnsastringrepresentationofanerrorcodethatwas
//returnedfromafunctioncalltoFatFs.Itcanbeusedforprintinghuman
//readableerrormessages.
//*****************************************************************************
constchar*
StringFromFresult(FRESULTfresult)
{
unsignedintuIdx;
//
//Enteralooptosearchtheerrorcodetableforamatchingerrorcode.
//
for(uIdx=0;uIdx{
//
//Ifamatchisfound,thenreturnthestringnameoftheerrorcode.
//
if(g_sFresultStrings[uIdx].fresult==fresult)
{
return(g_sFresultStrings[uIdx].pcResultStr);
}
}
//
//Atthispointnomatchingcodewasfound,soreturnastringindicating
//anunknownerror.
//
return("UNKNOWNERRORCODE");
}
//*****************************************************************************
//ThisisthehandlerforthisSysTickinterrupt.FatFsrequiresatimertick
//every10msforinternaltimingpurposes.
//*****************************************************************************
void
SysTickHandler(void)
{
//
//CalltheFatFsticktimer.
//
unsignedcharucData,ucDelta;
disk_timerproc();
//Getbuttonsstatususingbuttondebouncerdriver
ucData=ButtonsPoll(&ucDelta,0);
//Seeiftheselectbuttonwasjustpressed.
if(BUTTON_PRESSED(SELECT_BUTTON,ucData,ucDelta))
{
bSelectPressed=1;
}
if(BUTTON_RELEASED(SELECT_BUTTON,ucData,ucDelta))
{
bSelectPressed=0;
}
if(BUTTON_PRESSED(UP_BUTTON,ucData,ucDelta))
{
bUpPressed=1;
}
if(BUTTON_RELEASED(UP_BUTTON,ucData,ucDelta))
{
bUpPressed=0;
}
if(BUTTON_PRESSED(DOWN_BUTTON,ucData,ucDelta))
{
bDownPressed=1;
}
if(BUTTON_RELEASED(DOWN_BUTTON,ucData,ucDelta))
{
bDownPressed=0;
}
if(BUTTON_PRESSED(LEFT_BUTTON,ucData,ucDelta))
{
bLeftPressed=1;
}
if(BUTTON_RELEASED(LEFT_BUTTON,ucData,ucDelta))
{
bLeftPressed=0;
}
if(BUTTON_PRESSED(RIGHT_BUTTON,ucData,ucDelta))
{
bRightPressed=1;
}
if(BUTTON_RELEASED(RIGHT_BUTTON,ucData,ucDelta))
{
bRightPressed=0;
}
}
//*****************************************************************************
//Thisfunctionimplementsthe"ls"command.Itopensthecurrentdirectory
//andenumeratesthroughthecontents,andprintsalineforeachitemit
//finds.Itshowsdetailssuchasfileattributes,timeanddate,andthe
//filesize,alongwiththename.Itshowsasummaryoffilesizesattheend
//alongwithfreespace.
//*****************************************************************************
int
Cmd_ls(intargc,char*argv[])
{
unsignedlongulTotalSize;
unsignedlongulFileCount;
unsignedlongulDirCount;
FRESULTfresult;
FATFS*pFatFs;
//Openthecurrentdirectoryforaccess.
fresult=f_opendir(&g_sDirObject,g_cCwdBuf);
//Checkforerrorandreturnifthereisaproblem.
if(fresult!
=FR_OK)
{
return(fresult);
}
ulTotalSize=0;
ulFileCount=0;
ulDirCount=0;
//Giveanextrablanklinebeforethelisting.
UARTprintf("\n");
//Enterlooptoenumeratethroughalldirectoryentries.
for(;;)
{
//Readanentryfromthedirectory.
fresult=f_readdir(&g_sDirObject,&g_sFileInfo);
//Checkforerrorandreturnifthereisaproblem.
if(fresult!
=FR_OK)
{
return(fresult);
}
//Ifthefilenameisblank,thenthisistheendofthelisting.
if(!
g_sFileInfo.fname[0])
{
break;
}
//Iftheattribueisdirectory,thenincrementthedirectorycount.
if(g_sFileInfo.fattrib&AM_DIR)
{
ulDirCount++;
}
//Otherwise,itisafile.Incrementthefilecount,andaddinthe
//filesizetothetotal.
else
{
ulFileCount++;
ulTotalSize+=g_sFileInfo.fsize;
}
//Printtheentryinformationonasinglelinewithformatting