Skip to content

Commit

Permalink
Add keyboard interface
Browse files Browse the repository at this point in the history
  • Loading branch information
rikka0w0 committed Jan 12, 2018
1 parent 6926ec6 commit 3bacefe
Show file tree
Hide file tree
Showing 11 changed files with 156 additions and 74 deletions.
13 changes: 13 additions & 0 deletions USBCompositeDevice.uvopt
Original file line number Diff line number Diff line change
Expand Up @@ -363,6 +363,19 @@
<RteFlg>0</RteFlg>
<bShared>0</bShared>
</File>
<File>
<GroupNumber>1</GroupNumber>
<FileNumber>16</FileNumber>
<FileType>1</FileType>
<tvExp>1</tvExp>
<Focus>0</Focus>
<tvExpOptDlg>0</tvExpOptDlg>
<bDave2>0</bDave2>
<PathWithFileName>.\usb_hid_keyboard.c</PathWithFileName>
<FilenameWithoutPath>usb_hid_keyboard.c</FilenameWithoutPath>
<RteFlg>0</RteFlg>
<bShared>0</bShared>
</File>
</Group>

</ProjectOpt>
7 changes: 6 additions & 1 deletion USBCompositeDevice.uvproj
Original file line number Diff line number Diff line change
Expand Up @@ -307,7 +307,7 @@
<InterruptVectorAddress>0</InterruptVectorAddress>
<VariousControls>
<MiscControls></MiscControls>
<Define></Define>
<Define>CLOCK_FREQ_24</Define>
<Undefine></Undefine>
<IncludePath></IncludePath>
</VariousControls>
Expand Down Expand Up @@ -436,6 +436,11 @@
<FileType>1</FileType>
<FilePath>.\main.c</FilePath>
</File>
<File>
<FileName>usb_hid_keyboard.c</FileName>
<FileType>1</FileType>
<FilePath>.\usb_hid_keyboard.c</FilePath>
</File>
</Files>
</Group>
</Groups>
Expand Down
3 changes: 3 additions & 0 deletions ch554_conf.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,4 +40,7 @@

void CH554_Init(void);

// Interrupt Handlers
void USBInterrupt(void);

#endif
34 changes: 21 additions & 13 deletions main.c
Original file line number Diff line number Diff line change
@@ -1,27 +1,35 @@
#include "types.h"

#include "ch554_conf.H"
#include "Delay.H"
#include "usb_endp.h"
#include "usb_hid_keyboard.h"

#include <string.h>
#include <stdio.h>

xdata uint8_t FLAG;
extern uint8_t Ready;
uint8_t keyState, kbdModifier, kbdKey;

void main(void) {
CH554_Init(); //CH559时钟选择配置
CH554_Init();

//printf("start ...\n");
//允许单片机中断
FLAG = 0;
Ready = 0;
printf("start ...\n");

keyState = KBD_STATE_IDLE;

while(1);
while(1) {
while(keyState == KBD_STATE_IDLE);
keyState = KBD_STATE_KEYDOWN;
USB_Keyboard_SendKey(kbdModifier, kbdKey);
while(keyState == KBD_STATE_KEYDOWN);
USB_Keyboard_SendKey(0, 0);
}
}

extern void DeviceInterrupt(void);
void USB(void) interrupt INT_NO_USB {
DeviceInterrupt();
/*
* According to SDCC specification, interrupt handlers MUST be placed within the file which contains
* the void main(void) function, otherwise SDCC won't be able to recognize it. It's not a bug but a feature.
* If you know how to fix this, please let me know.
*/
void USBInterruptEntry(void) interrupt INT_NO_USB {
USBInterrupt();
}

8 changes: 4 additions & 4 deletions usb_desc.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,19 +9,19 @@
#define USB_DESCSIZE_DEVICE 18 // Constant, DO NOT change
#define USB_DESCSIZE_CONFIG_H 0
#define USB_DESCSIZE_CONFIG_L 89 // Actual size of your CfgDesc, set according to your configuration
#define USB_STRINGDESC_COUNT 5 // Number of String Descriptors available
// Device Descriptor
extern code const uint8_t DevDesc[];
// Configuration Descriptor, Interface Descriptors, Endpoint Descriptors and ...
extern code const uint8_t CfgDesc[];
// String Descriptors
#define USB_STRINGDESC_COUNT 5 // Number of String Descriptors available
extern code const uint8_t* StringDescs[];

// HID Report Descriptors
#define USB_HIDREPSIZE_KEYBOARD 62
#define USB_HIDREPSIZE_VENDORDEF 52
extern code const uint8_t* USB_HID_REPDESCS[];
extern code const uint8_t USB_HID_REPDESCS_SIZE[];
#define USB_HIDREPSIZE_KEYBOARD 62
uint8_t* USB_HID_GetReportDesc(uint8_t interface);
uint8_t USB_HID_GetReportDesc_Length(uint8_t interface);


#endif /* __USB_DESC_H */
23 changes: 14 additions & 9 deletions usb_endp.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#include "types.h"
#include "usb_endp.h"
#include "usb_hid_keyboard.h"

#define MAX_PACKET_SIZE 64

Expand Down Expand Up @@ -55,30 +56,34 @@ uint8_t USB_EP_HALT_CLEAR(uint8_t ep) {
}
}

extern xdata uint8_t FLAG;
extern uint8_t keyState, kbdModifier, kbdKey;

void USB_EP1_IN(void) {
UEP1_T_LEN = 0; //预使用发送长度一定要清空
//UEP2_CTRL ^= bUEP_T_TOG; //如果不设置自动翻转则需要手动翻转
UEP1_CTRL = UEP1_CTRL & ~MASK_UEP_T_RES | UEP_T_RES_NAK; //默认应答NAK
UEP1_T_LEN = 0;
UEP1_CTRL = UEP1_CTRL & ~MASK_UEP_T_RES | UEP_T_RES_NAK; // No data to send

FLAG = 1;
if (keyState == KBD_STATE_KEYDOWN)
keyState = KBD_STATE_IDLE;
}

void USB_EP2_IN(void) {
UEP2_T_LEN = 0; //预使用发送长度一定要清空
//UEP1_CTRL ^= bUEP_T_TOG; //如果不设置自动翻转则需要手动翻转
UEP2_CTRL = UEP2_CTRL & ~MASK_UEP_T_RES | UEP_T_RES_NAK; //默认应答NAK
UEP2_T_LEN = 0;
UEP2_CTRL = UEP2_CTRL & ~MASK_UEP_T_RES | UEP_T_RES_NAK; // No data to send
}



