| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275 |
- /* USER CODE BEGIN Header */
- /**
- ******************************************************************************
- * @file : usbd_custom_hid_if.c
- * @version : v2.0_Cube
- * @brief : USB Device Custom HID interface file.
- ******************************************************************************
- * @attention
- *
- * Copyright (c) 2024 STMicroelectronics.
- * All rights reserved.
- *
- * This software is licensed under terms that can be found in the LICENSE file
- * in the root directory of this software component.
- * If no LICENSE file comes with this software, it is provided AS-IS.
- *
- ******************************************************************************
- */
- /* USER CODE END Header */
- /* Includes ------------------------------------------------------------------*/
- #include "usbd_custom_hid_if.h"
- /* USER CODE BEGIN INCLUDE */
- #define LED1_REPORT_ID 0x01
- #define LED1_REPORT_COUNT 0x01
- #define LED2_REPORT_ID 0x02
- #define LED2_REPORT_COUNT 0x01
- #define LED3_REPORT_ID 0x03
- #define LED3_REPORT_COUNT 0x01
- #define LED4_REPORT_ID 0x04
- #define LED4_REPORT_COUNT 0x01
- #define KEY_REPORT_ID 0x05
- #define TAMPER_REPORT_ID 0x06
- #define ADC_REPORT_ID 0x07
- /* USER CODE END INCLUDE */
- /* Private typedef -----------------------------------------------------------*/
- /* Private define ------------------------------------------------------------*/
- /* Private macro -------------------------------------------------------------*/
- /* USER CODE BEGIN PV */
- /* Private variables ---------------------------------------------------------*/
- uint8_t HID_state;
- uint8_t HID_Buffer[64];
- /* USER CODE END PV */
- /** @addtogroup STM32_USB_OTG_DEVICE_LIBRARY
- * @brief Usb device.
- * @{
- */
- /** @addtogroup USBD_CUSTOM_HID
- * @{
- */
- /** @defgroup USBD_CUSTOM_HID_Private_TypesDefinitions USBD_CUSTOM_HID_Private_TypesDefinitions
- * @brief Private types.
- * @{
- */
- /* USER CODE BEGIN PRIVATE_TYPES */
- /* USER CODE END PRIVATE_TYPES */
- /**
- * @}
- */
- /** @defgroup USBD_CUSTOM_HID_Private_Defines USBD_CUSTOM_HID_Private_Defines
- * @brief Private defines.
- * @{
- */
- /* USER CODE BEGIN PRIVATE_DEFINES */
- /* USER CODE END PRIVATE_DEFINES */
- /**
- * @}
- */
- /** @defgroup USBD_CUSTOM_HID_Private_Macros USBD_CUSTOM_HID_Private_Macros
- * @brief Private macros.
- * @{
- */
- /* USER CODE BEGIN PRIVATE_MACRO */
- /* USER CODE END PRIVATE_MACRO */
- /**
- * @}
- */
- /** @defgroup USBD_CUSTOM_HID_Private_Variables USBD_CUSTOM_HID_Private_Variables
- * @brief Private variables.
- * @{
- */
- /** Usb HID report descriptor. */
- __ALIGN_BEGIN static uint8_t CUSTOM_HID_ReportDesc_FS[USBD_CUSTOM_HID_REPORT_DESC_SIZE] __ALIGN_END =
- {
- 0x05, 0x01, // USAGE_PAGE (Generic Desktop)
- 0x09, 0x06, // USAGE (Keyboard)
- 0xA1, 0x01, // COLLECTION (Application)
- 0x05, 0x07, // USAGE_PAGE (Keyboard)
- 0x19, 0xE0, // USAGE_MINIMUM (Keyboard LeftControl)
- 0x29, 0xE7, // USAGE_MAXIMUM (Keyboard Right GUI)
- 0x15, 0x00, // LOGICAL_MINIMUM (0)
- 0x25, 0x01, // LOGICAL_MAXIMUM (1)
- 0x75, 0x01, // REPORT_SIZE (1)
- 0x95, 0x08, // REPORT_COUNT (8)
- 0x81, 0x02, // INPUT (Data,Var,Abs)
- 0x95, 0x01, // REPORT_COUNT (1)
- 0x75, 0x08, // REPORT_SIZE (8)
- 0x81, 0x03, // INPUT (Cnst,Var,Abs)
- 0x95, 0x05, // REPORT_COUNT (5)
- 0x75, 0x01, // REPORT_SIZE (1)
- 0x05, 0x08, // USAGE_PAGE (LEDs)
- 0x19, 0x01, // USAGE_MINIMUM (Num Lock)
- 0x29, 0x05, // USAGE_MAXIMUM (Kana)
- 0x91, 0x02, // OUTPUT (Data,Var,Abs)
- 0x95, 0x01, // REPORT_COUNT (1)
- 0x75, 0x03, // REPORT_SIZE (3)
- 0x91, 0x03, // OUTPUT (Cnst,Var,Abs)
- 0x95, 0x06, // REPORT_COUNT (6)
- 0x75, 0x08, // REPORT_SIZE (8)
- 0x15, 0x00, // LOGICAL_MINIMUM (0)
- 0x25, 0x65, // LOGICAL_MAXIMUM (101)
- 0x05, 0x07, // USAGE_PAGE (Keyboard)
- 0x19, 0x00, // USAGE_MINIMUM (Reserved (no event indicated))
- 0x29, 0x65, // USAGE_MAXIMUM (Keyboard Application)
- 0x81, 0x00, // INPUT (Data,Ary,Abs)
- 0xC0 // END_COLLECTION
- };
- /* USER CODE BEGIN PRIVATE_VARIABLES */
- /* USER CODE END PRIVATE_VARIABLES */
- /**
- * @}
- */
- /** @defgroup USBD_CUSTOM_HID_Exported_Variables USBD_CUSTOM_HID_Exported_Variables
- * @brief Public variables.
- * @{
- */
- extern USBD_HandleTypeDef hUsbDeviceFS;
- /* USER CODE BEGIN EXPORTED_VARIABLES */
- /* USER CODE END EXPORTED_VARIABLES */
- /**
- * @}
- */
- /** @defgroup USBD_CUSTOM_HID_Private_FunctionPrototypes USBD_CUSTOM_HID_Private_FunctionPrototypes
- * @brief Private functions declaration.
- * @{
- */
- static int8_t CUSTOM_HID_Init_FS(void);
- static int8_t CUSTOM_HID_DeInit_FS(void);
- static int8_t CUSTOM_HID_OutEvent_FS(uint8_t event_idx, uint8_t state);
- /**
- * @}
- */
- USBD_CUSTOM_HID_ItfTypeDef USBD_CustomHID_fops_FS =
- {
- CUSTOM_HID_ReportDesc_FS,
- CUSTOM_HID_Init_FS,
- CUSTOM_HID_DeInit_FS,
- CUSTOM_HID_OutEvent_FS
- };
- /** @defgroup USBD_CUSTOM_HID_Private_Functions USBD_CUSTOM_HID_Private_Functions
- * @brief Private functions.
- * @{
- */
- /* Private functions ---------------------------------------------------------*/
- /**
- * @brief Initializes the CUSTOM HID media low layer
- * @retval USBD_OK if all operations are OK else USBD_FAIL
- */
- static int8_t CUSTOM_HID_Init_FS(void)
- {
- /* USER CODE BEGIN 4 */
- return (USBD_OK);
- /* USER CODE END 4 */
- }
- /**
- * @brief DeInitializes the CUSTOM HID media low layer
- * @retval USBD_OK if all operations are OK else USBD_FAIL
- */
- static int8_t CUSTOM_HID_DeInit_FS(void)
- {
- /* USER CODE BEGIN 5 */
- return (USBD_OK);
- /* USER CODE END 5 */
- }
- uint32_t size = 0;
- uint8_t buff[64];
- /**
- * @brief Manage the CUSTOM HID class events
- * @param event_idx: Event index
- * @param state: Event state
- * @retval USBD_OK if all operations are OK else USBD_FAIL
- */
- static int8_t CUSTOM_HID_OutEvent_FS(uint8_t event_idx, uint8_t state)
- {
- UNUSED(event_idx);
- UNUSED(state);
- size = USBD_LL_GetRxDataSize(&hUsbDeviceFS, CUSTOM_HID_EPOUT_ADDR); // 获取收到的数据长度
- USBD_CUSTOM_HID_HandleTypeDef *hhid = (USBD_CUSTOM_HID_HandleTypeDef *)(hUsbDeviceFS.pClassData);
- for(int i=0; i<size; i++)
- {
- buff[i]=hhid->Report_buf[i]; // 读取接收到的数据
- }
-
- USBD_CUSTOM_HID_SendReport(&hUsbDeviceFS, buff, size);
-
- // 开启下一次接收
- // if (USBD_CUSTOM_HID_ReceivePacket(&hUsbDeviceFS) != (uint8_t)USBD_OK)
- // {
- // return -1;
- // }
- return (USBD_OK);
- /* USER CODE END 6 */
- }
- /* USER CODE BEGIN 7 */
- /**
- * @brief Send the report to the Host
- * @param report: The report to be sent
- * @param len: The report length
- * @retval USBD_OK if all operations are OK else USBD_FAIL
- */
- /*
- static int8_t USBD_CUSTOM_HID_SendReport_FS(uint8_t *report, uint16_t len)
- {
- return USBD_CUSTOM_HID_SendReport(&hUsbDeviceFS, report, len);
- }
- */
- /* USER CODE END 7 */
- /* USER CODE BEGIN PRIVATE_FUNCTIONS_IMPLEMENTATION */
- /* USER CODE END PRIVATE_FUNCTIONS_IMPLEMENTATION */
- /**
- * @}
- */
- /**
- * @}
- */
- /**
- * @}
- */
|