📄 udc_ahb.c.orig
字号:
/******************************************************************************** **** Copyright (c) 2005 ST Microelectronics **** All rights reserved **** **** Filename : udc_ahb.c **** Author : Armando Visconti **** Revision : 1.0 **** **** **** *********************************************************************************/#include "stdio.h"#include "udc_ahb.h"#include "usb20.h"#include "vic_pl190.h"#if 0#define htonl(x) (unsigned int)( \ (((x) << 24) & 0xff000000) | \ (((x) << 8) & 0x00ff0000) | \ (((x) >> 8) & 0x0000ff00) | \ (((x) >> 24) & 0x000000ff) \ )#define ntohl(x) (unsigned int)( \ (((x) << 24) & 0xff000000) | \ (((x) << 8) & 0x00ff0000) | \ (((x) >> 8) & 0x0000ff00) | \ (((x) >> 24) & 0x000000ff) \ )#define htons(x) (unsigned short)( \ (((x) << 8) & 0xff00) | \ (((x) >> 8) & 0x00ff) \ )#define ntohs(x) (unsigned short)( \ (((x) << 8) & 0xff00) | \ (((x) >> 8) & 0x00ff) \ )#endif#define min(a,b) (((a) < (b)) ? (a) : (b))//extern volatile unsigned char bUSBPortStatus;volatile unsigned char bUSBPortStatus = 0x18;#define _USB_DMA//#define _USB_BIGEND#define _USB_DMA_THSLDEN//#define _USB_DMA_BURSTEN/* * USB Standard Descriptors. * (They should better be allocated dinamically to save ROM space). */static char DevDescr[USB_DEVICE_LEN] = { USB_DEVICE_LEN, // bLength USB_DEVICE, // bDescriptorType 0x00, // bcdUSB (lo) 0x02, // bcdUSB (hi) 0x07, // bDeviceClass 0x00, // bDeviceSubClass 0x00, // bDeviceProtocol 0x40, // bMaxPacketSize0 0xe8, // idVendor (lo) 0x04, // idVendor (hi) 0x06, // idProduct (lo) 0x31, // idProduct (hi) 0x00, // bcdDevice (lo) 0x01, // bcdDevice (hi) 0x01, // iManufacturer 0x02, // iProduct 0x03, // iSerialNumber 0x01 // bNumConfigurations };static char QualifierDescr[USB_DEVICE_QUALIFIER_LEN] = { USB_DEVICE_QUALIFIER_LEN, // bLength USB_DEVICE_QUALIFIER, // bDescriptorType 0x00, // bcdUSB (lo) 0x02, // bcdUSB (hi) 0x07, // bDeviceClass 0x01, // bDeviceSubClass 0x02, // bDeviceProtocol 0x40, // bMaxPacketSize0 0x01, // bNumConfigurations 0x00 // bReserved };static char CfgDescr[USB_CONFIGURATION_LEN] = { USB_CONFIGURATION_LEN, // bLength USB_CONFIGURATION, // bDescriptorType 0x20, // wTotalLength (lo) 0x00, // wTotalLength (hi) 0x01, // bNumInterfaces 0x01, // bConfigurationValue 0x00, // iConfiguration 0xe0, // bmAttributes (Selfpowered, Remote Wakeup) 0x00 // MaxPower }; static char OtherCfgDescr[USB_OTHER_SPEED_CONFIGURATION_LEN] = { USB_OTHER_SPEED_CONFIGURATION_LEN, // bLength USB_OTHER_SPEED_CONFIGURATION, // bDescriptorType 0x20, // wTotalLength (lo) 0x00, // wTotalLength (hi) 0x01, // bNumInterfaces 0x01, // bConfigurationValue 0x00, // iConfiguration 0xc0, // bmAttributes (Selfpowered) 0x00 // MaxPower }; static char IfDescr[USB_INTERFACE_LEN] = { USB_INTERFACE_LEN, // bLength USB_INTERFACE, // bDescriptorType 0x00, // bInterfaceNumber 0x00, // bAlternateSetting 0x02, // bNumEndpoints 0x07, // bInterfaceClass 0x01, // bInterfaceSubClass 0x02, // bInterfaceProtocol 0x00 // iInterface }; static char BulkOutDescrFS[USB_ENDPOINT_LEN] = { USB_ENDPOINT_LEN, // bLength USB_ENDPOINT, // bDescriptorType 0x01, // bEndpointAddress (OUT, #1) 0x02, // bmAttributes (Bulk) 0x40, // wMaxPacketSize (lo) 0x00, // wMaxPacketSize (hi) 0x00 // bInterval }; static char BulkInDescrFS[USB_ENDPOINT_LEN] = { USB_ENDPOINT_LEN, // bLength USB_ENDPOINT, // bDescriptorType 0x82, // bEndpointAddress (IN, #2) 0x02, // bmAttributes (Bulk) 0x40, // wMaxPacketSize (lo) 0x00, // wMaxPacketSize (hi) 0x00 // bInterval };static char BulkOutDescrHS[USB_ENDPOINT_LEN] = { USB_ENDPOINT_LEN, // bLength USB_ENDPOINT, // bDescriptorType 0x01, // bEndpointAddress (OUT, #1) 0x02, // bmAttributes (Bulk) 0x00, // wMaxPacketSize (lo) 0x02, // wMaxPacketSize (hi) 0x00 // bInterval }; static char BulkInDescrHS[USB_ENDPOINT_LEN] = { USB_ENDPOINT_LEN, // bLength USB_ENDPOINT, // bDescriptorType 0x82, // bEndpointAddress (IN, #2) 0x02, // bmAttributes (Bulk) 0x00, // wMaxPacketSize (lo) 0x02, // wMaxPacketSize (hi) 0x00 // bInterval };#define USB_DEV_DESC_LANGID 0#define USB_DEV_DESC_MANUFACTURER 1#define USB_DEV_DESC_PRODUCT 2#define USB_DEV_DESC_SERIAL 3char gaubLanguageIds[] ={ 4, /* descriptor length */ USB_STRING, /* descriptor type */ (LANGID & 0xFF), /* language id LSB */ (LANGID >> 8) /* language id MSB */};/****** NOTE: Strings are in Unicode! ******/char gaubManufacturerString[] ={ 58, /* descriptor length */ USB_STRING, /* descriptor type */ 'S',0, 'A',0, 'M',0, 'S',0, 'U',0, 'N',0, 'G',0, ' ',0, 'E',0, 'L',0, 'E',0, 'C',0, 'T',0, 'R',0, 'O',0, 'N',0, 'I',0, 'C',0, 'S',0, ' ',0, 'C',0, 'O',0, '.',0, ',',0, ' ',0, 'L',0, 'T',0, 'D',0};char gaubProductString[] ={ 46, /* descriptor length */ USB_STRING, /* descriptor type */ 'S',0, 'a',0, 'm',0, 's',0, 'u',0, 'n',0, 'g',0, ' ',0, 'M',0, 'L',0, '-',0, '7',0, '3',0, '0',0, '0',0, ' ',0, 'S',0, 'e',0, 'r',0, 'i',0, 'e',0, 's',0};char gaubSerialString[] ={ 34, /* descriptor length */ USB_STRING, /* descriptor type */ '0',0, '0',0, '0',0, '0',0, '-',0, '0',0, '0',0, '0',0, '0',0, '0',0, '0',0, '0',0, '0',0, '0',0, '0',0, '0',0};char gaubDeviceID[] ={ 0x00, 0x34, 0x4D, 0x46, 0x47, 0x3A, 0x53, 0x41, 0x4D, 0x53, 0x55, 0x4E, 0x47, 0x3B, 0x43, 0x4D, 0x44, 0x3A, 0x50, 0x43, 0x4C, 0x35, 0x45, 0x2C, 0x50, 0x43, 0x4C, 0x36, 0x3B, 0x4D, 0x4F, 0x44, 0x45, 0x4C, 0x3A, 0x37, 0x33, 0x30, 0x30, 0x3B, 0x43, 0x4C, 0x53, 0x3A, 0x50, 0x52, 0x49, 0x4E, 0x54, 0x45, 0x52, 0x3B};static int TotalDescrLen = -1;/* * The last USB SETUP packet received. */static UsbSetup UsbSetupBuf;/* * Buffer for DATA Packets. */#define TOT_NUM_OF_DESCRIPTORS 128#define SIZE_OF_DESCRIPTOR_CHAIN 4#define USB_PCK_SIZE_HS 512 // HS mode#define USB_PCK_SIZE_FS 64 // FS mode#define MAX_NUM_OF_ENDPOINTS 16#ifdef _USB_DMA_BUFFILL//#define tot_n_buf 16//#define tot_n_buf_o tot_n_buf/num_of_pck_in_transf//#define USB_BUFFER_SIZE num_of_pck_in_bfill * USB_PCK_SIZE_HS#else//#define tot_n_buf 8//#define tot_n_buf_o tot_n_buf#define USB_BUFFER_SIZE USB_PCK_SIZE_HS#endif // _USB_DMA_BUFFILL// SHOULD BE ALLOCATED DINAMICALLY!!!!!!static char USB_BufferList[TOT_NUM_OF_DESCRIPTORS + 1][USB_BUFFER_SIZE];//static DmaUsbBulkDescr DmaUsbBulkDescrArray[TOT_NUM_OF_DESCRIPTORS];static char DmaUsbBulkDescrArray[(TOT_NUM_OF_DESCRIPTORS + 1) * sizeof(DmaUsbBulkDescr) - 1];static unsigned int DmaUsbMaxPacketSize = USB_PCK_SIZE_HS; // Max Pck Size (default is HS)static unsigned int DmaUsbCtrlMaxPacketSize = 64; // Max Pck Size (default is HS)static enum { USB_CTRL_IDLE, USB_CTRLIN_DATA_IN_STAGE, USB_CTRLIN_STATUS_OUT_STAGE, USB_CTRLOUT_DATA_OUT_STAGE, USB_CTRLOUT_STATUS_IN_STAGE } USBCtrlTransftateMachine;static unsigned int DmaUsbDescrType = 0;/******************************************************************* * DMA MODE :: * * Routines to handle DMA descriptors freelist and endpoint queues. *******************************************************************//******************************************************************************************************************* * Descriptor FreeList * * 32 bit 32 bit 32 bit 32 bit * * ---> +------------+ +---> +------------+ +--> +------------+ +--> .... +-> +------------+ +--> NULL * | Status | | | Status | | | Status | | | Status | | * +------------+ | +------------+ | +------------+ | +------------+ | * | Reserved | | | Reserved | | | Reserved | | | Reserved | | * +------------+ | +------------+ | +------------+ | +------------+ | * +---| BufAddr | | +---| BufAddr | | +---| BufAddr | | +---| BufAddr | | * | +------------+ | | +------------+ | | +------------+ | | +------------+ | * | | Next |---+ | | Next |---+ | | Next |---+ | | Next |---+ * +-> +------------+ +-> +------------+ +-> +------------+ +-> +------------+ * | | | | | | | | * | | | | | | | | * . . . . . . . . * . DATA . . DATA . . DATA . . DATA . * . . . . . . . . * | | | | | | | | * | | | | | | | | * | | | | | | | | * +------------+ +------------+ +------------+ +------------+ * * * The driver keeps a freelist of structures in which the first part is the 16-byte DMA structured as described * in the inSilicon "UDC-AHB Subsystem - User Manual" and the second part is a data buffer (512 bytes for HS and 64 * bytes for FS). This data architecture allows the driver to easily convert a descriptor pointer into a buffer * pointer (and viceversa) by simply adding (subtracting) 16 to the pointer. * * The descriptors FreeList can be accessed by both the USB driver (interrupt handler) and the application; the former * needs to alloc descriptors to program DMA; the latter uses descriptor as a mean for getting/putting data to the * driver. The routines preserve data integrity in case of race condition (two tasks or a task and the interrupt * handler accessing concurrently the freelist). * There are three basics routines to handle the FreeList: * * static void USB_InitDescrFreeList(); // Init DMA descriptor freelist * void * USB_GetDescrFromFreeList(unsigned short num); // Returns a pointer to a list of 'num' free DMA descriptors * int USB_PutDescrIntoFreeList(DmaUsbBulkDescr *descp); // Returns a DMA descriptors chain into freelist * * * *******************************************************************************************************************/static DmaUsbBulkDescr *USBDMA_DescrFreeList = 0x0; #define OUT_DIR_EL 3#define IN_EP0 0#define IN_EP1 1#define IN_EP2 2#define OUT_EP0 3#define OUT_EP1 4#define OUT_EP2 5#define SETUP_EP0 6
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -