Skip to content

Commit eb39341

Browse files
author
Yury Vostrenkov
committed
Dynamic HID descriptor for all modes
1 parent 555285d commit eb39341

File tree

10 files changed

+120
-56
lines changed

10 files changed

+120
-56
lines changed

Inc/common_types.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -245,12 +245,12 @@ typedef struct
245245
{
246246
uint8_t dummy;
247247
uint8_t id;
248-
uint8_t button_data[MAX_BUTTONS_NUM/8];
249-
int16_t axis_data[MAX_AXIS_NUM];
250-
uint8_t pov_data[MAX_POVS_NUM];
251248
int16_t raw_axis_data[MAX_AXIS_NUM];
252249
uint8_t raw_button_data[9];
253-
uint8_t shift_button_data;
250+
uint8_t shift_button_data;
251+
int16_t axis_data[MAX_AXIS_NUM];
252+
uint8_t pov_data[MAX_POVS_NUM];
253+
uint8_t button_data[MAX_BUTTONS_NUM/8];
254254

255255
} joy_report_t;
256256

Inc/usb_desc.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@
5757

5858
#define CUSTOMHID_SIZ_DEVICE_DESC 18
5959
#define CUSTOMHID_SIZ_CONFIG_DESC 41
60-
#define CUSTOMHID_SIZ_REPORT_DESC 175
60+
#define CUSTOMHID_SIZ_REPORT_DESC 178
6161
#define CUSTOMHID_SIZ_STRING_LANGID 4
6262
#define CUSTOMHID_SIZ_STRING_VENDOR 38
6363
#define CUSTOMHID_SIZ_STRING_PRODUCT 32

MDK-ARM/FreeJoy.bin

136 Bytes
Binary file not shown.

MDK-ARM/FreeJoy.uvoptx

Lines changed: 36 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,24 @@
148148
<Name>UL2CM3(-S0 -C0 -P0 -FD20000000 -FC1000 -FN1 -FF0STM32F10x_128 -FS08000000 -FL020000 -FP0($$Device:STM32F103C8$Flash\STM32F10x_128.FLM))</Name>
149149
</SetRegEntry>
150150
</TargetDriverDllRegistry>
151-
<Breakpoint/>
151+
<Breakpoint>
152+
<Bp>
153+
<Number>0</Number>
154+
<Type>0</Type>
155+
<LineNumber>150</LineNumber>
156+
<EnabledFlag>1</EnabledFlag>
157+
<Address>134223878</Address>
158+
<ByteObject>0</ByteObject>
159+
<HtxType>0</HtxType>
160+
<ManyObjects>0</ManyObjects>
161+
<SizeOfObject>0</SizeOfObject>
162+
<BreakByAccess>0</BreakByAccess>
163+
<BreakIfRCount>1</BreakIfRCount>
164+
<Filename>..\Src\usb_prop.c</Filename>
165+
<ExecCommand></ExecCommand>
166+
<Expression>\\FreeJoy\../Src/usb_prop.c\150</Expression>
167+
</Bp>
168+
</Breakpoint>
152169
<WatchWindow1>
153170
<Ww>
154171
<count>0</count>
@@ -363,7 +380,24 @@
363380
<Name>UL2CM3(-S0 -C0 -P0 -FD20000000 -FC1000 -FN1 -FF0STM32F10x_128 -FS08000000 -FL020000 -FP0($$Device:STM32F103C8$Flash\STM32F10x_128.FLM))</Name>
364381
</SetRegEntry>
365382
</TargetDriverDllRegistry>
366-
<Breakpoint/>
383+
<Breakpoint>
384+
<Bp>
385+
<Number>0</Number>
386+
<Type>0</Type>
387+
<LineNumber>150</LineNumber>
388+
<EnabledFlag>1</EnabledFlag>
389+
<Address>0</Address>
390+
<ByteObject>0</ByteObject>
391+
<HtxType>0</HtxType>
392+
<ManyObjects>0</ManyObjects>
393+
<SizeOfObject>0</SizeOfObject>
394+
<BreakByAccess>0</BreakByAccess>
395+
<BreakIfRCount>0</BreakIfRCount>
396+
<Filename>..\Src\usb_prop.c</Filename>
397+
<ExecCommand></ExecCommand>
398+
<Expression></Expression>
399+
</Bp>
400+
</Breakpoint>
367401
<MemoryWindow1>
368402
<Mm>
369403
<WinNumber>1</WinNumber>

Src/analog.c

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -516,6 +516,8 @@ void AxesProcess (dev_config_t * p_dev_config)
516516
}
517517
}
518518

519+
// prevent not atomic read
520+
NVIC_DisableIRQ(TIM1_UP_IRQn);
519521
for (uint8_t i=0; i<MAX_AXIS_NUM; i++)
520522
{
521523
// Multi-axis process
@@ -552,7 +554,8 @@ void AxesProcess (dev_config_t * p_dev_config)
552554
else out_axis_data[i] = 0;
553555

554556
}
555-
557+
// resume IRQ
558+
NVIC_EnableIRQ(TIM1_UP_IRQn);
556559
}
557560

558561
/**

Src/buttons.c

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -654,8 +654,10 @@ void ButtonsReadLogical (dev_config_t * p_dev_config)
654654
}
655655
}
656656

657-
// convert data to report format
658657

658+
// prevent not atomic read
659+
NVIC_DisableIRQ(TIM1_UP_IRQn);
660+
// convert data to report format
659661
uint8_t k = 0;
660662
for (int i=0;i<MAX_BUTTONS_NUM;i++)
661663
{
@@ -699,7 +701,8 @@ void ButtonsReadLogical (dev_config_t * p_dev_config)
699701

700702
if (!is_hidden)
701703
{
702-
buttons_data[(k++ & 0xF8)>>3] |= (buttons_state[i].current_state << (i & 0x07));
704+
buttons_data[(k & 0xF8)>>3] |= (buttons_state[i].current_state << (k & 0x07));
705+
k++;
703706
}
704707
}
705708

@@ -737,7 +740,8 @@ void ButtonsReadLogical (dev_config_t * p_dev_config)
737740
break;
738741
}
739742
}
740-
743+
// resume IRQ
744+
NVIC_EnableIRQ(TIM1_UP_IRQn);
741745
}
742746

743747
/**

Src/config.c

Lines changed: 23 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -71,19 +71,35 @@ void AppConfigInit (dev_config_t * p_dev_config)
7171
if (p_dev_config->buttons[i].type == POV1_DOWN ||
7272
p_dev_config->buttons[i].type == POV1_UP ||
7373
p_dev_config->buttons[i].type == POV1_LEFT ||
74-
p_dev_config->buttons[i].type == POV1_RIGHT ||
75-
p_dev_config->buttons[i].type == POV2_DOWN ||
74+
p_dev_config->buttons[i].type == POV1_RIGHT)
75+
{
76+
app_config.povs |= 0x01;
77+
continue;
78+
}
79+
if (p_dev_config->buttons[i].type == POV2_DOWN ||
7680
p_dev_config->buttons[i].type == POV2_UP ||
7781
p_dev_config->buttons[i].type == POV2_LEFT ||
78-
p_dev_config->buttons[i].type == POV2_RIGHT ||
79-
p_dev_config->buttons[i].type == POV3_DOWN ||
82+
p_dev_config->buttons[i].type == POV2_RIGHT)
83+
{
84+
app_config.povs |= 0x02;
85+
continue;
86+
}
87+
if (p_dev_config->buttons[i].type == POV3_DOWN ||
8088
p_dev_config->buttons[i].type == POV3_UP ||
8189
p_dev_config->buttons[i].type == POV3_LEFT ||
82-
p_dev_config->buttons[i].type == POV3_RIGHT ||
83-
p_dev_config->buttons[i].type == POV4_DOWN ||
90+
p_dev_config->buttons[i].type == POV3_RIGHT)
91+
{
92+
app_config.povs |= 0x04;
93+
continue;
94+
}
95+
if (p_dev_config->buttons[i].type == POV4_DOWN ||
8496
p_dev_config->buttons[i].type == POV4_UP ||
8597
p_dev_config->buttons[i].type == POV4_LEFT ||
86-
p_dev_config->buttons[i].type == POV4_RIGHT) continue;
98+
p_dev_config->buttons[i].type == POV4_RIGHT)
99+
{
100+
app_config.povs |= 0x08;
101+
continue;
102+
}
87103

88104
for (uint8_t j=0; j<MAX_AXIS_NUM; j++)
89105
{

Src/periphery.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ void Timers_Init(void)
9797
}
9898

9999
/**
100-
* @brief Timers Configuration
100+
* @brief Timers pause
101101
* @param ms: Milliseconds to pause
102102
* @retval None
103103
*/

Src/usb_desc.c

Lines changed: 30 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -149,15 +149,25 @@ uint8_t CustomHID_ReportDescriptor[CUSTOMHID_SIZ_REPORT_DESC] =
149149
0xa1, 0x01, // COLLECTION (Application)
150150

151151
0x85, REPORT_ID_JOY, // REPORT_ID (JOY_REPORT_ID)
152-
0x05, 0x09, // USAGE_PAGE (Button)
153-
0x19, 0x01, // USAGE_MINIMUM (Button 1)
154-
0x29, MAX_BUTTONS_NUM, // USAGE_MAXIMUM (Button MAX_BUTTONS_NUM)
152+
153+
// raw axis data
154+
0x06, 0x00, 0xff, // USAGE_PAGE (Vendor Defined Page 1)
155+
0x09, 0x01, // USAGE (Vendor Usage 1)
156+
0x16, 0x01, 0x80, // LOGICAL_MINIMUM (-32767)
157+
0x26, 0xFF, 0x7F, // LOGICAL_MAXIMUM (32767)
158+
0x75, 0x10, // REPORT_SIZE (16)
159+
0x95, MAX_AXIS_NUM, // REPORT_COUNT (MAX_AXIS_NUM)
160+
0x81, 0x00, // INPUT (Data,Ary,Abs)
161+
162+
// raw button serial data
163+
0x09, 0x02, // USAGE (Vendor Usage 2)
155164
0x15, 0x00, // LOGICAL_MINIMUM (0)
156-
0x25, 0x01, // LOGICAL_MAXIMUM (1)
157-
0x75, 0x01, // REPORT_SIZE (1)
158-
0x95, MAX_BUTTONS_NUM, // REPORT_COUNT (MAX_BUTTONS_NUM)
159-
0x81, 0x02, // INPUT (Data,Var,Abs)
160-
165+
0x26, 0xff, 0x00, // LOGICAL_MAXIMUM (255)
166+
0x75, 0x08, // REPORT_SIZE (8)
167+
0x95, 0x0A, // REPORT_COUNT (10)
168+
0x81, 0x00, // INPUT (Data,Ary,Abs)
169+
170+
// axes data
161171
0x05, 0x01, // USAGE_PAGE (Generic Desktop)
162172
0x09, 0x30, // USAGE (X)
163173
0x09, 0x31, // USAGE (Y)
@@ -173,6 +183,7 @@ uint8_t CustomHID_ReportDescriptor[CUSTOMHID_SIZ_REPORT_DESC] =
173183
0x95, MAX_AXIS_NUM, // REPORT_COUNT (MAX_AXIS_NUM)
174184
0x81, 0x02, // INPUT (Data,Var,Abs)
175185

186+
// POV data
176187
0x09, 0x39, // USAGE (Hat switch)
177188
0x15, 0x00, // LOGICAL_MINIMUM (0)
178189
0x25, 0x07, // LOGICAL_MAXIMUM (7)
@@ -189,25 +200,21 @@ uint8_t CustomHID_ReportDescriptor[CUSTOMHID_SIZ_REPORT_DESC] =
189200
0x09, 0x39, // USAGE (Hat switch)
190201
0x81, 0x02, // INPUT (Data,Var,Abs)
191202

192-
// raw axis data
193-
0x06, 0x00, 0xff, // USAGE_PAGE (Vendor Defined Page 1)
194-
0x09, 0x01, // USAGE (Vendor Usage 1)
195-
0x16, 0x01, 0x80, // LOGICAL_MINIMUM (-32767)
196-
0x26, 0xFF, 0x7F, // LOGICAL_MAXIMUM (32767)
197-
0x75, 0x10, // REPORT_SIZE (16)
198-
0x95, MAX_AXIS_NUM, // REPORT_COUNT (MAX_AXIS_NUM)
199-
0x81, 0x00, // INPUT (Data,Ary,Abs)
200-
201-
// raw button serial data
202-
0x09, 0x02, // USAGE (Vendor Usage 2)
203+
// buttons data
204+
0x05, 0x09, // USAGE_PAGE (Button)
205+
0x19, 0x01, // USAGE_MINIMUM (Button 1)
206+
0x29, MAX_BUTTONS_NUM, // USAGE_MAXIMUM (Button MAX_BUTTONS_NUM)
203207
0x15, 0x00, // LOGICAL_MINIMUM (0)
204-
0x26, 0xff, 0x00, // LOGICAL_MAXIMUM (255)
205-
0x75, 0x08, // REPORT_SIZE (8)
206-
0x95, 0x0A, // REPORT_COUNT (10)
207-
0x81, 0x00, // INPUT (Data,Ary,Abs)
208+
0x25, 0x01, // LOGICAL_MAXIMUM (1)
209+
0x75, 0x01, // REPORT_SIZE (1)
210+
0x95, MAX_BUTTONS_NUM, // REPORT_COUNT (MAX_BUTTONS_NUM)
211+
0x81, 0x02, // INPUT (Data,Var,Abs)
212+
213+
208214

209215
// config data
210216
0x85, REPORT_ID_CONFIG_IN, // REPORT_ID (2)
217+
0x06, 0x00, 0xff, // USAGE_PAGE (Vendor Defined Page 1)
211218
0x09, 0x03, // USAGE (Vendor Usage 3)
212219
0x15, 0x00, // LOGICAL_MINIMUM (0)
213220
0x26, 0xff, 0x00, // LOGICAL_MAXIMUM (255)

Src/usb_hw.c

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -286,22 +286,22 @@ void Get_ReportDesc(void)
286286
AppConfigGet(&tmp_app_config);
287287
DevConfigGet(&tmp_dev_config);
288288

289-
CustomHID_ReportDescriptor[13] = tmp_app_config.buttons_cnt ? tmp_app_config.buttons_cnt : 1;
290-
//CustomHID_ReportDescriptor[21] = tmp_app_config.buttons_cnt ? ((tmp_app_config.buttons_cnt - 1)/8 + 1) * 8 : 0;
289+
CustomHID_ReportDescriptor[104] = tmp_app_config.buttons_cnt ? tmp_app_config.buttons_cnt : 1;
290+
CustomHID_ReportDescriptor[112] = tmp_app_config.buttons_cnt ? ((tmp_app_config.buttons_cnt - 1)/8 + 1) * 8 : 0;
291291

292-
CustomHID_ReportDescriptor[27] = tmp_dev_config.axis_config[0].out_enabled ? 0x30 : 0x00;
293-
CustomHID_ReportDescriptor[29] = tmp_dev_config.axis_config[1].out_enabled ? 0x31 : 0x00;
294-
CustomHID_ReportDescriptor[31] = tmp_dev_config.axis_config[2].out_enabled ? 0x32 : 0x00;
295-
CustomHID_ReportDescriptor[33] = tmp_dev_config.axis_config[3].out_enabled ? 0x33 : 0x00;
296-
CustomHID_ReportDescriptor[35] = tmp_dev_config.axis_config[4].out_enabled ? 0x34 : 0x00;
297-
CustomHID_ReportDescriptor[37] = tmp_dev_config.axis_config[5].out_enabled ? 0x35 : 0x00;
298-
CustomHID_ReportDescriptor[39] = tmp_dev_config.axis_config[6].out_enabled ? 0x36 : 0x00;
299-
CustomHID_ReportDescriptor[41] = tmp_dev_config.axis_config[7].out_enabled ? 0x36 : 0x00;
292+
CustomHID_ReportDescriptor[41] = tmp_dev_config.axis_config[0].out_enabled ? 0x30 : 0x00;
293+
CustomHID_ReportDescriptor[43] = tmp_dev_config.axis_config[1].out_enabled ? 0x31 : 0x00;
294+
CustomHID_ReportDescriptor[45] = tmp_dev_config.axis_config[2].out_enabled ? 0x32 : 0x00;
295+
CustomHID_ReportDescriptor[47] = tmp_dev_config.axis_config[3].out_enabled ? 0x33 : 0x00;
296+
CustomHID_ReportDescriptor[49] = tmp_dev_config.axis_config[4].out_enabled ? 0x34 : 0x00;
297+
CustomHID_ReportDescriptor[51] = tmp_dev_config.axis_config[5].out_enabled ? 0x35 : 0x00;
298+
CustomHID_ReportDescriptor[53] = tmp_dev_config.axis_config[6].out_enabled ? 0x36 : 0x00;
299+
CustomHID_ReportDescriptor[55] = tmp_dev_config.axis_config[7].out_enabled ? 0x36 : 0x00;
300300

301-
CustomHID_ReportDescriptor[55] = (tmp_app_config.povs & 0x01) ? 0x39 : 0x00;
302-
CustomHID_ReportDescriptor[74] = (tmp_app_config.povs & 0x02) ? 0x39 : 0x00;
303-
CustomHID_ReportDescriptor[78] = (tmp_app_config.povs & 0x04) ? 0x39 : 0x00;
304-
CustomHID_ReportDescriptor[82] = (tmp_app_config.povs & 0x08) ? 0x39 : 0x00;
301+
CustomHID_ReportDescriptor[69] = (tmp_app_config.povs & 0x01) ? 0x39 : 0x00;
302+
CustomHID_ReportDescriptor[88] = (tmp_app_config.povs & 0x02) ? 0x39 : 0x00;
303+
CustomHID_ReportDescriptor[92] = (tmp_app_config.povs & 0x04) ? 0x39 : 0x00;
304+
CustomHID_ReportDescriptor[96] = (tmp_app_config.povs & 0x08) ? 0x39 : 0x00;
305305
}
306306

307307
void USB_HW_Init(dev_config_t * p_dev_config)

0 commit comments

Comments
 (0)