139 lines
4.7 KiB
C
139 lines
4.7 KiB
C
|
|
/********************************************************************************
|
||
|
|
* Copyright (c) 2012, Beijing Tongfang Microelectroics Co., Ltd.
|
||
|
|
* All rights reserved.
|
||
|
|
* Module: USB
|
||
|
|
* Author: Yang Song
|
||
|
|
* Version: V1.0
|
||
|
|
* History:
|
||
|
|
* 2012-09-24 Original version
|
||
|
|
********************************************************************************/
|
||
|
|
|
||
|
|
#include "global.h"
|
||
|
|
|
||
|
|
static void usbSetMaxPktSize(u8 epn,u16 maxlength);
|
||
|
|
|
||
|
|
/***************************************************************************
|
||
|
|
* Function: usbEpInit
|
||
|
|
* Description: Initialize ep
|
||
|
|
* Input: NULL
|
||
|
|
* Output: NULL
|
||
|
|
* Return: NULL
|
||
|
|
* Other: NULL
|
||
|
|
**************************************************************************/
|
||
|
|
void usbEpInit(void)
|
||
|
|
{
|
||
|
|
// maxpkt=16B,clr buf 0 & 1,not complete CXSTS,CXDPKT,CXS enable
|
||
|
|
USBCECON = 0x00010C07;
|
||
|
|
|
||
|
|
if(g_bUsbDevType == USBDEVTYPE_HIDPOS)
|
||
|
|
{
|
||
|
|
// INT OUT,double buffer disable,maxpkt=64B,clr buf 0 & 1,not STALL
|
||
|
|
USBEP1CON = 0x00430600; // used for BULK OUT
|
||
|
|
// INT IN, double buffer disable,maxpkt=64B,clr buf 0 & 1,not STALL
|
||
|
|
USBEP2CON = 0x00530600; // used for BULK IN
|
||
|
|
}
|
||
|
|
else if(g_bUsbDevType == USBDEVTYPE_HIDKBD)
|
||
|
|
{
|
||
|
|
// INT OUT,double buffer disable,maxpkt=64B,clr buf 0 & 1,not STALL
|
||
|
|
USBEP1CON = 0x00430600; // used for BULK OUT
|
||
|
|
// INT IN, double buffer disable,maxpkt=64B,clr buf 0 & 1,not STALL
|
||
|
|
USBEP2CON = 0x00530600; // used for BULK IN
|
||
|
|
}
|
||
|
|
else if(g_bUsbDevType == USBDEVTYPE_VSP)
|
||
|
|
{
|
||
|
|
// BULK OUT,double buffer disable,maxpkt=64B,clr buf 0 & 1,not STALL
|
||
|
|
USBEP1CON = 0x00230600;
|
||
|
|
// BULK IN, double buffer disable,maxpkt=64B,clr buf 0 & 1,not STALL
|
||
|
|
USBEP2CON = 0x00330600;
|
||
|
|
// INT IN, double buffer disable,maxpkt=8B,clr buf 0 & 1,not STALL
|
||
|
|
USBEP3CON = 0x00500600;
|
||
|
|
}
|
||
|
|
|
||
|
|
#if 0
|
||
|
|
// BULK OUT,double buffer disable,maxpkt=64B,clr buf 0 & 1,not STALL
|
||
|
|
USBEP1CON = 0x00230600; // used for BULK OUT
|
||
|
|
// BULK IN, double buffer disable,maxpkt=64B,clr buf 0 & 1,not STALL
|
||
|
|
USBEP2CON = 0x00330600; // used for BULK IN
|
||
|
|
|
||
|
|
// INT IN, double buffer disable,maxpkt=8B,clr buf 0 & 1,not STALL
|
||
|
|
USBEP3CON = 0x00500600; // used for INTERRUPT IN
|
||
|
|
|
||
|
|
// BULK IN, double buffer disable,maxpkt=64B,clr buf 0 & 1,not STALL
|
||
|
|
USBEP4CON = 0x00330600; // used for BULK IN
|
||
|
|
// INT IN, double buffer disable,maxpkt=8B,clr buf 0 & 1,not STALL
|
||
|
|
USBEP5CON = 0x00500600; // used for INTERRUPT IN
|
||
|
|
// INT IN, double buffer disable,maxpkt=8B,clr buf 0 & 1,not STALL
|
||
|
|
USBEP6CON = 0x00500600; // used for INTERRUPT IN
|
||
|
|
// BULK OUT, double buffer disable,maxpkt=64B,clr buf 0 & 1,not STALL
|
||
|
|
USBEP7CON = 0x00230600; // used for INTERRUPT IN
|
||
|
|
// BULK IN, double buffer disable,maxpkt=64B,clr buf 0 & 1,not STALL
|
||
|
|
USBEP8CON = 0x00330600; // used for INTERRUPT IN
|
||
|
|
#endif
|
||
|
|
usbSetMaxPktSize(0,EP0PKTSIZE);
|
||
|
|
|
||
|
|
#if HID
|
||
|
|
usbSetMaxPktSize(1,HIDINTPKTSIZE);
|
||
|
|
usbSetMaxPktSize(2,HIDINTPKTSIZE);
|
||
|
|
#endif
|
||
|
|
|
||
|
|
#if DYNAMICFIFO
|
||
|
|
USBBFCON |= Bit7_En;
|
||
|
|
USBCEBF = (u32)g_abUsbEp0Buf;
|
||
|
|
|
||
|
|
USBEP1BF = (u32)g_abUsbEp1Buf;
|
||
|
|
USBEP2BF = (u32)g_abUsbEp2Buf;
|
||
|
|
USBEP3BF = (u32)g_abUsbEp3Buf;
|
||
|
|
|
||
|
|
#else
|
||
|
|
USBBFCON &= Bit7_Dis;
|
||
|
|
USBBFCON = (((u32)g_abUsbBuf) - USBFIFO_ADDR_START) >> 6; // set USB buf start address
|
||
|
|
#endif
|
||
|
|
|
||
|
|
USBDCON |= 0x00000001; // Enable EP0 interrupt
|
||
|
|
// USBEP1CON |= Bit0_En; // Enable EP1 interrupt
|
||
|
|
// USBEP2CON |= Bit0_En; // Enable EP2 interrupt
|
||
|
|
// USBEP3CON |= Bit0_En; // Enable EP3 interrupt
|
||
|
|
// USBEP4CON |= Bit0_En; // Enable EP4 interrupt
|
||
|
|
// USBDCON |= Bit16_En; // Enable RST interrupt for debugging
|
||
|
|
// USBDCON |= Bit17_En; // Enable Suspend interrupt for debugging
|
||
|
|
// USBDCON |= Bit18_En; // Enable SOF interrupt for debugging
|
||
|
|
// USBDCON = 0x7FFFF; // Enable all interrupts
|
||
|
|
// USBDCON &= Bit16_Dis; // Disable RST interrupt for debugging
|
||
|
|
// USBDCON &= Bit17_Dis; // Disable Suspend interrupt for debugging
|
||
|
|
// USBDCON &= Bit18_Dis; // Disable SOF interrupt for debugging
|
||
|
|
}
|
||
|
|
|
||
|
|
/***************************************************************************
|
||
|
|
* Function: usbSetMaxPktSize
|
||
|
|
* Description: Set all EP Max packet size
|
||
|
|
* Input: epn : the number of ep,form 0 to 8
|
||
|
|
maxlength : the max packet length, the value can be set to 16,32,64
|
||
|
|
* Output: NULL
|
||
|
|
* Return: NULL
|
||
|
|
* Other: NULL
|
||
|
|
**************************************************************************/
|
||
|
|
static void usbSetMaxPktSize(u8 epn,u16 maxlength)
|
||
|
|
{
|
||
|
|
u32 tempUSBEPCON;
|
||
|
|
|
||
|
|
tempUSBEPCON = USBEPCON(epn);
|
||
|
|
tempUSBEPCON &= 0xFFF8FFFF;
|
||
|
|
|
||
|
|
if(maxlength == 16)
|
||
|
|
{
|
||
|
|
tempUSBEPCON |= 0x00010000;
|
||
|
|
}
|
||
|
|
else if(maxlength == 64)
|
||
|
|
{
|
||
|
|
tempUSBEPCON |= 0x00030000;
|
||
|
|
}
|
||
|
|
else if(maxlength == 32)
|
||
|
|
{
|
||
|
|
tempUSBEPCON |= 0x00020000;
|
||
|
|
}
|
||
|
|
|
||
|
|
USBEPCON(epn) = tempUSBEPCON;
|
||
|
|
}
|
||
|
|
|
||
|
|
|