forked from Lotlab/nrf52-keyboard
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathuart.c
242 lines (218 loc) · 5.38 KB
/
uart.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
/*
Copyright (C) 2018,2019 Jim Jiang <[email protected]>
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
#include "uart.h"
#include "CH554_SDCC.h"
#include "config.h"
#include "endpoints.h"
#include "system.h"
#include "usb_comm.h"
#include <stdbool.h>
#include <string.h>
#ifdef PIN_STANDBY
#define IS_CHARGING (PIN_CHARGING && !PIN_STANDBY)
#else
#define IS_CHARGING (PIN_CHARGING)
#endif
uart_state uart_rx_state;
static uint8_t recv_len, pos;
static uint8_t __XDATA volatile recv_buff[64];
static bool uart_arrive_flag, last_success;
static uint8_t last_pos;
/**
* @brief 发送数据
*
* @param c
*/
static void uart_tx(uint8_t c)
{
SBUF1 = c;
while (U1TI == 0)
;
U1TI = 0;
}
/**
* @brief 接收数据
*
* @return uint8_t
*/
static uint8_t uart_rx()
{
while (U1RI == 0)
;
U1RI = 0;
return SBUF1;
}
/**
* @brief 获取数据的校验和
*
* @return uint8_t
*/
static uint8_t checksum(uint8_t* data, uint8_t len)
{
uint8_t sum = 0x00;
for (int i = 0; i < len; i++)
sum += data[i];
return sum;
}
/**
* @brief 初始化UART
*
*/
void uart_init()
{
U1SM0 = 0; // 8Bit
U1SMOD = 1; // fast mode
U1REN = 1; //串口0接收使能
SBAUD1 = 256 - (FREQ_SYS / 16 / 115200) & 0xFF;
IE_UART1 = 1; //启用串口中断
}
/**
* @brief 解析发来的UART数据包
*
*/
static void uart_data_parser(void)
{
if (checksum(recv_buff, recv_len - 1) != recv_buff[recv_len - 1]) {
// 不做任何事情,等待下次重发
return;
}
uint8_t command = recv_buff[0];
if (command >= 0x80) {
// 通信响应数据包
uint8_t datalen = command & 0x7F;
ResponseConfigurePacket(&recv_buff[1], datalen);
last_success = true;
} else if (command >= 0x40) {
uint8_t index = recv_buff[1];
uint8_t kplen = (command & 0x3F);
if (index == 0) {
// 通常键盘数据包
KeyboardGenericUpload(&recv_buff[2], kplen);
last_success = true;
} else {
// 附加数据包
// 发过来的包的id和reportID一致,不用处理
KeyboardExtraUpload(&recv_buff[1], kplen + 1);
last_success = true;
}
}
}
/**
* @brief 发送当前主机的状态
*
*/
static void uart_send_status()
{
uint8_t data = 0x10;
#ifdef PIN_CHARGING
if (!IS_CHARGING) // 是否充满
data |= 0x02;
#endif
if (usb_state.is_ready && !usb_state.is_sleep) // 是否连接主机
data |= 0x04;
if (usb_state.protocol)
data |= 0x08;
if (last_success) // 上次接收状态
data |= 0x01;
uart_tx(data);
}
static uint8_t volatile __XDATA send_buff[64];
static uint8_t send_len = 0;
/**
* @brief UART状态的定期检测
*
*/
void uart_check()
{
// UART接收超时强制退出
if (uart_rx_state == STATE_DATA && last_pos == pos) {
uart_rx_state = STATE_IDLE;
}
// UART数据接收完毕,准备解析
if (uart_arrive_flag) {
uart_arrive_flag = false;
uart_data_parser();
}
// 当前不在接收数据
if (uart_rx_state == STATE_IDLE) {
if (send_len > 0) {
// 有等待发送的数据则发送数据
for (uint8_t i = 0; i < send_len; i++) {
uart_tx(send_buff[i]);
}
send_len = 0;
} else if (!usb_state.is_busy) { // USB 当前空闲,可以轮询下一个数据包
// 没有等待发送的数据,发送定期Query状态包
uart_send_status();
// 重置success状态,默认接收失败
last_success = false;
}
}
last_pos = pos;
}
/**
* @brief 接收数据
*
*/
void uart_recv(void)
{
/**
* Packet Format:
* Len Type Data[0] ... Data[Len-2]
* Variable:
* len buf[0] buf[1] ... Buf[Len-1]
*/
uint8_t data = uart_rx();
switch (uart_rx_state) {
case STATE_IDLE:
if (data >= 0x40) {
recv_len = (data >= 0x80) ? ((data & 0x7F) + 2) : ((data & 0x3F) + 3);
pos = 0;
recv_buff[pos++] = data;
uart_rx_state = STATE_DATA;
}
break;
case STATE_DATA:
recv_buff[pos++] = data;
if (pos >= recv_len) {
uart_rx_state = STATE_IDLE;
uart_arrive_flag = true;
}
break;
}
}
/**
* @brief 下发LED信号
*
* @param val
*/
void uart_send_led(uint8_t val)
{
send_buff[0] = 0x20 + (val & 0x1F);
send_len = 1;
}
/**
* @brief 下发keymap数据
*
* @param data
* @param len
*/
void uart_send_keymap(uint8_t* data, uint8_t len)
{
send_buff[0] = len + 0x80; // command
for (uint8_t i = 0; i < len; i++)
send_buff[i + 1] = data[i];
send_buff[len + 1] = checksum(send_buff, len + 1);
send_len = len + 2; // cmd + sum
}