📄 usbout.c
字号:
/*********************************************************************************************
* File: usbout.c
* Author: embest
* Desc: USB bulk-OUT operation related functions
* History:
* R.X.Huang, March 12, 2005
* Programming modify, style of the program: nomenclature, commentary
* Y.J.Guo, April 28, 2005
* Modifying and reusing of S3C2410X u24xmon
*********************************************************************************************/
/*------------------------------------------------------------------------------------------*/
/* include files */
/*------------------------------------------------------------------------------------------*/
#include <string.h>
#include "2410lib.h"
#include "2410usb.h"
#include "usbmain.h"
#include "usb.h"
#include "usblib.h"
#include "usbsetup.h"
#include "usbout.h"
#include "u241mon.h"
/*------------------------------------------------------------------------------------------*/
/* functions declare */
/*------------------------------------------------------------------------------------------*/
static void PrintEpoPkt(UINT8T *pt,int cnt);
static void RdPktEp3_CheckSum(UINT8T *buf,int num);
void IsrDma2(void) __attribute__ ((interrupt ("IRQ")));
/*------------------------------------------------------------------------------------------*/
/* Macro defines */
/*------------------------------------------------------------------------------------------*/
// 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 packet size constraint!!!
// EP3 = OUT end point.
unsigned char ep3Buf[EP3_PKT_SIZE];
static unsigned char tempBuf[64+1];
/*********************************************************************************************
* name: Ep3Handler
* func: Interrupt handler for EP3
* para: none
* ret: none
* modify:
* comment:
*********************************************************************************************/
void Ep3Handler(void)
{
UINT8T 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((UINT8T *)downPt,8);
if(download_run==0)
{
downloadAddress=tempDownloadAddress;
}
else
{
downloadAddress=
*((UINT8T *)(downPt+0))+
(*((UINT8T *)(downPt+1))<<8)+
(*((UINT8T *)(downPt+2))<<16)+
(*((UINT8T *)(downPt+3))<<24);
}
downloadFileSize=
*((UINT8T *)(downPt+4))+
(*((UINT8T *)(downPt+5))<<8)+
(*((UINT8T *)(downPt+6))<<16)+
(*((UINT8T *)(downPt+7))<<24);
checkSum=0;
downPt=(UINT8T *)downloadAddress;
RdPktEp3_CheckSum((UINT8T *)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((UINT8T *)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;
}
}
/*********************************************************************************************
* name: PrintEpoPkt
* func: Print out packet of EP3
* para: UINT8T *pt -- pointer to packet of EP3
* int cnt -- packet count
* ret: none
* modify:
* comment:
*********************************************************************************************/
void PrintEpoPkt(UINT8T *pt,int cnt)
{
int i;
DbgPrintf("[BOUT:%d:",cnt);
for(i=0;i<cnt;i++)
DbgPrintf("%x,",pt[i]);
DbgPrintf("]");
}
/*********************************************************************************************
* name: RdPktEp3_CheckSum
* func: CheckSum of Read packet of EP3
* para: UINT8T *buf -- pointer to buffer of EP3
* int num --
* ret: none
* modify:
* comment:
*********************************************************************************************/
void RdPktEp3_CheckSum(UINT8T *buf,int num)
{
int i;
for(i=0;i<num;i++)
{
buf[i]=(UINT8T)rEP3_FIFO;
checkSum+=buf[i];
}
}
/*********************************************************************************************
* name: IsrDma2
* func: DMA packet of EP3
* para: none
* ret: none
* modify:
* comment:
*********************************************************************************************/
void IsrDma2(void)
{
UINT8T out_csr3;
UINT32T nextTotalDmaCount;
UINT8T 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=((UINT32T)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=((UINT32T)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=((UINT32T)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;
}
/*********************************************************************************************
* name: ClearEp3OutPktReady
* func:
* para: none
* ret: none
* modify:
* comment:
*********************************************************************************************/
void ClearEp3OutPktReady(void)
{
UINT8T 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 + -