From c5060ea81942b0e3f8577536ff78402a19abe3d3 Mon Sep 17 00:00:00 2001 From: tmk Date: Sat, 25 Aug 2012 15:49:08 +0900 Subject: test build of 'Host shield' in minimal env. --- protocol/usb_hid/Makefile | 141 --------------------- protocol/usb_hid/NullSerial.cpp | 44 ------- protocol/usb_hid/README | 19 +++ protocol/usb_hid/USBAPI.h | 22 ---- .../usb_hid/arduino-1.0.1/cores/arduino/USBDesc.h | 4 +- .../usb_hid/arduino-1.0.1/cores/arduino/wiring.c | 2 - protocol/usb_hid/leonardo_led.h | 10 ++ protocol/usb_hid/main.cpp | 66 ---------- protocol/usb_hid/override_Serial.cpp | 51 ++++++++ protocol/usb_hid/override_wiring.c | 28 ++++ protocol/usb_hid/parser.cpp | 15 +-- protocol/usb_hid/parser.h | 5 + protocol/usb_hid/test/Makefile | 126 ++++++++++++++++++ protocol/usb_hid/test/config.h | 40 ++++++ protocol/usb_hid/test/test.cpp | 92 ++++++++++++++ 15 files changed, 380 insertions(+), 285 deletions(-) delete mode 100644 protocol/usb_hid/Makefile delete mode 100644 protocol/usb_hid/NullSerial.cpp delete mode 100644 protocol/usb_hid/USBAPI.h create mode 100644 protocol/usb_hid/leonardo_led.h delete mode 100644 protocol/usb_hid/main.cpp create mode 100644 protocol/usb_hid/override_Serial.cpp create mode 100644 protocol/usb_hid/override_wiring.c create mode 100644 protocol/usb_hid/test/Makefile create mode 100644 protocol/usb_hid/test/config.h create mode 100644 protocol/usb_hid/test/test.cpp (limited to 'protocol/usb_hid') diff --git a/protocol/usb_hid/Makefile b/protocol/usb_hid/Makefile deleted file mode 100644 index ed3d6518d8..0000000000 --- a/protocol/usb_hid/Makefile +++ /dev/null @@ -1,141 +0,0 @@ -#---------------------------------------------------------------------------- -# On command line: -# -# make all = Make software. -# -# make clean = Clean out built project files. -# -# make coff = Convert ELF to AVR COFF. -# -# make extcoff = Convert ELF to AVR Extended COFF. -# -# make program = Download the hex file to the device. -# Please customize your programmer settings(PROGRAM_CMD) -# -# make teensy = Download the hex file to the device, using teensy_loader_cli. -# (must have teensy_loader_cli installed). -# -# make dfu = Download the hex file to the device, using dfu-programmer (must -# have dfu-programmer installed). -# -# make flip = Download the hex file to the device, using Atmel FLIP (must -# have Atmel FLIP installed). -# -# make dfu-ee = Download the eeprom file to the device, using dfu-programmer -# (must have dfu-programmer installed). -# -# make flip-ee = Download the eeprom file to the device, using Atmel FLIP -# (must have Atmel FLIP installed). -# -# make debug = Start either simulavr or avarice as specified for debugging, -# with avr-gdb or avr-insight as the front end for debugging. -# -# make filename.s = Just compile filename.c into the assembler code only. -# -# make filename.i = Create a preprocessed source file for use in submitting -# bug reports to the GCC project. -# -# To rebuild project do "make clean" then "make all". -#---------------------------------------------------------------------------- - -# Target file name (without extension). -TARGET = usbkbd - -# Directory keyboard dependent files exist -TARGET_DIR = . - -# MCU name -MCU = atmega32u4 - - -# Processor frequency. -# This will define a symbol, F_CPU, in all source code files equal to the -# processor frequency in Hz. You can then use this symbol in your source code to -# calculate timings. Do NOT tack on a 'UL' at the end, this will be done -# automatically to create a 32-bit value in your source code. -# -# This will be an integer division of F_USB below, as it is sourced by -# F_USB after it has run through any CPU prescalers. Note that this value -# does not *change* the processor frequency - it should merely be updated to -# reflect the processor speed set externally so that the code can use accurate -# software delays. -F_CPU = 16000000 - - - - -ARDUINO_DIR = arduino-1.0.1/cores -ARDUINO_SRC = \ - arduino/Print.cpp \ - arduino/Stream.cpp \ - arduino/wiring.c - -# arduino/main.cpp \ -# arduino/USBCore.cpp \ -# arduino/CDC.cpp \ -# arduino/HID.cpp \ -# arduino/HardwareSerial.cpp \ -# arduino/IPAddress.cpp \ -# arduino/Tone.cpp \ -# arduino/WMath.cpp \ -# arduino/WInterrupts.c \ -# arduino/wiring_analog.c \ -# arduino/wiring_pulse.c \ -# arduino/wiring_shift.c -# arduino/wiring_digital.c \ -# arduino/WString.cpp \ -# arduino/new.cpp \ - -USB_HOST_DIR = ./USB_Host_Shield_2.0 -USB_HOST_SRC = \ - Usb.cpp \ - cdcacm.cpp \ - cdcftdi.cpp \ - cdcprolific.cpp \ - hid.cpp \ - hidboot.cpp \ - hiduniversal.cpp \ - hidusagetitlearrays.cpp \ - hidescriptorparser.cpp \ - message.cpp \ - parsetools.cpp - - #PS3BT.cpp \ - #PS3USB.cpp \ - #RFCOMM.cpp \ - #XBOXUSB.cpp \ - #adk.cpp \ - #masstorage.cpp \ - #max_LCD.cpp \ - #usbhub.cpp - -#SRC = host_kbd.cpp -SRC = main.cpp -SRC += parser.cpp -SRC += NullSerial.cpp -SRC += $(USB_HOST_SRC) -SRC += $(ARDUINO_SRC) - -OPT_DEFS = -DARDUINO=101 -DUSB_VID=0x2341 -DUSB_PID=0x8036 - -# Search Path -VPATH += $(TARGET_DIR) -VPATH += $(USB_HOST_DIR) -VPATH += $(ARDUINO_DIR) -# for Arduino.h -VPATH += arduino-1.0.1/cores/arduino -# for pins_arduino.h -VPATH += arduino-1.0.1/variants/leonardo - - -# Ad hoc workaround to override original arduino/USBAPI.h with our own USBAPI.h. -# Obsolete but needed in order to remove directory including the current input file from search list. -# Option -iquote can't replace -I- for this purpose. -EXTRAFLAGS += -I- - - -# program Leonardo -PROGRAM_CMD = avrdude -patmega32u4 -cavr109 -P$(DEV) -b57600 -Uflash:w:$(TARGET).hex - - -include ../../rules.mk diff --git a/protocol/usb_hid/NullSerial.cpp b/protocol/usb_hid/NullSerial.cpp deleted file mode 100644 index 6d85caf2d8..0000000000 --- a/protocol/usb_hid/NullSerial.cpp +++ /dev/null @@ -1,44 +0,0 @@ -#include "USBAPI.h" - - -void NullSerial::begin(uint16_t baud_count) -{ -} - -void NullSerial::end(void) -{ -} - -void NullSerial::accept(void) -{ -} - -int NullSerial::available(void) -{ - return 0; -} - -int NullSerial::peek(void) -{ - return -1; -} - -int NullSerial::read(void) -{ - return -1; -} - -void NullSerial::flush(void) -{ -} - -size_t NullSerial::write(uint8_t c) -{ - return 1; -} - -NullSerial::operator bool() { - return true; -} - -NullSerial Serial; diff --git a/protocol/usb_hid/README b/protocol/usb_hid/README index 0acf677f8b..9750a1cc8d 100644 --- a/protocol/usb_hid/README +++ b/protocol/usb_hid/README @@ -1,3 +1,8 @@ +USB HID protocol +================ +Host side of USB HID keyboard protocol implementation. +Only standard HID Boot mode is supported at this time. This means most of normal keyboards are supported while proprietary >6KRO and NKRO is not. + Third party Libraries --------------------- USB_Host_Shield_2.0 @@ -7,3 +12,17 @@ USB_Host_Shield_2.0 arduino-1.0.1 Arduino files copied from: https://github.com/arduino/Arduino/hardware/arduino/{cores,variants} + + +Test build +---------- +In test directory; + $ make + $ DEV=/dev/ttyACM0 make program + +You can see HID keyboard reports on debug output. + + +Restriction and Bug +------------------- +Not statble yet. diff --git a/protocol/usb_hid/USBAPI.h b/protocol/usb_hid/USBAPI.h deleted file mode 100644 index e3390d49a8..0000000000 --- a/protocol/usb_hid/USBAPI.h +++ /dev/null @@ -1,22 +0,0 @@ -/* - * Override original arduino USBAPI.h. - */ -#include -#include "Stream.h" - - -class NullSerial : public Stream -{ -public: - void begin(uint16_t baud_count); - void end(void); - - virtual int available(void); - virtual void accept(void); - virtual int peek(void); - virtual int read(void); - virtual void flush(void); - virtual size_t write(uint8_t); - operator bool(); -}; -extern 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 229a638c53..900713e0f9 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 @@ ** SOFTWARE. */ -//#define CDC_ENABLED -//#define HID_ENABLED +#define CDC_ENABLED +#define HID_ENABLED #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 b1a4ce4b0b..ac8bb6f9b4 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() { void delay(unsigned long ms) { -//PORTB &= ~(1<<0); uint16_t start = (uint16_t)micros(); while (ms > 0) { @@ -117,7 +116,6 @@ void delay(unsigned long ms) start += 1000; } } -//PORTB |= (1<<0); } /* 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 0000000000..6f67a88f5b --- /dev/null +++ b/protocol/usb_hid/leonardo_led.h @@ -0,0 +1,10 @@ +#ifndef LEONARDO_LED_H +#define LEONARDO_LED_H + +// Leonardo "TX" LED for debug +#define LED_TX_INIT (DDRD |= (1<<5)) +#define LED_TX_ON (PORTD &= ~(1<<5)) +#define LED_TX_OFF (PORTD |= (1<<5)) +#define LED_TX_TOGGLE (PORTD ^= (1<<5)) + +#endif diff --git a/protocol/usb_hid/main.cpp b/protocol/usb_hid/main.cpp deleted file mode 100644 index c292d458e2..0000000000 --- a/protocol/usb_hid/main.cpp +++ /dev/null @@ -1,66 +0,0 @@ -#include -#include -#include "Usb.h" -#include "hid.h" -#include "hidboot.h" -#include "parser.h" - - -USB Usb; -HIDBoot kbd(&Usb); -KBDReportParser Prs; - -void usb_disable() -{ - USBCON &= ~(1< +#include "common/timer.h" +#include "Arduino.h" + + +unsigned long millis() +{ + return timer_read(); +} +unsigned long micros() +{ + return timer_read() * 1000UL; +} +void delay(unsigned long ms) +{ + _delay_ms(ms); +} +void delayMicroseconds(unsigned int us) +{ + _delay_us(us); +} +void init() +{ + timer_init(); +} diff --git a/protocol/usb_hid/parser.cpp b/protocol/usb_hid/parser.cpp index cf64322302..b03af8ae41 100644 --- a/protocol/usb_hid/parser.cpp +++ b/protocol/usb_hid/parser.cpp @@ -1,15 +1,14 @@ #include "parser.h" +#include "leonardo_led.h" +#include "debug.h" void KBDReportParser::Parse(HID *hid, bool is_rpt_id, uint8_t len, uint8_t *buf) { - PORTB ^= (1<<0); -/* - Serial.print("KBDReport: "); + LED_TX_TOGGLE; + debug("KBDReport: "); for (uint8_t i = 0; i < len; i++) { - PrintHex(buf[i]); - Serial.print(" "); + debug_hex(buf[i]); + debug(" "); } - Serial.print("\r\n"); -*/ - //PORTC &= ~(1<<7); + debug("\r\n"); } diff --git a/protocol/usb_hid/parser.h b/protocol/usb_hid/parser.h index dc14c82705..703eb1ed4c 100644 --- a/protocol/usb_hid/parser.h +++ b/protocol/usb_hid/parser.h @@ -1,3 +1,6 @@ +#ifndef PARSER_H +#define PARSER_H + #include "hid.h" class KBDReportParser : public HIDReportParser @@ -5,3 +8,5 @@ class KBDReportParser : public HIDReportParser public: virtual void Parse(HID *hid, bool is_rpt_id, uint8_t len, uint8_t *buf); }; + +#endif diff --git a/protocol/usb_hid/test/Makefile b/protocol/usb_hid/test/Makefile new file mode 100644 index 0000000000..39f5de4552 --- /dev/null +++ b/protocol/usb_hid/test/Makefile @@ -0,0 +1,126 @@ +#---------------------------------------------------------------------------- +# On command line: +# +# make all = Make software. +# +# make clean = Clean out built project files. +# +# make coff = Convert ELF to AVR COFF. +# +# make extcoff = Convert ELF to AVR Extended COFF. +# +# make program = Download the hex file to the device. +# Please customize your programmer settings(PROGRAM_CMD) +# +# make teensy = Download the hex file to the device, using teensy_loader_cli. +# (must have teensy_loader_cli installed). +# +# make dfu = Download the hex file to the device, using dfu-programmer (must +# have dfu-programmer installed). +# +# make flip = Download the hex file to the device, using Atmel FLIP (must +# have Atmel FLIP installed). +# +# make dfu-ee = Download the eeprom file to the device, using dfu-programmer +# (must have dfu-programmer installed). +# +# make flip-ee = Download the eeprom file to the device, using Atmel FLIP +# (must have Atmel FLIP installed). +# +# make debug = Start either simulavr or avarice as specified for debugging, +# with avr-gdb or avr-insight as the front end for debugging. +# +# make filename.s = Just compile filename.c into the assembler code only. +# +# make filename.i = Create a preprocessed source file for use in submitting +# bug reports to the GCC project. +# +# To rebuild project do "make clean" then "make all". +#---------------------------------------------------------------------------- + +# Target file name (without extension). +TARGET = usb_hid_test + +TOP_DIR = ../../.. + +# Directory keyboard dependent files exist +TARGET_DIR = . + +# MCU name +MCU = atmega32u4 + + +# Processor frequency. +# This will define a symbol, F_CPU, in all source code files equal to the +# processor frequency in Hz. You can then use this symbol in your source code to +# calculate timings. Do NOT tack on a 'UL' at the end, this will be done +# automatically to create a 32-bit value in your source code. +# +# This will be an integer division of F_USB below, as it is sourced by +# F_USB after it has run through any CPU prescalers. Note that this value +# does not *change* the processor frequency - it should merely be updated to +# reflect the processor speed set externally so that the code can use accurate +# software delays. +F_CPU = 16000000 + + + +# +# LUFA specific +# +# Target architecture (see library "Board Types" documentation). +ARCH = AVR8 +# Input clock frequency. +# This will define a symbol, F_USB, in all source code files equal to the +# input clock frequency (before any prescaling is performed) in Hz. This value may +# differ from F_CPU if prescaling is used on the latter, and is required as the +# raw input clock is fed directly to the PLL sections of the AVR for high speed +# clock generation for the USB and other AVR subsections. Do NOT tack on a 'UL' +# at the end, this will be done automatically to create a 32-bit value in your +# source code. +# +# If no clock division is performed on the input clock inside the AVR (via the +# CPU clock adjust registers or the clock division fuses), this will be equal to F_CPU. +F_USB = $(F_CPU) +# Interrupt driven control endpoint task +OPT_DEFS += -DINTERRUPT_CONTROL_ENDPOINT + + + +# Build Options +# comment out to disable the options. +# +# Console for debug +OPT_DEFS += -DCONSOLE_ENABLE + +# Boot Section Size in bytes +# Teensy halfKay 512 +# Atmel DFU loader 4096 +# LUFA bootloader 4096 +#OPT_DEFS += -DBOOT_SIZE=4096 + + + +SRC = test.cpp +SRC += common/debug.c +SRC += common/print.c + +CONFIG_H = config.h + + + +# Search Path +VPATH += $(TARGET_DIR) +VPATH += $(TOP_DIR) +VPATH += $(TOP_DIR)/common + + + +# program Leonardo +PROGRAM_CMD = avrdude -p$(MCU) -cavr109 -P$(DEV) -b57600 -Uflash:w:$(TARGET).hex + + + +include $(TOP_DIR)/protocol/usb_hid.mk +include $(TOP_DIR)/protocol/lufa.mk +include $(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 0000000000..c2230fb570 --- /dev/null +++ b/protocol/usb_hid/test/config.h @@ -0,0 +1,40 @@ +/* +Copyright 2012 Jun Wako + +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 2 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 . +*/ + +#ifndef CONFIG_H +#define CONFIG_H + + +#define VENDOR_ID 0xFEED +#define PRODUCT_ID 0xCAFE +#define DEVICE_VER 0x0814 +#define MANUFACTURER t.m.k. +#define PRODUCT USB to USB keyboard converter + + +#define DESCRIPTION Product from t.m.k. keyboard firmware project + + +/* matrix size */ +#define MATRIX_ROWS 32 +#define MATRIX_COLS 8 + + +/* key combination for command */ +#define IS_COMMAND() (keyboard_report->mods == (MOD_BIT(KB_LSHIFT) | MOD_BIT(KB_RSHIFT))) + +#endif diff --git a/protocol/usb_hid/test/test.cpp b/protocol/usb_hid/test/test.cpp new file mode 100644 index 0000000000..4958f0c619 --- /dev/null +++ b/protocol/usb_hid/test/test.cpp @@ -0,0 +1,92 @@ +#include +#include +#include +#include +#include + +// USB HID host +#include "Usb.h" +#include "hid.h" +#include "hidboot.h" +#include "parser.h" + +// LUFA +#include "lufa.h" + +#include "debug.h" + +#include "leonardo_led.h" + + +static USB usb_host; +static HIDBoot kbd(&usb_host); +static KBDReportParser kbd_parser; + +static void LUFA_setup(void) +{ + /* Disable watchdog if enabled by bootloader/fuses */ + MCUSR &= ~(1 << WDRF); + wdt_disable(); + + /* Disable clock division */ + clock_prescale_set(clock_div_1); + + // Leonardo needs. Without this USB device is not recognized. + USB_Disable(); + + USB_Init(); + + // for Console_Task + USB_Device_EnableSOFEvents(); +} + +static void HID_setup() +{ + // Arduino Timer startup: wiring.c + init(); + + if (usb_host.Init() == -1) { + debug("HID init: failed\n"); + LED_TX_OFF; + } + + _delay_ms(200); + + kbd.SetReportParser(0, (HIDReportParser*)&kbd_parser); +} + +int main(void) +{ + // LED for debug + LED_TX_INIT; + LED_TX_ON; + + print_enable = true; + debug_enable = true; + debug_matrix = true; + debug_keyboard = true; + debug_mouse = true; + + LUFA_setup(); + sei(); + + // wait for startup of sendchar routine + while (USB_DeviceState != DEVICE_STATE_Configured) ; + if (debug_enable) { + _delay_ms(1000); + } + + HID_setup(); + + debug("init: done\n"); + for (;;) { + usb_host.Task(); + +#if !defined(INTERRUPT_CONTROL_ENDPOINT) + // LUFA Task for control request + USB_USBTask(); +#endif + } + + return 0; +} -- cgit v1.2.3