📄 usbdev_+
字号:
void WrPktEp0(U8 *buf, int num)
{
int i;
U16 Wr_Data=0;
if (num&0x1) num++;
for(i=0;i<num;i+=2)
{
Wr_Data=((*(buf+1))<<8)|*buf;
Outp32(EP0_FIFO, Wr_Data);
buf +=2;
}
}
void WrPktEp1(U8 *buf, int num)
{
int i;
U16 Wr_Data=0;
if (num&0x1) num++;
for(i=0;i<num;i+=2)
{
Wr_Data=((*(buf+1))<<8)|*buf;
Outp32(EP1_FIFO, Wr_Data);
buf +=2;
}
}
void PrintEp0Pkt(U8 *pt, U8 count)
{
int i;
DbgUsb(("[DBG:"));
for(i=0;i<count;i++)
DbgUsb(("%x,", pt[i]));
DbgUsb(("]"));
}
void USBDEV::RdPktEp3(U8 *buf, int num)
{
int i;
U16 Rdata;
for(i=0;i<num;i+=2)
{
Inp32(EP3_FIFO, Rdata);
buf[i] = (U8)Rdata;
buf[i+1] = (U8)(Rdata>>8);
}
m_pDownPt += num;
}
bool USBDEV::IsEnumerationDone(void)
{
if (m_uEnumerationDone == 0)
return false;
else
return true;
}
void USBDEV::PrepareEp1Fifo(U32 BaseAddr)
{
int i;
U32 in_csr1;
U8* BulkInBuf = (U8*)BaseAddr;
if (m_uBulkInCount > m_uEp1MaxPktSize)
{
Outp32(INDEX_REG, EP1);
Outp32(BYTE_WRITE_CNT_REG, m_uEp1MaxPktSize);
WrPktEp1(BulkInBuf, m_uEp1MaxPktSize);
m_uBulkInAddr = BaseAddr + m_uEp1MaxPktSize;
m_uBulkInCount -= m_uEp1MaxPktSize;
}
else
{
Outp32(INDEX_REG, EP1);
Outp32(BYTE_WRITE_CNT_REG, m_uBulkInCount);
WrPktEp1(BulkInBuf, m_uBulkInCount);
m_uBulkInAddr = BaseAddr + m_uBulkInCount;
m_uBulkInCount = 0;
}
}
void USBDEV::FlushEp1Fifo(void)
{
U32 OrgValue;
Outp32(INDEX_REG, EP1);
Inp32(EP_CON_REG, OrgValue);
Outp32(EP_CON_REG, (1<<6));
Outp32(EP_CON_REG, OrgValue);
}
void USBDEV::SetMaxPktSizes(USB_SPEED eSpeed)
{
if (eSpeed == USB_HIGH)
{
m_eSpeed = USB_HIGH;
m_uEp0MaxPktSize = 64;
m_uEp1MaxPktSize = 512;
//m_uEp3MaxPktSize = 64;
m_uEp3MaxPktSize = 512;
}
else
{
m_eSpeed = USB_FULL;
m_uEp0MaxPktSize = 8;
m_uEp1MaxPktSize = 64;
m_uEp3MaxPktSize = 64;
}
// EP0 Max Packet size settings
Outp32(INDEX_REG, EP0);
Outp32(MAX_PKT_REG, m_uEp0MaxPktSize); // max packet size
// EP1 OUT Max Packet size settings
Outp32(INDEX_REG, EP1);
Outp32(MAX_PKT_REG, m_uEp1MaxPktSize); // max packet size
// EP2 IN Max Packet size settings
Outp32(INDEX_REG, EP3);
Outp32(MAX_PKT_REG, m_uEp3MaxPktSize); // max packet size
}
void USBDEV::SetOpMode(USB_OP mode)
{
U32 i;
// m_uTransferDone = 0;
m_uDownloadFileSize = 0;
m_eOpMode = mode;
}
bool USBDEV::VerifyChecksum(void)
{
U8* CalcCSPt;
U16 dnCS;
U16 checkSum;
U16 i=0;
// checksum calculation
CalcCSPt = (U8*)m_uDownloadAddress;
checkSum = 0;
// printf("Now, Checksum calculation:%9d",0);
while((U32)CalcCSPt < (m_uDownloadAddress+(m_uDownloadFileSize-8)))
{
checkSum += *CalcCSPt++;
// if((U16)(i*100/(m_uDownloadFileSize-8))%5==0)printf("\b\b\b\b\b\b\b\b%8d",i*100/(m_uDownloadFileSize-8));
i++;
}
// printf("\b\b\b\b\b\b\b\b%8d",100);
// checkSum was calculated including dnCS. So, dnCS should be subtracted.
checkSum=checkSum - *((unsigned char *)(m_uDownloadAddress+m_uDownloadFileSize-8-2))
- *( (unsigned char *)(m_uDownloadAddress+m_uDownloadFileSize-8-1) );
dnCS=*((unsigned char *)(m_uDownloadAddress+m_uDownloadFileSize-8-2))+
(*( (unsigned char *)(m_uDownloadAddress+m_uDownloadFileSize-8-1) )<<8);
if (checkSum!=dnCS)
{
printf("Checksum Error!!! MEM:%x DN:%x\n", checkSum, dnCS);
return false;
}
else
{
//Disp("\nDownload Check Sum Result is O.K.\n\n");
return true;
}
}
USBDEV::USBDEV()
{
m_poDescDevice = (USB_DEVICE_DESCRIPTOR*)malloc(sizeof (USB_DEVICE_DESCRIPTOR));
m_poStatusGet = (USB_GET_STATUS*)malloc(sizeof (USB_GET_STATUS));
m_poInterfaceGet = (USB_INTERFACE_GET*)malloc(sizeof (USB_INTERFACE_GET));
m_poDesc = (USB_DESCRIPTORS*)malloc(sizeof (USB_DESCRIPTORS));
m_poDeviceRequest = (DEVICE_REQUEST*)malloc(sizeof (DEVICE_REQUEST));
}
USBDEV::~USBDEV()
{
if (m_poDescDevice)
free(m_poDescDevice);
if (m_poStatusGet)
free(m_poStatusGet);
if (m_poInterfaceGet)
free(m_poInterfaceGet);
if (m_poDesc)
free(m_poDesc);
if (m_poDeviceRequest)
free(m_poDeviceRequest);
}
void USBDEV::Init(void)
{
m_uEnumerationDone = 0;
// SetDescriptorTable();
SetEndpoint();
m_uEp0State = EP0_STATE_INIT;
m_uEp0SubState = 0;
}
void USBDEV::SetDescriptorTable(void)
{
// Standard device descriptor
m_poDescDevice->bLength=0x12; // EP0_DEV_DESC_SIZE=0x12 bytes
m_poDescDevice->bDescriptorType=DEVICE_TYPE;
m_poDescDevice->bDeviceClass=0xFF; // 0x0
m_poDescDevice->bDeviceSubClass=0x0;
m_poDescDevice->bDeviceProtocol=0x0;
m_poDescDevice->bMaxPacketSize0=m_uEp0MaxPktSize;
m_poDescDevice->idVendorL=0x45;
m_poDescDevice->idVendorH=0x53;
m_poDescDevice->idProductL=0x34;
m_poDescDevice->idProductH=0x12;
m_poDescDevice->bcdDeviceL=0x00;
m_poDescDevice->bcdDeviceH=0x01;
m_poDescDevice->iManufacturer=0x1; // index of string descriptor
m_poDescDevice->iProduct=0x2; // index of string descriptor
m_poDescDevice->iSerialNumber=0x0;
m_poDescDevice->bNumConfigurations=0x1;
if (m_eSpeed == USB_FULL) {
m_poDescDevice->bcdUSBL=0x10;
m_poDescDevice->bcdUSBH=0x01; // Ver 1.10
}
else {
m_poDescDevice->bcdUSBL=0x00;
m_poDescDevice->bcdUSBH=0x02; // Ver 2.0
}
// Standard configuration descriptor
m_poDesc->oDescConfig.bLength=0x9;
m_poDesc->oDescConfig.bDescriptorType=CONFIGURATION_TYPE;
m_poDesc->oDescConfig.wTotalLengthL=0x20; // <cfg desc>+<if desc>+<endp0 desc>+<endp1 desc>
m_poDesc->oDescConfig.wTotalLengthH=0;
m_poDesc->oDescConfig.bNumInterfaces=1;
// dbg descConf.bConfigurationValue=2; // why 2? There's no reason.
m_poDesc->oDescConfig.bConfigurationValue=1;
m_poDesc->oDescConfig.iConfiguration=0;
m_poDesc->oDescConfig.bmAttributes=CONF_ATTR_DEFAULT; // bus powered only.
m_poDesc->oDescConfig.maxPower=25; // draws 50mA current from the USB bus.
// Standard interface descriptor
m_poDesc->oDescInterface.bLength=0x9;
m_poDesc->oDescInterface.bDescriptorType=INTERFACE_TYPE;
m_poDesc->oDescInterface.bInterfaceNumber=0x0;
m_poDesc->oDescInterface.bAlternateSetting=0x0; // ?
m_poDesc->oDescInterface.bNumEndpoints=2; // # of endpoints except EP0
m_poDesc->oDescInterface.bInterfaceClass=0xff; // 0x0 ?
m_poDesc->oDescInterface.bInterfaceSubClass=0x0;
m_poDesc->oDescInterface.bInterfaceProtocol=0x0;
m_poDesc->oDescInterface.iInterface=0x0;
// Standard endpoint0 descriptor
m_poDesc->oDescEndpt1.bLength=0x7;
m_poDesc->oDescEndpt1.bDescriptorType=ENDPOINT_TYPE;
m_poDesc->oDescEndpt1.bEndpointAddress=1|EP_ADDR_IN; // 2400Xendpoint 1 is IN endpoint.
m_poDesc->oDescEndpt1.bmAttributes=EP_ATTR_BULK;
m_poDesc->oDescEndpt1.wMaxPacketSizeL=(U8)m_uEp1MaxPktSize; // 64
m_poDesc->oDescEndpt1.wMaxPacketSizeH=(U8)(m_uEp1MaxPktSize>>8);
m_poDesc->oDescEndpt1.bInterval=0x0; // not used
// Standard endpoint1 descriptor
m_poDesc->oDescEndpt3.bLength=0x7;
m_poDesc->oDescEndpt3.bDescriptorType=ENDPOINT_TYPE;
m_poDesc->oDescEndpt3.bEndpointAddress=3|EP_ADDR_OUT; // 2400X endpoint 3 is OUT endpoint.
m_poDesc->oDescEndpt3.bmAttributes=EP_ATTR_BULK;
m_poDesc->oDescEndpt3.wMaxPacketSizeL=(U8)m_uEp3MaxPktSize; // 64
m_poDesc->oDescEndpt3.wMaxPacketSizeH=(U8)(m_uEp3MaxPktSize>>8);
m_poDesc->oDescEndpt3.bInterval=0x0; // not used
}
void USBDEV::SetEndpoint(void)
{
// *** End point information ***
// EP0: control
U16 SysStatus;
U16 Temp;
Outp32(INDEX_REG, EP0);
// For L18
Outp32(EP_DIR_REG, 0x02); // EP1=> TX, EP2=>RX , 0b=report mode[1], 15b=report mode[2], 3b~8b=ep2 delay_con
Outp32(EP_INT_EN_REG, 0x4d0f); // EP0, 1, 2 Interrupt enable, 15b=report mode[0], 3b~14b=ep0/1 delay_con
Inp32(EP_DIR_REG, Temp);
DbgUsb(("EP_DIR_REG : %x \n", Temp));
Inp32(EP_INT_EN_REG, Temp);
DbgUsb(("EP_INT_EN_REG : %x \n", Temp));
Outp32(TEST_REG, 0x0000);
//Outp32(SYS_CON_REG, 0x0283); // error interrupt enable, 16bit bus, Little format, suspend&reset enable
//Outp32(SYS_CON_REG, 0x0003); // error interrupt enable, 8bit bus, Little format, suspend&reset enable
//Outp32(SYS_CON_REG, 0x0023); // error interrupt enable, 16bit bus, Little format, suspend&reset enable
Outp32(SYS_CON_REG, 0x0123); // error interrupt enable, 16bit bus, Little format, suspend&reset enable
// Outp32(MAX_PKT_REG, MAXP_SIZE_64BYTE); // Initial矫 size?
Outp32(EP0_CON_REG, 0x0000);
// EP1 OUT Max Packet size settings
Outp32(INDEX_REG, EP1);
// Outp32(MAX_PKT_REG, 512); // max packet size 512 bytes
Outp32(EP_CON_REG, 0x0080); // dual enable
// EP2 IN Max Packet size settings
Outp32(INDEX_REG, EP3);
// Outp32(MAX_PKT_REG, 512); // max packet size 512 bytes
Outp32(EP_CON_REG, 0x0080); // dual enable
Outp32(INDEX_REG, EP0);
}
void USBDEV::HandleEvent(void)
{
U32 uStatus;
U32 Temp;
Inp32(SYS_STATUS_REG, uStatus); // System status read
DbgUsb(("SYS_STATUS_REG : %x \n", uStatus));
if (uStatus & INT_REG_VBUS)
{
Outp32(SYS_STATUS_REG, INT_REG_VBUS); // Interrupt Clear
DbgUsb(("\n [USB_Diag_Log] : INT_REG_VBUS\n"));
}
if (uStatus & 0xfc00) // Error interrupt check
{
Inp32(SYS_STATUS_REG , Temp);
printf("SYS_CONTROL_REG :%x \n",Temp);
Outp32(SYS_STATUS_REG, INT_ERR); // Interrupt Clear
DbgUsb(("\n [USB_Diag_Log] : Error_INT\n"));
}
// Which USB interrupts happen
if (uStatus & INT_REG_SUSPEND)
{
Outp32(SYS_STATUS_REG, INT_REG_SUSPEND); // Interrupt Clear
DbgUsb(("\n [USB_Diag_Log] : Suspend Mode"));
}
if (uStatus & INT_REG_RESUME)
{
Outp32(SYS_STATUS_REG, INT_REG_RESUME); // Host software send ClearPortFeature. Interrupt Clear
DbgUsb(("\n [USB_Diag_Log] : Resume Mode \n"));
}
if (uStatus & INT_REG_RESET) // Reset interrupt
{
Outp32(SYS_STATUS_REG, INT_REG_RESET); // Interrupt Clear
SetEndpoint();
m_uEp0State = EP0_STATE_INIT;
DbgUsb(("\n [USB_Diag_Log] : Reset Mode \n"));
}
if (uStatus & INT_REG_SDE) // Device Speed Detection interrupt
{
Outp32(SYS_STATUS_REG, INT_REG_SDE); // Interrupt Clear
DbgUsb(("\n [USB_Diag_Log] : Speed Detection interrupt \n"));
if (uStatus & INT_REG_HSP) // Set if Device is High speed or Full speed
{
Outp32(SYS_STATUS_REG, INT_REG_HSP); // High Speed Device Interrupt Clear?? may be not.
DbgUsb(("\n [USB_Diag_Log] : High Speed Detection\n"));
SetMaxPktSizes(USB_HIGH);
SetDescriptorTable();
}
else
{
SetMaxPktSizes(USB_FULL);
SetDescriptorTable();
}
}
U16 ep_int_status, ep_int;
Inp32(EP_STATUS_REG, ep_int_status); // EP interrrupt status read
DbgUsb(("EP_STATUS_REG : %x \n", ep_int_status));
Inp32(EP_INT_REG, ep_int);
DbgUsb(("EP_INT_REG : %x \n", ep_int));
if (ep_int & INT_REG_EP0)
{
// DbgUsb(("\n [USB_Diag_Log] : Control Transfer Interrupt \n"));
Outp32(EP_INT_REG, INT_REG_EP0); // Interrupt Clear
HandleEvent_EP0();
}
// Endpoint1 bulkIn
else if (ep_int & INT_REG_EP1)
{
Outp32(EP_INT_REG, INT_REG_EP1); // Interrupt Clear
// printf("\n [USB_Diag_Log] : Ep1 Interrupt \n");
DbgUsb(("\n [USB_Diag_Log] : Ep1 Interrupt \n"));
HandleEvent_BulkIn();
}
// Endpoint2 bulkOut
else if (ep_int & INT_REG_EP3)
{
DbgUsb(("\n [USB_Diag_Log] : Bulk Out Transfer Interrupt \n"));
Outp32(EP_INT_REG, INT_REG_EP3); // Interrupt Clear
// printf("*");
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -