stm32f107 usbh主模式库解析 联系客服

发布时间 : 星期五 文章stm32f107 usbh主模式库解析更新完毕开始阅读32e7995fc281e53a5802fffc

1。azerty或QWERTY键盘的选择是通过在文件usbh_hid_keybrd.h中# qwerty_keyboard, # azerty_keyboard来声明的

3库用户API和回调函数 3.1库用户API

库用户API功能局限于以下两个功能:

■void usbh_process(void):此功能实现的核心状态机程序。它要从用户的主循环周期调用。 ■usbh_init:这个函数应该被称为主机的USB硬件与库的初始化。 下面为usbh_init的函数原型:

void USBH_Init(USB_OTG_CORE_HANDLE *pdev, USBH_Class_cb_TypeDef *class_cb, USBH_Usr_cb_TypeDef *usr_cb)

–pdev:指向USB主机寄存器核心结构的指针(为了将来需要)

–class_cb:指针指向类结构USBH_Class_cb_TypeDef,它既可以指向MSC_cb类来操作MSC设备也可以指向HID_cb类操作HID鼠标/键盘设备。

–usr _cb:指向USBH_Usr_cb_TypeDef类结构。这个结构定义独立类回调函数。(见3.2.2节)

3.2用户的回调函数

用户回调函数在用户模板文件usbh_usr.c中声明 定义了两种类型的用户回调:

■与类的相关操作的回调函数(MSC或HID)

■与类操作相独立的回调函数;他们主要是在枚举阶段被调用。这些回调是一个USBH_Usr_cb_TypeDef结构类型中定义 3.2.1类的回调函数 MSC用户回调函数功能

对于MSC,下面的回调函数被调用:int usr_msc_application(void)。

类的初始化结束后,这个函数被MSC状态机调用的为了帮助用户访问文件操作系统。 在这个回调函数,用户可以访问任何FAT文件系统(文件打开,文件读,写文件……)使用EFSL文件系统API。

同时,用户可以访问一个与库MSC类驱动不同的出口结构变量,usbh_msc_param。这个变量提供关于大容量存储的关键信息。

这个变量是使用一种类型massstorageparameter_typedef定义的结构,定义如下: typedef struct __MassStorageParameter {

uint32_t MSCapacity; /*MS device capacity in bytes */

uint32_t MSSenseKey; /*Request Sense SCSI command returned value */ uint16_t MSPageLength; /* MS device Page length */ uint8_t MSBulkOutEp; /* Bulk OUT endpoint address */ uint8_t MSBulkInEp; /*Bulk IN endpoint address */

uint8_t MSWriteProtect; /*Write protection status, 0: non protected, 1:protected */ } MassStorageParameter_TypeDef; HID用户的回调函数

对HID类,以下定义的回调函数:

■void USR_MOUSE_Init(void):用户的鼠标应用程序初始化 ■void USR_KEYBRD_Init(void):键盘应用的用户初始化

■void USR_MOUSE_ProcessData(HID_MOUSE_Data_TypeDef *data):这个回调函数 被调用当hid_mouse_data_typedef数据类型的输入参数有效时(见下面附注1)。 ■void USR_KEYBRD_ProcessData (uint8_t data):这个回调被调用时,一个新的ASCII字符型。其特征是输入参数数据。

注:1 HID_MOUSE_Data_TypeDef定义如下:

typedef struct _HID_MOUSE_Data {

uint8_t x; uint8_t y;

uint8_t z; /* Not Supported */

uint8_t button; /*Bitmap showing pressed buttons 1:pressed, 0: non pressed */ }

HID_MOUSE_Data_TypeDef; 3.2.2类独立的回调函数

这类独立的回调函数是在一个结构类型定义 usbh_usr_cb_typedef定义如下:

typedef struct _USBH_USR_PROP {

void (*Init)(void);

void (*DeviceAttached)(void); void (*ResetDevice)(void);

void (*DeviceDisconnected)(void); void (*OverCurrentDetected)(void);

void (*DeviceSpeedDetected)(uint8_t DeviceSpeed); void (*DeviceDescAvailable)(void *); void (*DeviceAddressAssigned)(void);

void (*ConfigurationDescAvailable)(USBH_CfgDesc_TypeDef *, USBH_InterfaceDesc_TypeDef *, USBH_EpDesc_TypeDef *);

void (*ManufacturerString)(void *); void (*ProductString)(void *); void (*SerialNumString)(void *); void (*EnumerationDone)(void);

USBH_USR_Status (*UserInput)(void);

void (*USBH_USR_DeviceNotSupported)(void); void (*UnrecoveredError)(void); }

