📄 usb.c
字号:
void OTGDEV_InitCore(void)
{
Outp32(GAHBCFG, PTXFE_HALF|NPTXFE_HALF|MODE_SLAVE|BURST_SINGLE|GBL_INT_UNMASK);
Outp32(GUSBCFG, 0<<15 // PHY Low Power Clock sel
|1<<14 // Non-Periodic TxFIFO Rewind Enable
|0x5<<10 // Turnaround time
|0<<9|0<<8 // [0:HNP disable, 1:HNP enable][ 0:SRP disable, 1:SRP enable] H1= 1,1
|0<<7 // Ulpi DDR sel
|0<<6 // 0: high speed utmi+, 1: full speed serial
|0<<4 // 0: utmi+, 1:ulpi
|1<<3 // phy i/f 0:8bit, 1:16bit
|0x7<<0 // HS/FS Timeout*
);
}
//////////
// Function Name : OTGDEV_CheckCurrentMode
// Function Desctiption : This function checks the current mode.
// Input : pucMode, current mode(device or host)
// Output : NONE
// Version :
void OTGDEV_CheckCurrentMode(UINT8 *pucMode)
{
volatile UINT32 uTemp;
//uTemp = Inp32(GINTSTS);
Inp32(GINTSTS, uTemp);
*pucMode = uTemp & 0x1;
}
//////////
// Function Name : OTGDEV_SetSoftDisconnect
// Function Desctiption : This function puts the OTG device core in the disconnected state.
// Input : NONE
// Output : NONE
// Version :
void OTGDEV_SetSoftDisconnect(void)
{
volatile UINT32 uTemp;
//uTemp = Inp32(DCTL);
Inp32(DCTL, uTemp);
uTemp |= SOFT_DISCONNECT;
Outp32(DCTL, uTemp);
}
//////////
// Function Name : OTGDEV_ClearSoftDisconnect
// Function Desctiption : This function makes the OTG device core to exit from the disconnected state.
// Input : NONE
// Output : NONE
// Version :
void OTGDEV_ClearSoftDisconnect(void)
{
volatile UINT32 uTemp;
//uTemp = Inp32(DCTL);
Inp32(DCTL, uTemp);
uTemp = uTemp & ~SOFT_DISCONNECT;
Outp32(DCTL, uTemp);
}
//////////
// Function Name : OTGDEV_InitDevice
// Function Desctiption : This function configures OTG Core to initial settings of device mode.
// Input : NONE
// Output : NONE
// Version :
void OTGDEV_InitDevice(void)
{
Outp32(DCFG, 1<<18|oOtgDev.m_eSpeed<<0); // [][1: full speed(30Mhz) 0:high speed]
Outp32(GINTMSK, INT_RESUME|INT_OUT_EP|INT_IN_EP|INT_ENUMDONE|INT_RESET
|INT_SUSPEND|INT_RX_FIFO_NOT_EMPTY); //gint unmask
}
//////////
// Function Name : OTGDEV_SetAllOutEpNak
// Function Desctiption : This function sets NAK bit of all EPs.
// Input : NONE
// Output : NONE
// Version :
void OTGDEV_SetAllOutEpNak(void)
{
UINT8 i;
volatile UINT32 uTemp;
for(i=0;i<16;i++)
{
//uTemp = Inp32(DOEPCTL0+0x20*i);
Inp32(DOEPCTL0+0x20*i, uTemp);
uTemp |= DEPCTL_SNAK;
Outp32(DOEPCTL0+0x20*i, uTemp);
}
}
//////////
// Function Name : OTGDEV_ClearAllOutEpNak
// Function Desctiption : This function clears NAK bit of all EPs.
// Input : NONE
// Output : NONE
// Version :
void OTGDEV_ClearAllOutEpNak(void)
{
UINT8 i;
volatile UINT32 uTemp;
for(i=0;i<16;i++)
{
//uTemp = Inp32(DOEPCTL0+0x20*i);
Inp32(DOEPCTL0+0x20*i, uTemp);
uTemp |= DEPCTL_CNAK;
Outp32(DOEPCTL0+0x20*i, uTemp);
}
}
//////////
// Function Name : OTGDEV_SetMaxPktSizes
// Function Desctiption : This function sets the max packet sizes of USB transfer types according to the speed.
// Input : eSpeed, usb speed(high or full)
// Output : NONE
// Version :
void OTGDEV_SetMaxPktSizes(USB_SPEED eSpeed)
{
if (eSpeed == USB_HIGH)
{
oOtgDev.m_eSpeed = USB_HIGH;
oOtgDev.m_uControlEPMaxPktSize = HIGH_SPEED_CONTROL_PKT_SIZE;
oOtgDev.m_uBulkInEPMaxPktSize = HIGH_SPEED_BULK_PKT_SIZE;
oOtgDev.m_uBulkOutEPMaxPktSize = HIGH_SPEED_BULK_PKT_SIZE;
}
else
{
oOtgDev.m_eSpeed = USB_FULL;
oOtgDev.m_uControlEPMaxPktSize = FULL_SPEED_CONTROL_PKT_SIZE;
oOtgDev.m_uBulkInEPMaxPktSize = FULL_SPEED_BULK_PKT_SIZE;
oOtgDev.m_uBulkOutEPMaxPktSize = FULL_SPEED_BULK_PKT_SIZE;
}
}
//////////
// Function Name : OTGDEV_SetEndpoint
// Function Desctiption : This function sets the endpoint-specific CSRs.
// Input : NONE
// Output : NONE
// Version :
void OTGDEV_SetEndpoint(void)
{
// Unmask DAINT source
Outp32(DIEPINT0, 0xff);
Outp32(DOEPINT0, 0xff);
Outp32(bulkIn_DIEPINT, 0xff);
Outp32(bulkOut_DOEPINT, 0xff);
// Init For Ep0
if(oOtgDev.m_eSpeed == USB_HIGH)
{
Outp32(DIEPCTL0, (1u<<31)|((1<<26)|(BULK_IN_EP<<11)|(0<<0))); //MPS:64bytes
}
else
{
Outp32(DIEPCTL0, (1u<<31)|((1<<26)|(BULK_IN_EP<<11)|(3<<0))); //MPS:8bytes
}
OTGDEV_SetInEpXferSize(EP_TYPE_CONTROL, 1, oOtgDev.m_uControlEPMaxPktSize);
OTGDEV_SetOutEpXferSize(EP_TYPE_CONTROL, 1, oOtgDev.m_uControlEPMaxPktSize);
Outp32(DOEPCTL0, (0u<<31)|(1<<26)); //ep0 enable, clear nak
}
//////////
// Function Name : OTGDEV_SetDescriptorTable
// Function Desctiption : This function sets the standard descriptors.
// Input : NONE
// Output : NONE
// Version :
void OTGDEV_SetDescriptorTable(void)
{
// Standard device descriptor
oOtgDev.m_oDesc.oDescDevice.bLength=DEVICE_DESC_SIZE; //0x12
oOtgDev.m_oDesc.oDescDevice.bDescriptorType=DEVICE_DESCRIPTOR;
oOtgDev.m_oDesc.oDescDevice.bDeviceClass=0xFF; // 0x0
oOtgDev.m_oDesc.oDescDevice.bDeviceSubClass=0x0;
oOtgDev.m_oDesc.oDescDevice.bDeviceProtocol=0x0;
oOtgDev.m_oDesc.oDescDevice.bMaxPacketSize0=oOtgDev.m_uControlEPMaxPktSize;
oOtgDev.m_oDesc.oDescDevice.idVendorL=0xE8; //0x45;
oOtgDev.m_oDesc.oDescDevice.idVendorH=0x04; //0x53;
oOtgDev.m_oDesc.oDescDevice.idProductL=0x00; //0x34;
oOtgDev.m_oDesc.oDescDevice.idProductH=0x64; //0x12;
oOtgDev.m_oDesc.oDescDevice.bcdDeviceL=0x00;
oOtgDev.m_oDesc.oDescDevice.bcdDeviceH=0x01;
oOtgDev.m_oDesc.oDescDevice.iManufacturer=0x1; // index of string descriptor
oOtgDev.m_oDesc.oDescDevice.iProduct=0x2; // index of string descriptor
oOtgDev.m_oDesc.oDescDevice.iSerialNumber=0x0;
oOtgDev.m_oDesc.oDescDevice.bNumConfigurations=0x1;
if (oOtgDev.m_eSpeed == USB_FULL) {
oOtgDev.m_oDesc.oDescDevice.bcdUSBL=0x10;
oOtgDev.m_oDesc.oDescDevice.bcdUSBH=0x01; // Ver 1.10
}
else {
oOtgDev.m_oDesc.oDescDevice.bcdUSBL=0x00;
oOtgDev.m_oDesc.oDescDevice.bcdUSBH=0x02; // Ver 2.0
}
// Standard configuration descriptor
oOtgDev.m_oDesc.oDescConfig.bLength=CONFIG_DESC_SIZE; // 0x9 bytes
oOtgDev.m_oDesc.oDescConfig.bDescriptorType=CONFIGURATION_DESCRIPTOR;
oOtgDev.m_oDesc.oDescConfig.wTotalLengthL=CONFIG_DESC_TOTAL_SIZE;
oOtgDev.m_oDesc.oDescConfig.wTotalLengthH=0;
oOtgDev.m_oDesc.oDescConfig.bNumInterfaces=1;
// dbg descConf.bConfigurationValue=2; // why 2? There's no reason.
oOtgDev.m_oDesc.oDescConfig.bConfigurationValue=1;
oOtgDev.m_oDesc.oDescConfig.iConfiguration=0;
oOtgDev.m_oDesc.oDescConfig.bmAttributes=CONF_ATTR_DEFAULT|CONF_ATTR_SELFPOWERED; // bus powered only.
oOtgDev.m_oDesc.oDescConfig.maxPower=25; // draws 50mA current from the USB bus.
// Standard interface descriptor
oOtgDev.m_oDesc.oDescInterface.bLength=INTERFACE_DESC_SIZE; // 9
oOtgDev.m_oDesc.oDescInterface.bDescriptorType=INTERFACE_DESCRIPTOR;
oOtgDev.m_oDesc.oDescInterface.bInterfaceNumber=0x0;
oOtgDev.m_oDesc.oDescInterface.bAlternateSetting=0x0; // ?
oOtgDev.m_oDesc.oDescInterface.bNumEndpoints = 2; // # of endpoints except EP0
oOtgDev.m_oDesc.oDescInterface.bInterfaceClass=0xff; // 0x0 ?
oOtgDev.m_oDesc.oDescInterface.bInterfaceSubClass=0x0;
oOtgDev.m_oDesc.oDescInterface.bInterfaceProtocol=0x0;
oOtgDev.m_oDesc.oDescInterface.iInterface=0x0;
// Standard endpoint0 descriptor
oOtgDev.m_oDesc.oDescEndpt1.bLength=ENDPOINT_DESC_SIZE;
oOtgDev.m_oDesc.oDescEndpt1.bDescriptorType=ENDPOINT_DESCRIPTOR;
oOtgDev.m_oDesc.oDescEndpt1.bEndpointAddress=BULK_IN_EP|EP_ADDR_IN; // 2400Xendpoint 1 is IN endpoint.
oOtgDev.m_oDesc.oDescEndpt1.bmAttributes=EP_ATTR_BULK;
oOtgDev.m_oDesc.oDescEndpt1.wMaxPacketSizeL=(UINT8)oOtgDev.m_uBulkInEPMaxPktSize; // 64
oOtgDev.m_oDesc.oDescEndpt1.wMaxPacketSizeH=(UINT8)(oOtgDev.m_uBulkInEPMaxPktSize>>8);
oOtgDev.m_oDesc.oDescEndpt1.bInterval=0x0; // not used
// Standard endpoint1 descriptor
oOtgDev.m_oDesc.oDescEndpt2.bLength=ENDPOINT_DESC_SIZE;
oOtgDev.m_oDesc.oDescEndpt2.bDescriptorType=ENDPOINT_DESCRIPTOR;
oOtgDev.m_oDesc.oDescEndpt2.bEndpointAddress=BULK_OUT_EP|EP_ADDR_OUT; // 2400X endpoint 3 is OUT endpoint.
oOtgDev.m_oDesc.oDescEndpt2.bmAttributes=EP_ATTR_BULK;
oOtgDev.m_oDesc.oDescEndpt2.wMaxPacketSizeL=(UINT8)oOtgDev.m_uBulkOutEPMaxPktSize; // 64
oOtgDev.m_oDesc.oDescEndpt2.wMaxPacketSizeH=(UINT8)(oOtgDev.m_uBulkOutEPMaxPktSize>>8);
oOtgDev.m_oDesc.oDescEndpt2.bInterval=0x0; // not used
}
//////////
// Function Name : OTGDEV_CheckEnumeratedSpeed
// Function Desctiption : This function checks the current usb speed.
// Input : eSpeed, usb speed(high or full)
// Output : NONE
// Version :
void OTGDEV_CheckEnumeratedSpeed(USB_SPEED *eSpeed)
{
volatile UINT32 uDStatus;
//uDStatus = Inp32(DSTS); // System status read
Inp32(DSTS, uDStatus); // System status read
*eSpeed = (uDStatus&0x6) >>1;
}
//////////
// Function Name : OTGDEV_SetInEpXferSize
// Function Desctiption : This function sets DIEPTSIZn CSR according to input parameters.
// Input : eType, transfer type
// uPktCnt, packet count to transfer
// uXferSize, transfer size
// Output : NONE
// Version :
void OTGDEV_SetInEpXferSize(EP_TYPE eType, UINT32 uPktCnt, UINT32 uXferSize)
{
if(eType == EP_TYPE_CONTROL)
{
Outp32(DIEPTSIZ0, (uPktCnt<<19)|(uXferSize<<0));
}
else if(eType == EP_TYPE_BULK)
{
Outp32(bulkIn_DIEPTSIZ, (1<<29)|(uPktCnt<<19)|(uXferSize<<0));
}
}
//////////
// Function Name : OTGDEV_SetOutEpXferSize
// Function Desctiption : This function sets DOEPTSIZn CSR according to input parameters.
// Input : eType, transfer type
// uPktCnt, packet count to transfer
// uXferSize, transfer size
// Output : NONE
// Version :
void OTGDEV_SetOutEpXferSize(EP_TYPE eType, UINT32 uPktCnt, UINT32 uXferSize)
{
if(eType == EP_TYPE_CONTROL)
{
Outp32(DOEPTSIZ0, (1<<29)|(uPktCnt<<19)|(uXferSize<<0));
}
else if(eType == EP_TYPE_BULK)
{
Outp32(bulkOut_DOEPTSIZ, (uPktCnt<<19)|(uXferSize<<0));
}
}
//////////
// Function Name : OTGDEV_WrPktEp0
// Function Desctiption : This function reads data from the buffer and writes the data on EP0 FIFO.
// Input : buf, address of the data buffer to write on Control EP FIFO
// num, size of the data to write on Control EP FIFO(byte count)
// Output : NONE
// Version :
void OTGDEV_WrPktEp0(UINT8 *buf, int num)
{
int i;
volatile UINT32 Wr_Data=0;
for(i=0;i<num;i+=4)
{
Wr_Data = ((*(buf+3))<<24)|((*(buf+2))<<16)|((*(buf+1))<<8)|*buf;
Outp32(control_EP_FIFO, Wr_Data);
buf += 4;
}
}
//////////
// Function Name : OTGDEV_PrintEp0Pkt
// Function Desctiption : This function reads data from the buffer and displays the data.
// Input : pt, address of the data buffer to read
// count, size of the data to read(byte count)
// Output : NONE
// Version :
void OTGDEV_PrintEp0Pkt(UINT8 *pt, UINT8 count)
{
int i;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -