📄 usbout.c
字号:
/****************************************************************
NAME: usbout.c
DESC: USB bulk-OUT operation related functions
HISTORY:
Mar.25.2002:purnnamu: ported for S3C2410X.
Mar.27.2002:purnnamu: DMA is enabled.
****************************************************************/
#include <string.h>
#include "option.h"
#include "2410addr.h"
#include "2410lib.h"
#include "def.h"
#include "2410usb.h"
#include "usbmain.h"
#include "usb.h"
#include "usblib.h"
#include "usbsetup.h"
#include "usbout.h"
#include "u241mon.h"
#include "usb_implement.h"
#include "yaffsfs.h"
static void PrintEpoPkt(U8 *pt,int cnt);
static void RdPktEp3_CheckSum(U8 *buf,int num);
// ===================================================================
// All following commands will operate in case
// - out_csr3 is valid.
// ===================================================================
#define CLR_EP3_OUT_PKT_READY() rOUT_CSR1_REG= ( out_csr3 &(~ EPO_WR_BITS)\
&(~EPO_OUT_PKT_READY) )
#define SET_EP3_SEND_STALL() rOUT_CSR1_REG= ( out_csr3 & (~EPO_WR_BITS)\
| EPO_SEND_STALL) )
#define CLR_EP3_SENT_STALL() rOUT_CSR1_REG= ( out_csr3 & (~EPO_WR_BITS)\
&(~EPO_SENT_STALL) )
#define FLUSH_EP3_FIFO() rOUT_CSR1_REG= ( out_csr3 & (~EPO_WR_BITS)\
|EPO_FIFO_FLUSH) )
// ***************************
// *** VERY IMPORTANT NOTE ***
// ***************************
// Prepare for the packit size constraint!!!
// EP3 = OUT end point.
extern unsigned int data_send_flag;
U8 ep3Buf[EP3_PKT_SIZE];
static U8 tempBuf[64+1];
U8 UsbRxBuf[5120];
extern unsigned int cmd_ext;
extern unsigned char data_send_count;
extern int usb_rdfile_length;
extern char usb_rdfile_flag;
extern yaffs_DIR * ydir_temp;
extern int usb_read_fd;
extern char usb_trans_flag;
extern char usb_rdfile_path[];
extern char dump_start_flag;
extern int file_info_len;
char usb_dlfile_path[1024];
int usb_dlfile_flag=0;
int usb_wd_flag=0;
#if 0
void Ep3Handler(void)
{
U8 out_csr3;
int fifoCnt;
rINDEX_REG=3;
out_csr3=rOUT_CSR1_REG;
DbgPrintf("<3:%x]",out_csr3);
if(out_csr3 & EPO_OUT_PKT_READY)
{
fifoCnt=rOUT_FIFO_CNT1_REG;
#if 0
RdPktEp3(ep3Buf,fifoCnt);
PrintEpoPkt(ep3Buf,fifoCnt);
#else
if(downloadFileSize==0)
{
RdPktEp3((U8 *)downPt,8);
if(download_run==0)
{
downloadAddress=tempDownloadAddress;
}
else
{
downloadAddress=
*((U8 *)(downPt+0))+
(*((U8 *)(downPt+1))<<8)+
(*((U8 *)(downPt+2))<<16)+
(*((U8 *)(downPt+3))<<24);
}
downloadFileSize=
*((U8 *)(downPt+4))+
(*((U8 *)(downPt+5))<<8)+
(*((U8 *)(downPt+6))<<16)+
(*((U8 *)(downPt+7))<<24);
checkSum=0;
downPt=(U8 *)downloadAddress;
RdPktEp3_CheckSum((U8 *)downPt,fifoCnt-8); //The first 8-bytes are deleted.
downPt+=fifoCnt-8;
#if USBDMA
//CLR_EP3_OUT_PKT_READY() is not executed.
//So, USBD may generate NAK until DMA2 is configured for USB_EP3;
rINTMSK|=BIT_USBD; //for debug
return;
#endif
}
else
{
#if USBDMA
Uart_Printf("<ERROR>");
#endif
RdPktEp3_CheckSum((U8 *)downPt,fifoCnt);
downPt+=fifoCnt; //fifoCnt=64
}
#endif
CLR_EP3_OUT_PKT_READY();
return;
}
//I think that EPO_SENT_STALL will not be set to 1.
if(out_csr3 & EPO_SENT_STALL)
{
DbgPrintf("[STALL]");
CLR_EP3_SENT_STALL();
return;
}
Uart_Printf("\r\n");
}
#else
void usb_cmd_parse(unsigned int usb_cmd_ext)
{
Uart_Printf("in the usb_cmd_parse\n");
switch(usb_cmd_ext)
{
case USB_READ_FILE:
#if 1
if(usb_rdfile_flag != 0)
{
Uart_Printf("terminate unfinished file reading\n");
yaffs_close(usb_read_fd);
usb_rdfile_flag=0;
}
usb_rdfile_length=0;
usb_trans_flag=0;
data_send_flag=DATA_LEN_SEND;
//usb_rdfile(usb_rdfile_path);
usb_trans_flag=1;
#endif
break;
case USB_DUMP_DIR:
#if 1
if(dump_start_flag !=0)
{
Uart_Printf("terminate unfinished dump dir\n");
file_info_len=0;
yaffs_closedir(ydir_temp);
dump_start_flag=0;
}
file_info_len=0;
data_send_flag=DATA_LEN_SEND;
#endif
break;
case USB_DELETE_FILE:
#if 1
Uart_Printf("in the USB_DELETE_FILE \r\n");
Uart_Printf("the file to delete is :%s\r\n",usb_dlfile_path);
usb_dlfile_flag=yaffs_unlink(usb_dlfile_path);
if(usb_dlfile_flag==0)
Uart_Printf("File has been deleted. \r\n");
else
Uart_Printf("Fail to delete file %s\r\n",usb_dlfile_path);
#endif
break;
default:
break;
}
}
void Ep3Handler(void)
{
U8 out_csr3;
int fifoCnt;
rINDEX_REG=3;
out_csr3=rOUT_CSR1_REG;
usb_wd_flag=1;
//Uart_Printf("Ep3Handler 1\n");
DbgPrintf("<3:%x]",out_csr3);
if(out_csr3 & EPO_OUT_PKT_READY)
{
fifoCnt=rOUT_FIFO_CNT1_REG;
#if 0
RdPktEp3(ep3Buf,fifoCnt);
PrintEpoPkt(ep3Buf,fifoCnt);
#else
if(downloadFileSize==0)
{
downPt=(U8 *)UsbRxBuf;
RdPktEp3((U8 *)downPt,8);
if(download_run==0)
{
downloadAddress=tempDownloadAddress;
}
else
{
cmd_ext=
*((U8 *)(downPt+0))+
(*((U8 *)(downPt+1))<<8)+
(*((U8 *)(downPt+2))<<16)+
(*((U8 *)(downPt+3))<<24);
Uart_Printf("download flag is %08lx\n",cmd_ext);
//cmd_ext=downloadAddress;
}
cmd_ext=
*((U8 *)(downPt+0))+
(*((U8 *)(downPt+1))<<8)+
(*((U8 *)(downPt+2))<<16)+
(*((U8 *)(downPt+3))<<24);
Uart_Printf("download flag is %08lx\n",cmd_ext);
//cmd_ext=downloadAddress;
downloadAddress=(U32)UsbRxBuf;
#if 1
downloadFileSize=
*((U8 *)(downPt+4))+
(*((U8 *)(downPt+5))<<8)+
(*((U8 *)(downPt+6))<<16)+
(*((U8 *)(downPt+7))<<24);
#endif
checkSum=0;
// downPt=(U8 *)downloadAddress;
downPt=(U8 *)UsbRxBuf;
Uart_Printf("downloadFileSize :0x%08lx,downloadAddress :0x%08lx\n",downloadFileSize,downloadAddress);
RdPktEp3_CheckSum((U8 *)downPt,fifoCnt-8); //The first 8-bytes are deleted.
Cmd_Receive((unsigned char *)downPt,downloadFileSize-8-2);
usb_cmd_parse(cmd_ext);
downPt+=fifoCnt-8;
#if USBDMA
//CLR_EP3_OUT_PKT_READY() is not executed.
//So, USBD may generate NAK until DMA2 is configured for USB_EP3;
rINTMSK|=BIT_USBD; //for debug
return;
#endif
}
else
{
#if USBDMA
Uart_Printf("<ERROR>");
#endif
RdPktEp3_CheckSum((U8 *)downPt,fifoCnt);
downPt+=fifoCnt; //fifoCnt=64
}
#endif
CLR_EP3_OUT_PKT_READY();
return;
}
//I think that EPO_SENT_STALL will not be set to 1.
if(out_csr3 & EPO_SENT_STALL)
{
DbgPrintf("[STALL]");
CLR_EP3_SENT_STALL();
return;
}
}
#endif
void PrintEpoPkt(U8 *pt,int cnt)
{
int i;
DbgPrintf("[BOUT:%d:",cnt);
for(i=0;i<cnt;i++)
DbgPrintf("%x,",pt[i]);
DbgPrintf("]");
}
#if 0
void RdPktEp3_CheckSum(U8 *buf,int num)
{
int i;
for(i=0;i<num;i++)
{
buf[i]=(U8)rEP3_FIFO;
checkSum+=buf[i];
}
}
#else
void RdPktEp3_CheckSum(U8 *buf,int num)
{
int i;
for(i=0;i<num;i++)
{
buf[i]=(U8)rEP3_FIFO;
checkSum+=buf[i];
Uart_Printf("%c",buf[i]);
}
//Uart_Printf("\n");
}
#endif
void __irq IsrDma2(void)
{
U8 out_csr3;
U32 nextTotalDmaCount;
U8 saveIndexReg=rINDEX_REG;
rINDEX_REG=3;
out_csr3=rOUT_CSR1_REG;
ClearPending(BIT_DMA2);
totalDmaCount+=0x80000;
if(totalDmaCount>=downloadFileSize)// is last?
{
totalDmaCount=downloadFileSize;
ConfigEp3IntMode();
if(out_csr3& EPO_OUT_PKT_READY)
{
CLR_EP3_OUT_PKT_READY();
}
rINTMSK|=BIT_DMA2;
rINTMSK&=~(BIT_USBD);
}
else
{
if((totalDmaCount+0x80000)<downloadFileSize)
{
nextTotalDmaCount=totalDmaCount+0x80000;
if((nextTotalDmaCount+0x80000)<downloadFileSize)
{
//for (2~n)th autoreload.
while((rDSTAT2&0xfffff)==0); //wait until autoreload occurs.
rDIDST2=((U32)downloadAddress+nextTotalDmaCount-8);
rDIDSTC2=(0<<1)|(0<<0);
rDCON2=rDCON2&~(0xfffff)|(0x80000);
while(rEP3_DMA_TTC<0xfffff)
{
rEP3_DMA_TTC_L=0xff;
rEP3_DMA_TTC_M=0xff;
rEP3_DMA_TTC_H=0xf;
//0xfffff;
}
}
else
{
while((rDSTAT2&0xfffff)==0); //wait until autoreload occurs.
rDIDST2=((U32)downloadAddress+nextTotalDmaCount-8);
rDIDSTC2=(0<<1)|(0<<0);
rDCON2=rDCON2&~(0xfffff)|(downloadFileSize-nextTotalDmaCount);
while(rEP3_DMA_TTC<0xfffff)
{
rEP3_DMA_TTC_L=0xff;
rEP3_DMA_TTC_M=0xff;
rEP3_DMA_TTC_H=0xf;
//0xfffff;
}
}
}
else
{
while((rDSTAT2&0xfffff)==0); //wait until autoreload occurs.
rDIDST2=((U32)downloadAddress+downloadFileSize-8); //for next autoreload.
rDIDSTC2=(0<<1)|(0<<0);
rDCON2=rDCON2&~(0xfffff)|(0);
//There is no 2nd autoreload. This will not be used.
//rDMA_TX+=0x0; //USBD register
}
}
rINDEX_REG=saveIndexReg;
}
void ClearEp3OutPktReady(void)
{
U8 out_csr3;
rINDEX_REG=3;
out_csr3=rOUT_CSR1_REG;
CLR_EP3_OUT_PKT_READY();
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -