⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 usb.c

📁 EV440B实验源码
💻 C
📖 第 1 页 / 共 3 页
字号:
#include "..\inc\44b.h"
#include "..\inc\44blib.h"
#include "..\inc\def.h"
#include "..\inc\option.h"
#include "..\inc\usb.h"

//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
    0x00, 0x01,                         // bcdUSB
    0x00,                               // bDeviceClass
    0x00,                               // bDeviceSubClass
    0x00,                               // bDeviceProtocol
    0x10,                               // bMaxPacketSize0
    0x5e, 0x04,                         // idVendor
    0x0a, 0x93,                         // idProduct
    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
    0x40,                               // bmAttributes
    0x00,                               // MaxPower

    //
    // The interface descriptor structure.
    //
    0x09,                               // bLength
    0x04,                               // bDescriptorType
    0x00,                               // bInterfaceNumber
    0x00,                               // bAlternateSetting
    0x02,                               // bNumEndpoints
    0x00,                               // bInterfaceClass
    0x00,                               // bInterfaceSubClass
    0x00,                               // 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
    'M', 0x00,                          // wString[]
    'I', 0x00,
    'C', 0x00,
    'E', 0x00,
    'T', 0x00,
    'E', 0x00,
    'K', 0x00,
    ' ', 0x00,
    'T', 0x00,
    'e', 0x00,
    'c', 0x00,
    'h', 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[] =
{
    0x52,                               // bLength
    0x03,                               // bDescriptorType
    'M', 0x00,                          // wString[]
    'I', 0x00,
    'C', 0x00,
    'E', 0x00,
    'T', 0x00,
    'E', 0x00,
    'K', 0x00,
    ' ', 0x00,
    'T', 0x00,
    'e', 0x00,
    'c', 0x00,
    'h', 0x00,
    ' ', 0x00,
    'E', 0x00,
    'V', 0x00,
    '4', 0x00,
    '4', 0x00,
    'B', 0x00,
    '0', 0x00,
    '-', 0x00,
    'I', 0x00,
    'I', 0x00,
    ' ', 0x00,
    ' ', 0x00,
    'B', 0x00,
    'o', 0x00,
    'a', 0x00,
    'r', 0x00,
    'd', 0x00,
    ' ', 0x00,
    ' ', 0x00,
    ' ', 0x00,
    ' ', 0x00,
    ' ', 0x00,
    ' ', 0x00,
    ' ', 0x00,
    ' ', 0x00,
    ' ', 0x00,
    ' ', 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;
    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_EINT2=(int)USBISR;
 	rINTMSK =~(BIT_GLOBAL|BIT_EINT2);
}

void USBDisable(void)
{
	 rINTMSK =BIT_EINT2;
}

//****************************************************************************
//
// 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(100);
    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_4M);
    Uart_Printf("USB ON\n");
    Uart_Printf("Your PC will add the new USB\n");
    Uart_Printf("Press any key ,close USB and exit\n");
    USBEnable();
//    do{
//    	USBISRPOLLING(0);
//    }
    while(!Uart_GetKey());
    USBOFF();
    rSYSCFG=saveSyscfg;
}

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_4M);
 }

//****************************************************************************
//
// USBISR is the interrupt handler routine for the USB.
//
//****************************************************************************
void __irq USBISR(void)
{
    unsigned long ulIntStatus, ulTransactionStatus, ulLength;
    U8 *pucChar;
    
    // Read the PDIUSBD12 interrupt register.
    rI_ISPC=BIT_EINT2;	//clear pending_bit

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -