From 895cd4dfa29f0f3c623544f4868ac63e619c69d9 Mon Sep 17 00:00:00 2001 From: tmk Date: Tue, 14 Aug 2012 00:17:31 +0900 Subject: [PATCH] Add USB HID(host) protocol.(not finished) --- protocol/usb_hid/Makefile | 141 ++++++++++++++++++++++++++++++++ protocol/usb_hid/NullSerial.cpp | 44 ++++++++++ protocol/usb_hid/USBAPI.h | 22 +++++ protocol/usb_hid/main.cpp | 66 +++++++++++++++ protocol/usb_hid/parser.cpp | 15 ++++ protocol/usb_hid/parser.h | 7 ++ rules.mk | 5 ++ 7 files changed, 300 insertions(+) create mode 100644 protocol/usb_hid/Makefile create mode 100644 protocol/usb_hid/NullSerial.cpp create mode 100644 protocol/usb_hid/USBAPI.h create mode 100644 protocol/usb_hid/main.cpp create mode 100644 protocol/usb_hid/parser.cpp create mode 100644 protocol/usb_hid/parser.h diff --git a/protocol/usb_hid/Makefile b/protocol/usb_hid/Makefile new file mode 100644 index 0000000000..ed3d6518d8 --- /dev/null +++ b/protocol/usb_hid/Makefile @@ -0,0 +1,141 @@ +#---------------------------------------------------------------------------- +# 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 new file mode 100644 index 0000000000..6d85caf2d8 --- /dev/null +++ b/protocol/usb_hid/NullSerial.cpp @@ -0,0 +1,44 @@ +#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/USBAPI.h b/protocol/usb_hid/USBAPI.h new file mode 100644 index 0000000000..e3390d49a8 --- /dev/null +++ b/protocol/usb_hid/USBAPI.h @@ -0,0 +1,22 @@ +/* + * 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/main.cpp b/protocol/usb_hid/main.cpp new file mode 100644 index 0000000000..c292d458e2 --- /dev/null +++ b/protocol/usb_hid/main.cpp @@ -0,0 +1,66 @@ +#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<(buf[i]); + Serial.print(" "); + } + Serial.print("\r\n"); +*/ + //PORTC &= ~(1<<7); +} diff --git a/protocol/usb_hid/parser.h b/protocol/usb_hid/parser.h new file mode 100644 index 0000000000..dc14c82705 --- /dev/null +++ b/protocol/usb_hid/parser.h @@ -0,0 +1,7 @@ +#include "hid.h" + +class KBDReportParser : public HIDReportParser +{ +public: + virtual void Parse(HID *hid, bool is_rpt_id, uint8_t len, uint8_t *buf); +}; diff --git a/rules.mk b/rules.mk index e561eae631..02f07fd6f7 100644 --- a/rules.mk +++ b/rules.mk @@ -158,6 +158,10 @@ CPPFLAGS += -funsigned-bitfields CPPFLAGS += -fpack-struct CPPFLAGS += -fshort-enums CPPFLAGS += -fno-exceptions +CPPFLAGS += -ffunction-sections +CPPFLAGS += -fdata-sections +# to supress "warning: only initialized variables can be placed into program memory area" +CPPFLAGS += -w CPPFLAGS += -Wall CPPFLAGS += -Wundef #CPPFLAGS += -mshort-calls @@ -541,6 +545,7 @@ $(OBJDIR)/%.o : %.c # Compile: create object files from C++ source files. $(OBJDIR)/%.o : %.cpp @echo + mkdir -p $(@D) @echo $(MSG_COMPILING_CPP) $< $(CC) -c $(ALL_CPPFLAGS) $< -o $@