📄 s3c2510usbloopback.c
字号:
printf("USB_RT_VENDOR\n");
#endif /* DEBUG_TRACE */
s3c2510UsbDeviceRequestVendor();
break;
default:
printf("s3c2510Usb Error: Unknown category, 0x%02X\n", usbSetup.bmRequestType & USB_RT_CATEGORY_MASK);
break;
}
}
break;
case EP0_STATE_TRANSMIT:
#ifdef DEBUG_TRACE
printf("EP0_STATE_TRANSMIT\n");
#endif /* DEBUG_TRACE */
s3c2510UsbEp0Tx();
break;
case EP0_STATE_RECEIVE:
#ifdef DEBUG_TRACE
printf("EP0_STATE_RECEIVE\n");
#endif /* DEBUG_TRACE */
s3c2510UsbEp0Rx();
break;
}
}
/*******************************************************************************
*
* s3c2510UsbEP1Handler - Endpoint 1 interrupt handler.
*
* RETURNS : N/A
*/
void s3c2510UsbEP1Handler(void)
{
UINT32 length = 0;
#if ! GDMA_Transfer_Mode
UINT32 i = 0;
#endif
#ifdef DEBUG_TRACE
printf(" EP1 Handler entry \n");
#endif
/* Check if data is available in EP1 FIFO */
if(((*S3C2510_USBCSR(1))& S3C2510_USBEPCSR_OORDY) != S3C2510_USBEPCSR_OORDY)
{
/* log the meassage no data available on EP1 */
#ifdef DEBUG_TRACE
printf("no data available on EP1 \n");
#endif
return;
}
/* we are not ready to recieve as previous buffer is not loopbacked yet */
if(ep1RxBuffer == NULL) {
/* log the meassage EP1 Rx buffer is NULL */
printf("ep1RxBuffer NULL \n");
return;
}
/* Update the transfer length by shifting the write count by */
length = *S3C2510_USBWCEP(1);
length = length >> S3C2510_USBWCEP_WRTCNT_SHIFT;
/* if to use DMA for tarnsfers */
#if GDMA_Transfer_Mode
/* Update the source address for DMA transfer */
*S3C2510_DSAR(GDMA_USB_OUT)=(UINT)S3C2510_USBFIFO(1);
/* update the destination address */
*S3C2510_DDAR(GDMA_USB_OUT)=(UINT)ep1RxBuffer;
/* update the transfer count for DMA */
*S3C2510_DTCR(GDMA_USB_OUT) = length;
/* enable the DMA run register */
*S3C2510_DRER(GDMA_USB_OUT) |= S3C2510_DCON_RE;
#if 0
/* update the Rx buffer */
for(i=0; i < length/4; i++)
{
ep1RxBuffer++;
}
#else
ep1RxBuffer += length/4;
#endif
#else
/* tarnsfer the data from EP3 FIFO into ep3RxBuffer buffer */
for(i=0; i < length/4; i++)
{
*ep1RxBuffer = *S3C2510_USBFIFO(1);
ep1RxBuffer++;
}
/* transfer last non-word bytes of data */
if (length % 4)
{
*ep1RxBuffer = *S3C2510_USBFIFO(1);
}
#endif
/* First packet */
if (ep1RxCount == 0)
{
#if GDMA_Transfer_Mode
/* Wait until counter equals 0 */
while(*(S3C2510_DTCR(GDMA_USB_OUT)) & 0x00ffffff);
#endif
/* First two bytes gives the length of loopback data + this len header */
ep12LoopbackLen = *((UINT16 *)ep12LoopbackBuffer);
}
#ifdef DEBUG_TRACE
printf("ep12LoopbackLen is %d and EP1 length %d\n\n",ep12LoopbackLen,length);
#endif
ep1RxCount += length;
/* We have recieved last byte of data */
if (ep1RxCount >= ep12LoopbackLen )
{
#ifdef DEBUG_TRACE
printf("s3c2510Usb EP1Handler RX done, total length %d\n", ep12LoopbackLen);
s3c2510UsbDump((UCHAR *)ep12LoopbackBuffer, ep12LoopbackLen);
#endif /* DEBUG_TRACE */
/* update the buffer to be transmitted on EP2 */
ep2TxBuffer = (UINT32 *)ep12LoopbackBuffer;
/* Reset the EP3 buffer and counter */
ep1RxCount = 0;
ep1RxBuffer = NULL;
/* Invoke the EP4 handler */
s3c2510UsbEP2Handler();
}
#ifdef DEBUG_TRACE
printf(" EP1 Handler exit \n");
#endif
}
/*******************************************************************************
*
* s3c2510UsbEP2Handler - Endpoint 2 interrupt handler.
*
* RETURNS : N/A
*/
void s3c2510UsbEP2Handler(void)
{
UINT32 length;
#if ! GDMA_Transfer_Mode
UINT32 i = 0;
#endif
#ifdef DEBUG_TRACE
printf(" EP2 Handler entry \n");
#endif
if ((!ep2TxBuffer) || (!ep12LoopbackLen))
{
#ifdef DEBUG_TRACE
printf("s3c2510Usb Error: no EP2 data\n");
#endif
return;
}
if (!(*S3C2510_USBCSR(2) & S3C2510_USBEP0CSR_USBINRDY))
{
length = ep12LoopbackLen - ep2TxCount;
if (length > USB_EP2_MAXP_SIZE)
{
length = USB_EP2_MAXP_SIZE;
}
*S3C2510_USBWCEP(2) = length << S3C2510_USBWCEP_CPUWRTCNT_SHIFT;
if (length > 0)
{
#ifdef DEBUG_TRACE
printf("write %d bytes\n", (int)length);
#endif /* DEBUG_TRACE */
#if GDMA_Transfer_Mode /* If to use DMA */
/* Update the source address for DMA transfer */
*S3C2510_DSAR(GDMA_USB_IN)=(UINT)ep2TxBuffer;
/* update the destination address */
*S3C2510_DDAR(GDMA_USB_IN)=(UINT)S3C2510_USBFIFO(2);
/* update the transfer count for DMA */
*S3C2510_DTCR(GDMA_USB_IN) =length;
/* enable the DMA run register */
*S3C2510_DRER(GDMA_USB_IN) |= S3C2510_DCON_RE;
#if 0
/* update the length and tx buffer */
for (i=0; i<length; i+=4)
{
*ep2TxBuffer++;
ep2TxCount += 4;
}
#else
ep2TxBuffer += length/4;
ep2TxCount += length;
#endif
#else
for (i=0; i<length; i+=4)
{
*S3C2510_USBFIFO(2) = *ep2TxBuffer++;
ep2TxCount += 4;
}
#endif
}
if (ep2TxCount >= ep12LoopbackLen)
{
/* last short packet..note it will be last only */
if (length < USB_EP2_MAXP_SIZE)
{
*S3C2510_USBCSR(2) |= S3C2510_USBEPCSR_IINRDY;
}
/* reset the tx stuff */
ep2TxCount = 0;
ep2TxBuffer = NULL;
/* get ready for future loopbacks */
ep12LoopbackLen = 0;
memset(ep12LoopbackBuffer,0,sizeof(ep12LoopbackBuffer));
ep1RxBuffer = (UINT32 *)ep12LoopbackBuffer;
#ifdef DEBUG_TRACE
printf("EP2 TX done\n");
#endif /* DEBUG_TRACE */
}
}
#ifdef DEBUG_TRACE
printf(" EP2 Handler exit \n");
#endif
}
/*******************************************************************************
*
* s3c2510UsbEP3Handler - Endpoint 3 interrupt handler.
*
* RETURNS : N/A
*/
void s3c2510UsbEP3Handler(void)
{
UINT32 length = 0;
#if ! GDMA_Transfer_Mode
UINT32 i = 0;
#endif
#ifdef DEBUG_TRACE
printf(" EP3 Handler entry \n");
#endif
/* Check if data is available in EP3 FIFO */
if(((*S3C2510_USBCSR(3))& S3C2510_USBEPCSR_OORDY) != S3C2510_USBEPCSR_OORDY)
{
/* log the meassage no data available on EP3 */
#ifdef DEBUG_TRACE
printf("no data available on EP3 \n");
#endif
return;
}
/* we are not ready to recieve as previous buffer is not loopbacked yet */
if(ep3RxBuffer == NULL)
{
/* log the meassage ep3RxBuffer NULL */
printf("ep3RxBuffer NULL \n");
return;
}
/* Update the transfer length by shifting the write count by */
length = *S3C2510_USBWCEP(3);
length = length >> S3C2510_USBWCEP_WRTCNT_SHIFT;
/* if to use DMA for tarnsfers */
#if GDMA_Transfer_Mode
/* Update the source address for DMA transfer */
*S3C2510_DSAR(GDMA_USB_OUT)=(UINT)S3C2510_USBFIFO(3);
/* update the destination address */
*S3C2510_DDAR(GDMA_USB_OUT)=(UINT)ep3RxBuffer;
/* update the transfer count for DMA */
*S3C2510_DTCR(GDMA_USB_OUT) = length;
/* enable the DMA run register */
*S3C2510_DRER(GDMA_USB_OUT) |= S3C2510_DCON_RE;
#if 0
/* update the Rx buffer */
for(i=0; i < length/4; i++)
{
ep3RxBuffer++;
}
#else
ep3RxBuffer += length/4;
#endif
#else
/* tarnsfer the data from EP3 FIFO into ep3RxBuffer buffer */
for(i=0; i < length/4; i++)
{
*ep3RxBuffer = *S3C2510_USBFIFO(3);
ep3RxBuffer++;
}
/* transfer last non-word bytes of data */
if (length % 4)
{
*ep3RxBuffer = *S3C2510_USBFIFO(3);
}
#endif
/* First packet */
if (ep3RxCount == 0)
{
#if GDMA_Transfer_Mode
/* Wait until counter equals 0 */
while(*(S3C2510_DTCR(GDMA_USB_OUT)) & 0x00ffffff);
#endif
/* First two bytes gives the length of loopback data + this len header */
ep34LoopbackLen = *((UINT16 *)ep34LoopbackBuffer);
}
#ifdef DEBUG_TRACE
printf("ep34LoopbackLen is %d and EP3 length %d\n\n",ep34LoopbackLen,length);
#endif
ep3RxCount += length;
/* We have recieved last byte of data */
if (ep3RxCount >= ep34LoopbackLen )
{
#ifdef DEBUG_TRACE
printf("s3c2510Usb EP3Handler RX done, total length %d\n", ep3RxCount);
s3c2510UsbDump((UCHAR *)ep34LoopbackBuffer, ep3RxCount);
#endif /* DEBUG_TRACE */
/* update the buffer to be transmitted on EP4 */
ep4TxBuffer = (UINT32 *)ep34LoopbackBuffer;
/* Reset the EP3 buffer and counter */
ep3RxCount = 0;
ep3RxBuffer = NULL;
/* Invoke the EP4 handler */
s3c2510UsbEP4Handler();
}
#ifdef DEBUG_TRACE
printf(" EP3 Handler exit \n");
#endif
}
/*******************************************************************************
*
* s3c2510UsbEP4Handler - Endpoint 4 interrupt handler.
*
* RETURNS : N/A
*/
void s3c2510UsbEP4Handler(void)
{
UINT32 length;
#if ! GDMA_Transfer_Mode
UINT32 i = 0;
#endif
#ifdef DEBUG_TRACE
printf(" EP4 Handler entry \n");
#endif
if ((!ep4TxBuffer) || (!ep34LoopbackLen))
{
#ifdef DEBUG_TRACE
printf("s3c2510Usb Error: no EP4 data\n");
#endif
return;
}
if (!(*S3C2510_USBCSR(4) & S3C2510_USBEP0CSR_USBINRDY))
{
length = ep34LoopbackLen - ep4TxCount;
if (length > USB_EP4_MAXP_SIZE)
{
length = USB_EP4_MAXP_SIZE;
}
*S3C2510_USBWCEP(4) = length << S3C2510_USBWCEP_CPUWRTCNT_SHIFT;
if (length > 0)
{
#ifdef DEBUG_TRACE
printf("write %d bytes\n", (int)length);
#endif /* DEBUG_TRACE */
#if GDMA_Transfer_Mode /* If to use DMA */
/* Update the source address for DMA transfer */
*S3C2510_DSAR(GDMA_USB_IN)=(UINT)ep4TxBuffer;
/* update the destination address */
*S3C2510_DDAR(GDMA_USB_IN)=(UINT)S3C2510_USBFIFO(4);
/* update the transfer count for DMA */
*S3C2510_DTCR(GDMA_USB_IN) =length;
/* enable the DMA run register */
*S3C2510_DRER(GDMA_USB_IN) |= S3C2510_DCON_RE;
#if 0
/* update the length and tx buffer */
for (i=0; i<length; i+=4)
{
*ep4TxBuffer++;
ep4TxCount += 4;
}
#else
ep4TxBuffer += length/4;
ep4TxCount += length;
#endif
#else
for (i=0; i<length; i+=4)
{
*S3C2510_USBFIFO(4) = *ep4TxBuffer++;
ep4TxCount += 4;
}
#endif
}
if (ep4TxCount >= ep34LoopbackLen)
{
/* last short packet..note it will be last only */
if (length < USB_EP4_MAXP_SIZE)
{
*S3C2510_USBCSR(4) |= S3C2510_USBEPCSR_IINRDY;
}
/* reset the tx stuff */
ep4TxCount = 0;
ep4TxBuffer = NULL;
/* get ready for future loopbacks */
ep34LoopbackLen = 0;
memset(ep34LoopbackBuffer,0,sizeof(ep34LoopbackBuffer));
ep3RxBuffer = (UINT32 *)ep34LoopbackBuffer;
#ifdef DEBUG_TRACE
printf("EP4 TX done\n");
#endif /* DEBUG_TRACE */
}
}
#ifdef DEBUG_TRACE
printf(" EP4 Handler exit \n");
#endif
}
#ifdef DEBUG_TRACE
/*******************************************************************************
*
* s3c2510UsbDump - dump buffer.
*
* RETURNS : N/A
*/
void s3c2510UsbDump(UCHAR *p, ULONG l)
{
ULONG i;
while (l)
{
printf("%08X: ", (UINT32)p);
for (i=0; i<16; i++)
{
printf("%02X ", *p++);
if (--l == 0)
{
break;
}
}
printf("\n");
}
}
#endif /* DEBUG_TRACE */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -