USB复合设备(键盘鼠标U盘三合一)基于标准库
鍵盤鼠標屬于HID,U盤功能屬于MSC。至于這些定義,這里不再過多介紹。
網上有很多的例程,但是大多是基于HAL庫的,標準庫的我也找了不少例子看,但是沒有HID+MSC的例程。最后還是看了個官方的復合設備例程才頓悟的,官方的例程,網上也很好找。搜USB Composite examples應該就能找到。
手上的設備是基于stm32f1系列的,目前已經復合了鍵盤和鼠標,想要新增加一個U盤的功能。由于已經是成熟的產品了,硬件方面不方便修改,所以這里采用單片機內部的flash來模擬U盤功能。要去掉程序存儲的空間,我的單片機大小是512k,所以這里給U盤配置400k。
首先修改的就是usb_desc.c文件。這個文件主要存放的是一些描述符。一般來說,設備描述符是不需要修改的,主要修改的是配置描述符。
`//USB配置描述符 const uint8_t HID_ConfigDescriptor[CONFIG_DESC_SIZE] = {0x09, /* bLength: Configuration Descriptor size */CONFIGURATION_DESCRIPTOR_TYPE, /* bDescriptorType: Configuration */CONFIG_DESC_SIZE,//***********配置描述符長度,定義在.h文件中,移植需修改。0x00,0x03, //*********************接口數量配置,移植需修改。之前鼠標鍵盤為2,這里增加U盤改成30x01, //bConfiguration字段0x00, //iConfigurationz字段0xC0, //bmAttributes字段0x32, //bMaxPower字段/*******************第一個接口描述符(鍵盤)*********************/0x09, //bLength字段0x04, //bDescriptorType字段0x00, //bInterfaceNumber字段 接口的編號,第一個接口為000x00, //bAlternateSetting字段0x02, //bNumEndpoints字段,端點的數量,這里鍵盤采用輸入加輸出,所以2個端點0x03, //bInterfaceClass字段0x01, //bInterfaceSubClass字段0x01, //bInterfaceProtocol字段0x00, //iConfiguration字段/******************HID描述符************************/0x09, //bLength字段0x21, //bDescriptorType字段0x10, //bcdHID字段0x01,0x21, //bCountyCode字段0x01, //bNumDescriptors字段0x22, //bDescriptorType字段sizeof(HID_ReportDescriptor_KEY)&0xFF, //HID_ReportDescriptor_KEY為鍵盤的報告描述符(sizeof(HID_ReportDescriptor_KEY)>>8)&0xFF,/**********************輸入端點描述符***********************/0x07, //bLength字段0x05, //bDescriptorType字段0x81, //bEndpointAddress字段,01端點的輸入地址0x03, //bmAttributes字段0x10, //wMaxPacketSize字段0x00,0x01, //bInterval字段/**********************輸出端點描述符***********************/0x07, //bLength字段0x05, //bDescriptorType字段0x01, //bEndpointAddress字段,01d端點的輸出地址0x03, //bmAttributes字段0x10, //wMaxPacketSize字段0x00,0x01, //bInterval字段/*******************第二個接口描述符(鼠標)*********************/0x09, //bLength字段0x04, //bDescriptorType字段0x01, //bInterfaceNumber字段,第二個接口,為010x00, //bAlternateSetting字段0x01, //bNumEndpoints字段,我的鼠標采用了一個端點,所以為010x03, //bInterfaceClass字段0x01, //bInterfaceSubClass字段0x02, //bInterfaceProtocol字段0x00, //iConfiguration字段/******************HID描述符************************/0x09, //bLength字段0x21, //bDescriptorType字段0x10, //bcdHID字段0x01,0x21, //bCountyCode字段0x01, //bNumDescriptors字段0x22, //bDescriptorType字段sizeof(HID_ReportDescriptor_MOUSE)&0xFF,(sizeof(HID_ReportDescriptor_MOUSE)>>8)&0xFF,/**********************輸入端點描述符***********************/0x07, //bLength字段0x05, //bDescriptorType字段0x83, //bEndpointAddress字段,03端點的地址。0x03, //bmAttributes字段。D1~D0為端點傳輸類型選擇0x40, //wMaxPacketSize字段0x00,0x01, //bInterval字段//新增的U盤接口相關代碼 //******************** 第三個接口描述符(U盤) ********************/0x09, /* bLength: Interface Descriptor size */0x04, /* bDescriptorType: */0x02, /* bInterfaceNumber: Number of Interface 第三個接口的編號*/0x00, /* bAlternateSetting: Alternate setting */0x02, /* bNumEndpoints,U盤是有輸入輸出的,所以必須是兩個端點。*/0x08, /* bInterfaceClass: MASS STORAGE Class ,注意要識別成大容量存儲設備,這里必須選08*/0x06, /* bInterfaceSubClass : SCSI transparent*/0x50, /* nInterfaceProtocol */1, /* iInterface: *//******************** 輸入端點描述符 ******************/0x07, /*Endpoint descriptor length = 7*/0x05, /*Endpoint descriptor type */0x82, /*Endpoint address端點2的地址 */0x02, /*Bulk endpoint type */0x40, /*Maximum packet size (64 bytes) */0x00,0x00, /*Polling interval in milliseconds *//******************** 輸出端點描述符 ******************/0x07, /*Endpoint descriptor length = 7 */0x05, /*Endpoint descriptor type */0x02, /*Endpoint address端點2的地址 */0x02, /*Bulk endpoint type */0x40, /*Maximum packet size (64 bytes) */0x00,0x00 /*Polling interval in milliseconds*/ };`上面配置描述符和例程里面的都大同小異,主要是一些需要根據自己設備來修改的地方,但我進行了備注,根據備注理解改起來也很容易。
配置描述符改完,usb_desc.c文件的剩下的內容可以不做修改,要是沒有鼠標鍵盤的報告描述符,可以搜一個,然后根據其長度,修改配置描述符的大小就可以。
然后要修改的是usb_endp文件。這個主要根據上面的端點描述符來修改。
`uint8_t Receive_Buffer[2]; __IO uint8_t PrevXferComplete; //鍵盤的 void EP1_OUT_Callback(void) {USB_SIL_Read(EP1_OUT, Receive_Buffer);SetEPRxStatus(ENDP1, EP_RX_VALID); }void EP1_IN_Callback(void) {PrevXferComplete = 1; }//鼠標 void EP3_IN_Callback(void) {PrevXferComplete = 1; }//u盤 void EP2_IN_Callback(void) {Mass_Storage_In(); }void EP2_OUT_Callback(void) {Mass_Storage_Out(); } `當然還需要配置每個端點的地址。這里的地址好像可以隨便配置,但是最好是需要相差64k的,也就是0x40.
/* EP0 */ /* rx/tx buffer base address */ #define ENDP0_RXADDR (0x18) #define ENDP0_TXADDR (0x58)/* EP1 */ /* tx buffer base address */ /* tx buffer base address */ #define ENDP1_TXADDR (0x118) #define ENDP1_RXADDR (0x11C) /* EP3 */ /* tx buffer base address */ #define ENDP3_TXADDR (0x198)/* EP2 */ /* tx buffer base address */ /* tx buffer base address */ #define ENDP2_TXADDR (0x98) #define ENDP2_RXADDR (0xD8)接下來就是配置usb_prop.c文件來識別這些設備。
主要修改的是下面幾個函數。
根據前面自己的配置來修改,端點0是啟動usb所需要的。端點2是U盤的,所以類型不一樣。
/******************************************************************************* * Function Name : CustomHID_Data_Setup * Description : Handle the data class specific requests. * Input : Request Nb. * Output : None. * Return : USB_UNSUPPORT or USB_SUCCESS. *******************************************************************************/ RESULT CustomHID_Data_Setup(uint8_t RequestNo) {u8 *(*CopyRoutine)(u16);CopyRoutine = NULL;if ((RequestNo == GET_DESCRIPTOR)&& (Type_Recipient == (STANDARD_REQUEST | INTERFACE_RECIPIENT))&& (pInformation->USBwIndex0 < 2)){if (pInformation->USBwValue1 == REPORT_DESCRIPTOR){if (pInformation->USBwIndex0 == 0)//進行輪詢查詢,0為鍵盤。其他為鼠標,若是復合三個或多個hid可以0123一次增加。CopyRoutine = KP_GetReportDescriptor;elseCopyRoutine = Mouse_GetReportDescriptor;}else if (pInformation->USBwValue1 == HID_DESCRIPTOR_TYPE){if (pInformation->USBwIndex0 == 0)CopyRoutine = KP_GetHIDDescriptor;elseCopyRoutine = Mouse_GetHIDDescriptor;}} /* End of GET_DESCRIPTOR *//*** GET_PROTOCOL ***/else if ((Type_Recipient == (CLASS_REQUEST | INTERFACE_RECIPIENT))&& RequestNo == GET_PROTOCOL){CopyRoutine = CustomHID_GetProtocolValue;}if (CopyRoutine == NULL){return USB_UNSUPPORT;}pInformation->Ctrl_Info.CopyData = CopyRoutine;pInformation->Ctrl_Info.Usb_wOffset = 0;(*CopyRoutine)(0);return USB_SUCCESS; }如果你是從只有鍵盤的工程代碼中修改的話,還需要添加鼠標相關的獲取函數。這里直接貼出prop文件代碼。
#include "hw_config.h" #include "usb_lib.h" #include "usb_conf.h" #include "usb_prop.h" #include "usb_desc.h" #include "usb_pwr.h" #include "usb_bot.h" #include "memory.h" #include "mass_mal.h"uint32_t ProtocolValue; __IO uint8_t EXTI_Enable; __IO uint8_t Request = 0; uint8_t Report_Buf[2];DEVICE Device_Table = {EP_NUM,1 };/*CustomHID_SetReport_Feature function prototypes*/ uint8_t *CustomHID_SetReport_Feature(uint16_t Length);extern unsigned char Bot_State; extern Bulk_Only_CBW CBW; uint32_t Max_Lun = 0; DEVICE_PROP Device_Property = {CustomHID_init,CustomHID_Reset,CustomHID_Status_In,CustomHID_Status_Out,CustomHID_Data_Setup,CustomHID_NoData_Setup,CustomHID_Get_Interface_Setting,CustomHID_GetDeviceDescriptor,CustomHID_GetConfigDescriptor,CustomHID_GetStringDescriptor,0,0x40 /*MAX PACKET SIZE*/ }; USER_STANDARD_REQUESTS User_Standard_Requests = {CustomHID_GetConfiguration,CustomHID_SetConfiguration,CustomHID_GetInterface,CustomHID_SetInterface,CustomHID_GetStatus,CustomHID_ClearFeature,CustomHID_SetEndPointFeature,CustomHID_SetDeviceFeature,CustomHID_SetDeviceAddress };ONE_DESCRIPTOR Device_Descriptor = {(uint8_t*)HID_DeviceDescriptor,DEVICE_DESC_SIZE };ONE_DESCRIPTOR Config_Descriptor = {(uint8_t*)HID_ConfigDescriptor,CONFIG_DESC_SIZE };ONE_DESCRIPTOR KP_Report_Descriptor = { (u8 *)HID_ReportDescriptor_KEY, 63 }; ONE_DESCRIPTOR KP_Hid_Descriptor = { (u8*)HID_ConfigDescriptor + 18, 9 }; ONE_DESCRIPTOR Mouse_Report_Descriptor = { (u8 *)HID_ReportDescriptor_MOUSE, 54 }; ONE_DESCRIPTOR Mouse_Hid_Descriptor = { (u8*)HID_ConfigDescriptor + 50, 9 };ONE_DESCRIPTOR String_Descriptor[4] = {{ (uint8_t*)HID_LangIDString, LANGID_STRING },{ (uint8_t*)HID_VendorString, VENDOR_STRING_SIZE },{ (uint8_t*)HID_ProductString, PRODUCT_STRING_SIZE },{ (uint8_t*)HID_SerialString, SERIAL_STRING_SIZE } };/* Extern variables ----------------------------------------------------------*/ /* Private function prototypes -----------------------------------------------*//*CustomHID_SetReport_Feature function prototypes*/ uint8_t *CustomHID_SetReport_Feature(uint16_t Length);/* Extern function prototypes ------------------------------------------------*/ /* Private functions ---------------------------------------------------------*//******************************************************************************* * Function Name : CustomHID_init. * Description : Custom HID init routine. * Input : None. * Output : None. * Return : None. *******************************************************************************/ void CustomHID_init(void) {Get_SerialNum();pInformation->Current_Configuration = 0;/* Connect the device */PowerOn();/* Perform basic device initialization operations */USB_SIL_Init();bDeviceState = UNCONNECTED; }/******************************************************************************* * Function Name : CustomHID_Reset. * Description : Custom HID reset routine. * Input : None. * Output : None. * Return : None. *******************************************************************************/ void CustomHID_Reset(void) {/* Set CustomHID_DEVICE as not configured */pInformation->Current_Configuration = 0;pInformation->Current_Interface = 0;/*the default Interface*/pInformation->Current_Feature = HID_ConfigDescriptor[7];SetBTABLE(BTABLE_ADDRESS);/* Initialize Endpoint 0 */SetEPType(ENDP0, EP_CONTROL);SetEPTxStatus(ENDP0, EP_TX_STALL);SetEPRxAddr(ENDP0, ENDP0_RXADDR);SetEPTxAddr(ENDP0, ENDP0_TXADDR);Clear_Status_Out(ENDP0);SetEPRxCount(ENDP0, Device_Property.MaxPacketSize);SetEPRxValid(ENDP0);/* Initialize Endpoint 1 */SetEPType(ENDP1, EP_INTERRUPT);SetEPTxAddr(ENDP1, ENDP1_TXADDR);SetEPRxAddr(ENDP1, ENDP1_RXADDR);SetEPTxCount(ENDP1, 8);SetEPRxCount(ENDP1, 2);SetEPRxStatus(ENDP1, EP_RX_VALID);SetEPTxStatus(ENDP1, EP_TX_NAK);/* Initialize Endpoint In 3 */SetEPType(ENDP3, EP_INTERRUPT); //初始化為中斷端點類型SetEPTxAddr(ENDP3, ENDP3_TXADDR); //設置發送數據的地址SetEPTxCount(ENDP3, 5); //設置發送的長度SetEPTxStatus(ENDP3, EP_TX_NAK); //設置端點處于忙狀態/* Set this device to response on default address *//* 初始化端點2的輸入 */SetEPType(ENDP2, EP_BULK);SetEPTxCount(ENDP2, 64);SetEPTxAddr(ENDP2, ENDP2_TXADDR);SetEPTxStatus(ENDP2, EP_TX_NAK);/* 初始化端點2的輸出 */SetEPType(ENDP2, EP_BULK);SetEPRxAddr(ENDP2, ENDP2_RXADDR);SetEPRxCount(ENDP2, 64);SetEPRxStatus(ENDP2, EP_RX_VALID);bDeviceState = ATTACHED;SetDeviceAddress(0); } /******************************************************************************* * Function Name : CustomHID_SetConfiguration. * Description : Update the device state to configured and command the ADC * conversion. * Input : None. * Output : None. * Return : None. *******************************************************************************/ void CustomHID_SetConfiguration(void) {if (pInformation->Current_Configuration != 0)bDeviceState = CONFIGURED; // Device configured } /******************************************************************************* * Function Name : CustomHID_SetConfiguration. * Description : Update the device state to addressed. * Input : None. * Output : None. * Return : None. *******************************************************************************/ void CustomHID_SetDeviceAddress(void) { // bDeviceState = ADDRESSED; } /******************************************************************************* * Function Name : CustomHID_Status_In. * Description : Joystick status IN routine. * Input : None. * Output : None. * Return : None. *******************************************************************************/ void CustomHID_Status_In(void) {if (Report_Buf[1] == 0){//Led_State = Bit_RESET;}else{//Led_State = Bit_SET;}switch (Report_Buf[0]){/*Change LED's status according to the host report*/case 1: /* Led 1 */break;case 2: /* Led 2 */break;case 3:/* Led 3 */break;case 4:/* Led 4 */break;default:break;} }/******************************************************************************* * Function Name : CustomHID_Status_Out * Description : Joystick status OUT routine. * Input : None. * Output : None. * Return : None. *******************************************************************************/ void CustomHID_Status_Out(void) { }/******************************************************************************* * Function Name : CustomHID_Data_Setup * Description : Handle the data class specific requests. * Input : Request Nb. * Output : None. * Return : USB_UNSUPPORT or USB_SUCCESS. *******************************************************************************/ RESULT CustomHID_Data_Setup(uint8_t RequestNo) {u8 *(*CopyRoutine)(u16);CopyRoutine = NULL;if ((RequestNo == GET_DESCRIPTOR)&& (Type_Recipient == (STANDARD_REQUEST | INTERFACE_RECIPIENT))&& (pInformation->USBwIndex0 < 2)){if (pInformation->USBwValue1 == REPORT_DESCRIPTOR){if (pInformation->USBwIndex0 == 0)CopyRoutine = KP_GetReportDescriptor;elseCopyRoutine = Mouse_GetReportDescriptor;}else if (pInformation->USBwValue1 == HID_DESCRIPTOR_TYPE){if (pInformation->USBwIndex0 == 0)CopyRoutine = KP_GetHIDDescriptor;elseCopyRoutine = Mouse_GetHIDDescriptor;}} /* End of GET_DESCRIPTOR *//*** GET_PROTOCOL ***/else if ((Type_Recipient == (CLASS_REQUEST | INTERFACE_RECIPIENT))&& RequestNo == GET_PROTOCOL){CopyRoutine = CustomHID_GetProtocolValue;}if (CopyRoutine == NULL){return USB_UNSUPPORT;}pInformation->Ctrl_Info.CopyData = CopyRoutine;pInformation->Ctrl_Info.Usb_wOffset = 0;(*CopyRoutine)(0);return USB_SUCCESS; }/******************************************************************************* * Function Name : CustomHID_SetReport_Feature * Description : Set Feature request handling * Input : Length. * Output : None. * Return : Buffer *******************************************************************************/ uint8_t *CustomHID_SetReport_Feature(uint16_t Length) {if (Length == 0){pInformation->Ctrl_Info.Usb_wLength = 2;return NULL;}else{return Report_Buf;} }/******************************************************************************* * Function Name : CustomHID_NoData_Setup * Description : handle the no data class specific requests * Input : Request Nb. * Output : None. * Return : USB_UNSUPPORT or USB_SUCCESS. *******************************************************************************/ RESULT CustomHID_NoData_Setup(uint8_t RequestNo) {if ((Type_Recipient == (CLASS_REQUEST | INTERFACE_RECIPIENT))&& (RequestNo == SET_PROTOCOL)){return CustomHID_SetProtocol();}else{return USB_UNSUPPORT;} }/******************************************************************************* * Function Name : CustomHID_GetDeviceDescriptor. * Description : Gets the device descriptor. * Input : Length * Output : None. * Return : The address of the device descriptor. *******************************************************************************/ uint8_t *CustomHID_GetDeviceDescriptor(uint16_t Length) {return Standard_GetDescriptorData(Length, &Device_Descriptor); }/******************************************************************************* * Function Name : CustomHID_GetConfigDescriptor. * Description : Gets the configuration descriptor. * Input : Length * Output : None. * Return : The address of the configuration descriptor. *******************************************************************************/ uint8_t *CustomHID_GetConfigDescriptor(uint16_t Length) {return Standard_GetDescriptorData(Length, &Config_Descriptor); }/******************************************************************************* * Function Name : CustomHID_GetStringDescriptor * Description : Gets the string descriptors according to the needed index * Input : Length * Output : None. * Return : The address of the string descriptors. *******************************************************************************/ uint8_t *CustomHID_GetStringDescriptor(uint16_t Length) {uint8_t wValue0 = pInformation->USBwValue0; // if (wValue0 >= 4) // { // return NULL; // } // else // {return Standard_GetDescriptorData(Length, &String_Descriptor[wValue0]); // } }/******************************************************************************* * Function Name : Joystick_GetReportDescriptor. * Description : Gets the HID report descriptor. * Input : Length * Output : None. * Return : The address of the configuration descriptor. *******************************************************************************/ u8 *KP_GetReportDescriptor(u16 Length) {return Standard_GetDescriptorData(Length, &KP_Report_Descriptor); }u8 *Mouse_GetReportDescriptor(u16 Length) {return Standard_GetDescriptorData(Length, &Mouse_Report_Descriptor); }/******************************************************************************* * Function Name : Joystick_GetHIDDescriptor. * Description : Gets the HID descriptor. * Input : Length * Output : None. * Return : The address of the configuration descriptor. *******************************************************************************/ u8 *KP_GetHIDDescriptor(u16 Length) {return Standard_GetDescriptorData(Length, &KP_Hid_Descriptor); } u8 *Mouse_GetHIDDescriptor(u16 Length) {return Standard_GetDescriptorData(Length, &Mouse_Hid_Descriptor); }/******************************************************************************* * Function Name : CustomHID_Get_Interface_Setting. * Description : tests the interface and the alternate setting according to the * supported one. * Input : - Interface : interface number. * - AlternateSetting : Alternate Setting number. * Output : None. * Return : USB_SUCCESS or USB_UNSUPPORT. *******************************************************************************/ RESULT CustomHID_Get_Interface_Setting(uint8_t Interface, uint8_t AlternateSetting) {if (AlternateSetting > 0){return USB_UNSUPPORT;}else if (Interface > 0){return USB_UNSUPPORT;}return USB_SUCCESS; }/******************************************************************************* * Function Name : CustomHID_SetProtocol * Description : Joystick Set Protocol request routine. * Input : None. * Output : None. * Return : USB SUCCESS. *******************************************************************************/ RESULT CustomHID_SetProtocol(void) {uint8_t wValue0 = pInformation->USBwValue0;ProtocolValue = wValue0;return USB_SUCCESS; }/******************************************************************************* * Function Name : CustomHID_GetProtocolValue * Description : get the protocol value * Input : Length. * Output : None. * Return : address of the protocol value. *******************************************************************************/ uint8_t *CustomHID_GetProtocolValue(uint16_t Length) {if (Length == 0){pInformation->Ctrl_Info.Usb_wLength = 1;return NULL;}else{return (uint8_t *)(&ProtocolValue);} }//新增函數 / uint8_t *Get_Max_Lun(uint16_t Length) {if (Length == 0){pInformation->Ctrl_Info.Usb_wLength = LUN_DATA_LENGTH;return 0;}else{return((uint8_t*)(&Max_Lun));} }void CustomHID_ClearFeature (void) {if (CBW.dSignature != BOT_CBW_SIGNATURE)Bot_Abort(BOTH_DIR); } ///下面就該修改MSC相關的。
首先是文件mass_mal.c
其余的文件只需要修改端點為前面設置的端點就好了。
當然要進行flash的操作還需要stm32f10x_flash文件。
這里還有一個fatfs_flash_spi.c文件
結束
總結
以上是生活随笔為你收集整理的USB复合设备(键盘鼠标U盘三合一)基于标准库的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Vue中出现 error: ‘X
- 下一篇: 代码质量与安全 | 一文了解高级驾驶辅助