📄 usb.c
字号:
#include "44b.h"
#include "44blib.h"
#include "option.h"
#include "def.h"
#include "usb.h"
static U32 UsbBulkOutCnt=0;
static U32 Save_rINTMSK;
//static NU_HISR usb_comm_hisr;
//static NU_TASK task_usbcomm;
//NU_EVENT_GROUP eg_usbcomm;
static U8 usbbuffer0[256];
static U8 usbbuffer1[256];
static U8 usbbuffer2[256];
//void USBISRPOLLING(int number);
//****************************************************************************
//
// This structure defines the setup packet received from the host via the
// control out endpoint. This sturcture is padded at the end to the maximum
// control endpoint transfer size.
//
//****************************************************************************
typedef struct
{
U8 bmRequestType;
U8 bRequest;
U16 wValue;
U16 wIndex;
U16 wLength;
} ControlTransfer;
//****************************************************************************
//
// This is the configuration descriptor for the digital audio player. See the
// USB specification for the definition of this descriptor.
//
//****************************************************************************
static const U8 ucDeviceDescriptor[] =
{
//
// 设备描述符
//
0x12, // bLength
0x01, // bDescriptorType
0x10, 0x01, // bcdUSB
0x00, // bDeviceClass
0x00, // bDeviceSubClass
0x00, // bDeviceProtocol
0x10, // bMaxPacketSize0
0x71, 0x04, // idVendor
0x66, 0x06, // idProduct
0x13, 0x01, // bcdDevice
0x01, // iManufacturer
0x02, // iProduct
0x00, // iSerial Number
0x01 // bNumConfigurations
};
static const U8 ucConfigurationDescriptor[] =
{
//
// 配置描述符
//
0x09, // bLength
0x02, // bDescriptorType
0x2e, // wTotalLength
0x00, // bCorrection
0x01, // bNumInterfaces
0x01, // bConfigurationValue
0x00, // iConfiguration
0x80, // bmAttributes
0x32, // MaxPower
//
// 接口描述符
//
0x09, // bLength
0x04, // bDescriptorType
0x00, // bInterfaceNumber
0x00, // bAlternateSetting
0x04, // bNumEndpoints
0x08, // bInterfaceClass
0x04, // bInterfaceSubClass
0x50, // bInterfaceProtocol
0x00, // iInterface
//
// EP1IN端点描述符
//
0x07, // bLength
0x05, // bDescriptorType
0x81, // bEndpointAddress
0x02, // bmAttributes
0x10, 0x00, // wMaxPacketSize
0x00,
//
// EP1OUT端点描述符
//
0x07, // bLength
0x05, // bDescriptorType
0x01, // bEndpointAddress
0x02, // bmAttributes
0x10, 0x00, // wMaxPacketSize
0x00,
//
// EP2IN端点描述符
//
0x07, // bLength
0x05, // bDescriptorType
0x82, // bEndpointAddress
0x02, // bmAttributes
0x40, 0x00, // wMaxPacketSize
0x00, // bInterval
//
// EP2OUT端点描述符
//
0x07, // bLength
0x05, // bDescriptorType
0x02, // bEndpointAddress
0x02, // bmAttributes
0x40, 0x00, // wMaxPacketSize
0x00 // bInterval
};
//****************************************************************************
//
// String descriptor 0 for the digital audio player. This defines the
// languages supported by the string descriptors. See the USB specification
// for the definition of this descriptor.
//
//****************************************************************************
static const U8 ucString0[] =
{
0x04, // bLength
0x03, // bDescriptorType
0x09, 0x04 // wLANGID[0] -> US English
};
//****************************************************************************
//
// String descriptor 1 for the digital audio player. This defines the
// manufacturer of the player. See the USB specification for the definition
// of this descriptor.
//
//****************************************************************************
static const U8 ucString1[] =
{
0x20, // bLength
0x03, // bDescriptorType
'U', 0x00, // wString[]
'c', 0x00,
'd', 0x00,
'r', 0x00,
'a', 0x00,
'g', 0x00,
'o', 0x00,
'n', 0x00,
' ', 0x00,
'C', 0x00,
'o', 0x00,
'.', 0x00,
',', 0x00,
'L', 0x00,
't', 0x00,
'd', 0x00,
'.', 0x00
};
//****************************************************************************
//
// String descriptor 1 for the digital audio player. This defines the product
// description of the player. See the USB specification for the definition of
// this descriptor.
//
//****************************************************************************
static const U8 ucString2[] =
{
0x26, // bLength
0x03, // bDescriptorType
'F', 0x00, // wString[]
'S', 0x00,
'4', 0x00,
'4', 0x00,
'B', 0x00,
'0', 0x00,
'X', 0x00,
' ', 0x00,
'B', 0x00,
'o', 0x00,
'a', 0x00,
'r', 0x00,
'd', 0x00
};
//****************************************************************************
//
// An array of pointers to the USB standard device request handler Functions.
//
//****************************************************************************
void (* const USBStandardDeviceRequest[])(void) =
{
USBGetStatus,
USBClearFeature,
USBReserved,
USBSetFeature,
USBReserved,
USBSetAddress,
USBGetDescriptor,
USBReserved,
USBGetConfiguration,
USBSetConfiguration,
USBGetInterface,
USBSetInterface,
USBReserved
};
//****************************************************************************
//
// The following structure contains the persistent state of the USB interface.
//
//****************************************************************************
static struct
{
// The currently selected USB configuration.
unsigned long ulConfiguration;
// The buffer of data that is being sent to the control endpoint.
const unsigned char *pucControlIn;
// The number of bytes to be sent to the control endpoint.
unsigned long ulControlInCount;
// The buffer of data that is being received from the control endpoint.
ControlTransfer sControlOut;
// The buffer of data that is being sent to the bulk endpoint.
const unsigned char *pucBulkIn;
// The number of bytes to be sent to the bulk endpoint.
unsigned long ulBulkInCount;
// The buffer of data that is being received from the bulk endpoint.
unsigned char *pucBulkOut;
// The number of bytes still to be read from the bulk endpoint.
unsigned long ulBulkOutCount;
const unsigned char *pucACKIn;
unsigned long ulACKInCount;
unsigned char *pucCommandOut;
unsigned long ulCommandOutCount;
} sUSB;
#define USB_STRUCT_INITED 0x55AAA55A
U8 USBInitStruct(void)
{
if (sUSB.ulConfiguration == USB_STRUCT_INITED)
return 0;
sUSB.ulConfiguration = USB_STRUCT_INITED;
sUSB.pucControlIn = usbbuffer0;
sUSB.ulControlInCount = 0;
sUSB.pucBulkIn = usbbuffer2;
sUSB.ulBulkInCount = 0;
sUSB.pucBulkOut = usbbuffer2;
sUSB.ulBulkOutCount = 0;
sUSB.pucACKIn = usbbuffer1;
sUSB.ulACKInCount = 0;
sUSB.pucCommandOut = usbbuffer1;
sUSB.ulCommandOutCount = 0;
return 0;
}
void USBEnable(void)
{
pISR_EINT0=(int)USBISR;
// rINTMSK=~(BIT_GLOBAL|BIT_EINT0);
Save_rINTMSK=rINTMSK;
rINTMSK =~(BIT_GLOBAL|BIT_EINT0);
}
void USBDisable(void)
{
// rINTMSK=~(BIT_GLOBAL|BIT_TIMER0);
rINTMSK =Save_rINTMSK;
}
//****************************************************************************
//
// USBInit configures the PDIUSBD12 device.
//
//****************************************************************************
void USBON(void)
{
rSYSCFG=SYSCFG_8KBn;
USBInitStruct();
USBWriteCommand(USB_COMMAND_SEND_RESUME);
Delay(1000);
USBWriteCommand(USB_COMMAND_SET_ADDRESS_ENABLE);
USBWriteData(0);
USBWriteCommand(USB_COMMAND_SET_ENDPOINT_ENABLE);
USBWriteData(0);
//
// Enable the interrupts for the bulk endpoints.
//
USBWriteCommand(USB_COMMAND_SET_DMA);
USBWriteData(USB_DMA_ENDP4_INT_ENABLE | USB_DMA_ENDP5_INT_ENABLE);
//
// Configure the PDIUSBD12 and enable the SoftConnect pull-up.
//
USBWriteCommand(USB_COMMAND_SET_MODE);
USBWriteData(USB_CONFIG1_NO_LAZY_CLOCK | USB_CONFIG1_CLOCK_RUNNING |
USB_CONFIG1_SOFT_CONNECT);
USBWriteData(USB_CONFIG2_SET_TO_ONE | USB_CONFIG2_CLOCK_12M);
USBEnable();
Uart_Printf("\nUSB Connect!");
}
void USBOFF(void)
{
USBWriteCommand(USB_COMMAND_SEND_RESUME);
Delay(1000);
USBWriteCommand(USB_COMMAND_SET_ADDRESS_ENABLE);
USBWriteData(0);
USBWriteCommand(USB_COMMAND_SET_ENDPOINT_ENABLE);
USBWriteData(0);
//
// Enable the interrupts for the bulk endpoints.
//
USBWriteCommand(USB_COMMAND_SET_DMA);
USBWriteData(USB_DMA_ENDP4_INT_ENABLE | USB_DMA_ENDP5_INT_ENABLE);
USBWriteCommand(USB_COMMAND_SET_MODE);
USBWriteData(0);
USBWriteData(USB_CONFIG2_SET_TO_ONE | USB_CONFIG2_CLOCK_12M);
rSYSCFG=CACHECFG; // Using 8KB Cache//
USBDisable();
Uart_Printf("\nUSB Disconnect!");
}
//****************************************************************************
//
// USBWriteCommand will write the specified value to the command register of
// the PDIUSBD12.
//
//****************************************************************************
void USBWriteCommand(U8 commandvalue)
{
U8 * volatile commandregister = (U8 *)(Usb_Base_Address + Usb_Command_Address);
int delay;
// Write the value to the command register.
*commandregister = commandvalue;
// Delay a bit to comply with the timing specification of the PDIUSBD12.
for(delay = 0; delay < 5; delay++) {}
}
//****************************************************************************
//
// USBWriteData will write the specified value to the data register of the
// PDIUSBD12.
//
//****************************************************************************
void USBWriteData(U8 datavalue)
{
U8 * volatile dataregister = (U8 *)(Usb_Base_Address + Usb_Data_Address);
int delay;
// Write the value to the data register.
*dataregister = datavalue;
// Delay a bit to comply with the timing specification of the PDIUSBD12.
for(delay = 0; delay < 5; delay++) {}
}
//****************************************************************************
//
// USBReadData will read a value from the data register of the PDIUSBD12.
//
//****************************************************************************
unsigned char
USBReadData(void)
{
unsigned char * volatile dataregister = (unsigned char*)(unsigned char *)(Usb_Base_Address + Usb_Data_Address);
int delay;
U8 datavalue;
// Write the value to the data register.
datavalue = * dataregister;
for(delay = 0; delay < 5; delay++) {}
return(datavalue);
}
void Usb_BusReset(void)
{
}
void Usb_Control_OUT(void)
{
unsigned long ulTransactionStatus, ulLength;
U8 *pucChar;
// Read the status of the last transaction on the control out endpoint.
USBWriteCommand(USB_COMMAND_READ_LAST_XACTION_STATUS + USB_ENDPOINT_CONTROL_OUT);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -