Select one of the symbols to view example projects that use it.
 
Outline
Includes
#include "usbd_customhid_if.h"
Private function prototypes
Private variables
AdcHandle
ADCConvertedValue
ADC_Prev_ConvertedValue
SendBuffer
CustomHID_ReportDesc
USBD_CustomHID_fops
CustomHID_Init()
CustomHID_DeInit()
CustomHID_OutEvent(uint8_t, uint8_t)
HAL_GPIO_EXTI_Callback(uint16_t)
Files
loading...
CodeScopeSTM32 Libraries and SamplesCustomHID_StandaloneSrc/usbd_customhid_if.c
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
/** ****************************************************************************** * @file USB_Device/CustomHID_Standalone/Src/usbd_customhid_if.c * @author MCD Application Team * @brief USB Device Custom HID interface file. ****************************************************************************** * @attention * * Copyright (c) 2017 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. * ****************************************************************************** *//* ... */ /* Includes ------------------------------------------------------------------ */ #include "usbd_customhid_if.h" Includes /* Private typedef ----------------------------------------------------------- */ /* Private define ------------------------------------------------------------ */ /* Private macro ------------------------------------------------------------- */ /* Private function prototypes ----------------------------------------------- */ static int8_t CustomHID_Init(void); static int8_t CustomHID_DeInit(void); static int8_t CustomHID_OutEvent(uint8_t event_idx, uint8_t state); Private function prototypes /* Private variables --------------------------------------------------------- */ ADC_HandleTypeDef AdcHandle; uint32_t ADCConvertedValue = 0; uint32_t ADC_Prev_ConvertedValue = 0; uint8_t SendBuffer[2]; extern USBD_HandleTypeDef USBD_Device; __ALIGN_BEGIN static uint8_t CustomHID_ReportDesc[USBD_CUSTOM_HID_REPORT_DESC_SIZE] __ALIGN_END = { 0x06, 0xFF, 0x00, /* USAGE_PAGE (Vendor Page: 0xFF00) */ 0x09, 0x01, /* USAGE (Demo Kit) */ 0xa1, 0x01, /* COLLECTION (Application) */ /* 6 */ /* LED1 */ 0x85, LED1_REPORT_ID, /* REPORT_ID (1) */ 0x09, 0x01, /* USAGE (LED 1) */ 0x15, 0x00, /* LOGICAL_MINIMUM (0) */ 0x25, 0x01, /* LOGICAL_MAXIMUM (1) */ 0x75, 0x08, /* REPORT_SIZE (8) */ 0x95, LED1_REPORT_COUNT, /* REPORT_COUNT (1) */ 0xB1, 0x82, /* FEATURE (Data,Var,Abs,Vol) */ 0x85, LED1_REPORT_ID, /* REPORT_ID (1) */ 0x09, 0x01, /* USAGE (LED 1) */ 0x91, 0x82, /* OUTPUT (Data,Var,Abs,Vol) */ /* 26 */ /* LED2 */ 0x85, LED2_REPORT_ID, /* REPORT_ID 2 */ 0x09, 0x02, /* USAGE (LED 2) */ 0x15, 0x00, /* LOGICAL_MINIMUM (0) */ 0x25, 0x01, /* LOGICAL_MAXIMUM (1) */ 0x75, 0x08, /* REPORT_SIZE (8) */ 0x95, LED2_REPORT_COUNT, /* REPORT_COUNT (1) */ 0xB1, 0x82, /* FEATURE (Data,Var,Abs,Vol) */ 0x85, LED2_REPORT_ID, /* REPORT_ID (2) */ 0x09, 0x02, /* USAGE (LED 2) */ 0x91, 0x82, /* OUTPUT (Data,Var,Abs,Vol) */ /* 46 */ /* LED3 */ 0x85, LED3_REPORT_ID, /* REPORT_ID (3) */ 0x09, 0x03, /* USAGE (LED 3) */ 0x15, 0x00, /* LOGICAL_MINIMUM (0) */ 0x25, 0x01, /* LOGICAL_MAXIMUM (1) */ 0x75, 0x08, /* REPORT_SIZE (8) */ 0x95, LED3_REPORT_COUNT, /* REPORT_COUNT (1) */ 0xB1, 0x82, /* FEATURE (Data,Var,Abs,Vol) */ 0x85, LED3_REPORT_ID, /* REPORT_ID (3) */ 0x09, 0x03, /* USAGE (LED 3) */ 0x91, 0x82, /* OUTPUT (Data,Var,Abs,Vol) */ /* 66 */ /* LED4 */ 0x85, LED4_REPORT_ID, /* REPORT_ID 4) */ 0x09, 0x04, /* USAGE (LED 4) */ 0x15, 0x00, /* LOGICAL_MINIMUM (0) */ 0x25, 0x01, /* LOGICAL_MAXIMUM (1) */ 0x75, 0x08, /* REPORT_SIZE (8) */ 0x95, LED4_REPORT_COUNT, /* REPORT_COUNT (1) */ 0xB1, 0x82, /* FEATURE (Data,Var,Abs,Vol) */ 0x85, LED4_REPORT_ID, /* REPORT_ID (4) */ 0x09, 0x04, /* USAGE (LED 4) */ 0x91, 0x82, /* OUTPUT (Data,Var,Abs,Vol) */ /* 86 */ /* key Push Button */ 0x85, KEY_REPORT_ID, /* REPORT_ID (5) */ 0x09, 0x05, /* USAGE (Push Button) */ 0x15, 0x00, /* LOGICAL_MINIMUM (0) */ 0x25, 0x01, /* LOGICAL_MAXIMUM (1) */ 0x75, 0x01, /* REPORT_SIZE (1) */ 0x81, 0x82, /* INPUT (Data,Var,Abs,Vol) */ 0x09, 0x05, /* USAGE (Push Button) */ 0x75, 0x01, /* REPORT_SIZE (1) */ 0xb1, 0x82, /* FEATURE (Data,Var,Abs,Vol) */ 0x75, 0x07, /* REPORT_SIZE (7) */ 0x81, 0x83, /* INPUT (Cnst,Var,Abs,Vol) */ 0x85, KEY_REPORT_ID, /* REPORT_ID (2) */ 0x75, 0x07, /* REPORT_SIZE (7) */ 0xb1, 0x83, /* FEATURE (Cnst,Var,Abs,Vol) */ /* 114 */ /* Tamper Push Button */ 0x85, TAMPER_REPORT_ID, /* REPORT_ID (6) */ 0x09, 0x06, /* USAGE (Tamper Push Button) */ 0x15, 0x00, /* LOGICAL_MINIMUM (0) */ 0x25, 0x01, /* LOGICAL_MAXIMUM (1) */ 0x75, 0x01, /* REPORT_SIZE (1) */ 0x81, 0x82, /* INPUT (Data,Var,Abs,Vol) */ 0x09, 0x06, /* USAGE (Tamper Push Button) */ 0x75, 0x01, /* REPORT_SIZE (1) */ 0xb1, 0x82, /* FEATURE (Data,Var,Abs,Vol) */ 0x75, 0x07, /* REPORT_SIZE (7) */ 0x81, 0x83, /* INPUT (Cnst,Var,Abs,Vol) */ 0x85, TAMPER_REPORT_ID, /* REPORT_ID (6) */ 0x75, 0x07, /* REPORT_SIZE (7) */ 0xb1, 0x83, /* FEATURE (Cnst,Var,Abs,Vol) */ /* 142 */ /* ADC IN */ 0x85, ADC_REPORT_ID, /* REPORT_ID */ 0x09, 0x07, /* USAGE (ADC IN) */ 0x15, 0x00, /* LOGICAL_MINIMUM (0) */ 0x26, 0xff, 0x00, /* LOGICAL_MAXIMUM (255) */ 0x75, 0x08, /* REPORT_SIZE (8) */ 0x81, 0x82, /* INPUT (Data,Var,Abs,Vol) */ 0x85, ADC_REPORT_ID, /* REPORT_ID (7) */ 0x09, 0x07, /* USAGE (ADC in) */ 0xb1, 0x82, /* FEATURE (Data,Var,Abs,Vol) */ /* 161 */ 0xc0 /* END_COLLECTION */ ...}; USBD_CUSTOM_HID_ItfTypeDef USBD_CustomHID_fops = { CustomHID_ReportDesc, CustomHID_Init, CustomHID_DeInit, CustomHID_OutEvent, ...}; Private variables /* Private functions --------------------------------------------------------- */ /** * @brief CustomHID_Init * Initializes the CUSTOM HID media low layer * @param None * @retval Result of the operation: USBD_OK if all operations are OK else USBD_FAIL *//* ... */ static int8_t CustomHID_Init(void) { GPIO_InitTypeDef GPIO_InitStructure; ADC_ChannelConfTypeDef sConfig; /* Configure the ADC peripheral */ AdcHandle.Instance = ADCx; AdcHandle.Init.ClockPrescaler = ADC_CLOCKPRESCALER_PCLK_DIV4; AdcHandle.Init.Resolution = ADC_RESOLUTION_12B; AdcHandle.Init.ScanConvMode = DISABLE; AdcHandle.Init.ContinuousConvMode = ENABLE; AdcHandle.Init.DiscontinuousConvMode = DISABLE; AdcHandle.Init.NbrOfDiscConversion = 0; AdcHandle.Init.ExternalTrigConvEdge = ADC_EXTERNALTRIGCONVEDGE_NONE; AdcHandle.Init.ExternalTrigConv = ADC_EXTERNALTRIGCONV_T1_CC1; AdcHandle.Init.DataAlign = ADC_DATAALIGN_RIGHT; AdcHandle.Init.NbrOfConversion = 1; AdcHandle.Init.DMAContinuousRequests = ENABLE; AdcHandle.Init.EOCSelection = DISABLE; HAL_ADC_Init(&AdcHandle); /* Configure ADC regular channel */ sConfig.Channel = ADCx_CHANNEL; sConfig.Rank = 1; sConfig.SamplingTime = ADC_SAMPLETIME_480CYCLES; sConfig.Offset = 0; HAL_ADC_ConfigChannel(&AdcHandle, &sConfig); /* Start the conversion process and enable interrupt */ HAL_ADC_Start_DMA(&AdcHandle, (uint32_t *) & ADCConvertedValue, 1); /* Configure LED1, LED2, LED3 and LED4 */ BSP_LED_Init(LED1); BSP_LED_Init(LED2); BSP_LED_Init(LED3); BSP_LED_Init(LED4); /* Enable GPIOC clock */ __HAL_RCC_GPIOC_CLK_ENABLE(); /* Configure PG15 pin as input floating */ GPIO_InitStructure.Mode = GPIO_MODE_IT_RISING_FALLING; GPIO_InitStructure.Pull = GPIO_NOPULL; GPIO_InitStructure.Pin = GPIO_PIN_13; HAL_GPIO_Init(GPIOC, &GPIO_InitStructure); /* Enable and set EXTI15_10 Interrupt to the lowest priority */ HAL_NVIC_SetPriority(EXTI15_10_IRQn, 3, 0); HAL_NVIC_EnableIRQ(EXTI15_10_IRQn); return (0); }{ ... } /** * @brief CustomHID_DeInit * DeInitializes the CUSTOM HID media low layer * @param None * @retval Result of the operation: USBD_OK if all operations are OK else USBD_FAIL *//* ... */ static int8_t CustomHID_DeInit(void) { /* * Add your deinitialization code here *//* ... */ return (0); }{ ... } /** * @brief CustomHID_OutEvent * Manage the CUSTOM HID class Out Event * @param event_idx: LED Report Number * @param state: LED states (ON/OFF) *//* ... */ static int8_t CustomHID_OutEvent(uint8_t event_idx, uint8_t state) { switch (event_idx) { case 1: /* LED1 */ (state == 1) ? BSP_LED_On(LED1) : BSP_LED_Off(LED1); break; case 1: case 2: /* LED2 */ (state == 1) ? BSP_LED_On(LED2) : BSP_LED_Off(LED2); break;case 2: case 3: /* LED3 */ (state == 1) ? BSP_LED_On(LED3) : BSP_LED_Off(LED3); break;case 3: case 4: /* LED4 */ (state == 1) ? BSP_LED_On(LED4) : BSP_LED_Off(LED4); break; case 4: default: BSP_LED_Off(LED1); BSP_LED_Off(LED2); BSP_LED_Off(LED3); BSP_LED_Off(LED4); break;default }switch (event_idx) { ... } /* Start next USB packet transfer once data processing is completed */ USBD_CUSTOM_HID_ReceivePacket(&USBD_Device); return (0); }{ ... } /** * @brief EXTI line detection callbacks * @param GPIO_Pin: Specifies the pins connected EXTI line * @retval None *//* ... */ void HAL_GPIO_EXTI_Callback(uint16_t GPIO_Pin) { if (GPIO_Pin == KEY_BUTTON_PIN) { SendBuffer[0] = KEY_REPORT_ID; if (BSP_PB_GetState(BUTTON_KEY) == GPIO_PIN_RESET) { SendBuffer[1] = 0x01; }if (BSP_PB_GetState(BUTTON_KEY) == GPIO_PIN_RESET) { ... } else { SendBuffer[1] = 0x00; }else { ... } USBD_CUSTOM_HID_SendReport(&USBD_Device, SendBuffer, 2); }if (GPIO_Pin == KEY_BUTTON_PIN) { ... } }{ ... }