#include <stdlib.h>
#include "DSP2833x_Device.h"
#include "DSP2833x_Examples.h" // DSP2833x Examples Include File
#include "type.h"
#include "sx2.h"
#include "descriptors.h"
#include "DEC28335_USB.h"
#include "ep0req.h"
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);
interrupt void XINT3_ISR_A(void);
void InitXintf(void);
void InitGpio1(void);
void UsbReset(void);
extern char desctbl[];
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 */
volatile BOOL sx2EnumOK; /* status byte for enumeration complete volatile */
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;
volatile 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;
// Step 1. Initialize System Control:
// PLL, WatchDog, enable Peripheral Clocks
// This example function is found in the DSP2833x_SysCtrl.c file.
InitSysCtrl();
// Step 2. Initialize GPIO:
InitGpio1();
// Step 3. Clear all interrupts and initialize PIE vector table:
// Disable CPU interrupts
DINT;
// Initialize the PIE control registers to their default state.
// The default state is all PIE interrupts disabled and flags
// are cleared.
// This function is found in the DSP2833x_PieCtrl.c file.
InitPieCtrl();
// Disable CPU interrupts and clear all CPU interrupt flags:
IER = 0x0000;
IFR = 0x0000;
// Initialize the PIE vector table with pointers to the shell Interrupt
// Service Routines (ISR).
// This will populate the entire table, even if the interrupt
// is not used in this example. This is useful for debug purposes.
// The shell ISR routines are found in DSP2833x_DefaultIsr.c.
// This function is found in DSP2833x_PieVect.c.
InitPieVectTable();
//初始化ZONE 区
InitXintf();
//初始化外部中断
InitXIntrupt();
//RESET USB
// UsbReset();
EALLOW; // This is needed to write to EALLOW protected registers
PieVectTable.XINT3 = &XINT3_ISR_A;
EDIS; // This is needed to disable write to EALLOW protected registers
*USB_STS = 1;
/*开中断*/
IER |= M_INT12;
for(;;)
{
if(GpioDataRegs.GPBDAT.bit.GPIO54 == 0)
{
SX2_int = *USB_COMMAND & (SX2_INT_ENUMOK + SX2_INT_READY);
if(SX2_int)
{
EINT; // Enable Global interrupt INTM
ERTM; // Enable Global realtime interrupt DBGM
break;
}
}
else
{
SX2_int = *USB_COMMAND & (SX2_INT_ENUMOK + SX2_INT_READY);
EINT; // Enable Global interrupt INTM
ERTM; // Enable Global realtime interrupt DBGM
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]))
{
asm(" ESTOP0"); // stop here
}
/*装载描述表后,等待自举成功*/
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))
{