forked from zhuhuijia0001/ch559-usb-host
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTask.c
373 lines (302 loc) · 5.97 KB
/
Task.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
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
#include "Type.h"
#include "PinDefine.h"
#include "Mcu.h"
#include "System.h"
#include "Gpio.h"
#include "Uart.h"
#include "Timer2.h"
#include "UsbHost.h"
#include "KeyboardLed.h"
#include "RecvBuffer.h"
#include "Protocol.h"
#include "Ps2Keyboard.h"
#include "Ps2Mouse.h"
#include "IpPs2Keyboard.h"
#include "IpPs2Mouse.h"
#include "Packet.h"
#include "Task.h"
#include "Trace.h"
static UINT8 volatile s_5MsCounter = 0;
static BOOL volatile s_CheckUsbPort0 = FALSE;
static BOOL volatile s_CheckUsbPort1 = FALSE;
void Uart0Isr(void) interrupt INT_NO_UART0 using 1
{
if (RI)
{
UINT8 ch;
RI = 0;
ch = SBUF;
RecvBufferOneByte(ch);
}
if (TI)
{
TI = 0;
SetUart0Sent();
}
}
void Timer2Isr(void) interrupt INT_NO_TMR2 using 2
{
if (TF2)
{
TF2 = 0;
//uart receive timeout
RecvBufferTimerout();
if (s_5MsCounter == 0)
{
s_CheckUsbPort0 = TRUE;
}
else if (s_5MsCounter == 1)
{
s_CheckUsbPort1 = TRUE;
}
s_5MsCounter++;
if (s_5MsCounter == 2)
{
s_5MsCounter = 0;
}
#ifdef DEBUG
if (GET_GPIO_BIT(PIN_TEST_LED))
{
CLR_GPIO_BIT(PIN_TEST_LED);
}
else
{
SET_GPIO_BIT(PIN_TEST_LED);
}
#endif
}
}
static void InitPs2Port(void)
{
CH559GPIOModeSelt(PORT_PIN_KB_CLK, 3, OFFSET_PIN_KB_CLK);
CH559GPIOModeSelt(PORT_PIN_KB_DAT, 3, OFFSET_PIN_KB_DAT);
CH559GPIOModeSelt(PORT_PIN_MS_CLK, 3, OFFSET_PIN_MS_CLK);
CH559GPIOModeSelt(PORT_PIN_MS_DAT, 3, OFFSET_PIN_MS_DAT);
//for ip ps2 pins
CH559P4Mode();
DisablePs2MousePort();
DisablePs2KeyboardPort();
DisableIpPs2MousePort();
DisableIpPs2KeyboardPort();
}
static void InitTimer2(void)
{
mTimer2Clk12DivFsys( ); //时钟选择Fsys定时器方式
mTimer2Setup(0); //定时器功能演示
mTimer2Init(FREQ_SYS / 12 * 5 / 1000); //定时器赋初值
ET2 = 1; //使能全局中断
mTimer2RunCTL(1); //启动定时器
}
void InitSystem(void)
{
CfgFsys(); //CH559时钟选择配置
mDelaymS(5);
CH559GPIODrivCap(3, 1);
InitPs2Port();
#ifdef DEBUG
CH559GPIOModeSelt(PORT_PIN_TEST_LED, 2, OFFSET_PIN_TEST_LED);
SET_GPIO_BIT(PIN_TEST_LED);
#endif
InitRecvBuffer();
InitTimer2();
InitUART0();
InitUsbData();
InitUsbHost();
HAL_ENABLE_INTERRUPTS();
TRACE("system init\r\n");
}
void ProcessUsbHostPort()
{
DealUsbPort();
if (s_CheckUsbPort0)
{
s_CheckUsbPort0 = FALSE;
InterruptProcessRootHubPort(0);
}
if (s_CheckUsbPort1)
{
s_CheckUsbPort1 = FALSE;
InterruptProcessRootHubPort(1);
}
}
void ProcessPs2Port()
{
UINT8 res;
UINT8 buffer[KEYBOARD_LEN];
res = TransceivePs2KeyboardPort(buffer);
DisablePs2KeyboardPort();
if (res == PS2_KB_AA)
{
UINT8 led = GetKeyboardLedStatus();
SetPs2KeyboardLed(led);
DisablePs2KeyboardPort();
}
else if (res == PS2_KB_VALID_DATA)
{
UINT8 output[KEYBOARD_LEN + 2];
UINT8 pktLen;
#ifdef DEBUG
UINT16 i;
for (i = 0; i < KEYBOARD_LEN; i++)
{
TRACE1("0x%x ", buffer[i]);
}
TRACE("\r\n");
#endif
if (BuildPs2KeyboardPacket(output, sizeof(output), &pktLen, buffer))
{
if (pktLen == KEYBOARD_LEN + 2)
{
CH559UART0SendData(output, pktLen);
}
}
}
else if (res == PS2_KB_INVALID_DATA)
{
TRACE("invalid kb data\r\n");
}
res = TransceivePs2MousePort(buffer);
DisablePs2MousePort();
if (res == PS2_MS_AA)
{
BOOL res = InitPs2Mouse();
DisablePs2MousePort();
if (res)
{
TRACE("mouse init ok\r\n");
}
else
{
TRACE("mouse init failed\r\n");
}
}
else if (res == PS2_MS_VALID_DATA)
{
UINT8 output[MOUSE_LEN + 2];
UINT8 pktLen;
#ifdef DEBUG
UINT16 i;
for (i = 0; i < MOUSE_LEN; i++)
{
TRACE1("0x%x ", buffer[i]);
}
TRACE("\r\n");
#endif
if (BuildPs2MousePacket(output, sizeof(output), &pktLen, buffer))
{
if (pktLen == MOUSE_LEN + 2)
{
CH559UART0SendData(output, pktLen);
}
}
}
else if (res == PS2_KB_INVALID_DATA)
{
TRACE("invalid ms data\r\n");
}
}
void ProcessIpPs2Port()
{
UINT8 res;
UINT8 buffer[KEYBOARD_LEN];
res = TransceiveIpPs2KeyboardPort(buffer);
DisableIpPs2KeyboardPort();
if (res == IP_PS2_KB_AA)
{
UINT8 led = GetKeyboardLedStatus();
SetIpPs2KeyboardLed(led);
DisableIpPs2KeyboardPort();
}
else if (res == IP_PS2_KB_VALID_DATA)
{
UINT8 output[KEYBOARD_LEN + 2];
UINT8 pktLen;
#ifdef DEBUG
UINT16 i;
for (i = 0; i < KEYBOARD_LEN; i++)
{
TRACE1("0x%x ", buffer[i]);
}
TRACE("\r\n");
#endif
if (BuildPs2KeyboardPacket(output, sizeof(output), &pktLen, buffer))
{
if (pktLen == KEYBOARD_LEN + 2)
{
CH559UART0SendData(output, pktLen);
}
}
}
else if (res == IP_PS2_KB_INVALID_DATA)
{
TRACE("invalid kb data\r\n");
}
res = TransceiveIpPs2MousePort(buffer);
DisableIpPs2MousePort();
if (res == IP_PS2_MS_AA)
{
BOOL res = InitIpPs2Mouse();
DisableIpPs2MousePort();
if (res)
{
TRACE("mouse init ok\r\n");
}
else
{
TRACE("mouse init failed\r\n");
}
}
else if (res == IP_PS2_MS_VALID_DATA)
{
UINT8 output[MOUSE_LEN + 2];
UINT8 pktLen;
#ifdef DEBUG
UINT16 i;
for (i = 0; i < MOUSE_LEN; i++)
{
TRACE1("0x%x ", buffer[i]);
}
TRACE("\r\n");
#endif
if (BuildPs2MousePacket(output, sizeof(output), &pktLen, buffer))
{
if (pktLen == MOUSE_LEN + 2)
{
CH559UART0SendData(output, pktLen);
}
}
}
else if (res == IP_PS2_KB_INVALID_DATA)
{
TRACE("invalid ms data\r\n");
}
}
void ProcessKeyboardLed()
{
if (!IsRecvBufferEmpty())
{
UINT8 *packet = GetOutputBuffer();
UINT8 id = packet[0];
UINT8 *pData = &packet[1];
switch (id)
{
case ID_LED_STATUS:
{
UINT8 led = pData[0];
UINT8 ledSave = GetKeyboardLedStatus();
if (led != ledSave)
{
SetPs2KeyboardLed(led);
DisablePs2KeyboardPort();
SetIpPs2KeyboardLed(led);
DisableIpPs2KeyboardPort();
UpdateUsbKeyboardLed(led);
SetKeyboardLedStatus(led);
}
}
break;
default:
break;
}
}
}