日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

USB复合设备(键盘鼠标U盘三合一)基于标准库

發布時間:2024/3/26 编程问答 40 豆豆
生活随笔 收集整理的這篇文章主要介紹了 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文件來識別這些設備。
主要修改的是下面幾個函數。

/******************************************************************************* * 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); }

根據前面自己的配置來修改,端點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

#include "fatfs_flash_spi.h" #include "stm32f10x_flash.h" #include "mass_mal.h" #include <stdio.h> /* Private typedef -----------------------------------------------------------*/ /* Private define ------------------------------------------------------------*/ /* Private macro -------------------------------------------------------------*/ /* Private variables ---------------------------------------------------------*/ uint32_t Mass_Memory_Size[2]; uint32_t Mass_Block_Size[2]; uint32_t Mass_Block_Count[2]; __IO uint32_t Status = 0; //#define sFLASH_ID 0xEF3015 //W25X16 //#define sFLASH_ID 0xEF4015 //W25Q16 //#define sFLASH_ID 0XEF4017 //W25Q64 //#define sFLASH_ID 0XEF4018 //W25Q128 /* Private function prototypes -----------------------------------------------*/ /* Private functions ---------------------------------------------------------*/ /******************************************************************************* * Function Name : MAL_Init * Description : 初始化STM32上的媒體設備 * Input : None * Output : None * Return : None *******************************************************************************/ uint16_t MAL_Init(uint8_t lun) {uint16_t status = MAL_OK;switch (lun){case 0:FLASH_Unlock();break;default:return MAL_FAIL;}return status; } /******************************************************************************* * Function Name : MAL_Write * Description : Write sectors * Input : None * Output : None * Return : None *******************************************************************************/ uint16_t MAL_Write(uint8_t lun, uint32_t Memory_Offset, uint32_t *Writebuff, uint16_t Transfer_Length) { uint16_t i;switch (lun) { case 0: for(i=0;i<Transfer_Length;i+=FLASH_PAGE_SIZE) {if(FLASH_WaitForLastOperation(FLASH_WAIT_TIMEOUT)!=FLASH_TIMEOUT){ FLASH_ClearFlag(FLASH_FLAG_EOP|FLASH_FLAG_PGERR|FLASH_FLAG_WRPRTERR);} FLASH_ErasePage(FLASH_START_ADDR + Memory_Offset + i);}for(i=0;i<Transfer_Length;i+=4) { if(FLASH_WaitForLastOperation(FLASH_WAIT_TIMEOUT)!=FLASH_TIMEOUT){ FLASH_ClearFlag(FLASH_FLAG_EOP|FLASH_FLAG_PGERR| FLASH_FLAG_WRPRTERR);} FLASH_ProgramWord(FLASH_START_ADDR + Memory_Offset + i , Writebuff[i>>2]); }break; default: return MAL_FAIL; } return MAL_OK; }/******************************************************************************* * Function Name : MAL_Read * Description : Read sectors * Input : None * Output : None * Return : Buffer pointer *******************************************************************************/ uint16_t MAL_Read(uint8_t lun, uint32_t Memory_Offset, uint32_t *Readbuff, uint16_t Transfer_Length) { uint16_t i;switch (lun){ case 0:for(i=0;i<Transfer_Length;i+=4){ Readbuff[i>>2] = ((vu32*)(FLASH_START_ADDR + Memory_Offset))[i>>2]; } break;default: return MAL_FAIL;} return MAL_OK;}/******************************************************************************* * Function Name : MAL_GetStatus * Description : Get status * Input : None * Output : None * Return : None *******************************************************************************/ uint16_t MAL_GetStatus (uint8_t lun) { if (lun == 0){ Mass_Block_Count[0] = FLASH_SIZE/FLASH_PAGE_SIZE; Mass_Block_Size[0] = FLASH_PAGE_SIZE; Mass_Memory_Size[0] = FLASH_SIZE;return MAL_OK;} return MAL_FAIL; }/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/

其余的文件只需要修改端點為前面設置的端點就好了。
當然要進行flash的操作還需要stm32f10x_flash文件。
這里還有一個fatfs_flash_spi.c文件

/********************************************************************************* @file bsp_xxx.c* @author STMicroelectronics* @version V1.0* @date 2013-xx-xx* @brief spi flash 底層應用函數bsp ******************************************************************************* @attention** 實驗平臺:野火 F103-指南者 STM32 開發板 * 論壇 :http://www.firebbs.cn* 淘寶 :https://fire-stm32.taobao.com********************************************************************************/#include <fatfs_flash_spi.h> #include "stm32f10x_spi.h" #include "stm32f10x_gpio.h" #include "stm32f10x_rcc.h"static __IO uint32_t SPITimeout = SPIT_LONG_TIMEOUT; static uint16_t SPI_TIMEOUT_UserCallback(uint8_t errorCode);/*** @brief SPI_FLASH初始化* @param 無* @retval 無*/ uint8_t FLASH_SPI_disk_initialize(void) {SPI_InitTypeDef SPI_InitStructure;GPIO_InitTypeDef GPIO_InitStructure;/* 使能SPI時鐘 */FLASH_SPI_APBxClock_FUN ( FLASH_SPI_CLK, ENABLE );/* 使能SPI引腳相關的時鐘 */FLASH_SPI_CS_APBxClock_FUN ( FLASH_SPI_CS_CLK|FLASH_SPI_SCK_CLK|FLASH_SPI_MISO_PIN|FLASH_SPI_MOSI_PIN, ENABLE );/* 配置SPI的 CS引腳,普通IO即可 */GPIO_InitStructure.GPIO_Pin = FLASH_SPI_CS_PIN;GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;GPIO_Init(FLASH_SPI_CS_PORT, &GPIO_InitStructure);/* 配置SPI的 SCK引腳*/GPIO_InitStructure.GPIO_Pin = FLASH_SPI_SCK_PIN;GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;GPIO_Init(FLASH_SPI_SCK_PORT, &GPIO_InitStructure);/* 配置SPI的 MISO引腳*/GPIO_InitStructure.GPIO_Pin = FLASH_SPI_MISO_PIN;GPIO_Init(FLASH_SPI_MISO_PORT, &GPIO_InitStructure);/* 配置SPI的 MOSI引腳*/GPIO_InitStructure.GPIO_Pin = FLASH_SPI_MOSI_PIN;GPIO_Init(FLASH_SPI_MOSI_PORT, &GPIO_InitStructure);/* 停止信號 FLASH: CS引腳高電平*/SPI_FLASH_CS_HIGH();/* SPI 模式配置 */// FLASH芯片 支持SPI模式0及模式3,據此設置CPOL CPHASPI_InitStructure.SPI_Direction = SPI_Direction_2Lines_FullDuplex;SPI_InitStructure.SPI_Mode = SPI_Mode_Master;SPI_InitStructure.SPI_DataSize = SPI_DataSize_8b;SPI_InitStructure.SPI_CPOL = SPI_CPOL_High;SPI_InitStructure.SPI_CPHA = SPI_CPHA_2Edge;SPI_InitStructure.SPI_NSS = SPI_NSS_Soft;SPI_InitStructure.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_2;SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_MSB;SPI_InitStructure.SPI_CRCPolynomial = 7;SPI_Init(FLASH_SPIx , &SPI_InitStructure);/* 使能 SPI */SPI_Cmd(FLASH_SPIx , ENABLE);if(sFLASH_ID == SPI_FLASH_ReadID()) /*檢測FLASH是否正常工作*/{return 0; /* Clear STA_NOINIT flag */}else{return 1;} }/*** @brief 擦除FLASH扇區* @param SectorAddr:要擦除的扇區地址* @retval 無*/ void SPI_FLASH_SectorErase(u32 SectorAddr) {/* 發送FLASH寫使能命令 */SPI_FLASH_WriteEnable();SPI_FLASH_WaitForWriteEnd();/* 擦除扇區 *//* 選擇FLASH: CS低電平 */SPI_FLASH_CS_LOW();/* 發送扇區擦除指令*/SPI_FLASH_SendByte(W25X_SectorErase);/*發送擦除扇區地址的高位*/SPI_FLASH_SendByte((SectorAddr & 0xFF0000) >> 16);/* 發送擦除扇區地址的中位 */SPI_FLASH_SendByte((SectorAddr & 0xFF00) >> 8);/* 發送擦除扇區地址的低位 */SPI_FLASH_SendByte(SectorAddr & 0xFF);/* 停止信號 FLASH: CS 高電平 */SPI_FLASH_CS_HIGH();/* 等待擦除完畢*/SPI_FLASH_WaitForWriteEnd(); }/*** @brief 擦除FLASH扇區,整片擦除* @param 無* @retval 無*/ void SPI_FLASH_BulkErase(void) {/* 發送FLASH寫使能命令 */SPI_FLASH_WriteEnable();/* 整塊 Erase *//* 選擇FLASH: CS低電平 */SPI_FLASH_CS_LOW();/* 發送整塊擦除指令*/SPI_FLASH_SendByte(W25X_ChipErase);/* 停止信號 FLASH: CS 高電平 */SPI_FLASH_CS_HIGH();/* 等待擦除完畢*/SPI_FLASH_WaitForWriteEnd(); }/*** @brief 對FLASH按頁寫入數據,調用本函數寫入數據前需要先擦除扇區* @param pBuffer,要寫入數據的指針* @param WriteAddr,寫入地址* @param NumByteToWrite,寫入數據長度,必須小于等于SPI_FLASH_PerWritePageSize* @retval 無*/ void SPI_FLASH_PageWrite(u8* pBuffer, u32 WriteAddr, u16 NumByteToWrite) {/* 發送FLASH寫使能命令 */SPI_FLASH_WriteEnable();/* 選擇FLASH: CS低電平 */SPI_FLASH_CS_LOW();/* 寫頁寫指令*/SPI_FLASH_SendByte(W25X_PageProgram);/*發送寫地址的高位*/SPI_FLASH_SendByte((WriteAddr & 0xFF0000) >> 16);/*發送寫地址的中位*/SPI_FLASH_SendByte((WriteAddr & 0xFF00) >> 8);/*發送寫地址的低位*/SPI_FLASH_SendByte(WriteAddr & 0xFF);if(NumByteToWrite > SPI_FLASH_PerWritePageSize){NumByteToWrite = SPI_FLASH_PerWritePageSize;FLASH_ERROR("SPI_FLASH_PageWrite too large!"); }/* 寫入數據*/while (NumByteToWrite--){/* 發送當前要寫入的字節數據 */SPI_FLASH_SendByte(*pBuffer);/* 指向下一字節數據 */pBuffer++;}/* 停止信號 FLASH: CS 高電平 */SPI_FLASH_CS_HIGH();/* 等待寫入完畢*/SPI_FLASH_WaitForWriteEnd(); }/*** @brief 對FLASH寫入數據,調用本函數寫入數據前需要先擦除扇區* @param pBuffer,要寫入數據的指針* @param WriteAddr,寫入地址* @param NumByteToWrite,寫入數據長度* @retval 無*/ void SPI_FLASH_BufferWrite(u8* pBuffer, u32 WriteAddr, u16 NumByteToWrite) {u8 NumOfPage = 0, NumOfSingle = 0, Addr = 0, count = 0, temp = 0;/*mod運算求余,若writeAddr是SPI_FLASH_PageSize整數倍,運算結果Addr值為0*/Addr = WriteAddr % SPI_FLASH_PageSize;/*差count個數據值,剛好可以對齊到頁地址*/count = SPI_FLASH_PageSize - Addr;/*計算出要寫多少整數頁*/NumOfPage = NumByteToWrite / SPI_FLASH_PageSize;/*mod運算求余,計算出剩余不滿一頁的字節數*/NumOfSingle = NumByteToWrite % SPI_FLASH_PageSize;/* Addr=0,則WriteAddr 剛好按頁對齊 aligned */if (Addr == 0){/* NumByteToWrite < SPI_FLASH_PageSize */if (NumOfPage == 0) {SPI_FLASH_PageWrite(pBuffer, WriteAddr, NumByteToWrite);}else /* NumByteToWrite > SPI_FLASH_PageSize */{ /*先把整數頁都寫了*/while (NumOfPage--){SPI_FLASH_PageWrite(pBuffer, WriteAddr, SPI_FLASH_PageSize);WriteAddr += SPI_FLASH_PageSize;pBuffer += SPI_FLASH_PageSize;}/*若有多余的不滿一頁的數據,把它寫完*/SPI_FLASH_PageWrite(pBuffer, WriteAddr, NumOfSingle);}}/* 若地址與 SPI_FLASH_PageSize 不對齊 */else {/* NumByteToWrite < SPI_FLASH_PageSize */if (NumOfPage == 0){/*當前頁剩余的count個位置比NumOfSingle小,一頁寫不完*/if (NumOfSingle > count) {temp = NumOfSingle - count;/*先寫滿當前頁*/SPI_FLASH_PageWrite(pBuffer, WriteAddr, count);WriteAddr += count;pBuffer += count;/*再寫剩余的數據*/SPI_FLASH_PageWrite(pBuffer, WriteAddr, temp);}else /*當前頁剩余的count個位置能寫完NumOfSingle個數據*/{SPI_FLASH_PageWrite(pBuffer, WriteAddr, NumByteToWrite);}}else /* NumByteToWrite > SPI_FLASH_PageSize */{/*地址不對齊多出的count分開處理,不加入這個運算*/NumByteToWrite -= count;NumOfPage = NumByteToWrite / SPI_FLASH_PageSize;NumOfSingle = NumByteToWrite % SPI_FLASH_PageSize;/* 先寫完count個數據,為的是讓下一次要寫的地址對齊 */SPI_FLASH_PageWrite(pBuffer, WriteAddr, count);/* 接下來就重復地址對齊的情況 */WriteAddr += count;pBuffer += count;/*把整數頁都寫了*/while (NumOfPage--){SPI_FLASH_PageWrite(pBuffer, WriteAddr, SPI_FLASH_PageSize);WriteAddr += SPI_FLASH_PageSize;pBuffer += SPI_FLASH_PageSize;}/*若有多余的不滿一頁的數據,把它寫完*/if (NumOfSingle != 0){SPI_FLASH_PageWrite(pBuffer, WriteAddr, NumOfSingle);}}} }/*** @brief 讀取FLASH數據* @param pBuffer,存儲讀出數據的指針* @param ReadAddr,讀取地址* @param NumByteToRead,讀取數據長度* @retval 無*/ void SPI_FLASH_BufferRead(u8* pBuffer, u32 ReadAddr, u16 NumByteToRead) {/* 選擇FLASH: CS低電平 */SPI_FLASH_CS_LOW();/* 發送 讀 指令 */SPI_FLASH_SendByte(W25X_ReadData);/* 發送 讀 地址高位 */SPI_FLASH_SendByte((ReadAddr & 0xFF0000) >> 16);/* 發送 讀 地址中位 */SPI_FLASH_SendByte((ReadAddr& 0xFF00) >> 8);/* 發送 讀 地址低位 */SPI_FLASH_SendByte(ReadAddr & 0xFF);/* 讀取數據 */while (NumByteToRead--) /* while there is data to be read */{/* 讀取一個字節*/*pBuffer = SPI_FLASH_SendByte(Dummy_Byte);/* 指向下一個字節緩沖區 */pBuffer++;}/* 停止信號 FLASH: CS 高電平 */SPI_FLASH_CS_HIGH(); }/*** @brief 讀取FLASH ID* @param 無* @retval FLASH ID*/ u32 SPI_FLASH_ReadID(void) {u32 Temp = 0, Temp0 = 0, Temp1 = 0, Temp2 = 0;/* 開始通訊:CS低電平 */SPI_FLASH_CS_LOW();/* 發送JEDEC指令,讀取ID */SPI_FLASH_SendByte(W25X_JedecDeviceID);/* 讀取一個字節數據 */Temp0 = SPI_FLASH_SendByte(Dummy_Byte);/* 讀取一個字節數據 */Temp1 = SPI_FLASH_SendByte(Dummy_Byte);/* 讀取一個字節數據 */Temp2 = SPI_FLASH_SendByte(Dummy_Byte);/* 停止通訊:CS高電平 */SPI_FLASH_CS_HIGH();/*把數據組合起來,作為函數的返回值*/Temp = (Temp0 << 16) | (Temp1 << 8) | Temp2;return Temp; }/*** @brief 讀取FLASH Device ID* @param 無* @retval FLASH Device ID*/ u32 SPI_FLASH_ReadDeviceID(void) {u32 Temp = 0;/* Select the FLASH: Chip Select low */SPI_FLASH_CS_LOW();/* Send "RDID " instruction */SPI_FLASH_SendByte(W25X_DeviceID);SPI_FLASH_SendByte(Dummy_Byte);SPI_FLASH_SendByte(Dummy_Byte);SPI_FLASH_SendByte(Dummy_Byte);/* Read a byte from the FLASH */Temp = SPI_FLASH_SendByte(Dummy_Byte);/* Deselect the FLASH: Chip Select high */SPI_FLASH_CS_HIGH();return Temp; } /******************************************************************************* * Function Name : SPI_FLASH_StartReadSequence * Description : Initiates a read data byte (READ) sequence from the Flash. * This is done by driving the /CS line low to select the device, * then the READ instruction is transmitted followed by 3 bytes * address. This function exit and keep the /CS line low, so the * Flash still being selected. With this technique the whole * content of the Flash is read with a single READ instruction. * Input : - ReadAddr : FLASH's internal address to read from. * Output : None * Return : None *******************************************************************************/ void SPI_FLASH_StartReadSequence(u32 ReadAddr) {/* Select the FLASH: Chip Select low */SPI_FLASH_CS_LOW();/* Send "Read from Memory " instruction */SPI_FLASH_SendByte(W25X_ReadData);/* Send the 24-bit address of the address to read from -----------------------*//* Send ReadAddr high nibble address byte */SPI_FLASH_SendByte((ReadAddr & 0xFF0000) >> 16);/* Send ReadAddr medium nibble address byte */SPI_FLASH_SendByte((ReadAddr& 0xFF00) >> 8);/* Send ReadAddr low nibble address byte */SPI_FLASH_SendByte(ReadAddr & 0xFF); }/*** @brief 使用SPI讀取一個字節的數據* @param 無* @retval 返回接收到的數據*/ u8 SPI_FLASH_ReadByte(void) {return (SPI_FLASH_SendByte(Dummy_Byte)); }/*** @brief 使用SPI發送一個字節的數據* @param byte:要發送的數據* @retval 返回接收到的數據*/ u8 SPI_FLASH_SendByte(u8 byte) {SPITimeout = SPIT_FLAG_TIMEOUT;/* 等待發送緩沖區為空,TXE事件 */while (SPI_I2S_GetFlagStatus(FLASH_SPIx , SPI_I2S_FLAG_TXE) == RESET){if((SPITimeout--) == 0) return SPI_TIMEOUT_UserCallback(0);}/* 寫入數據寄存器,把要寫入的數據寫入發送緩沖區 */SPI_I2S_SendData(FLASH_SPIx , byte);SPITimeout = SPIT_FLAG_TIMEOUT;/* 等待接收緩沖區非空,RXNE事件 */while (SPI_I2S_GetFlagStatus(FLASH_SPIx , SPI_I2S_FLAG_RXNE) == RESET){if((SPITimeout--) == 0) return SPI_TIMEOUT_UserCallback(1);}/* 讀取數據寄存器,獲取接收緩沖區數據 */return SPI_I2S_ReceiveData(FLASH_SPIx ); }/*** @brief 使用SPI發送兩個字節的數據* @param byte:要發送的數據* @retval 返回接收到的數據*/ u16 SPI_FLASH_SendHalfWord(u16 HalfWord) {SPITimeout = SPIT_FLAG_TIMEOUT;/* 等待發送緩沖區為空,TXE事件 */while (SPI_I2S_GetFlagStatus(FLASH_SPIx , SPI_I2S_FLAG_TXE) == RESET){if((SPITimeout--) == 0) return SPI_TIMEOUT_UserCallback(2);}/* 寫入數據寄存器,把要寫入的數據寫入發送緩沖區 */SPI_I2S_SendData(FLASH_SPIx , HalfWord);SPITimeout = SPIT_FLAG_TIMEOUT;/* 等待接收緩沖區非空,RXNE事件 */while (SPI_I2S_GetFlagStatus(FLASH_SPIx , SPI_I2S_FLAG_RXNE) == RESET){if((SPITimeout--) == 0) return SPI_TIMEOUT_UserCallback(3);}/* 讀取數據寄存器,獲取接收緩沖區數據 */return SPI_I2S_ReceiveData(FLASH_SPIx ); }/*** @brief 向FLASH發送 寫使能 命令* @param none* @retval none*/ void SPI_FLASH_WriteEnable(void) {/* 通訊開始:CS低 */SPI_FLASH_CS_LOW();/* 發送寫使能命令*/SPI_FLASH_SendByte(W25X_WriteEnable);/*通訊結束:CS高 */SPI_FLASH_CS_HIGH(); }/* WIP(busy)標志,FLASH內部正在寫入 */ #define WIP_Flag 0x01/*** @brief 等待WIP(BUSY)標志被置0,即等待到FLASH內部數據寫入完畢* @param none* @retval none*/ void SPI_FLASH_WaitForWriteEnd(void) {u8 FLASH_Status = 0;/* 選擇 FLASH: CS 低 */SPI_FLASH_CS_LOW();/* 發送 讀狀態寄存器 命令 */SPI_FLASH_SendByte(W25X_ReadStatusReg);/* 若FLASH忙碌,則等待 */do{/* 讀取FLASH芯片的狀態寄存器 */FLASH_Status = SPI_FLASH_SendByte(Dummy_Byte); }while ((FLASH_Status & WIP_Flag) == SET); /* 正在寫入標志 *//* 停止信號 FLASH: CS 高 */SPI_FLASH_CS_HIGH(); }//進入掉電模式 void SPI_Flash_PowerDown(void) { /* 通訊開始:CS低 */SPI_FLASH_CS_LOW();/* 發送 掉電 命令 */SPI_FLASH_SendByte(W25X_PowerDown);/*通訊結束:CS高 */SPI_FLASH_CS_HIGH(); } //喚醒 void SPI_Flash_WAKEUP(void) {/*選擇 FLASH: CS 低 */SPI_FLASH_CS_LOW();/* 發送 上電 命令 */SPI_FLASH_SendByte(W25X_ReleasePowerDown);/* 停止信號 FLASH: CS 高 */SPI_FLASH_CS_HIGH(); } /*** @brief 等待超時回調函數* @param None.* @retval None.*/ static uint16_t SPI_TIMEOUT_UserCallback(uint8_t errorCode) {/* 等待超時后的處理,輸出錯誤信息 */FLASH_ERROR("SPI 等待超時!errorCode = %d",errorCode);return 0; }/*********************************************END OF FILE**********************/

結束

總結

以上是生活随笔為你收集整理的USB复合设备(键盘鼠标U盘三合一)基于标准库的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。