usbd_custom_hid_if.c 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  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. 0x05, 0x01, // USAGE_PAGE (Generic Desktop)
  85. 0x09, 0x06, // USAGE (Keyboard)
  86. 0xA1, 0x01, // COLLECTION (Application)
  87. 0x05, 0x07, // USAGE_PAGE (Keyboard)
  88. 0x19, 0xE0, // USAGE_MINIMUM (Keyboard LeftControl)
  89. 0x29, 0xE7, // USAGE_MAXIMUM (Keyboard Right GUI)
  90. 0x15, 0x00, // LOGICAL_MINIMUM (0)
  91. 0x25, 0x01, // LOGICAL_MAXIMUM (1)
  92. 0x75, 0x01, // REPORT_SIZE (1)
  93. 0x95, 0x08, // REPORT_COUNT (8)
  94. 0x81, 0x02, // INPUT (Data,Var,Abs)
  95. 0x95, 0x01, // REPORT_COUNT (1)
  96. 0x75, 0x08, // REPORT_SIZE (8)
  97. 0x81, 0x03, // INPUT (Cnst,Var,Abs)
  98. 0x95, 0x05, // REPORT_COUNT (5)
  99. 0x75, 0x01, // REPORT_SIZE (1)
  100. 0x05, 0x08, // USAGE_PAGE (LEDs)
  101. 0x19, 0x01, // USAGE_MINIMUM (Num Lock)
  102. 0x29, 0x05, // USAGE_MAXIMUM (Kana)
  103. 0x91, 0x02, // OUTPUT (Data,Var,Abs)
  104. 0x95, 0x01, // REPORT_COUNT (1)
  105. 0x75, 0x03, // REPORT_SIZE (3)
  106. 0x91, 0x03, // OUTPUT (Cnst,Var,Abs)
  107. 0x95, 0x06, // REPORT_COUNT (6)
  108. 0x75, 0x08, // REPORT_SIZE (8)
  109. 0x15, 0x00, // LOGICAL_MINIMUM (0)
  110. 0x25, 0x65, // LOGICAL_MAXIMUM (101)
  111. 0x05, 0x07, // USAGE_PAGE (Keyboard)
  112. 0x19, 0x00, // USAGE_MINIMUM (Reserved (no event indicated))
  113. 0x29, 0x65, // USAGE_MAXIMUM (Keyboard Application)
  114. 0x81, 0x00, // INPUT (Data,Ary,Abs)
  115. 0xC0 // END_COLLECTION
  116. };
  117. /* USER CODE BEGIN PRIVATE_VARIABLES */
  118. /* USER CODE END PRIVATE_VARIABLES */
  119. /**
  120. * @}
  121. */
  122. /** @defgroup USBD_CUSTOM_HID_Exported_Variables USBD_CUSTOM_HID_Exported_Variables
  123. * @brief Public variables.
  124. * @{
  125. */
  126. extern USBD_HandleTypeDef hUsbDeviceFS;
  127. /* USER CODE BEGIN EXPORTED_VARIABLES */
  128. /* USER CODE END EXPORTED_VARIABLES */
  129. /**
  130. * @}
  131. */
  132. /** @defgroup USBD_CUSTOM_HID_Private_FunctionPrototypes USBD_CUSTOM_HID_Private_FunctionPrototypes
  133. * @brief Private functions declaration.
  134. * @{
  135. */
  136. static int8_t CUSTOM_HID_Init_FS(void);
  137. static int8_t CUSTOM_HID_DeInit_FS(void);
  138. static int8_t CUSTOM_HID_OutEvent_FS(uint8_t event_idx, uint8_t state);
  139. /**
  140. * @}
  141. */
  142. USBD_CUSTOM_HID_ItfTypeDef USBD_CustomHID_fops_FS =
  143. {
  144. CUSTOM_HID_ReportDesc_FS,
  145. CUSTOM_HID_Init_FS,
  146. CUSTOM_HID_DeInit_FS,
  147. CUSTOM_HID_OutEvent_FS
  148. };
  149. /** @defgroup USBD_CUSTOM_HID_Private_Functions USBD_CUSTOM_HID_Private_Functions
  150. * @brief Private functions.
  151. * @{
  152. */
  153. /* Private functions ---------------------------------------------------------*/
  154. /**
  155. * @brief Initializes the CUSTOM HID media low layer
  156. * @retval USBD_OK if all operations are OK else USBD_FAIL
  157. */
  158. static int8_t CUSTOM_HID_Init_FS(void)
  159. {
  160. /* USER CODE BEGIN 4 */
  161. return (USBD_OK);
  162. /* USER CODE END 4 */
  163. }
  164. /**
  165. * @brief DeInitializes the CUSTOM HID media low layer
  166. * @retval USBD_OK if all operations are OK else USBD_FAIL
  167. */
  168. static int8_t CUSTOM_HID_DeInit_FS(void)
  169. {
  170. /* USER CODE BEGIN 5 */
  171. return (USBD_OK);
  172. /* USER CODE END 5 */
  173. }
  174. uint32_t size = 0;
  175. uint8_t buff[64];
  176. /**
  177. * @brief Manage the CUSTOM HID class events
  178. * @param event_idx: Event index
  179. * @param state: Event state
  180. * @retval USBD_OK if all operations are OK else USBD_FAIL
  181. */
  182. static int8_t CUSTOM_HID_OutEvent_FS(uint8_t event_idx, uint8_t state)
  183. {
  184. UNUSED(event_idx);
  185. UNUSED(state);
  186. size = USBD_LL_GetRxDataSize(&hUsbDeviceFS, CUSTOM_HID_EPOUT_ADDR); // 获取收到的数据长度
  187. USBD_CUSTOM_HID_HandleTypeDef *hhid = (USBD_CUSTOM_HID_HandleTypeDef *)(hUsbDeviceFS.pClassData);
  188. for(int i=0; i<size; i++)
  189. {
  190. buff[i]=hhid->Report_buf[i]; // 读取接收到的数据
  191. }
  192. USBD_CUSTOM_HID_SendReport(&hUsbDeviceFS, buff, size);
  193. // 开启下一次接收
  194. // if (USBD_CUSTOM_HID_ReceivePacket(&hUsbDeviceFS) != (uint8_t)USBD_OK)
  195. // {
  196. // return -1;
  197. // }
  198. return (USBD_OK);
  199. /* USER CODE END 6 */
  200. }
  201. /* USER CODE BEGIN 7 */
  202. /**
  203. * @brief Send the report to the Host
  204. * @param report: The report to be sent
  205. * @param len: The report length
  206. * @retval USBD_OK if all operations are OK else USBD_FAIL
  207. */
  208. /*
  209. static int8_t USBD_CUSTOM_HID_SendReport_FS(uint8_t *report, uint16_t len)
  210. {
  211. return USBD_CUSTOM_HID_SendReport(&hUsbDeviceFS, report, len);
  212. }
  213. */
  214. /* USER CODE END 7 */
  215. /* USER CODE BEGIN PRIVATE_FUNCTIONS_IMPLEMENTATION */
  216. /* USER CODE END PRIVATE_FUNCTIONS_IMPLEMENTATION */
  217. /**
  218. * @}
  219. */
  220. /**
  221. * @}
  222. */
  223. /**
  224. * @}
  225. */