USBH_Usr_cb_TypeDef; 以上回调函数功能描述如下。

–初始化:初始化时被usbh_init内核调用的功能。在这个功能,用户可以实现任何自己的应用程序相关的特定的初始化。

–deviceattached:当一个USB设备连接时被调用。它可以通过显示屏告诉用户任何有用的设备连接信息。

–devicereset:从主模式发出的USB复位命令。 –devicedisconnect:“当一个设备断开时调用。

–overcurrentdetected:对USB的VBUS电流进行检测时调用。 –devicespeeddetected:检测装置传输速率时调用 –devicedescavailable:设备描述符有效时调用 –deviceaddressassigned:当设备地址分配时调用。

–configurationdescavailable:当设备,接口和端点描述符可用时调用描述符是可用的 –manufacturingstring:提取制造商字符串时调用。 –productstring:提取设备字符串时调用。 –serialnumstring:序列号字符串提取时调用。 –enumerationdone:枚举完成时调用。

–userinput:枚举的过程结束后,提示用户进一步的行动,如按下一个按钮启动主模式的类操作

–usbh_usr_devicenotsupported:被检测的设备是在不在当前类驱动程序支持时调用。 –unrecoverederror:在“host_error_state”状态时调用内核的状态机。它允许用户处理任何错误,在液晶屏上显示一个错误的信息,例如。

注:1设备的速率信息是通过devicespeed参数返回。可能的值是:0x1对应全速设备,0x2对应低速设备。

2设备描述符的信息通过指针DeviceDesc返回,它指向usbh_devdesc_typedefdefined型结构如下:

typedef struct _DeviceDescriptor {

uint8_t bLength;

uint8_t bDescriptorType;

uint16_t bcdUSB; /* USB Specification Number which device complies too */ uint8_t bDeviceClass;

uint8_t bDeviceSubClass; uint8_t bDeviceProtocol; uint8_t bMaxPacketSize;

uint16_t idVendor; /* Vendor ID (Assigned by USB Org) */

uint16_t idProduct; /* Product ID (Assigned by Manufacturer) */ uint16_t bcdDevice; /* Device Release Number */

uint8_t iManufacturer; /* Index of Manufacturer String Descriptor */ uint8_t iProduct; /* Index of Product String Descriptor */

uint8_t iSerialNumber; /* Index of Serial Number String Descriptor */ uint8_t bNumConfigurations; /* Number of Possible Configurations */ }

USBH_DevDesc_TypeDef;

3设备配置信息(配置,接口和端点描述符)通过结构指针返回usbh_cfgdesc_typedef,usbh_interfacedesc_typedef和usbh_epdesc_typedefdefined定义如下: typedef struct _ConfigurationDescriptor {

uint8_t bLength;

uint8_t bDescriptorType; uint16_t wTotalLength; uint8_t bNumInterfaces; uint8_t bConfigurationValue; uint8_t iConfiguration; uint8_t bmAttributes; uint8_t bMaxPower; }

USBH_CfgDesc_TypeDef;

typedef struct _InterfaceDescriptor {

uint8_t bLength;

uint8_t bDescriptorType; uint8_t bInterfaceNumber;

uint8_t bAlternateSetting; /* Value used to select alternative setting */ uint8_t bNumEndpoints; /* Number of Endpoints used for this interface */ uint8_t bInterfaceClass; /* Class Code (Assigned by USB Org) */

uint8_t bInterfaceSubClass; /* Subclass Code (Assigned by USB Org) */ uint8_t bInterfaceProtocol; /* Protocol Code */

uint8_t iInterface; /* Index of String Descriptor Describing this interface */ }

USBH_InterfaceDesc_TypeDef; typedef struct _EndpointDescriptor {

uint8_t bLength;

uint8_t bDescriptorType;

uint8_t bEndpointAddress; /* indicates what endpoint this descriptor is describing */

uint8_t bmAttributes; /* specifies the transfer type. */

uint16_t wMaxPacketSize; /* Maximum Packet Size this endpoint is capable of sending or receiving */

uint8_t bInterval; /* is used to specify the polling interval of certain transfers. */ }

USBH_EpDesc_TypeDef;

4为了移动内核状态机到host_class_request状态,userinput回调函数应该返回值usbh_usr_resp_ok类型的usbh_usr_status typedef enum {