aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--common.mk1
-rw-r--r--common/debug.h19
-rw-r--r--common/host.h8
-rw-r--r--common/keyboard.h6
-rw-r--r--common/print.h13
-rw-r--r--common/sendchar.h8
-rw-r--r--common/timer.h12
-rw-r--r--protocol/lufa.mk10
-rw-r--r--protocol/lufa/lufa.c96
-rw-r--r--protocol/lufa/lufa.h11
-rw-r--r--protocol/usb_hid.mk67
-rw-r--r--protocol/usb_hid/NullSerial.cpp44
-rw-r--r--protocol/usb_hid/README19
-rw-r--r--protocol/usb_hid/USBAPI.h22
-rw-r--r--protocol/usb_hid/arduino-1.0.1/cores/arduino/USBDesc.h4
-rw-r--r--protocol/usb_hid/arduino-1.0.1/cores/arduino/wiring.c2
-rw-r--r--protocol/usb_hid/leonardo_led.h10
-rw-r--r--protocol/usb_hid/main.cpp66
-rw-r--r--protocol/usb_hid/override_Serial.cpp51
-rw-r--r--protocol/usb_hid/override_wiring.c28
-rw-r--r--protocol/usb_hid/parser.cpp15
-rw-r--r--protocol/usb_hid/parser.h5
-rw-r--r--protocol/usb_hid/test/Makefile (renamed from protocol/usb_hid/Makefile)119
-rw-r--r--protocol/usb_hid/test/config.h40
-rw-r--r--protocol/usb_hid/test/test.cpp92
-rw-r--r--rules.mk9
26 files changed, 505 insertions, 272 deletions
diff --git a/common.mk b/common.mk
index 580a8cf8f..26b4bb859 100644
--- a/common.mk
+++ b/common.mk
@@ -5,6 +5,7 @@ SRC += $(COMMON_DIR)/host.c \
5 $(COMMON_DIR)/layer.c \ 5 $(COMMON_DIR)/layer.c \
6 $(COMMON_DIR)/timer.c \ 6 $(COMMON_DIR)/timer.c \
7 $(COMMON_DIR)/print.c \ 7 $(COMMON_DIR)/print.c \
8 $(COMMON_DIR)/debug.c \
8 $(COMMON_DIR)/bootloader.c \ 9 $(COMMON_DIR)/bootloader.c \
9 $(COMMON_DIR)/util.c 10 $(COMMON_DIR)/util.c
10 11
diff --git a/common/debug.h b/common/debug.h
index 230d3b349..9cc8d882f 100644
--- a/common/debug.h
+++ b/common/debug.h
@@ -18,19 +18,28 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
18#ifndef DEBUG_H 18#ifndef DEBUG_H
19#define DEBUG_H 1 19#define DEBUG_H 1
20 20
21#include <stdbool.h>
21#include "print.h" 22#include "print.h"
22 23
23 24
24#define debug(s) if(debug_enable) print(s) 25#define debug(s) if(debug_enable) print_P(PSTR(s))
25#define debug_hex(c) if(debug_enable) phex(c) 26#define debug_hex(c) if(debug_enable) phex(c)
26#define debug_hex16(i) if(debug_enable) phex16(i) 27#define debug_hex16(i) if(debug_enable) phex16(i)
27#define debug_bin(c) if(debug_enable) pbin(c) 28#define debug_bin(c) if(debug_enable) pbin(c)
28#define debug_bin_reverse(c) if(debug_enable) pbin_reverse(c) 29#define debug_bin_reverse(c) if(debug_enable) pbin_reverse(c)
29 30
30 31
31bool debug_enable; 32#ifdef __cplusplus
32bool debug_matrix; 33extern "C" {
33bool debug_keyboard; 34#endif
34bool debug_mouse; 35
36extern bool debug_enable;
37extern bool debug_matrix;
38extern bool debug_keyboard;
39extern bool debug_mouse;
40
41#ifdef __cplusplus
42}
43#endif
35 44
36#endif 45#endif
diff --git a/common/host.h b/common/host.h
index 11b9aacd7..26bf3c362 100644
--- a/common/host.h
+++ b/common/host.h
@@ -23,6 +23,10 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
23#include "host_driver.h" 23#include "host_driver.h"
24 24
25 25
26#ifdef __cplusplus
27extern "C" {
28#endif
29
26#ifdef NKRO_ENABLE 30#ifdef NKRO_ENABLE
27extern bool keyboard_nkro; 31extern bool keyboard_nkro;
28#endif 32#endif
@@ -54,4 +58,8 @@ void host_mouse_send(report_mouse_t *report);
54void host_system_send(uint16_t data); 58void host_system_send(uint16_t data);
55void host_consumer_send(uint16_t data); 59void host_consumer_send(uint16_t data);
56 60
61#ifdef __cplusplus
62}
63#endif
64
57#endif 65#endif
diff --git a/common/keyboard.h b/common/keyboard.h
index 988dac36e..51bf67379 100644
--- a/common/keyboard.h
+++ b/common/keyboard.h
@@ -21,8 +21,14 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
21#include <stdint.h> 21#include <stdint.h>
22 22
23 23
24#ifdef __cplusplus
25extern "C" {
26#endif
24void keyboard_init(void); 27void keyboard_init(void);
25void keyboard_proc(void); 28void keyboard_proc(void);
26void keyboard_set_leds(uint8_t leds); 29void keyboard_set_leds(uint8_t leds);
30#ifdef __cplusplus
31}
32#endif
27 33
28#endif 34#endif
diff --git a/common/print.h b/common/print.h
index 686fa89ac..d55f5695d 100644
--- a/common/print.h
+++ b/common/print.h
@@ -29,11 +29,17 @@
29#include <avr/pgmspace.h> 29#include <avr/pgmspace.h>
30 30
31 31
32extern bool print_enable; 32// avoid collision with arduino/Print.h
33 33#ifndef __cplusplus
34// this macro allows you to write print("some text") and 34// this macro allows you to write print("some text") and
35// the string is automatically placed into flash memory :) 35// the string is automatically placed into flash memory :)
36#define print(s) print_P(PSTR(s)) 36#define print(s) print_P(PSTR(s))
37#endif
38
39#ifdef __cplusplus
40extern "C" {
41#endif
42extern bool print_enable;
37 43
38void print_S(const char *s); 44void print_S(const char *s);
39void print_P(const char *s); 45void print_P(const char *s);
@@ -41,5 +47,8 @@ void phex(unsigned char c);
41void phex16(unsigned int i); 47void phex16(unsigned int i);
42void pbin(unsigned char c); 48void pbin(unsigned char c);
43void pbin_reverse(unsigned char c); 49void pbin_reverse(unsigned char c);
50#ifdef __cplusplus
51}
52#endif
44 53
45#endif 54#endif
diff --git a/common/sendchar.h b/common/sendchar.h
index 7c81303c7..7a64d00c7 100644
--- a/common/sendchar.h
+++ b/common/sendchar.h
@@ -21,7 +21,15 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
21#include <stdint.h> 21#include <stdint.h>
22 22
23 23
24#ifdef __cplusplus
25extern "C" {
26#endif
27
24/* transmit a character. return 0 on success, -1 on error. */ 28/* transmit a character. return 0 on success, -1 on error. */
25int8_t sendchar(uint8_t c); 29int8_t sendchar(uint8_t c);
26 30
31#ifdef __cplusplus
32}
33#endif
34
27#endif 35#endif
diff --git a/common/timer.h b/common/timer.h
index f9e8181e6..d24d3eab6 100644
--- a/common/timer.h
+++ b/common/timer.h
@@ -23,10 +23,12 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
23#ifndef TIMER_PRESCALER 23#ifndef TIMER_PRESCALER
24# if F_CPU > 16000000 24# if F_CPU > 16000000
25# define TIMER_PRESCALER 256 25# define TIMER_PRESCALER 256
26# elif F_CPU >= 4000000 26# elif F_CPU > 2000000
27# define TIMER_PRESCALER 64 27# define TIMER_PRESCALER 64
28# else 28# elif F_CPU > 250000
29# define TIMER_PRESCALER 8 29# define TIMER_PRESCALER 8
30# else
31# define TIMER_PRESCALER 1
30# endif 32# endif
31#endif 33#endif
32#define TIMER_RAW_FREQ (F_CPU/TIMER_PRESCALER) 34#define TIMER_RAW_FREQ (F_CPU/TIMER_PRESCALER)
@@ -42,6 +44,9 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
42#define TIMER_DIFF_MS(a, b) TIMER_DIFF(a, b, UINT16_MAX) 44#define TIMER_DIFF_MS(a, b) TIMER_DIFF(a, b, UINT16_MAX)
43 45
44 46
47#ifdef __cplusplus
48extern "C" {
49#endif
45extern volatile uint16_t timer_count; 50extern volatile uint16_t timer_count;
46 51
47 52
@@ -49,5 +54,8 @@ void timer_init(void);
49void timer_clear(void); 54void timer_clear(void);
50uint16_t timer_read(void); 55uint16_t timer_read(void);
51uint16_t timer_elapsed(uint16_t last); 56uint16_t timer_elapsed(uint16_t last);
57#ifdef __cplusplus
58}
59#endif
52 60
53#endif 61#endif
diff --git a/protocol/lufa.mk b/protocol/lufa.mk
index ad6cab6d3..bf6fc97b8 100644
--- a/protocol/lufa.mk
+++ b/protocol/lufa.mk
@@ -1,18 +1,20 @@
1LUFA_DIR = protocol/lufa 1LUFA_DIR = protocol/lufa
2 2
3# Path to the LUFA library 3# Path to the LUFA library
4LUFA_PATH = $(TOP_DIR)/protocol/lufa/LUFA-120219 4LUFA_PATH = protocol/lufa/LUFA-120219
5 5
6# Create the LUFA source path variables by including the LUFA root makefile 6# Create the LUFA source path variables by including the LUFA root makefile
7include $(LUFA_PATH)/LUFA/makefile 7include $(TOP_DIR)/$(LUFA_PATH)/LUFA/makefile
8 8
9LUFA_SRC = $(LUFA_DIR)/lufa.c \ 9LUFA_SRC = $(LUFA_DIR)/lufa.c \
10 $(LUFA_DIR)/descriptor.c \ 10 $(LUFA_DIR)/descriptor.c \
11 $(LUFA_SRC_USB) 11 $(LUFA_SRC_USB)
12SRC += $(subst $(LUFA_PATH)/,,$(LUFA_SRC)) 12
13SRC += $(LUFA_SRC)
13 14
14# Search Path 15# Search Path
15VPATH += $(LUFA_PATH) 16VPATH += $(TOP_DIR)/$(LUFA_DIR)
17VPATH += $(TOP_DIR)/$(LUFA_PATH)
16 18
17# Option modules 19# Option modules
18#ifdef $(or MOUSEKEY_ENABLE, PS2_MOUSE_ENABLE) 20#ifdef $(or MOUSEKEY_ENABLE, PS2_MOUSE_ENABLE)
diff --git a/protocol/lufa/lufa.c b/protocol/lufa/lufa.c
index b7ecb8488..81409ce9e 100644
--- a/protocol/lufa/lufa.c
+++ b/protocol/lufa/lufa.c
@@ -59,7 +59,7 @@ static void send_keyboard(report_keyboard_t *report);
59static void send_mouse(report_mouse_t *report); 59static void send_mouse(report_mouse_t *report);
60static void send_system(uint16_t data); 60static void send_system(uint16_t data);
61static void send_consumer(uint16_t data); 61static void send_consumer(uint16_t data);
62static host_driver_t lufa_driver = { 62host_driver_t lufa_driver = {
63 keyboard_leds, 63 keyboard_leds,
64 send_keyboard, 64 send_keyboard,
65 send_mouse, 65 send_mouse,
@@ -68,49 +68,9 @@ static host_driver_t lufa_driver = {
68}; 68};
69 69
70 70
71static void SetupHardware(void); 71/*******************************************************************************
72static void Console_Task(void); 72 * Console
73 73 ******************************************************************************/
74int main(void)
75{
76 SetupHardware();
77 sei();
78
79 print_enable = true;
80 debug_enable = true;
81 debug_matrix = true;
82 debug_keyboard = true;
83 debug_mouse = true;
84
85 // TODO: can't print here
86 debug("LUFA init\n");
87
88 keyboard_init();
89 host_set_driver(&lufa_driver);
90 while (1) {
91 keyboard_proc();
92
93#if !defined(INTERRUPT_CONTROL_ENDPOINT)
94 USB_USBTask();
95#endif
96 }
97}
98
99void SetupHardware(void)
100{
101 /* Disable watchdog if enabled by bootloader/fuses */
102 MCUSR &= ~(1 << WDRF);
103 wdt_disable();
104
105 /* Disable clock division */
106 clock_prescale_set(clock_div_1);
107
108 USB_Init();
109
110 // for Console_Task
111 USB_Device_EnableSOFEvents();
112}
113
114#ifdef CONSOLE_ENABLE 74#ifdef CONSOLE_ENABLE
115static void Console_Task(void) 75static void Console_Task(void)
116{ 76{
@@ -446,3 +406,51 @@ int8_t sendchar(uint8_t c)
446 return 0; 406 return 0;
447} 407}
448#endif 408#endif
409
410
411/*******************************************************************************
412 * main
413 ******************************************************************************/
414static void SetupHardware(void)
415{
416 /* Disable watchdog if enabled by bootloader/fuses */
417 MCUSR &= ~(1 << WDRF);
418 wdt_disable();
419
420 /* Disable clock division */
421 clock_prescale_set(clock_div_1);
422
423 // Leonardo needs. Without this USB device is not recognized.
424 USB_Disable();
425
426 USB_Init();
427
428 // for Console_Task
429 USB_Device_EnableSOFEvents();
430}
431
432int main(void) __attribute__ ((weak));
433int main(void)
434{
435 SetupHardware();
436 sei();
437
438 print_enable = true;
439 debug_enable = true;
440 debug_matrix = true;
441 debug_keyboard = true;
442 debug_mouse = true;
443
444 // TODO: can't print here
445 debug("LUFA init\n");
446
447 keyboard_init();
448 host_set_driver(&lufa_driver);
449 while (1) {
450 keyboard_proc();
451
452#if !defined(INTERRUPT_CONTROL_ENDPOINT)
453 USB_USBTask();
454#endif
455 }
456}
diff --git a/protocol/lufa/lufa.h b/protocol/lufa/lufa.h
index 71c279b0d..bcee060d6 100644
--- a/protocol/lufa/lufa.h
+++ b/protocol/lufa/lufa.h
@@ -47,8 +47,19 @@
47#include <string.h> 47#include <string.h>
48#include <LUFA/Version.h> 48#include <LUFA/Version.h>
49#include <LUFA/Drivers/USB/USB.h> 49#include <LUFA/Drivers/USB/USB.h>
50#include "host.h"
50 51
51 52
53#ifdef __cplusplus
54extern "C" {
55#endif
56
57extern host_driver_t lufa_driver;
58
59#ifdef __cplusplus
60}
61#endif
62
52/* extra report structure */ 63/* extra report structure */
53typedef struct { 64typedef struct {
54 uint8_t report_id; 65 uint8_t report_id;
diff --git a/protocol/usb_hid.mk b/protocol/usb_hid.mk
new file mode 100644
index 000000000..f3149f7f4
--- /dev/null
+++ b/protocol/usb_hid.mk
@@ -0,0 +1,67 @@
1USB_HID_DIR = protocol/usb_hid
2
3
4#
5# USB Host Shield
6#
7USB_HOST_SHIELD_DIR = $(USB_HID_DIR)/USB_Host_Shield_2.0
8USB_HOST_SHIELD_SRC = \
9 $(USB_HOST_SHIELD_DIR)/Usb.cpp \
10 $(USB_HOST_SHIELD_DIR)/hid.cpp \
11 $(USB_HOST_SHIELD_DIR)/parsetools.cpp \
12 $(USB_HOST_SHIELD_DIR)/message.cpp
13
14
15
16#
17# Arduino
18#
19ARDUINO_DIR = $(USB_HID_DIR)/arduino-1.0.1
20ARDUINO_CORES_DIR = $(ARDUINO_DIR)/cores/arduino
21ARDUINO_CORES_SRC = \
22 $(ARDUINO_CORES_DIR)/Print.cpp \
23 $(ARDUINO_CORES_DIR)/Stream.cpp
24
25# replaced with override_Serial.c
26# $(ARDUINO_CORES_DIR)/CDC.cpp \
27# $(ARDUINO_CORES_DIR)/HID.cpp \
28# $(ARDUINO_CORES_DIR)/USBCore.cpp \
29
30# replaced with override_wiring.c and common/timer.c
31# $(ARDUINO_CORES_DIR)/wiring.c \
32
33
34
35#
36# HID parser
37#
38SRC += $(USB_HID_DIR)/parser.cpp
39
40# replace arduino/CDC.cpp
41SRC += $(USB_HID_DIR)/override_Serial.cpp
42
43# replace arduino/wiring.c
44SRC += $(USB_HID_DIR)/override_wiring.c
45SRC += common/timer.c
46
47SRC += $(USB_HOST_SHIELD_SRC)
48SRC += $(ARDUINO_CORES_SRC)
49
50
51OPT_DEFS += -DARDUINO=101
52# Arduino USBCore needs USB_VID and USB_PID.
53#OPT_DEFS += -DARDUINO=101 -DUSB_VID=0x2341 -DUSB_PID=0x8036
54
55
56
57#
58# Search Path
59#
60VPATH += $(TOP_DIR)/$(USB_HID_DIR)
61VPATH += $(TOP_DIR)/$(USB_HOST_SHIELD_DIR)
62
63# for #include "Arduino.h"
64VPATH += $(TOP_DIR)/$(ARDUINO_CORES_DIR)
65
66# for #include "pins_arduino.h"
67VPATH += $(TOP_DIR)/$(ARDUINO_DIR)/variants/leonardo
diff --git a/protocol/usb_hid/NullSerial.cpp b/protocol/usb_hid/NullSerial.cpp
deleted file mode 100644
index 6d85caf2d..000000000
--- a/protocol/usb_hid/NullSerial.cpp
+++ /dev/null
@@ -1,44 +0,0 @@
1#include "USBAPI.h"
2
3
4void NullSerial::begin(uint16_t baud_count)
5{
6}
7
8void NullSerial::end(void)
9{
10}
11
12void NullSerial::accept(void)
13{
14}
15
16int NullSerial::available(void)
17{
18 return 0;
19}
20
21int NullSerial::peek(void)
22{
23 return -1;
24}
25
26int NullSerial::read(void)
27{
28 return -1;
29}
30
31void NullSerial::flush(void)
32{
33}
34
35size_t NullSerial::write(uint8_t c)
36{
37 return 1;
38}
39
40NullSerial::operator bool() {
41 return true;
42}
43
44NullSerial Serial;
diff --git a/protocol/usb_hid/README b/protocol/usb_hid/README
index 0acf677f8..9750a1cc8 100644
--- a/protocol/usb_hid/README
+++ b/protocol/usb_hid/README
@@ -1,3 +1,8 @@
1USB HID protocol
2================
3Host side of USB HID keyboard protocol implementation.
4Only standard HID Boot mode is supported at this time. This means most of normal keyboards are supported while proprietary >6KRO and NKRO is not.
5
1Third party Libraries 6Third party Libraries
2--------------------- 7---------------------
3USB_Host_Shield_2.0 8USB_Host_Shield_2.0
@@ -7,3 +12,17 @@ USB_Host_Shield_2.0
7arduino-1.0.1 12arduino-1.0.1
8 Arduino files copied from: 13 Arduino files copied from:
9 https://github.com/arduino/Arduino/hardware/arduino/{cores,variants} 14 https://github.com/arduino/Arduino/hardware/arduino/{cores,variants}
15
16
17Test build
18----------
19In test directory;
20 $ make
21 $ DEV=/dev/ttyACM0 make program
22
23You can see HID keyboard reports on debug output.
24
25
26Restriction and Bug
27-------------------
28Not statble yet.
diff --git a/protocol/usb_hid/USBAPI.h b/protocol/usb_hid/USBAPI.h
deleted file mode 100644
index e3390d49a..000000000
--- a/protocol/usb_hid/USBAPI.h
+++ /dev/null
@@ -1,22 +0,0 @@
1/*
2 * Override original arduino USBAPI.h.
3 */
4#include <stdint.h>
5#include "Stream.h"
6
7
8class NullSerial : public Stream
9{
10public:
11 void begin(uint16_t baud_count);
12 void end(void);
13
14 virtual int available(void);
15 virtual void accept(void);
16 virtual int peek(void);
17 virtual int read(void);
18 virtual void flush(void);
19 virtual size_t write(uint8_t);
20 operator bool();
21};
22extern NullSerial Serial;
diff --git a/protocol/usb_hid/arduino-1.0.1/cores/arduino/USBDesc.h b/protocol/usb_hid/arduino-1.0.1/cores/arduino/USBDesc.h
index 229a638c5..900713e0f 100644
--- a/protocol/usb_hid/arduino-1.0.1/cores/arduino/USBDesc.h
+++ b/protocol/usb_hid/arduino-1.0.1/cores/arduino/USBDesc.h
@@ -16,8 +16,8 @@
16** SOFTWARE. 16** SOFTWARE.
17*/ 17*/
18 18
19//#define CDC_ENABLED 19#define CDC_ENABLED
20//#define HID_ENABLED 20#define HID_ENABLED
21 21
22 22
23#ifdef CDC_ENABLED 23#ifdef CDC_ENABLED
diff --git a/protocol/usb_hid/arduino-1.0.1/cores/arduino/wiring.c b/protocol/usb_hid/arduino-1.0.1/cores/arduino/wiring.c
index b1a4ce4b0..ac8bb6f9b 100644
--- a/protocol/usb_hid/arduino-1.0.1/cores/arduino/wiring.c
+++ b/protocol/usb_hid/arduino-1.0.1/cores/arduino/wiring.c
@@ -108,7 +108,6 @@ unsigned long micros() {
108 108
109void delay(unsigned long ms) 109void delay(unsigned long ms)
110{ 110{
111//PORTB &= ~(1<<0);
112 uint16_t start = (uint16_t)micros(); 111 uint16_t start = (uint16_t)micros();
113 112
114 while (ms > 0) { 113 while (ms > 0) {
@@ -117,7 +116,6 @@ void delay(unsigned long ms)
117 start += 1000; 116 start += 1000;
118 } 117 }
119 } 118 }
120//PORTB |= (1<<0);
121} 119}
122 120
123/* Delay for the given number of microseconds. Assumes a 8 or 16 MHz clock. */ 121/* Delay for the given number of microseconds. Assumes a 8 or 16 MHz clock. */
diff --git a/protocol/usb_hid/leonardo_led.h b/protocol/usb_hid/leonardo_led.h
new file mode 100644
index 000000000..6f67a88f5
--- /dev/null
+++ b/protocol/usb_hid/leonardo_led.h
@@ -0,0 +1,10 @@
1#ifndef LEONARDO_LED_H
2#define LEONARDO_LED_H
3
4// Leonardo "TX" LED for debug
5#define LED_TX_INIT (DDRD |= (1<<5))
6#define LED_TX_ON (PORTD &= ~(1<<5))
7#define LED_TX_OFF (PORTD |= (1<<5))
8#define LED_TX_TOGGLE (PORTD ^= (1<<5))
9
10#endif
diff --git a/protocol/usb_hid/main.cpp b/protocol/usb_hid/main.cpp
deleted file mode 100644
index c292d458e..000000000
--- a/protocol/usb_hid/main.cpp
+++ /dev/null
@@ -1,66 +0,0 @@
1#include <util/delay.h>
2#include <Arduino.h>
3#include "Usb.h"
4#include "hid.h"
5#include "hidboot.h"
6#include "parser.h"
7
8
9USB Usb;
10HIDBoot<HID_PROTOCOL_KEYBOARD> kbd(&Usb);
11KBDReportParser Prs;
12
13void usb_disable()
14{
15 USBCON &= ~(1<<VBUSTI);
16 UDIEN = 0;
17 USBINT = 0;
18 UDINT = 0;
19 UDCON |= (1<<DETACH);
20 USBCON &= ~(1<<USBE);
21 PLLCSR = 0;
22 UHWCON &= ~(1<<UVREGE);
23 USBCON &= ~(1<<OTGPADE);
24}
25
26void setup()
27{
28 usb_disable();
29
30 // RX LED for debug
31 DDRB |= (1<<0);
32
33 Serial.begin( 115200 );
34 while (!Serial) ;
35
36 delay( 1000 );
37
38 Serial.println("Start");
39
40 if (Usb.Init() == -1) {
41 Serial.println("OSC did not start.");
42 }
43
44 delay( 200 );
45
46 kbd.SetReportParser(0, (HIDReportParser*)&Prs);
47}
48
49void loop()
50{
51 Usb.Task();
52}
53
54int main(void)
55{
56 // arduino/wiring.c(Timer initialize)
57 init();
58
59 setup();
60
61 for (;;) {
62 loop();
63 }
64
65 return 0;
66}
diff --git a/protocol/usb_hid/override_Serial.cpp b/protocol/usb_hid/override_Serial.cpp
new file mode 100644
index 000000000..e1755a5dc
--- /dev/null
+++ b/protocol/usb_hid/override_Serial.cpp
@@ -0,0 +1,51 @@
1/*
2 * Null implementation of Serial to dump debug print into blackhole
3 */
4#include "Arduino.h"
5#include "sendchar.h"
6
7#include "USBAPI.h"
8
9
10void Serial_::begin(uint16_t baud_count)
11{
12}
13
14void Serial_::end(void)
15{
16}
17
18void Serial_::accept(void)
19{
20}
21
22int Serial_::available(void)
23{
24 return 0;
25}
26
27int Serial_::peek(void)
28{
29 return -1;
30}
31
32int Serial_::read(void)
33{
34 return -1;
35}
36
37void Serial_::flush(void)
38{
39}
40
41size_t Serial_::write(uint8_t c)
42{
43 sendchar(c);
44 return 1;
45}
46
47Serial_::operator bool() {
48 return true;
49}
50
51Serial_ Serial;
diff --git a/protocol/usb_hid/override_wiring.c b/protocol/usb_hid/override_wiring.c
new file mode 100644
index 000000000..a8c28dd89
--- /dev/null
+++ b/protocol/usb_hid/override_wiring.c
@@ -0,0 +1,28 @@
1/*
2 * To keep Timer0 for common/timer.c override arduino/wiring.c.
3 */
4#include <util/delay.h>
5#include "common/timer.h"
6#include "Arduino.h"
7
8
9unsigned long millis()
10{
11 return timer_read();
12}
13unsigned long micros()
14{
15 return timer_read() * 1000UL;
16}
17void delay(unsigned long ms)
18{
19 _delay_ms(ms);
20}
21void delayMicroseconds(unsigned int us)
22{
23 _delay_us(us);
24}
25void init()
26{
27 timer_init();
28}
diff --git a/protocol/usb_hid/parser.cpp b/protocol/usb_hid/parser.cpp
index cf6432230..b03af8ae4 100644
--- a/protocol/usb_hid/parser.cpp
+++ b/protocol/usb_hid/parser.cpp
@@ -1,15 +1,14 @@
1#include "parser.h" 1#include "parser.h"
2#include "leonardo_led.h"
3#include "debug.h"
2 4
3void KBDReportParser::Parse(HID *hid, bool is_rpt_id, uint8_t len, uint8_t *buf) 5void KBDReportParser::Parse(HID *hid, bool is_rpt_id, uint8_t len, uint8_t *buf)
4{ 6{
5 PORTB ^= (1<<0); 7 LED_TX_TOGGLE;
6/* 8 debug("KBDReport: ");
7 Serial.print("KBDReport: ");
8 for (uint8_t i = 0; i < len; i++) { 9 for (uint8_t i = 0; i < len; i++) {
9 PrintHex<uint8_t>(buf[i]); 10 debug_hex(buf[i]);
10 Serial.print(" "); 11 debug(" ");
11 } 12 }
12 Serial.print("\r\n"); 13 debug("\r\n");
13*/
14 //PORTC &= ~(1<<7);
15} 14}
diff --git a/protocol/usb_hid/parser.h b/protocol/usb_hid/parser.h
index dc14c8270..703eb1ed4 100644
--- a/protocol/usb_hid/parser.h
+++ b/protocol/usb_hid/parser.h
@@ -1,3 +1,6 @@
1#ifndef PARSER_H
2#define PARSER_H
3
1#include "hid.h" 4#include "hid.h"
2 5
3class KBDReportParser : public HIDReportParser 6class KBDReportParser : public HIDReportParser
@@ -5,3 +8,5 @@ class KBDReportParser : public HIDReportParser
5public: 8public:
6 virtual void Parse(HID *hid, bool is_rpt_id, uint8_t len, uint8_t *buf); 9 virtual void Parse(HID *hid, bool is_rpt_id, uint8_t len, uint8_t *buf);
7}; 10};
11
12#endif
diff --git a/protocol/usb_hid/Makefile b/protocol/usb_hid/test/Makefile
index ed3d6518d..39f5de455 100644
--- a/protocol/usb_hid/Makefile
+++ b/protocol/usb_hid/test/Makefile
@@ -39,7 +39,9 @@
39#---------------------------------------------------------------------------- 39#----------------------------------------------------------------------------
40 40
41# Target file name (without extension). 41# Target file name (without extension).
42TARGET = usbkbd 42TARGET = usb_hid_test
43
44TOP_DIR = ../../..
43 45
44# Directory keyboard dependent files exist 46# Directory keyboard dependent files exist
45TARGET_DIR = . 47TARGET_DIR = .
@@ -63,79 +65,62 @@ F_CPU = 16000000
63 65
64 66
65 67
68#
69# LUFA specific
70#
71# Target architecture (see library "Board Types" documentation).
72ARCH = AVR8
73# Input clock frequency.
74# This will define a symbol, F_USB, in all source code files equal to the
75# input clock frequency (before any prescaling is performed) in Hz. This value may
76# differ from F_CPU if prescaling is used on the latter, and is required as the
77# raw input clock is fed directly to the PLL sections of the AVR for high speed
78# clock generation for the USB and other AVR subsections. Do NOT tack on a 'UL'
79# at the end, this will be done automatically to create a 32-bit value in your
80# source code.
81#
82# If no clock division is performed on the input clock inside the AVR (via the
83# CPU clock adjust registers or the clock division fuses), this will be equal to F_CPU.
84F_USB = $(F_CPU)
85# Interrupt driven control endpoint task
86OPT_DEFS += -DINTERRUPT_CONTROL_ENDPOINT
87
88
89
90# Build Options
91# comment out to disable the options.
92#
93# Console for debug
94OPT_DEFS += -DCONSOLE_ENABLE
95
96# Boot Section Size in bytes
97# Teensy halfKay 512
98# Atmel DFU loader 4096
99# LUFA bootloader 4096
100#OPT_DEFS += -DBOOT_SIZE=4096
101
102
103
104SRC = test.cpp
105SRC += common/debug.c
106SRC += common/print.c
107
108CONFIG_H = config.h
109
66 110
67ARDUINO_DIR = arduino-1.0.1/cores
68ARDUINO_SRC = \
69 arduino/Print.cpp \
70 arduino/Stream.cpp \
71 arduino/wiring.c
72
73# arduino/main.cpp \
74# arduino/USBCore.cpp \
75# arduino/CDC.cpp \
76# arduino/HID.cpp \
77# arduino/HardwareSerial.cpp \
78# arduino/IPAddress.cpp \
79# arduino/Tone.cpp \
80# arduino/WMath.cpp \
81# arduino/WInterrupts.c \
82# arduino/wiring_analog.c \
83# arduino/wiring_pulse.c \
84# arduino/wiring_shift.c
85# arduino/wiring_digital.c \
86# arduino/WString.cpp \
87# arduino/new.cpp \
88
89USB_HOST_DIR = ./USB_Host_Shield_2.0
90USB_HOST_SRC = \
91 Usb.cpp \
92 cdcacm.cpp \
93 cdcftdi.cpp \
94 cdcprolific.cpp \
95 hid.cpp \
96 hidboot.cpp \
97 hiduniversal.cpp \
98 hidusagetitlearrays.cpp \
99 hidescriptorparser.cpp \
100 message.cpp \
101 parsetools.cpp
102
103 #PS3BT.cpp \
104 #PS3USB.cpp \
105 #RFCOMM.cpp \
106 #XBOXUSB.cpp \
107 #adk.cpp \
108 #masstorage.cpp \
109 #max_LCD.cpp \
110 #usbhub.cpp
111
112#SRC = host_kbd.cpp
113SRC = main.cpp
114SRC += parser.cpp
115SRC += NullSerial.cpp
116SRC += $(USB_HOST_SRC)
117SRC += $(ARDUINO_SRC)
118
119OPT_DEFS = -DARDUINO=101 -DUSB_VID=0x2341 -DUSB_PID=0x8036
120 111
121# Search Path 112# Search Path
122VPATH += $(TARGET_DIR) 113VPATH += $(TARGET_DIR)
123VPATH += $(USB_HOST_DIR) 114VPATH += $(TOP_DIR)
124VPATH += $(ARDUINO_DIR) 115VPATH += $(TOP_DIR)/common
125# for Arduino.h
126VPATH += arduino-1.0.1/cores/arduino
127# for pins_arduino.h
128VPATH += arduino-1.0.1/variants/leonardo
129 116
130 117
131# Ad hoc workaround to override original arduino/USBAPI.h with our own USBAPI.h.
132# Obsolete but needed in order to remove directory including the current input file from search list.
133# Option -iquote can't replace -I- for this purpose.
134EXTRAFLAGS += -I-
135
136 118
137# program Leonardo 119# program Leonardo
138PROGRAM_CMD = avrdude -patmega32u4 -cavr109 -P$(DEV) -b57600 -Uflash:w:$(TARGET).hex 120PROGRAM_CMD = avrdude -p$(MCU) -cavr109 -P$(DEV) -b57600 -Uflash:w:$(TARGET).hex
121
139 122
140 123
141include ../../rules.mk 124include $(TOP_DIR)/protocol/usb_hid.mk
125include $(TOP_DIR)/protocol/lufa.mk
126include $(TOP_DIR)/rules.mk
diff --git a/protocol/usb_hid/test/config.h b/protocol/usb_hid/test/config.h
new file mode 100644
index 000000000..c2230fb57
--- /dev/null
+++ b/protocol/usb_hid/test/config.h
@@ -0,0 +1,40 @@
1/*
2Copyright 2012 Jun Wako <wakojun@gmail.com>
3
4This program is free software: you can redistribute it and/or modify
5it under the terms of the GNU General Public License as published by
6the Free Software Foundation, either version 2 of the License, or
7(at your option) any later version.
8
9This program is distributed in the hope that it will be useful,
10but WITHOUT ANY WARRANTY; without even the implied warranty of
11MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12GNU General Public License for more details.
13
14You should have received a copy of the GNU General Public License
15along with this program. If not, see <http://www.gnu.org/licenses/>.
16*/
17
18#ifndef CONFIG_H
19#define CONFIG_H
20
21
22#define VENDOR_ID 0xFEED
23#define PRODUCT_ID 0xCAFE
24#define DEVICE_VER 0x0814
25#define MANUFACTURER t.m.k.
26#define PRODUCT USB to USB keyboard converter
27
28
29#define DESCRIPTION Product from t.m.k. keyboard firmware project
30
31
32/* matrix size */
33#define MATRIX_ROWS 32
34#define MATRIX_COLS 8
35
36
37/* key combination for command */
38#define IS_COMMAND() (keyboard_report->mods == (MOD_BIT(KB_LSHIFT) | MOD_BIT(KB_RSHIFT)))
39
40#endif
diff --git a/protocol/usb_hid/test/test.cpp b/protocol/usb_hid/test/test.cpp
new file mode 100644
index 000000000..4958f0c61
--- /dev/null
+++ b/protocol/usb_hid/test/test.cpp
@@ -0,0 +1,92 @@
1#include <avr/io.h>
2#include <avr/wdt.h>
3#include <avr/power.h>
4#include <util/delay.h>
5#include <Arduino.h>
6
7// USB HID host
8#include "Usb.h"
9#include "hid.h"
10#include "hidboot.h"
11#include "parser.h"
12
13// LUFA
14#include "lufa.h"
15
16#include "debug.h"
17
18#include "leonardo_led.h"
19
20
21static USB usb_host;
22static HIDBoot<HID_PROTOCOL_KEYBOARD> kbd(&usb_host);
23static KBDReportParser kbd_parser;
24
25static void LUFA_setup(void)
26{
27 /* Disable watchdog if enabled by bootloader/fuses */
28 MCUSR &= ~(1 << WDRF);
29 wdt_disable();
30
31 /* Disable clock division */
32 clock_prescale_set(clock_div_1);
33
34 // Leonardo needs. Without this USB device is not recognized.
35 USB_Disable();
36
37 USB_Init();
38
39 // for Console_Task
40 USB_Device_EnableSOFEvents();
41}
42
43static void HID_setup()
44{
45 // Arduino Timer startup: wiring.c
46 init();
47
48 if (usb_host.Init() == -1) {
49 debug("HID init: failed\n");
50 LED_TX_OFF;
51 }
52
53 _delay_ms(200);
54
55 kbd.SetReportParser(0, (HIDReportParser*)&kbd_parser);
56}
57
58int main(void)
59{
60 // LED for debug
61 LED_TX_INIT;
62 LED_TX_ON;
63
64 print_enable = true;
65 debug_enable = true;
66 debug_matrix = true;
67 debug_keyboard = true;
68 debug_mouse = true;
69
70 LUFA_setup();
71 sei();
72
73 // wait for startup of sendchar routine
74 while (USB_DeviceState != DEVICE_STATE_Configured) ;
75 if (debug_enable) {
76 _delay_ms(1000);
77 }
78
79 HID_setup();
80
81 debug("init: done\n");
82 for (;;) {
83 usb_host.Task();
84
85#if !defined(INTERRUPT_CONTROL_ENDPOINT)
86 // LUFA Task for control request
87 USB_USBTask();
88#endif
89 }
90
91 return 0;
92}
diff --git a/rules.mk b/rules.mk
index 02f07fd6f..f53fe912a 100644
--- a/rules.mk
+++ b/rules.mk
@@ -187,6 +187,7 @@ endif
187# -listing-cont-lines: Sets the maximum number of continuation lines of hex 187# -listing-cont-lines: Sets the maximum number of continuation lines of hex
188# dump that will be displayed for a given single line of source input. 188# dump that will be displayed for a given single line of source input.
189ASFLAGS = $(ADEFS) -Wa,-adhlns=$(@:%.o=%.lst),-gstabs,--listing-cont-lines=100 189ASFLAGS = $(ADEFS) -Wa,-adhlns=$(@:%.o=%.lst),-gstabs,--listing-cont-lines=100
190ASFLAGS += $(patsubst %,-I%,$(EXTRAINCDIRS))
190ifdef CONFIG_H 191ifdef CONFIG_H
191 ASFLAGS += -include $(CONFIG_H) 192 ASFLAGS += -include $(CONFIG_H)
192endif 193endif
@@ -345,9 +346,9 @@ GENDEPFLAGS = -MMD -MP -MF .dep/$(@F).d
345# Combine all necessary flags and optional flags. 346# Combine all necessary flags and optional flags.
346# Add target processor to flags. 347# Add target processor to flags.
347# You can give extra flags at 'make' command line like: make EXTRAFLAGS=-DFOO=bar 348# You can give extra flags at 'make' command line like: make EXTRAFLAGS=-DFOO=bar
348ALL_CFLAGS = -mmcu=$(MCU) -I. $(CFLAGS) $(GENDEPFLAGS) $(EXTRAFLAGS) 349ALL_CFLAGS = -mmcu=$(MCU) $(CFLAGS) $(GENDEPFLAGS) $(EXTRAFLAGS)
349ALL_CPPFLAGS = -mmcu=$(MCU) -I. -x c++ $(CPPFLAGS) $(GENDEPFLAGS) $(EXTRAFLAGS) 350ALL_CPPFLAGS = -mmcu=$(MCU) -x c++ $(CPPFLAGS) $(GENDEPFLAGS) $(EXTRAFLAGS)
350ALL_ASFLAGS = -mmcu=$(MCU) -I. -x assembler-with-cpp $(ASFLAGS) $(EXTRAFLAGS) 351ALL_ASFLAGS = -mmcu=$(MCU) -x assembler-with-cpp $(ASFLAGS) $(EXTRAFLAGS)
351 352
352 353
353 354
@@ -569,7 +570,7 @@ $(OBJDIR)/%.o : %.S
569 570
570# Create preprocessed source for use in sending a bug report. 571# Create preprocessed source for use in sending a bug report.
571%.i : %.c 572%.i : %.c
572 $(CC) -E -mmcu=$(MCU) -I. $(CFLAGS) $< -o $@ 573 $(CC) -E -mmcu=$(MCU) $(CFLAGS) $< -o $@
573 574
574 575
575# Target: clean project. 576# Target: clean project.