forked from BBS215/STM8S-VUSB-KEYBOARD
-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.c
288 lines (257 loc) · 8.16 KB
/
main.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
#include "main.h"
#include "usb.h"
#include "init.h"
#include "peripheral.h"
#include "HIDClassCommon.h"
/// GLOBAL VARS ////////////////////////////////////////////
volatile uint8_t g_systimer_flag;
t_HID_Dev HID_Dev;
/// CALLBACKS //////////////////////////////////////////////
void USB_EP0_RxReady_callback(uint8_t *p_data)
{
if (p_data[0] == STD_KEYBOARD_REPORT_ID) {
if (p_data[1] & HID_KEYBOARD_LED_NUMLOCK) // Num Lock
Led_setmode(LED_0, LED_ON);
else Led_setmode(LED_0, LED_OFF);
if (p_data[1] & HID_KEYBOARD_LED_CAPSLOCK) // Caps Lock
Led_setmode(LED_1, LED_ON);
else Led_setmode(LED_1, LED_OFF);
if (p_data[1] & HID_KEYBOARD_LED_SCROLLLOCK) // Scroll lock
Led_setmode(LED_2, LED_ON);
else Led_setmode(LED_2, LED_OFF);
}
}
void KEY_changed(uint8_t key_num)
{
switch(key_num) {
case BTN_0: // used in STD keyboard
case BTN_1: // used in STD keyboard
case BTN_2: // used in STD keyboard
case BTN_3: // used in STD keyboard
case BTN_4: // used in STD keyboard
case BTN_5: // used in STD keyboard
case BTN_6: // used in STD keyboard
case BTN_7: // used in STD keyboard
HID_Dev.STD_KB_Report_changed_flag = 1; // STD keyboard
break;
//HID_Dev.EXT1_KB_Report_changed_flag = 1; // EXT1 keyboard
//break;
//HID_Dev.EXT2_KB_Report_changed_flag = 1; // EXT2 keyboard
//break;
default:
break;
}
}
void EVENT_KEY_pressed(uint8_t key_num) { KEY_changed(key_num); }
void EVENT_KEY_released(uint8_t key_num) { KEY_changed(key_num); }
void BTN_USB_send_Loop(void)
{
uint8_t KeyCodesMAX;
uint8_t key_mask = Buttons_get_mask();
uint8_t UsedKeyCodes = 0;
// STD Keyboard
if (HID_Dev.STD_KB_Report_changed_flag) {
KeyCodesMAX = 5;
HID_Dev.STD_KB_Report.Modifier = 0;
if (key_mask & (1 << BTN_0))
HID_Dev.STD_KB_Report.Modifier |= HID_KEYBOARD_MODIFIER_LEFTGUI;
if (key_mask & (1 << BTN_1))
HID_Dev.STD_KB_Report.Modifier |= HID_KEYBOARD_MODIFIER_LEFTCTRL;
if (key_mask & (1 << BTN_6))
HID_Dev.STD_KB_Report.Modifier |= HID_KEYBOARD_MODIFIER_LEFTSHIFT;
if (key_mask & (1 << BTN_7))
HID_Dev.STD_KB_Report.Modifier |= HID_KEYBOARD_MODIFIER_LEFTALT;
if (key_mask & (1 << BTN_2)) {
HID_Dev.STD_KB_Report.KeyCode[UsedKeyCodes++] = HID_KEYBOARD_SC_1_AND_EXCLAMATION;
if (UsedKeyCodes >= KeyCodesMAX) goto STD_Send;
}
if (key_mask & (1 << BTN_3)) {
HID_Dev.STD_KB_Report.KeyCode[UsedKeyCodes++] = HID_KEYBOARD_SC_2_AND_AT;
if (UsedKeyCodes >= KeyCodesMAX) goto STD_Send;
}
if (key_mask & (1 << BTN_4)) {
HID_Dev.STD_KB_Report.KeyCode[UsedKeyCodes++] = HID_KEYBOARD_SC_3_AND_HASHMARK;
if (UsedKeyCodes >= KeyCodesMAX) goto STD_Send;
}
if (key_mask & (1 << BTN_5)) {
HID_Dev.STD_KB_Report.KeyCode[UsedKeyCodes++] = HID_KEYBOARD_SC_4_AND_DOLLAR;
if (UsedKeyCodes >= KeyCodesMAX) goto STD_Send;
}
while(UsedKeyCodes < KeyCodesMAX) HID_Dev.STD_KB_Report.KeyCode[UsedKeyCodes++] = 0;
STD_Send:
if (USB_Send_Data((uint8_t*)&HID_Dev.STD_KB_Report, sizeof(HID_Dev.STD_KB_Report), 1) == 0)
HID_Dev.STD_KB_Report_changed_flag = 0;
return;
}
// EXT1 Keyboard
if (HID_Dev.EXT1_KB_Report_changed_flag) {
/*if (key_mask & (1 << BTN_3)) {
HID_Dev.EXT1_KB_Report.KeyCode_LO = LOBYTE(0x????);
HID_Dev.EXT1_KB_Report.KeyCode_HI = HIBYTE(0x????);
goto EXT1_Send;
}*/
/*if (key_mask & (1 << BTN_4)) {
HID_Dev.EXT1_KB_Report.KeyCode_LO = LOBYTE(0x????);
HID_Dev.EXT1_KB_Report.KeyCode_HI = HIBYTE(0x????);
goto EXT1_Send;
}*/
HID_Dev.EXT1_KB_Report.KeyCode_LO = 0;
HID_Dev.EXT1_KB_Report.KeyCode_HI = 0;
EXT1_Send:
if (USB_Send_Data((uint8_t*)&HID_Dev.EXT1_KB_Report, sizeof(HID_Dev.EXT1_KB_Report), 1) == 0)
HID_Dev.EXT1_KB_Report_changed_flag = 0;
return;
}
// EXT2 Keyboard
if (HID_Dev.EXT2_KB_Report_changed_flag) {
KeyCodesMAX = 7;
/*if (key_mask & (1 << BTN_1)) {
HID_Dev.EXT2_KB_Report.KeyCode[UsedKeyCodes++] = 0x??;
if (UsedKeyCodes >= KeyCodesMAX) goto EXT2_Send;
}*/
/*if (key_mask & (1 << BTN_2)) {
HID_Dev.EXT2_KB_Report.KeyCode[UsedKeyCodes++] = 0x??;
if (UsedKeyCodes >= KeyCodesMAX) goto EXT2_Send;
}*/
while(UsedKeyCodes < KeyCodesMAX) HID_Dev.EXT2_KB_Report.KeyCode[UsedKeyCodes++] = 0;
EXT2_Send:
if (USB_Send_Data((uint8_t*)&HID_Dev.EXT2_KB_Report, sizeof(HID_Dev.EXT2_KB_Report), 1) == 0)
HID_Dev.EXT2_KB_Report_changed_flag = 0;
return;
}
}
int8_t USB_Class_Init_callback(uint8_t dev_config)
{
uint8_t i;
for(i=0;i<sizeof(HID_Dev);i++) ((uint8_t*)&HID_Dev)[i] = 0;
HID_Dev.STD_KB_Report.ReportID = STD_KEYBOARD_REPORT_ID;
HID_Dev.EXT1_KB_Report.ReportID = EXT1_KEYBOARD_REPORT_ID;
HID_Dev.EXT2_KB_Report.ReportID = EXT2_KEYBOARD_REPORT_ID;
HID_Dev.STD_KB_Report_changed_flag = 1;
HID_Dev.EXT1_KB_Report_changed_flag = 1;
HID_Dev.EXT2_KB_Report_changed_flag = 1;
return 0;
}
int8_t USB_Class_DeInit_callback(void)
{
return 0;
}
int8_t USB_Setup_Request_callback(t_USB_SetupReq *p_req)
{
switch (p_req->bmRequest & USB_REQ_TYPE_MASK)
{
case USB_REQ_TYPE_CLASS:
{
switch (p_req->bRequest)
{
case HID_REQ_GET_REPORT: // 0x01
{
if (p_req->wIndex_LO == INTERFACE_ID_HIDDev) // HID Device
{
if (p_req->wValue_HI == HID_REPORT_FEATURE)
{
switch(p_req->wValue_LO) // ReportID
{
case STD_KEYBOARD_REPORT_ID: // STD Keyboard
return USB_Send_Data((uint8_t*)&HID_Dev.STD_KB_Report, sizeof(HID_Dev.STD_KB_Report), 0);
case EXT1_KEYBOARD_REPORT_ID: // EXT1 Keyboard
return USB_Send_Data((uint8_t*)&HID_Dev.EXT1_KB_Report, sizeof(HID_Dev.EXT1_KB_Report), 0);
case EXT2_KEYBOARD_REPORT_ID: // EXT2 Keyboard
return USB_Send_Data((uint8_t*)&HID_Dev.EXT2_KB_Report, sizeof(HID_Dev.EXT2_KB_Report), 0);
}
}
}
break;
}
case HID_REQ_SET_REPORT: // 0x09
{
if (p_req->wIndex_LO == INTERFACE_ID_HIDDev) // HID Device
{
if (p_req->wValue_HI == HID_REPORT_OUTPUT)
{
if (p_req->wValue_LO == STD_KEYBOARD_REPORT_ID) // ReportID
{
return USB_Send_Data(NULL, 0, 0);
}
}
}
break;
}
case HID_REQ_SET_PROTOCOL: // 0x0B
HID_Dev.Protocol = p_req->wValue_LO;
return USB_Send_Data(NULL, 0, 0);
case HID_REQ_GET_PROTOCOL: // 0x03
return USB_Send_Data(&HID_Dev.Protocol, 1, 0);
case HID_REQ_SET_IDLE: // 0x0A
HID_Dev.IdleState = p_req->wValue_HI;
return USB_Send_Data(NULL, 0, 0);
case HID_REQ_GET_IDLE: // 0x02
return USB_Send_Data(&HID_Dev.IdleState, 1, 0);
}
break;
}
case USB_REQ_TYPE_STANDARD:
{
switch (p_req->bRequest)
{
case USB_REQ_GET_DESCRIPTOR:
{
if (p_req->wValue_HI == TYPE_REPORT_DESCRIPTOR) // 0x22
{
if (p_req->wIndex_LO == INTERFACE_ID_HIDDev) {
return USB_Send_Data(usb_report_descriptor, (uint8_t)MIN(p_req->wLength_LO, SIZE_REPORT_DESCRIPTOR), 0);
}
} else
if (p_req->wValue_HI == TYPE_HID_DESCRIPTOR) // 0x21
{
return USB_Send_Data(USB_HID_descriptor, (uint8_t)MIN(p_req->wLength_LO, SIZE_HID_DESCRIPTOR), 0);
}
break;
}
case USB_REQ_GET_INTERFACE:
{
if (p_req->wIndex_LO == INTERFACE_ID_HIDDev) {
return USB_Send_Data(&HID_Dev.AltSetting, 1, 0);
}
break;
}
case USB_REQ_SET_INTERFACE:
{
if (p_req->wIndex_LO == INTERFACE_ID_HIDDev) {
HID_Dev.AltSetting = p_req->wValue_LO;
return USB_Send_Data(NULL, 0, 0);
}
break;
}
}
}
}
return -1;
}
void USB_EP1_RxReady_callback(uint8_t *p_data)
{
// Nothing. Really.
}
/// MAIN ///////////////////////////////////////////////////
void main(void)
{
disableInterrupts();
Init_Clock();
Init_GPIO();
Leds_init();
Buttons_init();
SYSTimer_Init();
USB_Init();
enableInterrupts();
USB_connect();
while(1)
{
USB_loop();
if (g_systimer_flag) { // 100 Hz call
g_systimer_flag = 0;
Buttons_loop();
BTN_USB_send_Loop();
USB_slow_loop();
}
}
}