📄 test.c
字号:
#include "common.h"
#include "usb.h"
#include "sd.h"
#include "tpbulk.h"
#include "fat.h"
#include "fat32.h"
#define FAT32_ACCESS
void playMp3();
void sdPlayMp3();
//void usbDebug(unsigned char nBytes, unsigned char *pBytes);
XXGFLAGS bdata bXXGFlags; // status flags
// storage definitions
UINT8 xdata pUSERBUF[64]; // user buffer, for misc. use
UINT8 xdata DBUF[SECT_LEN] _at_ 0x0; // 512B data buffer, allocated at 0x0
UINT8 xdata RDIRBUF[SECT_LEN]; // 512 byte buffer for root directory
UINT8 xdata FATBUF[SECT_LEN]; // 512 byte buffer for FAT
UINT8 xdata respBuf[16]; // SPI response buffer
DIR_IDX dirIdx;
pDIR_INFO pDIR;
unsigned char skipSong;
//extern FILE_INFO xdata ThisFile; // file information
extern SYS_INFO_BLOCK DeviceInfo;
extern XXGPKG usbstack; // USB packet variables
extern TPB_STRUC data TPBulk_Block;
void main(void)
{
UINT8 i;
UINT16 j;
UINT32 nSector;
unsigned char temp;
bXXGFlags.SLAVE_ONLINE = FALSE;
bXXGFlags.SLAVE_FOUND = FALSE;
bXXGFlags.SLAVE_REMOVED=FALSE;
bXXGFlags.SLAVE_ENUMERATED = FALSE;
bXXGFlags.SLAVE_IS_ATTACHED = FALSE;
bXXGFlags.bUartInDone=FALSE;
bXXGFlags.bToggle=FALSE;
bXXGFlags.bIN_ISR=FALSE;
SCON = SCON_FAST; // set UART speed 38400bps
BufSel = MCU2MP3; // destination to mp3, source from mcu
SPI_CTL0 = 0x4E;
SPI_CTL1 = SPI_375K; // set low speed
P1= 0x0;
for(i=0;i<64;i++)
{
DBUF[i] = 0;
}
P3_DDR = 0xFA; // enable P3.2 for input
temp = P3;
//SendUart(&temp, 1);
if (!(temp & SD_DETECT)) // this bit pulled low if SD card is inserted, otherwise high
{
SDIdentify();
GetDevInfo();
}
else
{
usbInit();
usbReset(); // bus reset
if(!bXXGFlags.SLAVE_ENUMERATED)
{
DISABLE_INTERRUPTS; //EA=0
if(enumUsbDev(1))
{
bXXGFlags.SLAVE_ENUMERATED = TRUE;
}
else
{
bXXGFlags.SLAVE_ENUMERATED = FALSE;
bXXGFlags.SLAVE_IS_ATTACHED = FALSE;
}
}
if(bXXGFlags.SLAVE_ENUMERATED)
{ //bXXGFlags.bMassDevice is valued in EnumUsbDev();
bXXGFlags.bMassDevice=FALSE; //can only be set by enumeration
if(EnumMassDev())
{
bXXGFlags.SLAVE_IS_ATTACHED = TRUE;
temp = 0xAA;
// SendUart(&temp, 1);
}
else
{
bXXGFlags.SLAVE_IS_ATTACHED = FALSE;
}
}
}
//SendUart( (unsigned char *) &DeviceInfo, 512);
//Start fat16 file system
if (DeviceInfo.bFatType == 2)
{
dirIdx.sector = 0;
dirIdx.offset = 0;
if (bXXGFlags.SLAVE_IS_ATTACHED)
RBC_ReadOneSec(DeviceInfo.RootStartSector, RDIRBUF);
else
SDReadRdir(DeviceInfo.RootStartSector);
pDIR = (pDIR_INFO) RDIRBUF; // 16 Byte file information
// SendUart((unsigned char *) pDIR, 16);
//
for (j=0;j<DeviceInfo.BPB_RootEntCnt;j++) // loop for number of root dir entries
{
if ((pDIR->name[0] != 0xe5) && (pDIR->name[0] != 0x0) && (pDIR->attribute & 0x20))// if first char of file name is 0xe5 = erased
{
// check extension = mp3, if OK, play song
if((pDIR->extension[0] == 0x4d) && (pDIR->extension[1] == 0x50) &&
(pDIR->extension[2] == 0x33))
{
if (bXXGFlags.SLAVE_IS_ATTACHED)
playMp3();
else
sdPlayMp3();
}
}
// SendUart((unsigned char *) &dirIdx, 3);
if (dirIdx.offset == 15)
{
dirIdx.sector++;
nSector = DeviceInfo.RootStartSector + (UINT32) dirIdx.sector;
if (bXXGFlags.SLAVE_IS_ATTACHED)
RBC_ReadOneSec(nSector, RDIRBUF);
else
SDReadRdir(nSector);
dirIdx.offset = 0;
pDIR = (pDIR_INFO) RDIRBUF;
// SendUart((unsigned char *) pDIR, 16);
}
else
{
dirIdx.offset++;
pDIR++;
}
// SendUart((unsigned char *) &dirIdx, 3);
}
}
}
void playMp3()
{
UINT8 k;
UINT16 currCluster;
UINT16 nextCluster;
UINT32 file_len;
UINT32 dSectorNum;
UINT8 usTemp;
file_len = SwapINT32( pDIR->length );
currCluster = WordSwap( pDIR->startCluster );
usTemp = Mp3Ctrl;
Playmode = 0x10;
Playmode = 0x00;
DelayUs ( 100 );
// Enable MP3 decoder & clear work memory
Mp3Ctrl = 0x31;
while (file_len)
{
dSectorNum = FirstSectorofCluster ( currCluster );
nextCluster = GetNextClusterNum ( currCluster );
for ( k=0; k<DeviceInfo.BPB_SecPerClus; )
{
if(Alarm & MP3_REQ)
{
bXXGFlags.bIN_ISR=TRUE;
if(file_len < 32768) // last sector
{
// RBC_ReadOneSec(dSectorNum, DBUF);
file_len = 0;
break;
}
else
{
if(!RBC_ReadOneSec(dSectorNum, DBUF))
{
break;
}
file_len -= 512;
dSectorNum++; // point to next sector
}
// ***important: if using polling, make sure to clear Alarm
// only after sector is transfered.
Alarm = MP3_REQ;
bXXGFlags.bIN_ISR=FALSE;
k++;
}
}
currCluster = nextCluster;
}
DelayMs(500);
Mp3Ctrl = usTemp;
}
void sdPlayMp3()
{
unsigned char k;
unsigned int currCluster;
unsigned int nextCluster;
unsigned long file_len;
unsigned long dSectorNum;
unsigned char usTemp;
SPI_CTL1 = SPI_3M; // set high speed
// SendUart((unsigned char *) pDIR, 32); // debug
file_len = SwapINT32( pDIR->length );
currCluster = WordSwap( pDIR->startCluster );
dSectorNum = FirstSectorofCluster ( currCluster );
usTemp = Mp3Ctrl;
Playmode = 0x10;
Playmode = 0x40;
// DelayMs ( 200 );
// Enable MP3 decoder & clear work memory
Mp3Ctrl = 0x31;
while (file_len)
{
//dSectorNum = FirstSectorofCluster ( currCluster );
//nextCluster = GetNextClusterNum ( currCluster );
// for ( k=0; k<DeviceInfo.BPB_SecPerClus; )
// {
if(Alarm & MP3_REQ)
{
SDMp3Read(dSectorNum);
file_len -= 512;
if(file_len < 512) // last sector
file_len = 0;
dSectorNum++; // point to next sector
// ***important: if using polling, make sure to clear Alarm
// only after sector is transfered.
Alarm = MP3_REQ;
// k++;
// }
}
// currCluster = nextCluster;
}
Mp3Ctrl = usTemp;
SPI_CTL1 = SPI_375K; // set low speed
// DelayMs(200);
}
/*
void usbDebug(unsigned char nBytes, unsigned char *pBytes)
{
unsigned char i;
// send out 8 byte data on the USB
BufSel = MCU2USB; //src=CPU,dest=USB
TxPktlen = nBytes;
BufRst = 0x01; // reset write pointer
BufRst = 0x00;
for (i=0;i<nBytes;i++)
{
BufAddr = *pBytes;
pBytes++;
}
BufRst = 0x03; // flip buffer state for bus read
BufRst = 0x00;
TxAddr = 0x5;
TxCtrl = WRITE0;
DelayMs(1);
TxAddr = 0x1;
}
*/
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -