Skip to content

Commit 84d3180

Browse files
committed
skelton added and descriptors are modified
1 parent 9c31a56 commit 84d3180

7 files changed

Lines changed: 1128 additions & 0 deletions

File tree

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
/*
2+
HID Keyboard example
3+
4+
5+
created 2020
6+
by Deqing Sun for use with CH55xduino
7+
8+
This example code is in the public domain.
9+
10+
*/
11+
12+
//For windows user, if you ever played with other HID device with the same PID C55D
13+
//You may need to uninstall the previous driver completely
14+
15+
16+
#ifndef USER_USB_RAM
17+
#error "This example needs to be compiled with a USER USB setting"
18+
#endif
19+
20+
#include "src/userUsbHidKeyboard/USBHIDKeyboard.h"
21+
22+
#define BUTTON1_PIN 30
23+
#define BUTTON2_PIN 31
24+
#define BUTTON3_PIN 32
25+
26+
#define LED_BUILTIN 33
27+
28+
bool button1PressPrev = false;
29+
bool button2PressPrev = false;
30+
bool button3PressPrev = false;
31+
32+
33+
void setup() {
34+
USBInit();
35+
pinMode(BUTTON1_PIN, INPUT_PULLUP);
36+
pinMode(BUTTON2_PIN, INPUT_PULLUP);
37+
pinMode(BUTTON3_PIN, INPUT_PULLUP);
38+
pinMode(LED_BUILTIN, OUTPUT);
39+
}
40+
41+
void loop() {
42+
//button 1 is mapped to letter 'a'
43+
bool button1Press = !digitalRead(BUTTON1_PIN);
44+
if (button1PressPrev != button1Press) {
45+
button1PressPrev = button1Press;
46+
if (button1Press) {
47+
Keyboard_press('a');
48+
} else {
49+
Keyboard_release('a');
50+
}
51+
}
52+
53+
//button 2 is mapped to string 'hello'
54+
bool button2Press = !digitalRead(BUTTON2_PIN);
55+
if (button2PressPrev != button2Press) {
56+
button2PressPrev = button2Press;
57+
if (button2Press) {
58+
Keyboard_write('H');
59+
Keyboard_write('e');
60+
Keyboard_write('l');
61+
Keyboard_write('l');
62+
Keyboard_write('o');
63+
}
64+
}
65+
66+
//button 3 is mapped to Capslock
67+
bool button3Press = !digitalRead(BUTTON3_PIN);
68+
if (button3PressPrev != button3Press) {
69+
button3PressPrev = button3Press;
70+
if (button3Press) {
71+
Keyboard_press(KEY_CAPS_LOCK);
72+
delay(100); //a quick capslock press is not recognized on mac
73+
Keyboard_release(KEY_CAPS_LOCK);
74+
}
75+
}
76+
77+
//map capsLock to LED
78+
//Bit 0: NUM lock, Bit 1: CAPS lock, Bit 2: SCROLL lock, Bit 3: Compose, Bit 4: Kana,
79+
if (Keyboard_getLEDStatus() & 2) {
80+
digitalWrite(LED_BUILTIN, HIGH);
81+
} else {
82+
digitalWrite(LED_BUILTIN, LOW);
83+
}
84+
85+
delay(50); //naive debouncing
86+
87+
}
Lines changed: 284 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,284 @@
1+
#include <stdint.h>
2+
#include <stdbool.h>
3+
#include "include/ch5xx.h"
4+
#include "include/ch5xx_usb.h"
5+
#include "USBconstant.h"
6+
#include "USBhandler.h"
7+
8+
extern __xdata __at (EP0_ADDR) uint8_t Ep0Buffer[];
9+
extern __xdata __at (EP1_ADDR) uint8_t Ep1Buffer[];
10+
11+
volatile __xdata uint8_t UpPoint1_Busy = 0; //Flag of whether upload pointer is busy
12+
13+
__xdata uint8_t HIDKey[8] = {0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0};
14+
15+
#define SHIFT 0x80
16+
__code uint8_t _asciimap[128] =
17+
{
18+
0x00, // NUL
19+
0x00, // SOH
20+
0x00, // STX
21+
0x00, // ETX
22+
0x00, // EOT
23+
0x00, // ENQ
24+
0x00, // ACK
25+
0x00, // BEL
26+
0x2a, // BS Backspace
27+
0x2b, // TAB Tab
28+
0x28, // LF Enter
29+
0x00, // VT
30+
0x00, // FF
31+
0x00, // CR
32+
0x00, // SO
33+
0x00, // SI
34+
0x00, // DEL
35+
0x00, // DC1
36+
0x00, // DC2
37+
0x00, // DC3
38+
0x00, // DC4
39+
0x00, // NAK
40+
0x00, // SYN
41+
0x00, // ETB
42+
0x00, // CAN
43+
0x00, // EM
44+
0x00, // SUB
45+
0x00, // ESC
46+
0x00, // FS
47+
0x00, // GS
48+
0x00, // RS
49+
0x00, // US
50+
51+
0x2c, // ' '
52+
0x1e|SHIFT, // !
53+
0x34|SHIFT, // "
54+
0x20|SHIFT, // #
55+
0x21|SHIFT, // $
56+
0x22|SHIFT, // %
57+
0x24|SHIFT, // &
58+
0x34, // '
59+
0x26|SHIFT, // (
60+
0x27|SHIFT, // )
61+
0x25|SHIFT, // *
62+
0x2e|SHIFT, // +
63+
0x36, // ,
64+
0x2d, // -
65+
0x37, // .
66+
0x38, // /
67+
0x27, // 0
68+
0x1e, // 1
69+
0x1f, // 2
70+
0x20, // 3
71+
0x21, // 4
72+
0x22, // 5
73+
0x23, // 6
74+
0x24, // 7
75+
0x25, // 8
76+
0x26, // 9
77+
0x33|SHIFT, // :
78+
0x33, // ;
79+
0x36|SHIFT, // <
80+
0x2e, // =
81+
0x37|SHIFT, // >
82+
0x38|SHIFT, // ?
83+
0x1f|SHIFT, // @
84+
0x04|SHIFT, // A
85+
0x05|SHIFT, // B
86+
0x06|SHIFT, // C
87+
0x07|SHIFT, // D
88+
0x08|SHIFT, // E
89+
0x09|SHIFT, // F
90+
0x0a|SHIFT, // G
91+
0x0b|SHIFT, // H
92+
0x0c|SHIFT, // I
93+
0x0d|SHIFT, // J
94+
0x0e|SHIFT, // K
95+
0x0f|SHIFT, // L
96+
0x10|SHIFT, // M
97+
0x11|SHIFT, // N
98+
0x12|SHIFT, // O
99+
0x13|SHIFT, // P
100+
0x14|SHIFT, // Q
101+
0x15|SHIFT, // R
102+
0x16|SHIFT, // S
103+
0x17|SHIFT, // T
104+
0x18|SHIFT, // U
105+
0x19|SHIFT, // V
106+
0x1a|SHIFT, // W
107+
0x1b|SHIFT, // X
108+
0x1c|SHIFT, // Y
109+
0x1d|SHIFT, // Z
110+
0x2f, // [
111+
0x31, // bslash
112+
0x30, // ]
113+
0x23|SHIFT, // ^
114+
0x2d|SHIFT, // _
115+
0x35, // `
116+
0x04, // a
117+
0x05, // b
118+
0x06, // c
119+
0x07, // d
120+
0x08, // e
121+
0x09, // f
122+
0x0a, // g
123+
0x0b, // h
124+
0x0c, // i
125+
0x0d, // j
126+
0x0e, // k
127+
0x0f, // l
128+
0x10, // m
129+
0x11, // n
130+
0x12, // o
131+
0x13, // p
132+
0x14, // q
133+
0x15, // r
134+
0x16, // s
135+
0x17, // t
136+
0x18, // u
137+
0x19, // v
138+
0x1a, // w
139+
0x1b, // x
140+
0x1c, // y
141+
0x1d, // z
142+
0x2f|SHIFT, // {
143+
0x31|SHIFT, // |
144+
0x30|SHIFT, // }
145+
0x35|SHIFT, // ~
146+
0 // DEL
147+
};
148+
149+
typedef void( *pTaskFn)( void );
150+
151+
void delayMicroseconds(uint16_t us);
152+
153+
void USBInit(){
154+
USBDeviceCfg(); //Device mode configuration
155+
USBDeviceEndPointCfg(); //Endpoint configuration
156+
USBDeviceIntCfg(); //Interrupt configuration
157+
UEP0_T_LEN = 0;
158+
UEP1_T_LEN = 0; //Pre-use send length must be cleared
159+
UEP2_T_LEN = 0;
160+
}
161+
162+
163+
164+
void USB_EP1_IN(){
165+
UEP1_T_LEN = 0;
166+
UEP1_CTRL = UEP1_CTRL & ~ MASK_UEP_T_RES | UEP_T_RES_NAK; // Default NAK
167+
UpPoint1_Busy = 0; //Clear busy flag
168+
}
169+
170+
171+
void USB_EP1_OUT(){
172+
if ( U_TOG_OK ) // Discard unsynchronized packets
173+
{
174+
175+
}
176+
}
177+
178+
uint8_t USB_EP1_send(){
179+
uint16_t waitWriteCount = 0;
180+
181+
waitWriteCount = 0;
182+
while (UpPoint1_Busy){//wait for 250ms or give up
183+
waitWriteCount++;
184+
delayMicroseconds(5);
185+
if (waitWriteCount>=50000) return 0;
186+
}
187+
188+
for (uint8_t i=0;i<sizeof(HIDKey);i++){ //load data for upload
189+
Ep1Buffer[64+i] = HIDKey[i];
190+
}
191+
192+
UEP1_T_LEN = sizeof(HIDKey); //data length
193+
UpPoint1_Busy = 1;
194+
UEP1_CTRL = UEP1_CTRL & ~ MASK_UEP_T_RES | UEP_T_RES_ACK; //upload data and respond ACK
195+
196+
return 1;
197+
}
198+
199+
uint8_t Keyboard_press(uint8_t k) {
200+
uint8_t i;
201+
if (k >= 136) { // it's a non-printing key (not a modifier)
202+
k = k - 136;
203+
} else if (k >= 128) { // it's a modifier key
204+
HIDKey[0] |= (1<<(k-128));
205+
k = 0;
206+
} else { // it's a printing key
207+
k = _asciimap[k];
208+
if (!k) {
209+
//setWriteError();
210+
return 0;
211+
}
212+
if (k & 0x80) { // it's a capital letter or other character reached with shift
213+
HIDKey[0] |= 0x02; // the left shift modifier
214+
k &= 0x7F;
215+
}
216+
}
217+
218+
// Add k to the key report only if it's not already present
219+
// and if there is an empty slot.
220+
if (HIDKey[2] != k && HIDKey[3] != k &&
221+
HIDKey[4] != k && HIDKey[5] != k &&
222+
HIDKey[6] != k && HIDKey[7] != k) {
223+
224+
for (i=2; i<8; i++) {
225+
if (HIDKey[i] == 0x00) {
226+
HIDKey[i] = k;
227+
break;
228+
}
229+
}
230+
if (i == 8) {
231+
//setWriteError();
232+
return 0;
233+
}
234+
}
235+
USB_EP1_send();
236+
return 1;
237+
}
238+
239+
uint8_t Keyboard_release(uint8_t k) {
240+
uint8_t i;
241+
if (k >= 136) { // it's a non-printing key (not a modifier)
242+
k = k - 136;
243+
} else if (k >= 128) { // it's a modifier key
244+
HIDKey[0] &= ~(1<<(k-128));
245+
k = 0;
246+
} else { // it's a printing key
247+
k = _asciimap[k];
248+
if (!k) {
249+
return 0;
250+
}
251+
if (k & 0x80) { // it's a capital letter or other character reached with shift
252+
HIDKey[0] &= ~(0x02); // the left shift modifier
253+
k &= 0x7F;
254+
}
255+
}
256+
257+
// Test the key report to see if k is present. Clear it if it exists.
258+
// Check all positions in case the key is present more than once (which it shouldn't be)
259+
for (i=2; i<8; i++) {
260+
if (0 != k && HIDKey[i] == k) {
261+
HIDKey[i] = 0x00;
262+
}
263+
}
264+
265+
USB_EP1_send();
266+
return 1;
267+
}
268+
269+
void Keyboard_releaseAll(void){
270+
for (uint8_t i=0;i<sizeof(HIDKey);i++){ //load data for upload
271+
HIDKey[i] = 0;
272+
}
273+
USB_EP1_send();
274+
}
275+
276+
uint8_t Keyboard_write(uint8_t c){
277+
uint8_t p = Keyboard_press(c); // Keydown
278+
Keyboard_release(c); // Keyup
279+
return p; // just return the result of press() since release() almost always returns 1
280+
}
281+
282+
uint8_t Keyboard_getLEDStatus(){
283+
return Ep1Buffer[0]; //The only info we gets
284+
}

0 commit comments

Comments
 (0)