📄 usb.c
字号:
#include "..\inc\44b.h"
#include "..\inc\44blib.h"
#include "..\inc\def.h"
#include "..\inc\option.h"
#include "..\inc\usb.h"
#include <stdarg.h>
#include <string.h>
#include <stdio.h>
//static NU_HISR usb_comm_hisr;
//static NU_TASK task_usbcomm;
//NU_EVENT_GROUP eg_usbcomm;
int usboperation=0;
static U32 verbose = 0;
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
0x00, 0x01, // bcdUSB
0x00, // bDeviceClass
0x00, // bDeviceSubClass
0x00, // bDeviceProtocol
0x10, // bMaxPacketSize0
0x71, 0x04, // idVendor 0x5e, 0x04,
0x22, 0x02, // idProduct 0x0a, 0x93,
0x00, 0x01, // bcdDevice
0x01, // iManufacturer
0x02, // iProduct
0x00, // iSerial Number
0x01 // bNumConfigurations
};
static const U8 ucConfigurationDescriptor[] =
{
//
// The configuration descriptor structure.
//
0x09, // bLength
0x02, // bDescriptorType
0x20, // wTotalLength
0x00, // bCorrection
0x01, // bNumInterfaces
0x01, // bConfigurationValue
0x00, // iConfiguration
0x80, // bmAttributes 0x40
0x32, // MaxPower 0x00
//
// The interface descriptor structure.
//
0x09, // bLength
0x04, // bDescriptorType
0x00, // bInterfaceNumber
0x00, // bAlternateSetting
0x02, // bNumEndpoints
0x00, //00 // bInterfaceClass
0x00, // bInterfaceSubClass
0x00, //00 // bInterfaceProtocol
0x00, // iInterface
//
// The endpoint descriptor structure.
//
0x07, // bLength
0x05, // bDescriptorType
0x82, // bEndpointAddress
0x02, // bmAttributes
0x40, 0x00, // wMaxPacketSize
0x00, // bInterval
//
// The endpoint descriptor structure.
//
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[] =
{
0x26, // bLength
0x03, // bDescriptorType
'S', 0x00, // wString[]
'W', 0x00,
'4', 0x00,
'4', 0x00,
'B', 0x00,
'0', 0x00,
' ', 0x00,
'B', 0x00,
'O', 0x00,
'A', 0x00,
'R', 0x00,
'D', 0x00,
' ', 0x00,
' ', 0x00,
' ', 0x00,
' ', 0x00,
' ', 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[] =
{
0x52, // bLength
0x03, // bDescriptorType
'S', 0x00, // wString[]
'W', 0x00,
'4', 0x00,
'4', 0x00,
'B', 0x00,
'0', 0x00,
'-', 0x00,
'P', 0x00,
'D', 0x00,
'I', 0x00,
'U', 0x00,
'S', 0x00,
'B', 0x00,
'D', 0x00,
'1', 0x00,
'2', 0x00,
' ', 0x00,
'D', 0x00,
'E', 0x00,
'V', 0x00,
'I', 0x00,
'C', 0x00,
'E', 0x00,
'S', 0x00,
' ', 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;
memset(&sUSB, 0 , sizeof(sUSB));
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);
}
void USBDisable(void)
{
rINTMSK =BIT_EINT0;
}
//****************************************************************************
//
// 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 < 24; 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 < 24; delay++)
{
}
}
//****************************************************************************
//
// USBInit configures the PDIUSBD12 device.
//
//****************************************************************************
void USBON(void)
{
int saveSyscfg;
saveSyscfg=rSYSCFG;
// rNCACHBE1=( (0x6000000>>12)<<16 )|(0x4000000>>12);
// rSYSCFG=SYSCFG_8KBn;
USBInitStruct();
USBWriteCommand(USB_COMMAND_SEND_RESUME);
Delay(10000);
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.
//
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -