1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
21
22
23
24
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
160
161
162
163
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
229
235
236
237
243
244
245
246
247
251
254
257
261
267
268
269
270
271
272
273
274
275
280
281
282
283
284
285
286
290
294
295
296
/* ... */
#include "usbd_customhid_if.h"
Includes
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
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,
0x09, 0x01,
0xa1, 0x01,
0x85, LED1_REPORT_ID,
0x09, 0x01,
0x15, 0x00,
0x25, 0x01,
0x75, 0x08,
0x95, LED1_REPORT_COUNT,
0xB1, 0x82,
0x85, LED1_REPORT_ID,
0x09, 0x01,
0x91, 0x82,
0x85, LED2_REPORT_ID,
0x09, 0x02,
0x15, 0x00,
0x25, 0x01,
0x75, 0x08,
0x95, LED2_REPORT_COUNT,
0xB1, 0x82,
0x85, LED2_REPORT_ID,
0x09, 0x02,
0x91, 0x82,
0x85, LED3_REPORT_ID,
0x09, 0x03,
0x15, 0x00,
0x25, 0x01,
0x75, 0x08,
0x95, LED3_REPORT_COUNT,
0xB1, 0x82,
0x85, LED3_REPORT_ID,
0x09, 0x03,
0x91, 0x82,
0x85, LED4_REPORT_ID,
0x09, 0x04,
0x15, 0x00,
0x25, 0x01,
0x75, 0x08,
0x95, LED4_REPORT_COUNT,
0xB1, 0x82,
0x85, LED4_REPORT_ID,
0x09, 0x04,
0x91, 0x82,
0x85, KEY_REPORT_ID,
0x09, 0x05,
0x15, 0x00,
0x25, 0x01,
0x75, 0x01,
0x81, 0x82,
0x09, 0x05,
0x75, 0x01,
0xb1, 0x82,
0x75, 0x07,
0x81, 0x83,
0x85, KEY_REPORT_ID,
0x75, 0x07,
0xb1, 0x83,
0x85, TAMPER_REPORT_ID,
0x09, 0x06,
0x15, 0x00,
0x25, 0x01,
0x75, 0x01,
0x81, 0x82,
0x09, 0x06,
0x75, 0x01,
0xb1, 0x82,
0x75, 0x07,
0x81, 0x83,
0x85, TAMPER_REPORT_ID,
0x75, 0x07,
0xb1, 0x83,
0x85, ADC_REPORT_ID,
0x09, 0x07,
0x15, 0x00,
0x26, 0xff, 0x00,
0x75, 0x08,
0x81, 0x82,
0x85, ADC_REPORT_ID,
0x09, 0x07,
0xb1, 0x82,
0xc0
...};
USBD_CUSTOM_HID_ItfTypeDef USBD_CustomHID_fops = {
CustomHID_ReportDesc,
CustomHID_Init,
CustomHID_DeInit,
CustomHID_OutEvent,
...};
Private variables
/* ... */
static int8_t CustomHID_Init(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
ADC_ChannelConfTypeDef sConfig;
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);
sConfig.Channel = ADCx_CHANNEL;
sConfig.Rank = 1;
sConfig.SamplingTime = ADC_SAMPLETIME_480CYCLES;
sConfig.Offset = 0;
HAL_ADC_ConfigChannel(&AdcHandle, &sConfig);
HAL_ADC_Start_DMA(&AdcHandle, (uint32_t *) & ADCConvertedValue, 1);
BSP_LED_Init(LED1);
BSP_LED_Init(LED2);
BSP_LED_Init(LED3);
BSP_LED_Init(LED4);
__HAL_RCC_GPIOC_CLK_ENABLE();
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);
HAL_NVIC_SetPriority(EXTI15_10_IRQn, 3, 0);
HAL_NVIC_EnableIRQ(EXTI15_10_IRQn);
return (0);
}{ ... }
/* ... */
static int8_t CustomHID_DeInit(void)
{
/* ... */
return (0);
}{ ... }
/* ... */
static int8_t CustomHID_OutEvent(uint8_t event_idx, uint8_t state)
{
switch (event_idx)
{
case 1:
(state == 1) ? BSP_LED_On(LED1) : BSP_LED_Off(LED1);
break;
case 1:
case 2:
(state == 1) ? BSP_LED_On(LED2) : BSP_LED_Off(LED2);
break;case 2:
case 3:
(state == 1) ? BSP_LED_On(LED3) : BSP_LED_Off(LED3);
break;case 3:
case 4:
(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) { ... }
USBD_CUSTOM_HID_ReceivePacket(&USBD_Device);
return (0);
}{ ... }
/* ... */
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) { ... }
}{ ... }