aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--boards.txt157
-rw-r--r--bootloaders/diskloader/DiskLoader-Leonardo.hex113
-rw-r--r--bootloaders/diskloader/DiskLoader-Micro.hex113
-rw-r--r--bootloaders/diskloader/Makefile105
-rw-r--r--bootloaders/diskloader/src/DiskLoader.cpp239
-rw-r--r--bootloaders/diskloader/src/Platform.h49
-rw-r--r--bootloaders/diskloader/src/USBCore.cpp508
-rw-r--r--bootloaders/diskloader/src/USBCore.h246
-rw-r--r--bootloaders/diskloader/src/USBDesc.cpp82
-rw-r--r--bootloaders/diskloader/src/USBDesc.h63
-rw-r--r--bootloaders/nuevo_diskloader/DiskLoader.elf.hex172
-rw-r--r--bootloaders/nuevo_diskloader/DiskLoader.hex172
-rw-r--r--bootloaders/nuevo_diskloader/Makefile109
-rw-r--r--bootloaders/nuevo_diskloader/src/CDC.cpp173
-rw-r--r--bootloaders/nuevo_diskloader/src/DiskLoader.cpp280
-rw-r--r--bootloaders/nuevo_diskloader/src/Platform.h72
-rw-r--r--bootloaders/nuevo_diskloader/src/USBAPI.h83
-rw-r--r--bootloaders/nuevo_diskloader/src/USBCore.cpp669
-rw-r--r--bootloaders/nuevo_diskloader/src/USBCore.h303
-rw-r--r--bootloaders/nuevo_diskloader/src/USBDesc.h65
-rwxr-xr-xcores/arduino/Arduino.h206
-rw-r--r--cores/arduino/CDC.cpp175
-rw-r--r--cores/arduino/Client.h26
-rw-r--r--cores/arduino/HID.cpp446
-rw-r--r--cores/arduino/HardwareSerial.cpp275
-rw-r--r--cores/arduino/IPAddress.cpp56
-rw-r--r--cores/arduino/IPAddress.h76
-rw-r--r--cores/arduino/Platform.h23
-rwxr-xr-xcores/arduino/Print.h62
-rw-r--r--cores/arduino/Printable.h40
-rw-r--r--cores/arduino/Server.h9
-rw-r--r--cores/arduino/Stream.cpp243
-rw-r--r--cores/arduino/Stream.h59
-rwxr-xr-xcores/arduino/Tone.cpp2
-rw-r--r--cores/arduino/USBAPI.h166
-rw-r--r--cores/arduino/USBCore.cpp660
-rw-r--r--cores/arduino/USBCore.h303
-rw-r--r--cores/arduino/USBDesc.h67
-rw-r--r--cores/arduino/Udp.h88
-rw-r--r--cores/arduino/WConstants.h1
-rwxr-xr-xcores/arduino/WInterrupts.c1
-rwxr-xr-xcores/arduino/WProgram.h63
-rw-r--r--cores/arduino/WString.cpp736
-rwxr-xr-xcores/arduino/main.cpp11
-rw-r--r--cores/arduino/new.cpp18
-rw-r--r--cores/arduino/new.h22
-rw-r--r--cores/arduino/pins_arduino.h88
-rwxr-xr-xcores/arduino/wiring.c6
-rwxr-xr-xcores/arduino/wiring.h135
-rw-r--r--cores/arduino/wiring_analog.c15
-rwxr-xr-xcores/arduino/wiring_digital.c11
-rwxr-xr-xcores/arduino/wiring_private.h3
-rw-r--r--programmers.txt4
-rw-r--r--variants/eightanaloginputs/pins_arduino.h27
-rw-r--r--variants/leonardo/pins_arduino.h276
-rwxr-xr-xvariants/mega/pins_arduino.h (renamed from cores/arduino/pins_arduino.c)0
-rw-r--r--variants/micro/pins_arduino.h27
-rw-r--r--variants/standard/pins_arduino.h218
58 files changed, 7721 insertions, 696 deletions
diff --git a/boards.txt b/boards.txt
index c252a75..3e99f81 100644
--- a/boards.txt
+++ b/boards.txt
@@ -1,7 +1,7 @@
##############################################################
uno.name=Arduino Uno
-uno.upload.protocol=stk500
+uno.upload.protocol=arduino
uno.upload.maximum_size=32256
uno.upload.speed=115200
uno.bootloader.low_fuses=0xff
@@ -14,12 +14,13 @@ uno.bootloader.lock_bits=0x2F
uno.build.mcu=atmega328p
uno.build.f_cpu=16000000L
uno.build.core=arduino
+uno.build.variant=standard
##############################################################
-atmega328.name=Arduino Duemilanove or Nano w/ ATmega328
+atmega328.name=Arduino Duemilanove w/ ATmega328
-atmega328.upload.protocol=stk500
+atmega328.upload.protocol=arduino
atmega328.upload.maximum_size=30720
atmega328.upload.speed=57600
@@ -34,12 +35,13 @@ atmega328.bootloader.lock_bits=0x0F
atmega328.build.mcu=atmega328p
atmega328.build.f_cpu=16000000L
atmega328.build.core=arduino
+atmega328.build.variant=standard
##############################################################
-diecimila.name=Arduino Diecimila, Duemilanove, or Nano w/ ATmega168
+diecimila.name=Arduino Diecimila or Duemilanove w/ ATmega168
-diecimila.upload.protocol=stk500
+diecimila.upload.protocol=arduino
diecimila.upload.maximum_size=14336
diecimila.upload.speed=19200
@@ -54,10 +56,53 @@ diecimila.bootloader.lock_bits=0x0F
diecimila.build.mcu=atmega168
diecimila.build.f_cpu=16000000L
diecimila.build.core=arduino
+diecimila.build.variant=standard
##############################################################
-mega2560.name=Arduino Mega 2560
+nano328.name=Arduino Nano w/ ATmega328
+
+nano328.upload.protocol=arduino
+nano328.upload.maximum_size=30720
+nano328.upload.speed=57600
+
+nano328.bootloader.low_fuses=0xFF
+nano328.bootloader.high_fuses=0xDA
+nano328.bootloader.extended_fuses=0x05
+nano328.bootloader.path=atmega
+nano328.bootloader.file=ATmegaBOOT_168_atmega328.hex
+nano328.bootloader.unlock_bits=0x3F
+nano328.bootloader.lock_bits=0x0F
+
+nano328.build.mcu=atmega328p
+nano328.build.f_cpu=16000000L
+nano328.build.core=arduino
+nano328.build.variant=eightanaloginputs
+
+##############################################################
+
+nano.name=Arduino Nano w/ ATmega168
+
+nano.upload.protocol=arduino
+nano.upload.maximum_size=14336
+nano.upload.speed=19200
+
+nano.bootloader.low_fuses=0xff
+nano.bootloader.high_fuses=0xdd
+nano.bootloader.extended_fuses=0x00
+nano.bootloader.path=atmega
+nano.bootloader.file=ATmegaBOOT_168_diecimila.hex
+nano.bootloader.unlock_bits=0x3F
+nano.bootloader.lock_bits=0x0F
+
+nano.build.mcu=atmega168
+nano.build.f_cpu=16000000L
+nano.build.core=arduino
+nano.build.variant=eightanaloginputs
+
+##############################################################
+
+mega2560.name=Arduino Mega 2560 or Mega ADK
mega2560.upload.protocol=stk500v2
mega2560.upload.maximum_size=258048
@@ -74,12 +119,13 @@ mega2560.bootloader.lock_bits=0x0F
mega2560.build.mcu=atmega2560
mega2560.build.f_cpu=16000000L
mega2560.build.core=arduino
+mega2560.build.variant=mega
##############################################################
mega.name=Arduino Mega (ATmega1280)
-mega.upload.protocol=stk500
+mega.upload.protocol=arduino
mega.upload.maximum_size=126976
mega.upload.speed=57600
@@ -94,12 +140,49 @@ mega.bootloader.lock_bits=0x0F
mega.build.mcu=atmega1280
mega.build.f_cpu=16000000L
mega.build.core=arduino
+mega.build.variant=mega
+
+##############################################################
+
+leonardo.name=Arduino Leonardo
+leonardo.upload.protocol=arduino
+leonardo.upload.maximum_size=30720
+leonardo.upload.speed=1200
+leonardo.bootloader.low_fuses=0xde
+leonardo.bootloader.high_fuses=0xda
+leonardo.bootloader.extended_fuses=0xcb
+leonardo.bootloader.path=nuevo_diskloader
+leonardo.bootloader.file=DiskLoader.hex
+leonardo.bootloader.unlock_bits=0x3F
+leonardo.bootloader.lock_bits=0x2F
+leonardo.build.mcu=atmega32u4
+leonardo.build.f_cpu=16000000L
+leonardo.build.core=arduino
+leonardo.build.variant=leonardo
+
+##############################################################
+
+#micro.name=Arduino Micro
+#micro.upload.protocol=arduino
+#micro.upload.maximum_size=30720
+#micro.upload.speed=1200
+#micro.bootloader.low_fuses=0xde
+#micro.bootloader.high_fuses=0xda
+#micro.bootloader.extended_fuses=0xcb
+#micro.bootloader.path=diskloader
+#micro.bootloader.file=DiskLoader-Micro.hex
+#micro.bootloader.unlock_bits=0x3F
+#micro.bootloader.lock_bits=0x2F
+#micro.build.mcu=atmega32u4
+#micro.build.f_cpu=16000000L
+#micro.build.core=arduino
+#micro.build.variant=micro
##############################################################
mini.name=Arduino Mini
-mini.upload.protocol=stk500
+mini.upload.protocol=arduino
mini.upload.maximum_size=14336
mini.upload.speed=19200
@@ -114,12 +197,34 @@ mini.bootloader.lock_bits=0x0F
mini.build.mcu=atmega168
mini.build.f_cpu=16000000L
mini.build.core=arduino
+mini.build.variant=eightanaloginputs
+
+##############################################################
+
+ethernet.name=Arduino Ethernet
+
+ethernet.upload.protocol=arduino
+ethernet.upload.maximum_size=32256
+ethernet.upload.speed=115200
+
+ethernet.bootloader.low_fuses=0xff
+ethernet.bootloader.high_fuses=0xde
+ethernet.bootloader.extended_fuses=0x05
+ethernet.bootloader.path=optiboot
+ethernet.bootloader.file=optiboot_atmega328.hex
+ethernet.bootloader.unlock_bits=0x3F
+ethernet.bootloader.lock_bits=0x0F
+
+ethernet.build.variant=standard
+ethernet.build.mcu=atmega328p
+ethernet.build.f_cpu=16000000L
+ethernet.build.core=arduino
##############################################################
fio.name=Arduino Fio
-fio.upload.protocol=stk500
+fio.upload.protocol=arduino
fio.upload.maximum_size=30720
fio.upload.speed=57600
@@ -133,13 +238,14 @@ fio.bootloader.lock_bits=0x0F
fio.build.mcu=atmega328p
fio.build.f_cpu=8000000L
-fio.build.core=arduino:arduino
+fio.build.core=arduino
+fio.build.variant=eightanaloginputs
##############################################################
bt328.name=Arduino BT w/ ATmega328
-bt328.upload.protocol=stk500
+bt328.upload.protocol=arduino
bt328.upload.maximum_size=28672
bt328.upload.speed=19200
bt328.upload.disable_flushing=true
@@ -155,12 +261,13 @@ bt328.bootloader.lock_bits=0x0F
bt328.build.mcu=atmega328p
bt328.build.f_cpu=16000000L
bt328.build.core=arduino
+bt328.build.variant=eightanaloginputs
##############################################################
bt.name=Arduino BT w/ ATmega168
-bt.upload.protocol=stk500
+bt.upload.protocol=arduino
bt.upload.maximum_size=14336
bt.upload.speed=19200
bt.upload.disable_flushing=true
@@ -176,12 +283,13 @@ bt.bootloader.lock_bits=0x0F
bt.build.mcu=atmega168
bt.build.f_cpu=16000000L
bt.build.core=arduino
+bt.build.variant=eightanaloginputs
##############################################################
lilypad328.name=LilyPad Arduino w/ ATmega328
-lilypad328.upload.protocol=stk500
+lilypad328.upload.protocol=arduino
lilypad328.upload.maximum_size=30720
lilypad328.upload.speed=57600
@@ -196,12 +304,13 @@ lilypad328.bootloader.lock_bits=0x0F
lilypad328.build.mcu=atmega328p
lilypad328.build.f_cpu=8000000L
lilypad328.build.core=arduino
+lilypad328.build.variant=standard
##############################################################
lilypad.name=LilyPad Arduino w/ ATmega168
-lilypad.upload.protocol=stk500
+lilypad.upload.protocol=arduino
lilypad.upload.maximum_size=14336
lilypad.upload.speed=19200
@@ -216,12 +325,13 @@ lilypad.bootloader.lock_bits=0x0F
lilypad.build.mcu=atmega168
lilypad.build.f_cpu=8000000L
lilypad.build.core=arduino
+lilypad.build.variant=standard
##############################################################
pro5v328.name=Arduino Pro or Pro Mini (5V, 16 MHz) w/ ATmega328
-pro5v328.upload.protocol=stk500
+pro5v328.upload.protocol=arduino
pro5v328.upload.maximum_size=30720
pro5v328.upload.speed=57600
@@ -236,12 +346,13 @@ pro5v328.bootloader.lock_bits=0x0F
pro5v328.build.mcu=atmega328p
pro5v328.build.f_cpu=16000000L
pro5v328.build.core=arduino
+pro5v328.build.variant=standard
##############################################################
pro5v.name=Arduino Pro or Pro Mini (5V, 16 MHz) w/ ATmega168
-pro5v.upload.protocol=stk500
+pro5v.upload.protocol=arduino
pro5v.upload.maximum_size=14336
pro5v.upload.speed=19200
@@ -256,12 +367,13 @@ pro5v.bootloader.lock_bits=0x0F
pro5v.build.mcu=atmega168
pro5v.build.f_cpu=16000000L
pro5v.build.core=arduino
+pro5v.build.variant=standard
##############################################################
pro328.name=Arduino Pro or Pro Mini (3.3V, 8 MHz) w/ ATmega328
-pro328.upload.protocol=stk500
+pro328.upload.protocol=arduino
pro328.upload.maximum_size=30720
pro328.upload.speed=57600
@@ -276,12 +388,13 @@ pro328.bootloader.lock_bits=0x0F
pro328.build.mcu=atmega328p
pro328.build.f_cpu=8000000L
pro328.build.core=arduino
+pro328.build.variant=standard
##############################################################
pro.name=Arduino Pro or Pro Mini (3.3V, 8 MHz) w/ ATmega168
-pro.upload.protocol=stk500
+pro.upload.protocol=arduino
pro.upload.maximum_size=14336
pro.upload.speed=19200
@@ -296,12 +409,13 @@ pro.bootloader.lock_bits=0x0F
pro.build.mcu=atmega168
pro.build.f_cpu=8000000L
pro.build.core=arduino
+pro.build.variant=standard
##############################################################
atmega168.name=Arduino NG or older w/ ATmega168
-atmega168.upload.protocol=stk500
+atmega168.upload.protocol=arduino
atmega168.upload.maximum_size=14336
atmega168.upload.speed=19200
@@ -316,12 +430,13 @@ atmega168.bootloader.lock_bits=0x0F
atmega168.build.mcu=atmega168
atmega168.build.f_cpu=16000000L
atmega168.build.core=arduino
+atmega168.build.variant=standard
##############################################################
atmega8.name=Arduino NG or older w/ ATmega8
-atmega8.upload.protocol=stk500
+atmega8.upload.protocol=arduino
atmega8.upload.maximum_size=7168
atmega8.upload.speed=19200
@@ -335,4 +450,4 @@ atmega8.bootloader.lock_bits=0x0F
atmega8.build.mcu=atmega8
atmega8.build.f_cpu=16000000L
atmega8.build.core=arduino
-
+atmega8.build.variant=standard
diff --git a/bootloaders/diskloader/DiskLoader-Leonardo.hex b/bootloaders/diskloader/DiskLoader-Leonardo.hex
new file mode 100644
index 0000000..4528a8b
--- /dev/null
+++ b/bootloaders/diskloader/DiskLoader-Leonardo.hex
@@ -0,0 +1,113 @@
+:1078000011241FBECFEFDAE0DEBFCDBFF2C04101D1
+:1078100042144505560455026403740300001E9586
+:1078200087020110030000C18081C106C0FF0A0069
+:107830000CA10185037508150026FF00954009017C
+:107840008102954009029102C004030904380341F2
+:107850000072006400750069006E006F0020004C2B
+:107860000065006F006E006100720064006F002010
+:107870000062006F006F0074006C006F00610064B4
+:107880000065007200180341007200640075006911
+:10789000006E006F0020004C004C004300120100FD
+:1078A00002020000404123340000010102000112E5
+:1078B00001000200000040412334000001010200E9
+:1078C00001090264000301008032080B0002020279
+:1078D000010009040000010202000005240010015B
+:1078E0000524010101042402020524060001070504
+:1078F000810310004009040100020A00000007058E
+:107900000202400000070583024000000904020053
+:107910000103000000092101010001221E000705EA
+:1079200084034000400020918A0130918B012C5F3C
+:107930003F4F30938B0120938A01C901892F9927EA
+:107940008695982F803418F08FE7891B982F990F10
+:10795000921710F447980895479A08955D9A289AC7
+:1079600081E08093E0001092E200EE27FF27099467
+:1079700008950F931F93CF93DF93982FEB01042F5C
+:1079800010E088E76030780730F411E083E0FB0115
+:1079900080935700E895892F68E071E0402FF8D078
+:1079A000112311F107B600FCFDCF402F4695FE01D3
+:1079B000A8E0B1E020E031E009C08D919D910C017B
+:1079C00030935700E895112432962F5F2417A8F3BF
+:1079D00085E0FE0180935700E89507B600FCFDCFD7
+:1079E00081E180935700E895DF91CF911F910F912E
+:1079F000089588E10FB6F8948093600010926000BB
+:107A00000FBE5D9A289A47983F9A209A559A90E817
+:107A1000909361001092610081E885BF95BF9FD06F
+:107A200084E18093880180E180938901E0E0F0E0C7
+:107A3000859194918F5F9F4F19F081E080938F0122
+:107A4000EE24FF24BB24B39454EFC52E51E0D52E71
+:107A50000ED2082F8EE098E7FC0124913196029611
+:107A60004491201711F02223B9F71092910110923E
+:107A7000900182E068E071E08BD0013479F460918C
+:107A800008016058633028F0683111F064E001C0EB
+:107A900063E0C62FD0E0CF5DD7480EC0063571F445
+:107AA00080910801803311F011E022C080910A0119
+:107AB000C82FD0E0C25ED74811E022C0053721F4BC
+:107AC00013E0CEE1D8E71CC0053539F4E090080199
+:107AD000F0900901EE0CFF1C0AC0043631F482E07C
+:107AE000B7014091090145DF02C0043721F010E0E1
+:107AF000C5E2D8E705C010910901E701E10EF11CCC
+:107B0000B6D18097B1F483E068E871E041E050E0DD
+:107B100059D0112329F0412F50E083E8BE0152D003
+:107B200083E469E871E041E050E04CD0013509F0B0
+:107B30008FCFD0929101C0929001B0928F01EE242C
+:107B4000FF2486CFFC01289884E680938D0104C031
+:107B50008091F100819361506623D1F708951092CE
+:107B600091011092900110928E0110928F0181E08C
+:107B70008093D70080EA8093D80082E189BD09B460
+:107B800000FEFDCF80E98093D8001092E0000895B8
+:107B9000FB018093E90024E69BE611C08091E80098
+:107BA00085FFFCCF289820938D018091F10081936F
+:107BB0008091E80085FD02C09093E8004150442385
+:107BC00069F70895982FFB01282F20728770809302
+:107BD000E90064E63AE317C08091E80085FFFCCF36
+:107BE00097FF02C0849101C080813196211180E00D
+:107BF0005D9860938C018093F1008091E80085FD91
+:107C000002C03093E800415050408FEF4F3F58077B
+:107C100019F796FF03C08AE38093E80008958091E6
+:107C20009301813299F45D9884E680938C0120E081
+:107C300030E003C080818093F100F901E050FF4FF4
+:107C40002F5F3F4F28303105A9F714C0803261F40F
+:107C50008091E80082FFFCCF80E091E067E072DF76
+:107C60008BEF8093E80006C0823221F4809194016A
+:107C70008093070181E0089520919501223261F1FE
+:107C8000213081F48091980190919901089711F425
+:107C900020939A0180919A01882309F04AC0EFEA63
+:107CA000F8E749C0223021F484E6E1ECF8E717C098
+:107CB000233009F03CC080919401882319F4E9E451
+:107CC000F8E739C0823019F4EDE4F8E734C08130C8
+:107CD00071F5E5E8F8E72FC081E0EBE2F8E7909175
+:107CE0009801382F981708F4392F90918C0120E0D3
+:107CF0004EEF8091E8008570E1F38091E80082FD0D
+:107D000012C02F5F84915D988093F100822F8F7352
+:107D100011F44093E800319694E6231750F39093C2
+:107D20008C0181E0089590938C0180E0089580E0BB
+:107D30000895EDE9F8E78491D2CF1092E90080919F
+:107D4000E80083FF61C082E991E068E0FBDE82EF3A
+:107D50008093E8008091920187FF05C08091E80040
+:107D600080FFFCCF03C08EEF8093E80080919301E9
+:107D7000853051F48091E80080FFFCCF8091940120
+:107D800080688093E30039C08930E1F480919401E8
+:107D900080938E01E7E2F8E791E031E026E39093EB
+:107DA000E9003093EB0084918093EC002093ED0088
+:107DB0009F5F3196953099F78EE78093EA00109295
+:107DC000EA001BC0883049F490918E015D9884E6EA
+:107DD00080938C019093F10010C0882339F45D9852
+:107DE00084E680938C011092F10007C0863011F474
+:107DF00043DF01C014DF882321F08EEF8093E80079
+:107E0000089581E28093EB0008958091E100109243
+:107E1000E100282F83FF0CC01092E90081E08093DD
+:107E2000EB001092EC0082E38093ED0010928E0143
+:107E300022FF1CC080918C01882331F081508093F7
+:107E40008C01882309F45D9A80918D01882331F09B
+:107E5000815080938D01882309F4289A80918F01A5
+:107E6000882321F4109291011092900108951F939C
+:107E7000CF93DF9312E0C0E9D1E05FDFC6DF10935C
+:107E8000E9008091E80085FF13C0289884E680937C
+:107E90008D019091F1008091E80085FD03C08BE693
+:107EA0008093E800892F90E0DF91CF911F91089592
+:107EB000809190019091910101979093910180930D
+:107EC00090018091900190919101892B09F446DDF8
+:0A7ED000CE010197F1F727DDD0CFB6
+:087EDA0000E1000000000000BF
+:040000030000780081
+:00000001FF
diff --git a/bootloaders/diskloader/DiskLoader-Micro.hex b/bootloaders/diskloader/DiskLoader-Micro.hex
new file mode 100644
index 0000000..fbbcc52
--- /dev/null
+++ b/bootloaders/diskloader/DiskLoader-Micro.hex
@@ -0,0 +1,113 @@
+:1078000011241FBECFEFDAE0DEBFCDBFF2C04101D1
+:1078100042144505560455026403740300001E9586
+:1078200087020110030000C18081C106C0FF0A0069
+:107830000CA10185037508150026FF00954009017C
+:107840008102954009029102C004030904380341F2
+:107850000072006400750069006E006F0020004D2A
+:10786000006900630072006F00200062006F006F0B
+:107870000074006C006F00610064006500720020FD
+:1078800000200020001803410072006400750069A8
+:10789000006E006F0020004C004C004300120100FD
+:1078A00002020000404123350000010102000112E4
+:1078B00001000200000040412335000001010200E8
+:1078C00001090264000301008032080B0002020279
+:1078D000010009040000010202000005240010015B
+:1078E0000524010101042402020524060001070504
+:1078F000810310004009040100020A00000007058E
+:107900000202400000070583024000000904020053
+:107910000103000000092101010001221E000705EA
+:1079200084034000400020918A0130918B012C5F3C
+:107930003F4F30938B0120938A01C901892F9927EA
+:107940008695982F803418F08FE7891B982F990F10
+:10795000921710F447980895479A08955D9A289AC7
+:1079600081E08093E0001092E200EE27FF27099467
+:1079700008950F931F93CF93DF93982FEB01042F5C
+:1079800010E088E76030780730F411E083E0FB0115
+:1079900080935700E895892F68E071E0402FF8D078
+:1079A000112311F107B600FCFDCF402F4695FE01D3
+:1079B000A8E0B1E020E031E009C08D919D910C017B
+:1079C00030935700E895112432962F5F2417A8F3BF
+:1079D00085E0FE0180935700E89507B600FCFDCFD7
+:1079E00081E180935700E895DF91CF911F910F912E
+:1079F000089588E10FB6F8948093600010926000BB
+:107A00000FBE5D9A289A47983F9A209A559A90E817
+:107A1000909361001092610081E885BF95BF9FD06F
+:107A200084E18093880180E180938901E0E0F0E0C7
+:107A3000859194918F5F9F4F19F081E080938F0122
+:107A4000EE24FF24BB24B39454EFC52E51E0D52E71
+:107A50000ED2082F8EE098E7FC0124913196029611
+:107A60004491201711F02223B9F71092910110923E
+:107A7000900182E068E071E08BD0013479F460918C
+:107A800008016058633028F0683111F064E001C0EB
+:107A900063E0C62FD0E0CF5DD7480EC0063571F445
+:107AA00080910801803311F011E022C080910A0119
+:107AB000C82FD0E0C25ED74811E022C0053721F4BC
+:107AC00013E0CEE1D8E71CC0053539F4E090080199
+:107AD000F0900901EE0CFF1C0AC0043631F482E07C
+:107AE000B7014091090145DF02C0043721F010E0E1
+:107AF000C5E2D8E705C010910901E701E10EF11CCC
+:107B0000B6D18097B1F483E068E871E041E050E0DD
+:107B100059D0112329F0412F50E083E8BE0152D003
+:107B200083E469E871E041E050E04CD0013509F0B0
+:107B30008FCFD0929101C0929001B0928F01EE242C
+:107B4000FF2486CFFC01289884E680938D0104C031
+:107B50008091F100819361506623D1F708951092CE
+:107B600091011092900110928E0110928F0181E08C
+:107B70008093D70080EA8093D80082E189BD09B460
+:107B800000FEFDCF80E98093D8001092E0000895B8
+:107B9000FB018093E90024E69BE611C08091E80098
+:107BA00085FFFCCF289820938D018091F10081936F
+:107BB0008091E80085FD02C09093E8004150442385
+:107BC00069F70895982FFB01282F20728770809302
+:107BD000E90064E63AE317C08091E80085FFFCCF36
+:107BE00097FF02C0849101C080813196211180E00D
+:107BF0005D9860938C018093F1008091E80085FD91
+:107C000002C03093E800415050408FEF4F3F58077B
+:107C100019F796FF03C08AE38093E80008958091E6
+:107C20009301813299F45D9884E680938C0120E081
+:107C300030E003C080818093F100F901E050FF4FF4
+:107C40002F5F3F4F28303105A9F714C0803261F40F
+:107C50008091E80082FFFCCF80E091E067E072DF76
+:107C60008BEF8093E80006C0823221F4809194016A
+:107C70008093070181E0089520919501223261F1FE
+:107C8000213081F48091980190919901089711F425
+:107C900020939A0180919A01882309F04AC0EFEA63
+:107CA000F8E749C0223021F484E6E1ECF8E717C098
+:107CB000233009F03CC080919401882319F4E9E451
+:107CC000F8E739C0823019F4EDE4F8E734C08130C8
+:107CD00071F5E5E8F8E72FC081E0EBE2F8E7909175
+:107CE0009801382F981708F4392F90918C0120E0D3
+:107CF0004EEF8091E8008570E1F38091E80082FD0D
+:107D000012C02F5F84915D988093F100822F8F7352
+:107D100011F44093E800319694E6231750F39093C2
+:107D20008C0181E0089590938C0180E0089580E0BB
+:107D30000895EDE9F8E78491D2CF1092E90080919F
+:107D4000E80083FF61C082E991E068E0FBDE82EF3A
+:107D50008093E8008091920187FF05C08091E80040
+:107D600080FFFCCF03C08EEF8093E80080919301E9
+:107D7000853051F48091E80080FFFCCF8091940120
+:107D800080688093E30039C08930E1F480919401E8
+:107D900080938E01E7E2F8E791E031E026E39093EB
+:107DA000E9003093EB0084918093EC002093ED0088
+:107DB0009F5F3196953099F78EE78093EA00109295
+:107DC000EA001BC0883049F490918E015D9884E6EA
+:107DD00080938C019093F10010C0882339F45D9852
+:107DE00084E680938C011092F10007C0863011F474
+:107DF00043DF01C014DF882321F08EEF8093E80079
+:107E0000089581E28093EB0008958091E100109243
+:107E1000E100282F83FF0CC01092E90081E08093DD
+:107E2000EB001092EC0082E38093ED0010928E0143
+:107E300022FF1CC080918C01882331F081508093F7
+:107E40008C01882309F45D9A80918D01882331F09B
+:107E5000815080938D01882309F4289A80918F01A5
+:107E6000882321F4109291011092900108951F939C
+:107E7000CF93DF9312E0C0E9D1E05FDFC6DF10935C
+:107E8000E9008091E80085FF13C0289884E680937C
+:107E90008D019091F1008091E80085FD03C08BE693
+:107EA0008093E800892F90E0DF91CF911F91089592
+:107EB000809190019091910101979093910180930D
+:107EC00090018091900190919101892B09F446DDF8
+:0A7ED000CE010197F1F727DDD0CFB6
+:087EDA0000E1000000000000BF
+:040000030000780081
+:00000001FF
diff --git a/bootloaders/diskloader/Makefile b/bootloaders/diskloader/Makefile
new file mode 100644
index 0000000..6ac3db3
--- /dev/null
+++ b/bootloaders/diskloader/Makefile
@@ -0,0 +1,105 @@
+###############################################################################
+# Makefile for DiskLoader
+###############################################################################
+
+## General Flags
+PROJECT = DiskLoader
+TARGET = DiskLoader.elf
+CC = avr-gcc
+
+# BOARD2
+MCU = atmega32u4
+AVR_FREQ = 16000000L
+
+# Specify the Arduino model using the assigned PID. This is used by Descriptors.c
+# to set PID and product descriptor string
+# Arduino Leonardo PID
+ARDUINO_MODEL_PID = 0x0034
+# Arduino Micro PID
+#ARDUINO_MODEL_PID = 0x0035
+
+# 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) -DARDUINO_MODEL_PID=$(ARDUINO_MODEL_PID) $(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..6580618
--- /dev/null
+++ b/bootloaders/diskloader/src/DiskLoader.cpp
@@ -0,0 +1,239 @@
+
+
+#include "Platform.h"
+
+// This bootloader creates a composite Serial device
+//
+// The serial interface supports a STK500v1 protocol that is very similar to optiboot
+//
+// 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);
+int main(void) __attribute__ ((naked));
+
+// STK500v1 main loop, very similar to optiboot in protocol and implementation
+int main()
+{
+ wdt_disable();
+ TXLED0;
+ RXLED0;
+ LED0;
+ BOARD_INIT();
+ USBInit();
+
+ _inSync = STK_INSYNC;
+ _ok = STK_OK;
+
+ if (pgm_read_word(0) != -1)
+ _ejected = 1;
+
+ 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;
+ }
+ _timeout = 0;
+ // 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 = 500; // wait a moment before exiting the bootloader - may need to finish responding to 'Q' for example
+ _ejected = 1;
+ }
+}
+
+// Nice breathing LED indicates we are in the firmware
+u16 _pulse;
+void LEDPulse()
+{
+ _pulse += 4;
+ u8 p = _pulse >> 9;
+ if (p > 63)
+ p = 127-p;
+ p += p;
+ if (((u8)_pulse) > p)
+ LED0;
+ else
+ LED1;
+}
+
+void Reboot()
+{
+ TXLED0; // switch off the RX and TX LEDs before starting the user sketch
+ RXLED0;
+ 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..6acbf39
--- /dev/null
+++ b/bootloaders/diskloader/src/Platform.h
@@ -0,0 +1,49 @@
+
+#include <inttypes.h>
+#include <avr/io.h>
+#include <avr/pgmspace.h>
+#include <avr/boot.h>
+#include <util/delay.h>
+#include <avr/interrupt.h>
+#include <avr/wdt.h>
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+
+typedef unsigned char u8;
+typedef unsigned short u16;
+typedef unsigned long u32;
+
+
+#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_PID_LEONARDO 0x0034
+#define USB_PID_MICRO 0x0035
+#define USB_VID 0x2341 // arduino LLC vid
+#define USB_PID ARDUINO_MODEL_PID // passed in by Makefile - 0x0034 for Leonardo, 0x0035 for MIcro
+
+#define OEM_NAME 'l','e','o','n','a','r','d','o' // 8 chars
+#define BOARD_INIT() DDRC |= (1<<7); DDRB |= (1<<0); DDRD |= (1<<5); CPU_PRESCALE(0); DISABLE_JTAG();
+#define LED0 PORTC &= ~(1<<7)
+#define LED1 PORTC |= (1<<7)
+#define TXLED0 PORTD |= (1<<5)
+#define TXLED1 PORTD &= ~(1<<5)
+#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
+
+#include "USBCore.h"
+#include "USBDesc.h"
+
+
diff --git a/bootloaders/diskloader/src/USBCore.cpp b/bootloaders/diskloader/src/USBCore.cpp
new file mode 100644
index 0000000..ca74721
--- /dev/null
+++ b/bootloaders/diskloader/src/USBCore.cpp
@@ -0,0 +1,508 @@
+
+
+/* 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 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
+
+/** Pulse generation counters to keep track of the number of milliseconds remaining for each pulse type */
+#define TX_RX_LED_PULSE_MS 100
+u8 TxLEDPulse; /**< Milliseconds remaining for data Tx LED pulse */
+u8 RxLEDPulse; /**< Milliseconds remaining for data Rx LED pulse */
+
+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
+ TxLEDPulse = TX_RX_LED_PULSE_MS;
+ while (count--)
+ UEDATX = *data++;
+}
+
+void Recv(volatile u8* data, u8 count)
+{
+ RXLED1; // light the RX LED
+ RxLEDPulse = TX_RX_LED_PULSE_MS;
+ while (count--)
+ *data++ = UEDATX;
+}
+
+static inline u8 Recv8()
+{
+ RXLED1; // light the RX LED
+ RxLEDPulse = TX_RX_LED_PULSE_MS;
+ return UEDATX;
+}
+
+static inline void Send8(u8 d)
+{
+ TXLED1; // light the TX LED
+ TxLEDPulse = TX_RX_LED_PULSE_MS;
+ 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
+};
+
+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;
+ }
+
+ 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 == IPRODUCT)
+ desc_addr = (const u8*)&STRING_IPRODUCT;
+ else if (setup.wValueL == IMANUFACTURER)
+ desc_addr = (const u8*)&STRING_IMANUFACTURER;
+ 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
+ }
+
+ // Start of Frame - happens every millisecond so we use it for TX and RX LED one-shot timing, too
+ if (udint & (1<<SOFI))
+ {
+ // check whether the one-shot period has elapsed. if so, turn off the LED
+ if (TxLEDPulse && !(--TxLEDPulse))
+ TXLED0;
+ if (RxLEDPulse && !(--RxLEDPulse))
+ RXLED0;
+
+ if (!_ejected)
+ _timeout = 0;
+ }
+}
+
+void LEDPulse();
+int USBGetChar()
+{
+ for(;;)
+ {
+ USBSetupInterrupt();
+ USBGeneralInterrupt();
+
+ // Read a char
+ if (HasData(CDC_RX))
+ {
+ u8 c = Recv8();
+ if (!ReadWriteAllowed())
+ ReleaseRX();
+ return c;
+ }
+
+ if (!--_timeout) {
+ Reboot(); // USB not connected, run firmware
+ }
+
+ _delay_us(100); // stretch out the bootloader period to about 5 seconds after enumeration
+ LEDPulse();
+ }
+ return -1;
+}
diff --git a/bootloaders/diskloader/src/USBCore.h b/bootloaders/diskloader/src/USBCore.h
new file mode 100644
index 0000000..537c0ec
--- /dev/null
+++ b/bootloaders/diskloader/src/USBCore.h
@@ -0,0 +1,246 @@
+
+// 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
+
+// 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
+
+
+// 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
+{
+ 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..c8ea9c6
--- /dev/null
+++ b/bootloaders/diskloader/src/USBDesc.cpp
@@ -0,0 +1,82 @@
+
+
+/* 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
+};
+
+
+const u16 STRING_IPRODUCT[28] = {
+ (3<<8) | (2+2*27),
+#if USB_PID == USB_PID_LEONARDO
+ 'A','r','d','u','i','n','o',' ','L','e','o','n','a','r','d','o',' ','b','o','o','t','l','o','a','d','e','r'
+#elif USB_PID == USB_PID_MICRO
+ 'A','r','d','u','i','n','o',' ','M','i','c','r','o',' ','b','o','o','t','l','o','a','d','e','r',' ',' ',' '
+#endif
+};
+
+const u16 STRING_IMANUFACTURER[12] = {
+ (3<<8) | (2+2*11),
+ 'A','r','d','u','i','n','o',' ','L','L','C'
+};
+
+
+//#ifdef CDC_ENABLED
+DeviceDescriptor USB_DeviceDescriptorA = D_DEVICE(0X02,0X00,0X00,64,USB_VID,USB_PID,0x100,IMANUFACTURER,IPRODUCT,0,1);
+//#else
+DeviceDescriptor USB_DeviceDescriptor = D_DEVICE(0x00,0x00,0x00,64,USB_VID,USB_PID,0x100,IMANUFACTURER,IPRODUCT,0,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)
+ }
+};
+
diff --git a/bootloaders/diskloader/src/USBDesc.h b/bootloaders/diskloader/src/USBDesc.h
new file mode 100644
index 0000000..094dba5
--- /dev/null
+++ b/bootloaders/diskloader/src/USBDesc.h
@@ -0,0 +1,63 @@
+
+
+/* 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.
+*/
+
+
+#ifdef CDC_ENABLED
+
+#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
+
+#define INTERFACE_COUNT 3 // 2 for cdc + 1 for hid
+
+#else
+
+#define HID_INTERFACE 2 // HID Interface
+#define HID_ENDPOINT_INT 4
+
+#define INTERFACE_COUNT 1 // 1 for hid
+
+#endif
+
+typedef struct
+{
+ ConfigDescriptor config;
+#ifdef CDC_ENABLED
+ CDCDescriptor cdc;
+#endif
+ HIDDescriptor hid;
+} 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_IPRODUCT[28] PROGMEM;
+extern const u16 STRING_IMANUFACTURER[12] PROGMEM;
+
+#define IMANUFACTURER 1
+#define IPRODUCT 2
+
+#define CDC_TX CDC_ENDPOINT_IN
+#define CDC_RX CDC_ENDPOINT_OUT \ No newline at end of file
diff --git a/bootloaders/nuevo_diskloader/DiskLoader.elf.hex b/bootloaders/nuevo_diskloader/DiskLoader.elf.hex
new file mode 100644
index 0000000..df5f1da
--- /dev/null
+++ b/bootloaders/nuevo_diskloader/DiskLoader.elf.hex
@@ -0,0 +1,172 @@
+:10700000BDC00000E0C00000DEC00000DCC0000029
+:10701000DAC00000D8C00000D6C00000D4C0000014
+:10702000D2C00000D0C000008AC30000FCC3000032
+:10703000CAC00000C8C00000C6C00000C4C0000034
+:10704000C2C00000C0C00000BEC00000BCC0000044
+:10705000BAC00000B8C00000B6C00000B4C0000054
+:10706000B2C00000B0C00000AEC00000ACC0000064
+:10707000AAC00000A8C00000A6C00000A4C0000074
+:10708000A2C00000A0C000009EC000009CC0000084
+:107090009AC0000098C0000096C0000094C0000094
+:1070A00092C0000090C000008EC0000011241FBEDE
+:1070B000CFEFDAE0DEBFCDBFF7C0080B000202025F
+:1070C0000100090400000102020000052400100173
+:1070D0000524010101042402060524060001070518
+:1070E000810310004009040100020A0000000705A6
+:1070F00002024000000705830240000041014214E3
+:107100004505560455026403740300001E9587026A
+:107110000110030004030904220300000000000022
+:10712000000000000000000000000000000000005F
+:107130000000000000000000000018034100720081
+:107140006400750069006E006F0020004C004C0068
+:1071500043001201000200000040412389000001A9
+:1071600001020001120100020200004041238900D7
+:1071700000010102000100C18081033C11241FBEF7
+:10718000CFEFDAE0DEBFCDBF11E0A0E0B1E0E2E892
+:10719000FAE702C005900D92A830B107D9F711E0C7
+:1071A000A8E0B1E001C01D92A539B107E1F711E7F0
+:1071B000CCE7D1E703C02297FE015DD4CA37D107DF
+:1071C000D1F772D05CC41CCFFC0180818E5F8083BC
+:1071D00080E86AEB70E742E450E063C2FC01818121
+:1071E0009081913A49F48132C1F480E060E071E02D
+:1071F00047E050E056D20FC0913279F4803231F43A
+:1072000080E091E067E070E088D205C0823229F426
+:1072100082818093070181E0089580E00895FF92C4
+:107220000F931F93CF93DF93982F8B01FF2480E759
+:107230006030780738F4FF24F39483E0FB018093F7
+:107240005700E895C42FD0E0892F68E071E0AE01C7
+:107250001AD1FF2019F107B600FCFDCFD595C795CF
+:107260004C2FF801A8E0B1E020E031E009C08D9199
+:107270009D910C0130935700E895112432962F5FB1
+:107280002417A8F385E0F80180935700E89507B626
+:1072900000FCFDCF81E180935700E895DF91CF910D
+:1072A0001F910F91FF90089588E10FB6F894809395
+:1072B0006000109260000FBE3F9A209A6E9A90E88C
+:1072C000909361001092610081E885BF95BF01E055
+:1072D00005BF02E005BF769A289A479884E991E0B5
+:1072E0007CD2789484E18093880180E18093890145
+:1072F000CC24DD2430E9A32E31E0B32E99249394DD
+:1073000002C0CC24DD2482E0B5D08823E1F382E002
+:107310000FD1E82E8CEF90E7FC0124913196029674
+:1073200044912E1511F02223B9F710928B0110927F
+:107330008A0150E082E068E071E0A5D0F1E4EF1648
+:1073400079F4609108016058633028F0683111F0D9
+:1073500064E001C063E0C62FD0E0C15FDE4810C02A
+:1073600026E5E21681F480910801803319F0FF24AC
+:10737000F39428C080910A01C82FD0E0C45FDE4892
+:10738000FF24F39427C085E7E81629F423E0F22EC2
+:10739000CCE0D1E71FC0E5E5EE1639F4C090080156
+:1073A000D0900901CC0CDD1C0CC0F4E6EF1631F4D2
+:1073B00082E0B6014091090132DF03C024E7E21602
+:1073C00021F0FF24C3E1D1E705C0F0900901E601F7
+:1073D000CF0CD11C08E813E1015010400930110511
+:1073E00009F433C082E046D0882309F02EC0F4CFE0
+:1073F000809709F086CF83E068E871E041E050E0D3
+:10740000BFD0FF2029F04F2D50E083E8BE01B8D057
+:1074100083E469E871E041E050E0B2D081E5E8162C
+:1074200009F071CF88EE93E0F5013197F1F70197FC
+:10743000D9F701E005BF00E005BF9092E00010928F
+:10744000E200EE27FF2709945ECF82E071D0EFEFD4
+:107450008F3F9E0709F0CCCFCECFFC0104C08091B6
+:10746000F100819361506623D1F7289884E68093D8
+:107470008D0108959FB7F89487708093E9008091FB
+:10748000F2009FBF0895FB019FB7F8948770809327
+:10749000E90034E62BE610C08091E80085FFFCCFC0
+:1074A000289830938D018091F10081938091E800BC
+:1074B00085FD02C02093E800415050408FEF4F3FC0
+:1074C000580751F79FBF0895282F80918E01882378
+:1074D000C1F057FF1AC015C0289890938D01809174
+:1074E000F1008193E217F307B9F74115510539F01F
+:1074F0008091F200882319F48BE68093E800AFBFF7
+:1075000002C04FEF5FEFCA010895AFB7F89427703C
+:107510002093E9008091F200282F30E024173507EE
+:107520000CF4A901FB019B01240F311D94E6DACF75
+:10753000DF93CF930F92CDB7DEB7BE016F5F7F4F62
+:1075400041E050E0C1DF019719F08FEF9FEF02C0DB
+:10755000898190E00F90CF91DF9108952FB7F89433
+:1075600087708093E9008091E80085FD02C090E07B
+:1075700004C08091F20090E4981B2FBF892F0895DA
+:107580005F926F927F928F929F92AF92BF92CF92B3
+:10759000DF92EF92FF920F931F93CF93DF93F82E1A
+:1075A0006A0180918E01882319F44FEF5FEF68C064
+:1075B0008B01EA0157E0E52EEF208F2D90E040E2AD
+:1075C000842E912C882299223AE3532E20E4A22E75
+:1075D000B12CA822B92280EA682E8FE0782E43C011
+:1075E0008F2DBCDF282F882321F4C3010197F1F7E9
+:1075F0003AC090E0C817D9070CF42C2FC21BD10950
+:107600004FB7F894E092E9008114910421F406C088
+:107610001092F10021502223D9F719C0F7FC03C0C2
+:10762000F801922F10C0C801322F06C0FC0101964C
+:10763000E491E093F10031503323C1F706C081910A
+:107640008093F10091509923D1F7020F111D809181
+:10765000E80085FF05C0209729F4A114B10411F0BA
+:107660005092E8004FBF209709F0BACFF6FE03C052
+:107670008AE38093E800769884E680938C01A601E3
+:10768000CA01DF91CF911F910F91FF90EF90DF9092
+:10769000CF90BF90AF909F908F907F906F905F90B2
+:1076A00008950F931F93182FFB01A0919101B091A2
+:1076B0009201BA010EEF2AC017FF02C0949101C0D7
+:1076C000908120918F01309190012A173B07ACF4F3
+:1076D0008091E8008570E1F38091E80082FF03C0AB
+:1076E0004FEF5FEF16C09093F100C90101968F73C1
+:1076F0009070892B11F40093E8002F5F3F4F309377
+:10770000900120938F016150704031966115710591
+:1077100099F6CA011F910F910895CF93DF939C01B1
+:10772000EB018091E80082FFFCCFC9016C2F95DE50
+:107730008BEF8093E800CE01DF91CF9108951F92E7
+:107740000F920FB60F9211242F933F938F93809136
+:10775000E1001092E100282F83FF0FC01092E90092
+:1077600081E08093EB001092EC0082E38093ED00C7
+:1077700010928E0188E08093F00022FF26C083E003
+:107780008093E9008091F200882319F08AE38093C6
+:10779000E80080918C01882351F080918C01815008
+:1077A00080938C0180918C01882309F4769A8091D2
+:1077B0008D01882351F080918D01815080938D013E
+:1077C00080918D01882309F4289A8F913F912F9100
+:1077D0000F900FBE0F901F90189510928E0181E0B0
+:1077E0008093D70080EA8093D80082E189BD09B4F4
+:1077F00000FEFDCF80E98093D8008CE08093E2000A
+:107800001092E00008950895DF93CF930F92CDB7C3
+:10781000DEB71982CE010196D7DC898190E00F9006
+:10782000CF91DF9108951F920F920FB60F921124FE
+:10783000EF92FF921F932F933F934F935F936F931A
+:107840007F938F939F93AF93BF93EF93FF93DF93B8
+:10785000CF93CDB7DEB76197DEBFCDBF1092E90001
+:107860008091E80083FFECC0CE01019668E0F5DD71
+:1078700082EF8093E800998197FF05C08091E8002E
+:1078800080FFFCCF03C08EEF8093E800292F30E00B
+:10789000C90180769070892B09F0B3C08A81882352
+:1078A00029F41092F1001092F100BFC0813009F468
+:1078B000BCC0833009F4B9C0853049F48091E80038
+:1078C00080FFFCCF8B8180688093E300AEC0863060
+:1078D00009F06DC01C81EF80F8841230A9F5109278
+:1078E000E9001092900110928F0110929201109273
+:1078F00091018ADF99E0FE013996DF01292F1D925F
+:107900002A95E9F799871A8791E09E8790E8988BE6
+:107910009AEF998B20918F0130919001275F3F4F13
+:107920003C872B878D871092E9001092900110926E
+:107930008F01F0929201E092910180E0BF0149E055
+:1079400050E0AFDE61DF71C01092E900109290014B
+:1079500010928F01F0929201E0929101113071F436
+:1079600088E0E816F10411F41093930180919301DB
+:10797000882309F062C0E2E5F1E713C0133009F093
+:1079800058C08B81882319F4E4E1F1E70AC0823002
+:1079900019F4E8E1F1E705C0813009F04AC0EAE3F3
+:1079A000F1E7449150E080E8BF017BDE3EC08730C4
+:1079B00009F43FC0883021F481E08093F10035C0A4
+:1079C000893099F523703070232B99F5E7E7F1E7BB
+:1079D00091E031E026E39093E9003093EB0084914D
+:1079E0008093EC002093ED009F5F3196943099F7DF
+:1079F0008EE78093EA001092EA008B8180938E01DB
+:107A000014C08F8198851092E90010929001109215
+:107A10008F0190939201809391018D81882349F485
+:107A2000CE010196DBDB882321F08EEF8093E80006
+:107A300007C081E28093EB0003C0E4E6F1E7B1CF39
+:107A40006196DEBFCDBFCF91DF91FF91EF91BF91E6
+:107A5000AF919F918F917F916F915F914F913F91E6
+:107A60002F911F91FF90EF900F900FBE0F901F90DE
+:107A70001895EE0FFF1F0590F491E02D0994F894EE
+:027A8000FFCF36
+:087A820000E10000000000001B
+:040000030000700089
+:00000001FF
diff --git a/bootloaders/nuevo_diskloader/DiskLoader.hex b/bootloaders/nuevo_diskloader/DiskLoader.hex
new file mode 100644
index 0000000..df5f1da
--- /dev/null
+++ b/bootloaders/nuevo_diskloader/DiskLoader.hex
@@ -0,0 +1,172 @@
+:10700000BDC00000E0C00000DEC00000DCC0000029
+:10701000DAC00000D8C00000D6C00000D4C0000014
+:10702000D2C00000D0C000008AC30000FCC3000032
+:10703000CAC00000C8C00000C6C00000C4C0000034
+:10704000C2C00000C0C00000BEC00000BCC0000044
+:10705000BAC00000B8C00000B6C00000B4C0000054
+:10706000B2C00000B0C00000AEC00000ACC0000064
+:10707000AAC00000A8C00000A6C00000A4C0000074
+:10708000A2C00000A0C000009EC000009CC0000084
+:107090009AC0000098C0000096C0000094C0000094
+:1070A00092C0000090C000008EC0000011241FBEDE
+:1070B000CFEFDAE0DEBFCDBFF7C0080B000202025F
+:1070C0000100090400000102020000052400100173
+:1070D0000524010101042402060524060001070518
+:1070E000810310004009040100020A0000000705A6
+:1070F00002024000000705830240000041014214E3
+:107100004505560455026403740300001E9587026A
+:107110000110030004030904220300000000000022
+:10712000000000000000000000000000000000005F
+:107130000000000000000000000018034100720081
+:107140006400750069006E006F0020004C004C0068
+:1071500043001201000200000040412389000001A9
+:1071600001020001120100020200004041238900D7
+:1071700000010102000100C18081033C11241FBEF7
+:10718000CFEFDAE0DEBFCDBF11E0A0E0B1E0E2E892
+:10719000FAE702C005900D92A830B107D9F711E0C7
+:1071A000A8E0B1E001C01D92A539B107E1F711E7F0
+:1071B000CCE7D1E703C02297FE015DD4CA37D107DF
+:1071C000D1F772D05CC41CCFFC0180818E5F8083BC
+:1071D00080E86AEB70E742E450E063C2FC01818121
+:1071E0009081913A49F48132C1F480E060E071E02D
+:1071F00047E050E056D20FC0913279F4803231F43A
+:1072000080E091E067E070E088D205C0823229F426
+:1072100082818093070181E0089580E00895FF92C4
+:107220000F931F93CF93DF93982F8B01FF2480E759
+:107230006030780738F4FF24F39483E0FB018093F7
+:107240005700E895C42FD0E0892F68E071E0AE01C7
+:107250001AD1FF2019F107B600FCFDCFD595C795CF
+:107260004C2FF801A8E0B1E020E031E009C08D9199
+:107270009D910C0130935700E895112432962F5FB1
+:107280002417A8F385E0F80180935700E89507B626
+:1072900000FCFDCF81E180935700E895DF91CF910D
+:1072A0001F910F91FF90089588E10FB6F894809395
+:1072B0006000109260000FBE3F9A209A6E9A90E88C
+:1072C000909361001092610081E885BF95BF01E055
+:1072D00005BF02E005BF769A289A479884E991E0B5
+:1072E0007CD2789484E18093880180E18093890145
+:1072F000CC24DD2430E9A32E31E0B32E99249394DD
+:1073000002C0CC24DD2482E0B5D08823E1F382E002
+:107310000FD1E82E8CEF90E7FC0124913196029674
+:1073200044912E1511F02223B9F710928B0110927F
+:107330008A0150E082E068E071E0A5D0F1E4EF1648
+:1073400079F4609108016058633028F0683111F0D9
+:1073500064E001C063E0C62FD0E0C15FDE4810C02A
+:1073600026E5E21681F480910801803319F0FF24AC
+:10737000F39428C080910A01C82FD0E0C45FDE4892
+:10738000FF24F39427C085E7E81629F423E0F22EC2
+:10739000CCE0D1E71FC0E5E5EE1639F4C090080156
+:1073A000D0900901CC0CDD1C0CC0F4E6EF1631F4D2
+:1073B00082E0B6014091090132DF03C024E7E21602
+:1073C00021F0FF24C3E1D1E705C0F0900901E601F7
+:1073D000CF0CD11C08E813E1015010400930110511
+:1073E00009F433C082E046D0882309F02EC0F4CFE0
+:1073F000809709F086CF83E068E871E041E050E0D3
+:10740000BFD0FF2029F04F2D50E083E8BE01B8D057
+:1074100083E469E871E041E050E0B2D081E5E8162C
+:1074200009F071CF88EE93E0F5013197F1F70197FC
+:10743000D9F701E005BF00E005BF9092E00010928F
+:10744000E200EE27FF2709945ECF82E071D0EFEFD4
+:107450008F3F9E0709F0CCCFCECFFC0104C08091B6
+:10746000F100819361506623D1F7289884E68093D8
+:107470008D0108959FB7F89487708093E9008091FB
+:10748000F2009FBF0895FB019FB7F8948770809327
+:10749000E90034E62BE610C08091E80085FFFCCFC0
+:1074A000289830938D018091F10081938091E800BC
+:1074B00085FD02C02093E800415050408FEF4F3FC0
+:1074C000580751F79FBF0895282F80918E01882378
+:1074D000C1F057FF1AC015C0289890938D01809174
+:1074E000F1008193E217F307B9F74115510539F01F
+:1074F0008091F200882319F48BE68093E800AFBFF7
+:1075000002C04FEF5FEFCA010895AFB7F89427703C
+:107510002093E9008091F200282F30E024173507EE
+:107520000CF4A901FB019B01240F311D94E6DACF75
+:10753000DF93CF930F92CDB7DEB7BE016F5F7F4F62
+:1075400041E050E0C1DF019719F08FEF9FEF02C0DB
+:10755000898190E00F90CF91DF9108952FB7F89433
+:1075600087708093E9008091E80085FD02C090E07B
+:1075700004C08091F20090E4981B2FBF892F0895DA
+:107580005F926F927F928F929F92AF92BF92CF92B3
+:10759000DF92EF92FF920F931F93CF93DF93F82E1A
+:1075A0006A0180918E01882319F44FEF5FEF68C064
+:1075B0008B01EA0157E0E52EEF208F2D90E040E2AD
+:1075C000842E912C882299223AE3532E20E4A22E75
+:1075D000B12CA822B92280EA682E8FE0782E43C011
+:1075E0008F2DBCDF282F882321F4C3010197F1F7E9
+:1075F0003AC090E0C817D9070CF42C2FC21BD10950
+:107600004FB7F894E092E9008114910421F406C088
+:107610001092F10021502223D9F719C0F7FC03C0C2
+:10762000F801922F10C0C801322F06C0FC0101964C
+:10763000E491E093F10031503323C1F706C081910A
+:107640008093F10091509923D1F7020F111D809181
+:10765000E80085FF05C0209729F4A114B10411F0BA
+:107660005092E8004FBF209709F0BACFF6FE03C052
+:107670008AE38093E800769884E680938C01A601E3
+:10768000CA01DF91CF911F910F91FF90EF90DF9092
+:10769000CF90BF90AF909F908F907F906F905F90B2
+:1076A00008950F931F93182FFB01A0919101B091A2
+:1076B0009201BA010EEF2AC017FF02C0949101C0D7
+:1076C000908120918F01309190012A173B07ACF4F3
+:1076D0008091E8008570E1F38091E80082FF03C0AB
+:1076E0004FEF5FEF16C09093F100C90101968F73C1
+:1076F0009070892B11F40093E8002F5F3F4F309377
+:10770000900120938F016150704031966115710591
+:1077100099F6CA011F910F910895CF93DF939C01B1
+:10772000EB018091E80082FFFCCFC9016C2F95DE50
+:107730008BEF8093E800CE01DF91CF9108951F92E7
+:107740000F920FB60F9211242F933F938F93809136
+:10775000E1001092E100282F83FF0FC01092E90092
+:1077600081E08093EB001092EC0082E38093ED00C7
+:1077700010928E0188E08093F00022FF26C083E003
+:107780008093E9008091F200882319F08AE38093C6
+:10779000E80080918C01882351F080918C01815008
+:1077A00080938C0180918C01882309F4769A8091D2
+:1077B0008D01882351F080918D01815080938D013E
+:1077C00080918D01882309F4289A8F913F912F9100
+:1077D0000F900FBE0F901F90189510928E0181E0B0
+:1077E0008093D70080EA8093D80082E189BD09B4F4
+:1077F00000FEFDCF80E98093D8008CE08093E2000A
+:107800001092E00008950895DF93CF930F92CDB7C3
+:10781000DEB71982CE010196D7DC898190E00F9006
+:10782000CF91DF9108951F920F920FB60F921124FE
+:10783000EF92FF921F932F933F934F935F936F931A
+:107840007F938F939F93AF93BF93EF93FF93DF93B8
+:10785000CF93CDB7DEB76197DEBFCDBF1092E90001
+:107860008091E80083FFECC0CE01019668E0F5DD71
+:1078700082EF8093E800998197FF05C08091E8002E
+:1078800080FFFCCF03C08EEF8093E800292F30E00B
+:10789000C90180769070892B09F0B3C08A81882352
+:1078A00029F41092F1001092F100BFC0813009F468
+:1078B000BCC0833009F4B9C0853049F48091E80038
+:1078C00080FFFCCF8B8180688093E300AEC0863060
+:1078D00009F06DC01C81EF80F8841230A9F5109278
+:1078E000E9001092900110928F0110929201109273
+:1078F00091018ADF99E0FE013996DF01292F1D925F
+:107900002A95E9F799871A8791E09E8790E8988BE6
+:107910009AEF998B20918F0130919001275F3F4F13
+:107920003C872B878D871092E9001092900110926E
+:107930008F01F0929201E092910180E0BF0149E055
+:1079400050E0AFDE61DF71C01092E900109290014B
+:1079500010928F01F0929201E0929101113071F436
+:1079600088E0E816F10411F41093930180919301DB
+:10797000882309F062C0E2E5F1E713C0133009F093
+:1079800058C08B81882319F4E4E1F1E70AC0823002
+:1079900019F4E8E1F1E705C0813009F04AC0EAE3F3
+:1079A000F1E7449150E080E8BF017BDE3EC08730C4
+:1079B00009F43FC0883021F481E08093F10035C0A4
+:1079C000893099F523703070232B99F5E7E7F1E7BB
+:1079D00091E031E026E39093E9003093EB0084914D
+:1079E0008093EC002093ED009F5F3196943099F7DF
+:1079F0008EE78093EA001092EA008B8180938E01DB
+:107A000014C08F8198851092E90010929001109215
+:107A10008F0190939201809391018D81882349F485
+:107A2000CE010196DBDB882321F08EEF8093E80006
+:107A300007C081E28093EB0003C0E4E6F1E7B1CF39
+:107A40006196DEBFCDBFCF91DF91FF91EF91BF91E6
+:107A5000AF919F918F917F916F915F914F913F91E6
+:107A60002F911F91FF90EF900F900FBE0F901F90DE
+:107A70001895EE0FFF1F0590F491E02D0994F894EE
+:027A8000FFCF36
+:087A820000E10000000000001B
+:040000030000700089
+:00000001FF
diff --git a/bootloaders/nuevo_diskloader/Makefile b/bootloaders/nuevo_diskloader/Makefile
new file mode 100644
index 0000000..88e0cfe
--- /dev/null
+++ b/bootloaders/nuevo_diskloader/Makefile
@@ -0,0 +1,109 @@
+###############################################################################
+# Makefile for DiskLoader
+###############################################################################
+
+## General Flags
+PROJECT = DiskLoader
+TARGET = DiskLoader.elf
+CC = avr-gcc
+
+# BOARD2
+MCU = atmega32u4
+AVR_FREQ = 16000000L
+
+# Specify the Arduino model using the assigned PID. This is used by Descriptors.c
+# to set PID and product descriptor string
+# Arduino Leonardo PID
+ARDUINO_MODEL_PID = 0x0089
+# Arduino Micro PID
+#ARDUINO_MODEL_PID = 0x0035
+
+# 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) -DARDUINO_MODEL_PID=$(ARDUINO_MODEL_PID) $(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,--cref
+#LDFLAGS += -Wl,--section-start=.text=0x7000,--relax
+#LDFLAGS += -Wl,--gc-sections
+#LDFLAGS += -nodefaultlibs -nostartfiles
+#LDFLAGS += -nostartfiles
+LDFLAGS += -Wl,-gc-sections,-Map=DiskLoader.map,--section-start=.text=0x7000,--relax
+
+
+## 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 1 -u -U flash:w:$(TARGET).hex
diff --git a/bootloaders/nuevo_diskloader/src/CDC.cpp b/bootloaders/nuevo_diskloader/src/CDC.cpp
new file mode 100644
index 0000000..cc798b4
--- /dev/null
+++ b/bootloaders/nuevo_diskloader/src/CDC.cpp
@@ -0,0 +1,173 @@
+
+
+/* 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
+
+void Reboot()
+{
+ USB.detach();
+ cli();
+ asm volatile("jmp 0x7800"); // jump to bootloader - DiskLoader takes up last 2 kB
+}
+
+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);
+ return true;
+ }
+
+ if (CDC_SET_CONTROL_LINE_STATE == r)
+ {
+// if (0 != _usbLineInfo.lineState && 1200 == _usbLineInfo.dwDTERate) // auto-reset is triggered when the port, already open at 1200 bps, is closed
+// Reboot();
+ _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)
+{
+
+ // 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/bootloaders/nuevo_diskloader/src/DiskLoader.cpp b/bootloaders/nuevo_diskloader/src/DiskLoader.cpp
new file mode 100644
index 0000000..216ddc6
--- /dev/null
+++ b/bootloaders/nuevo_diskloader/src/DiskLoader.cpp
@@ -0,0 +1,280 @@
+/*
+ * DiskLoader.cpp
+ */
+
+#include "Platform.h"
+//#include "USBCore.h"
+
+
+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" // Y-register
+ "ldi r29, 0x0A\n" // Y-register
+ "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;
+u16 do_reset = 0;
+volatile u16 _timeout;
+
+void Program(u8 ep, u16 page, u8 count)
+{
+ u8 write = page < 28*1024; // Don't write over bootloader please
+ if (write)
+ boot_page_erase(page);
+
+ USB_Recv_block(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 ();
+}
+
+#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, //
+};
+
+//int getch(void)
+//{
+// u16 timeout;
+// u8 c;
+// for (timeout = 0; timeout; timeout--)
+// {
+// c = USB_Recv(CDC_RX);
+// if (c != -1)
+// return c;
+// }
+// return -1;
+//}
+
+
+void start_sketch()
+{
+ UDCON = 1; // Detatch USB
+ UDIEN = 0;
+ asm volatile ( // Reset vector to run firmware
+ "clr r30\n"
+ "clr r31\n"
+ "ijmp\n"
+ ::);
+}
+
+int main(void) __attribute__ ((naked));
+int main()
+{
+ wdt_disable();
+ BOARD_INIT();
+ /* move interrupts to boot section:
+ * uses inline assembly because the procedure must be completed in four cycles.
+ * seems to fail if called before disabling WDT and calling BOARD_INIT()
+ */
+ asm volatile (
+ "ldi r16, 0x01\n" // (1<<IVCE) /* Enable change of interrupt vectors */
+ "out 0x35, r16\n" // MCUCR
+ "ldi r16, 0x02\n" // (1<<IVSEL) /* Move interrupts to boot flash section */
+ "out 0x35, r16\n" // MCUCR
+ );
+ TX_LED_OFF();
+ RX_LED_OFF();
+ L_LED_OFF();
+ USB.attach();
+ sei();
+
+ _inSync = STK_INSYNC;
+ _ok = STK_OK;
+
+ for (;;)
+ {
+ u8* packet = _flashbuf;
+ u16 address = 0;
+ for (;;)
+ {
+// while (!USB_Available(CDC_RX))
+// ;
+ if (USB_Available(CDC_RX))
+ {
+ u8 cmd = USB_Recv(CDC_RX);
+
+ // 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;
+ }
+ _timeout = 0;
+ // Read params
+ USB_Recv_block(CDC_RX, packet, len);
+
+ // Send a response
+ u8 send = 0;
+ const u8* pgm = _consts+7;
+ 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];
+ send = 1;
+ }
+ else if (STK_READ_SIGN == cmd)
+ {
+ pgm = _consts;
+ send = 3;
+ }
+ else if (STK_LOAD_ADDRESS == cmd)
+ {
+ address = *((u16*)packet); // word address
+ 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;
+ }
+
+ // Check sync
+ // if (Serial.available() > 0 && Serial.read() != ' ')
+ // break;
+ // if (USB_Available(CDC_RX) && USB_Recv(CDC_RX) != ' ')
+
+ // u8 countdown = 10;
+ // while (!USB_Available(CDC_RX))
+ // {
+ // if (countdown-- == 0)
+ // break;
+ // }
+ // u8 x = USB_Recv(CDC_RX);
+ // if (x != -1 && x != ' ')
+ // {
+ // L_LED_ON();
+ // break;
+ // }
+
+ // if (getch() != ' ')
+ // break;
+
+ // while (!USB_Available(CDC_RX))
+ // ;
+ //
+ // int x = USB_Recv(CDC_RX);
+ // if (x == -1)
+ // {
+ // UEINTX = 0x6B;
+ // break;
+ // }
+ // else if (x != ' ')
+ // {
+ //// UEINTX = 0x6B;
+ // break;
+ // }
+
+ u16 countdown = 5000;
+ while (countdown-- > 10 && !USB_Available(CDC_RX))
+ ;
+ int x = USB_Recv(CDC_RX);
+ if (x != -1 && x != ' ')
+ break;
+
+ USB_Send(CDC_TX, &_inSync, 1);
+
+ if (send)
+ USB_Send(CDC_TX|TRANSFER_PGM, pgm, send);
+
+ // Send ok
+ USB_Send(CDC_TX|TRANSFER_RELEASE, &_ok, 1);
+
+ if ('Q' == cmd)
+ {
+ _delay_ms(100);
+ /* move interrupts to application section:
+ * uses inline assembly because the procedure must be completed in four cycles.
+ */
+ asm volatile (
+ "ldi r16, 0x01\n" // (1<<IVCE) /* Enable change of interrupt vectors */
+ "out 0x35, r16\n" // MCUCR
+ "ldi r16, 0x00\n" // (1<<IVSEL) /* Move interrupts to application flash section */
+ "out 0x35, r16\n" // MCUCR
+ );
+ start_sketch();
+ // break;
+ }
+ }
+ }
+ }
+}
diff --git a/bootloaders/nuevo_diskloader/src/Platform.h b/bootloaders/nuevo_diskloader/src/Platform.h
new file mode 100644
index 0000000..4b58f9f
--- /dev/null
+++ b/bootloaders/nuevo_diskloader/src/Platform.h
@@ -0,0 +1,72 @@
+/*
+ * Platform.h
+ */
+
+#ifndef __PLATFORM_H__
+#define __PLATFORM_H__
+
+#include <inttypes.h>
+#include <avr/io.h>
+#include <avr/pgmspace.h>
+#include <avr/boot.h>
+#include <util/delay.h>
+#include <avr/interrupt.h>
+#include <avr/wdt.h>
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+
+#ifdef __cplusplus
+extern "C"{
+#endif
+
+typedef unsigned char u8;
+typedef unsigned short u16;
+typedef unsigned long u32;
+
+#define CDC_ENABLED
+
+#define USB_PID_LEONARDO 0x0034
+#define USB_PID_MICRO 0x0035
+#define USB_VID 0x2341 // arduino LLC vid
+#define USB_PID ARDUINO_MODEL_PID // passed in by Makefile - 0x0034 for Leonardo, 0x0035 for MIcro
+
+//#include "USBDesc.h"
+//#include "../../../cores/arduino/USBCore.h"
+
+
+#define min(a,b) ((a)<(b)?(a):(b))
+
+#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 BOARD_INIT() DDRC |= (1<<7); DDRB |= (1<<0); DDRE |= (1<<6); CPU_PRESCALE(0); DISABLE_JTAG(); // for XXX-series boards
+//#define BOARD_INIT() DDRC |= (1<<7); DDRB |= (1<<0); DDRD |= (1<<5); CPU_PRESCALE(0); DISABLE_JTAG(); // for non-XXX boards
+
+// for XXX-series boards
+#define TX_LED_OFF() PORTE |= (1<<6)
+#define TX_LED_ON() PORTE &= ~(1<<6)
+#define RX_LED_OFF() PORTB |= (1<<0)
+#define RX_LED_ON() PORTB &= ~(1<<0)
+#define L_LED_OFF() PORTC &= ~(1<<7)
+#define L_LED_ON() PORTC |= (1<<7)
+#define L_LED_TOGGLE() PORTC ^= (1<<7)
+
+// these for non-XXX boards
+//#define LED0 PORTC &= ~(1<<7)
+//#define LED1 PORTC |= (1<<7)
+//#define TXLED0 PORTD |= (1<<5)
+//#define TXLED1 PORTD &= ~(1<<5)
+//#define RXLED0 PORTB |= (1<<0)
+//#define RXLED1 PORTB &= ~(1<<0)
+
+#ifdef __cplusplus
+} /* extern "C"{ */
+#endif
+
+#include "USBDesc.h"
+#include "USBCore.h"
+#include "USBAPI.h"
+
+#endif /* __PLATFORM_H__ */
diff --git a/bootloaders/nuevo_diskloader/src/USBAPI.h b/bootloaders/nuevo_diskloader/src/USBAPI.h
new file mode 100644
index 0000000..1724ce1
--- /dev/null
+++ b/bootloaders/nuevo_diskloader/src/USBAPI.h
@@ -0,0 +1,83 @@
+
+
+#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:
+ 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;
+
+//================================================================================
+//================================================================================
+// Low level API
+
+typedef struct
+{
+ uint8_t bmRequestType;
+ uint8_t bRequest;
+ uint8_t wValueL;
+ uint8_t wValueH;
+ uint16_t wIndex;
+ uint16_t wLength;
+} Setup;
+
+//================================================================================
+//================================================================================
+// CDC '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
+void USB_Recv_block(u8 ep, u8* dst, int len); // 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/bootloaders/nuevo_diskloader/src/USBCore.cpp b/bootloaders/nuevo_diskloader/src/USBCore.cpp
new file mode 100644
index 0000000..56944e5
--- /dev/null
+++ b/bootloaders/nuevo_diskloader/src/USBCore.cpp
@@ -0,0 +1,669 @@
+
+
+/* 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
+
+/** Pulse generation counters to keep track of the number of milliseconds remaining for each pulse type */
+#define TX_RX_LED_PULSE_MS 100
+volatile u8 TxLEDPulse; /**< Milliseconds remaining for data Tx LED pulse */
+volatile u8 RxLEDPulse; /**< Milliseconds remaining for data Rx LED pulse */
+
+//==================================================================
+//==================================================================
+
+extern const u16 STRING_LANGUAGE[] PROGMEM;
+extern const u16 STRING_IPRODUCT[] PROGMEM;
+extern const u16 STRING_IMANUFACTURER[] PROGMEM;
+extern const DeviceDescriptor USB_DeviceDescriptor PROGMEM;
+extern const DeviceDescriptor USB_DeviceDescriptorA PROGMEM;
+
+const u16 STRING_LANGUAGE[2] = {
+ (3<<8) | (2+2),
+ 0x0409 // English
+};
+
+const u16 STRING_IPRODUCT[17] = {
+ (3<<8) | (2+2*16),
+#if USB_PID == USB_PID_LEONARDO
+ 'A','r','d','u','i','n','o',' ','L','e','o','n','a','r','d','o'
+#elif USB_PID == USB_PID_MICRO
+ 'A','r','d','u','i','n','o',' ','M','i','c','r','o',' ',' ',' '
+#endif
+};
+
+const u16 STRING_IMANUFACTURER[12] = {
+ (3<<8) | (2+2*11),
+ 'A','r','d','u','i','n','o',' ','L','L','C'
+};
+
+#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,IMANUFACTURER,IPRODUCT,0,1);
+
+const DeviceDescriptor USB_DeviceDescriptorA =
+ D_DEVICE(DEVICE_CLASS,0x00,0x00,64,USB_VID,USB_PID,0x100,IMANUFACTURER,IPRODUCT,0,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;
+
+ RX_LED_ON();
+ RxLEDPulse = TX_RX_LED_PULSE_MS;
+}
+
+static inline u8 Recv8()
+{
+ RX_LED_ON();
+ RxLEDPulse = TX_RX_LED_PULSE_MS;
+
+ 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();
+}
+
+void USB_Recv_block(u8 ep, u8* dst, int len)
+{
+// SetEP(ep & 7);
+ LockEP lock(ep);
+ while (len--)
+ {
+ while (!ReadWriteAllowed())
+ ;
+ *dst++ = Recv8();
+ if (!ReadWriteAllowed()) // release empty buffer
+ ReleaseRX();
+ }
+}
+
+// Non Blocking receive
+// 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);
+ _delay_ms(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();
+ }
+ }
+ if (ep & TRANSFER_RELEASE)
+ ReleaseTX();
+
+ TX_LED_ON();
+ TxLEDPulse = TX_RX_LED_PULSE_MS;
+ 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
+};
+
+#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
+
+ 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
+
+ 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;
+ else if (setup.wValueL == IPRODUCT)
+ desc_addr = (const u8*)&STRING_IPRODUCT;
+ else if (setup.wValueL == IMANUFACTURER)
+ desc_addr = (const u8*)&STRING_IMANUFACTURER;
+ else
+ return false;
+ }
+
+ 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();
+
+ 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 - happens every millisecond so we use it for TX and RX LED one-shot timing, too
+ if (udint & (1<<SOFI))
+ {
+//#ifdef CDC_ENABLED
+ USB_Flush(CDC_TX); // Send a tx frame if found
+//#endif
+
+ // check whether the one-shot period has elapsed. if so, turn off the LED
+ if (TxLEDPulse && !(--TxLEDPulse))
+ TX_LED_OFF();
+ if (RxLEDPulse && !(--RxLEDPulse))
+ RX_LED_OFF();
+ }
+}
+
+// VBUS or counting frames
+// Any frame counting?
+u8 USBConnected()
+{
+ u8 f = UDFNUML;
+// delay(3);
+ _delay_ms(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()
+{
+}
+
+#endif /* if defined(USBCON) */ \ No newline at end of file
diff --git a/bootloaders/nuevo_diskloader/src/USBCore.h b/bootloaders/nuevo_diskloader/src/USBCore.h
new file mode 100644
index 0000000..8d13806
--- /dev/null
+++ b/bootloaders/nuevo_diskloader/src/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/bootloaders/nuevo_diskloader/src/USBDesc.h b/bootloaders/nuevo_diskloader/src/USBDesc.h
new file mode 100644
index 0000000..a3f1b0f
--- /dev/null
+++ b/bootloaders/nuevo_diskloader/src/USBDesc.h
@@ -0,0 +1,65 @@
+
+
+/* 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
+
+#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
+
+#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 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
+
+#define IMANUFACTURER 1
+#define IPRODUCT 2
+#define USB_PID_LEONARDO 0x0034
+#define USB_PID_MICRO 0x0035
+#define USB_VID 0x2341 // arduino LLC vid
+//#define USB_PID ARDUINO_MODEL_USB_PID
+
diff --git a/cores/arduino/Arduino.h b/cores/arduino/Arduino.h
new file mode 100755
index 0000000..7f2fc05
--- /dev/null
+++ b/cores/arduino/Arduino.h
@@ -0,0 +1,206 @@
+#ifndef Arduino_h
+#define Arduino_h
+
+#include <stdlib.h>
+#include <string.h>
+#include <math.h>
+
+#include <avr/pgmspace.h>
+#include <avr/io.h>
+#include <avr/interrupt.h>
+
+#include "binary.h"
+
+#ifdef __cplusplus
+extern "C"{
+#endif
+
+#define HIGH 0x1
+#define LOW 0x0
+
+#define INPUT 0x0
+#define OUTPUT 0x1
+
+#define true 0x1
+#define false 0x0
+
+#define PI 3.1415926535897932384626433832795
+#define HALF_PI 1.5707963267948966192313216916398
+#define TWO_PI 6.283185307179586476925286766559
+#define DEG_TO_RAD 0.017453292519943295769236907684886
+#define RAD_TO_DEG 57.295779513082320876798154814105
+
+#define SERIAL 0x0
+#define DISPLAY 0x1
+
+#define LSBFIRST 0
+#define MSBFIRST 1
+
+#define CHANGE 1
+#define FALLING 2
+#define RISING 3
+
+#if defined(__AVR_ATmega1280__) || defined(__AVR_ATmega2560__)
+#define INTERNAL1V1 2
+#define INTERNAL2V56 3
+#else
+#define INTERNAL 3
+#endif
+#define DEFAULT 1
+#define EXTERNAL 0
+
+// undefine stdlib's abs if encountered
+#ifdef abs
+#undef abs
+#endif
+
+#define min(a,b) ((a)<(b)?(a):(b))
+#define max(a,b) ((a)>(b)?(a):(b))
+#define abs(x) ((x)>0?(x):-(x))
+#define constrain(amt,low,high) ((amt)<(low)?(low):((amt)>(high)?(high):(amt)))
+#define round(x) ((x)>=0?(long)((x)+0.5):(long)((x)-0.5))
+#define radians(deg) ((deg)*DEG_TO_RAD)
+#define degrees(rad) ((rad)*RAD_TO_DEG)
+#define sq(x) ((x)*(x))
+
+#define interrupts() sei()
+#define noInterrupts() cli()
+
+#define clockCyclesPerMicrosecond() ( F_CPU / 1000000L )
+#define clockCyclesToMicroseconds(a) ( ((a) * 1000L) / (F_CPU / 1000L) )
+#define microsecondsToClockCycles(a) ( ((a) * (F_CPU / 1000L)) / 1000L )
+
+#define lowByte(w) ((uint8_t) ((w) & 0xff))
+#define highByte(w) ((uint8_t) ((w) >> 8))
+
+#define bitRead(value, bit) (((value) >> (bit)) & 0x01)
+#define bitSet(value, bit) ((value) |= (1UL << (bit)))
+#define bitClear(value, bit) ((value) &= ~(1UL << (bit)))
+#define bitWrite(value, bit, bitvalue) (bitvalue ? bitSet(value, bit) : bitClear(value, bit))
+
+
+typedef unsigned int word;
+
+#define bit(b) (1UL << (b))
+
+typedef uint8_t boolean;
+typedef uint8_t byte;
+
+void init(void);
+
+void pinMode(uint8_t, uint8_t);
+void digitalWrite(uint8_t, uint8_t);
+int digitalRead(uint8_t);
+int analogRead(uint8_t);
+void analogReference(uint8_t mode);
+void analogWrite(uint8_t, int);
+
+unsigned long millis(void);
+unsigned long micros(void);
+void delay(unsigned long);
+void delayMicroseconds(unsigned int us);
+unsigned long pulseIn(uint8_t pin, uint8_t state, unsigned long timeout);
+
+void shiftOut(uint8_t dataPin, uint8_t clockPin, uint8_t bitOrder, uint8_t val);
+uint8_t shiftIn(uint8_t dataPin, uint8_t clockPin, uint8_t bitOrder);
+
+void attachInterrupt(uint8_t, void (*)(void), int mode);
+void detachInterrupt(uint8_t);
+
+void setup(void);
+void loop(void);
+
+// Get the bit location within the hardware port of the given virtual pin.
+// This comes from the pins_*.c file for the active board configuration.
+
+#define analogInPinToBit(P) (P)
+
+// On the ATmega1280, the addresses of some of the port registers are
+// greater than 255, so we can't store them in uint8_t's.
+extern const uint16_t PROGMEM port_to_mode_PGM[];
+extern const uint16_t PROGMEM port_to_input_PGM[];
+extern const uint16_t PROGMEM port_to_output_PGM[];
+
+extern const uint8_t PROGMEM digital_pin_to_port_PGM[];
+// extern const uint8_t PROGMEM digital_pin_to_bit_PGM[];
+extern const uint8_t PROGMEM digital_pin_to_bit_mask_PGM[];
+extern const uint8_t PROGMEM digital_pin_to_timer_PGM[];
+
+// Get the bit location within the hardware port of the given virtual pin.
+// This comes from the pins_*.c file for the active board configuration.
+//
+// These perform slightly better as macros compared to inline functions
+//
+#define digitalPinToPort(P) ( pgm_read_byte( digital_pin_to_port_PGM + (P) ) )
+#define digitalPinToBitMask(P) ( pgm_read_byte( digital_pin_to_bit_mask_PGM + (P) ) )
+#define digitalPinToTimer(P) ( pgm_read_byte( digital_pin_to_timer_PGM + (P) ) )
+#define analogInPinToBit(P) (P)
+#define portOutputRegister(P) ( (volatile uint8_t *)( pgm_read_word( port_to_output_PGM + (P))) )
+#define portInputRegister(P) ( (volatile uint8_t *)( pgm_read_word( port_to_input_PGM + (P))) )
+#define portModeRegister(P) ( (volatile uint8_t *)( pgm_read_word( port_to_mode_PGM + (P))) )
+
+#define NOT_A_PIN 0
+#define NOT_A_PORT 0
+
+#define PA 1
+#define PB 2
+#define PC 3
+#define PD 4
+#define PE 5
+#define PF 6
+#define PG 7
+#define PH 8
+#define PJ 10
+#define PK 11
+#define PL 12
+
+#define NOT_ON_TIMER 0
+#define TIMER0A 1
+#define TIMER0B 2
+#define TIMER1A 3
+#define TIMER1B 4
+#define TIMER2 5
+#define TIMER2A 6
+#define TIMER2B 7
+
+#define TIMER3A 8
+#define TIMER3B 9
+#define TIMER3C 10
+#define TIMER4A 11
+#define TIMER4B 12
+#define TIMER4C 13
+#define TIMER4D 14
+#define TIMER5A 15
+#define TIMER5B 16
+#define TIMER5C 17
+
+#ifdef __cplusplus
+} // extern "C"
+#endif
+
+#ifdef __cplusplus
+#include "WCharacter.h"
+#include "WString.h"
+#include "HardwareSerial.h"
+
+uint16_t makeWord(uint16_t w);
+uint16_t makeWord(byte h, byte l);
+
+#define word(...) makeWord(__VA_ARGS__)
+
+unsigned long pulseIn(uint8_t pin, uint8_t state, unsigned long timeout = 1000000L);
+
+void tone(uint8_t _pin, unsigned int frequency, unsigned long duration = 0);
+void noTone(uint8_t _pin);
+
+// WMath prototypes
+long random(long);
+long random(long, long);
+void randomSeed(unsigned int);
+long map(long, long, long, long, long);
+
+#endif
+
+#include "pins_arduino.h"
+
+#endif \ No newline at end of file
diff --git a/cores/arduino/CDC.cpp b/cores/arduino/CDC.cpp
new file mode 100644
index 0000000..14a0eae
--- /dev/null
+++ b/cores/arduino/CDC.cpp
@@ -0,0 +1,175 @@
+
+
+/* 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
+
+void Reboot()
+{
+ USB.detach();
+ cli();
+ asm volatile("jmp 0x7800"); // jump to bootloader - DiskLoader takes up last 2 kB
+}
+
+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);
+ return true;
+ }
+
+ if (CDC_SET_CONTROL_LINE_STATE == r)
+ {
+ if (0 != _usbLineInfo.lineState && 1200 == _usbLineInfo.dwDTERate) // auto-reset is triggered when the port, already open at 1200 bps, is closed
+ Reboot();
+ _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/Client.h b/cores/arduino/Client.h
new file mode 100644
index 0000000..ea13483
--- /dev/null
+++ b/cores/arduino/Client.h
@@ -0,0 +1,26 @@
+#ifndef client_h
+#define client_h
+#include "Print.h"
+#include "Stream.h"
+#include "IPAddress.h"
+
+class Client : public Stream {
+
+public:
+ virtual int connect(IPAddress ip, uint16_t port) =0;
+ virtual int connect(const char *host, uint16_t port) =0;
+ virtual size_t write(uint8_t) =0;
+ virtual size_t write(const uint8_t *buf, size_t size) =0;
+ virtual int available() = 0;
+ virtual int read() = 0;
+ virtual int read(uint8_t *buf, size_t size) = 0;
+ virtual int peek() = 0;
+ virtual void flush() = 0;
+ virtual void stop() = 0;
+ virtual uint8_t connected() = 0;
+ virtual operator bool() = 0;
+protected:
+ uint8_t* rawIPAddress(IPAddress& addr) { return addr.raw_address(); };
+};
+
+#endif
diff --git a/cores/arduino/HID.cpp b/cores/arduino/HID.cpp
new file mode 100644
index 0000000..8ed1566
--- /dev/null
+++ b/cores/arduino/HID.cpp
@@ -0,0 +1,446 @@
+
+
+/* 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);
+}
+
+bool Mouse_::isPressed(uint8_t b)
+{
+ if (b & _buttons > 0)
+ return true;
+ return false;
+}
+
+//================================================================================
+//================================================================================
+// 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 b5dba1e..1b1fa71 100644
--- a/cores/arduino/HardwareSerial.cpp
+++ b/cores/arduino/HardwareSerial.cpp
@@ -24,7 +24,7 @@
#include <stdio.h>
#include <string.h>
#include <inttypes.h>
-#include "wiring.h"
+#include "Arduino.h"
#include "wiring_private.h"
// this next line disables the entire HardwareSerial.cpp,
@@ -34,109 +34,96 @@
#include "HardwareSerial.h"
// Define constants and variables for buffering incoming serial data. We're
-// using a ring buffer (I think), in which rx_buffer_head is the index of the
-// location to which to write the next incoming character and rx_buffer_tail
-// is the index of the location from which to read.
+// using a ring buffer (I think), in which head is the index of the location
+// to which to write the next incoming character and tail is the index of the
+// location from which to read.
#if (RAMEND < 1000)
- #define RX_BUFFER_SIZE 32
+ #define SERIAL_BUFFER_SIZE 16
#else
- #define RX_BUFFER_SIZE 128
+ #define SERIAL_BUFFER_SIZE 64
#endif
struct ring_buffer
{
- unsigned char buffer[RX_BUFFER_SIZE];
- int head;
- int tail;
+ unsigned char buffer[SERIAL_BUFFER_SIZE];
+ volatile int head;
+ 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 };
#endif
#if defined(UBRR1H)
ring_buffer rx_buffer1 = { { 0 }, 0, 0 };
+ ring_buffer tx_buffer1 = { { 0 }, 0, 0 };
#endif
#if defined(UBRR2H)
ring_buffer rx_buffer2 = { { 0 }, 0, 0 };
+ ring_buffer tx_buffer2 = { { 0 }, 0, 0 };
#endif
#if defined(UBRR3H)
ring_buffer rx_buffer3 = { { 0 }, 0, 0 };
+ ring_buffer tx_buffer3 = { { 0 }, 0, 0 };
#endif
-inline void store_char(unsigned char c, ring_buffer *rx_buffer)
+inline void store_char(unsigned char c, ring_buffer *buffer)
{
- int i = (unsigned int)(rx_buffer->head + 1) % RX_BUFFER_SIZE;
+ int i = (unsigned int)(buffer->head + 1) % SERIAL_BUFFER_SIZE;
// if we should be storing the received character into the location
// just before the tail (meaning that the head would advance to the
// current location of the tail), we're about to overflow the buffer
// and so we don't write the character or advance the head.
- if (i != rx_buffer->tail) {
- rx_buffer->buffer[rx_buffer->head] = c;
- rx_buffer->head = i;
+ if (i != buffer->tail) {
+ buffer->buffer[buffer->head] = c;
+ buffer->head = i;
}
}
+#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)
+ #error "Don't know what the Data Received vector is called for the first UART"
+#else
+ void serialEvent() __attribute__((weak));
+ void serialEvent() {}
+ #define serialEvent_implemented
#if defined(USART_RX_vect)
SIGNAL(USART_RX_vect)
- {
- #if defined(UDR0)
- unsigned char c = UDR0;
- #elif defined(UDR)
- unsigned char c = UDR; // atmega8535
- #else
- #error UDR not defined
- #endif
- store_char(c, &rx_buffer);
- }
-#elif defined(SIG_USART0_RECV) && defined(UDR0)
+#elif defined(SIG_USART0_RECV)
SIGNAL(SIG_USART0_RECV)
- {
- unsigned char c = UDR0;
- store_char(c, &rx_buffer);
- }
-#elif defined(SIG_UART0_RECV) && defined(UDR0)
+#elif defined(SIG_UART0_RECV)
SIGNAL(SIG_UART0_RECV)
- {
- unsigned char c = UDR0;
- store_char(c, &rx_buffer);
- }
-//#elif defined(SIG_USART_RECV)
#elif defined(USART0_RX_vect)
- // fixed by Mark Sproul this is on the 644/644p
- //SIGNAL(SIG_USART_RECV)
SIGNAL(USART0_RX_vect)
+#elif defined(SIG_UART_RECV)
+ SIGNAL(SIG_UART_RECV)
+#endif
{
#if defined(UDR0)
unsigned char c = UDR0;
#elif defined(UDR)
- unsigned char c = UDR; // atmega8, atmega32
+ unsigned char c = UDR;
#else
#error UDR not defined
#endif
store_char(c, &rx_buffer);
}
-#elif defined(SIG_UART_RECV)
- // this is for atmega8
- SIGNAL(SIG_UART_RECV)
- {
- #if defined(UDR0)
- unsigned char c = UDR0; // atmega645
- #elif defined(UDR)
- unsigned char c = UDR; // atmega8
- #endif
- store_char(c, &rx_buffer);
- }
-#elif defined(USBCON)
- #warning No interrupt handler for usart 0
- #warning Serial(0) is on USB interface
-#else
- #error No interrupt handler for usart 0
+#endif
#endif
-//#if defined(SIG_USART1_RECV)
#if defined(USART1_RX_vect)
- //SIGNAL(SIG_USART1_RECV)
+ void serialEvent1() __attribute__((weak));
+ void serialEvent1() {}
+ #define serialEvent1_implemented
SIGNAL(USART1_RX_vect)
{
unsigned char c = UDR1;
@@ -147,6 +134,9 @@ inline void store_char(unsigned char c, ring_buffer *rx_buffer)
#endif
#if defined(USART2_RX_vect) && defined(UDR2)
+ void serialEvent2() __attribute__((weak));
+ void serialEvent2() {}
+ #define serialEvent2_implemented
SIGNAL(USART2_RX_vect)
{
unsigned char c = UDR2;
@@ -157,6 +147,9 @@ inline void store_char(unsigned char c, ring_buffer *rx_buffer)
#endif
#if defined(USART3_RX_vect) && defined(UDR3)
+ void serialEvent3() __attribute__((weak));
+ void serialEvent3() {}
+ #define serialEvent3_implemented
SIGNAL(USART3_RX_vect)
{
unsigned char c = UDR3;
@@ -166,17 +159,126 @@ inline void store_char(unsigned char c, ring_buffer *rx_buffer)
#error SIG_USART3_RECV
#endif
+void serialEventRun(void)
+{
+#ifdef serialEvent_implemented
+ if (Serial.available()) serialEvent();
+#endif
+#ifdef serialEvent1_implemented
+ if (Serial1.available()) serialEvent1();
+#endif
+#ifdef serialEvent2_implemented
+ if (Serial2.available()) serialEvent2();
+#endif
+#ifdef serialEvent3_implemented
+ if (Serial3.available()) serialEvent3();
+#endif
+}
+
+
+#if !defined(USART0_UDRE_vect) && defined(USART1_UDRE_vect)
+// do nothing - on the 32u4 the first USART is USART1
+#else
+#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
+#if defined(UART0_UDRE_vect)
+ISR(UART0_UDRE_vect)
+#elif defined(UART_UDRE_vect)
+ISR(UART_UDRE_vect)
+#elif defined(USART0_UDRE_vect)
+ISR(USART0_UDRE_vect)
+#elif defined(USART_UDRE_vect)
+ISR(USART_UDRE_vect)
+#endif
+{
+ if (tx_buffer.head == tx_buffer.tail) {
+ // Buffer empty, so disable interrupts
+#if defined(UCSR0B)
+ cbi(UCSR0B, UDRIE0);
+#else
+ cbi(UCSRB, UDRIE);
+#endif
+ }
+ else {
+ // There is more data in the output buffer. Send the next byte
+ unsigned char c = tx_buffer.buffer[tx_buffer.tail];
+ tx_buffer.tail = (tx_buffer.tail + 1) % SERIAL_BUFFER_SIZE;
+
+ #if defined(UDR0)
+ UDR0 = c;
+ #elif defined(UDR)
+ UDR = c;
+ #else
+ #error UDR not defined
+ #endif
+ }
+}
+#endif
+#endif
+
+#ifdef USART1_UDRE_vect
+ISR(USART1_UDRE_vect)
+{
+ if (tx_buffer1.head == tx_buffer1.tail) {
+ // Buffer empty, so disable interrupts
+ cbi(UCSR1B, UDRIE1);
+ }
+ else {
+ // There is more data in the output buffer. Send the next byte
+ unsigned char c = tx_buffer1.buffer[tx_buffer1.tail];
+ tx_buffer1.tail = (tx_buffer1.tail + 1) % SERIAL_BUFFER_SIZE;
+
+ UDR1 = c;
+ }
+}
+#endif
+
+#ifdef USART2_UDRE_vect
+ISR(USART2_UDRE_vect)
+{
+ if (tx_buffer2.head == tx_buffer2.tail) {
+ // Buffer empty, so disable interrupts
+ cbi(UCSR2B, UDRIE2);
+ }
+ else {
+ // There is more data in the output buffer. Send the next byte
+ unsigned char c = tx_buffer2.buffer[tx_buffer2.tail];
+ tx_buffer2.tail = (tx_buffer2.tail + 1) % SERIAL_BUFFER_SIZE;
+
+ UDR2 = c;
+ }
+}
+#endif
+
+#ifdef USART3_UDRE_vect
+ISR(USART3_UDRE_vect)
+{
+ if (tx_buffer3.head == tx_buffer3.tail) {
+ // Buffer empty, so disable interrupts
+ cbi(UCSR3B, UDRIE3);
+ }
+ else {
+ // There is more data in the output buffer. Send the next byte
+ unsigned char c = tx_buffer3.buffer[tx_buffer3.tail];
+ tx_buffer3.tail = (tx_buffer3.tail + 1) % SERIAL_BUFFER_SIZE;
+
+ UDR3 = c;
+ }
+}
+#endif
// Constructors ////////////////////////////////////////////////////////////////
-HardwareSerial::HardwareSerial(ring_buffer *rx_buffer,
+HardwareSerial::HardwareSerial(ring_buffer *rx_buffer, ring_buffer *tx_buffer,
volatile uint8_t *ubrrh, volatile uint8_t *ubrrl,
volatile uint8_t *ucsra, volatile uint8_t *ucsrb,
volatile uint8_t *udr,
- uint8_t rxen, uint8_t txen, uint8_t rxcie, uint8_t udre, uint8_t u2x)
+ uint8_t rxen, uint8_t txen, uint8_t rxcie, uint8_t udrie, uint8_t u2x)
{
_rx_buffer = rx_buffer;
+ _tx_buffer = tx_buffer;
_ubrrh = ubrrh;
_ubrrl = ubrrl;
_ucsra = ucsra;
@@ -185,7 +287,7 @@ HardwareSerial::HardwareSerial(ring_buffer *rx_buffer,
_rxen = rxen;
_txen = txen;
_rxcie = rxcie;
- _udre = udre;
+ _udrie = udrie;
_u2x = u2x;
}
@@ -228,18 +330,27 @@ try_again:
sbi(*_ucsrb, _rxen);
sbi(*_ucsrb, _txen);
sbi(*_ucsrb, _rxcie);
+ cbi(*_ucsrb, _udrie);
}
void HardwareSerial::end()
{
+ // wait for transmission of outgoing data
+ while (_tx_buffer->head != _tx_buffer->tail)
+ ;
+
cbi(*_ucsrb, _rxen);
cbi(*_ucsrb, _txen);
cbi(*_ucsrb, _rxcie);
+ cbi(*_ucsrb, _udrie);
+
+ // clear any received data
+ _rx_buffer->head = _rx_buffer->tail;
}
int HardwareSerial::available(void)
{
- return (unsigned int)(RX_BUFFER_SIZE + _rx_buffer->head - _rx_buffer->tail) % RX_BUFFER_SIZE;
+ return (unsigned int)(SERIAL_BUFFER_SIZE + _rx_buffer->head - _rx_buffer->tail) % SERIAL_BUFFER_SIZE;
}
int HardwareSerial::peek(void)
@@ -258,53 +369,55 @@ int HardwareSerial::read(void)
return -1;
} else {
unsigned char c = _rx_buffer->buffer[_rx_buffer->tail];
- _rx_buffer->tail = (unsigned int)(_rx_buffer->tail + 1) % RX_BUFFER_SIZE;
+ _rx_buffer->tail = (unsigned int)(_rx_buffer->tail + 1) % SERIAL_BUFFER_SIZE;
return c;
}
}
void HardwareSerial::flush()
{
- // don't reverse this or there may be problems if the RX interrupt
- // occurs after reading the value of rx_buffer_head but before writing
- // the value to rx_buffer_tail; the previous value of rx_buffer_head
- // may be written to rx_buffer_tail, making it appear as if the buffer
- // don't reverse this or there may be problems if the RX interrupt
- // occurs after reading the value of rx_buffer_head but before writing
- // the value to rx_buffer_tail; the previous value of rx_buffer_head
- // may be written to rx_buffer_tail, making it appear as if the buffer
- // were full, not empty.
- _rx_buffer->head = _rx_buffer->tail;
+ while (_tx_buffer->head != _tx_buffer->tail)
+ ;
}
-void HardwareSerial::write(uint8_t c)
+size_t HardwareSerial::write(uint8_t c)
{
- while (!((*_ucsra) & (1 << _udre)))
+ int i = (_tx_buffer->head + 1) % SERIAL_BUFFER_SIZE;
+
+ // If the output buffer is full, there's nothing for it other than to
+ // wait for the interrupt handler to empty it a bit
+ // ???: return 0 here instead?
+ while (i == _tx_buffer->tail)
;
-
- *_udr = c;
+
+ _tx_buffer->buffer[_tx_buffer->head] = c;
+ _tx_buffer->head = i;
+
+ sbi(*_ucsrb, _udrie);
+
+ return 1;
}
// Preinstantiate Objects //////////////////////////////////////////////////////
#if defined(UBRRH) && defined(UBRRL)
- HardwareSerial Serial(&rx_buffer, &UBRRH, &UBRRL, &UCSRA, &UCSRB, &UDR, RXEN, TXEN, RXCIE, UDRE, U2X);
+ HardwareSerial Serial(&rx_buffer, &tx_buffer, &UBRRH, &UBRRL, &UCSRA, &UCSRB, &UDR, RXEN, TXEN, RXCIE, UDRIE, U2X);
#elif defined(UBRR0H) && defined(UBRR0L)
- HardwareSerial Serial(&rx_buffer, &UBRR0H, &UBRR0L, &UCSR0A, &UCSR0B, &UDR0, RXEN0, TXEN0, RXCIE0, UDRE0, U2X0);
+ 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
#if defined(UBRR1H)
- HardwareSerial Serial1(&rx_buffer1, &UBRR1H, &UBRR1L, &UCSR1A, &UCSR1B, &UDR1, RXEN1, TXEN1, RXCIE1, UDRE1, U2X1);
+ HardwareSerial Serial1(&rx_buffer1, &tx_buffer1, &UBRR1H, &UBRR1L, &UCSR1A, &UCSR1B, &UDR1, RXEN1, TXEN1, RXCIE1, UDRIE1, U2X1);
#endif
#if defined(UBRR2H)
- HardwareSerial Serial2(&rx_buffer2, &UBRR2H, &UBRR2L, &UCSR2A, &UCSR2B, &UDR2, RXEN2, TXEN2, RXCIE2, UDRE2, U2X2);
+ HardwareSerial Serial2(&rx_buffer2, &tx_buffer2, &UBRR2H, &UBRR2L, &UCSR2A, &UCSR2B, &UDR2, RXEN2, TXEN2, RXCIE2, UDRIE2, U2X2);
#endif
#if defined(UBRR3H)
- HardwareSerial Serial3(&rx_buffer3, &UBRR3H, &UBRR3L, &UCSR3A, &UCSR3B, &UDR3, RXEN3, TXEN3, RXCIE3, UDRE3, U2X3);
+ HardwareSerial Serial3(&rx_buffer3, &tx_buffer3, &UBRR3H, &UBRR3L, &UCSR3A, &UCSR3B, &UDR3, RXEN3, TXEN3, RXCIE3, UDRIE3, U2X3);
#endif
#endif // whole file
diff --git a/cores/arduino/IPAddress.cpp b/cores/arduino/IPAddress.cpp
new file mode 100644
index 0000000..fe3deb7
--- /dev/null
+++ b/cores/arduino/IPAddress.cpp
@@ -0,0 +1,56 @@
+
+#include <Arduino.h>
+#include <IPAddress.h>
+
+IPAddress::IPAddress()
+{
+ memset(_address, 0, sizeof(_address));
+}
+
+IPAddress::IPAddress(uint8_t first_octet, uint8_t second_octet, uint8_t third_octet, uint8_t fourth_octet)
+{
+ _address[0] = first_octet;
+ _address[1] = second_octet;
+ _address[2] = third_octet;
+ _address[3] = fourth_octet;
+}
+
+IPAddress::IPAddress(uint32_t address)
+{
+ memcpy(_address, &address, sizeof(_address));
+}
+
+IPAddress::IPAddress(const uint8_t *address)
+{
+ memcpy(_address, address, sizeof(_address));
+}
+
+IPAddress& IPAddress::operator=(const uint8_t *address)
+{
+ memcpy(_address, address, sizeof(_address));
+ return *this;
+}
+
+IPAddress& IPAddress::operator=(uint32_t address)
+{
+ memcpy(_address, (const uint8_t *)&address, sizeof(_address));
+ return *this;
+}
+
+bool IPAddress::operator==(const uint8_t* addr)
+{
+ return memcmp(addr, _address, sizeof(_address)) == 0;
+}
+
+size_t IPAddress::printTo(Print& p) const
+{
+ size_t n = 0;
+ for (int i =0; i < 3; i++)
+ {
+ n += p.print(_address[i], DEC);
+ n += p.print('.');
+ }
+ n += p.print(_address[3], DEC);
+ return n;
+}
+
diff --git a/cores/arduino/IPAddress.h b/cores/arduino/IPAddress.h
new file mode 100644
index 0000000..2585aec
--- /dev/null
+++ b/cores/arduino/IPAddress.h
@@ -0,0 +1,76 @@
+/*
+ *
+ * MIT License:
+ * Copyright (c) 2011 Adrian McEwen
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ * THE SOFTWARE.
+ *
+ * adrianm@mcqn.com 1/1/2011
+ */
+
+#ifndef IPAddress_h
+#define IPAddress_h
+
+#include <Printable.h>
+
+// A class to make it easier to handle and pass around IP addresses
+
+class IPAddress : public Printable {
+private:
+ uint8_t _address[4]; // IPv4 address
+ // Access the raw byte array containing the address. Because this returns a pointer
+ // to the internal structure rather than a copy of the address this function should only
+ // be used when you know that the usage of the returned uint8_t* will be transient and not
+ // stored.
+ uint8_t* raw_address() { return _address; };
+
+public:
+ // Constructors
+ IPAddress();
+ IPAddress(uint8_t first_octet, uint8_t second_octet, uint8_t third_octet, uint8_t fourth_octet);
+ IPAddress(uint32_t address);
+ IPAddress(const uint8_t *address);
+
+ // Overloaded cast operator to allow IPAddress objects to be used where a pointer
+ // to a four-byte uint8_t array is expected
+ operator uint32_t() { return *((uint32_t*)_address); };
+ bool operator==(const IPAddress& addr) { return (*((uint32_t*)_address)) == (*((uint32_t*)addr._address)); };
+ bool operator==(const uint8_t* addr);
+
+ // Overloaded index operator to allow getting and setting individual octets of the address
+ uint8_t operator[](int index) const { return _address[index]; };
+ uint8_t& operator[](int index) { return _address[index]; };
+
+ // Overloaded copy operators to allow initialisation of IPAddress objects from other types
+ IPAddress& operator=(const uint8_t *address);
+ IPAddress& operator=(uint32_t address);
+
+ virtual size_t printTo(Print& p) const;
+
+ friend class EthernetClass;
+ friend class UDP;
+ friend class Client;
+ friend class Server;
+ friend class DhcpClass;
+ friend class DNSClient;
+};
+
+const IPAddress INADDR_NONE(0,0,0,0);
+
+
+#endif
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/Print.h b/cores/arduino/Print.h
index b092ae5..1af6b72 100755
--- a/cores/arduino/Print.h
+++ b/cores/arduino/Print.h
@@ -24,43 +24,55 @@
#include <stdio.h> // for size_t
#include "WString.h"
+#include "Printable.h"
#define DEC 10
#define HEX 16
#define OCT 8
#define BIN 2
-#define BYTE 0
class Print
{
private:
- void printNumber(unsigned long, uint8_t);
- void printFloat(double, uint8_t);
+ int write_error;
+ size_t printNumber(unsigned long, uint8_t);
+ size_t printFloat(double, uint8_t);
+ protected:
+ void setWriteError(int err = 1) { write_error = err; }
public:
- virtual void write(uint8_t) = 0;
- virtual void write(const char *str);
- virtual void write(const uint8_t *buffer, size_t size);
+ Print() : write_error(0) {}
+
+ int getWriteError() { return write_error; }
+ void clearWriteError() { setWriteError(0); }
+
+ virtual size_t write(uint8_t) = 0;
+ size_t write(const char *str) { return write((const uint8_t *)str, strlen(str)); }
+ virtual size_t write(const uint8_t *buffer, size_t size);
- void print(const String &);
- void print(const char[]);
- void print(char, int = BYTE);
- void print(unsigned char, int = BYTE);
- void print(int, int = DEC);
- void print(unsigned int, int = DEC);
- void print(long, int = DEC);
- void print(unsigned long, int = DEC);
- void print(double, int = 2);
+ size_t print(const __FlashStringHelper *);
+ size_t print(const String &);
+ size_t print(const char[]);
+ size_t print(char);
+ size_t print(unsigned char, int = DEC);
+ size_t print(int, int = DEC);
+ size_t print(unsigned int, int = DEC);
+ size_t print(long, int = DEC);
+ size_t print(unsigned long, int = DEC);
+ size_t print(double, int = 2);
+ size_t print(const Printable&);
- void println(const String &s);
- void println(const char[]);
- void println(char, int = BYTE);
- void println(unsigned char, int = BYTE);
- void println(int, int = DEC);
- void println(unsigned int, int = DEC);
- void println(long, int = DEC);
- void println(unsigned long, int = DEC);
- void println(double, int = 2);
- void println(void);
+ size_t println(const __FlashStringHelper *);
+ size_t println(const String &s);
+ size_t println(const char[]);
+ size_t println(char);
+ size_t println(unsigned char, int = DEC);
+ size_t println(int, int = DEC);
+ size_t println(unsigned int, int = DEC);
+ size_t println(long, int = DEC);
+ size_t println(unsigned long, int = DEC);
+ size_t println(double, int = 2);
+ size_t println(const Printable&);
+ size_t println(void);
};
#endif
diff --git a/cores/arduino/Printable.h b/cores/arduino/Printable.h
new file mode 100644
index 0000000..d03c9af
--- /dev/null
+++ b/cores/arduino/Printable.h
@@ -0,0 +1,40 @@
+/*
+ Printable.h - Interface class that allows printing of complex types
+ Copyright (c) 2011 Adrian McEwen. All right reserved.
+
+ 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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
+*/
+
+#ifndef Printable_h
+#define Printable_h
+
+#include <new.h>
+
+class Print;
+
+/** The Printable class provides a way for new classes to allow themselves to be printed.
+ By deriving from Printable and implementing the printTo method, it will then be possible
+ for users to print out instances of this class by passing them into the usual
+ Print::print and Print::println methods.
+*/
+
+class Printable
+{
+ public:
+ virtual size_t printTo(Print& p) const = 0;
+};
+
+#endif
+
diff --git a/cores/arduino/Server.h b/cores/arduino/Server.h
new file mode 100644
index 0000000..9674c76
--- /dev/null
+++ b/cores/arduino/Server.h
@@ -0,0 +1,9 @@
+#ifndef server_h
+#define server_h
+
+class Server : public Print {
+public:
+ virtual void begin() =0;
+};
+
+#endif
diff --git a/cores/arduino/Stream.cpp b/cores/arduino/Stream.cpp
new file mode 100644
index 0000000..a7d9d38
--- /dev/null
+++ b/cores/arduino/Stream.cpp
@@ -0,0 +1,243 @@
+/*
+ Stream.cpp - adds parsing methods to Stream class
+ Copyright (c) 2008 David A. Mellis. All right reserved.
+
+ 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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
+
+ Created July 2011
+ parsing functions based on TextFinder library by Michael Margolis
+ */
+
+#include "Arduino.h"
+#include "Stream.h"
+
+#define PARSE_TIMEOUT 1000 // default number of milli-seconds to wait
+#define NO_SKIP_CHAR 1 // a magic char not found in a valid ASCII numeric field
+
+// private method to read stream with timeout
+int Stream::timedRead()
+{
+ int c;
+ _startMillis = millis();
+ do {
+ c = read();
+ if (c >= 0) return c;
+ } while(millis() - _startMillis < _timeout);
+ return -1; // -1 indicates timeout
+}
+
+// private method to peek stream with timeout
+int Stream::timedPeek()
+{
+ int c;
+ _startMillis = millis();
+ do {
+ c = peek();
+ if (c >= 0) return c;
+ } while(millis() - _startMillis < _timeout);
+ return -1; // -1 indicates timeout
+}
+
+// returns peek of the next digit in the stream or -1 if timeout
+// discards non-numeric characters
+int Stream::peekNextDigit()
+{
+ int c;
+ while (1) {
+ c = timedPeek();
+ if (c < 0) return c; // timeout
+ if (c == '-') return c;
+ if (c >= '0' && c <= '9') return c;
+ read(); // discard non-numeric
+ }
+}
+
+// Public Methods
+//////////////////////////////////////////////////////////////
+
+void Stream::setTimeout(unsigned long timeout) // sets the maximum number of milliseconds to wait
+{
+ _timeout = timeout;
+}
+
+ // find returns true if the target string is found
+bool Stream::find(char *target)
+{
+ return findUntil(target, NULL);
+}
+
+// reads data from the stream until the target string of given length is found
+// returns true if target string is found, false if timed out
+bool Stream::find(char *target, size_t length)
+{
+ return findUntil(target, length, NULL, 0);
+}
+
+// as find but search ends if the terminator string is found
+bool Stream::findUntil(char *target, char *terminator)
+{
+ return findUntil(target, strlen(target), terminator, strlen(terminator));
+}
+
+// reads data from the stream until the target string of the given length is found
+// search terminated if the terminator string is found
+// returns true if target string is found, false if terminated or timed out
+bool Stream::findUntil(char *target, size_t targetLen, char *terminator, size_t termLen)
+{
+ size_t index = 0; // maximum target string length is 64k bytes!
+ size_t termIndex = 0;
+ int c;
+
+ if( *target == 0)
+ return true; // return true if target is a null string
+ while( (c = timedRead()) > 0){
+ if( c == target[index]){
+ //////Serial.print("found "); Serial.write(c); Serial.print("index now"); Serial.println(index+1);
+ if(++index >= targetLen){ // return true if all chars in the target match
+ return true;
+ }
+ }
+ else{
+ index = 0; // reset index if any char does not match
+ }
+ if(termLen > 0 && c == terminator[termIndex]){
+ if(++termIndex >= termLen)
+ return false; // return false if terminate string found before target string
+ }
+ else
+ termIndex = 0;
+ }
+ return false;
+}
+
+
+// returns the first valid (long) integer value from the current position.
+// initial characters that are not digits (or the minus sign) are skipped
+// function is terminated by the first character that is not a digit.
+long Stream::parseInt()
+{
+ return parseInt(NO_SKIP_CHAR); // terminate on first non-digit character (or timeout)
+}
+
+// as above but a given skipChar is ignored
+// this allows format characters (typically commas) in values to be ignored
+long Stream::parseInt(char skipChar)
+{
+ boolean isNegative = false;
+ long value = 0;
+ int c;
+
+ c = peekNextDigit();
+ // ignore non numeric leading characters
+ if(c < 0)
+ return 0; // zero returned if timeout
+
+ do{
+ if(c == skipChar)
+ ; // ignore this charactor
+ else if(c == '-')
+ isNegative = true;
+ else if(c >= '0' && c <= '9') // is c a digit?
+ value = value * 10 + c - '0';
+ read(); // consume the character we got with peek
+ c = timedPeek();
+ }
+ while( (c >= '0' && c <= '9') || c == skipChar );
+
+ if(isNegative)
+ value = -value;
+ return value;
+}
+
+
+// as parseInt but returns a floating point value
+float Stream::parseFloat()
+{
+ return parseFloat(NO_SKIP_CHAR);
+}
+
+// as above but the given skipChar is ignored
+// this allows format characters (typically commas) in values to be ignored
+float Stream::parseFloat(char skipChar){
+ boolean isNegative = false;
+ boolean isFraction = false;
+ long value = 0;
+ char c;
+ float fraction = 1.0;
+
+ c = peekNextDigit();
+ // ignore non numeric leading characters
+ if(c < 0)
+ return 0; // zero returned if timeout
+
+ do{
+ if(c == skipChar)
+ ; // ignore
+ else if(c == '-')
+ isNegative = true;
+ else if (c == '.')
+ isFraction = true;
+ else if(c >= '0' && c <= '9') { // is c a digit?
+ value = value * 10 + c - '0';
+ if(isFraction)
+ fraction *= 0.1;
+ }
+ read(); // consume the character we got with peek
+ c = timedPeek();
+ }
+ while( (c >= '0' && c <= '9') || c == '.' || c == skipChar );
+
+ if(isNegative)
+ value = -value;
+ if(isFraction)
+ return value * fraction;
+ else
+ return value;
+}
+
+// read characters from stream into buffer
+// terminates if length characters have been read, null is detected or timeout (see setTimeout)
+// returns the number of characters placed in the buffer (0 means no valid data found)
+int Stream::readBytes( char *buffer, size_t length)
+{
+ return readBytesUntil( 0, buffer, length);
+}
+
+
+// as readBytes with terminator character
+// terminates if length characters have been read, timeout, or if the terminator character detected
+// returns the number of characters placed in the buffer (0 means no valid data found)
+
+int Stream::readBytesUntil( char terminator, char *buffer, size_t length)
+{
+ unsigned int index = 0;
+ *buffer = 0;
+ while(index < length-1 ){
+ int c = timedRead();
+ if( c <= 0 ){
+ return 0; // timeout returns 0 !
+ }
+ else if( c == terminator){
+ buffer[index] = 0; // terminate the string
+ return index; // data got successfully
+ }
+ else{
+ buffer[index++] = (char)c;
+ }
+ }
+ buffer[index] = 0;
+ return index; // here if buffer is full before detecting the terminator
+}
+
diff --git a/cores/arduino/Stream.h b/cores/arduino/Stream.h
index 93d8275..8f2de1e 100644
--- a/cores/arduino/Stream.h
+++ b/cores/arduino/Stream.h
@@ -15,6 +15,8 @@
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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
+
+ parsing functions based on TextFinder library by Michael Margolis
*/
#ifndef Stream_h
@@ -23,13 +25,70 @@
#include <inttypes.h>
#include "Print.h"
+// compatability macros for testing
+/*
+#define getInt() parseInt()
+#define getInt(skipChar) parseInt(skipchar)
+#define getFloat() parseFloat()
+#define getFloat(skipChar) parseFloat(skipChar)
+#define getString( pre_string, post_string, buffer, length)
+readBytesBetween( pre_string, terminator, buffer, length)
+*/
+
class Stream : public Print
{
+ private:
+ unsigned long _timeout; // number of milliseconds to wait for the next char before aborting timed read
+ unsigned long _startMillis; // used for timeout measurement
+ int timedRead(); // private method to read stream with timeout
+ int timedPeek(); // private method to peek stream with timeout
+ int peekNextDigit(); // returns the next numeric digit in the stream or -1 if timeout
+
public:
virtual int available() = 0;
virtual int read() = 0;
virtual int peek() = 0;
virtual void flush() = 0;
+
+ Stream() {_timeout=1000;}
+
+// parsing methods
+
+ void setTimeout(unsigned long timeout); // sets maximum milliseconds to wait for stream data, default is 1 second
+
+ bool find(char *target); // reads data from the stream until the target string is found
+ // returns true if target string is found, false if timed out (see setTimeout)
+
+ bool find(char *target, size_t length); // reads data from the stream until the target string of given length is found
+ // returns true if target string is found, false if timed out
+
+ bool findUntil(char *target, char *terminator); // as find but search ends if the terminator string is found
+
+ bool findUntil(char *target, size_t targetLen, char *terminate, size_t termLen); // as above but search ends if the terminate string is found
+
+
+ long parseInt(); // returns the first valid (long) integer value from the current position.
+ // initial characters that are not digits (or the minus sign) are skipped
+ // integer is terminated by the first character that is not a digit.
+
+ long parseInt(char skipChar); // as above but the given skipChar is ignored
+ // as above but the given skipChar is ignored
+ // this allows format characters (typically commas) in values to be ignored
+
+ float parseFloat(); // float version of parseInt
+
+ float parseFloat(char skipChar); // as above but the given skipChar is ignored
+
+ int readBytes( char *buffer, size_t length); // read chars from stream into buffer
+ // terminates if length characters have been read or timeout (see setTimeout)
+ // returns the number of characters placed in the buffer (0 means no valid data found)
+
+ int readBytesUntil( char terminator, char *buffer, size_t length); // as readBytes with terminator character
+ // terminates if length characters have been read, timeout, or if the terminator character detected
+ // returns the number of characters placed in the buffer (0 means no valid data found)
+
+ // Arduino String functions to be added here
+
};
#endif
diff --git a/cores/arduino/Tone.cpp b/cores/arduino/Tone.cpp
index c3910e7..20eed3f 100755
--- a/cores/arduino/Tone.cpp
+++ b/cores/arduino/Tone.cpp
@@ -33,7 +33,7 @@ Version Modified By Date Comments
#include <avr/interrupt.h>
#include <avr/pgmspace.h>
-#include "wiring.h"
+#include "Arduino.h"
#include "pins_arduino.h"
#if defined(__AVR_ATmega8__) || defined(__AVR_ATmega128__)
diff --git a/cores/arduino/USBAPI.h b/cores/arduino/USBAPI.h
new file mode 100644
index 0000000..26a2032
--- /dev/null
+++ b/cores/arduino/USBAPI.h
@@ -0,0 +1,166 @@
+
+
+#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_RIGHT 2
+#define MOUSE_MIDDLE 4
+#define MOUSE_ALL (MOUSE_LEFT | MOUSE_RIGHT | MOUSE_MIDDLE)
+
+class Mouse_
+{
+private:
+ uint8_t _buttons;
+ void buttons(uint8_t b);
+public:
+ Mouse_();
+ void click(uint8_t b = MOUSE_LEFT);
+ void move(signed char x, signed char y, signed char wheel = 0);
+ void press(uint8_t b = MOUSE_LEFT); // press LEFT by default
+ void release(uint8_t b = MOUSE_LEFT); // release LEFT by default
+ bool isPressed(uint8_t b = MOUSE_ALL); // check all buttons 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
+{
+private:
+ KeyMap* _keyMap;
+ void sendReport(KeyReport* keys);
+ void setKeyMap(KeyMap* keyMap);
+public:
+ Keyboard_();
+ 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..398bc73
--- /dev/null
+++ b/cores/arduino/USBCore.cpp
@@ -0,0 +1,660 @@
+
+
+/* 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
+
+/** Pulse generation counters to keep track of the number of milliseconds remaining for each pulse type */
+#define TX_RX_LED_PULSE_MS 100
+volatile u8 TxLEDPulse; /**< Milliseconds remaining for data Tx LED pulse */
+volatile u8 RxLEDPulse; /**< Milliseconds remaining for data Rx LED pulse */
+
+//==================================================================
+//==================================================================
+
+extern const u16 STRING_LANGUAGE[] PROGMEM;
+extern const u16 STRING_IPRODUCT[] PROGMEM;
+extern const u16 STRING_IMANUFACTURER[] PROGMEM;
+extern const DeviceDescriptor USB_DeviceDescriptor PROGMEM;
+extern const DeviceDescriptor USB_DeviceDescriptorA PROGMEM;
+
+const u16 STRING_LANGUAGE[2] = {
+ (3<<8) | (2+2),
+ 0x0409 // English
+};
+
+const u16 STRING_IPRODUCT[17] = {
+ (3<<8) | (2+2*16),
+#if USB_PID == USB_PID_LEONARDO
+ 'A','r','d','u','i','n','o',' ','L','e','o','n','a','r','d','o'
+#elif USB_PID == USB_PID_MICRO
+ 'A','r','d','u','i','n','o',' ','M','i','c','r','o',' ',' ',' '
+#endif
+};
+
+const u16 STRING_IMANUFACTURER[12] = {
+ (3<<8) | (2+2*11),
+ 'A','r','d','u','i','n','o',' ','L','L','C'
+};
+
+#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,IMANUFACTURER,IPRODUCT,0,1);
+
+const DeviceDescriptor USB_DeviceDescriptorA =
+ D_DEVICE(DEVICE_CLASS,0x00,0x00,64,USB_VID,USB_PID,0x100,IMANUFACTURER,IPRODUCT,0,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;
+
+ RXLED1; // light the RX LED
+ RxLEDPulse = TX_RX_LED_PULSE_MS;
+}
+
+static inline u8 Recv8()
+{
+ RXLED1; // light the RX LED
+ RxLEDPulse = TX_RX_LED_PULSE_MS;
+
+ 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 receive
+// 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();
+ }
+ }
+ TXLED1; // light the TX LED
+ TxLEDPulse = TX_RX_LED_PULSE_MS;
+ 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
+};
+
+#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 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
+
+ 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;
+ else if (setup.wValueL == IPRODUCT)
+ desc_addr = (const u8*)&STRING_IPRODUCT;
+ else if (setup.wValueL == IMANUFACTURER)
+ desc_addr = (const u8*)&STRING_IMANUFACTURER;
+ else
+ return false;
+ }
+
+ 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();
+
+ 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 - happens every millisecond so we use it for TX and RX LED one-shot timing, too
+ if (udint & (1<<SOFI))
+ {
+#ifdef CDC_ENABLED
+ USB_Flush(CDC_TX); // Send a tx frame if found
+#endif
+
+ // check whether the one-shot period has elapsed. if so, turn off the LED
+ if (TxLEDPulse && !(--TxLEDPulse))
+ TXLED0;
+ if (RxLEDPulse && !(--RxLEDPulse))
+ RXLED0;
+ }
+}
+
+// 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
+
+ TX_RX_LED_INIT;
+}
+
+void USB_::detach()
+{
+}
+
+// Check for interrupts
+// TODO: VBUS detection
+bool USB_::configured()
+{
+ return _usbConfiguration;
+}
+
+void USB_::poll()
+{
+}
+
+#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..549ed9e
--- /dev/null
+++ b/cores/arduino/USBDesc.h
@@ -0,0 +1,67 @@
+
+
+/* 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
+
+
+#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
+
+#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 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
+
+#define IMANUFACTURER 1
+#define IPRODUCT 2
+#define USB_PID_LEONARDO 0x0034
+#define USB_PID_MICRO 0x0035
+#define USB_VID 0x2341 // arduino LLC vid
+#define USB_PID ARDUINO_MODEL_USB_PID
+
diff --git a/cores/arduino/Udp.h b/cores/arduino/Udp.h
new file mode 100644
index 0000000..dc5644b
--- /dev/null
+++ b/cores/arduino/Udp.h
@@ -0,0 +1,88 @@
+/*
+ * Udp.cpp: Library to send/receive UDP packets.
+ *
+ * NOTE: UDP is fast, but has some important limitations (thanks to Warren Gray for mentioning these)
+ * 1) UDP does not guarantee the order in which assembled UDP packets are received. This
+ * might not happen often in practice, but in larger network topologies, a UDP
+ * packet can be received out of sequence.
+ * 2) UDP does not guard against lost packets - so packets *can* disappear without the sender being
+ * aware of it. Again, this may not be a concern in practice on small local networks.
+ * For more information, see http://www.cafeaulait.org/course/week12/35.html
+ *
+ * MIT License:
+ * Copyright (c) 2008 Bjoern Hartmann
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ * THE SOFTWARE.
+ *
+ * bjoern@cs.stanford.edu 12/30/2008
+ */
+
+#ifndef udp_h
+#define udp_h
+
+#include <Stream.h>
+#include <IPAddress.h>
+
+class UDP : public Stream {
+
+public:
+ virtual uint8_t begin(uint16_t) =0; // initialize, start listening on specified port. Returns 1 if successful, 0 if there are no sockets available to use
+ virtual void stop() =0; // Finish with the UDP socket
+
+ // Sending UDP packets
+
+ // Start building up a packet to send to the remote host specific in ip and port
+ // Returns 1 if successful, 0 if there was a problem with the supplied IP address or port
+ virtual int beginPacket(IPAddress ip, uint16_t port) =0;
+ // Start building up a packet to send to the remote host specific in host and port
+ // Returns 1 if successful, 0 if there was a problem resolving the hostname or port
+ virtual int beginPacket(const char *host, uint16_t port) =0;
+ // Finish off this packet and send it
+ // Returns 1 if the packet was sent successfully, 0 if there was an error
+ virtual int endPacket() =0;
+ // Write a single byte into the packet
+ virtual size_t write(uint8_t) =0;
+ // Write size bytes from buffer into the packet
+ virtual size_t write(const uint8_t *buffer, size_t size) =0;
+
+ // Start processing the next available incoming packet
+ // Returns the size of the packet in bytes, or 0 if no packets are available
+ virtual int parsePacket() =0;
+ // Number of bytes remaining in the current packet
+ virtual int available() =0;
+ // Read a single byte from the current packet
+ virtual int read() =0;
+ // Read up to len bytes from the current packet and place them into buffer
+ // Returns the number of bytes read, or 0 if none are available
+ virtual int read(unsigned char* buffer, size_t len) =0;
+ // Read up to len characters from the current packet and place them into buffer
+ // Returns the number of characters read, or 0 if none are available
+ virtual int read(char* buffer, size_t len) =0;
+ // Return the next byte from the current packet without moving on to the next byte
+ virtual int peek() =0;
+ virtual void flush() =0; // Finish reading the current packet
+
+ // Return the IP address of the host who sent the current incoming packet
+ virtual IPAddress remoteIP() =0;
+ // Return the port of the host who sent the current incoming packet
+ virtual uint16_t remotePort() =0;
+protected:
+ uint8_t* rawIPAddress(IPAddress& addr) { return addr.raw_address(); };
+};
+
+#endif
diff --git a/cores/arduino/WConstants.h b/cores/arduino/WConstants.h
deleted file mode 100644
index 3e19ac4..0000000
--- a/cores/arduino/WConstants.h
+++ /dev/null
@@ -1 +0,0 @@
-#include "wiring.h"
diff --git a/cores/arduino/WInterrupts.c b/cores/arduino/WInterrupts.c
index 3b3e0c9..75c713b 100755
--- a/cores/arduino/WInterrupts.c
+++ b/cores/arduino/WInterrupts.c
@@ -30,7 +30,6 @@
#include <avr/pgmspace.h>
#include <stdio.h>
-#include "WConstants.h"
#include "wiring_private.h"
volatile static voidFuncPtr intFunc[EXTERNAL_NUM_INTERRUPTS];
diff --git a/cores/arduino/WProgram.h b/cores/arduino/WProgram.h
deleted file mode 100755
index f73e760..0000000
--- a/cores/arduino/WProgram.h
+++ /dev/null
@@ -1,63 +0,0 @@
-#ifndef WProgram_h
-#define WProgram_h
-
-#include <stdlib.h>
-#include <string.h>
-#include <math.h>
-
-#include <avr/interrupt.h>
-
-#include "wiring.h"
-
-#ifdef __cplusplus
-#include "WCharacter.h"
-#include "WString.h"
-#include "HardwareSerial.h"
-
-uint16_t makeWord(uint16_t w);
-uint16_t makeWord(byte h, byte l);
-
-#define word(...) makeWord(__VA_ARGS__)
-
-unsigned long pulseIn(uint8_t pin, uint8_t state, unsigned long timeout = 1000000L);
-
-void tone(uint8_t _pin, unsigned int frequency, unsigned long duration = 0);
-void noTone(uint8_t _pin);
-
-// WMath prototypes
-long random(long);
-long random(long, long);
-void randomSeed(unsigned int);
-long map(long, long, long, long, long);
-
-#if defined(__AVR_ATmega1280__) || defined(__AVR_ATmega2560__)
-const static uint8_t A0 = 54;
-const static uint8_t A1 = 55;
-const static uint8_t A2 = 56;
-const static uint8_t A3 = 57;
-const static uint8_t A4 = 58;
-const static uint8_t A5 = 59;
-const static uint8_t A6 = 60;
-const static uint8_t A7 = 61;
-const static uint8_t A8 = 62;
-const static uint8_t A9 = 63;
-const static uint8_t A10 = 64;
-const static uint8_t A11 = 65;
-const static uint8_t A12 = 66;
-const static uint8_t A13 = 67;
-const static uint8_t A14 = 68;
-const static uint8_t A15 = 69;
-#else
-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;
-#endif
-
-#endif
-
-#endif \ No newline at end of file
diff --git a/cores/arduino/WString.cpp b/cores/arduino/WString.cpp
index db5a441..ad8d828 100644
--- a/cores/arduino/WString.cpp
+++ b/cores/arduino/WString.cpp
@@ -1,6 +1,8 @@
/*
WString.cpp - String library for Wiring & Arduino
+ ...mostly rewritten by Paul Stoffregen...
Copyright (c) 2009-10 Hernando Barragan. All rights reserved.
+ Copyright 2011, Paul Stoffregen, paul@pjrc.com
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
@@ -17,427 +19,627 @@
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
-#include <stdlib.h>
-#include "WProgram.h"
#include "WString.h"
-String::String( const char *value )
+/*********************************************/
+/* Constructors */
+/*********************************************/
+
+String::String(const char *cstr)
{
- if ( value == NULL )
- value = "";
- getBuffer( _length = strlen( value ) );
- if ( _buffer != NULL )
- strcpy( _buffer, value );
+ init();
+ if (cstr) copy(cstr, strlen(cstr));
}
-String::String( const String &value )
+String::String(const String &value)
{
- getBuffer( _length = value._length );
- if ( _buffer != NULL )
- strcpy( _buffer, value._buffer );
+ init();
+ *this = value;
}
-String::String( const char value )
+#ifdef __GXX_EXPERIMENTAL_CXX0X__
+String::String(String &&rval)
{
- _length = 1;
- getBuffer(1);
- if ( _buffer != NULL ) {
- _buffer[0] = value;
- _buffer[1] = 0;
- }
+ init();
+ move(rval);
}
-
-String::String( const unsigned char value )
+String::String(StringSumHelper &&rval)
{
- _length = 1;
- getBuffer(1);
- if ( _buffer != NULL) {
- _buffer[0] = value;
- _buffer[1] = 0;
- }
+ init();
+ move(rval);
}
+#endif
-String::String( const int value, const int base )
+String::String(char c)
{
- char buf[33];
- itoa((signed long)value, buf, base);
- getBuffer( _length = strlen(buf) );
- if ( _buffer != NULL )
- strcpy( _buffer, buf );
+ init();
+ char buf[2];
+ buf[0] = c;
+ buf[1] = 0;
+ *this = buf;
}
-String::String( const unsigned int value, const int base )
+String::String(unsigned char value, unsigned char base)
{
- char buf[33];
- ultoa((unsigned long)value, buf, base);
- getBuffer( _length = strlen(buf) );
- if ( _buffer != NULL )
- strcpy( _buffer, buf );
+ init();
+ char buf[9];
+ utoa(value, buf, base);
+ *this = buf;
}
-String::String( const long value, const int base )
+String::String(int value, unsigned char base)
{
- char buf[33];
- ltoa(value, buf, base);
- getBuffer( _length = strlen(buf) );
- if ( _buffer != NULL )
- strcpy( _buffer, buf );
+ init();
+ char buf[18];
+ itoa(value, buf, base);
+ *this = buf;
}
-String::String( const unsigned long value, const int base )
+String::String(unsigned int value, unsigned char base)
{
- char buf[33];
- ultoa(value, buf, 10);
- getBuffer( _length = strlen(buf) );
- if ( _buffer != NULL )
- strcpy( _buffer, buf );
+ init();
+ char buf[17];
+ utoa(value, buf, base);
+ *this = buf;
}
-char String::charAt( unsigned int loc ) const
+String::String(long value, unsigned char base)
{
- return operator[]( loc );
+ init();
+ char buf[34];
+ ltoa(value, buf, base);
+ *this = buf;
}
-void String::setCharAt( unsigned int loc, const char aChar )
+String::String(unsigned long value, unsigned char base)
{
- if(_buffer == NULL) return;
- if(_length > loc) {
- _buffer[loc] = aChar;
- }
+ init();
+ char buf[33];
+ ultoa(value, buf, base);
+ *this = buf;
}
-int String::compareTo( const String &s2 ) const
+String::~String()
{
- return strcmp( _buffer, s2._buffer );
+ free(buffer);
}
-const String & String::concat( const String &s2 )
+/*********************************************/
+/* Memory Management */
+/*********************************************/
+
+inline void String::init(void)
{
- return (*this) += s2;
+ buffer = NULL;
+ capacity = 0;
+ len = 0;
+ flags = 0;
}
-const String & String::operator=( const String &rhs )
+void String::invalidate(void)
{
- if ( this == &rhs )
- return *this;
-
- if ( rhs._length > _length )
- {
- free(_buffer);
- getBuffer( rhs._length );
- }
-
- if ( _buffer != NULL ) {
- _length = rhs._length;
- strcpy( _buffer, rhs._buffer );
- }
- return *this;
+ if (buffer) free(buffer);
+ buffer = NULL;
+ capacity = len = 0;
}
-//const String & String::operator+=( const char aChar )
-//{
-// if ( _length == _capacity )
-// doubleBuffer();
-//
-// _buffer[ _length++ ] = aChar;
-// _buffer[ _length ] = '\0';
-// return *this;
-//}
+unsigned char String::reserve(unsigned int size)
+{
+ if (buffer && capacity >= size) return 1;
+ if (changeBuffer(size)) {
+ if (len == 0) buffer[0] = 0;
+ return 1;
+ }
+ return 0;
+}
-const String & String::operator+=( const String &other )
+unsigned char String::changeBuffer(unsigned int maxStrLen)
{
- _length += other._length;
- if ( _length > _capacity )
- {
- char *temp = (char *)realloc(_buffer, _length + 1);
- if ( temp != NULL ) {
- _buffer = temp;
- _capacity = _length;
- } else {
- _length -= other._length;
- return *this;
- }
- }
- strcat( _buffer, other._buffer );
- return *this;
+ char *newbuffer = (char *)realloc(buffer, maxStrLen + 1);
+ if (newbuffer) {
+ buffer = newbuffer;
+ capacity = maxStrLen;
+ return 1;
+ }
+ return 0;
}
+/*********************************************/
+/* Copy and Move */
+/*********************************************/
-int String::operator==( const String &rhs ) const
+String & String::copy(const char *cstr, unsigned int length)
{
- return ( _length == rhs._length && strcmp( _buffer, rhs._buffer ) == 0 );
+ if (!reserve(length)) {
+ invalidate();
+ return *this;
+ }
+ len = length;
+ strcpy(buffer, cstr);
+ return *this;
}
-int String::operator!=( const String &rhs ) const
+#ifdef __GXX_EXPERIMENTAL_CXX0X__
+void String::move(String &rhs)
{
- return ( _length != rhs.length() || strcmp( _buffer, rhs._buffer ) != 0 );
+ if (buffer) {
+ if (capacity >= rhs.len) {
+ strcpy(buffer, rhs.buffer);
+ len = rhs.len;
+ rhs.len = 0;
+ return;
+ } else {
+ free(buffer);
+ }
+ }
+ buffer = rhs.buffer;
+ capacity = rhs.capacity;
+ len = rhs.len;
+ rhs.buffer = NULL;
+ rhs.capacity = 0;
+ rhs.len = 0;
}
+#endif
-int String::operator<( const String &rhs ) const
+String & String::operator = (const String &rhs)
{
- return strcmp( _buffer, rhs._buffer ) < 0;
+ if (this == &rhs) return *this;
+
+ if (rhs.buffer) copy(rhs.buffer, rhs.len);
+ else invalidate();
+
+ return *this;
}
-int String::operator>( const String &rhs ) const
+#ifdef __GXX_EXPERIMENTAL_CXX0X__
+String & String::operator = (String &&rval)
{
- return strcmp( _buffer, rhs._buffer ) > 0;
+ if (this != &rval) move(rval);
+ return *this;
}
-int String::operator<=( const String &rhs ) const
+String & String::operator = (StringSumHelper &&rval)
{
- return strcmp( _buffer, rhs._buffer ) <= 0;
+ if (this != &rval) move(rval);
+ return *this;
}
+#endif
-int String::operator>=( const String & rhs ) const
+String & String::operator = (const char *cstr)
{
- return strcmp( _buffer, rhs._buffer ) >= 0;
+ if (cstr) copy(cstr, strlen(cstr));
+ else invalidate();
+
+ return *this;
}
-char & String::operator[]( unsigned int index )
+/*********************************************/
+/* concat */
+/*********************************************/
+
+unsigned char String::concat(const String &s)
{
- static char dummy_writable_char;
- if (index >= _length || !_buffer) {
- dummy_writable_char = 0;
- return dummy_writable_char;
- }
- return _buffer[ index ];
+ return concat(s.buffer, s.len);
}
-char String::operator[]( unsigned int index ) const
+unsigned char String::concat(const char *cstr, unsigned int length)
{
- // need to check for valid index, to do later
- return _buffer[ index ];
+ unsigned int newlen = len + length;
+ if (!cstr) return 0;
+ if (length == 0) return 1;
+ if (!reserve(newlen)) return 0;
+ strcpy(buffer + len, cstr);
+ len = newlen;
+ return 1;
}
-boolean String::endsWith( const String &s2 ) const
+unsigned char String::concat(const char *cstr)
{
- if ( _length < s2._length )
- return 0;
-
- return strcmp( &_buffer[ _length - s2._length], s2._buffer ) == 0;
+ if (!cstr) return 0;
+ return concat(cstr, strlen(cstr));
}
-boolean String::equals( const String &s2 ) const
+unsigned char String::concat(char c)
{
- return ( _length == s2._length && strcmp( _buffer,s2._buffer ) == 0 );
+ char buf[2];
+ buf[0] = c;
+ buf[1] = 0;
+ return concat(buf, 1);
}
-boolean String::equalsIgnoreCase( const String &s2 ) const
+unsigned char String::concat(unsigned char num)
{
- if ( this == &s2 )
- return true; //1;
- else if ( _length != s2._length )
- return false; //0;
+ char buf[4];
+ itoa(num, buf, 10);
+ return concat(buf, strlen(buf));
+}
- return strcmp(toLowerCase()._buffer, s2.toLowerCase()._buffer) == 0;
+unsigned char String::concat(int num)
+{
+ char buf[7];
+ itoa(num, buf, 10);
+ return concat(buf, strlen(buf));
}
-String String::replace( char findChar, char replaceChar )
+unsigned char String::concat(unsigned int num)
{
- if ( _buffer == NULL ) return *this;
- String theReturn = _buffer;
- char* temp = theReturn._buffer;
- while( (temp = strchr( temp, findChar )) != 0 )
- *temp = replaceChar;
+ char buf[6];
+ utoa(num, buf, 10);
+ return concat(buf, strlen(buf));
+}
- return theReturn;
+unsigned char String::concat(long num)
+{
+ char buf[12];
+ ltoa(num, buf, 10);
+ return concat(buf, strlen(buf));
}
-String String::replace( const String& match, const String& replace )
+unsigned char String::concat(unsigned long num)
{
- if ( _buffer == NULL ) return *this;
- String temp = _buffer, newString;
+ char buf[11];
+ ultoa(num, buf, 10);
+ return concat(buf, strlen(buf));
+}
+
+/*********************************************/
+/* Concatenate */
+/*********************************************/
- int loc;
- while ( (loc = temp.indexOf( match )) != -1 )
- {
- newString += temp.substring( 0, loc );
- newString += replace;
- temp = temp.substring( loc + match._length );
- }
- newString += temp;
- return newString;
+StringSumHelper & operator + (const StringSumHelper &lhs, const String &rhs)
+{
+ StringSumHelper &a = const_cast<StringSumHelper&>(lhs);
+ if (!a.concat(rhs.buffer, rhs.len)) a.invalidate();
+ return a;
}
-int String::indexOf( char temp ) const
+StringSumHelper & operator + (const StringSumHelper &lhs, const char *cstr)
{
- return indexOf( temp, 0 );
+ StringSumHelper &a = const_cast<StringSumHelper&>(lhs);
+ if (!cstr || !a.concat(cstr, strlen(cstr))) a.invalidate();
+ return a;
}
-int String::indexOf( char ch, unsigned int fromIndex ) const
+StringSumHelper & operator + (const StringSumHelper &lhs, char c)
{
- if ( fromIndex >= _length )
- return -1;
+ StringSumHelper &a = const_cast<StringSumHelper&>(lhs);
+ if (!a.concat(c)) a.invalidate();
+ return a;
+}
- const char* temp = strchr( &_buffer[fromIndex], ch );
- if ( temp == NULL )
- return -1;
+StringSumHelper & operator + (const StringSumHelper &lhs, unsigned char num)
+{
+ StringSumHelper &a = const_cast<StringSumHelper&>(lhs);
+ if (!a.concat(num)) a.invalidate();
+ return a;
+}
- return temp - _buffer;
+StringSumHelper & operator + (const StringSumHelper &lhs, int num)
+{
+ StringSumHelper &a = const_cast<StringSumHelper&>(lhs);
+ if (!a.concat(num)) a.invalidate();
+ return a;
}
-int String::indexOf( const String &s2 ) const
+StringSumHelper & operator + (const StringSumHelper &lhs, unsigned int num)
{
- return indexOf( s2, 0 );
+ StringSumHelper &a = const_cast<StringSumHelper&>(lhs);
+ if (!a.concat(num)) a.invalidate();
+ return a;
}
-int String::indexOf( const String &s2, unsigned int fromIndex ) const
+StringSumHelper & operator + (const StringSumHelper &lhs, long num)
{
- if ( fromIndex >= _length )
- return -1;
+ StringSumHelper &a = const_cast<StringSumHelper&>(lhs);
+ if (!a.concat(num)) a.invalidate();
+ return a;
+}
- const char *theFind = strstr( &_buffer[ fromIndex ], s2._buffer );
+StringSumHelper & operator + (const StringSumHelper &lhs, unsigned long num)
+{
+ StringSumHelper &a = const_cast<StringSumHelper&>(lhs);
+ if (!a.concat(num)) a.invalidate();
+ return a;
+}
- if ( theFind == NULL )
- return -1;
+/*********************************************/
+/* Comparison */
+/*********************************************/
- return theFind - _buffer; // pointer subtraction
+int String::compareTo(const String &s) const
+{
+ if (!buffer || !s.buffer) {
+ if (s.buffer && s.len > 0) return 0 - *(unsigned char *)s.buffer;
+ if (buffer && len > 0) return *(unsigned char *)buffer;
+ return 0;
+ }
+ return strcmp(buffer, s.buffer);
}
-int String::lastIndexOf( char theChar ) const
+unsigned char String::equals(const String &s2) const
{
- return lastIndexOf( theChar, _length - 1 );
+ return (len == s2.len && compareTo(s2) == 0);
}
-int String::lastIndexOf( char ch, unsigned int fromIndex ) const
+unsigned char String::equals(const char *cstr) const
{
- if ( fromIndex >= _length )
- return -1;
+ if (len == 0) return (cstr == NULL || *cstr == 0);
+ if (cstr == NULL) return buffer[0] == 0;
+ return strcmp(buffer, cstr) == 0;
+}
- char tempchar = _buffer[fromIndex + 1];
- _buffer[fromIndex + 1] = '\0';
- char* temp = strrchr( _buffer, ch );
- _buffer[fromIndex + 1] = tempchar;
+unsigned char String::operator<(const String &rhs) const
+{
+ return compareTo(rhs) < 0;
+}
- if ( temp == NULL )
- return -1;
+unsigned char String::operator>(const String &rhs) const
+{
+ return compareTo(rhs) > 0;
+}
- return temp - _buffer;
+unsigned char String::operator<=(const String &rhs) const
+{
+ return compareTo(rhs) <= 0;
}
-int String::lastIndexOf( const String &s2 ) const
+unsigned char String::operator>=(const String &rhs) const
{
- return lastIndexOf( s2, _length - s2._length );
+ return compareTo(rhs) >= 0;
}
-int String::lastIndexOf( const String &s2, unsigned int fromIndex ) const
+unsigned char String::equalsIgnoreCase( const String &s2 ) const
{
- // check for empty strings
- if ( s2._length == 0 || s2._length - 1 > fromIndex || fromIndex >= _length )
- return -1;
+ if (this == &s2) return 1;
+ if (len != s2.len) return 0;
+ if (len == 0) return 1;
+ const char *p1 = buffer;
+ const char *p2 = s2.buffer;
+ while (*p1) {
+ if (tolower(*p1++) != tolower(*p2++)) return 0;
+ }
+ return 1;
+}
- // matching first character
- char temp = s2[ 0 ];
+unsigned char String::startsWith( const String &s2 ) const
+{
+ if (len < s2.len) return 0;
+ return startsWith(s2, 0);
+}
- for ( int i = fromIndex; i >= 0; i-- )
- {
- if ( _buffer[ i ] == temp && (*this).substring( i, i + s2._length ).equals( s2 ) )
- return i;
- }
- return -1;
+unsigned char String::startsWith( const String &s2, unsigned int offset ) const
+{
+ if (offset > len - s2.len || !buffer || !s2.buffer) return 0;
+ return strncmp( &buffer[offset], s2.buffer, s2.len ) == 0;
}
-boolean String::startsWith( const String &s2 ) const
+unsigned char String::endsWith( const String &s2 ) const
{
- if ( _length < s2._length )
- return 0;
+ if ( len < s2.len || !buffer || !s2.buffer) return 0;
+ return strcmp(&buffer[len - s2.len], s2.buffer) == 0;
+}
+
+/*********************************************/
+/* Character Access */
+/*********************************************/
- return startsWith( s2, 0 );
+char String::charAt(unsigned int loc) const
+{
+ return operator[](loc);
}
-boolean String::startsWith( const String &s2, unsigned int offset ) const
+void String::setCharAt(unsigned int loc, char c)
{
- if ( offset > _length - s2._length )
- return 0;
+ if (loc < len) buffer[loc] = c;
+}
- return strncmp( &_buffer[offset], s2._buffer, s2._length ) == 0;
+char & String::operator[](unsigned int index)
+{
+ static char dummy_writable_char;
+ if (index >= len || !buffer) {
+ dummy_writable_char = 0;
+ return dummy_writable_char;
+ }
+ return buffer[index];
}
-String String::substring( unsigned int left ) const
+char String::operator[]( unsigned int index ) const
{
- return substring( left, _length );
+ if (index >= len || !buffer) return 0;
+ return buffer[index];
}
-String String::substring( unsigned int left, unsigned int right ) const
+void String::getBytes(unsigned char *buf, unsigned int bufsize, unsigned int index) const
{
- if ( left > right )
- {
- int temp = right;
- right = left;
- left = temp;
- }
+ if (!bufsize || !buf) return;
+ if (index >= len) {
+ buf[0] = 0;
+ return;
+ }
+ unsigned int n = bufsize - 1;
+ if (n > len - index) n = len - index;
+ strncpy((char *)buf, buffer + index, n);
+ buf[n] = 0;
+}
- if ( right > _length )
- {
- right = _length;
- }
+/*********************************************/
+/* Search */
+/*********************************************/
- char temp = _buffer[ right ]; // save the replaced character
- _buffer[ right ] = '\0';
- String outPut = ( _buffer + left ); // pointer arithmetic
- _buffer[ right ] = temp; //restore character
- return outPut;
+int String::indexOf(char c) const
+{
+ return indexOf(c, 0);
}
-String String::toLowerCase() const
+int String::indexOf( char ch, unsigned int fromIndex ) const
{
- String temp = _buffer;
-
- for ( unsigned int i = 0; i < _length; i++ )
- temp._buffer[ i ] = (char)tolower( temp._buffer[ i ] );
- return temp;
+ if (fromIndex >= len) return -1;
+ const char* temp = strchr(buffer + fromIndex, ch);
+ if (temp == NULL) return -1;
+ return temp - buffer;
}
-String String::toUpperCase() const
+int String::indexOf(const String &s2) const
{
- String temp = _buffer;
-
- for ( unsigned int i = 0; i < _length; i++ )
- temp._buffer[ i ] = (char)toupper( temp._buffer[ i ] );
- return temp;
+ return indexOf(s2, 0);
}
-String String::trim() const
+int String::indexOf(const String &s2, unsigned int fromIndex) const
{
- if ( _buffer == NULL ) return *this;
- String temp = _buffer;
- unsigned int i,j;
+ if (fromIndex >= len) return -1;
+ const char *found = strstr(buffer + fromIndex, s2.buffer);
+ if (found == NULL) return -1;
+ return found - buffer;
+}
- for ( i = 0; i < _length; i++ )
- {
- if ( !isspace(_buffer[i]) )
- break;
- }
+int String::lastIndexOf( char theChar ) const
+{
+ return lastIndexOf(theChar, len - 1);
+}
- for ( j = temp._length - 1; j > i; j-- )
- {
- if ( !isspace(_buffer[j]) )
- break;
- }
+int String::lastIndexOf(char ch, unsigned int fromIndex) const
+{
+ if (fromIndex >= len || fromIndex < 0) return -1;
+ char tempchar = buffer[fromIndex + 1];
+ buffer[fromIndex + 1] = '\0';
+ char* temp = strrchr( buffer, ch );
+ buffer[fromIndex + 1] = tempchar;
+ if (temp == NULL) return -1;
+ return temp - buffer;
+}
- return temp.substring( i, j + 1);
+int String::lastIndexOf(const String &s2) const
+{
+ return lastIndexOf(s2, len - s2.len);
}
-void String::getBytes(unsigned char *buf, unsigned int bufsize)
+int String::lastIndexOf(const String &s2, unsigned int fromIndex) const
{
- if (!bufsize || !buf) return;
- unsigned int len = bufsize - 1;
- if (len > _length) len = _length;
- strncpy((char *)buf, _buffer, len);
- buf[len] = 0;
+ if (s2.len == 0 || len == 0 || s2.len > len || fromIndex < 0) return -1;
+ if (fromIndex >= len) fromIndex = len - 1;
+ int found = -1;
+ for (char *p = buffer; p <= buffer + fromIndex; p++) {
+ p = strstr(p, s2.buffer);
+ if (!p) break;
+ if ((unsigned int)(p - buffer) <= fromIndex) found = p - buffer;
+ }
+ return found;
}
-void String::toCharArray(char *buf, unsigned int bufsize)
+String String::substring( unsigned int left ) const
{
- if (!bufsize || !buf) return;
- unsigned int len = bufsize - 1;
- if (len > _length) len = _length;
- strncpy(buf, _buffer, len);
- buf[len] = 0;
+ return substring(left, len);
+}
+
+String String::substring(unsigned int left, unsigned int right) const
+{
+ if (left > right) {
+ unsigned int temp = right;
+ right = left;
+ left = temp;
+ }
+ String out;
+ if (left > len) return out;
+ if (right > len) right = len;
+ char temp = buffer[right]; // save the replaced character
+ buffer[right] = '\0';
+ out = buffer + left; // pointer arithmetic
+ buffer[right] = temp; //restore character
+ return out;
+}
+
+/*********************************************/
+/* Modification */
+/*********************************************/
+
+void String::replace(char find, char replace)
+{
+ if (!buffer) return;
+ for (char *p = buffer; *p; p++) {
+ if (*p == find) *p = replace;
+ }
+}
+
+void String::replace(const String& find, const String& replace)
+{
+ if (len == 0 || find.len == 0) return;
+ int diff = replace.len - find.len;
+ char *readFrom = buffer;
+ char *foundAt;
+ if (diff == 0) {
+ while ((foundAt = strstr(readFrom, find.buffer)) != NULL) {
+ memcpy(foundAt, replace.buffer, replace.len);
+ readFrom = foundAt + replace.len;
+ }
+ } else if (diff < 0) {
+ char *writeTo = buffer;
+ while ((foundAt = strstr(readFrom, find.buffer)) != NULL) {
+ unsigned int n = foundAt - readFrom;
+ memcpy(writeTo, readFrom, n);
+ writeTo += n;
+ memcpy(writeTo, replace.buffer, replace.len);
+ writeTo += replace.len;
+ readFrom = foundAt + find.len;
+ len += diff;
+ }
+ strcpy(writeTo, readFrom);
+ } else {
+ unsigned int size = len; // compute size needed for result
+ while ((foundAt = strstr(readFrom, find.buffer)) != NULL) {
+ readFrom = foundAt + find.len;
+ size += diff;
+ }
+ if (size == len) return;
+ if (size > capacity && !changeBuffer(size)) return; // XXX: tell user!
+ int index = len - 1;
+ while ((index = lastIndexOf(find, index)) >= 0) {
+ readFrom = buffer + index + find.len;
+ memmove(readFrom + diff, readFrom, len - (readFrom - buffer));
+ len += diff;
+ buffer[len] = 0;
+ memcpy(buffer + index, replace.buffer, replace.len);
+ index--;
+ }
+ }
+}
+
+void String::toLowerCase(void)
+{
+ if (!buffer) return;
+ for (char *p = buffer; *p; p++) {
+ *p = tolower(*p);
+ }
+}
+
+void String::toUpperCase(void)
+{
+ if (!buffer) return;
+ for (char *p = buffer; *p; p++) {
+ *p = toupper(*p);
+ }
+}
+
+void String::trim(void)
+{
+ if (!buffer || len == 0) return;
+ char *begin = buffer;
+ while (isspace(*begin)) begin++;
+ char *end = buffer + len - 1;
+ while (isspace(*end) && end >= begin) end--;
+ len = end + 1 - begin;
+ if (begin > buffer) memcpy(buffer, begin, len);
+ buffer[len] = 0;
+}
+
+/*********************************************/
+/* Parsing / Conversion */
+/*********************************************/
+
+long String::toInt(void) const
+{
+ if (buffer) return atol(buffer);
+ return 0;
}
-long String::toInt() {
- return atol(_buffer);
-}
diff --git a/cores/arduino/main.cpp b/cores/arduino/main.cpp
index cc6e81d..0ef5256 100755
--- a/cores/arduino/main.cpp
+++ b/cores/arduino/main.cpp
@@ -1,13 +1,20 @@
-#include <WProgram.h>
+#define ARDUINO_MAIN
+#include <Arduino.h>
int main(void)
{
init();
+#if defined(USBCON)
+ USB.attach();
+#endif
+
setup();
- for (;;)
+ for (;;) {
loop();
+ if (serialEventRun) serialEventRun();
+ }
return 0;
}
diff --git a/cores/arduino/new.cpp b/cores/arduino/new.cpp
new file mode 100644
index 0000000..0f6d422
--- /dev/null
+++ b/cores/arduino/new.cpp
@@ -0,0 +1,18 @@
+#include <new.h>
+
+void * operator new(size_t size)
+{
+ return malloc(size);
+}
+
+void operator delete(void * ptr)
+{
+ free(ptr);
+}
+
+int __cxa_guard_acquire(__guard *g) {return !*(char *)(g);};
+void __cxa_guard_release (__guard *g) {*(char *)g = 1;};
+void __cxa_guard_abort (__guard *) {};
+
+void __cxa_pure_virtual(void) {};
+
diff --git a/cores/arduino/new.h b/cores/arduino/new.h
new file mode 100644
index 0000000..cd940ce
--- /dev/null
+++ b/cores/arduino/new.h
@@ -0,0 +1,22 @@
+/* Header to define new/delete operators as they aren't provided by avr-gcc by default
+ Taken from http://www.avrfreaks.net/index.php?name=PNphpBB2&file=viewtopic&t=59453
+ */
+
+#ifndef NEW_H
+#define NEW_H
+
+#include <stdlib.h>
+
+void * operator new(size_t size);
+void operator delete(void * ptr);
+
+__extension__ typedef int __guard __attribute__((mode (__DI__)));
+
+extern "C" int __cxa_guard_acquire(__guard *);
+extern "C" void __cxa_guard_release (__guard *);
+extern "C" void __cxa_guard_abort (__guard *);
+
+extern "C" void __cxa_pure_virtual(void);
+
+#endif
+
diff --git a/cores/arduino/pins_arduino.h b/cores/arduino/pins_arduino.h
deleted file mode 100644
index bc931c5..0000000
--- a/cores/arduino/pins_arduino.h
+++ /dev/null
@@ -1,88 +0,0 @@
-/*
- 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>
-
-#define NOT_A_PIN 0
-#define NOT_A_PORT 0
-
-#define NOT_ON_TIMER 0
-#define TIMER0A 1
-#define TIMER0B 2
-#define TIMER1A 3
-#define TIMER1B 4
-#define TIMER2 5
-#define TIMER2A 6
-#define TIMER2B 7
-
-#define TIMER3A 8
-#define TIMER3B 9
-#define TIMER3C 10
-#define TIMER4A 11
-#define TIMER4B 12
-#define TIMER4C 13
-#define TIMER5A 14
-#define TIMER5B 15
-#define TIMER5C 16
-
-#if defined(__AVR_ATmega1280__) || defined(__AVR_ATmega2560__)
-const static uint8_t SS = 53;
-const static uint8_t MOSI = 51;
-const static uint8_t MISO = 50;
-const static uint8_t SCK = 52;
-#else
-const static uint8_t SS = 10;
-const static uint8_t MOSI = 11;
-const static uint8_t MISO = 12;
-const static uint8_t SCK = 13;
-#endif
-
-// On the ATmega1280, the addresses of some of the port registers are
-// greater than 255, so we can't store them in uint8_t's.
-extern const uint16_t PROGMEM port_to_mode_PGM[];
-extern const uint16_t PROGMEM port_to_input_PGM[];
-extern const uint16_t PROGMEM port_to_output_PGM[];
-
-extern const uint8_t PROGMEM digital_pin_to_port_PGM[];
-// extern const uint8_t PROGMEM digital_pin_to_bit_PGM[];
-extern const uint8_t PROGMEM digital_pin_to_bit_mask_PGM[];
-extern const uint8_t PROGMEM digital_pin_to_timer_PGM[];
-
-// Get the bit location within the hardware port of the given virtual pin.
-// This comes from the pins_*.c file for the active board configuration.
-//
-// These perform slightly better as macros compared to inline functions
-//
-#define digitalPinToPort(P) ( pgm_read_byte( digital_pin_to_port_PGM + (P) ) )
-#define digitalPinToBitMask(P) ( pgm_read_byte( digital_pin_to_bit_mask_PGM + (P) ) )
-#define digitalPinToTimer(P) ( pgm_read_byte( digital_pin_to_timer_PGM + (P) ) )
-#define analogInPinToBit(P) (P)
-#define portOutputRegister(P) ( (volatile uint8_t *)( pgm_read_word( port_to_output_PGM + (P))) )
-#define portInputRegister(P) ( (volatile uint8_t *)( pgm_read_word( port_to_input_PGM + (P))) )
-#define portModeRegister(P) ( (volatile uint8_t *)( pgm_read_word( port_to_mode_PGM + (P))) )
-
-#endif
diff --git a/cores/arduino/wiring.c b/cores/arduino/wiring.c
index b90d07e..1b3fd44 100755
--- a/cores/arduino/wiring.c
+++ b/cores/arduino/wiring.c
@@ -41,7 +41,11 @@ volatile unsigned long timer0_overflow_count = 0;
volatile unsigned long timer0_millis = 0;
static unsigned char timer0_fract = 0;
+#if defined(__AVR_ATtiny24__) || defined(__AVR_ATtiny44__) || defined(__AVR_ATtiny84__)
+SIGNAL(TIM0_OVF_vect)
+#else
SIGNAL(TIMER0_OVF_vect)
+#endif
{
// copy these to local variables so they can be stored in registers
// (volatile variables must be read from memory on every access)
@@ -212,10 +216,10 @@ void init()
// note, however, that fast pwm mode can achieve a frequency of up
// 8 MHz (with a 16 MHz clock) at 50% duty cycle
+#if defined(TCCR1B) && defined(CS11) && defined(CS10)
TCCR1B = 0;
// set timer 1 prescale factor to 64
-#if defined(TCCR1B) && defined(CS11) && defined(CS10)
sbi(TCCR1B, CS11);
sbi(TCCR1B, CS10);
#elif defined(TCCR1) && defined(CS11) && defined(CS10)
diff --git a/cores/arduino/wiring.h b/cores/arduino/wiring.h
deleted file mode 100755
index e29959b..0000000
--- a/cores/arduino/wiring.h
+++ /dev/null
@@ -1,135 +0,0 @@
-/*
- wiring.h - Partial implementation of the Wiring API for the ATmega8.
- Part of Arduino - http://www.arduino.cc/
-
- Copyright (c) 2005-2006 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$
-*/
-
-#ifndef Wiring_h
-#define Wiring_h
-
-#include <avr/io.h>
-#include <stdlib.h>
-#include "binary.h"
-
-#ifdef __cplusplus
-extern "C"{
-#endif
-
-#define HIGH 0x1
-#define LOW 0x0
-
-#define INPUT 0x0
-#define OUTPUT 0x1
-
-#define true 0x1
-#define false 0x0
-
-#define PI 3.1415926535897932384626433832795
-#define HALF_PI 1.5707963267948966192313216916398
-#define TWO_PI 6.283185307179586476925286766559
-#define DEG_TO_RAD 0.017453292519943295769236907684886
-#define RAD_TO_DEG 57.295779513082320876798154814105
-
-#define SERIAL 0x0
-#define DISPLAY 0x1
-
-#define LSBFIRST 0
-#define MSBFIRST 1
-
-#define CHANGE 1
-#define FALLING 2
-#define RISING 3
-
-#if defined(__AVR_ATmega1280__) || defined(__AVR_ATmega2560__)
-#define INTERNAL1V1 2
-#define INTERNAL2V56 3
-#else
-#define INTERNAL 3
-#endif
-#define DEFAULT 1
-#define EXTERNAL 0
-
-// undefine stdlib's abs if encountered
-#ifdef abs
-#undef abs
-#endif
-
-#define min(a,b) ((a)<(b)?(a):(b))
-#define max(a,b) ((a)>(b)?(a):(b))
-#define abs(x) ((x)>0?(x):-(x))
-#define constrain(amt,low,high) ((amt)<(low)?(low):((amt)>(high)?(high):(amt)))
-#define round(x) ((x)>=0?(long)((x)+0.5):(long)((x)-0.5))
-#define radians(deg) ((deg)*DEG_TO_RAD)
-#define degrees(rad) ((rad)*RAD_TO_DEG)
-#define sq(x) ((x)*(x))
-
-#define interrupts() sei()
-#define noInterrupts() cli()
-
-#define clockCyclesPerMicrosecond() ( F_CPU / 1000000L )
-#define clockCyclesToMicroseconds(a) ( ((a) * 1000L) / (F_CPU / 1000L) )
-#define microsecondsToClockCycles(a) ( ((a) * (F_CPU / 1000L)) / 1000L )
-
-#define lowByte(w) ((uint8_t) ((w) & 0xff))
-#define highByte(w) ((uint8_t) ((w) >> 8))
-
-#define bitRead(value, bit) (((value) >> (bit)) & 0x01)
-#define bitSet(value, bit) ((value) |= (1UL << (bit)))
-#define bitClear(value, bit) ((value) &= ~(1UL << (bit)))
-#define bitWrite(value, bit, bitvalue) (bitvalue ? bitSet(value, bit) : bitClear(value, bit))
-
-
-typedef unsigned int word;
-
-#define bit(b) (1UL << (b))
-
-typedef uint8_t boolean;
-typedef uint8_t byte;
-
-void init(void);
-
-void pinMode(uint8_t, uint8_t);
-void digitalWrite(uint8_t, uint8_t);
-int digitalRead(uint8_t);
-int analogRead(uint8_t);
-void analogReference(uint8_t mode);
-void analogWrite(uint8_t, int);
-
-unsigned long millis(void);
-unsigned long micros(void);
-void delay(unsigned long);
-void delayMicroseconds(unsigned int us);
-unsigned long pulseIn(uint8_t pin, uint8_t state, unsigned long timeout);
-
-void shiftOut(uint8_t dataPin, uint8_t clockPin, uint8_t bitOrder, uint8_t val);
-uint8_t shiftIn(uint8_t dataPin, uint8_t clockPin, uint8_t bitOrder);
-
-void attachInterrupt(uint8_t, void (*)(void), int mode);
-void detachInterrupt(uint8_t);
-
-void setup(void);
-void loop(void);
-
-#ifdef __cplusplus
-} // extern "C"
-#endif
-
-#endif
diff --git a/cores/arduino/wiring_analog.c b/cores/arduino/wiring_analog.c
index d248f4c..2b1f3a0 100644
--- a/cores/arduino/wiring_analog.c
+++ b/cores/arduino/wiring_analog.c
@@ -46,8 +46,11 @@ int analogRead(uint8_t pin)
#else
if (pin >= 14) pin -= 14; // allow for channel or pin numbers
#endif
-
-#if defined(ADCSRB) && defined(MUX5)
+
+#if defined(__AVR_ATmega32U4__)
+ pin = analogPinToChannel(pin);
+ ADCSRB = (ADCSRB & ~(1 << MUX5)) | (((pin >> 3) & 0x01) << MUX5);
+#elif defined(ADCSRB) && defined(MUX5)
// the MUX5 bit of ADCSRB selects whether we're reading from channels
// 0 to 7 (MUX5 low) or 8 to 15 (MUX5 high).
ADCSRB = (ADCSRB & ~(1 << MUX5)) | (((pin >> 3) & 0x01) << MUX5);
@@ -222,6 +225,14 @@ void analogWrite(uint8_t pin, int val)
OCR4C = val; // set pwm duty
break;
#endif
+
+ #if defined(TCCR4A) && defined(COM4D1)
+ case TIMER4D:
+ // connect pwm to pin on timer 4, channel D
+ sbi(TCCR4A, COM4D1);
+ OCR4D = val; // set pwm duty
+ break;
+ #endif
#if defined(TCCR5A) && defined(COM5A1)
case TIMER5A:
diff --git a/cores/arduino/wiring_digital.c b/cores/arduino/wiring_digital.c
index 0949da4..dd1b949 100755
--- a/cores/arduino/wiring_digital.c
+++ b/cores/arduino/wiring_digital.c
@@ -136,17 +136,16 @@ void digitalWrite(uint8_t pin, uint8_t val)
out = portOutputRegister(port);
+ uint8_t oldSREG = SREG;
+ cli();
+
if (val == LOW) {
- uint8_t oldSREG = SREG;
- cli();
*out &= ~bit;
- SREG = oldSREG;
} else {
- uint8_t oldSREG = SREG;
- cli();
*out |= bit;
- SREG = oldSREG;
}
+
+ SREG = oldSREG;
}
int digitalRead(uint8_t pin)
diff --git a/cores/arduino/wiring_private.h b/cores/arduino/wiring_private.h
index 11f6f00..41d1d40 100755
--- a/cores/arduino/wiring_private.h
+++ b/cores/arduino/wiring_private.h
@@ -27,11 +27,10 @@
#include <avr/io.h>
#include <avr/interrupt.h>
-#include <avr/delay.h>
#include <stdio.h>
#include <stdarg.h>
-#include "wiring.h"
+#include "Arduino.h"
#ifdef __cplusplus
extern "C"{
diff --git a/programmers.txt b/programmers.txt
index 466f602..b4b12ef 100644
--- a/programmers.txt
+++ b/programmers.txt
@@ -9,6 +9,10 @@ avrispmkii.protocol=stk500v2
usbtinyisp.name=USBtinyISP
usbtinyisp.protocol=usbtiny
+usbasp.name=USBasp
+usbasp.communication=usb
+usbasp.protocol=usbasp
+
parallel.name=Parallel Programmer
parallel.protocol=dapa
parallel.force=true
diff --git a/variants/eightanaloginputs/pins_arduino.h b/variants/eightanaloginputs/pins_arduino.h
new file mode 100644
index 0000000..52b37ef
--- /dev/null
+++ b/variants/eightanaloginputs/pins_arduino.h
@@ -0,0 +1,27 @@
+/*
+ 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 $
+*/
+
+#include "../standard/pins_arduino.h"
+#undef NUM_ANALOG_INPUTS
+#define NUM_ANALOG_INPUTS 8
diff --git a/variants/leonardo/pins_arduino.h b/variants/leonardo/pins_arduino.h
new file mode 100644
index 0000000..cfb4579
--- /dev/null
+++ b/variants/leonardo/pins_arduino.h
@@ -0,0 +1,276 @@
+/*
+ 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>
+
+#define ARDUINO_MODEL_USB_PID 0x0034
+
+#define TX_RX_LED_INIT DDRD |= (1<<5), DDRB |= (1<<0)
+#define TXLED0 PORTD |= (1<<5)
+#define TXLED1 PORTD &= ~(1<<5)
+#define RXLED0 PORTB |= (1<<0)
+#define RXLED1 PORTB &= ~(1<<0)
+
+// 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;
+const static uint8_t A8 = 22;
+const static uint8_t A9 = 23;
+const static uint8_t A10 = 24;
+
+// __AVR_ATmega32U4__ has an unusual mapping of pins to channels
+extern const uint8_t PROGMEM analog_pin_to_channel_PGM[];
+#define analogPinToChannel(P) ( pgm_read_byte( analog_pin_to_channel_PGM + (P) ) )
+
+#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# PC6 ??? OC3A/#OC4A
+// D6# A7 PD7 FastPWM #OC4D/ADC10
+// D7 PE6 INT6/AIN0
+//
+// D8 PB4 ADC11/PCINT4
+// D9# A8 PB5 PWM16 OC1A/#OC4B/ADC13/PCINT5
+// D10# A9 PB6 PWM16 OC1B/0c4B/ADC12/PCINT6
+// D11# PB7 PWM8/16 0C0A/OC1C/#RTS/PCINT7
+// D12 A10 PD6 T1/#OC4D/ADC9
+// 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 PD5
+// RXLED PB0
+// 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,
+ PC, /* 5 */
+ PD, /* 6 */
+ PE, /* 7 */
+
+ PB, /* 8 */
+ PB,
+ PB,
+ PB,
+
+ PD, /* 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), /* 5, port C */
+ _BV(7), /* 6, port D */
+ _BV(6), /* 7, port E */
+
+ _BV(4), /* 8, port B */
+ _BV(5),
+ _BV(6),
+ _BV(7),
+
+ _BV(6), /* 12 port D */
+ _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,
+ NOT_ON_TIMER,
+ NOT_ON_TIMER,
+ TIMER0B, /* 3 */
+ NOT_ON_TIMER,
+ TIMER3A, /* 5 */
+ TIMER4D, /* 6 */
+ NOT_ON_TIMER,
+
+ NOT_ON_TIMER,
+ TIMER1A, /* 9 */
+ TIMER1B, /* 10 */
+ TIMER0A, /* 11 */
+
+ NOT_ON_TIMER,
+ TIMER4A, /* 13 */
+
+ NOT_ON_TIMER,
+ NOT_ON_TIMER,
+};
+
+const uint8_t PROGMEM analog_pin_to_channel_PGM[11] = {
+ /*
+ A0 PF7 ADC7
+ A1 PF6 ADC6
+ A2 PF5 ADC5
+ A3 PF4 ADC4
+ A4 PF1 ADC1
+ A5 PF0 ADC0
+ D4 A6 PD4 ADC8
+ D6 A7 PD7 FastPWM #OC4D/ADC10
+ D9 A8 PB5 PWM16 OC1A/#OC4B/ADC13/PCINT5
+ D10 A9 PB6 PWM16 OC1B/0c4B/ADC12/PCINT6
+ D12 A10 PD6 T1/#OC4D/ADC9
+ */
+
+ 7,
+ 6,
+ 5,
+ 4,
+ 1,
+ 0,
+ 8,
+ 10,
+
+ 13,
+ 12,
+ 9
+};
+
+#endif
+
+#endif \ No newline at end of file
diff --git a/cores/arduino/pins_arduino.c b/variants/mega/pins_arduino.h
index 62e10ad..62e10ad 100755
--- a/cores/arduino/pins_arduino.c
+++ b/variants/mega/pins_arduino.h
diff --git a/variants/micro/pins_arduino.h b/variants/micro/pins_arduino.h
new file mode 100644
index 0000000..c9f25eb
--- /dev/null
+++ b/variants/micro/pins_arduino.h
@@ -0,0 +1,27 @@
+/*
+ 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 $
+*/
+
+#include "../leonardo/pins_arduino.h"
+#undef ARDUINO_MODEL_USB_PID
+#define ARDUINO_MODEL_USB_PID 0x0035 \ No newline at end of file
diff --git a/variants/standard/pins_arduino.h b/variants/standard/pins_arduino.h
new file mode 100644
index 0000000..3999d1f
--- /dev/null
+++ b/variants/standard/pins_arduino.h
@@ -0,0 +1,218 @@
+/*
+ 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>
+
+#define NUM_DIGITAL_PINS 20
+#define NUM_ANALOG_INPUTS 6
+#define analogInputToDigitalPin(p) ((p < 6) ? (p) + 14 : -1)
+
+#if defined(__AVR_ATmega8__)
+#define digitalPinHasPWM(p) ((p) == 9 || (p) == 10 || (p) == 11)
+#else
+#define digitalPinHasPWM(p) ((p) == 3 || (p) == 5 || (p) == 6 || (p) == 9 || (p) == 10 || (p) == 11)
+#endif
+
+const static uint8_t SS = 10;
+const static uint8_t MOSI = 11;
+const static uint8_t MISO = 12;
+const static uint8_t SCK = 13;
+
+const static uint8_t SDA = 18;
+const static uint8_t SCL = 19;
+const static uint8_t LED = 13;
+
+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;
+
+#define digitalPinToPCICR(p) (((p) >= 0 && (p) <= 21) ? (&PCICR) : ((uint8_t *)0))
+#define digitalPinToPCICRbit(p) (((p) <= 7) ? 2 : (((p) <= 13) ? 0 : 1))
+#define digitalPinToPCMSK(p) (((p) <= 7) ? (&PCMSK2) : (((p) <= 13) ? (&PCMSK0) : (((p) <= 21) ? (&PCMSK1) : ((uint8_t *)0))))
+#define digitalPinToPCMSKbit(p) (((p) <= 7) ? (p) : (((p) <= 13) ? ((p) - 8) : ((p) - 14)))
+
+#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
+
+
+// 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,
+};
+
+const uint16_t PROGMEM port_to_output_PGM[] = {
+ NOT_A_PORT,
+ NOT_A_PORT,
+ (uint16_t) &PORTB,
+ (uint16_t) &PORTC,
+ (uint16_t) &PORTD,
+};
+
+const uint16_t PROGMEM port_to_input_PGM[] = {
+ NOT_A_PORT,
+ NOT_A_PORT,
+ (uint16_t) &PINB,
+ (uint16_t) &PINC,
+ (uint16_t) &PIND,
+};
+
+const uint8_t PROGMEM digital_pin_to_port_PGM[] = {
+ PD, /* 0 */
+ PD,
+ PD,
+ PD,
+ PD,
+ PD,
+ PD,
+ PD,
+ PB, /* 8 */
+ PB,
+ PB,
+ PB,
+ PB,
+ PB,
+ PC, /* 14 */
+ PC,
+ PC,
+ PC,
+ PC,
+ PC,
+};
+
+const uint8_t PROGMEM digital_pin_to_bit_mask_PGM[] = {
+ _BV(0), /* 0, port D */
+ _BV(1),
+ _BV(2),
+ _BV(3),
+ _BV(4),
+ _BV(5),
+ _BV(6),
+ _BV(7),
+ _BV(0), /* 8, port B */
+ _BV(1),
+ _BV(2),
+ _BV(3),
+ _BV(4),
+ _BV(5),
+ _BV(0), /* 14, port C */
+ _BV(1),
+ _BV(2),
+ _BV(3),
+ _BV(4),
+ _BV(5),
+};
+
+const uint8_t PROGMEM digital_pin_to_timer_PGM[] = {
+ NOT_ON_TIMER, /* 0 - port D */
+ NOT_ON_TIMER,
+ NOT_ON_TIMER,
+ // on the ATmega168, digital pin 3 has hardware pwm
+#if defined(__AVR_ATmega8__)
+ NOT_ON_TIMER,
+#else
+ TIMER2B,
+#endif
+ NOT_ON_TIMER,
+ // on the ATmega168, digital pins 5 and 6 have hardware pwm
+#if defined(__AVR_ATmega8__)
+ NOT_ON_TIMER,
+ NOT_ON_TIMER,
+#else
+ TIMER0B,
+ TIMER0A,
+#endif
+ NOT_ON_TIMER,
+ NOT_ON_TIMER, /* 8 - port B */
+ TIMER1A,
+ TIMER1B,
+#if defined(__AVR_ATmega8__)
+ TIMER2,
+#else
+ TIMER2A,
+#endif
+ NOT_ON_TIMER,
+ NOT_ON_TIMER,
+ NOT_ON_TIMER,
+ NOT_ON_TIMER, /* 14 - port C */
+ NOT_ON_TIMER,
+ NOT_ON_TIMER,
+ NOT_ON_TIMER,
+ NOT_ON_TIMER,
+};
+
+#endif
+
+#endif \ No newline at end of file