usbd_custom_hid_if.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  1. /* USER CODE BEGIN Header */
  2. /**
  3. ******************************************************************************
  4. * @file : usbd_custom_hid_if.c
  5. * @version : v2.0_Cube
  6. * @brief : USB Device Custom HID interface file.
  7. ******************************************************************************
  8. * @attention
  9. *
  10. * Copyright (c) 2024 STMicroelectronics.
  11. * All rights reserved.
  12. *
  13. * This software is licensed under terms that can be found in the LICENSE file
  14. * in the root directory of this software component.
  15. * If no LICENSE file comes with this software, it is provided AS-IS.
  16. *
  17. ******************************************************************************
  18. */
  19. /* USER CODE END Header */
  20. /* Includes ------------------------------------------------------------------*/
  21. #include "usbd_custom_hid_if.h"
  22. /* USER CODE BEGIN INCLUDE */
  23. #define LED1_REPORT_ID 0x01
  24. #define LED1_REPORT_COUNT 0x01
  25. #define LED2_REPORT_ID 0x02
  26. #define LED2_REPORT_COUNT 0x01
  27. #define LED3_REPORT_ID 0x03
  28. #define LED3_REPORT_COUNT 0x01
  29. #define LED4_REPORT_ID 0x04
  30. #define LED4_REPORT_COUNT 0x01
  31. #define KEY_REPORT_ID 0x05
  32. #define TAMPER_REPORT_ID 0x06
  33. #define ADC_REPORT_ID 0x07
  34. /* USER CODE END INCLUDE */
  35. /* Private typedef -----------------------------------------------------------*/
  36. /* Private define ------------------------------------------------------------*/
  37. /* Private macro -------------------------------------------------------------*/
  38. /* USER CODE BEGIN PV */
  39. /* Private variables ---------------------------------------------------------*/
  40. uint8_t HID_state;
  41. uint8_t HID_Buffer[64];
  42. /* USER CODE END PV */
  43. /** @addtogroup STM32_USB_OTG_DEVICE_LIBRARY
  44. * @brief Usb device.
  45. * @{
  46. */
  47. /** @addtogroup USBD_CUSTOM_HID
  48. * @{
  49. */
  50. /** @defgroup USBD_CUSTOM_HID_Private_TypesDefinitions USBD_CUSTOM_HID_Private_TypesDefinitions
  51. * @brief Private types.
  52. * @{
  53. */
  54. /* USER CODE BEGIN PRIVATE_TYPES */
  55. /* USER CODE END PRIVATE_TYPES */
  56. /**
  57. * @}
  58. */
  59. /** @defgroup USBD_CUSTOM_HID_Private_Defines USBD_CUSTOM_HID_Private_Defines
  60. * @brief Private defines.
  61. * @{
  62. */
  63. /* USER CODE BEGIN PRIVATE_DEFINES */
  64. /* USER CODE END PRIVATE_DEFINES */
  65. /**
  66. * @}
  67. */
  68. /** @defgroup USBD_CUSTOM_HID_Private_Macros USBD_CUSTOM_HID_Private_Macros
  69. * @brief Private macros.
  70. * @{
  71. */
  72. /* USER CODE BEGIN PRIVATE_MACRO */
  73. /* USER CODE END PRIVATE_MACRO */
  74. /**
  75. * @}
  76. */
  77. /** @defgroup USBD_CUSTOM_HID_Private_Variables USBD_CUSTOM_HID_Private_Variables
  78. * @brief Private variables.
  79. * @{
  80. */
  81. /** Usb HID report descriptor. */
  82. __ALIGN_BEGIN static uint8_t CUSTOM_HID_ReportDesc_FS[USBD_CUSTOM_HID_REPORT_DESC_SIZE] __ALIGN_END =
  83. {
  84. 0x06, 0x00, 0xFF, // Usage Page (Vendor Defined 0xFF00)
  85. 0x09, 0x00, // Usage (0x00)
  86. 0xA1, 0x01, // Collection (Application)
  87. 0x09, 0x01, // Usage (0x01)
  88. 0x15, 0x00, // Logical Minimum (0)
  89. 0x26, 0xFF, 0x00, // Logical Maximum (255)
  90. 0x75, 0x08, // Report Size (8)
  91. 0x95, 0x40, // Report Count (64)
  92. 0x81, 0x00, // Input (Data,Array,Abs,No Wrap,Linear,Preferred State,No Null Position)
  93. 0x09, 0x02, // Usage (0x02)
  94. 0x15, 0x00, // Logical Minimum (0)
  95. 0x26, 0xFF, 0x00, // Logical Maximum (255)
  96. 0x75, 0x08, // Report Size (8)
  97. 0x95, 0x40, // Report Count (64)
  98. 0x91, 0x00, // Output (Data,Array,Abs,No Wrap,Linear,Preferred State,No Null Position,Non-volatile)
  99. 0xC0, // End Collection
  100. // 34 bytes
  101. };
  102. /* USER CODE BEGIN PRIVATE_VARIABLES */
  103. /* USER CODE END PRIVATE_VARIABLES */
  104. /**
  105. * @}
  106. */
  107. /** @defgroup USBD_CUSTOM_HID_Exported_Variables USBD_CUSTOM_HID_Exported_Variables
  108. * @brief Public variables.
  109. * @{
  110. */
  111. extern USBD_HandleTypeDef hUsbDeviceFS;
  112. /* USER CODE BEGIN EXPORTED_VARIABLES */
  113. /* USER CODE END EXPORTED_VARIABLES */
  114. /**
  115. * @}
  116. */
  117. /** @defgroup USBD_CUSTOM_HID_Private_FunctionPrototypes USBD_CUSTOM_HID_Private_FunctionPrototypes
  118. * @brief Private functions declaration.
  119. * @{
  120. */
  121. static int8_t CUSTOM_HID_Init_FS(void);
  122. static int8_t CUSTOM_HID_DeInit_FS(void);
  123. static int8_t CUSTOM_HID_OutEvent_FS(uint8_t event_idx, uint8_t state);
  124. /**
  125. * @}
  126. */
  127. USBD_CUSTOM_HID_ItfTypeDef USBD_CustomHID_fops_FS =
  128. {
  129. CUSTOM_HID_ReportDesc_FS,
  130. CUSTOM_HID_Init_FS,
  131. CUSTOM_HID_DeInit_FS,
  132. CUSTOM_HID_OutEvent_FS
  133. };
  134. /** @defgroup USBD_CUSTOM_HID_Private_Functions USBD_CUSTOM_HID_Private_Functions
  135. * @brief Private functions.
  136. * @{
  137. */
  138. /* Private functions ---------------------------------------------------------*/
  139. /**
  140. * @brief Initializes the CUSTOM HID media low layer
  141. * @retval USBD_OK if all operations are OK else USBD_FAIL
  142. */
  143. static int8_t CUSTOM_HID_Init_FS(void)
  144. {
  145. /* USER CODE BEGIN 4 */
  146. return (USBD_OK);
  147. /* USER CODE END 4 */
  148. }
  149. /**
  150. * @brief DeInitializes the CUSTOM HID media low layer
  151. * @retval USBD_OK if all operations are OK else USBD_FAIL
  152. */
  153. static int8_t CUSTOM_HID_DeInit_FS(void)
  154. {
  155. /* USER CODE BEGIN 5 */
  156. return (USBD_OK);
  157. /* USER CODE END 5 */
  158. }
  159. uint32_t size = 0;
  160. uint8_t buff[64];
  161. /**
  162. * @brief Manage the CUSTOM HID class events
  163. * @param event_idx: Event index
  164. * @param state: Event state
  165. * @retval USBD_OK if all operations are OK else USBD_FAIL
  166. */
  167. static int8_t CUSTOM_HID_OutEvent_FS(uint8_t event_idx, uint8_t state)
  168. {
  169. UNUSED(event_idx);
  170. UNUSED(state);
  171. size = USBD_LL_GetRxDataSize(&hUsbDeviceFS, CUSTOM_HID_EPOUT_ADDR); // 获取收到的数据长度
  172. USBD_CUSTOM_HID_HandleTypeDef *hhid = (USBD_CUSTOM_HID_HandleTypeDef *)(hUsbDeviceFS.pClassData);
  173. for(int i=0; i<size; i++)
  174. {
  175. buff[i]=hhid->Report_buf[i]; // 读取接收到的数据
  176. }
  177. USBD_CUSTOM_HID_SendReport(&hUsbDeviceFS, buff, size);
  178. /* USER CODE BEGIN 6 */
  179. return (USBD_OK);
  180. /* USER CODE END 6 */
  181. }
  182. /* USER CODE BEGIN 7 */
  183. /**
  184. * @brief Send the report to the Host
  185. * @param report: The report to be sent
  186. * @param len: The report length
  187. * @retval USBD_OK if all operations are OK else USBD_FAIL
  188. */
  189. /*
  190. static int8_t USBD_CUSTOM_HID_SendReport_FS(uint8_t *report, uint16_t len)
  191. {
  192. return USBD_CUSTOM_HID_SendReport(&hUsbDeviceFS, report, len);
  193. }
  194. */
  195. /* USER CODE END 7 */
  196. /* USER CODE BEGIN PRIVATE_FUNCTIONS_IMPLEMENTATION */
  197. /* USER CODE END PRIVATE_FUNCTIONS_IMPLEMENTATION */
  198. /**
  199. * @}
  200. */
  201. /**
  202. * @}
  203. */
  204. /**
  205. * @}
  206. */