#include <stdlib.h>
#include "type.h"
#include "sx2.h"
#include "descriptors.h"
#include "SEED-DEC2407.h"
#include "DEC2407_USB.h"
#include "ep0req.h"
void SystemInit(void);
BOOL Load_descriptors(char length, char* desc);
BOOL Write_SX2reg(unsigned char addr, unsigned int value);
BOOL SX2_comwritebyte(unsigned int value);
BOOL SX2_FifoWrite(int channel,unsigned int *pdata,unsigned length);
BOOL SX2_FifoWriteSingle(int channel,unsigned int pdata);
unsigned int SX2_FifoReadSingle(int channel);
/* external variables */
extern char desctbl[];
/* global variables */
//extern BYTE irqValue; /* interrupt register value */
//extern BYTE setupBuff[8]; /* setup transaction buffer */
//extern BOOL sx2Ready; /* status byte for POST */
//extern BOOL sx2EnumOK; /* status byte for enumeration complete */
//extern BOOL sx2Setup; /* setup flag set in Int0() ISR */
//extern BOOL readFlag;
//extern BOOL buffer_halffull;
//extern BOOL buffer_full;
/* global variables specific to this test firmware */
unsigned char keepAliveCnt; /* counter of Timer0 interrupts */
HANDLE codec_command =0;
HANDLE codec_data=0;
unsigned int FifoWriteCnt = 0;
unsigned int epdatar[512] ={0};
unsigned int epdataw[512] ={0};
unsigned int epdataw1[256] ={0};
unsigned int irqValue; /* interrupt register value */
unsigned int setupBuff[8]; /* setup transaction buffer */
BOOL sx2Ready; /* status byte for POST */
BOOL sx2BusActivity; /* status byte for suspend */
BOOL sx2EnumOK; /* status byte for enumeration complete */
BOOL sx2EP0Buf; /* status for endpoint 0 buffer */
BOOL sx2Setup; /* setup flag set in Int0() ISR */
BOOL FLAGS_READ = FALSE; /*FIFO的状态读请求*/
unsigned int INT_STATUS;
unsigned int SX2_int;
BOOL readFlag;
unsigned int usbtimeout = 0x400;
unsigned int regdataread = 0;
unsigned int Fifolong = 0; /*FIFO的长度*/
unsigned int setupCnt = 0;
static BOOL setupDat = FALSE;
void main(void)
{
unsigned int regValue = 0; /* register value from a read */
unsigned int Sx2int = 0; /*SX2的中断状态*/
unsigned int Usb2or11= 1; /*USB工作在哪一个标准上*/
unsigned int endpoint0count = 0;/*EP0的数据长度*/
unsigned int endpoint0data[64] ={0};/*EP0的数据缓冲区*/
unsigned int i = 0;
unsigned int FifoStatus24 = 0; /*FIFO24的状态标识*/
unsigned int FifoStatus68 = 0; /*FIFO68的状态标识*/
unsigned int Fifostatus = 0;
BOOL hshostlink = FALSE; /*为真是高速USB接口,为假是低速USB接口*/
BOOL codec_runing = FALSE;
BOOL codec_halt = FALSE;
BOOL codec_rev = FALSE;
BOOL codec_play = FALSE;
BOOL codec_datok = FALSE;
BOOL codec_horfull = TRUE;
BOOL codec_back = FALSE;
// BOOL sx2EnumOK = FALSE;
unsigned int codec_count = 0;
unsigned int codec_sample = 0;
unsigned int codec_regvalue = 0;
unsigned int audiodata =0;
unsigned int RecievedDataLongth = 0;
unsigned int LedCount = 0; /*记录LED定时器的次数*/
unsigned int DataToEndpoint0 = 0;/*写入到Endpoint0的数据缓冲*/
unsigned long flashbaddr = 0;
unsigned int flashdata = 0;
unsigned int flasherr = 0;
unsigned long flashlong = 0;
DINT;
SystemInit();
*IFR = 0x3f;
USB_STS = 1;
for(;;)
{
INT_STATUS = INT_STS & 0x01;
if(INT_STATUS == 0)
{
SX2_int = USB_COMMAND & (SX2_INT_ENUMOK + SX2_INT_READY);
if(SX2_int)
{
// *IFR = 0x3f;
*IMR = 0x01;
EINT;
break;
}
}
else
{
// *IFR = 0x3f;
*IMR = 0x01;
EINT;
break;
}
}
for(;;)
{
/* initialize global variables */
readFlag = FALSE; /* false until register read */
sx2Ready = FALSE; /* false until POST or wakeup */
sx2BusActivity = FALSE; /* false until absence or resumption of USB bus activity */
sx2EnumOK = FALSE; /* false until ENUMOK interrupt */
sx2EP0Buf = FALSE; /* false until EP0BUF interrupt */
sx2Setup = FALSE; /* false until SETUP interrupt */
/* Initialize global variables specific to this test firmware */
keepAliveCnt = 0;
/* Initialize local variables */
/* reusable variable for read register data */
regValue = 0;
if(!Load_descriptors(DESCTBL_LEN, &desctbl[0]))
{
while(TRUE);
}
/*装载描述表后,等待自举成功*/
while(sx2EnumOK == FALSE);
/*设置当前的接口的形式*/
Write_SX2reg(SX2_IFCONFIG , 0xE8);
/*设置当前系统中各使能信号的极性
其中SLOE、SLRD、SLWR只能有EEPROM来配置*/
Write_SX2reg(SX2_FIFOPOLAR, SX2_WUPOL | SX2_EF | SX2_FF);
/*读取当前工作在哪个USB的标准*/
Read_SX2reg(SX2_FNADDR, &Usb2or11);
hshostlink = (Usb2or11 & SX2_HSGRANT) ? TRUE : FALSE;
/*初始化USB的工作状态*/
if(hshostlink ==TRUE)
{
/*工作在2.00标准,设定数字接口为16位,数据包的大小为512字节*/
Fifolong = 0x100;
Write_SX2reg(SX2_EP2PKTLENH , SX2_WORDWIDE | 0x02);
Write_SX2reg(SX2_EP2PKTLENL , 0x00);
Write_SX2reg(SX2_EP4PKTLENH , SX2_WORDWIDE | 0x02);
Write_SX2reg(SX2_EP4PKTLENL , 0x00);
Write_SX2reg(SX2_EP6PKTLENH , SX2_WORDWIDE | 0x02);
Write_SX2reg(SX2_EP6PKTLENL , 0x00);
Write_SX2reg(SX2_EP8PKTLENH , SX2_WORDWIDE | 0x02);
Write_SX2reg(SX2_EP8PKTLENL , 0x00);
}
else
{
/*工作在1.1标准,设定数字接口为16位,数据包的大小为64字节*/
Fifolong =0x20;
Write_SX2reg(SX2_EP2PKTLENH , SX2_WORDWIDE);
Write_SX2reg(SX2_EP2PKTLENL , 0x40);
Write_SX2reg(SX2_EP4PKTLENH , SX2_WORDWIDE);
Write_SX2reg(SX2_EP4PKTLENL , 0x40);
Write_SX2reg(SX2_EP6PKTLENH , SX2_WORDWIDE);
Write_SX2reg(SX2_EP6PKTLENL , 0x40);
Write_SX2reg(SX2_EP8PKTLENH , SX2_WORDWIDE);
Write_SX2reg(SX2_EP8PKTLENL , 0x40);
}
/*设置FLAGSA为FIFO6的空的标志位;
设置FLAGSB为FIFO8的空的标志位;
FLAGSC与FLAGSD的状态为默认的状态*/
Write_SX2reg(SX2_FLAGAB , SX2_FLAGA_FF6 | SX2_FLAGB_FF8);
/*清空所有的节点*/
Write_SX2reg(SX2_INPKTEND, SX2_CLEARALL);
Read_SX2reg(SX2_EP68FLAGS, &FifoStatus68);
/*自举后进行主程序的循环*/
while(sx2EnumOK)
{
/*读FIFO状态*/
if(FLAGS_READ)
{
FLAGS_READ = FALSE;
/*FIFO24状态的读取*/
if(Read_SX2reg(SX2_EP24FLAGS, &FifoStatus24))
{
/*确定是否有FIFO满*/
Fifostatus = FifoStatus24;
if(!(Fifostatus & SX2_EP2EF))
{
RecievedDataLongth = Fifolong;
for(i = 0;i<Fifolong;i++)
{
epdatar[i] = SX2_FifoReadSingle(ENDPOINT2);
/*SX2_FifoWriteSingle(ENDPOINT6,epdatar[i]);*/
}
SX2_FifoWrite(ENDPOINT6,&epdatar[0],Fifolong);
/*小于整数据包的数据提交SX2发送给主机*/
if(RecievedDataLongth<(Fifolong-1))
{
Write_SX2reg(SX2_INPKTEND, 0x06);
}
}
Fifostatus = FifoStatus24;
if(!(Fifostatus & SX2_EP4EF))
{
i = 0;
while(!(Fifostatus & SX2_EP4EF))
{
epdatar[i] = SX2_FifoReadSingle(ENDPOINT4);
Read_SX2reg(SX2_EP24FLAGS, &Fifostatus);
RecievedDataLongth = i;
i = i +1;
}
SX2_FifoWrite(ENDPOINT8,&epdatar[0],Fifolong);
/*小于整数据包的数据提交SX2发送给主机*/
if(RecievedDataLongth<(Fifolong-1))
{
Write_SX2reg(SX2_INPKTEND, 0x08);
}
}
}/*FIFO24状态的读取*/
/*FIFO68状态的读取*/
if(Read_SX2reg(SX2_EP68FLAGS, &FifoStatus68))
{
/*无操作,可由用户测试使用*/
}
}/*不是音频传送*/
/*关于setup中断的处理*/
if(sx2Setup)
{
/*清SETUP数据读的标志*/
sx2Setup = FALSE;
/*解析OUT类型的命令申请*/
if(setupBuff[0] == VR_TYPE_OUT)
{
/*分析命令类型*/
switch(setupBuff[1])
{
/*系统复位*/
case VR_RESET:
/*写0到EP0的计数寄存器,结束本次控制握手*/
Write_SX2reg(SX2_EP0BC, 0);
break;
/*读命令*/
case VR_BULK_READ:
/*usbcommand = BULK_READ;*/
/*写0到EP0的计数寄存器,结束本次控制握手*/
Write_SX2reg(SX2_EP0BC, 0);
break;
/*