USB Device ROM Stack API example program

Dependencies:   mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers usb.c Source File

usb.c

00001 #if defined(TARGET_LPC1549)
00002 #include "LPC15xx.h"
00003 #define ROM_API_ADDR (0x03000200)
00004 #else
00005 #error "target error"
00006 #endif
00007 #include <rl_usb.h>
00008 #include <string.h>
00009 
00010 USBD_API_T* pUsbApi;
00011 USBD_HANDLE_T hUsb;
00012 
00013 uint32_t USBD_MSC_MemorySize;
00014 uint32_t USBD_MSC_BlockSize;
00015 uint32_t USBD_MSC_BlockGroup;
00016 uint32_t USBD_MSC_BlockCount;
00017 uint8_t* USBD_MSC_BlockBuf;
00018 uint8_t USBD_MSC_MediaReady;
00019 
00020 extern void USBD_Init(void); // interface/*/usbd_*.c
00021 extern void USBD_Connect(uint8_t con);
00022 
00023 static USBD_API_INIT_PARAM_T usb_param;
00024 static USB_CORE_DESCS_T desc;
00025 static USBD_MSC_INIT_PARAM_T msc_param;
00026 
00027 static uint8_t usbmem[2048] __attribute__ ((aligned(2048)));
00028 static uint8_t* mem_base;
00029 static uint32_t mem_size;
00030 
00031 #define WBVAL(x) ((x) & 0xFF),(((x) >> 8) & 0xFF)
00032 
00033 /* USB Standard Device Descriptor */
00034 static const uint8_t USB_DeviceDescriptor[] = {
00035   18,               /* bLength */
00036   1,                /* USB_DEVICE_DESCRIPTOR_TYPE, bDescriptorType */
00037   WBVAL(0x0200),    /* 2.00  bcdUSB */
00038   0x00,             /* bDeviceClass */
00039   0x00,             /* bDeviceSubClass */
00040   0x00,             /* bDeviceProtocol */
00041   64,               /* USB_MAX_PACKET0, bMaxPacketSize0 */
00042   WBVAL(0x0d28),    /* idVendor */
00043   WBVAL(0x0204),    /* idProduct */
00044   WBVAL(0x0001),    /* bcdDevice */
00045   1,                /* iManufacturer */
00046   2,                /* iProduct */
00047   3,                /* iSerialNumber */
00048   1                 /* bNumConfigurations */
00049 };
00050 
00051 /* USB FSConfiguration Descriptor */
00052 /*   All Descriptors (Configuration, Interface, Endpoint, Class, Vendor */
00053 static const uint8_t USB_FsConfigDescriptor[] = {
00054 /* Configuration 1 */
00055   9,                /* USB_CONFIGUARTION_DESC_SIZE, bLength */
00056   2,                /* USB_CONFIGURATION_DESCRIPTOR_TYPE, bDescriptorType */
00057   WBVAL(9 + 9 + 7 * 2),/* wTotalLength */
00058   0x01,             /* bNumInterfaces */
00059   0x01,             /* bConfigurationValue */
00060   0x00,             /* iConfiguration */
00061   0xc0,             /* USB_CONFIG_SELF_POWERED, bmAttributes */
00062   50,               /* USB_CONFIG_POWER_MA(100), bMaxPower */
00063 /* Interface 0, Alternate Setting 0, MSC Class */
00064   9,                /* USB_INTERFACE_DESC_SIZE, bLength */
00065   4,                /* USB_INTERFACE_DESCRIPTOR_TYPE, bDescriptorType */
00066   0,                /* bInterfaceNumber */
00067   0,                /* bAlternateSetting */
00068   2,                /* bNumEndpoints */
00069   0x08,             /* USB_DEVICE_CLASS_STORAGE, bInterfaceClass */
00070   0x06,             /* MSC_SUBCLASS_SCSI, bInterfaceSubClass */
00071   0x50,             /* MSC_PROTOCOL_BULK_ONLY, bInterfaceProtocol */
00072   0x05,             /* iInterface */
00073 /* Bulk In Endpoint */
00074   7,                /* USB_ENDPOINT_DESC_SIZE, bLength */
00075   5,                /* USB_ENDPOINT_DESCRIPTOR_TYPE, bDescriptorType */
00076   0x01,             /* MSC_EP_IN, bEndpointAddress */
00077   0x02,             /* USB_ENDPOINT_TYPE_BULK, bmAttributes */
00078   WBVAL(64),        /* WBVAL(USB_FS_MAX_BULK_PACKET), wMaxPacketSize */
00079   0,                /* bInterval */
00080 /* Bulk Out Endpoint */
00081   7,                /* USB_ENDPOINT_DESC_SIZE, bLength */
00082   5,                /* USB_ENDPOINT_DESCRIPTOR_TYPE bDescriptorType */
00083   0x81,             /* MSC_EP_OUT bEndpointAddress */
00084   0x02,             /* USB_ENDPOINT_TYPE_BULK bmAttributes */
00085   WBVAL(64),        /* WBVAL(USB_FS_MAX_BULK_PACKET) wMaxPacketSize */
00086   0,                /* bInterval */
00087 /* Terminator */
00088   0                 /* bLength */
00089 };
00090 
00091 /* USB String Descriptor (optional) */
00092 static const uint8_t USB_StringDescriptor[] = {
00093   /* Index 0x00: LANGID Codes */
00094   4,            /* bLength */
00095   3,            /* USB_STRING_DESCRIPTOR_TYPE, bDescriptorType */
00096   WBVAL(0x0409),/* US English wLANGID */
00097   /* Index 0x01: Manufacturer */
00098   (8*2 + 2),   /* bLength (8 Char + Type + lenght) */
00099   3,            /* USB_STRING_DESCRIPTOR_TYPE, bDescriptorType */
00100   'm', 0, 'b', 0, 'e', 0, 'd', 0, '.', 0, 'o', 0, 'r', 0, 'g', 0, /* mbed.org */
00101   /* Index 0x02: Product */
00102   (14*2 + 2),   /* bLength (14 Char + Type + lenght) */
00103   3,            /* USB_STRING_DESCRIPTOR_TYPE, bDescriptorType */
00104   'M', 0, 'B', 0, 'E', 0, ' ', 0, 'D', 0,                                 /* MBED_ */
00105   'C', 0, 'M', 0, 'S', 0, 'I', 0, 'S', 0, '-', 0, 'D', 0, 'A', 0, 'P', 0, /* CMSIS-DAP */
00106   /* Index 0x03: Serial Number */
00107   (10*2 + 2),   /* bLength (10 Char + Type + lenght) */
00108   3,            /* USB_STRING_DESCRIPTOR_TYPE, bDescriptorType */
00109   '0', 0, '1', 0, '2', 0, '3', 0, '4', 0, '5', 0, '6', 0, '7', 0, '8', 0, '9', 0,
00110   /* Index 0x04: Interface 0, Alternate Setting 0 */
00111   (3*2 + 2),    /* bLength (3 Char + Type + lenght) */
00112   3,            /* USB_STRING_DESCRIPTOR_TYPE, bDescriptorType */
00113   'M', 0, 'S', 0, 'D', 0,
00114   /* Index 0x05: Interface 1, Alternate Setting 0 */
00115   (3*2 + 2),    /* bLength (3 Char + Type + lenght) */
00116   3,            /* USB_STRING_DESCRIPTOR_TYPE, bDescriptorType */
00117   'U', 0, 'S', 0, 'B', 0,
00118 };
00119 
00120 static const uint8_t InquiryStr[28] = {
00121     'M', 'B', 'E', 'D', '.', 'O', 'R', 'G', 'M', 'B', 
00122     'E', 'D', ' ', 'U', 'S', 'B', ' ', 'D', 'I', 'S', 
00123     'K', ' ', ' ', ' ', '1', '.', '0', ' '
00124 };
00125 
00126 
00127 static uint8_t MSC_buf[512];                           
00128 static void MSC_Read(uint32_t offset, uint8_t** buff_adr, uint32_t length, uint32_t high_offset)
00129 {
00130     int i, j;
00131     if (offset % 512 == 0) { /* head chunk ? */
00132         usbd_msc_read_sect(offset / 512, MSC_buf, 1);
00133     }
00134     j = offset % 512;
00135     for (i = 0; i < length; i++, j++) {
00136       (*buff_adr)[i] = MSC_buf[j];
00137    }
00138 }
00139 
00140 static void MSC_Write(uint32_t offset, uint8_t** buff_adr, uint32_t length, uint32_t high_offset)
00141 {
00142     int i, j;
00143     j = offset % 512;
00144     for(i = 0; i < length; i++, j++) {
00145         MSC_buf[j] = (*buff_adr)[i];
00146     }
00147     if ((offset % 512) + length == 512) { /* tail chunk ? */
00148         usbd_msc_write_sect(offset / 512, MSC_buf, 1);
00149     }
00150 }
00151 
00152 static ErrorCode_t MSC_Verify(uint32_t offset, uint8_t* src, uint32_t length, uint32_t high_offset)
00153 {
00154     return LPC_OK;
00155 }
00156                            
00157 static void USBD_MSC_Init(void) {
00158     ErrorCode_t ret;
00159     
00160     usbd_msc_init();
00161     memset((void*)&msc_param, 0, sizeof(msc_param));
00162     msc_param.mem_base = mem_base;
00163     msc_param.mem_size = mem_size;
00164 
00165     /* mass storage paramas */
00166     msc_param.InquiryStr = InquiryStr; 
00167     msc_param.BlockCount = USBD_MSC_BlockCount;
00168     msc_param.BlockSize = USBD_MSC_BlockSize;
00169     msc_param.MemorySize = USBD_MSC_MemorySize;
00170     msc_param.intf_desc = USB_FsConfigDescriptor + 9;
00171 
00172     /* user defined functions */
00173     msc_param.MSC_Write = MSC_Write; 
00174     msc_param.MSC_Read = MSC_Read;
00175     msc_param.MSC_Verify = MSC_Verify;   
00176 
00177     ret = pUsbApi->msc->init(hUsb, &msc_param);
00178     if (ret == LPC_OK) {
00179         /* update memory variables */
00180         mem_base = msc_param.mem_base;
00181         mem_size = msc_param.mem_size;
00182     }
00183 }
00184 
00185 void usbd_init(void)
00186 {
00187     ErrorCode_t ret;
00188 
00189     USBD_Init();
00190     
00191     /* get USB API table pointer */
00192     pUsbApi = (USBD_API_T*)((*(ROM **)ROM_API_ADDR)->pUSBD);
00193 
00194     mem_base = usbmem;
00195     mem_size = sizeof(usbmem);
00196 
00197     /* initilize call back structures */
00198     memset((void*)&usb_param, 0, sizeof(usb_param));
00199     usb_param.usb_reg_base = LPC_USB_BASE;
00200     usb_param.mem_base = mem_base;
00201     usb_param.mem_size = mem_size;
00202     usb_param.max_num_ep = 2;
00203 
00204     /* Initialize Descriptor pointers */
00205     memset((void*)&desc, 0, sizeof(desc));
00206     desc.device_desc = USB_DeviceDescriptor;
00207     desc.string_desc = USB_StringDescriptor;
00208     desc.full_speed_desc = USB_FsConfigDescriptor;
00209     desc.high_speed_desc = USB_FsConfigDescriptor;
00210 
00211     /* USB Initialization */
00212     ret = pUsbApi->hw->Init(&hUsb, &desc, &usb_param);  
00213     if (ret == LPC_OK) {
00214         mem_base = usb_param.mem_base;
00215         mem_size = usb_param.mem_size;
00216         USBD_MSC_Init();    
00217     }
00218 }
00219 
00220 void usbd_connect(uint8_t con)
00221 {
00222     USBD_Connect(con);
00223     if (con) {
00224         pUsbApi->hw->Connect(hUsb, 1);
00225     }        
00226 }