Skip to content

Commit 24f198d

Browse files
committed
mouse, keyboard and vendor specific io device support
1 parent 36f12e7 commit 24f198d

11 files changed

Lines changed: 1404 additions & 0 deletions

File tree

Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
/*
2+
HID Keyboard, Mouse and vendor specific inout device example
3+
4+
5+
created 2021
6+
by betaEncoder 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/userUsbHidMouseKeyGen/USBHIDKeyboard.h"
21+
#include "src/userUsbHidMouseKeyGen/USBHIDMouse.h"
22+
#include "src/userUsbHidMouseKeyGen/USBHIDGeneric.h"
23+
24+
#define ADC_PIN 15
25+
#define BUTTON2_PIN 16
26+
#define BUTTON3_PIN 17
27+
28+
#define LED_BUILTIN 14
29+
30+
bool button1PressPrev = false;
31+
bool button2PressPrev = false;
32+
bool button3PressPrev = false;
33+
34+
uint8_t adc=0;
35+
36+
uint8_t *buff_in;
37+
uint8_t *buff_out;
38+
39+
40+
void setup() {
41+
USBInit();
42+
buff_in = get_IN_buffer();
43+
buff_out = get_OUT_buffer();
44+
pinMode(ADC_PIN, INPUT);
45+
pinMode(BUTTON2_PIN, INPUT_PULLUP);
46+
pinMode(BUTTON3_PIN, INPUT_PULLUP);
47+
pinMode(LED_BUILTIN, OUTPUT);
48+
digitalWrite(LED_BUILTIN, LOW);
49+
}
50+
51+
void loop() {
52+
adc = analogRead(ADC_PIN);
53+
54+
if(is_received()){
55+
clear_received_flag();
56+
if(buff_in[0]==3){ // report ID match
57+
if(buff_in[1]==0){
58+
if(buff_in[2]==1){
59+
digitalWrite(LED_BUILTIN, HIGH);
60+
}else{
61+
digitalWrite(LED_BUILTIN, LOW);
62+
}
63+
}else if(buff_in[1]==1){
64+
buff_out[1] = button2PressPrev;
65+
}else if(buff_in[1]==2){
66+
buff_out[1] = 0;
67+
buff_out[2] = adc;
68+
}
69+
buff_out[0] = 3; // report ID
70+
USB_EP1_send();
71+
}
72+
}
73+
74+
//button 2 is mapped to left click
75+
bool button2Press = !digitalRead(BUTTON2_PIN);
76+
if (button2PressPrev != button2Press) {
77+
button2PressPrev = button2Press;
78+
if (button2Press) {
79+
Mouse_press(BUTTON_LEFT);
80+
}else{
81+
Mouse_release(BUTTON_LEFT);
82+
}
83+
}
84+
85+
//button 3 is mapped to letter 'a'
86+
bool button3Press = !digitalRead(BUTTON3_PIN);
87+
if (button3PressPrev != button3Press) {
88+
button3PressPrev = button3Press;
89+
if (button3Press) {
90+
Keyboard_press('a');
91+
} else {
92+
Keyboard_release('a');
93+
}
94+
}
95+
96+
//map capsLock to LED
97+
//Bit 0: NUM lock, Bit 1: CAPS lock, Bit 2: SCROLL lock, Bit 3: Compose, Bit 4: Kana,
98+
if (Keyboard_getLEDStatus() & 2) {
99+
//digitalWrite(LED_BUILTIN, HIGH);
100+
} else {
101+
//digitalWrite(LED_BUILTIN, LOW);
102+
}
103+
104+
delay(50); //naive debouncing
105+
106+
}
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
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+
volatile uint8_t received = 0;
9+
10+
//volatile __xdata uint8_t UpPoint1_Busy; //Flag of whether upload pointer is busy
11+
volatile __xdata uint8_t USBByteCountEP1 = 0; //Bytes of received data on USB endpoint
12+
volatile __xdata uint8_t USBBufOutPointEP1 = 0; //Data pointer for fetching
13+
14+
__xdata uint8_t HIDRep[64];
15+
16+
typedef void( *pTaskFn)( void );
17+
18+
void delayMicroseconds(uint16_t us);
19+
20+
void USBInit(){
21+
uint8_t i;
22+
USBDeviceCfg(); //Device mode configuration
23+
USBDeviceEndPointCfg(); //Endpoint configuration
24+
USBDeviceIntCfg(); //Interrupt configuration
25+
UEP0_T_LEN = 0;
26+
UEP1_T_LEN = 0; //Pre-use send length must be cleared
27+
for(i=0;i<sizeof(HIDRep);i++){ //clear buffer
28+
HIDRep[i] = 0;
29+
}
30+
}
31+
32+
33+
uint8_t* get_IN_buffer(){
34+
return Ep1Buffer;
35+
}
36+
37+
uint8_t* get_OUT_buffer(){
38+
return HIDRep;
39+
}
40+
41+
bool is_received(){
42+
return received;
43+
}
44+
45+
void clear_received_flag(){
46+
received = 0;
47+
USBByteCountEP1 = 0;//USB_RX_LEN;
48+
USBBufOutPointEP1 = 0; //Reset Data pointer for fetching
49+
50+
}
51+
52+
void USB_EP1_IN(){
53+
UEP1_T_LEN = 0;
54+
UEP1_CTRL = UEP1_CTRL & ~ MASK_UEP_T_RES | UEP_T_RES_NAK; // Default NAK
55+
UpPoint1_Busy = 0; //Clear busy flag
56+
}
57+
58+
59+
void USB_EP1_OUT(){
60+
if ( U_TOG_OK ) // Discard unsynchronized packets
61+
{
62+
received = 1;
63+
if (USBByteCountEP1) UEP1_CTRL = UEP1_CTRL & ~ MASK_UEP_R_RES | UEP_R_RES_NAK; //Respond NAK after a packet. Let main code change response after handling.
64+
}
65+
}
66+
67+
uint8_t USB_EP1_send(){
68+
uint16_t waitWriteCount = 0;
69+
70+
waitWriteCount = 0;
71+
while (UpPoint1_Busy){//wait for 250ms or give up
72+
waitWriteCount++;
73+
delayMicroseconds(5);
74+
if (waitWriteCount>=50000) return 0;
75+
}
76+
77+
for (uint8_t i=0;i<sizeof(HIDRep);i++){ //load data for upload
78+
Ep1Buffer[64+i] = HIDRep[i];
79+
}
80+
81+
UEP1_T_LEN = sizeof(HIDRep); //data length
82+
UpPoint1_Busy = 1;
83+
UEP1_CTRL = UEP1_CTRL & ~ MASK_UEP_T_RES | UEP_T_RES_ACK; //upload data and respond ACK
84+
85+
return 1;
86+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
#ifndef __USB_HID_GENERIC_H__
2+
#define __USB_HID_GENERIC_H__
3+
4+
#include <stdint.h>
5+
#include "include/ch5xx.h"
6+
#include "include/ch5xx_usb.h"
7+
8+
#ifdef __cplusplus
9+
extern "C" {
10+
#endif
11+
12+
void USBInit(void);
13+
14+
uint8_t* get_IN_buffer();
15+
uint8_t* get_OUT_buffer();
16+
bool is_received();
17+
void clear_received_flag();
18+
uint8_t USB_EP1_send();
19+
20+
#ifdef __cplusplus
21+
} // extern "C"
22+
#endif
23+
24+
#endif

0 commit comments

Comments
 (0)