接下来将是两个例子:USB RamDisk(一个使用MCU内RAM的U盘)和USH HID(模拟鼠标),供懒得看/看不懂手册的童鞋了解相关过程
仍未完成,明天继续
仍未完成,明天继续
<code class="lang-cpp">/** * @brief USB_OTG_BSP_Init * Initializes BSP configurations * @param None * @retval None */ void USB_OTG_BSP_Init(void) { } /** * @brief USB_OTG_BSP_EnableInterrupt * Enable USB Global interrupt * @param None * @retval None */ void USB_OTG_BSP_EnableInterrupt(void) { }</code>首先是void USB_OTG_BSP_Init(void),在这里需要:
<code class="lang-cpp">GPIO_InitTypeDef GPIO_InitStructure; RCC_AHB1PeriphClockCmd( RCC_AHB1Periph_GPIOA , ENABLE); /* Configure SOF VBUS ID DM DP Pins */ GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8 | GPIO_Pin_9 | GPIO_Pin_11 | GPIO_Pin_12; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF; GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL ; GPIO_Init(GPIOA, &GPIO_InitStructure); GPIO_PinAFConfig(GPIOA,GPIO_PinSource8,GPIO_AF_OTG1_FS) ; GPIO_PinAFConfig(GPIOA,GPIO_PinSource9,GPIO_AF_OTG1_FS) ; GPIO_PinAFConfig(GPIOA,GPIO_PinSource11,GPIO_AF_OTG1_FS) ; GPIO_PinAFConfig(GPIOA,GPIO_PinSource12,GPIO_AF_OTG1_FS) ; /* this for ID line debug */ GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10; GPIO_InitStructure.GPIO_OType = GPIO_OType_OD; GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP ; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz; GPIO_Init(GPIOA, &GPIO_InitStructure); GPIO_PinAFConfig(GPIOA,GPIO_PinSource10,GPIO_AF_OTG1_FS) ;</code>2.之后打开时钟
<code class="lang-cpp">RCC_APB2PeriphClockCmd(RCC_APB2Periph_SYSCFG, ENABLE); RCC_AHB2PeriphClockCmd(RCC_AHB2Periph_OTG_FS, ENABLE) ;</code>之后是void USB_OTG_BSP_EnableInterrupt(void),在这里配置USB相关的中断
<code class="lang-cpp">NVIC_InitTypeDef NVIC_InitStructure; NVIC_PriorityGroupConfig(NVIC_PriorityGroup_1); NVIC_InitStructure.NVIC_IRQChannel = OTG_FS_IRQn; NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1; NVIC_InitStructure.NVIC_IRQChannelSubPriority = 3; NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; NVIC_Init(&NVIC_InitStructure);</code>usb_conf.h
<code class="lang-cpp">#ifdef USB_OTG_FS_CORE #define RX_FIFO_FS_SIZE 128 #define TX0_FIFO_FS_SIZE 64 #define TX1_FIFO_FS_SIZE 128 #define TX2_FIFO_FS_SIZE 0 #define TX3_FIFO_FS_SIZE 0 #define TXH_NP_HS_FIFOSIZ 96 #define TXH_P_HS_FIFOSIZ 96 // #define USB_OTG_FS_LOW_PWR_MGMT_SUPPORT // #define USB_OTG_FS_SOF_OUTPUT_ENABLED #endif</code>
<code class="lang-cpp">#define USBD_CFG_MAX_NUM 1 #define USB_MAX_STR_DESC_SIZ 64 /**** USB_MSC_Class_Layer_Parameter *********/ #define MSC_IN_EP 0x81 #define MSC_OUT_EP 0x01 #define MSC_MAX_PACKET 512 #define MSC_MEDIA_PACKET 4096 /**** USB_HID Class_Layer_Parameter *********/ #define HID_IN_EP 0x81 #define HID_OUT_EP 0x01 #define HID_IN_PACKET 4 #define HID_OUT_PACKE 4</code>XXXXbd_desc.c
<code class="lang-cpp">typedef struct _Device_TypeDef { uint8_t *(*GetDeviceDescriptor)( uint8_t speed , uint16_t *length); uint8_t *(*GetLangIDStrDescriptor)( uint8_t speed , uint16_t *length); uint8_t *(*GetManufacturerStrDescriptor)( uint8_t speed , uint16_t *length); uint8_t *(*GetProductStrDescriptor)( uint8_t speed , uint16_t *length); uint8_t *(*GetSerialStrDescriptor)( uint8_t speed , uint16_t *length); uint8_t *(*GetConfigurationStrDescriptor)( uint8_t speed , uint16_t *length); uint8_t *(*GetInterfaceStrDescriptor)( uint8_t speed , uint16_t *length); #ifdef USB_SUPPORT_USER_STRING_DESC uint8_t* (*Get_USRStringDesc) (uint8_t speed, uint8_t idx , uint16_t *length); #endif /* USB_SUPPORT_USER_STRING_DESC */ } USBD_DEVICE, *pUSBD_DEVICE;</code>这些回调的具体定义请参考手册6.6章 USB device user interface
<code class="lang-cpp">__ALIGN_BEGIN uint8_t USBD_DeviceDesc[USB_SIZ_DEVICE_DESC] __ALIGN_END = { 0x12, /*bLength */ USB_DEVICE_DESCRIPTOR_TYPE, /*bDescriptorType*/ 0x00, /*bcdUSB */ 0x02, 0x00, /*bDeviceClass*/ 0x00, /*bDeviceSubClass*/ 0x00, /*bDeviceProtocol*/ USB_OTG_MAX_EP0_SIZE, /*bMaxPacketSize*/ LOBYTE(USBD_VID), /*idVendor*/ HIBYTE(USBD_VID), /*idVendor*/ LOBYTE(USBD_PID), /*idVendor*/ HIBYTE(USBD_PID), /*idVendor*/ 0x00, /*bcdDevice rel. 2.00*/ 0x02, USBD_IDX_MFC_STR, /*Index of manufacturer string*/ USBD_IDX_PRODUCT_STR, /*Index of product string*/ USBD_IDX_SERIAL_STR, /*Index of serial number string*/ USBD_CFG_MAX_NUM /*bNumConfigurations*/ } ; /* USB_DeviceDescriptor */</code>并把其中涉及的设备信息定位成了宏
<code class="lang-cpp">#define USBD_VID 0x0483 #define USBD_PID 0x5720 #define USBD_LANGID_STRING 0x409 #define USBD_MANUFACTURER_STRING "STMicroelectronics" #define USBD_PRODUCT_HS_STRING "Mass Storage in HS Mode" #define USBD_PRODUCT_FS_STRING "Mass Storage in FS Mode" #define USBD_CONFIGURATION_HS_STRING "MSC Config" #define USBD_INTERFACE_HS_STRING "MSC Interface" #define USBD_CONFIGURATION_FS_STRING "MSC Config" #define USBD_INTERFACE_FS_STRING "MSC Interface"</code>
<code class="lang-cpp">void USBD_Init(USB_OTG_CORE_HANDLE *pdev, USB_OTG_CORE_ID_TypeDef coreID, USBD_DEVICE *pDevice, USBD_Class_cb_TypeDef *class_cb, USBD_Usr_cb_TypeDef *usr_cb);</code>
[修改于 8年9个月前 - 2016/04/05 12:05:51]
时段 | 个数 |
---|---|
{{f.startingTime}}点 - {{f.endTime}}点 | {{f.fileCount}} |
200字以内,仅用于支线交流,主线讨论请采用回复功能。