void USB_EP2_OUT(void) {
uint8_t i;
if (U_TOG_OK) { // Discard unsynchronized packets
for (i = 0; i < USB_RX_LEN; i++)
EP2_TX_BUF[i] = EP2_RX_BUF[i] ^ 0xFF; // Invert bits and Tx to host (for validation)

if (EP2_RX_BUF[0]==0xEE) {
keyState = KBD_STATE_KEYDOWN;
kbdModifier = EP2_RX_BUF[1];
kbdKey = EP2_RX_BUF[2];
}

UEP2_T_LEN = USB_RX_LEN;
UEP2_CTRL = UEP2_CTRL & ~MASK_UEP_T_RES | UEP_T_RES_ACK; // Enable Tx
}
Expand Down
8 changes: 3 additions & 5 deletions usb_ep0.c
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ xdatabuf(EP0_ADDR, Ep0Buffer, 8 > (USB_ENDP0_SIZE + 2) ? 8 : (USB_ENDP0_SIZE + 2

#define UsbSetupBuf ((PUSB_SETUP_REQ)Ep0Buffer)

uint8_t SetupReq, SetupLen, Ready, UsbConfig;
uint8_t SetupReq, SetupLen, UsbConfig;
uint8_t* pDescr;

// Process SETUP packet
Expand Down Expand Up @@ -55,11 +55,9 @@ void USB_EP0_SETUP(void) {
case 0x22: // Report Descriptor
len = UsbSetupBuf->wIndexL;
if (len < USB_INTERFACES) {
if (len == USB_INTERFACES - 1)
Ready = 1;

pDescr = (uint8_t*)(USB_HID_REPDESCS[len]);
len = USB_HID_REPDESCS_SIZE[len];
pDescr = USB_HID_GetReportDesc(len);
len = USB_HID_GetReportDesc_Length(len);
} else {
len = 0xff; // The host is trying to config invalid interfaces
}
Expand Down
91 changes: 50 additions & 41 deletions usb_hid_desc.c
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
#include "usb_desc.h"
#include "types.h"

code const uint8_t KeyRepDesc[] =
{
code const uint8_t KeyRepDesc[] = {
0x05,0x01,0x09,0x06,0xA1,0x01,0x05,0x07,
0x19,0xe0,0x29,0xe7,0x15,0x00,0x25,0x01,
0x75,0x01,0x95,0x08,0x81,0x02,0x95,0x01,
Expand All @@ -14,46 +13,56 @@ code const uint8_t KeyRepDesc[] =
};

//gliethttp
code const uint8_t VendorDefDesc[] =
{
0x06, 0xA0, 0xFF, // Usage Page(FFA0h, vendor defined)
0x09, 0x01, // Usage(vendor defined)
0xA1, 0x01, // Collection(Application)
0x09, 0x02 , // Usage(vendor defined)
0xA1, 0x00, // Collection(Physical)
0x06,0xA1,0xFF, // Usage Page(vendor defined)
code const uint8_t VendorDefDesc[] = {
0x06, 0xA0, 0xFF, // Usage Page(FFA0h, vendor defined)
0x09, 0x01, // Usage(vendor defined)
0xA1, 0x01, // Collection(Application)
0x09, 0x02 , // Usage(vendor defined)
0xA1, 0x00, // Collection(Physical)
0x06,0xA1,0xFF, // Usage Page(vendor defined)

// Input Report
0x09, 0x03 , // Usage(vendor defined)
0x09, 0x04, // Usage(vendor defined)
0x15, 0x80, // Logical Minimum(0x80 or -128)
0x25, 0x7F, // Logical Maximum(0x7F or 127)
0x35, 0x00, // Physical Minimum(0)
0x45, 0xFF, // Physical Maximum(255)
0x75, 0x08, // Report size(8 Bits, 1 Byte)
0x95, 0x40, // Report count(64 fields)
0x81, 0x02, // Input(data, variable, absolute)
// Output Report
0x09, 0x05, // Usage(vendor defined)
0x09, 0x06, // Usage(vendor defined)
0x15, 0x80, // Logical Minimum(0x80 or -128)
0x25, 0x7F, // Logical Maximum(0x7F or 127)
0x35, 0x00, // Physical Minimum(0)
0x45,0xFF, // Physical Maximum(255)
0x75,0x08, // Report size(8 Bits, 1 Byte)
0x95, 0x40, // Report count(64 fields)
0x91, 0x02, // Output(data, variable, absolute)
0xC0, //End Collection(Physical)
0xC0 //End Collection(Application)
// Input Report
0x09, 0x03 , // Usage(vendor defined)
0x09, 0x04, // Usage(vendor defined)
0x15, 0x80, // Logical Minimum(0x80 or -128)
0x25, 0x7F, // Logical Maximum(0x7F or 127)
0x35, 0x00, // Physical Minimum(0)
0x45, 0xFF, // Physical Maximum(255)
0x75, 0x08, // Report size(8 Bits, 1 Byte)
0x95, 0x40, // Report count(64 fields)
0x81, 0x02, // Input(data, variable, absolute)
// Output Report
0x09, 0x05, // Usage(vendor defined)
0x09, 0x06, // Usage(vendor defined)
0x15, 0x80, // Logical Minimum(0x80 or -128)
0x25, 0x7F, // Logical Maximum(0x7F or 127)
0x35, 0x00, // Physical Minimum(0)
0x45,0xFF, // Physical Maximum(255)
0x75,0x08, // Report size(8 Bits, 1 Byte)
0x95, 0x40, // Report count(64 fields)
0x91, 0x02, // Output(data, variable, absolute)
0xC0, //End Collection(Physical)
0xC0 //End Collection(Application)
};

code const uint8_t* USB_HID_REPDESCS[] =
{
VendorDefDesc, // Interface 0
KeyRepDesc, // Interface 1
};
uint8_t* USB_HID_GetReportDesc(uint8_t interface) {
switch (interface){
case 0: // Interface 0
return VendorDefDesc;
case 1: // Interface 1
return KeyRepDesc;
default:
return 0;
}
}

code const uint8_t USB_HID_REPDESCS_SIZE[] = {
USB_HIDREPSIZE_VENDORDEF, // Interface 0
USB_HIDREPSIZE_KEYBOARD // Interface 1
};
uint8_t USB_HID_GetReportDesc_Length(uint8_t interface) {
switch (interface){
case 0: // Interface 0
return USB_HIDREPSIZE_VENDORDEF;
case 1: // Interface 1
return USB_HIDREPSIZE_KEYBOARD;
default:
return 0xFF; // Unsupported
}
}
17 changes: 17 additions & 0 deletions usb_hid_keyboard.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#include "usb_hid_keyboard.h"
#include "usb_endp.h"

void USB_Keyboard_SendKey(uint8_t modifier, uint8_t key) {
KBD_Tx_Buf[0] = modifier;
KBD_Tx_Buf[1] = 0; // reserved
KBD_Tx_Buf[2] = key; // key0

KBD_Tx_Buf[3] = 0; // key1
KBD_Tx_Buf[4] = 0; // key2
KBD_Tx_Buf[5] = 0; // key3
KBD_Tx_Buf[6] = 0; // key4
KBD_Tx_Buf[7] = 0; // key5
UEP1_T_LEN = 8;

KBD_Tx_Enable();
}
24 changes: 24 additions & 0 deletions usb_hid_keyboard.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#ifndef __USB_HID_KEYBOARD_H
#define __USB_HID_KEYBOARD_H

#include "types.h"

#define KBD_Tx_Buf Ep1Buffer
#define KBD_Tx_Enable() {UEP1_CTRL = UEP1_CTRL & ~ MASK_UEP_T_RES | UEP_T_RES_ACK;}

#define KBD_LCTRL 0x01
#define KBD_LSHIFT 0x02
#define KBD_LALT 0x04
#define KBD_LGUI 0x08
#define KBD_RCTRL 0x10
#define KBD_RSHIFT 0x20
#define KBD_RALT 0x40
#define KBD_RGUI 0x80

#define KBD_STATE_IDLE 0
#define KBD_STATE_KEYDOWN 1
#define KBD_STATE_KEYUP 2

void USB_Keyboard_SendKey(uint8_t modifier, uint8_t key);

#endif /* __USB_HID_KEYBOARD_H */
2 changes: 1 addition & 1 deletion usb_it.c
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ code const void (*pEndPoint_SETUP_CallBack[])(void) =
EP4_SETUP_Callback,
};

void DeviceInterrupt(void) {
void USBInterrupt(void) {
if(UIF_TRANSFER) {
// Dispatch to service functions
switch (USB_INT_ST & MASK_UIS_TOKEN) {
Expand Down

0 comments on commit 3bacefe

Please sign in to comment.