aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--boards.txt18
-rw-r--r--bootloaders/diskloader/Arduino_Leonardo.inf106
-rw-r--r--bootloaders/diskloader/DiskLoader.hex102
-rw-r--r--bootloaders/diskloader/Makefile99
-rw-r--r--bootloaders/diskloader/src/DiskLoader.cpp251
-rw-r--r--bootloaders/diskloader/src/Platform.h56
-rw-r--r--bootloaders/diskloader/src/SCSI.cpp213
-rw-r--r--bootloaders/diskloader/src/ThinFAT.cpp130
-rw-r--r--bootloaders/diskloader/src/USBCore.cpp526
-rw-r--r--bootloaders/diskloader/src/USBCore.h261
-rw-r--r--bootloaders/diskloader/src/USBDesc.cpp88
-rw-r--r--bootloaders/diskloader/src/USBDesc.h81
-rw-r--r--cores/arduino/CDC.cpp184
-rw-r--r--cores/arduino/HID.cpp439
-rw-r--r--cores/arduino/HardwareSerial.cpp17
-rw-r--r--cores/arduino/HardwareSerial.h3
-rw-r--r--cores/arduino/Platform.h23
-rw-r--r--cores/arduino/USBAPI.h163
-rw-r--r--cores/arduino/USBCore.cpp655
-rw-r--r--cores/arduino/USBCore.h303
-rw-r--r--cores/arduino/USBDesc.h107
-rwxr-xr-xcores/arduino/main.cpp4
-rw-r--r--pins/leonardo/pins_arduino.h231
-rw-r--r--variants/leonardo/pins_arduino.h231
24 files changed, 4289 insertions, 2 deletions
diff --git a/boards.txt b/boards.txt
index fb2a1cd..57fe222 100644
--- a/boards.txt
+++ b/boards.txt
@@ -1,5 +1,23 @@
##############################################################
+leonardo.name=Arduino Leonardo
+leonardo.upload.protocol=stk500
+leonardo.upload.maximum_size=30720
+leonardo.upload.speed=300
+leonardo.bootloader.low_fuses=0xde
+leonardo.bootloader.high_fuses=0xd8
+leonardo.bootloader.extended_fuses=0xf3
+leonardo.bootloader.path=diskloader
+leonardo.bootloader.file=DiskLoader.hex
+leonardo.bootloader.unlock_bits=0x3F
+leonardo.bootloader.lock_bits=0x0F
+leonardo.build.mcu=atmega32u4
+leonardo.build.f_cpu=16000000L
+leonardo.build.core=arduino
+leonardo.build.variant=leonardo
+
+##############################################################
+
uno.name=Arduino Uno
uno.upload.protocol=arduino
uno.upload.maximum_size=32256
diff --git a/bootloaders/diskloader/Arduino_Leonardo.inf b/bootloaders/diskloader/Arduino_Leonardo.inf
new file mode 100644
index 0000000..8c3feb3
--- /dev/null
+++ b/bootloaders/diskloader/Arduino_Leonardo.inf
@@ -0,0 +1,106 @@
+;************************************************************
+; Windows USB CDC ACM Setup File
+; Copyright (c) 2000 Microsoft Corporation
+
+
+[Version]
+Signature="$Windows NT$"
+Class=Ports
+ClassGuid={4D36E978-E325-11CE-BFC1-08002BE10318}
+Provider=%MFGNAME%
+LayoutFile=layout.inf
+CatalogFile=%MFGFILENAME%.cat
+DriverVer=11/15/2007,5.1.2600.0
+
+[Manufacturer]
+%MFGNAME%=DeviceList, NTamd64
+
+[DestinationDirs]
+DefaultDestDir=12
+
+
+;------------------------------------------------------------------------------
+; Windows 2000/XP/Vista-32bit Sections
+;------------------------------------------------------------------------------
+
+[DriverInstall.nt]
+include=mdmcpq.inf
+CopyFiles=DriverCopyFiles.nt
+AddReg=DriverInstall.nt.AddReg
+
+[DriverCopyFiles.nt]
+usbser.sys,,,0x20
+
+[DriverInstall.nt.AddReg]
+HKR,,DevLoader,,*ntkern
+HKR,,NTMPDriver,,%DRIVERFILENAME%.sys
+HKR,,EnumPropPages32,,"MsPorts.dll,SerialPortPropPageProvider"
+
+[DriverInstall.nt.Services]
+AddService=usbser, 0x00000002, DriverService.nt
+
+[DriverService.nt]
+DisplayName=%SERVICE%
+ServiceType=1
+StartType=3
+ErrorControl=1
+ServiceBinary=%12%\%DRIVERFILENAME%.sys
+
+;------------------------------------------------------------------------------
+; Vista-64bit Sections
+;------------------------------------------------------------------------------
+
+[DriverInstall.NTamd64]
+include=mdmcpq.inf
+CopyFiles=DriverCopyFiles.NTamd64
+AddReg=DriverInstall.NTamd64.AddReg
+
+[DriverCopyFiles.NTamd64]
+%DRIVERFILENAME%.sys,,,0x20
+
+[DriverInstall.NTamd64.AddReg]
+HKR,,DevLoader,,*ntkern
+HKR,,NTMPDriver,,%DRIVERFILENAME%.sys
+HKR,,EnumPropPages32,,"MsPorts.dll,SerialPortPropPageProvider"
+
+[DriverInstall.NTamd64.Services]
+AddService=usbser, 0x00000002, DriverService.NTamd64
+
+[DriverService.NTamd64]
+DisplayName=%SERVICE%
+ServiceType=1
+StartType=3
+ErrorControl=1
+ServiceBinary=%12%\%DRIVERFILENAME%.sys
+
+
+;------------------------------------------------------------------------------
+; Vendor and Product ID Definitions
+;------------------------------------------------------------------------------
+; When developing your USB device, the VID and PID used in the PC side
+; application program and the firmware on the microcontroller must match.
+; Modify the below line to use your VID and PID. Use the format as shown below.
+; Note: One INF file can be used for multiple devices with different VID and PIDs.
+; For each supported device, append ",USB\VID_xxxx&PID_yyyy" to the end of the line.
+;------------------------------------------------------------------------------
+[SourceDisksFiles]
+[SourceDisksNames]
+[DeviceList]
+%DESCRIPTION%=DriverInstall, USB\VID_2341&PID_0020&MI_00
+
+[DeviceList.NTamd64]
+%DESCRIPTION%=DriverInstall, USB\VID_2341&PID_0020&MI_00
+
+
+;------------------------------------------------------------------------------
+; String Definitions
+;------------------------------------------------------------------------------
+;Modify these strings to customize your device
+;------------------------------------------------------------------------------
+[Strings]
+MFGFILENAME="CDC_vista"
+DRIVERFILENAME ="usbser"
+MFGNAME="Arduino LLC (www.arduino.cc)"
+INSTDISK="Arduino Leonardo Driver Installer"
+DESCRIPTION="Arduino Lenonardo"
+SERVICE="USB RS-232 Emulation Driver" \ No newline at end of file
diff --git a/bootloaders/diskloader/DiskLoader.hex b/bootloaders/diskloader/DiskLoader.hex
new file mode 100644
index 0000000..ced02bd
--- /dev/null
+++ b/bootloaders/diskloader/DiskLoader.hex
@@ -0,0 +1,102 @@
+:1078000011241FBECFEFDAE0DEBFCDBFD0C04101F3
+:1078100042144505560455026403740300001E9586
+:1078200087020110030000C18081C106C0FF0A0069
+:107830000CA10185037508150026FF00954009017C
+:107840008102954009029102C0040309041A033021
+:1078500000300030003000300030003000300031A7
+:1078600000370030003100120100020200004041E8
+:1078700023200000010000010112010002000000AD
+:1078800040412320000001000001010902640003BF
+:1078900001008032080B000202020100090400000E
+:1078A0000102020000052400100105240101010469
+:1078B0002402020524060001070581031000400987
+:1078C000040100020A000000070502024000000750
+:1078D00005830240000009040200010300000009C2
+:1078E0002101010001221E00070584034000400021
+:1078F00020918A0130918B012F5F3F4F30938B0194
+:1079000020938A01832F9927982F87FD9095990FAF
+:10791000921710F447980895479A089581E080934C
+:10792000E0001092E200EE27FF27099408950F93DC
+:107930001F93CF93DF93982FEB01042F10E088E77C
+:107940006030780730F411E083E0FB01809357004A
+:10795000E895892F68E071E0402FE2D0112311F102
+:1079600007B600FCFDCF402F4695FE01A8E0B1E030
+:1079700020E031E009C08D919D910C0130935700BA
+:10798000E895112432962F5F2417A8F385E0FE01B5
+:1079900080935700E89507B600FCFDCF81E1809306
+:1079A0005700E895DF91CF911F910F910895769A36
+:1079B000289A47983F9A209A6E9A90E8909361008F
+:1079C0001092610081E885BF95BF91D084E18093DA
+:1079D000880180E180938901E0E0F0E08591949155
+:1079E0008F5F9F4F19F081E080938D01EE24FF247B
+:1079F000BB24B39421E0C22ED12CE1D1082F8EE01C
+:107A000098E7FC012491319602964491201711F0D9
+:107A10002223B9F710928F0110928E0182E068E064
+:107A200071E07ED0013479F46091080160586330D0
+:107A300028F0683111F064E001C063E0C62FD0E0A7
+:107A4000CF5DD7480EC0063571F4809108018033B0
+:107A500011F011E022C080910A01C82FD0E0C25E6F
+:107A6000D74811E022C0053721F413E0CEE1D8E772
+:107A70001CC0053539F4E0900801F0900901EE0CC6
+:107A8000FF1C0AC0043631F482E0B70140910901BD
+:107A90004EDF02C0043721F010E0C5E2D8E705C090
+:107AA00010910901E701E10EF11C89D18097B1F431
+:107AB00083E068E871E041E050E049D0112329F00B
+:107AC000412F50E083E8BE0142D083E469E871E0D1
+:107AD00041E050E03CD0013509F08FCFD0928F01CA
+:107AE000C0928E01B0928D01EE24FF2486CF1092B9
+:107AF0008F0110928E0110928C0110928D0181E005
+:107B00008093D70080EA8093D80082E189BD09B4D0
+:107B100000FEFDCF80E98093D8001092E000089528
+:107B2000FB018093E9009BE60FC08091E80085FF90
+:107B3000FCCF28988091F10081938091E80085FD29
+:107B400002C09093E8004150442379F70895982F9C
+:107B5000FB01282F207287708093E9003AE315C05B
+:107B60008091E80085FFFCCF97FF02C0849101C09F
+:107B700080813196211180E076988093F100809188
+:107B8000E80085FD02C03093E800415050408FEF7F
+:107B90004F3F580729F796FF03C08AE38093E80018
+:107BA000089580919101813281F4769820E030E04F
+:107BB00003C080818093F100F901E050FF4F2F5FF7
+:107BC0003F4F28303105A9F71FC08032B9F48091AA
+:107BD000E80082FFFCCF289820E030E003C08091CD
+:107BE000F1008083F901E050FF4F2F5F3F4F2830B5
+:107BF0003105A9F78BEF8093E80006C0823221F4AB
+:107C0000809192018093070181E008952091930172
+:107C1000223231F1213081F48091960190919701C7
+:107C2000089711F42093980180919801882309F016
+:107C30003BC0E9E7F8E73AC0223021F484E6EBE8FC
+:107C4000F8E711C0233071F580919201882319F46F
+:107C5000E9E4F8E72BC0813029F5EDE4F8E726C028
+:107C600081E0EBE2F8E790919601282F981708F44D
+:107C7000292F90E03EEF8091E8008570E1F380913C
+:107C8000E80082FD0FC09F5F849176988093F10099
+:107C9000892F8F7311F43093E8003196921758F3BF
+:107CA00081E0089580E00895E7E6F8E78491DBCF6E
+:107CB0001092E9008091E80083FF62C02898E0E913
+:107CC000F1E003C08091F100819381E0E839F80789
+:107CD000C9F782EF8093E8008091900187FF05C08B
+:107CE0008091E80080FFFCCF03C08EEF8093E80016
+:107CF00080919101853051F48091E80080FFFCCFA4
+:107D00008091920180688093E30033C08930E1F470
+:107D10008091920180938C01E7E2F8E791E031E0F5
+:107D200026E39093E9003093EB0084918093EC007C
+:107D30002093ED009F5F3196953099F78EE7809301
+:107D4000EA001092EA0015C0883031F480918C016D
+:107D500076988093F1000DC0882321F476981092D4
+:107D6000F10007C0863011F451DF01C01ADF88230B
+:107D700021F08EEF8093E800089581E28093EB007C
+:107D800008958091E1001092E100282F83FF0CC03C
+:107D90001092E90081E08093EB001092EC0082E306
+:107DA0008093ED0010928C0122FF08C080918D011C
+:107DB000882321F410928F0110928E0108951F9351
+:107DC00012E076DFDEDF1093E9008091E80085FFA6
+:107DD0000EC028989091F1008091E80085FD03C0C5
+:107DE0008BE68093E800892F90E01F91089584E648
+:107DF00000000000000000008150D1F7289A769A18
+:107E000080918E0190918F01019790938F018093C3
+:107E10008E0180918E0190918F01892B19F4769AB1
+:087E2000289A7CDD65DDCDCF61
+:087E280000E100000000000071
+:040000030000780081
+:00000001FF
diff --git a/bootloaders/diskloader/Makefile b/bootloaders/diskloader/Makefile
new file mode 100644
index 0000000..ca6e11e
--- /dev/null
+++ b/bootloaders/diskloader/Makefile
@@ -0,0 +1,99 @@
+###############################################################################
+# Makefile for DiskLoader
+###############################################################################
+
+## General Flags
+PROJECT = DiskLoader
+TARGET = DiskLoader.elf
+CC = avr-gcc
+
+# BOARD2
+MCU = atmega32u4
+AVR_FREQ = 16000000L
+
+
+# Change if your programmer is different
+AVRDUDE_PROGRAMMER = avrispmkII
+AVRDUDE_PORT = usb
+
+# program name should not be changed...
+PROGRAM = DiskLoader
+
+AVRDUDE = avrdude
+AVRDUDE_FLAGS = -p $(MCU) -P $(AVRDUDE_PORT) -c $(AVRDUDE_PROGRAMMER) -p $(MCU)
+
+## Options common to compile, link and assembly rules
+COMMON = -mmcu=$(MCU)
+
+override CFLAGS = -g -Wall -Os -mmcu=$(MCU) -DF_CPU=$(AVR_FREQ) $(DEFS) -ffunction-sections -gdwarf-2 -fdata-sections -fno-split-wide-types
+
+## Assembly specific flags
+ASMFLAGS = $(COMMON)
+ASMFLAGS += $(CFLAGS)
+ASMFLAGS += -x assembler-with-cpp -Wa,-gdwarf2
+
+## Linker flags
+LDFLAGS = $(COMMON)
+LDFLAGS += -Wl,-gc-sections,-Map=DiskLoader.map,--section-start=.text=0x7800,--relax
+LDFLAGS += -nodefaultlibs -nostartfiles
+
+
+## Intel Hex file production flags
+HEX_EEPROM_FLAGS = -j .eeprom
+HEX_EEPROM_FLAGS += --set-section-flags=.eeprom="alloc,load"
+HEX_EEPROM_FLAGS += --change-section-lma .eeprom=0 --no-change-warnings
+
+## Objects explicitly added by the user
+LINKONLYOBJECTS =
+
+MODULES := .
+SRC_DIR := $(addprefix src/,$(MODULES))
+BUILD_DIR := $(addprefix build/,$(MODULES))
+
+SRC := $(foreach sdir,$(SRC_DIR),$(wildcard $(sdir)/*.cpp))
+OBJ := $(patsubst src/%.cpp,build/%.o,$(SRC))
+DEP := $(OBJ:%.o=%.d)
+INCLUDES := $(addprefix -I,$(SRC_DIR))
+
+vpath %.cpp $(SRC_DIR)
+
+.PHONY: all checkdirs clean
+
+all: checkdirs $(TARGET) DiskLoader.hex DiskLoader.lss size
+
+-include $(DEP)
+
+checkdirs: $(BUILD_DIR)
+
+$(BUILD_DIR):
+ @mkdir -p $@
+
+clean:
+ @rm -rf build/
+ @rm -f *.hex
+ @rm -f *.elf
+ @rm -f *.lss
+ @rm -f *.map
+
+define make-goal
+$1/%.o: %.cpp
+ $(CC) $(INCLUDES) $(CFLAGS) -c $$< -MD -o $$@
+endef
+
+$(foreach bdir,$(BUILD_DIR),$(eval $(call make-goal,$(bdir))))
+
+$(TARGET): $(OBJ)
+ $(CC) $(LDFLAGS) $(LINKONLYOBJECTS) $(LIBDIRS) $(LIBS) $^ -o $@
+
+%.hex: $(TARGET)
+ avr-objcopy -O ihex $(HEX_FLASH_FLAGS) $< $@
+
+%.lss: $(TARGET)
+ avr-objdump -h -S $< > $@
+
+size: $(TARGET)
+ @echo
+# @avr-size -C --mcu=${MCU} ${TARGET}.elf
+
+program: $(TARGET).hex
+ $(AVRDUDE) $(AVRDUDE_FLAGS) -B 5 -u -U flash:w:$(TARGET).hex \ No newline at end of file
diff --git a/bootloaders/diskloader/src/DiskLoader.cpp b/bootloaders/diskloader/src/DiskLoader.cpp
new file mode 100644
index 0000000..9ecbc4b
--- /dev/null
+++ b/bootloaders/diskloader/src/DiskLoader.cpp
@@ -0,0 +1,251 @@
+
+
+#include "Platform.h"
+
+// This bootloader creates a composite Serial/Mass storage device
+//
+// The serial interface supports a STK500v1 protocol that is very similar to optiboot
+// The mass storage interface emulates a disk image that is used to read or write firmware.bin files
+// Delete firmware.bin and copy a new binary (not .hex) file to flash the firmware i.e:
+//
+// cp firmware.bin f:/. -f
+//
+// Ejecting the disk will run the firmware
+//
+// The bootloader will timeout and start the firmware after a few hundred milliseconds
+// if a usb connection is not detected.
+//
+// The tweakier code is to keep the bootloader below 2k (no interrupt table, for example)
+
+extern "C"
+void entrypoint(void) __attribute__ ((naked)) __attribute__ ((section (".vectors")));
+void entrypoint(void)
+{
+ asm volatile (
+ "eor r1, r1\n" // Zero register
+ "out 0x3F, r1\n" // SREG
+ "ldi r28, 0xFF\n"
+ "ldi r29, 0x0A\n"
+ "out 0x3E, r29\n" // SPH
+ "out 0x3D, r28\n" // SPL
+ "rjmp main" // Stack is all set up, start the main code
+ ::);
+}
+
+u8 _flashbuf[128];
+u8 _inSync;
+u8 _ok;
+extern volatile u8 _ejected;
+extern volatile u16 _timeout;
+
+void Program(u8 ep, u16 page, u8 count)
+{
+ u8 write = page < 30*1024; // Don't write over firmware please
+ if (write)
+ boot_page_erase(page);
+
+ Recv(ep,_flashbuf,count); // Read while page is erasing
+
+ if (!write)
+ return;
+
+ boot_spm_busy_wait(); // Wait until the memory is erased.
+
+ count >>= 1;
+ u16* p = (u16*)page;
+ u16* b = (u16*)_flashbuf;
+ for (u8 i = 0; i < count; i++)
+ boot_page_fill(p++, b[i]);
+
+ boot_page_write(page);
+ boot_spm_busy_wait();
+ boot_rww_enable ();
+}
+
+
+int USBGetChar();
+#define getch USBGetChar
+
+#define HW_VER 0x02
+#define SW_MAJOR 0x01
+#define SW_MINOR 0x10
+
+#define STK_OK 0x10
+#define STK_INSYNC 0x14 // ' '
+#define CRC_EOP 0x20 // 'SPACE'
+#define STK_GET_SYNC 0x30 // '0'
+
+#define STK_GET_PARAMETER 0x41 // 'A'
+#define STK_SET_DEVICE 0x42 // 'B'
+#define STK_SET_DEVICE_EXT 0x45 // 'E'
+#define STK_LOAD_ADDRESS 0x55 // 'U'
+#define STK_UNIVERSAL 0x56 // 'V'
+#define STK_PROG_PAGE 0x64 // 'd'
+#define STK_READ_PAGE 0x74 // 't'
+#define STK_READ_SIGN 0x75 // 'u'
+
+extern const u8 _readSize[] PROGMEM;
+const u8 _readSize[] =
+{
+ STK_GET_PARAMETER, 1,
+ STK_SET_DEVICE, 20,
+ STK_SET_DEVICE_EXT, 5,
+ STK_UNIVERSAL, 4,
+ STK_LOAD_ADDRESS, 2,
+ STK_PROG_PAGE, 3,
+ STK_READ_PAGE, 3,
+ 0,0
+};
+
+extern const u8 _consts[] PROGMEM;
+const u8 _consts[] =
+{
+ SIGNATURE_0,
+ SIGNATURE_1,
+ SIGNATURE_2,
+ HW_VER, // Hardware version
+ SW_MAJOR, // Software major version
+ SW_MINOR, // Software minor version
+ 0x03, // Unknown but seems to be required by avr studio 3.56
+ 0x00, //
+};
+
+
+void USBInit(void);
+extern u8 _sector3[512];
+
+int main(void) __attribute__ ((naked));
+
+// STK500v1 main loop, very similar to optiboot in protocol and implementation
+int main()
+{
+ TXLED0;
+ RXLED0;
+ LED0;
+ BOARD_INIT();
+ USBInit();
+
+#ifdef WRITABLE_DIRECTORY
+ _sector3[0] = 0;
+#endif
+
+ _inSync = STK_INSYNC;
+ _ok = STK_OK;
+#ifndef MSC_ENABLED
+ if (pgm_read_word(0) != -1)
+ _ejected = 1;
+#endif
+ for(;;)
+ {
+ u8* packet = _flashbuf;
+ u16 address = 0;
+ for (;;)
+ {
+ u8 cmd = getch();
+
+ // Read packet contents
+ u8 len;
+ const u8* rs = _readSize;
+ for(;;)
+ {
+ u8 c = pgm_read_byte(rs++);
+ len = pgm_read_byte(rs++);
+ if (c == cmd || c == 0)
+ break;
+ }
+#ifndef MSC_ENABLED
+ _timeout = 0;
+#endif
+ // Read params
+ Recv(CDC_RX,packet,len);
+
+ // Send a response
+ u8 send = 0;
+ const u8* pgm = _consts+7; // 0
+ if (STK_GET_PARAMETER == cmd)
+ {
+ u8 i = packet[0] - 0x80;
+ if (i > 2)
+ i = (i == 0x18) ? 3 : 4; // 0x80:HW_VER,0x81:SW_MAJOR,0x82:SW_MINOR,0x18:3 or 0
+ pgm = _consts + i + 3;
+ send = 1;
+ }
+
+ else if (STK_UNIVERSAL == cmd)
+ {
+ if (packet[0] == 0x30)
+ pgm = _consts + packet[2]; // read signature
+ send = 1;
+ }
+
+ // Read signature bytes
+ else if (STK_READ_SIGN == cmd)
+ {
+ pgm = _consts;
+ send = 3;
+ }
+
+ else if (STK_LOAD_ADDRESS == cmd)
+ {
+ address = *((u16*)packet); // word addresses
+ address += address;
+ }
+
+ else if (STK_PROG_PAGE == cmd)
+ {
+ Program(CDC_RX,address,packet[1]);
+ }
+
+ else if (STK_READ_PAGE == cmd)
+ {
+ send = packet[1];
+ pgm = (const u8*)address;
+ address += send; // not sure of this is required
+ }
+
+ // Check sync
+ if (getch() != ' ')
+ break;
+ Transfer(CDC_TX,&_inSync,1);
+
+ // Send result
+ if (send)
+ Transfer(CDC_TX|TRANSFER_PGM,pgm,send); // All from pgm memory
+
+ // Send ok
+ Transfer(CDC_TX|TRANSFER_RELEASE,&_ok,1);
+
+ if (cmd == 'Q')
+ break;
+ }
+ _timeout = 1; // signal to exit the bootloader
+ _ejected = 1;
+ }
+}
+
+// Nice breathing LED indicates we are in the firmware
+u16 _pulse;
+void LEDPulse()
+{
+ _pulse++;
+ u8 p = _pulse >> 8;
+ if (p > 127)
+ p = 255-p;
+ p += p;
+ if (((u8)_pulse) > p)
+ LED0;
+ else
+ LED1;
+}
+
+void Reboot()
+{
+ /* TODO - ZE - this should probably be a WDT reset instead, right? */
+ UDCON = 1; // Detatch USB
+ UDIEN = 0;
+ asm volatile ( // Reset vector to run firmware
+ "clr r30\n"
+ "clr r31\n"
+ "ijmp\n"
+ ::);
+}
diff --git a/bootloaders/diskloader/src/Platform.h b/bootloaders/diskloader/src/Platform.h
new file mode 100644
index 0000000..004933e
--- /dev/null
+++ b/bootloaders/diskloader/src/Platform.h
@@ -0,0 +1,56 @@
+
+#include <inttypes.h>
+#include <avr/io.h>
+#include <avr/pgmspace.h>
+#include <avr/boot.h>
+#include <util/delay.h>
+#include <avr/interrupt.h>
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+
+typedef unsigned char u8;
+typedef unsigned short u16;
+typedef unsigned long u32;
+
+//#define MICROTOUCH
+
+//#define WRITABLE_DIRECTORY // undef saved 56 + 512 RAM
+
+#define FAT_DISK_LABEL 'b','o','o','t','l','o','a','d','e','r',' ' // 11 chars (undef saves 12)
+#define FAT_FILE_NAME 'F','I','R','M','W','A','R','E','B','I','N' // 11 chars
+#define MSC_DISK_SERIAL '0','0','0','0','0','0','0','0','1','7','0','1' // 12 chars
+
+
+#define CPU_PRESCALE(n) (CLKPR = 0x80, CLKPR = (n))
+#define DISABLE_JTAG() MCUCR = (1 << JTD) | (1 << IVCE) | (0 << PUD); MCUCR = (1 << JTD) | (0 << IVSEL) | (0 << IVCE) | (0 << PUD);
+
+#define USB_VID 0x2341 // arduino LLC vid
+#define USB_PID 0x0020 // educated guess
+
+#define FAT_OEM_NAME 'l','e','o','n','a','r','d','o' // 8 chars
+#define BOARD_INIT() DDRC |= (1<<7); DDRB |= (1<<0); DDRE |= (1<<6); CPU_PRESCALE(0); DISABLE_JTAG();
+#define LED0 PORTC &= ~(1<<7)
+#define LED1 PORTC |= (1<<7)
+#define TXLED0 PORTE |= (1<<6)
+#define TXLED1 PORTE &= ~(1<<6)
+#define RXLED0 PORTB |= (1<<0)
+#define RXLED1 PORTB &= ~(1<<0)
+
+#define TRANSFER_PGM 0x80
+#define TRANSFER_RELEASE 0x40
+#define TRANSFER_ZERO 0x20
+
+void Transfer(u8 ep, const u8* data, int len);
+void Recv(u8 ep, u8* dst, u8 len);
+void Program(u8 ep, u16 page, u8 count);
+
+#define CDC_ENABLED
+//#define MSC_ENABLED
+//#define MSC_NO_MEDIA // MSC is enabled but not media inserted
+
+#include "USBCore.h"
+#include "USBDesc.h"
+
+
diff --git a/bootloaders/diskloader/src/SCSI.cpp b/bootloaders/diskloader/src/SCSI.cpp
new file mode 100644
index 0000000..dbbf9cf
--- /dev/null
+++ b/bootloaders/diskloader/src/SCSI.cpp
@@ -0,0 +1,213 @@
+
+
+/* Copyright (c) 2010, Peter Barrett
+**
+** Permission to use, copy, modify, and/or distribute this software for
+** any purpose with or without fee is hereby granted, provided that the
+** above copyright notice and this permission notice appear in all copies.
+**
+** THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL
+** WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED
+** WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR
+** BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES
+** OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
+** WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,
+** ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
+** SOFTWARE.
+*/
+
+#include "Platform.h"
+
+#ifdef MSC_ENABLED
+
+// SCSI Commands
+#define TEST_UNIT_READY 0x00
+#define REQUEST_SENSE 0x03
+#define FORMAT_UNIT 0x04
+#define INQUIRY 0x12
+#define MODE_SENSE 0x1A
+#define START_STOP_UNIT 0x1B
+#define MEDIA_REMOVAL 0x1E
+#define READ_FORMAT_CAPACITIES 0x23
+#define READ_CAPACITY 0x25
+#define READ10 0x28
+#define WRITE10 0x2A
+#define VERIFY10 0x2F
+
+
+// In FakeFAT
+void SendSector(u8 b);
+void RecvSector(u8 b);
+
+// In USBCore
+u8 RecvPacket(u8 ep, u8* dst, u8 len);
+u8 SendPacket(u8 ep, const u8* src, u8 len);
+void Reboot();
+
+#define MSC_TX MSC_ENDPOINT_IN
+#define MSC_RX MSC_ENDPOINT_OUT
+
+extern const u8 P_Inquiry[36] PROGMEM;
+extern const u8 P_RequestSense[18] PROGMEM;
+extern const u8 P_ReadCapacity[8-3] PROGMEM;
+extern const u8 P_ModeSense[4] PROGMEM;
+
+typedef struct
+{
+ u8 signature[4];
+ u8 tag[4];
+ u32 transferLength;
+ u8 flags;
+ u8 lun;
+ u8 cblength;
+ u8 cb[16];
+} CBW;
+
+const u8 P_Inquiry[36] =
+{
+ 0x00, // Direct Access Device
+ 0x80, // Removable Medium
+ 0x00, // Version
+ 0x01,
+
+ 36-4, // Additional Length
+ 0x80, // Storage Controller Component
+ 0x00,
+ 0x00,
+
+ 'r','o','s','s','u','m','u','r', // Vendor
+
+ 'm','i','c','r','o','t','o','u', // Product
+ 'c','h',' ',' ',' ',' ',' ',' ',
+
+ '2','.','4',' ' // Revision
+};
+
+const u8 P_RequestSense[18] =
+{
+ 0x70, // Response Code
+ 0x00,
+// 0x02, // Sense Key
+ 0x00,
+ 0x00,
+
+ 0x00,
+ 0x00,
+ 0x00,
+ 0x0A, // Additional Length
+
+ 0x00,
+ 0x00,
+ 0x00,
+ 0x00,
+
+ 0x30, // ASC
+ 0x01, // ASCQ
+
+ 0x00,
+ 0x00,
+ 0x00,
+ 0x00
+};
+
+// Save 3 bytes if merging
+const u8 P_ReadCapacity[8-3] =
+{
+// 0x00,
+// 0x00,
+// 0x00, // Steal 3 zero bytes from end of P_RequestSense
+ 64+4, // block count 64 + 4
+ 0x00,
+ 0x00,
+ 0x02, // block size = 512
+ 0x00
+};
+
+const u8 P_ModeSense[4] =
+{
+ 0x03,
+ 0x00,
+ 0x00,
+ 0x00
+};
+
+extern volatile u8 _ejected;
+
+void SCSI(CBW& cbw)
+{
+ const u8* pgm = 0;
+ u8 status = 0;
+ u8 blen = 0;
+
+ u8 cmd = cbw.cb[0];
+ if (START_STOP_UNIT == cmd)
+ {
+ _ejected = 1; // Stopped; going away
+ }
+ else if (TEST_UNIT_READY == cmd || MEDIA_REMOVAL == cmd)
+ {
+#ifdef MSC_NO_MEDIA
+ status = 1;
+#else
+ status = _ejected;
+#endif
+ }
+ else if (REQUEST_SENSE == cmd)
+ {
+ pgm = P_RequestSense;
+ blen = sizeof(P_RequestSense);
+ }
+ else if (INQUIRY == cmd)
+ {
+ pgm = P_Inquiry;
+ blen = sizeof(P_Inquiry);
+ }
+ else if (WRITE10 == cmd || READ10 == cmd)
+ {
+ // Write 10 byte form
+ // Read 10 byte form
+ {
+ u8 lba = cbw.cb[5];
+ u8 count = cbw.cb[8];
+ u8 read = READ10 == cbw.cb[0];
+ while (count--)
+ {
+ if (read)
+ SendSector(lba);
+ else
+ RecvSector(lba);
+ lba++;
+ cbw.transferLength -= 512; // 24 bytes
+ }
+ }
+ }
+ else if (MODE_SENSE == cmd)
+ {
+ pgm = P_ModeSense;
+ blen = 4;
+ }
+ else
+ {
+ pgm = P_ReadCapacity-3;
+ blen = 8;
+ if (READ_CAPACITY != cmd)
+ status = 1; // Faking a data phase for unknown commands
+ }
+
+ if (pgm)
+ Transfer(MSC_TX | TRANSFER_PGM | TRANSFER_RELEASE,pgm,blen);
+
+ cbw.flags = status;
+ cbw.transferLength -= blen;
+ cbw.signature[3] = 'S';
+ Transfer(MSC_TX | TRANSFER_RELEASE,(u8*)&cbw,13);
+}
+
+CBW cbw; // 38 byte savings by making this global
+void SCSITask()
+{
+ Recv(MSC_RX,(u8*)&cbw,sizeof(CBW));
+ SCSI(cbw);
+}
+
+#endif \ No newline at end of file
diff --git a/bootloaders/diskloader/src/ThinFAT.cpp b/bootloaders/diskloader/src/ThinFAT.cpp
new file mode 100644
index 0000000..c238741
--- /dev/null
+++ b/bootloaders/diskloader/src/ThinFAT.cpp
@@ -0,0 +1,130 @@
+
+
+/* Copyright (c) 2011, Peter Barrett
+**
+** Permission to use, copy, modify, and/or distribute this software for
+** any purpose with or without fee is hereby granted, provided that the
+** above copyright notice and this permission notice appear in all copies.
+**
+** THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL
+** WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED
+** WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR
+** BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES
+** OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
+** WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,
+** ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
+** SOFTWARE.
+*/
+
+#include "Platform.h"
+
+#ifdef MSC_ENABLED
+
+extern const u8 Sector_0[] PROGMEM;
+extern const u8 Sector_1[] PROGMEM;
+extern const u8 Sector_2[] PROGMEM;
+extern const u8 Sector_3[] PROGMEM;
+extern const u8 Sector_Size[] PROGMEM;
+
+
+const u8 Sector_0[27] =
+{
+ 0xEB,0x3C,0x90,
+ FAT_OEM_NAME,
+ 0x00,0x02, // Bytes per sector 512
+ 0x40, // Sectors per Cluster 32k per cluster
+ 0x01,0x00, // Reserved sectors
+ 0x02, // FATSs
+ 0x10,0x00, // root entries
+ 4+64,0x00, // number of sectors: 30k file
+ 0xF8, // Media type fixed
+ 0x01,0x00, // Sectors per FAT
+ 0x01,0x00, // Sectors per head
+ 0x01,//0x00, // Heads per cylinder
+ //0x00,0x00,0x00,0x00, // Hidden
+ //0x00,0x00,0x00,0x00, // long number of sectors
+ //0x00,0x00, // Ext flags
+ // 38 bytes
+};
+
+// FAT12 clusters for 32k file
+const u8 Sector_1[] =
+{
+#if 0
+ 0xF8,0xFF,0xFF,
+ 0xFF,0x0F//,0x00
+#endif
+};
+
+const u8 Sector_2[] =
+{
+};
+
+const u8 Sector_3[] =
+{
+#if 0
+ FAT_FILE_NAME, 0x00,0x00,0x00,0x00,0x00, // Uppercase name please
+ 0x43,0x3E,0x43,0x3E,0x00,0x00,0xAB,0x8C,0x40,0x3E,0x02,0x00,0x00,0x80,//0x00,0x00
+ // 0x00,0x00,
+#endif
+
+#ifdef FAT_DISK_LABEL
+ FAT_DISK_LABEL,0x28
+#endif
+};
+
+#if 0
+const u8 Sector_Size[] =
+{
+ 0,
+ sizeof(Sector_0),
+ sizeof(Sector_0) + sizeof(Sector_1),
+ sizeof(Sector_0) + sizeof(Sector_1) + sizeof(Sector_2),
+ sizeof(Sector_0) + sizeof(Sector_1) + sizeof(Sector_2) + sizeof(Sector_3)
+};
+#endif
+
+const u8* LBAToROM(u8 b)
+{
+ b -= 4;
+ return (const u8*)(b << 9);
+}
+
+void SendSector(u8 b)
+{
+ if (b == 0)
+ {
+ u8 n = sizeof(Sector_0);
+ Transfer(MSC_TX | TRANSFER_PGM,Sector_0,n);
+ Transfer(MSC_TX | TRANSFER_ZERO,0,512-n);
+ return;
+ }
+
+ if ((b < 4) || (b >= 64))
+ {
+ Transfer(MSC_TX | TRANSFER_ZERO,0,512);
+ return;
+ }
+
+ // Copy body of file from ROM
+ Transfer(MSC_TX | TRANSFER_PGM,LBAToROM(b),512);
+}
+
+#define GENERATE_INVALID_ROM_ADDRESS 60
+
+void RecvSector(u8 lba)
+{
+ if ((lba < 4) || (lba >= 64))
+ lba = GENERATE_INVALID_ROM_ADDRESS; // Only care about the first 30k
+
+ // Write sectors to flash
+ u16 addr = (u16)LBAToROM(lba); // may generate out of range addresses, Program will fix
+ u8 i = 4; // 4x128 is a sector
+ while (i--)
+ {
+ Program(MSC_RX,addr,128);
+ addr += 128;
+ }
+}
+
+#endif \ No newline at end of file
diff --git a/bootloaders/diskloader/src/USBCore.cpp b/bootloaders/diskloader/src/USBCore.cpp
new file mode 100644
index 0000000..8b071a1
--- /dev/null
+++ b/bootloaders/diskloader/src/USBCore.cpp
@@ -0,0 +1,526 @@
+
+
+/* Copyright (c) 2010, Peter Barrett
+**
+** Permission to use, copy, modify, and/or distribute this software for
+** any purpose with or without fee is hereby granted, provided that the
+** above copyright notice and this permission notice appear in all copies.
+**
+** THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL
+** WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED
+** WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR
+** BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES
+** OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
+** WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,
+** ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
+** SOFTWARE.
+*/
+
+#include "Platform.h"
+
+#define MSC_TX MSC_ENDPOINT_IN
+#define MSC_RX MSC_ENDPOINT_OUT
+#define CDC_TX CDC_ENDPOINT_IN
+#define CDC_RX CDC_ENDPOINT_OUT
+
+#define EP_TYPE_CONTROL 0x00
+#define EP_TYPE_BULK_IN 0x81
+#define EP_TYPE_BULK_OUT 0x80
+#define EP_TYPE_INTERRUPT_IN 0xC1
+#define EP_TYPE_INTERRUPT_OUT 0xC0
+#define EP_TYPE_ISOCHRONOUS_IN 0x41
+#define EP_TYPE_ISOCHRONOUS_OUT 0x40
+
+void Reboot();
+
+//==================================================================
+//==================================================================
+
+typedef struct
+{
+ u32 dwDTERate;
+ u8 bCharFormat;
+ u8 bParityType;
+ u8 bDataBits;
+ u8 lineState;
+} LineInfo;
+
+static volatile LineInfo _usbLineInfo = { 57600, 0x00, 0x00, 0x00, 0x00 };
+
+//==================================================================
+//==================================================================
+
+// 4 bytes of RAM
+volatile u8 _usbConfiguration;
+volatile u8 _ejected;
+volatile u16 _timeout;
+
+static inline void WaitIN(void)
+{
+ while (!(UEINTX & (1<<TXINI)));
+}
+
+static inline void ClearIN(void)
+{
+ UEINTX = ~(1<<TXINI);
+}
+
+static inline void WaitOUT(void)
+{
+ while (!(UEINTX & (1<<RXOUTI)))
+ ;
+}
+
+static inline u8 WaitForINOrOUT()
+{
+ while (!(UEINTX & ((1<<TXINI)|(1<<RXOUTI))))
+ ;
+ return (UEINTX & (1<<RXOUTI)) == 0;
+}
+
+static inline void ClearOUT(void)
+{
+ UEINTX = ~(1<<RXOUTI);
+}
+
+static
+void Send(volatile const u8* data, u8 count)
+{
+ TXLED1; // light the TX LED
+ while (count--)
+ UEDATX = *data++;
+}
+
+void Recv(volatile u8* data, u8 count)
+{
+ RXLED1; // light the RX LED
+ while (count--)
+ *data++ = UEDATX;
+}
+
+static inline u8 Recv8()
+{
+ RXLED1; // light the RX LED
+ return UEDATX;
+}
+
+static inline void Send8(u8 d)
+{
+ TXLED1; // light the TX LED
+ UEDATX = d;
+}
+
+static inline void SetEP(u8 ep)
+{
+ UENUM = ep;
+}
+
+static inline u8 FifoByteCount()
+{
+ return UEBCLX;
+}
+
+static inline u8 ReceivedSetupInt()
+{
+ return UEINTX & (1<<RXSTPI);
+}
+
+static inline void ClearSetupInt()
+{
+ UEINTX = ~((1<<RXSTPI) | (1<<RXOUTI) | (1<<TXINI));
+}
+
+static inline void Stall()
+{
+ UECONX = (1<<STALLRQ) | (1<<EPEN);
+}
+
+static inline u8 ReadWriteAllowed()
+{
+ return UEINTX & (1<<RWAL);
+}
+
+static inline u8 Stalled()
+{
+ return UEINTX & (1<<STALLEDI);
+}
+
+static inline u8 FifoFree()
+{
+ return UEINTX & (1<<FIFOCON);
+}
+
+static inline void ReleaseRX()
+{
+ UEINTX = 0x6B; // FIFOCON=0 NAKINI=1 RWAL=1 NAKOUTI=0 RXSTPI=1 RXOUTI=0 STALLEDI=1 TXINI=1
+}
+
+static inline void ReleaseTX()
+{
+ UEINTX = 0x3A; // FIFOCON=0 NAKINI=0 RWAL=1 NAKOUTI=1 RXSTPI=1 RXOUTI=0 STALLEDI=1 TXINI=0
+}
+
+static inline u8 FrameNumber()
+{
+ return UDFNUML;
+}
+
+//==================================================================
+//==================================================================
+
+#define EP_SINGLE_64 0x32 // EP0
+#define EP_DOUBLE_64 0x36 // Other endpoints
+
+static void InitEP(u8 index, u8 type, u8 size)
+{
+ UENUM = index;
+ UECONX = 1;
+ UECFG0X = type;
+ UECFG1X = size;
+}
+
+// API
+void USBInit(void)
+{
+ _timeout = 0;
+ _usbConfiguration = 0;
+ _ejected = 0;
+
+ UHWCON = 0x01; // power internal reg (don't need this?)
+ USBCON = (1<<USBE)|(1<<FRZCLK); // clock frozen, usb enabled
+ PLLCSR = 0x12; // Need 16 MHz xtal
+ while (!(PLLCSR & (1<<PLOCK))) // wait for lock pll
+ ;
+ USBCON = ((1<<USBE)|(1<<OTGPADE)); // start USB clock
+ UDCON = 0; // enable attach resistor
+}
+
+u8 USBGetConfiguration(void)
+{
+ return _usbConfiguration;
+}
+
+u8 HasData(u8 ep)
+{
+ SetEP(ep);
+ return ReadWriteAllowed(); // count in fifo
+}
+
+int USBGetChar();
+void Recv(u8 ep, u8* dst, u8 len)
+{
+ SetEP(ep);
+ while (len--)
+ {
+ while (!ReadWriteAllowed())
+ ;
+ *dst++ = Recv8();
+ if (!ReadWriteAllowed()) // release empty buffer
+ ReleaseRX();
+ }
+}
+
+// Transmit a packet to endpoint
+void Transfer(u8 ep, const u8* data, int len)
+{
+ u8 zero = ep & TRANSFER_ZERO;
+ SetEP(ep & 7);
+ while (len--)
+ {
+ while (!ReadWriteAllowed())
+ ; // TODO Check for STALL etc
+
+ u8 d = (ep & TRANSFER_PGM) ? pgm_read_byte(data) : data[0];
+ data++;
+ if (zero)
+ d = 0;
+ Send8(d);
+
+ if (!ReadWriteAllowed())
+ ReleaseTX();
+ }
+ if (ep & TRANSFER_RELEASE)
+ ReleaseTX();
+}
+
+extern const u8 _initEndpoints[] PROGMEM;
+const u8 _initEndpoints[] =
+{
+ 0,
+
+#ifdef CDC_ENABLED
+ EP_TYPE_INTERRUPT_IN, // CDC_ENDPOINT_ACM
+ EP_TYPE_BULK_OUT, // CDC_ENDPOINT_OUT
+ EP_TYPE_BULK_IN, // CDC_ENDPOINT_IN
+#endif
+
+ EP_TYPE_INTERRUPT_IN, // HID_ENDPOINT_INT
+
+#ifdef MSC_ENABLED
+ EP_TYPE_BULK_OUT, // MSC_ENDPOINT_OUT
+ EP_TYPE_BULK_IN // MSC_ENDPOINT_IN
+#endif
+};
+
+static void InitEndpoints()
+{
+ for (u8 i = 1; i < sizeof(_initEndpoints); i++)
+ {
+ UENUM = i;
+ UECONX = 1;
+ UECFG0X = pgm_read_byte(_initEndpoints+i);
+ UECFG1X = EP_DOUBLE_64;
+ }
+ UERST = 0x7E; // And reset them
+ UERST = 0;
+}
+
+typedef struct
+{
+ u8 bmRequestType;
+ u8 bRequest;
+ u8 wValueL;
+ u8 wValueH;
+ u16 wIndex;
+ u16 wLength;
+} Setup;
+Setup _setup;
+
+//bool USBHook(Setup& setup)
+bool USBHook()
+{
+ Setup& setup = _setup;
+ u8 r = setup.bRequest;
+
+ // CDC Requests
+ if (CDC_GET_LINE_CODING == r)
+ {
+ Send((const volatile u8*)&_usbLineInfo,7);
+ }
+
+ else if (CDC_SET_LINE_CODING == r)
+ {
+ WaitOUT();
+ Recv((volatile u8*)&_usbLineInfo,7);
+ ClearOUT();
+ }
+
+ else if (CDC_SET_CONTROL_LINE_STATE == r)
+ {
+ _usbLineInfo.lineState = setup.wValueL;
+ }
+
+#ifdef MSC_ENABLED
+ // MSC Requests
+ else if (MSC_GET_MAX_LUN == r)
+ {
+ Send8(0);
+ }
+ else if (MSC_RESET == r)
+ {
+ // MSC_Reset();
+ } else
+ return false; // unhandled
+#endif
+
+ return true;
+}
+
+extern const u8 _rawHID[] PROGMEM;
+#define LSB(_x) ((_x) & 0xFF)
+#define MSB(_x) ((_x) >> 8)
+
+#define RAWHID_USAGE_PAGE 0xFFC0
+#define RAWHID_USAGE 0x0C00
+#define RAWHID_TX_SIZE 64
+#define RAWHID_RX_SIZE 64
+
+const u8 _rawHID[] =
+{
+ // RAW HID
+ 0x06, LSB(RAWHID_USAGE_PAGE), MSB(RAWHID_USAGE_PAGE), // 30
+ 0x0A, LSB(RAWHID_USAGE), MSB(RAWHID_USAGE),
+
+ 0xA1, 0x01, // Collection 0x01
+ 0x85, 0x03, // REPORT_ID (3)
+ 0x75, 0x08, // report size = 8 bits
+ 0x15, 0x00, // logical minimum = 0
+ 0x26, 0xFF, 0x00, // logical maximum = 255
+
+ 0x95, 64, // report count TX
+ 0x09, 0x01, // usage
+ 0x81, 0x02, // Input (array)
+
+ 0x95, 64, // report count RX
+ 0x09, 0x02, // usage
+ 0x91, 0x02, // Output (array)
+ 0xC0 // end collection
+};
+
+u8 _cdcComposite = 0;
+
+bool SendDescriptor()
+{
+ Setup& setup = _setup;
+ u8 desc_length = 0;
+ const u8* desc_addr = 0;
+
+ u8 t = setup.wValueH;
+ if (0x22 == t)
+ {
+ desc_addr = _rawHID;
+ desc_length = sizeof(desc_length);
+ } else if (USB_DEVICE_DESCRIPTOR_TYPE == t)
+ {
+ if (setup.wLength == 8)
+ _cdcComposite = 1;
+ desc_addr = _cdcComposite ? (const u8*)&USB_DeviceDescriptorA : (const u8*)&USB_DeviceDescriptor;
+ }
+ else if (USB_CONFIGURATION_DESCRIPTOR_TYPE == t)
+ {
+ desc_addr = (const u8*)&USB_ConfigDescriptor;
+ desc_length = sizeof(USB_ConfigDescriptor);
+ }
+ else if (USB_STRING_DESCRIPTOR_TYPE == t)
+ {
+ if (setup.wValueL == 0)
+ desc_addr = (const u8*)&STRING_LANGUAGE;
+ else if (setup.wValueL == ISERIAL)
+ desc_addr = (const u8*)&STRING_SERIAL;
+ else
+ return false;
+ } else
+ return false;
+
+ if (desc_length == 0)
+ desc_length = pgm_read_byte(desc_addr);
+ if ((u8)setup.wLength < desc_length) // bit of a cheat limiting to 255 bytes TODO (saved 8 bytes)
+ desc_length = (u8)setup.wLength;
+
+ // Send descriptor
+ // EP0 is 64 bytes long
+ // RWAL and FIFOCON don't work on EP0
+ u8 n = 0;
+ do
+ {
+ if (!WaitForINOrOUT())
+ return false;
+ Send8(pgm_read_byte(&desc_addr[n++]));
+ u8 clr = n & 0x3F;
+ if (!clr)
+ ClearIN(); // Fifo is full, release this packet
+ } while (n < desc_length);
+ return true;
+}
+
+void USBSetupInterrupt()
+{
+ SetEP(0);
+ if (!ReceivedSetupInt())
+ return;
+
+ Setup& setup = _setup; // global saves ~30 bytes
+ Recv((u8*)&setup,8);
+ ClearSetupInt();
+
+ if (setup.bmRequestType & DEVICETOHOST)
+ WaitIN();
+ else
+ ClearIN();
+
+ bool ok = true;
+ u8 r = setup.bRequest;
+ if (SET_ADDRESS == r)
+ {
+ WaitIN();
+ UDADDR = setup.wValueL | (1<<ADDEN);
+ }
+ else if (SET_CONFIGURATION == r)
+ {
+ _usbConfiguration = setup.wValueL;
+ InitEndpoints();
+ }
+ else if (GET_CONFIGURATION == r)
+ {
+ Send8(_usbConfiguration);
+ }
+ else if (GET_STATUS == r)
+ {
+ Send8(0); // All good as far as I know
+ }
+ else if (GET_DESCRIPTOR == r)
+ {
+ ok = SendDescriptor();
+ }
+ else
+ {
+ ok = USBHook();
+ }
+
+ if (ok)
+ ClearIN();
+ else
+ Stall();
+}
+
+void USBGeneralInterrupt()
+{
+ u8 udint = UDINT;
+ UDINT = 0;
+
+ // End of Reset
+ if (udint & (1<<EORSTI))
+ {
+ InitEP(0,EP_TYPE_CONTROL,EP_SINGLE_64); // init ep0
+ _usbConfiguration = 0; // not configured yet
+ //UEIENX = 1 << RXSTPE;
+ }
+
+ // Start of Frame
+ if (udint & (1<<SOFI))
+ {
+ if (!_ejected)
+ _timeout = 0;
+ }
+}
+
+void SCSITask();
+
+void LEDPulse();
+int USBGetChar()
+{
+ for(;;)
+ {
+ USBSetupInterrupt();
+ USBGeneralInterrupt();
+
+#ifdef MSC_ENABLED
+ // Service disk
+ if (HasData(MSC_RX))
+ SCSITask();
+#endif
+ // Read a char
+ if (HasData(CDC_RX))
+ {
+ u8 c = Recv8();
+ if (!ReadWriteAllowed())
+ ReleaseRX();
+ return c;
+ } else {
+ u8 temp = 0;
+ for (temp=100; temp; temp--)
+ asm volatile("nop\n\t" "nop\n\t" "nop\n\t" "nop\n\t"::);
+ RXLED0; // we turn the RX and TX LEDs on in the relevant Send or Recv instruction
+ TXLED0; // we turn them off here after some time has passed to ensure a minimum on time.
+ }
+
+ if (!--_timeout) {
+ TXLED0; // switch off the RX and TX LEDs before starting the user sketch
+ RXLED0;
+ Reboot(); // USB not connected, run firmware
+ }
+
+ LEDPulse();
+ }
+ return -1;
+}
diff --git a/bootloaders/diskloader/src/USBCore.h b/bootloaders/diskloader/src/USBCore.h
new file mode 100644
index 0000000..a37b9f2
--- /dev/null
+++ b/bootloaders/diskloader/src/USBCore.h
@@ -0,0 +1,261 @@
+
+// Copyright (c) 2010, Peter Barrett
+/*
+** Permission to use, copy, modify, and/or distribute this software for
+** any purpose with or without fee is hereby granted, provided that the
+** above copyright notice and this permission notice appear in all copies.
+**
+** THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL
+** WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED
+** WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR
+** BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES
+** OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
+** WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,
+** ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
+** SOFTWARE.
+*/
+
+#ifndef __USBCORE_H__
+#define __USBCORE_H__
+
+#define GET_STATUS 0
+#define CLEAR_FEATURE 1
+#define SET_FEATURE 3
+#define SET_ADDRESS 5
+#define GET_DESCRIPTOR 6
+#define GET_CONFIGURATION 8
+#define SET_CONFIGURATION 9
+#define GET_INTERFACE 10
+#define SET_INTERFACE 11
+
+// bmRequestType
+#define HOSTTODEVICE 0x00
+#define DEVICETOHOST 0x80
+#define STANDARD 0x00
+#define CLASS 0x20
+#define VENDOR 0x40
+#define DEVICE 0x00
+#define INTERFACE 0x01
+#define ENDPOINT 0x02
+#define OTHER 0x03
+
+#define CDC_SET_LINE_CODING 0x20
+#define CDC_GET_LINE_CODING 0x21
+#define CDC_SET_CONTROL_LINE_STATE 0x22
+
+#define MSC_RESET 0xFF
+#define MSC_GET_MAX_LUN 0xFE
+
+
+// Descriptors
+
+#define USB_DEVICE_DESC_SIZE 18
+#define USB_CONFIGUARTION_DESC_SIZE 9
+#define USB_INTERFACE_DESC_SIZE 9
+#define USB_ENDPOINT_DESC_SIZE 7
+
+#define USB_DEVICE_DESCRIPTOR_TYPE 1
+#define USB_CONFIGURATION_DESCRIPTOR_TYPE 2
+#define USB_STRING_DESCRIPTOR_TYPE 3
+#define USB_INTERFACE_DESCRIPTOR_TYPE 4
+#define USB_ENDPOINT_DESCRIPTOR_TYPE 5
+
+#define USB_DEVICE_CLASS_COMMUNICATIONS 0x02
+#define USB_DEVICE_CLASS_HUMAN_INTERFACE 0x03
+#define USB_DEVICE_CLASS_STORAGE 0x08
+#define USB_DEVICE_CLASS_VENDOR_SPECIFIC 0xFF
+
+#define USB_CONFIG_POWERED_MASK 0x40
+#define USB_CONFIG_BUS_POWERED 0x80
+#define USB_CONFIG_SELF_POWERED 0xC0
+#define USB_CONFIG_REMOTE_WAKEUP 0x20
+
+// bMaxPower in Configuration Descriptor
+#define USB_CONFIG_POWER_MA(mA) ((mA)/2)
+
+// bEndpointAddress in Endpoint Descriptor
+#define USB_ENDPOINT_DIRECTION_MASK 0x80
+#define USB_ENDPOINT_OUT(addr) ((addr) | 0x00)
+#define USB_ENDPOINT_IN(addr) ((addr) | 0x80)
+
+#define USB_ENDPOINT_TYPE_MASK 0x03
+#define USB_ENDPOINT_TYPE_CONTROL 0x00
+#define USB_ENDPOINT_TYPE_ISOCHRONOUS 0x01
+#define USB_ENDPOINT_TYPE_BULK 0x02
+#define USB_ENDPOINT_TYPE_INTERRUPT 0x03
+
+#define TOBYTES(x) ((x) & 0xFF),(((x) >> 8) & 0xFF)
+
+#define CDC_V1_10 0x0110
+#define CDC_COMMUNICATION_INTERFACE_CLASS 0x02
+
+#define CDC_CALL_MANAGEMENT 0x01
+#define CDC_ABSTRACT_CONTROL_MODEL 0x02
+#define CDC_HEADER 0x00
+#define CDC_ABSTRACT_CONTROL_MANAGEMENT 0x02
+#define CDC_UNION 0x06
+#define CDC_CS_INTERFACE 0x24
+#define CDC_CS_ENDPOINT 0x25
+#define CDC_DATA_INTERFACE_CLASS 0x0A
+
+#define MSC_SUBCLASS_SCSI 0x06
+#define MSC_PROTOCOL_BULK_ONLY 0x50
+
+
+
+// Device
+typedef struct {
+ u8 len; // 18
+ u8 dtype; // 1 USB_DEVICE_DESCRIPTOR_TYPE
+ u16 usbVersion; // 0x200
+ u8 deviceClass;
+ u8 deviceSubClass;
+ u8 deviceProtocol;
+ u8 packetSize0; // Packet 0
+ u16 idVendor;
+ u16 idProduct;
+ u16 deviceVersion; // 0x100
+ u8 iManufacturer;
+ u8 iProduct;
+ u8 iSerialNumber;
+ u8 bNumConfigurations;
+} DeviceDescriptor;
+
+// Config
+typedef struct {
+ u8 len; // 9
+ u8 dtype; // 2
+ u16 clen; // total length
+ u8 numInterfaces;
+ u8 config;
+ u8 iconfig;
+ u8 attributes;
+ u8 maxPower;
+} ConfigDescriptor;
+
+// String
+
+// Interface
+typedef struct
+{
+ u8 len; // 9
+ u8 dtype; // 4
+ u8 number;
+ u8 alternate;
+ u8 numEndpoints;
+ u8 interfaceClass;
+ u8 interfaceSubClass;
+ u8 protocol;
+ u8 iInterface;
+} InterfaceDescriptor;
+
+// Endpoint
+typedef struct
+{
+ u8 len; // 7
+ u8 dtype; // 5
+ u8 addr;
+ u8 attr;
+ u16 packetSize;
+ u8 interval;
+} EndpointDescriptor;
+
+// Interface Association Descriptor
+// Used to bind 2 interfaces together in CDC compostite device
+typedef struct
+{
+ u8 len; // 8
+ u8 dtype; // 11
+ u8 firstInterface;
+ u8 interfaceCount;
+ u8 functionClass;
+ u8 funtionSubClass;
+ u8 functionProtocol;
+ u8 iInterface;
+} IADDescriptor;
+
+// CDC CS interface descriptor
+typedef struct
+{
+ u8 len; // 5
+ u8 dtype; // 0x24
+ u8 subtype;
+ u8 d0;
+ u8 d1;
+} CDCCSInterfaceDescriptor;
+
+typedef struct
+{
+ u8 len; // 4
+ u8 dtype; // 0x24
+ u8 subtype;
+ u8 d0;
+} CDCCSInterfaceDescriptor4;
+
+typedef struct
+{
+ IADDescriptor iad; // Only needed on compound device
+
+ // Control
+ InterfaceDescriptor cif; //
+ CDCCSInterfaceDescriptor header;
+ CDCCSInterfaceDescriptor callManagement;
+ CDCCSInterfaceDescriptor4 controlManagement;
+ CDCCSInterfaceDescriptor functionalDescriptor;
+ EndpointDescriptor cifin;
+
+ // Data
+ InterfaceDescriptor dif;
+ EndpointDescriptor in;
+ EndpointDescriptor out;
+} CDCDescriptor;
+
+typedef struct
+{
+ InterfaceDescriptor msc;
+ EndpointDescriptor in;
+ EndpointDescriptor out;
+} MSCDescriptor;
+
+typedef struct
+{
+ u8 len; // 9
+ u8 dtype; // 0x21
+ u8 addr;
+ u8 versionL; // 0x101
+ u8 versionH; // 0x101
+ u8 country;
+ u8 desctype; // 0x22 report
+ u8 descLenL;
+ u8 descLenH;
+} HIDDescDescriptor;
+
+typedef struct
+{
+ InterfaceDescriptor hid;
+ HIDDescDescriptor desc;
+ EndpointDescriptor in;
+} HIDDescriptor;
+
+#define D_DEVICE(_class,_subClass,_proto,_packetSize0,_vid,_pid,_version,_im,_ip,_is,_configs) \
+ { 18, 1, 0x200, _class,_subClass,_proto,_packetSize0,_vid,_pid,_version,_im,_ip,_is,_configs }
+
+#define D_CONFIG(_totalLength,_interfaces) \
+ { 9, 2, _totalLength,_interfaces, 1, 0, USB_CONFIG_BUS_POWERED, USB_CONFIG_POWER_MA(100) }
+
+#define D_INTERFACE(_n,_numEndpoints,_class,_subClass,_protocol) \
+ { 9, 4, _n, 0, _numEndpoints, _class,_subClass, _protocol, 0 }
+
+#define D_ENDPOINT(_addr,_attr,_packetSize, _interval) \
+ { 7, 5, _addr,_attr,_packetSize, _interval }
+
+#define D_IAD(_firstInterface, _count, _class, _subClass, _protocol) \
+ { 8, 11, _firstInterface, _count, _class, _subClass, _protocol, 0 }
+
+#define D_HIDREPORT(_descriptorLength) \
+ { 9, 0x21, 0x1, 0x1, 0, 1, 0x22, _descriptorLength, 0 }
+
+#define D_CDCCS(_subtype,_d0,_d1) { 5, 0x24, _subtype, _d0, _d1 }
+#define D_CDCCS4(_subtype,_d0) { 4, 0x24, _subtype, _d0 }
+
+#endif \ No newline at end of file
diff --git a/bootloaders/diskloader/src/USBDesc.cpp b/bootloaders/diskloader/src/USBDesc.cpp
new file mode 100644
index 0000000..dfde01d
--- /dev/null
+++ b/bootloaders/diskloader/src/USBDesc.cpp
@@ -0,0 +1,88 @@
+
+
+/* Copyright (c) 2011, Peter Barrett
+**
+** Permission to use, copy, modify, and/or distribute this software for
+** any purpose with or without fee is hereby granted, provided that the
+** above copyright notice and this permission notice appear in all copies.
+**
+** THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL
+** WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED
+** WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR
+** BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES
+** OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
+** WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,
+** ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
+** SOFTWARE.
+*/
+
+#include "Platform.h"
+
+//====================================================================================================
+//====================================================================================================
+// Actual device descriptors
+
+const u16 STRING_LANGUAGE[2] = {
+ (3<<8) | (2+2),
+ 0x0409 // English
+};
+
+#if 0
+const u16 STRING_PRODUCT[] = {
+ (3<<8) | (2+2*10),
+ 'M','i','c','r','o','t','o','u','c','h'
+};
+#endif
+
+const u16 STRING_SERIAL[13] = {
+ (3<<8) | (2+2*12),
+ MSC_DISK_SERIAL
+};
+
+
+//#ifdef CDC_ENABLED
+DeviceDescriptor USB_DeviceDescriptorA = D_DEVICE(0X02,0X00,0X00,64,USB_VID,USB_PID,0x100,0,IPRODUCT,ISERIAL,1);
+//#else
+DeviceDescriptor USB_DeviceDescriptor = D_DEVICE(0x00,0x00,0x00,64,USB_VID,USB_PID,0x100,0,IPRODUCT,ISERIAL,1);
+//#endif
+
+Config USB_ConfigDescriptor =
+{
+ D_CONFIG(sizeof(Config),INTERFACE_COUNT),
+
+#ifdef CDC_ENABLED
+ // CDC
+ {
+ D_IAD(0,2,CDC_COMMUNICATION_INTERFACE_CLASS,CDC_ABSTRACT_CONTROL_MODEL,1),
+
+ // CDC communication interface
+ D_INTERFACE(CDC_ACM_INTERFACE,1,CDC_COMMUNICATION_INTERFACE_CLASS,CDC_ABSTRACT_CONTROL_MODEL,0),
+ D_CDCCS(CDC_HEADER,0x10,0x01), // Header (1.10 bcd)
+ D_CDCCS(CDC_CALL_MANAGEMENT,1,1), // Device handles call management
+ D_CDCCS4(CDC_ABSTRACT_CONTROL_MANAGEMENT,2), // SET_LINE_CODING, GET_LINE_CODING, SET_CONTROL_LINE_STATE supported
+ D_CDCCS(CDC_UNION,CDC_ACM_INTERFACE,CDC_DATA_INTERFACE), // Communication interface is master, data interface is slave 0
+ D_ENDPOINT(USB_ENDPOINT_IN (CDC_ENDPOINT_ACM),USB_ENDPOINT_TYPE_INTERRUPT,0x10,0x40),
+
+ // CDC data interface
+ D_INTERFACE(CDC_DATA_INTERFACE,2,CDC_DATA_INTERFACE_CLASS,0,0),
+ D_ENDPOINT(USB_ENDPOINT_OUT(CDC_ENDPOINT_OUT),USB_ENDPOINT_TYPE_BULK,0x40,0),
+ D_ENDPOINT(USB_ENDPOINT_IN (CDC_ENDPOINT_IN ),USB_ENDPOINT_TYPE_BULK,0x40,0)
+ },
+#endif
+ // HID
+ {
+ D_INTERFACE(HID_INTERFACE,1,3,0,0),
+ D_HIDREPORT(30),
+ D_ENDPOINT(USB_ENDPOINT_IN (HID_ENDPOINT_INT),USB_ENDPOINT_TYPE_INTERRUPT,0x40,0x40)
+ },
+
+#ifdef MSC_ENABLED
+ // Mass Storage
+ {
+ D_INTERFACE(MSC_INTERFACE,2,USB_DEVICE_CLASS_STORAGE,MSC_SUBCLASS_SCSI,MSC_PROTOCOL_BULK_ONLY),
+ D_ENDPOINT(USB_ENDPOINT_OUT(MSC_ENDPOINT_OUT),USB_ENDPOINT_TYPE_BULK,0x40,0),
+ D_ENDPOINT(USB_ENDPOINT_IN (MSC_ENDPOINT_IN ),USB_ENDPOINT_TYPE_BULK,0x40,0)
+ }
+#endif
+};
+
diff --git a/bootloaders/diskloader/src/USBDesc.h b/bootloaders/diskloader/src/USBDesc.h
new file mode 100644
index 0000000..a551d69
--- /dev/null
+++ b/bootloaders/diskloader/src/USBDesc.h
@@ -0,0 +1,81 @@
+
+
+/* Copyright (c) 2011, Peter Barrett
+**
+** Permission to use, copy, modify, and/or distribute this software for
+** any purpose with or without fee is hereby granted, provided that the
+** above copyright notice and this permission notice appear in all copies.
+**
+** THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL
+** WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED
+** WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR
+** BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES
+** OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
+** WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,
+** ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
+** SOFTWARE.
+*/
+
+
+#ifndef CDC_ENABLED
+
+#define MSC_INTERFACE 0 // MSC Interface
+#define MSC_ENDPOINT_OUT 1
+#define MSC_ENDPOINT_IN 2
+
+#define INTERFACE_COUNT 1 // 1 for msc
+
+#else
+
+#define CDC_ACM_INTERFACE 0 // CDC ACM
+#define CDC_DATA_INTERFACE 1 // CDC Data
+#define CDC_ENDPOINT_ACM 1
+#define CDC_ENDPOINT_OUT 2
+#define CDC_ENDPOINT_IN 3
+
+#define HID_INTERFACE 2 // HID Interface
+#define HID_ENDPOINT_INT 4
+
+#ifdef MSC_ENABLED
+
+#define MSC_INTERFACE 3 // MSC Interface
+#define MSC_ENDPOINT_OUT 5
+#define MSC_ENDPOINT_IN 6
+#define INTERFACE_COUNT 4 // 2 for cdc + 1 for hid + 1 for msc
+
+#else
+
+#define INTERFACE_COUNT 3 // 2 for cdc + 1 for hid
+
+#endif
+
+#endif
+
+typedef struct
+{
+ ConfigDescriptor config;
+#ifdef CDC_ENABLED
+ CDCDescriptor cdc;
+#endif
+ HIDDescriptor hid;
+#ifdef MSC_ENABLED
+ MSCDescriptor msc;
+#endif
+} Config;
+
+extern Config USB_ConfigDescriptor PROGMEM;
+extern DeviceDescriptor USB_DeviceDescriptor PROGMEM;
+extern DeviceDescriptor USB_DeviceDescriptorA PROGMEM;
+
+extern const u16 STRING_LANGUAGE[2] PROGMEM;
+extern const u16 STRING_SERIAL[13] PROGMEM;
+
+#define IMANUFACTURER 0
+#define IPRODUCT 0
+#define ISERIAL 1 // Only need this for MSC
+
+
+#define MSC_TX MSC_ENDPOINT_IN
+#define MSC_RX MSC_ENDPOINT_OUT
+#define CDC_TX CDC_ENDPOINT_IN
+#define CDC_RX CDC_ENDPOINT_OUT \ No newline at end of file
diff --git a/cores/arduino/CDC.cpp b/cores/arduino/CDC.cpp
new file mode 100644
index 0000000..0d04b9c
--- /dev/null
+++ b/cores/arduino/CDC.cpp
@@ -0,0 +1,184 @@
+
+
+/* Copyright (c) 2011, Peter Barrett
+**
+** Permission to use, copy, modify, and/or distribute this software for
+** any purpose with or without fee is hereby granted, provided that the
+** above copyright notice and this permission notice appear in all copies.
+**
+** THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL
+** WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED
+** WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR
+** BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES
+** OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
+** WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,
+** ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
+** SOFTWARE.
+*/
+
+#include "Platform.h"
+#include "USBAPI.h"
+#include <avr/wdt.h>
+
+#if defined(USBCON)
+#ifdef CDC_ENABLED
+
+// TODO: Should really use the wdt here
+// Not currently working for a non-obvious reason
+
+typedef void (*AppPtr_t)(void) __attribute__ ((noreturn));
+AppPtr_t Bootloader = (AppPtr_t)(30*1024);
+
+void Reboot()
+{
+ USB.detach();
+ cli();
+ Bootloader();
+ //wdt_enable(WDTO_15MS);
+ //while(1); // reboot
+}
+
+typedef struct
+{
+ u32 dwDTERate;
+ u8 bCharFormat;
+ u8 bParityType;
+ u8 bDataBits;
+ u8 lineState;
+} LineInfo;
+
+static volatile LineInfo _usbLineInfo = { 57600, 0x00, 0x00, 0x00, 0x00 };
+
+#define WEAK __attribute__ ((weak))
+
+extern const CDCDescriptor _cdcInterface PROGMEM;
+const CDCDescriptor _cdcInterface =
+{
+ D_IAD(0,2,CDC_COMMUNICATION_INTERFACE_CLASS,CDC_ABSTRACT_CONTROL_MODEL,1),
+
+ // CDC communication interface
+ D_INTERFACE(CDC_ACM_INTERFACE,1,CDC_COMMUNICATION_INTERFACE_CLASS,CDC_ABSTRACT_CONTROL_MODEL,0),
+ D_CDCCS(CDC_HEADER,0x10,0x01), // Header (1.10 bcd)
+ D_CDCCS(CDC_CALL_MANAGEMENT,1,1), // Device handles call management (not)
+ D_CDCCS4(CDC_ABSTRACT_CONTROL_MANAGEMENT,6), // SET_LINE_CODING, GET_LINE_CODING, SET_CONTROL_LINE_STATE supported
+ D_CDCCS(CDC_UNION,CDC_ACM_INTERFACE,CDC_DATA_INTERFACE), // Communication interface is master, data interface is slave 0
+ D_ENDPOINT(USB_ENDPOINT_IN (CDC_ENDPOINT_ACM),USB_ENDPOINT_TYPE_INTERRUPT,0x10,0x40),
+
+ // CDC data interface
+ D_INTERFACE(CDC_DATA_INTERFACE,2,CDC_DATA_INTERFACE_CLASS,0,0),
+ D_ENDPOINT(USB_ENDPOINT_OUT(CDC_ENDPOINT_OUT),USB_ENDPOINT_TYPE_BULK,0x40,0),
+ D_ENDPOINT(USB_ENDPOINT_IN (CDC_ENDPOINT_IN ),USB_ENDPOINT_TYPE_BULK,0x40,0)
+};
+
+int WEAK CDC_GetInterface(u8* interfaceNum)
+{
+ interfaceNum[0] += 2; // uses 2
+ return USB_SendControl(TRANSFER_PGM,&_cdcInterface,sizeof(_cdcInterface));
+}
+
+bool WEAK CDC_Setup(Setup& setup)
+{
+ u8 r = setup.bRequest;
+ u8 requestType = setup.bmRequestType;
+
+ if (REQUEST_DEVICETOHOST_CLASS_INTERFACE == requestType)
+ {
+ if (CDC_GET_LINE_CODING == r)
+ {
+ USB_SendControl(0,(void*)&_usbLineInfo,7);
+ return true;
+ }
+ }
+
+ if (REQUEST_HOSTTODEVICE_CLASS_INTERFACE == requestType)
+ {
+ if (CDC_SET_LINE_CODING == r)
+ {
+ USB_RecvControl((void*)&_usbLineInfo,7);
+ if (300 == _usbLineInfo.dwDTERate) // auto-reset is triggered when avrdude opens the port at 1200 bps
+ Reboot();
+
+ return true;
+ }
+
+ if (CDC_SET_CONTROL_LINE_STATE == r)
+ {
+ _usbLineInfo.lineState = setup.wValueL;
+ return true;
+ }
+ }
+ return false;
+}
+
+
+int _serialPeek = -1;
+void Serial_::begin(uint16_t baud_count)
+{
+}
+
+void Serial_::end(void)
+{
+}
+
+int Serial_::available(void)
+{
+ u8 avail = USB_Available(CDC_RX);
+ if (_serialPeek != -1)
+ avail++;
+ return avail;
+}
+
+// peek is nasty
+int Serial_::peek(void)
+{
+ if (_serialPeek == -1)
+ _serialPeek = read();
+ return _serialPeek;
+}
+
+int Serial_::read(void)
+{
+ int c;
+ if (_serialPeek != -1)
+ {
+ c = _serialPeek;
+ _serialPeek = -1;
+ } else {
+ c = USB_Recv(CDC_RX);
+ }
+ return c;
+}
+
+void Serial_::flush(void)
+{
+ USB_Flush(CDC_TX);
+}
+
+size_t Serial_::write(uint8_t c)
+{
+ /* only try to send bytes if the high-level CDC connection itself
+ is open (not just the pipe) - the OS should set lineState when the port
+ is opened and clear lineState when the port is closed.
+ bytes sent before the user opens the connection or after
+ the connection is closed are lost - just like with a UART. */
+
+ // TODO - ZE - check behavior on different OSes and test what happens if an
+ // open connection isn't broken cleanly (cable is yanked out, host dies
+ // or locks up, or host virtual serial port hangs)
+ if (_usbLineInfo.lineState > 0) {
+ int r = USB_Send(CDC_TX,&c,1);
+ if (r > 0) {
+ return r;
+ } else {
+ setWriteError();
+ return 0;
+ }
+ }
+ setWriteError();
+ return 0;
+}
+
+Serial_ Serial;
+
+#endif
+#endif /* if defined(USBCON) */ \ No newline at end of file
diff --git a/cores/arduino/HID.cpp b/cores/arduino/HID.cpp
new file mode 100644
index 0000000..16d0448
--- /dev/null
+++ b/cores/arduino/HID.cpp
@@ -0,0 +1,439 @@
+
+
+/* Copyright (c) 2011, Peter Barrett
+**
+** Permission to use, copy, modify, and/or distribute this software for
+** any purpose with or without fee is hereby granted, provided that the
+** above copyright notice and this permission notice appear in all copies.
+**
+** THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL
+** WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED
+** WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR
+** BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES
+** OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
+** WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,
+** ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
+** SOFTWARE.
+*/
+
+#include "Platform.h"
+#include "USBAPI.h"
+#include "USBDesc.h"
+
+#if defined(USBCON)
+#ifdef HID_ENABLED
+
+//#define RAWHID_ENABLED
+
+// Singletons for mouse and keyboard
+
+Mouse_ Mouse;
+Keyboard_ Keyboard;
+
+//================================================================================
+//================================================================================
+
+// HID report descriptor
+
+#define LSB(_x) ((_x) & 0xFF)
+#define MSB(_x) ((_x) >> 8)
+
+#define RAWHID_USAGE_PAGE 0xFFC0
+#define RAWHID_USAGE 0x0C00
+#define RAWHID_TX_SIZE 64
+#define RAWHID_RX_SIZE 64
+
+extern const u8 _hidReportDescriptor[] PROGMEM;
+const u8 _hidReportDescriptor[] = {
+
+ // Mouse
+ 0x05, 0x01, // USAGE_PAGE (Generic Desktop) // 54
+ 0x09, 0x02, // USAGE (Mouse)
+ 0xa1, 0x01, // COLLECTION (Application)
+ 0x09, 0x01, // USAGE (Pointer)
+ 0xa1, 0x00, // COLLECTION (Physical)
+ 0x85, 0x01, // REPORT_ID (1)
+ 0x05, 0x09, // USAGE_PAGE (Button)
+ 0x19, 0x01, // USAGE_MINIMUM (Button 1)
+ 0x29, 0x03, // USAGE_MAXIMUM (Button 3)
+ 0x15, 0x00, // LOGICAL_MINIMUM (0)
+ 0x25, 0x01, // LOGICAL_MAXIMUM (1)
+ 0x95, 0x03, // REPORT_COUNT (3)
+ 0x75, 0x01, // REPORT_SIZE (1)
+ 0x81, 0x02, // INPUT (Data,Var,Abs)
+ 0x95, 0x01, // REPORT_COUNT (1)
+ 0x75, 0x05, // REPORT_SIZE (5)
+ 0x81, 0x03, // INPUT (Cnst,Var,Abs)
+ 0x05, 0x01, // USAGE_PAGE (Generic Desktop)
+ 0x09, 0x30, // USAGE (X)
+ 0x09, 0x31, // USAGE (Y)
+ 0x09, 0x38, // USAGE (Wheel)
+ 0x15, 0x81, // LOGICAL_MINIMUM (-127)
+ 0x25, 0x7f, // LOGICAL_MAXIMUM (127)
+ 0x75, 0x08, // REPORT_SIZE (8)
+ 0x95, 0x03, // REPORT_COUNT (3)
+ 0x81, 0x06, // INPUT (Data,Var,Rel)
+ 0xc0, // END_COLLECTION
+ 0xc0, // END_COLLECTION
+
+ // Keyboard
+ 0x05, 0x01, // USAGE_PAGE (Generic Desktop) // 47
+ 0x09, 0x06, // USAGE (Keyboard)
+ 0xa1, 0x01, // COLLECTION (Application)
+ 0x85, 0x02, // REPORT_ID (2)
+ 0x05, 0x07, // USAGE_PAGE (Keyboard)
+
+ 0x19, 0xe0, // USAGE_MINIMUM (Keyboard LeftControl)
+ 0x29, 0xe7, // USAGE_MAXIMUM (Keyboard Right GUI)
+ 0x15, 0x00, // LOGICAL_MINIMUM (0)
+ 0x25, 0x01, // LOGICAL_MAXIMUM (1)
+ 0x75, 0x01, // REPORT_SIZE (1)
+
+ 0x95, 0x08, // REPORT_COUNT (8)
+ 0x81, 0x02, // INPUT (Data,Var,Abs)
+ 0x95, 0x01, // REPORT_COUNT (1)
+ 0x75, 0x08, // REPORT_SIZE (8)
+ 0x81, 0x03, // INPUT (Cnst,Var,Abs)
+
+ 0x95, 0x06, // REPORT_COUNT (6)
+ 0x75, 0x08, // REPORT_SIZE (8)
+ 0x15, 0x00, // LOGICAL_MINIMUM (0)
+ 0x25, 0x65, // LOGICAL_MAXIMUM (101)
+ 0x05, 0x07, // USAGE_PAGE (Keyboard)
+
+ 0x19, 0x00, // USAGE_MINIMUM (Reserved (no event indicated))
+ 0x29, 0x65, // USAGE_MAXIMUM (Keyboard Application)
+ 0x81, 0x00, // INPUT (Data,Ary,Abs)
+ 0xc0, // END_COLLECTION
+
+#if RAWHID_ENABLED
+ // RAW HID
+ 0x06, LSB(RAWHID_USAGE_PAGE), MSB(RAWHID_USAGE_PAGE), // 30
+ 0x0A, LSB(RAWHID_USAGE), MSB(RAWHID_USAGE),
+
+ 0xA1, 0x01, // Collection 0x01
+ 0x85, 0x03, // REPORT_ID (3)
+ 0x75, 0x08, // report size = 8 bits
+ 0x15, 0x00, // logical minimum = 0
+ 0x26, 0xFF, 0x00, // logical maximum = 255
+
+ 0x95, 64, // report count TX
+ 0x09, 0x01, // usage
+ 0x81, 0x02, // Input (array)
+
+ 0x95, 64, // report count RX
+ 0x09, 0x02, // usage
+ 0x91, 0x02, // Output (array)
+ 0xC0 // end collection
+#endif
+};
+
+extern const HIDDescriptor _hidInterface PROGMEM;
+const HIDDescriptor _hidInterface =
+{
+ D_INTERFACE(HID_INTERFACE,1,3,0,0),
+ D_HIDREPORT(sizeof(_hidReportDescriptor)),
+ D_ENDPOINT(USB_ENDPOINT_IN (HID_ENDPOINT_INT),USB_ENDPOINT_TYPE_INTERRUPT,0x40,0x01)
+};
+
+//================================================================================
+//================================================================================
+// Driver
+
+u8 _hid_protocol = 1;
+u8 _hid_idle = 1;
+
+#define WEAK __attribute__ ((weak))
+#define WEAK
+
+int WEAK HID_GetInterface(u8* interfaceNum)
+{
+ interfaceNum[0] += 1; // uses 1
+ return USB_SendControl(TRANSFER_PGM,&_hidInterface,sizeof(_hidInterface));
+}
+
+int WEAK HID_GetDescriptor(int i)
+{
+ return USB_SendControl(TRANSFER_PGM,_hidReportDescriptor,sizeof(_hidReportDescriptor));
+}
+
+void WEAK HID_SendReport(u8 id, const void* data, int len)
+{
+ USB_Send(HID_TX, &id, 1);
+ USB_Send(HID_TX | TRANSFER_RELEASE,data,len);
+}
+
+bool WEAK HID_Setup(Setup& setup)
+{
+ u8 r = setup.bRequest;
+ u8 requestType = setup.bmRequestType;
+ if (REQUEST_DEVICETOHOST_CLASS_INTERFACE == requestType)
+ {
+ if (HID_GET_REPORT == r)
+ {
+ //HID_GetReport();
+ return true;
+ }
+ if (HID_GET_PROTOCOL == r)
+ {
+ //Send8(_hid_protocol); // TODO
+ return true;
+ }
+ }
+
+ if (REQUEST_HOSTTODEVICE_CLASS_INTERFACE == requestType)
+ {
+ if (HID_SET_PROTOCOL == r)
+ {
+ _hid_protocol = setup.wValueL;
+ return true;
+ }
+
+ if (HID_SET_IDLE == r)
+ {
+ _hid_idle = setup.wValueL;
+ return true;
+ }
+ }
+ return false;
+}
+
+//================================================================================
+//================================================================================
+// Mouse
+
+Mouse_::Mouse_() : _buttons(0)
+{
+}
+
+void Mouse_::click(uint8_t b)
+{
+ _buttons = b;
+ move(0,0,0);
+ _buttons = 0;
+ move(0,0,0);
+}
+
+void Mouse_::move(signed char x, signed char y, signed char wheel)
+{
+ u8 m[4];
+ m[0] = _buttons;
+ m[1] = x;
+ m[2] = y;
+ m[3] = wheel;
+ HID_SendReport(1,m,4);
+}
+
+void Mouse_::buttons(uint8_t b)
+{
+ if (b != _buttons)
+ {
+ _buttons = b;
+ move(0,0,0);
+ }
+}
+
+void Mouse_::press(uint8_t b)
+{
+ buttons(_buttons | b);
+}
+
+void Mouse_::release(uint8_t b)
+{
+ buttons(_buttons & ~b);
+}
+
+//================================================================================
+//================================================================================
+// Keyboard
+
+Keyboard_::Keyboard_() : _keyMap(0)
+{
+}
+
+void Keyboard_::sendReport(KeyReport* keys)
+{
+ HID_SendReport(2,keys,sizeof(KeyReport));
+}
+
+void Keyboard_::setKeyMap(KeyMap* keyMap)
+{
+ _keyMap = keyMap;
+}
+
+extern
+const uint8_t _asciimap[128] PROGMEM;
+
+#define SHIFT 0x80
+const uint8_t _asciimap[128] =
+{
+ 0x00, // NUL
+ 0x00, // SOH
+ 0x00, // STX
+ 0x00, // ETX
+ 0x00, // EOT
+ 0x00, // ENQ
+ 0x00, // ACK
+ 0x00, // BEL
+ 0x2a, // BS Backspace
+ 0x2b, // TAB Tab
+ 0x28, // LF Enter
+ 0x00, // VT
+ 0x00, // FF
+ 0x00, // CR
+ 0x00, // SO
+ 0x00, // SI
+ 0x00, // DEL
+ 0x00, // DC1
+ 0x00, // DC2
+ 0x00, // DC3
+ 0x00, // DC4
+ 0x00, // NAK
+ 0x00, // SYN
+ 0x00, // ETB
+ 0x00, // CAN
+ 0x00, // EM
+ 0x00, // SUB
+ 0x00, // ESC
+ 0x00, // FS
+ 0x00, // GS
+ 0x00, // RS
+ 0x00, // US
+
+ 0x2c, // ' '
+ 0x1e|SHIFT, // !
+ 0x34|SHIFT, // "
+ 0x20|SHIFT, // #
+ 0x21|SHIFT, // $
+ 0x22|SHIFT, // %
+ 0x24|SHIFT, // &
+ 0x34, // '
+ 0x26|SHIFT, // (
+ 0x27|SHIFT, // )
+ 0x25|SHIFT, // *
+ 0x2e|SHIFT, // +
+ 0x36, // ,
+ 0x2d, // -
+ 0x37, // .
+ 0x38, // /
+ 0x27, // 0
+ 0x1e, // 1
+ 0x1f, // 2
+ 0x20, // 3
+ 0x21, // 4
+ 0x22, // 5
+ 0x23, // 6
+ 0x24, // 7
+ 0x25, // 8
+ 0x26, // 9
+ 0x33|SHIFT, // :
+ 0x33, // ;
+ 0x36|SHIFT, // <
+ 0x2e, // =
+ 0x37|SHIFT, // >
+ 0x38|SHIFT, // ?
+ 0x1f|SHIFT, // @
+ 0x04|SHIFT, // A
+ 0x05|SHIFT, // B
+ 0x06|SHIFT, // C
+ 0x07|SHIFT, // D
+ 0x08|SHIFT, // E
+ 0x09|SHIFT, // F
+ 0x0a|SHIFT, // G
+ 0x0b|SHIFT, // H
+ 0x0c|SHIFT, // I
+ 0x0d|SHIFT, // J
+ 0x0e|SHIFT, // K
+ 0x0f|SHIFT, // L
+ 0x10|SHIFT, // M
+ 0x11|SHIFT, // N
+ 0x12|SHIFT, // O
+ 0x13|SHIFT, // P
+ 0x14|SHIFT, // Q
+ 0x15|SHIFT, // R
+ 0x16|SHIFT, // S
+ 0x17|SHIFT, // T
+ 0x18|SHIFT, // U
+ 0x19|SHIFT, // V
+ 0x1a|SHIFT, // W
+ 0x1b|SHIFT, // X
+ 0x1c|SHIFT, // Y
+ 0x1d|SHIFT, // Z
+ 0x2f, // [
+ 0x31, // bslash
+ 0x30, // ]
+ 0x23|SHIFT, // ^
+ 0x2d|SHIFT, // _
+ 0x35, // `
+ 0x04, // a
+ 0x05, // b
+ 0x06, // c
+ 0x07, // d
+ 0x08, // e
+ 0x09, // f
+ 0x0a, // g
+ 0x0b, // h
+ 0x0c, // i
+ 0x0d, // j
+ 0x0e, // k
+ 0x0f, // l
+ 0x10, // m
+ 0x11, // n
+ 0x12, // o
+ 0x13, // p
+ 0x14, // q
+ 0x15, // r
+ 0x16, // s
+ 0x17, // t
+ 0x18, // u
+ 0x19, // v
+ 0x1a, // w
+ 0x1b, // x
+ 0x1c, // y
+ 0x1d, // z
+ 0x2f|SHIFT, //
+ 0x31|SHIFT, // |
+ 0x30|SHIFT, // }
+ 0x35|SHIFT, // ~
+ 0 // DEL
+};
+
+uint8_t USBPutChar(uint8_t c);
+size_t Keyboard_::write(uint8_t c)
+{
+ // Keydown
+ {
+ KeyReport keys = {0};
+ if (_keyMap)
+ _keyMap->charToKey(c,&keys);
+ else
+ {
+ if (c >= 128) {
+ setWriteError();
+ return 0;
+ }
+ c = pgm_read_byte(_asciimap + c);
+ if (!c) {
+ setWriteError();
+ return 0;
+ }
+ if (c & 0x80)
+ {
+ keys.modifiers |= KEY_MODIFIER_LEFT_SHIFT;
+ c &= 0x7F;
+ }
+ keys.keys[0] = c;
+ }
+ sendReport(&keys);
+ }
+ // Keyup
+ {
+ KeyReport keys = {0};
+ sendReport(&keys);
+ }
+ return 1;
+}
+
+#endif
+
+#endif /* if defined(USBCON) */ \ No newline at end of file
diff --git a/cores/arduino/HardwareSerial.cpp b/cores/arduino/HardwareSerial.cpp
index 3ed8d07..a31b266 100644
--- a/cores/arduino/HardwareSerial.cpp
+++ b/cores/arduino/HardwareSerial.cpp
@@ -50,6 +50,10 @@ struct ring_buffer
volatile int tail;
};
+#if defined(USBCON)
+ ring_buffer rx_buffer = { { 0 }, 0, 0};
+ ring_buffer tx_buffer = { { 0 }, 0, 0};
+#endif
#if defined(UBRRH) || defined(UBRR0H)
ring_buffer rx_buffer = { { 0 }, 0, 0 };
ring_buffer tx_buffer = { { 0 }, 0, 0 };
@@ -81,6 +85,9 @@ inline void store_char(unsigned char c, ring_buffer *buffer)
}
}
+#if !defined(USART0_RX_vect) && defined(USART1_RX_vect)
+// do nothing - on the 32u4 the first USART is USART1
+#else
#if !defined(USART_RX_vect) && !defined(SIG_USART0_RECV) && \
!defined(SIG_UART0_RECV) && !defined(USART0_RX_vect) && \
!defined(SIG_UART_RECV)
@@ -113,6 +120,7 @@ inline void store_char(unsigned char c, ring_buffer *buffer)
serialEvent_flag = 1;
}
#endif
+#endif
#if defined(USART1_RX_vect)
void serialEvent1() __attribute__((weak));
@@ -159,6 +167,11 @@ inline void store_char(unsigned char c, ring_buffer *buffer)
#error SIG_USART3_RECV
#endif
+<<<<<<< HEAD
+#if !defined(USART0_UDRE_vect) && defined(USART1_UDRE_vect)
+// do nothing - on the 32u4 the first USART is USART1
+#else
+=======
void serialEventRun(void)
{
unsigned char flag, oldSREG;
@@ -197,6 +210,7 @@ void serialEventRun(void)
}
+>>>>>>> 0c92f230b5202b241de875a3baff9dfd1cf7a618
#if !defined(UART0_UDRE_vect) && !defined(UART_UDRE_vect) && !defined(USART0_UDRE_vect) && !defined(USART_UDRE_vect)
#error Don't know what the Data Register Empty vector is called for the first UART
#else
@@ -233,6 +247,7 @@ ISR(USART_UDRE_vect)
}
}
#endif
+#endif
#ifdef USART1_UDRE_vect
ISR(USART1_UDRE_vect)
@@ -422,7 +437,7 @@ size_t HardwareSerial::write(uint8_t c)
#elif defined(UBRR0H) && defined(UBRR0L)
HardwareSerial Serial(&rx_buffer, &tx_buffer, &UBRR0H, &UBRR0L, &UCSR0A, &UCSR0B, &UDR0, RXEN0, TXEN0, RXCIE0, UDRIE0, U2X0);
#elif defined(USBCON)
- #warning no serial port defined (port 0)
+ // do nothing - Serial object and buffers are initialized in CDC code
#else
#error no serial port defined (port 0)
#endif
diff --git a/cores/arduino/HardwareSerial.h b/cores/arduino/HardwareSerial.h
index cbb0e5e..d0dca06 100644
--- a/cores/arduino/HardwareSerial.h
+++ b/cores/arduino/HardwareSerial.h
@@ -62,7 +62,8 @@ class HardwareSerial : public Stream
#if defined(UBRRH) || defined(UBRR0H)
extern HardwareSerial Serial;
#elif defined(USBCON)
- #include "usb_api.h"
+ #include "USBAPI.h"
+// extern HardwareSerial Serial_;
#endif
#if defined(UBRR1H)
extern HardwareSerial Serial1;
diff --git a/cores/arduino/Platform.h b/cores/arduino/Platform.h
new file mode 100644
index 0000000..8b8f742
--- /dev/null
+++ b/cores/arduino/Platform.h
@@ -0,0 +1,23 @@
+
+#ifndef __PLATFORM_H__
+#define __PLATFORM_H__
+
+#include <inttypes.h>
+#include <avr/pgmspace.h>
+#include <avr/eeprom.h>
+#include <avr/interrupt.h>
+#include <util/delay.h>
+
+typedef unsigned char u8;
+typedef unsigned short u16;
+typedef unsigned long u32;
+
+#include "Arduino.h"
+
+#if defined(USBCON)
+ #include "USBDesc.h"
+ #include "USBCore.h"
+ #include "USBAPI.h"
+#endif /* if defined(USBCON) */
+
+#endif
diff --git a/cores/arduino/USBAPI.h b/cores/arduino/USBAPI.h
new file mode 100644
index 0000000..03dddb8
--- /dev/null
+++ b/cores/arduino/USBAPI.h
@@ -0,0 +1,163 @@
+
+
+#ifndef __USBAPI__
+#define __USBAPI__
+
+#if defined(USBCON)
+
+//================================================================================
+//================================================================================
+// USB
+
+class USB_
+{
+public:
+ USB_();
+ bool configured();
+
+ void attach();
+ void detach(); // Serial port goes down too...
+ void poll();
+};
+extern USB_ USB;
+
+//================================================================================
+//================================================================================
+// Serial over CDC (Serial1 is the physical port)
+
+class Serial_ : public Stream
+{
+public:
+ void begin(uint16_t baud_count);
+ void end(void);
+
+ virtual int available(void);
+ virtual int peek(void);
+ virtual int read(void);
+ virtual void flush(void);
+ virtual size_t write(uint8_t);
+};
+extern Serial_ Serial;
+
+//================================================================================
+//================================================================================
+// Mouse
+
+#define MOUSE_LEFT 1
+#define MOUSE_MIDDLE 2
+#define MOUSE_RIGHT 4
+#define MOUSE_ALL (MOUSE_LEFT | MOUSE_MIDDLE | MOUSE_RIGHT)
+
+class Mouse_
+{
+ uint8_t _buttons;
+public:
+ Mouse_();
+ void click(uint8_t b = MOUSE_LEFT);
+ void move(signed char x, signed char y, signed char wheel = 0);
+ void buttons(uint8_t b);
+ void press(uint8_t b = MOUSE_LEFT); // press LEFT by default
+ void release(uint8_t b = MOUSE_LEFT); // release LEFT by default
+};
+extern Mouse_ Mouse;
+
+//================================================================================
+//================================================================================
+// Keyboard
+
+#define KEY_MODIFIER_LEFT_CTRL 0x01
+#define KEY_MODIFIER_LEFT_SHIFT 0x02
+#define KEY_MODIFIER_LEFT_ALT 0x04
+#define KEY_MODIFIER_LEFT_GUI 0x08
+#define KEY_MODIFIER_RIGHT_CTRL 0x010
+#define KEY_MODIFIER_RIGHT_SHIFT 0x020
+#define KEY_MODIFIER_RIGHT_ALT 0x040
+#define KEY_MODIFIER_RIGHT_GUI 0x080
+
+// Low level key report: up to 6 keys and shift, ctrl etc at once
+typedef struct
+{
+ uint8_t modifiers;
+ uint8_t reserved;
+ uint8_t keys[6];
+} KeyReport;
+
+// Map a character into a key report
+// Called from Print to map text to keycodes
+class KeyMap
+{
+public:
+ virtual void charToKey(int c, KeyReport* keyReport) = 0;
+};
+
+//
+class Keyboard_ : public Print
+{
+ KeyMap* _keyMap;
+public:
+ Keyboard_();
+ void sendReport(KeyReport* keys);
+ void setKeyMap(KeyMap* keyMap);
+ virtual size_t write(uint8_t);
+};
+extern Keyboard_ Keyboard;
+
+//================================================================================
+//================================================================================
+// Low level API
+
+typedef struct
+{
+ uint8_t bmRequestType;
+ uint8_t bRequest;
+ uint8_t wValueL;
+ uint8_t wValueH;
+ uint16_t wIndex;
+ uint16_t wLength;
+} Setup;
+
+//================================================================================
+//================================================================================
+// HID 'Driver'
+
+int HID_GetInterface(uint8_t* interfaceNum);
+int HID_GetDescriptor(int i);
+bool HID_Setup(Setup& setup);
+void HID_SendReport(uint8_t id, const void* data, int len);
+
+//================================================================================
+//================================================================================
+// MSC 'Driver'
+
+int MSC_GetInterface(uint8_t* interfaceNum);
+int MSC_GetDescriptor(int i);
+bool MSC_Setup(Setup& setup);
+bool MSC_Data(uint8_t rx,uint8_t tx);
+
+//================================================================================
+//================================================================================
+// CSC 'Driver'
+
+int CDC_GetInterface(uint8_t* interfaceNum);
+int CDC_GetDescriptor(int i);
+bool CDC_Setup(Setup& setup);
+
+//================================================================================
+//================================================================================
+
+#define TRANSFER_PGM 0x80
+#define TRANSFER_RELEASE 0x40
+#define TRANSFER_ZERO 0x20
+
+int USB_SendControl(uint8_t flags, const void* d, int len);
+int USB_RecvControl(void* d, int len);
+
+uint8_t USB_Available(uint8_t ep);
+int USB_Send(uint8_t ep, const void* data, int len); // blocking
+int USB_Recv(uint8_t ep, void* data, int len); // non-blocking
+int USB_Recv(uint8_t ep); // non-blocking
+void USB_Flush(uint8_t ep);
+
+#endif
+
+#endif /* if defined(USBCON) */ \ No newline at end of file
diff --git a/cores/arduino/USBCore.cpp b/cores/arduino/USBCore.cpp
new file mode 100644
index 0000000..9ce2e00
--- /dev/null
+++ b/cores/arduino/USBCore.cpp
@@ -0,0 +1,655 @@
+
+
+/* Copyright (c) 2010, Peter Barrett
+**
+** Permission to use, copy, modify, and/or distribute this software for
+** any purpose with or without fee is hereby granted, provided that the
+** above copyright notice and this permission notice appear in all copies.
+**
+** THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL
+** WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED
+** WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR
+** BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES
+** OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
+** WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,
+** ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
+** SOFTWARE.
+*/
+
+#include "Platform.h"
+#include "USBAPI.h"
+#include "USBDesc.h"
+
+#if defined(USBCON)
+
+#define EP_TYPE_CONTROL 0x00
+#define EP_TYPE_BULK_IN 0x81
+#define EP_TYPE_BULK_OUT 0x80
+#define EP_TYPE_INTERRUPT_IN 0xC1
+#define EP_TYPE_INTERRUPT_OUT 0xC0
+#define EP_TYPE_ISOCHRONOUS_IN 0x41
+#define EP_TYPE_ISOCHRONOUS_OUT 0x40
+
+//==================================================================
+//==================================================================
+
+extern const u16 STRING_LANGUAGE[] PROGMEM;
+extern const u16 STRING_SERIAL[] PROGMEM;
+extern const DeviceDescriptor USB_DeviceDescriptor PROGMEM;
+extern const DeviceDescriptor USB_DeviceDescriptorA PROGMEM;
+
+const u16 STRING_LANGUAGE[2] = {
+ (3<<8) | (2+2),
+ 0x0409 // English
+};
+
+#if 0
+const u16 STRING_PRODUCT[] = {
+ (3<<8) | (2+2*10),
+ PRODUCT_NAME
+};
+#endif
+
+const u16 STRING_SERIAL[13] = {
+ (3<<8) | (2+2*12),
+ MSC_DISK_SERIAL
+};
+
+#ifdef CDC_ENABLED
+#define DEVICE_CLASS 0x02
+#else
+#define DEVICE_CLASS 0x00
+#endif
+
+// DEVICE DESCRIPTOR
+const DeviceDescriptor USB_DeviceDescriptor =
+ D_DEVICE(0x00,0x00,0x00,64,USB_VID,USB_PID,0x100,0,IPRODUCT,ISERIAL,1);
+
+const DeviceDescriptor USB_DeviceDescriptorA =
+ D_DEVICE(DEVICE_CLASS,0x00,0x00,64,USB_VID,USB_PID,0x100,0,IPRODUCT,ISERIAL,1);
+
+//==================================================================
+//==================================================================
+
+volatile u8 _usbConfiguration = 0;
+
+static inline void WaitIN(void)
+{
+ while (!(UEINTX & (1<<TXINI)));
+}
+
+static inline void ClearIN(void)
+{
+ UEINTX = ~(1<<TXINI);
+}
+
+static inline void WaitOUT(void)
+{
+ while (!(UEINTX & (1<<RXOUTI)))
+ ;
+}
+
+static inline u8 WaitForINOrOUT()
+{
+ while (!(UEINTX & ((1<<TXINI)|(1<<RXOUTI))))
+ ;
+ return (UEINTX & (1<<RXOUTI)) == 0;
+}
+
+static inline void ClearOUT(void)
+{
+ UEINTX = ~(1<<RXOUTI);
+}
+
+void Recv(volatile u8* data, u8 count)
+{
+ while (count--)
+ *data++ = UEDATX;
+}
+
+static inline u8 Recv8()
+{
+ return UEDATX;
+}
+
+static inline void Send8(u8 d)
+{
+ UEDATX = d;
+}
+
+static inline void SetEP(u8 ep)
+{
+ UENUM = ep;
+}
+
+static inline u8 FifoByteCount()
+{
+ return UEBCLX;
+}
+
+static inline u8 ReceivedSetupInt()
+{
+ return UEINTX & (1<<RXSTPI);
+}
+
+static inline void ClearSetupInt()
+{
+ UEINTX = ~((1<<RXSTPI) | (1<<RXOUTI) | (1<<TXINI));
+}
+
+static inline void Stall()
+{
+ UECONX = (1<<STALLRQ) | (1<<EPEN);
+}
+
+static inline u8 ReadWriteAllowed()
+{
+ return UEINTX & (1<<RWAL);
+}
+
+static inline u8 Stalled()
+{
+ return UEINTX & (1<<STALLEDI);
+}
+
+static inline u8 FifoFree()
+{
+ return UEINTX & (1<<FIFOCON);
+}
+
+static inline void ReleaseRX()
+{
+ UEINTX = 0x6B; // FIFOCON=0 NAKINI=1 RWAL=1 NAKOUTI=0 RXSTPI=1 RXOUTI=0 STALLEDI=1 TXINI=1
+}
+
+static inline void ReleaseTX()
+{
+ UEINTX = 0x3A; // FIFOCON=0 NAKINI=0 RWAL=1 NAKOUTI=1 RXSTPI=1 RXOUTI=0 STALLEDI=1 TXINI=0
+}
+
+static inline u8 FrameNumber()
+{
+ return UDFNUML;
+}
+
+//==================================================================
+//==================================================================
+
+u8 USBGetConfiguration(void)
+{
+ return _usbConfiguration;
+}
+
+#define USB_RECV_TIMEOUT
+class LockEP
+{
+ u8 _sreg;
+public:
+ LockEP(u8 ep) : _sreg(SREG)
+ {
+ cli();
+ SetEP(ep & 7);
+ }
+ ~LockEP()
+ {
+ SREG = _sreg;
+ }
+};
+
+// Number of bytes, assumes a rx endpoint
+u8 USB_Available(u8 ep)
+{
+ LockEP lock(ep);
+ return FifoByteCount();
+}
+
+// Non Blocking recieve
+// Return number of bytes read
+int USB_Recv(u8 ep, void* d, int len)
+{
+ if (!_usbConfiguration || len < 0)
+ return -1;
+
+ LockEP lock(ep);
+ u8 n = FifoByteCount();
+ len = min(n,len);
+ n = len;
+ u8* dst = (u8*)d;
+ while (n--)
+ *dst++ = Recv8();
+ if (len && !FifoByteCount()) // release empty buffer
+ ReleaseRX();
+
+ return len;
+}
+
+// Recv 1 byte if ready
+int USB_Recv(u8 ep)
+{
+ u8 c;
+ if (USB_Recv(ep,&c,1) != 1)
+ return -1;
+ return c;
+}
+
+// Space in send EP
+u8 USB_SendSpace(u8 ep)
+{
+ LockEP lock(ep);
+ if (!ReadWriteAllowed())
+ return 0;
+ return 64 - FifoByteCount();
+}
+
+// Blocking Send of data to an endpoint
+int USB_Send(u8 ep, const void* d, int len)
+{
+ if (!_usbConfiguration)
+ return -1;
+
+ int r = len;
+ const u8* data = (const u8*)d;
+ u8 zero = ep & TRANSFER_ZERO;
+ u8 timeout = 250; // 250ms timeout on send? TODO
+ while (len)
+ {
+ u8 n = USB_SendSpace(ep);
+ if (n == 0)
+ {
+ if (!(--timeout))
+ return -1;
+ delay(1);
+ continue;
+ }
+
+ if (n > len)
+ n = len;
+ len -= n;
+ {
+ LockEP lock(ep);
+ if (ep & TRANSFER_ZERO)
+ {
+ while (n--)
+ Send8(0);
+ }
+ else if (ep & TRANSFER_PGM)
+ {
+ while (n--)
+ Send8(pgm_read_byte(data++));
+ }
+ else
+ {
+ while (n--)
+ Send8(*data++);
+ }
+ if (!ReadWriteAllowed() || ((len == 0) && (ep & TRANSFER_RELEASE))) // Release full buffer
+ ReleaseTX();
+ }
+ }
+ return r;
+}
+
+extern const u8 _initEndpoints[] PROGMEM;
+const u8 _initEndpoints[] =
+{
+ 0,
+
+#ifdef CDC_ENABLED
+ EP_TYPE_INTERRUPT_IN, // CDC_ENDPOINT_ACM
+ EP_TYPE_BULK_OUT, // CDC_ENDPOINT_OUT
+ EP_TYPE_BULK_IN, // CDC_ENDPOINT_IN
+#endif
+
+#ifdef HID_ENABLED
+ EP_TYPE_INTERRUPT_IN // HID_ENDPOINT_INT
+#endif
+
+#ifdef MSC_ENABLED
+ EP_TYPE_BULK_OUT, // MSC_ENDPOINT_OUT
+ EP_TYPE_BULK_IN, // MSC_ENDPOINT_IN
+#endif
+};
+
+#define EP_SINGLE_64 0x32 // EP0
+#define EP_DOUBLE_64 0x36 // Other endpoints
+
+static
+void InitEP(u8 index, u8 type, u8 size)
+{
+ UENUM = index;
+ UECONX = 1;
+ UECFG0X = type;
+ UECFG1X = size;
+}
+
+static
+void InitEndpoints()
+{
+ for (u8 i = 1; i < sizeof(_initEndpoints); i++)
+ {
+ UENUM = i;
+ UECONX = 1;
+ UECFG0X = pgm_read_byte(_initEndpoints+i);
+ UECFG1X = EP_DOUBLE_64;
+ }
+ UERST = 0x7E; // And reset them
+ UERST = 0;
+}
+
+// Handle CLASS_INTERFACE requests
+static
+bool ClassInterfaceRequest(Setup& setup)
+{
+ u8 i = setup.wIndex;
+
+#ifdef CDC_ENABLED
+ if (CDC_ACM_INTERFACE == i)
+ return CDC_Setup(setup);
+#endif
+
+#ifdef MSC_ENABLED
+ if (MSC_INTERFACE == i)
+ return MSC_Setup(setup);
+#endif
+
+#ifdef HID_ENABLED
+ if (HID_INTERFACE == i)
+ return HID_Setup(setup);
+#endif
+ return false;
+}
+
+int _cmark;
+int _cend;
+void InitControl(int end)
+{
+ SetEP(0);
+ _cmark = 0;
+ _cend = end;
+}
+
+static
+bool SendControl(u8 d)
+{
+ if (_cmark < _cend)
+ {
+ if (!WaitForINOrOUT())
+ return false;
+ Send8(d);
+ if (!((_cmark + 1) & 0x3F))
+ ClearIN(); // Fifo is full, release this packet
+ }
+ _cmark++;
+ return true;
+};
+
+// Clipped by _cmark/_cend
+int USB_SendControl(u8 flags, const void* d, int len)
+{
+ int sent = len;
+ const u8* data = (const u8*)d;
+ bool pgm = flags & TRANSFER_PGM;
+ while (len--)
+ {
+ u8 c = pgm ? pgm_read_byte(data++) : *data++;
+ if (!SendControl(c))
+ return -1;
+ }
+ return sent;
+}
+
+// Does not timeout or cross fifo boundaries
+// Will only work for transfers <= 64 bytes
+// TODO
+int USB_RecvControl(void* d, int len)
+{
+ WaitOUT();
+ Recv((u8*)d,len);
+ ClearOUT();
+ return len;
+}
+
+int SendInterfaces()
+{
+ int total = 0;
+ u8 interfaces = 0;
+
+#ifdef CDC_ENABLED
+ total = CDC_GetInterface(&interfaces);
+#endif
+
+#ifdef HID_ENABLED
+ total += HID_GetInterface(&interfaces);
+#endif
+
+#ifdef MSC_ENABLED
+ total += MSC_GetInterface(&interfaces);
+#endif
+ return interfaces;
+}
+
+// Construct a dynamic configuration descriptor
+// This really needs dynamic endpoint allocation etc
+// TODO
+static
+bool SendConfiguration(int maxlen)
+{
+ // Count and measure interfaces
+ InitControl(0);
+ int interfaces = SendInterfaces();
+ ConfigDescriptor config = D_CONFIG(_cmark + sizeof(ConfigDescriptor),interfaces);
+
+ // Now send them
+ InitControl(maxlen);
+ USB_SendControl(0,&config,sizeof(ConfigDescriptor));
+ SendInterfaces();
+ return true;
+}
+
+u8 _cdcComposite = 0;
+
+static
+bool SendDescriptor(Setup& setup)
+{
+ u8 t = setup.wValueH;
+ if (USB_CONFIGURATION_DESCRIPTOR_TYPE == t)
+ return SendConfiguration(setup.wLength);
+
+ InitControl(setup.wLength);
+#ifdef HID_ENABLED
+ if (HID_REPORT_DESCRIPTOR_TYPE == t)
+ return HID_GetDescriptor(t);
+#endif
+
+ u8 desc_length = 0;
+ const u8* desc_addr = 0;
+ if (USB_DEVICE_DESCRIPTOR_TYPE == t)
+ {
+ if (setup.wLength == 8)
+ _cdcComposite = 1;
+ desc_addr = _cdcComposite ? (const u8*)&USB_DeviceDescriptorA : (const u8*)&USB_DeviceDescriptor;
+ }
+ else if (USB_STRING_DESCRIPTOR_TYPE == t)
+ {
+ if (setup.wValueL == 0)
+ desc_addr = (const u8*)&STRING_LANGUAGE;
+ if (setup.wValueL == ISERIAL)
+ desc_addr = (const u8*)&STRING_SERIAL;
+ }
+
+ if (desc_addr == 0)
+ return false;
+ if (desc_length == 0)
+ desc_length = pgm_read_byte(desc_addr);
+
+ USB_SendControl(TRANSFER_PGM,desc_addr,desc_length);
+ return true;
+}
+
+// Endpoint 0 interrupt
+ISR(USB_COM_vect)
+{
+ SetEP(0);
+ if (!ReceivedSetupInt())
+ return;
+
+ Setup setup;
+ Recv((u8*)&setup,8);
+ ClearSetupInt();
+
+ //printHex((u8*)&setup,8);
+
+ u8 requestType = setup.bmRequestType;
+ if (requestType & REQUEST_DEVICETOHOST)
+ WaitIN();
+ else
+ ClearIN();
+
+ bool ok = true;
+ if (REQUEST_STANDARD == (requestType & REQUEST_TYPE))
+ {
+ // Standard Requests
+ u8 r = setup.bRequest;
+ if (GET_STATUS == r)
+ {
+ Send8(0); // TODO
+ Send8(0);
+ }
+ else if (CLEAR_FEATURE == r)
+ {
+ }
+ else if (SET_FEATURE == r)
+ {
+ }
+ else if (SET_ADDRESS == r)
+ {
+ WaitIN();
+ UDADDR = setup.wValueL | (1<<ADDEN);
+ }
+ else if (GET_DESCRIPTOR == r)
+ {
+ ok = SendDescriptor(setup);
+ }
+ else if (SET_DESCRIPTOR == r)
+ {
+ ok = false;
+ }
+ else if (GET_CONFIGURATION == r)
+ {
+ Send8(1);
+ }
+ else if (SET_CONFIGURATION == r)
+ {
+ if (REQUEST_DEVICE == (requestType & REQUEST_RECIPIENT))
+ {
+ InitEndpoints();
+ _usbConfiguration = setup.wValueL;
+ } else
+ ok = false;
+ }
+ else if (GET_INTERFACE == r)
+ {
+ }
+ else if (SET_INTERFACE == r)
+ {
+ }
+ }
+ else
+ {
+ InitControl(setup.wLength); // Max length of transfer
+ ok = ClassInterfaceRequest(setup);
+ }
+
+ if (ok)
+ ClearIN();
+ else
+ {
+ Stall();
+ }
+}
+
+void USB_Flush(u8 ep)
+{
+ SetEP(ep);
+ if (FifoByteCount())
+ ReleaseTX();
+}
+
+// General interrupt
+ISR(USB_GEN_vect)
+{
+ u8 udint = UDINT;
+ UDINT = 0;
+
+ // End of Reset
+ if (udint & (1<<EORSTI))
+ {
+ InitEP(0,EP_TYPE_CONTROL,EP_SINGLE_64); // init ep0
+ _usbConfiguration = 0; // not configured yet
+ UEIENX = 1 << RXSTPE; // Enable interrupts for ep0
+ }
+
+ // Start of Frame
+ if (udint & (1<<SOFI))
+ {
+#ifdef CDC_ENABLED
+ USB_Flush(CDC_TX); // Send a tx frame if found
+#endif
+ }
+}
+
+// VBUS or counting frames
+// Any frame counting?
+u8 USBConnected()
+{
+ u8 f = UDFNUML;
+ delay(3);
+ return f != UDFNUML;
+}
+
+//=======================================================================
+//=======================================================================
+
+USB_ USB;
+
+USB_::USB_()
+{
+}
+
+void USB_::attach()
+{
+ _usbConfiguration = 0;
+ UHWCON = 0x01; // power internal reg
+ USBCON = (1<<USBE)|(1<<FRZCLK); // clock frozen, usb enabled
+ PLLCSR = 0x12; // Need 16 MHz xtal
+ while (!(PLLCSR & (1<<PLOCK))) // wait for lock pll
+ ;
+ USBCON = ((1<<USBE)|(1<<OTGPADE)); // start USB clock
+ UDIEN = (1<<EORSTE)|(1<<SOFE); // Enable interrupts for EOR (End of Reset) and SOF (start of frame)
+ UDCON = 0; // enable attach resistor
+}
+
+void USB_::detach()
+{
+}
+
+// Check for interrupts
+// TODO: VBUS detection
+bool USB_::configured()
+{
+ return _usbConfiguration;
+}
+
+void USB_::poll()
+{
+#ifdef MSC_ENABLED
+ if (!_usbConfiguration)
+ return;
+
+ // Service disk
+ if (USB_Available(MSC_RX))
+ MSC_Data(MSC_RX,MSC_TX);
+#endif
+}
+
+#endif /* if defined(USBCON) */ \ No newline at end of file
diff --git a/cores/arduino/USBCore.h b/cores/arduino/USBCore.h
new file mode 100644
index 0000000..8d13806
--- /dev/null
+++ b/cores/arduino/USBCore.h
@@ -0,0 +1,303 @@
+
+// Copyright (c) 2010, Peter Barrett
+/*
+** Permission to use, copy, modify, and/or distribute this software for
+** any purpose with or without fee is hereby granted, provided that the
+** above copyright notice and this permission notice appear in all copies.
+**
+** THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL
+** WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED
+** WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR
+** BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES
+** OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
+** WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,
+** ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
+** SOFTWARE.
+*/
+
+#ifndef __USBCORE_H__
+#define __USBCORE_H__
+
+// Standard requests
+#define GET_STATUS 0
+#define CLEAR_FEATURE 1
+#define SET_FEATURE 3
+#define SET_ADDRESS 5
+#define GET_DESCRIPTOR 6
+#define SET_DESCRIPTOR 7
+#define GET_CONFIGURATION 8
+#define SET_CONFIGURATION 9
+#define GET_INTERFACE 10
+#define SET_INTERFACE 11
+
+
+// bmRequestType
+#define REQUEST_HOSTTODEVICE 0x00
+#define REQUEST_DEVICETOHOST 0x80
+#define REQUEST_DIRECTION 0x80
+
+#define REQUEST_STANDARD 0x00
+#define REQUEST_CLASS 0x20
+#define REQUEST_VENDOR 0x40
+#define REQUEST_TYPE 0x60
+
+#define REQUEST_DEVICE 0x00
+#define REQUEST_INTERFACE 0x01
+#define REQUEST_ENDPOINT 0x02
+#define REQUEST_OTHER 0x03
+#define REQUEST_RECIPIENT 0x03
+
+#define REQUEST_DEVICETOHOST_CLASS_INTERFACE (REQUEST_DEVICETOHOST + REQUEST_CLASS + REQUEST_INTERFACE)
+#define REQUEST_HOSTTODEVICE_CLASS_INTERFACE (REQUEST_HOSTTODEVICE + REQUEST_CLASS + REQUEST_INTERFACE)
+
+// Class requests
+
+#define CDC_SET_LINE_CODING 0x20
+#define CDC_GET_LINE_CODING 0x21
+#define CDC_SET_CONTROL_LINE_STATE 0x22
+
+#define MSC_RESET 0xFF
+#define MSC_GET_MAX_LUN 0xFE
+
+#define HID_GET_REPORT 0x01
+#define HID_GET_IDLE 0x02
+#define HID_GET_PROTOCOL 0x03
+#define HID_SET_REPORT 0x09
+#define HID_SET_IDLE 0x0A
+#define HID_SET_PROTOCOL 0x0B
+
+// Descriptors
+
+#define USB_DEVICE_DESC_SIZE 18
+#define USB_CONFIGUARTION_DESC_SIZE 9
+#define USB_INTERFACE_DESC_SIZE 9
+#define USB_ENDPOINT_DESC_SIZE 7
+
+#define USB_DEVICE_DESCRIPTOR_TYPE 1
+#define USB_CONFIGURATION_DESCRIPTOR_TYPE 2
+#define USB_STRING_DESCRIPTOR_TYPE 3
+#define USB_INTERFACE_DESCRIPTOR_TYPE 4
+#define USB_ENDPOINT_DESCRIPTOR_TYPE 5
+
+#define USB_DEVICE_CLASS_COMMUNICATIONS 0x02
+#define USB_DEVICE_CLASS_HUMAN_INTERFACE 0x03
+#define USB_DEVICE_CLASS_STORAGE 0x08
+#define USB_DEVICE_CLASS_VENDOR_SPECIFIC 0xFF
+
+#define USB_CONFIG_POWERED_MASK 0x40
+#define USB_CONFIG_BUS_POWERED 0x80
+#define USB_CONFIG_SELF_POWERED 0xC0
+#define USB_CONFIG_REMOTE_WAKEUP 0x20
+
+// bMaxPower in Configuration Descriptor
+#define USB_CONFIG_POWER_MA(mA) ((mA)/2)
+
+// bEndpointAddress in Endpoint Descriptor
+#define USB_ENDPOINT_DIRECTION_MASK 0x80
+#define USB_ENDPOINT_OUT(addr) ((addr) | 0x00)
+#define USB_ENDPOINT_IN(addr) ((addr) | 0x80)
+
+#define USB_ENDPOINT_TYPE_MASK 0x03
+#define USB_ENDPOINT_TYPE_CONTROL 0x00
+#define USB_ENDPOINT_TYPE_ISOCHRONOUS 0x01
+#define USB_ENDPOINT_TYPE_BULK 0x02
+#define USB_ENDPOINT_TYPE_INTERRUPT 0x03
+
+#define TOBYTES(x) ((x) & 0xFF),(((x) >> 8) & 0xFF)
+
+#define CDC_V1_10 0x0110
+#define CDC_COMMUNICATION_INTERFACE_CLASS 0x02
+
+#define CDC_CALL_MANAGEMENT 0x01
+#define CDC_ABSTRACT_CONTROL_MODEL 0x02
+#define CDC_HEADER 0x00
+#define CDC_ABSTRACT_CONTROL_MANAGEMENT 0x02
+#define CDC_UNION 0x06
+#define CDC_CS_INTERFACE 0x24
+#define CDC_CS_ENDPOINT 0x25
+#define CDC_DATA_INTERFACE_CLASS 0x0A
+
+#define MSC_SUBCLASS_SCSI 0x06
+#define MSC_PROTOCOL_BULK_ONLY 0x50
+
+#define HID_HID_DESCRIPTOR_TYPE 0x21
+#define HID_REPORT_DESCRIPTOR_TYPE 0x22
+#define HID_PHYSICAL_DESCRIPTOR_TYPE 0x23
+
+
+// Device
+typedef struct {
+ u8 len; // 18
+ u8 dtype; // 1 USB_DEVICE_DESCRIPTOR_TYPE
+ u16 usbVersion; // 0x200
+ u8 deviceClass;
+ u8 deviceSubClass;
+ u8 deviceProtocol;
+ u8 packetSize0; // Packet 0
+ u16 idVendor;
+ u16 idProduct;
+ u16 deviceVersion; // 0x100
+ u8 iManufacturer;
+ u8 iProduct;
+ u8 iSerialNumber;
+ u8 bNumConfigurations;
+} DeviceDescriptor;
+
+// Config
+typedef struct {
+ u8 len; // 9
+ u8 dtype; // 2
+ u16 clen; // total length
+ u8 numInterfaces;
+ u8 config;
+ u8 iconfig;
+ u8 attributes;
+ u8 maxPower;
+} ConfigDescriptor;
+
+// String
+
+// Interface
+typedef struct
+{
+ u8 len; // 9
+ u8 dtype; // 4
+ u8 number;
+ u8 alternate;
+ u8 numEndpoints;
+ u8 interfaceClass;
+ u8 interfaceSubClass;
+ u8 protocol;
+ u8 iInterface;
+} InterfaceDescriptor;
+
+// Endpoint
+typedef struct
+{
+ u8 len; // 7
+ u8 dtype; // 5
+ u8 addr;
+ u8 attr;
+ u16 packetSize;
+ u8 interval;
+} EndpointDescriptor;
+
+// Interface Association Descriptor
+// Used to bind 2 interfaces together in CDC compostite device
+typedef struct
+{
+ u8 len; // 8
+ u8 dtype; // 11
+ u8 firstInterface;
+ u8 interfaceCount;
+ u8 functionClass;
+ u8 funtionSubClass;
+ u8 functionProtocol;
+ u8 iInterface;
+} IADDescriptor;
+
+// CDC CS interface descriptor
+typedef struct
+{
+ u8 len; // 5
+ u8 dtype; // 0x24
+ u8 subtype;
+ u8 d0;
+ u8 d1;
+} CDCCSInterfaceDescriptor;
+
+typedef struct
+{
+ u8 len; // 4
+ u8 dtype; // 0x24
+ u8 subtype;
+ u8 d0;
+} CDCCSInterfaceDescriptor4;
+
+typedef struct
+{
+ u8 len;
+ u8 dtype; // 0x24
+ u8 subtype; // 1
+ u8 bmCapabilities;
+ u8 bDataInterface;
+} CMFunctionalDescriptor;
+
+typedef struct
+{
+ u8 len;
+ u8 dtype; // 0x24
+ u8 subtype; // 1
+ u8 bmCapabilities;
+} ACMFunctionalDescriptor;
+
+typedef struct
+{
+ // IAD
+ IADDescriptor iad; // Only needed on compound device
+
+ // Control
+ InterfaceDescriptor cif; //
+ CDCCSInterfaceDescriptor header;
+ CMFunctionalDescriptor callManagement; // Call Management
+ ACMFunctionalDescriptor controlManagement; // ACM
+ CDCCSInterfaceDescriptor functionalDescriptor; // CDC_UNION
+ EndpointDescriptor cifin;
+
+ // Data
+ InterfaceDescriptor dif;
+ EndpointDescriptor in;
+ EndpointDescriptor out;
+} CDCDescriptor;
+
+typedef struct
+{
+ InterfaceDescriptor msc;
+ EndpointDescriptor in;
+ EndpointDescriptor out;
+} MSCDescriptor;
+
+typedef struct
+{
+ u8 len; // 9
+ u8 dtype; // 0x21
+ u8 addr;
+ u8 versionL; // 0x101
+ u8 versionH; // 0x101
+ u8 country;
+ u8 desctype; // 0x22 report
+ u8 descLenL;
+ u8 descLenH;
+} HIDDescDescriptor;
+
+typedef struct
+{
+ InterfaceDescriptor hid;
+ HIDDescDescriptor desc;
+ EndpointDescriptor in;
+} HIDDescriptor;
+
+
+#define D_DEVICE(_class,_subClass,_proto,_packetSize0,_vid,_pid,_version,_im,_ip,_is,_configs) \
+ { 18, 1, 0x200, _class,_subClass,_proto,_packetSize0,_vid,_pid,_version,_im,_ip,_is,_configs }
+
+#define D_CONFIG(_totalLength,_interfaces) \
+ { 9, 2, _totalLength,_interfaces, 1, 0, USB_CONFIG_BUS_POWERED, USB_CONFIG_POWER_MA(500) }
+
+#define D_INTERFACE(_n,_numEndpoints,_class,_subClass,_protocol) \
+ { 9, 4, _n, 0, _numEndpoints, _class,_subClass, _protocol, 0 }
+
+#define D_ENDPOINT(_addr,_attr,_packetSize, _interval) \
+ { 7, 5, _addr,_attr,_packetSize, _interval }
+
+#define D_IAD(_firstInterface, _count, _class, _subClass, _protocol) \
+ { 8, 11, _firstInterface, _count, _class, _subClass, _protocol, 0 }
+
+#define D_HIDREPORT(_descriptorLength) \
+ { 9, 0x21, 0x1, 0x1, 0, 1, 0x22, _descriptorLength, 0 }
+
+#define D_CDCCS(_subtype,_d0,_d1) { 5, 0x24, _subtype, _d0, _d1 }
+#define D_CDCCS4(_subtype,_d0) { 4, 0x24, _subtype, _d0 }
+
+
+#endif \ No newline at end of file
diff --git a/cores/arduino/USBDesc.h b/cores/arduino/USBDesc.h
new file mode 100644
index 0000000..2ad7725
--- /dev/null
+++ b/cores/arduino/USBDesc.h
@@ -0,0 +1,107 @@
+
+
+/* Copyright (c) 2011, Peter Barrett
+**
+** Permission to use, copy, modify, and/or distribute this software for
+** any purpose with or without fee is hereby granted, provided that the
+** above copyright notice and this permission notice appear in all copies.
+**
+** THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL
+** WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED
+** WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR
+** BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES
+** OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
+** WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,
+** ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
+** SOFTWARE.
+*/
+
+#define CDC_ENABLED
+#define HID_ENABLED
+//#define MSC_ENABLED
+
+
+#ifdef CDC_ENABLED
+#define CDC_INTERFACE_COUNT 2
+#define CDC_ENPOINT_COUNT 3
+#else
+#define CDC_INTERFACE_COUNT 0
+#define CDC_ENPOINT_COUNT 0
+#endif
+
+#ifdef HID_ENABLED
+#define HID_INTERFACE_COUNT 1
+#define HID_ENPOINT_COUNT 1
+#else
+#define HID_INTERFACE_COUNT 0
+#define HID_ENPOINT_COUNT 0
+#endif
+
+#ifdef MSC_ENABLED
+#define MSC_INTERFACE_COUNT 1
+#define MSC_ENPOINT_COUNT 2
+#else
+#define MSC_INTERFACE_COUNT 0
+#define MSC_ENPOINT_COUNT 0
+#endif
+
+#define CDC_ACM_INTERFACE 0 // CDC ACM
+#define CDC_DATA_INTERFACE 1 // CDC Data
+#define CDC_FIRST_ENDPOINT 1
+#define CDC_ENDPOINT_ACM (CDC_FIRST_ENDPOINT) // CDC First
+#define CDC_ENDPOINT_OUT (CDC_FIRST_ENDPOINT+1)
+#define CDC_ENDPOINT_IN (CDC_FIRST_ENDPOINT+2)
+
+#define HID_INTERFACE (CDC_ACM_INTERFACE + CDC_INTERFACE_COUNT) // HID Interface
+#define HID_FIRST_ENDPOINT (CDC_FIRST_ENDPOINT + CDC_ENPOINT_COUNT)
+#define HID_ENDPOINT_INT (HID_FIRST_ENDPOINT)
+
+#define MSC_INTERFACE (HID_INTERFACE + HID_INTERFACE_COUNT) // MSC Interface
+#define MSC_FIRST_ENDPOINT (HID_FIRST_ENDPOINT + HID_ENPOINT_COUNT)
+#define MSC_ENDPOINT_OUT (MSC_FIRST_ENDPOINT)
+#define MSC_ENDPOINT_IN (MSC_FIRST_ENDPOINT+1)
+
+#define INTERFACE_COUNT (MSC_INTERFACE + MSC_INTERFACE_COUNT)
+
+#ifdef CDC_ENABLED
+#define CDC_RX CDC_ENDPOINT_OUT
+#define CDC_TX CDC_ENDPOINT_IN
+#endif
+
+#ifdef HID_ENABLED
+#define HID_TX HID_ENDPOINT_INT
+#endif
+
+#ifdef MSC_ENABLED
+#define MSC_RX MSC_ENDPOINT_OUT
+#define MSC_TX MSC_ENDPOINT_IN
+#endif
+
+
+#define IMANUFACTURER 0
+#define IPRODUCT 0
+#define ISERIAL 1 // Only need this for MSC
+
+
+#define WRITABLE_DIRECTORY // undef saved 56 + 512 RAM
+
+#define FAT_DISK_LABEL 'b','o','o','t','l','o','a','d','e','r',' ' // 11 chars (undef saves 12)
+#define FAT_FILE_NAME 'F','I','R','M','W','A','R','E','B','I','N' // 11 chars
+#define MSC_DISK_SERIAL '0','0','0','0','0','0','0','0','1','7','0','1' // 12 chars
+
+#ifdef MICROTOUCH
+
+#define USB_VID 0x239A // Adafruit vid
+#define USB_PID 0x2001 // sorry dave sprach zarathustra
+#define FAT_OEM_NAME 'r','o','s','s','u','m','u','r' // 8 chars
+#define PRODUCT_NAME 'M','i','c','r','o','t','o','u','c','h'
+
+#else
+
+#define USB_VID 0x2341 // arduino LLC vid
+#define USB_PID 0x0020 // educated guess
+#define FAT_OEM_NAME 'l','e','o','n','a','r','d','o' // 8 chars
+#define PRODUCT_NAME 'A','r','d','u','i','n','o','l','l','c' // 10
+
+
+#endif \ No newline at end of file
diff --git a/cores/arduino/main.cpp b/cores/arduino/main.cpp
index 1c2ea9a..097f5ed 100755
--- a/cores/arduino/main.cpp
+++ b/cores/arduino/main.cpp
@@ -5,6 +5,10 @@ int main(void)
{
init();
+#if defined(USBCON)
+ USB.attach();
+#endif
+
setup();
for (;;) {
diff --git a/pins/leonardo/pins_arduino.h b/pins/leonardo/pins_arduino.h
new file mode 100644
index 0000000..79826f0
--- /dev/null
+++ b/pins/leonardo/pins_arduino.h
@@ -0,0 +1,231 @@
+/*
+ pins_arduino.h - Pin definition functions for Arduino
+ Part of Arduino - http://www.arduino.cc/
+
+ Copyright (c) 2007 David A. Mellis
+
+ This library is free software; you can redistribute it and/or
+ modify it under the terms of the GNU Lesser General Public
+ License as published by the Free Software Foundation; either
+ version 2.1 of the License, or (at your option) any later version.
+
+ This library 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
+ Lesser General Public License for more details.
+
+ You should have received a copy of the GNU Lesser General
+ Public License along with this library; if not, write to the
+ Free Software Foundation, Inc., 59 Temple Place, Suite 330,
+ Boston, MA 02111-1307 USA
+
+ $Id: wiring.h 249 2007-02-03 16:52:51Z mellis $
+*/
+
+#ifndef Pins_Arduino_h
+#define Pins_Arduino_h
+
+#include <avr/pgmspace.h>
+
+// Map SPI port to 'new' pins D14..D17
+// D14 PB0 RXLED,SS/PCINT0
+// D15 PB1 SCK,PCINT1
+// D16 PB2 MOSI,PCINT2
+// D17 PB3 MISO,PCINT3
+const static uint8_t SS = 14;
+const static uint8_t MOSI = 16;
+const static uint8_t MISO = 17;
+const static uint8_t SCK = 15;
+
+const static uint8_t A0 = 14;
+const static uint8_t A1 = 15;
+const static uint8_t A2 = 16;
+const static uint8_t A3 = 17;
+const static uint8_t A4 = 18;
+const static uint8_t A5 = 19;
+const static uint8_t A6 = 20;
+const static uint8_t A7 = 21;
+
+#ifdef ARDUINO_MAIN
+
+// On the Arduino board, digital pins are also used
+// for the analog output (software PWM). Analog input
+// pins are a separate set.
+
+// ATMEL ATMEGA8 & 168 / ARDUINO
+//
+// +-\/-+
+// PC6 1| |28 PC5 (AI 5)
+// (D 0) PD0 2| |27 PC4 (AI 4)
+// (D 1) PD1 3| |26 PC3 (AI 3)
+// (D 2) PD2 4| |25 PC2 (AI 2)
+// PWM+ (D 3) PD3 5| |24 PC1 (AI 1)
+// (D 4) PD4 6| |23 PC0 (AI 0)
+// VCC 7| |22 GND
+// GND 8| |21 AREF
+// PB6 9| |20 AVCC
+// PB7 10| |19 PB5 (D 13)
+// PWM+ (D 5) PD5 11| |18 PB4 (D 12)
+// PWM+ (D 6) PD6 12| |17 PB3 (D 11) PWM
+// (D 7) PD7 13| |16 PB2 (D 10) PWM
+// (D 8) PB0 14| |15 PB1 (D 9) PWM
+// +----+
+//
+// (PWM+ indicates the additional PWM pins on the ATmega168.)
+
+// ATMEL ATMEGA1280 / ARDUINO
+//
+// 0-7 PE0-PE7 works
+// 8-13 PB0-PB5 works
+// 14-21 PA0-PA7 works
+// 22-29 PH0-PH7 works
+// 30-35 PG5-PG0 works
+// 36-43 PC7-PC0 works
+// 44-51 PJ7-PJ0 works
+// 52-59 PL7-PL0 works
+// 60-67 PD7-PD0 works
+// A0-A7 PF0-PF7
+// A8-A15 PK0-PK7
+
+// ATMEL ATMEGA32U4 / ARDUINO LEONARDO
+//
+// D0 PD2 RXD1/INT2
+// D1 PD3 TXD1/INT3
+// D2 PD1 SDA SDA/INT1
+// D3# PD0 PWM8/SCL OC0B/SCL/INT0
+// D4 A6 PD4 ADC8
+// D5# A7 PD6 FastPWM OC4D/ADC9
+// D6# A8 PD7 FastPWM #OC4D/ADC10
+// D7 PD5 #CTS
+//
+// D8 PB4 ADC11/PCINT4
+// D9# A9 PB5 PWM16 OC1A/#OC4B/ADC13/PCINT5
+// D10# A10 PB6 PWM16 OC1B/0c4B/ADC12/PCINT6
+// D11# PB7 PWM8/16 0C0A/OC1C/#RTS/PCINT7
+// D12# PC6 PWM16 0C3A/#OC4A
+// D13# PC7 PWM10 CLK0/OC4A
+//
+// A0 PF7 ADC7
+// A1 PF6 ADC6
+// A2 PF5 ADC5
+// A3 PF4 ADC4
+// A4 PF1 ADC1
+// A5 PF0 ADC0
+//
+// New pins D14..D17 to map SPI port to digitial pins
+//
+// D14 PB0 RXLED,SS/PCINT0
+// D15 PB1 SCK,PCINT1
+// D16 PB2 MOSI,PCINT2
+// D17 PB3 MISO,PCINT3
+//
+// TXLED PE6 INT6
+// HWB PE2 HWB
+
+
+// these arrays map port names (e.g. port B) to the
+// appropriate addresses for various functions (e.g. reading
+// and writing)
+const uint16_t PROGMEM port_to_mode_PGM[] = {
+ NOT_A_PORT,
+ NOT_A_PORT,
+ (uint16_t) &DDRB,
+ (uint16_t) &DDRC,
+ (uint16_t) &DDRD,
+ (uint16_t) &DDRE,
+ (uint16_t) &DDRF,
+};
+
+const uint16_t PROGMEM port_to_output_PGM[] = {
+ NOT_A_PORT,
+ NOT_A_PORT,
+ (uint16_t) &PORTB,
+ (uint16_t) &PORTC,
+ (uint16_t) &PORTD,
+ (uint16_t) &PORTE,
+ (uint16_t) &PORTF,
+};
+
+const uint16_t PROGMEM port_to_input_PGM[] = {
+ NOT_A_PORT,
+ NOT_A_PORT,
+ (uint16_t) &PINB,
+ (uint16_t) &PINC,
+ (uint16_t) &PIND,
+ (uint16_t) &PINE,
+ (uint16_t) &PINF,
+};
+
+const uint8_t PROGMEM digital_pin_to_port_PGM[18] = {
+ PD, /* 0 */
+ PD,
+ PD,
+ PD,
+ PD,
+ PD,
+ PD,
+ PD,
+
+ PB, /* 8 */
+ PB,
+ PB,
+ PB,
+
+ PC, /* 12 */
+ PC, /* 13 */
+
+ PB, /* 14 */
+ PB, /* 15 */
+ PB, /* 16 */
+ PB, /* 17 */
+};
+
+const uint8_t PROGMEM digital_pin_to_bit_mask_PGM[18] = {
+ _BV(2), /* 0, port D */
+ _BV(3),
+ _BV(1),
+ _BV(0),
+ _BV(4),
+ _BV(6),
+ _BV(7),
+ _BV(5),
+
+ _BV(4), /* 8, port B */
+ _BV(5),
+ _BV(6),
+ _BV(7),
+
+ _BV(6), /* 12 port C */
+ _BV(7),
+
+ _BV(0), /* 14, port B */
+ _BV(1),
+ _BV(2),
+ _BV(3),
+};
+
+const uint8_t PROGMEM digital_pin_to_timer_PGM[18] = {
+ NOT_ON_TIMER, /* 0, port D */
+ NOT_ON_TIMER,
+ NOT_ON_TIMER,
+ TIMER0B,
+ NOT_ON_TIMER,
+ NOT_ON_TIMER, // TIMER4D TODO - ZE - Fix this when there's a decision on what to do about these PWM pins
+ NOT_ON_TIMER, // TIMER4D_NOT TODO - ZE - Complementary output of TIMER4D on Digital Pin 6. Not sure this was intended.
+ NOT_ON_TIMER,
+
+ NOT_ON_TIMER, /* 8 port B */
+ TIMER1A,
+ TIMER1B,
+ TIMER0A,
+
+ TIMER3A, /* 12 port C */
+ TIMER4A,
+
+ NOT_ON_TIMER, /* 14 port B */
+ NOT_ON_TIMER,
+};
+
+#endif
+
+#endif \ No newline at end of file
diff --git a/variants/leonardo/pins_arduino.h b/variants/leonardo/pins_arduino.h
new file mode 100644
index 0000000..79826f0
--- /dev/null
+++ b/variants/leonardo/pins_arduino.h
@@ -0,0 +1,231 @@
+/*
+ pins_arduino.h - Pin definition functions for Arduino
+ Part of Arduino - http://www.arduino.cc/
+
+ Copyright (c) 2007 David A. Mellis
+
+ This library is free software; you can redistribute it and/or
+ modify it under the terms of the GNU Lesser General Public
+ License as published by the Free Software Foundation; either
+ version 2.1 of the License, or (at your option) any later version.
+
+ This library 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
+ Lesser General Public License for more details.
+
+ You should have received a copy of the GNU Lesser General
+ Public License along with this library; if not, write to the
+ Free Software Foundation, Inc., 59 Temple Place, Suite 330,
+ Boston, MA 02111-1307 USA
+
+ $Id: wiring.h 249 2007-02-03 16:52:51Z mellis $
+*/
+
+#ifndef Pins_Arduino_h
+#define Pins_Arduino_h
+
+#include <avr/pgmspace.h>
+
+// Map SPI port to 'new' pins D14..D17
+// D14 PB0 RXLED,SS/PCINT0
+// D15 PB1 SCK,PCINT1
+// D16 PB2 MOSI,PCINT2
+// D17 PB3 MISO,PCINT3
+const static uint8_t SS = 14;
+const static uint8_t MOSI = 16;
+const static uint8_t MISO = 17;
+const static uint8_t SCK = 15;
+
+const static uint8_t A0 = 14;
+const static uint8_t A1 = 15;
+const static uint8_t A2 = 16;
+const static uint8_t A3 = 17;
+const static uint8_t A4 = 18;
+const static uint8_t A5 = 19;
+const static uint8_t A6 = 20;
+const static uint8_t A7 = 21;
+
+#ifdef ARDUINO_MAIN
+
+// On the Arduino board, digital pins are also used
+// for the analog output (software PWM). Analog input
+// pins are a separate set.
+
+// ATMEL ATMEGA8 & 168 / ARDUINO
+//
+// +-\/-+
+// PC6 1| |28 PC5 (AI 5)
+// (D 0) PD0 2| |27 PC4 (AI 4)
+// (D 1) PD1 3| |26 PC3 (AI 3)
+// (D 2) PD2 4| |25 PC2 (AI 2)
+// PWM+ (D 3) PD3 5| |24 PC1 (AI 1)
+// (D 4) PD4 6| |23 PC0 (AI 0)
+// VCC 7| |22 GND
+// GND 8| |21 AREF
+// PB6 9| |20 AVCC
+// PB7 10| |19 PB5 (D 13)
+// PWM+ (D 5) PD5 11| |18 PB4 (D 12)
+// PWM+ (D 6) PD6 12| |17 PB3 (D 11) PWM
+// (D 7) PD7 13| |16 PB2 (D 10) PWM
+// (D 8) PB0 14| |15 PB1 (D 9) PWM
+// +----+
+//
+// (PWM+ indicates the additional PWM pins on the ATmega168.)
+
+// ATMEL ATMEGA1280 / ARDUINO
+//
+// 0-7 PE0-PE7 works
+// 8-13 PB0-PB5 works
+// 14-21 PA0-PA7 works
+// 22-29 PH0-PH7 works
+// 30-35 PG5-PG0 works
+// 36-43 PC7-PC0 works
+// 44-51 PJ7-PJ0 works
+// 52-59 PL7-PL0 works
+// 60-67 PD7-PD0 works
+// A0-A7 PF0-PF7
+// A8-A15 PK0-PK7
+
+// ATMEL ATMEGA32U4 / ARDUINO LEONARDO
+//
+// D0 PD2 RXD1/INT2
+// D1 PD3 TXD1/INT3
+// D2 PD1 SDA SDA/INT1
+// D3# PD0 PWM8/SCL OC0B/SCL/INT0
+// D4 A6 PD4 ADC8
+// D5# A7 PD6 FastPWM OC4D/ADC9
+// D6# A8 PD7 FastPWM #OC4D/ADC10
+// D7 PD5 #CTS
+//
+// D8 PB4 ADC11/PCINT4
+// D9# A9 PB5 PWM16 OC1A/#OC4B/ADC13/PCINT5
+// D10# A10 PB6 PWM16 OC1B/0c4B/ADC12/PCINT6
+// D11# PB7 PWM8/16 0C0A/OC1C/#RTS/PCINT7
+// D12# PC6 PWM16 0C3A/#OC4A
+// D13# PC7 PWM10 CLK0/OC4A
+//
+// A0 PF7 ADC7
+// A1 PF6 ADC6
+// A2 PF5 ADC5
+// A3 PF4 ADC4
+// A4 PF1 ADC1
+// A5 PF0 ADC0
+//
+// New pins D14..D17 to map SPI port to digitial pins
+//
+// D14 PB0 RXLED,SS/PCINT0
+// D15 PB1 SCK,PCINT1
+// D16 PB2 MOSI,PCINT2
+// D17 PB3 MISO,PCINT3
+//
+// TXLED PE6 INT6
+// HWB PE2 HWB
+
+
+// these arrays map port names (e.g. port B) to the
+// appropriate addresses for various functions (e.g. reading
+// and writing)
+const uint16_t PROGMEM port_to_mode_PGM[] = {
+ NOT_A_PORT,
+ NOT_A_PORT,
+ (uint16_t) &DDRB,
+ (uint16_t) &DDRC,
+ (uint16_t) &DDRD,
+ (uint16_t) &DDRE,
+ (uint16_t) &DDRF,
+};
+
+const uint16_t PROGMEM port_to_output_PGM[] = {
+ NOT_A_PORT,
+ NOT_A_PORT,
+ (uint16_t) &PORTB,
+ (uint16_t) &PORTC,
+ (uint16_t) &PORTD,
+ (uint16_t) &PORTE,
+ (uint16_t) &PORTF,
+};
+
+const uint16_t PROGMEM port_to_input_PGM[] = {
+ NOT_A_PORT,
+ NOT_A_PORT,
+ (uint16_t) &PINB,
+ (uint16_t) &PINC,
+ (uint16_t) &PIND,
+ (uint16_t) &PINE,
+ (uint16_t) &PINF,
+};
+
+const uint8_t PROGMEM digital_pin_to_port_PGM[18] = {
+ PD, /* 0 */
+ PD,
+ PD,
+ PD,
+ PD,
+ PD,
+ PD,
+ PD,
+
+ PB, /* 8 */
+ PB,
+ PB,
+ PB,
+
+ PC, /* 12 */
+ PC, /* 13 */
+
+ PB, /* 14 */
+ PB, /* 15 */
+ PB, /* 16 */
+ PB, /* 17 */
+};
+
+const uint8_t PROGMEM digital_pin_to_bit_mask_PGM[18] = {
+ _BV(2), /* 0, port D */
+ _BV(3),
+ _BV(1),
+ _BV(0),
+ _BV(4),
+ _BV(6),
+ _BV(7),
+ _BV(5),
+
+ _BV(4), /* 8, port B */
+ _BV(5),
+ _BV(6),
+ _BV(7),
+
+ _BV(6), /* 12 port C */
+ _BV(7),
+
+ _BV(0), /* 14, port B */
+ _BV(1),
+ _BV(2),
+ _BV(3),
+};
+
+const uint8_t PROGMEM digital_pin_to_timer_PGM[18] = {
+ NOT_ON_TIMER, /* 0, port D */
+ NOT_ON_TIMER,
+ NOT_ON_TIMER,
+ TIMER0B,
+ NOT_ON_TIMER,
+ NOT_ON_TIMER, // TIMER4D TODO - ZE - Fix this when there's a decision on what to do about these PWM pins
+ NOT_ON_TIMER, // TIMER4D_NOT TODO - ZE - Complementary output of TIMER4D on Digital Pin 6. Not sure this was intended.
+ NOT_ON_TIMER,
+
+ NOT_ON_TIMER, /* 8 port B */
+ TIMER1A,
+ TIMER1B,
+ TIMER0A,
+
+ TIMER3A, /* 12 port C */
+ TIMER4A,
+
+ NOT_ON_TIMER, /* 14 port B */
+ NOT_ON_TIMER,
+};
+
+#endif
+
+#endif \ No newline at end of file