forked from zhuhuijia0001/usb-device-ch551
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Task.c
252 lines (195 loc) · 4.15 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
#include "Type.h"
#include "Mcu.h"
#include "PinDefine.h"
#include "Protocol.h"
#include "RecvBuffer.h"
#include "Task.h"
#include "System.h"
#include "Uart.h"
#include "Timer.h"
#include "Usb.h"
#include "Gpio.h"
#include "Packet.h"
#define OUT_BUFFER_SIZE 8
//whether it is the switched port
static BOOL s_isSwitchedPort = FALSE;
//keyboard break code
static UINT8C s_keyboardBreakCode[KEYBOARD_LEN] =
{
0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00,
};
//mouse break code
static UINT8C s_mouseBreakCode[MOUSE_LEN] =
{
0x00, 0x00, 0x00, 0x00,
};
static void SendKeyboardToUsb(UINT8 *pData, UINT8 len)
{
Enp1IntIn(pData, len);
}
static void SendMouseToUsb(UINT8 *pData, UINT8 len)
{
Enp2IntIn(pData, len);
}
static void InitTimer2(UINT8 ms)
{
UINT16 val = FREQ_SYS / 1000ul * ms;
mTimer2ClkFsys(); //T2定时器时钟设置
mTimer_x_ModInit(2, 0); //T2 定时器模式设置
mTimer_x_SetData(2, val); //T2定时器赋值
mTimer2RunCTL(1); //T2定时器启动
ET2 = 1;
}
void InitSystem(void)
{
CfgFsys(); //CH551时钟选择配置
mDelaymS(5); //修改主频等待内部晶振稳定,必加
InitRecvBuffer();
InitUART0(); //串口0初始化
InitTimer2(4); //4ms 中断
#ifdef DEBUG
Port1Cfg(6, 1);
Port1Cfg(7, 1);
#endif
Port3Cfg(4, GPIO_Mode_IN_Floating);
USBDeviceInit(); //USB设备模式初始化
#ifndef DEBUG
CH554WDTModeSelect(1);
#endif
HAL_ENABLE_INTERRUPTS(); //允许单片机中断
}
void ProcessUartData(void)
{
#ifdef DEBUG
if (CheckPCReady())
{
P1_6 = 1;
}
else
{
P1_6 = 0;
}
#endif
if (!IsRecvBufferEmpty())
{
UINT8 *packet = GetOutputBuffer();
UINT8 id = packet[0];
UINT8 *pData = &packet[1];
switch (id)
{
case ID_USB_KEYBOARD:
if (CheckPCReady() && CheckPCSleeped())
{
CH554USBDevWakeup();
}
SendKeyboardToUsb(pData, KEYBOARD_LEN);
break;
case ID_USB_MOUSE:
if (CheckPCReady() && CheckPCSleeped())
{
CH554USBDevWakeup();
}
SendMouseToUsb(pData, MOUSE_LEN);
break;
case ID_QUERY_ONLINE:
{
UINT8 online;
UINT8 len;
UINT8 buffer[OUT_BUFFER_SIZE];
#ifdef DEBUG
P1_6 = !P1_6;
#endif
if (GET_GPIO_BIT(PIN_USB_POWER))
{
online = STATUS_ONLINE;
}
else
{
online = STATUS_OFFLINE;
}
if (BuildOnlineStatusPacket(buffer, sizeof(buffer), &len, online))
{
CH554UART0SendData(buffer, len);
}
if (pData[0] == QUERY_CURRENT_PORT)
{
UINT8 led = GetKeyboardLedStatus();
if (BuildKeyboardLedPacket(buffer, sizeof(buffer), &len, led))
{
CH554UART0SendData(buffer, len);
}
s_isSwitchedPort = TRUE;
}
else
{
s_isSwitchedPort = FALSE;
}
}
break;
case ID_SWITCH:
if (pData[0] == SWITCH_IN)
{
//switch in
UINT8 len;
UINT8 buffer[OUT_BUFFER_SIZE];
UINT8 led = GetKeyboardLedStatus();
if (BuildKeyboardLedPacket(buffer, sizeof(buffer), &len, led))
{
CH554UART0SendData(buffer, len);
}
}
else
{
//switch out
//send break code
if (!CheckPCSleeped())
{
SendKeyboardToUsb(s_keyboardBreakCode, KEYBOARD_LEN);
SendMouseToUsb(s_mouseBreakCode, MOUSE_LEN);
}
}
break;
default:
break;
}
}
}
void ProcessKeyboardLed(void)
{
static UINT8 ledSave = 0x00;
UINT8 led = GetKeyboardLedStatus();
if (led != ledSave)
{
if (s_isSwitchedPort)
{
UINT8 len;
UINT8 buffer[OUT_BUFFER_SIZE];
if (BuildKeyboardLedPacket(buffer, sizeof(buffer), &len, led))
{
CH554UART0SendData(buffer, len);
}
}
ledSave = led;
#ifdef DEBUG
if (led & 0x02)
{
P1_7 = 1;
}
else
{
P1_7 = 0;
}
#endif
}
}
#ifndef DEBUG
void FeedWdt(void)
{
CH554WDTFeed(0);
}
#else
void FeedWdt(void)
{
}
#endif