Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 0ecdd72

Browse files
committedAug 11, 2011
diskloader source and binary for 32u4
1 parent cdf887b commit 0ecdd72

File tree

12 files changed

+1902
-1
lines changed

12 files changed

+1902
-1
lines changed
 

‎hardware/arduino/boards.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ leonardo.bootloader.low_fuses=0xde
88
leonardo.bootloader.high_fuses=0xd8
99
leonardo.bootloader.extended_fuses=0xf3
1010
leonardo.bootloader.path=diskloader
11-
leonardo.bootloader.file=diskloader_atmega32u4.hex
11+
leonardo.bootloader.file=DiskLoader.hex
1212
leonardo.bootloader.unlock_bits=0x3F
1313
leonardo.bootloader.lock_bits=0x0F
1414
leonardo.build.mcu=atmega32u4
Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
;************************************************************
2+
; Windows USB CDC ACM Setup File
3+
; Copyright (c) 2000 Microsoft Corporation
4+
5+
6+
[Version]
7+
Signature="$Windows NT$"
8+
Class=Ports
9+
ClassGuid={4D36E978-E325-11CE-BFC1-08002BE10318}
10+
Provider=%MFGNAME%
11+
LayoutFile=layout.inf
12+
CatalogFile=%MFGFILENAME%.cat
13+
DriverVer=11/15/2007,5.1.2600.0
14+
15+
[Manufacturer]
16+
%MFGNAME%=DeviceList, NTamd64
17+
18+
[DestinationDirs]
19+
DefaultDestDir=12
20+
21+
22+
;------------------------------------------------------------------------------
23+
; Windows 2000/XP/Vista-32bit Sections
24+
;------------------------------------------------------------------------------
25+
26+
[DriverInstall.nt]
27+
include=mdmcpq.inf
28+
CopyFiles=DriverCopyFiles.nt
29+
AddReg=DriverInstall.nt.AddReg
30+
31+
[DriverCopyFiles.nt]
32+
usbser.sys,,,0x20
33+
34+
[DriverInstall.nt.AddReg]
35+
HKR,,DevLoader,,*ntkern
36+
HKR,,NTMPDriver,,%DRIVERFILENAME%.sys
37+
HKR,,EnumPropPages32,,"MsPorts.dll,SerialPortPropPageProvider"
38+
39+
[DriverInstall.nt.Services]
40+
AddService=usbser, 0x00000002, DriverService.nt
41+
42+
[DriverService.nt]
43+
DisplayName=%SERVICE%
44+
ServiceType=1
45+
StartType=3
46+
ErrorControl=1
47+
ServiceBinary=%12%\%DRIVERFILENAME%.sys
48+
49+
;------------------------------------------------------------------------------
50+
; Vista-64bit Sections
51+
;------------------------------------------------------------------------------
52+
53+
[DriverInstall.NTamd64]
54+
include=mdmcpq.inf
55+
CopyFiles=DriverCopyFiles.NTamd64
56+
AddReg=DriverInstall.NTamd64.AddReg
57+
58+
[DriverCopyFiles.NTamd64]
59+
%DRIVERFILENAME%.sys,,,0x20
60+
61+
[DriverInstall.NTamd64.AddReg]
62+
HKR,,DevLoader,,*ntkern
63+
HKR,,NTMPDriver,,%DRIVERFILENAME%.sys
64+
HKR,,EnumPropPages32,,"MsPorts.dll,SerialPortPropPageProvider"
65+
66+
[DriverInstall.NTamd64.Services]
67+
AddService=usbser, 0x00000002, DriverService.NTamd64
68+
69+
[DriverService.NTamd64]
70+
DisplayName=%SERVICE%
71+
ServiceType=1
72+
StartType=3
73+
ErrorControl=1
74+
ServiceBinary=%12%\%DRIVERFILENAME%.sys
75+
76+
77+
;------------------------------------------------------------------------------
78+
; Vendor and Product ID Definitions
79+
;------------------------------------------------------------------------------
80+
; When developing your USB device, the VID and PID used in the PC side
81+
; application program and the firmware on the microcontroller must match.
82+
; Modify the below line to use your VID and PID. Use the format as shown below.
83+
; Note: One INF file can be used for multiple devices with different VID and PIDs.
84+
; For each supported device, append ",USB\VID_xxxx&PID_yyyy" to the end of the line.
85+
;------------------------------------------------------------------------------
86+
[SourceDisksFiles]
87+
[SourceDisksNames]
88+
[DeviceList]
89+
%DESCRIPTION%=DriverInstall, USB\VID_2341&PID_0020&MI_00
90+
91+
[DeviceList.NTamd64]
92+
%DESCRIPTION%=DriverInstall, USB\VID_2341&PID_0020&MI_00
93+
94+
95+
;------------------------------------------------------------------------------
96+
; String Definitions
97+
;------------------------------------------------------------------------------
98+
;Modify these strings to customize your device
99+
;------------------------------------------------------------------------------
100+
[Strings]
101+
MFGFILENAME="CDC_vista"
102+
DRIVERFILENAME ="usbser"
103+
MFGNAME="Arduino LLC (www.arduino.cc)"
104+
INSTDISK="Arduino Leonardo Driver Installer"
105+
DESCRIPTION="Arduino Lenonardo"
106+
SERVICE="USB RS-232 Emulation Driver"
Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
:1078000011241FBECFEFDAE0DEBFCDBFC4C04101FF
2+
:1078100042144505560455026403740300001E9586
3+
:1078200087020110030000C18081C106C0FF0A0069
4+
:107830000CA10185037508150026FF00954009017C
5+
:107840008102954009029102C0040309041A033021
6+
:1078500000300030003000300030003000300031A7
7+
:1078600000370030003100120100020200004041E8
8+
:1078700023200000010000010112010002000000AD
9+
:1078800040412320000001000001010902640003BF
10+
:1078900001008032080B000202020100090400000E
11+
:1078A0000102020000052400100105240101010469
12+
:1078B0002402020524060001070581031000400987
13+
:1078C000040100020A000000070502024000000750
14+
:1078D00005830240000009040200010300000009C2
15+
:1078E0002101010001221E00070584034000400021
16+
:1078F00080918A0190918B01019690938B018093E6
17+
:107900008A01089581E08093E0001092E200EE2762
18+
:10791000FF27099408950F931F93CF93DF93982F18
19+
:10792000EB01042F10E088E76030780730F411E0B5
20+
:1079300083E0FB0180935700E895892F68E071E0B0
21+
:10794000402FCED0112311F107B600FCFDCF402F00
22+
:107950004695FE01A8E0B1E020E031E009C08D913C
23+
:107960009D910C0130935700E895112432962F5FBA
24+
:107970002417A8F385E0FE0180935700E89507B629
25+
:1079800000FCFDCF81E180935700E895DF91CF9116
26+
:107990001F910F9108958BD084E18093880180E13D
27+
:1079A00080938901E0E0F0E0859194918F5F9F4F93
28+
:1079B00019F081E080938D01EE24FF24DD24D3941F
29+
:1079C000D6D1082F8EE098E7FC01249131960296DB
30+
:1079D0004491201711F02223B9F710928F011092D1
31+
:1079E0008E0182E068E071E07BD0013479F460912F
32+
:1079F00008016058633028F0683111F064E001C07C
33+
:107A000063E0C62FD0E0CF5DD74808C0063579F4D3
34+
:107A100080910801803321F0C5E2D8E711E029C048
35+
:107A200080910A01C82FD0E0C25ED748F7CF053752
36+
:107A300021F4CEE1D8E713E01CC0053539F4E0901D
37+
:107A40000801F0900901EE0CFF1C0AC0043631F465
38+
:107A500082E0B701409109015EDF02C0043721F0E6
39+
:107A6000C5E2D8E710E005C010910901E701E10E79
40+
:107A7000F11C7DD18097B1F483E068E871E041E0CA
41+
:107A800050E044D0112329F0412F50E083E8BE019B
42+
:107A90003DD083E469E871E041E050E037D0013542
43+
:107AA00009F08ECFD0928D01EE24FF2489CF109261
44+
:107AB0008F0110928E0110928C0110928D0181E045
45+
:107AC0008093D70080EA8093D80082E189BD09B411
46+
:107AD00000FEFDCF80E98093D8001092E000089569
47+
:107AE000FB018093E9009BE60EC08091E80085FFD2
48+
:107AF000FCCF8091F10081938091E80085FD02C068
49+
:107B00009093E8004150442381F70895982FFB019A
50+
:107B1000282F207287708093E9003AE314C0809187
51+
:107B2000E80085FFFCCF97FF02C0849101C08081EF
52+
:107B30003196211180E08093F1008091E80085FD6D
53+
:107B400002C03093E800415050408FEF4F3F58073C
54+
:107B500031F796FF03C08AE38093E800089580918F
55+
:107B60009101813279F420E030E003C0808180937C
56+
:107B7000F100F901E050FF4F2F5F3F4F28303105F2
57+
:107B8000A9F71EC08032B1F48091E80082FFFCCFDB
58+
:107B900020E030E003C08091F1008083F901E050E3
59+
:107BA000FF4F2F5F3F4F28303105A9F78BEF8093B0
60+
:107BB000E80006C0823221F480919201809307018F
61+
:107BC00081E0089520919301223231F1213081F436
62+
:107BD0008091960190919701089711F42093980154
63+
:107BE00080919801882309F03AC0E9E7F8E739C0A5
64+
:107BF000223021F484E6EBE8F8E711C0233069F580
65+
:107C000080919201882319F4E9E4F8E72AC08130D1
66+
:107C100021F5EDE4F8E725C081E0EBE2F8E790918B
67+
:107C20009601282F981708F4292F90E03EEF8091B5
68+
:107C3000E8008570E1F38091E80082FD0EC09F5F4F
69+
:107C400084918093F100892F8F7311F43093E800B1
70+
:107C50003196921760F381E0089580E00895E7E699
71+
:107C6000F8E78491DCCF1092E9008091E80083FF6F
72+
:107C70005FC0E0E9F1E003C08091F100819381E011
73+
:107C8000E839F807C9F782EF8093E8008091900106
74+
:107C900087FF05C08091E80080FFFCCF03C08EEF16
75+
:107CA0008093E80080919101853051F48091E80043
76+
:107CB00080FFFCCF8091920180688093E30031C007
77+
:107CC0008930E1F48091920180938C01E7E2F8E73A
78+
:107CD00091E031E026E39093E9003093EB0084914A
79+
:107CE0008093EC002093ED009F5F3196953099F7DB
80+
:107CF0008EE78093EA001092EA0013C0883029F4DE
81+
:107D000080918C018093F1000CC0882319F41092AB
82+
:107D1000F10007C0863011F455DF01C020DF882351
83+
:107D200021F08EEF8093E800089581E28093EB00CC
84+
:107D300008958091E1001092E100282F83FF0CC08C
85+
:107D40001092E90081E08093EB001092EC0082E356
86+
:107D50008093ED0010928C0122FF08C080918D016C
87+
:107D6000882321F410928F0110928E0108951F93A1
88+
:107D700012E079DFDEDF1093E9008091E80085FFF3
89+
:107D80000DC09091F1008091E80085FD03C08BE665
90+
:107D90008093E800892F90E01F91089580918E01D3
91+
:107DA00090918F01019790938F0180938E01809124
92+
:107DB0008E0190918F01892B09F4A4DD99DDD9CF33
93+
:087DC00000E1000000000000DA
94+
:040000030000780081
95+
:00000001FF
Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
###############################################################################
2+
# Makefile for DiskLoader
3+
###############################################################################
4+
5+
## General Flags
6+
PROJECT = DiskLoader
7+
TARGET = DiskLoader.elf
8+
CC = avr-gcc
9+
10+
# BOARD2
11+
MCU = atmega32u4
12+
AVR_FREQ = 16000000L
13+
14+
15+
# Change if your programmer is different
16+
AVRDUDE_PROGRAMMER = avrispmkII
17+
AVRDUDE_PORT = usb
18+
19+
# program name should not be changed...
20+
PROGRAM = DiskLoader
21+
22+
AVRDUDE = avrdude
23+
AVRDUDE_FLAGS = -p $(MCU) -P $(AVRDUDE_PORT) -c $(AVRDUDE_PROGRAMMER) -p $(MCU)
24+
25+
## Options common to compile, link and assembly rules
26+
COMMON = -mmcu=$(MCU)
27+
28+
override CFLAGS = -g -Wall -Os -mmcu=$(MCU) -DF_CPU=$(AVR_FREQ) $(DEFS) -ffunction-sections -gdwarf-2 -fdata-sections -fno-split-wide-types
29+
30+
## Assembly specific flags
31+
ASMFLAGS = $(COMMON)
32+
ASMFLAGS += $(CFLAGS)
33+
ASMFLAGS += -x assembler-with-cpp -Wa,-gdwarf2
34+
35+
## Linker flags
36+
LDFLAGS = $(COMMON)
37+
LDFLAGS += -Wl,-gc-sections,-Map=DiskLoader.map,--section-start=.text=0x7800,--relax
38+
LDFLAGS += -nodefaultlibs -nostartfiles
39+
40+
41+
## Intel Hex file production flags
42+
HEX_EEPROM_FLAGS = -j .eeprom
43+
HEX_EEPROM_FLAGS += --set-section-flags=.eeprom="alloc,load"
44+
HEX_EEPROM_FLAGS += --change-section-lma .eeprom=0 --no-change-warnings
45+
46+
## Objects explicitly added by the user
47+
LINKONLYOBJECTS =
48+
49+
MODULES := .
50+
SRC_DIR := $(addprefix src/,$(MODULES))
51+
BUILD_DIR := $(addprefix build/,$(MODULES))
52+
53+
SRC := $(foreach sdir,$(SRC_DIR),$(wildcard $(sdir)/*.cpp))
54+
OBJ := $(patsubst src/%.cpp,build/%.o,$(SRC))
55+
DEP := $(OBJ:%.o=%.d)
56+
INCLUDES := $(addprefix -I,$(SRC_DIR))
57+
58+
vpath %.cpp $(SRC_DIR)
59+
60+
.PHONY: all checkdirs clean
61+
62+
all: checkdirs $(TARGET) DiskLoader.hex DiskLoader.lss size
63+
64+
-include $(DEP)
65+
66+
checkdirs: $(BUILD_DIR)
67+
68+
$(BUILD_DIR):
69+
@mkdir -p $@
70+
71+
clean:
72+
@rm -rf build/
73+
@rm -f *.hex
74+
@rm -f *.elf
75+
@rm -f *.lss
76+
@rm -f *.map
77+
78+
define make-goal
79+
$1/%.o: %.cpp
80+
$(CC) $(INCLUDES) $(CFLAGS) -c $$< -MD -o $$@
81+
endef
82+
83+
$(foreach bdir,$(BUILD_DIR),$(eval $(call make-goal,$(bdir))))
84+
85+
$(TARGET): $(OBJ)
86+
$(CC) $(LDFLAGS) $(LINKONLYOBJECTS) $(LIBDIRS) $(LIBS) $^ -o $@
87+
88+
%.hex: $(TARGET)
89+
avr-objcopy -O ihex $(HEX_FLASH_FLAGS) $< $@
90+
91+
%.lss: $(TARGET)
92+
avr-objdump -h -S $< > $@
93+
94+
size: $(TARGET)
95+
@echo
96+
# @avr-size -C --mcu=${MCU} ${TARGET}.elf
97+
98+
program: $(TARGET).hex
99+
$(AVRDUDE) $(AVRDUDE_FLAGS) -B 5 -u -U flash:w:$(TARGET).hex
Lines changed: 246 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,246 @@
1+
2+
3+
#include "Platform.h"
4+
5+
// This bootloader creates a composite Serial/Mass storage device
6+
//
7+
// The serial interface supports a STK500v1 protocol that is very similar to optiboot
8+
// The mass storage interface emulates a disk image that is used to read or write firmware.bin files
9+
// Delete firmware.bin and copy a new binary (not .hex) file to flash the firmware i.e:
10+
//
11+
// cp firmware.bin f:/. -f
12+
//
13+
// Ejecting the disk will run the firmware
14+
//
15+
// The bootloader will timeout and start the firmware after a few hundred milliseconds
16+
// if a usb connection is not detected.
17+
//
18+
// The tweakier code is to keep the bootloader below 2k (no interrupt table, for example)
19+
20+
extern "C"
21+
void entrypoint(void) __attribute__ ((naked)) __attribute__ ((section (".vectors")));
22+
void entrypoint(void)
23+
{
24+
asm volatile (
25+
"eor r1, r1\n" // Zero register
26+
"out 0x3F, r1\n" // SREG
27+
"ldi r28, 0xFF\n"
28+
"ldi r29, 0x0A\n"
29+
"out 0x3E, r29\n" // SPH
30+
"out 0x3D, r28\n" // SPL
31+
"rjmp main" // Stack is all set up, start the main code
32+
::);
33+
}
34+
35+
u8 _flashbuf[128];
36+
u8 _inSync;
37+
u8 _ok;
38+
extern volatile u8 _ejected;
39+
extern volatile u16 _timeout;
40+
41+
void Program(u8 ep, u16 page, u8 count)
42+
{
43+
u8 write = page < 30*1024; // Don't write over firmware please
44+
if (write)
45+
boot_page_erase(page);
46+
47+
Recv(ep,_flashbuf,count); // Read while page is erasing
48+
49+
if (!write)
50+
return;
51+
52+
boot_spm_busy_wait(); // Wait until the memory is erased.
53+
54+
count >>= 1;
55+
u16* p = (u16*)page;
56+
u16* b = (u16*)_flashbuf;
57+
for (u8 i = 0; i < count; i++)
58+
boot_page_fill(p++, b[i]);
59+
60+
boot_page_write(page);
61+
boot_spm_busy_wait();
62+
boot_rww_enable ();
63+
}
64+
65+
66+
int USBGetChar();
67+
#define getch USBGetChar
68+
69+
#define HW_VER 0x02
70+
#define SW_MAJOR 0x01
71+
#define SW_MINOR 0x10
72+
73+
#define STK_OK 0x10
74+
#define STK_INSYNC 0x14 // ' '
75+
#define CRC_EOP 0x20 // 'SPACE'
76+
#define STK_GET_SYNC 0x30 // '0'
77+
78+
#define STK_GET_PARAMETER 0x41 // 'A'
79+
#define STK_SET_DEVICE 0x42 // 'B'
80+
#define STK_SET_DEVICE_EXT 0x45 // 'E'
81+
#define STK_LOAD_ADDRESS 0x55 // 'U'
82+
#define STK_UNIVERSAL 0x56 // 'V'
83+
#define STK_PROG_PAGE 0x64 // 'd'
84+
#define STK_READ_PAGE 0x74 // 't'
85+
#define STK_READ_SIGN 0x75 // 'u'
86+
87+
extern const u8 _readSize[] PROGMEM;
88+
const u8 _readSize[] =
89+
{
90+
STK_GET_PARAMETER, 1,
91+
STK_SET_DEVICE, 20,
92+
STK_SET_DEVICE_EXT, 5,
93+
STK_UNIVERSAL, 4,
94+
STK_LOAD_ADDRESS, 2,
95+
STK_PROG_PAGE, 3,
96+
STK_READ_PAGE, 3,
97+
0,0
98+
};
99+
100+
extern const u8 _consts[] PROGMEM;
101+
const u8 _consts[] =
102+
{
103+
SIGNATURE_0,
104+
SIGNATURE_1,
105+
SIGNATURE_2,
106+
HW_VER, // Hardware version
107+
SW_MAJOR, // Software major version
108+
SW_MINOR, // Software minor version
109+
0x03, // Unknown but seems to be required by avr studio 3.56
110+
0x00, //
111+
};
112+
113+
114+
void USBInit(void);
115+
extern u8 _sector3[512];
116+
117+
int main(void) __attribute__ ((naked));
118+
119+
// STK500v1 main loop, very similar to optiboot in protocol and implementation
120+
int main()
121+
{
122+
BOARD_INIT();
123+
USBInit();
124+
125+
#ifdef WRITABLE_DIRECTORY
126+
_sector3[0] = 0;
127+
#endif
128+
129+
_inSync = STK_INSYNC;
130+
_ok = STK_OK;
131+
#ifndef MSC_ENABLED
132+
if (pgm_read_word(0) != -1)
133+
_ejected = 1;
134+
#endif
135+
for(;;)
136+
{
137+
u8* packet = _flashbuf;
138+
u16 address = 0;
139+
for (;;)
140+
{
141+
u8 cmd = getch();
142+
143+
// Read packet contents
144+
u8 len;
145+
const u8* rs = _readSize;
146+
for(;;)
147+
{
148+
u8 c = pgm_read_byte(rs++);
149+
len = pgm_read_byte(rs++);
150+
if (c == cmd || c == 0)
151+
break;
152+
}
153+
#ifndef MSC_ENABLED
154+
_timeout = 0;
155+
#endif
156+
// Read params
157+
Recv(CDC_RX,packet,len);
158+
159+
// Send a response
160+
u8 send = 0;
161+
const u8* pgm = _consts+7; // 0
162+
if (STK_GET_PARAMETER == cmd)
163+
{
164+
u8 i = packet[0] - 0x80;
165+
if (i > 2)
166+
i = (i == 0x18) ? 3 : 4; // 0x80:HW_VER,0x81:SW_MAJOR,0x82:SW_MINOR,0x18:3 or 0
167+
pgm = _consts + i + 3;
168+
send = 1;
169+
}
170+
171+
else if (STK_UNIVERSAL == cmd)
172+
{
173+
if (packet[0] == 0x30)
174+
pgm = _consts + packet[2]; // read signature
175+
send = 1;
176+
}
177+
178+
// Read signature bytes
179+
else if (STK_READ_SIGN == cmd)
180+
{
181+
pgm = _consts;
182+
send = 3;
183+
}
184+
185+
else if (STK_LOAD_ADDRESS == cmd)
186+
{
187+
address = *((u16*)packet); // word addresses
188+
address += address;
189+
}
190+
191+
else if (STK_PROG_PAGE == cmd)
192+
{
193+
Program(CDC_RX,address,packet[1]);
194+
}
195+
196+
else if (STK_READ_PAGE == cmd)
197+
{
198+
send = packet[1];
199+
pgm = (const u8*)address;
200+
address += send; // not sure of this is required
201+
}
202+
203+
// Check sync
204+
if (getch() != ' ')
205+
break;
206+
Transfer(CDC_TX,&_inSync,1);
207+
208+
// Send result
209+
if (send)
210+
Transfer(CDC_TX|TRANSFER_PGM,pgm,send); // All from pgm memory
211+
212+
// Send ok
213+
Transfer(CDC_TX|TRANSFER_RELEASE,&_ok,1);
214+
215+
if (cmd == 'Q')
216+
break;
217+
}
218+
_ejected = 1;
219+
}
220+
}
221+
222+
// Nice breathing LED indicates we are in the firmware
223+
u16 _pulse;
224+
void LEDPulse()
225+
{
226+
_pulse++;
227+
u8 p = _pulse >> 8;
228+
if (p > 127)
229+
p = 255-p;
230+
p += p;
231+
if (((u8)_pulse) > p)
232+
LED0;
233+
else
234+
LED1;
235+
}
236+
237+
void Reboot()
238+
{
239+
UDCON = 1; // Detatch USB
240+
UDIEN = 0;
241+
asm volatile ( // Reset vector to run firmware
242+
"clr r30\n"
243+
"clr r31\n"
244+
"ijmp\n"
245+
::);
246+
}
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
2+
#include <inttypes.h>
3+
#include <avr/io.h>
4+
#include <avr/pgmspace.h>
5+
#include <avr/boot.h>
6+
#include <util/delay.h>
7+
#include <avr/interrupt.h>
8+
9+
#include <stdio.h>
10+
#include <stdlib.h>
11+
#include <string.h>
12+
13+
typedef unsigned char u8;
14+
typedef unsigned short u16;
15+
typedef unsigned long u32;
16+
17+
//#define MICROTOUCH
18+
19+
//#define WRITABLE_DIRECTORY // undef saved 56 + 512 RAM
20+
21+
#define FAT_DISK_LABEL 'b','o','o','t','l','o','a','d','e','r',' ' // 11 chars (undef saves 12)
22+
#define FAT_FILE_NAME 'F','I','R','M','W','A','R','E','B','I','N' // 11 chars
23+
#define MSC_DISK_SERIAL '0','0','0','0','0','0','0','0','1','7','0','1' // 12 chars
24+
25+
26+
#define CPU_PRESCALE(n) (CLKPR = 0x80, CLKPR = (n))
27+
#define DISABLE_JTAG() MCUCR = (1 << JTD) | (1 << IVCE) | (0 << PUD); MCUCR = (1 << JTD) | (0 << IVSEL) | (0 << IVCE) | (0 << PUD);
28+
29+
30+
#ifdef MICROTOUCH
31+
32+
#define USB_VID 0x239A // Adafruit vid
33+
#define USB_PID 0x2001 // sorry dave sprach zarathustra
34+
35+
#define FAT_OEM_NAME 'r','o','s','s','u','m','u','r' // 8 chars
36+
#define BOARD_INIT() DDRC = 0xFF; CPU_PRESCALE(0); DISABLE_JTAG();
37+
#define LED0 PORTC &= ~(1<<7)
38+
#define LED1 PORTC |= (1<<7)
39+
#define BACKLIGHT0 PORTC &= ~(1<<6)
40+
#define BACKLIGHT1 PORTC |= (1<<6)
41+
42+
#else
43+
44+
#define USB_VID 0x2341 // arduino LLC vid
45+
#define USB_PID 0x0020 // educated guess
46+
47+
#define FAT_OEM_NAME 'l','e','o','n','a','r','d','o' // 8 chars
48+
#define BOARD_INIT() // etc
49+
#define LED0
50+
#define LED1
51+
52+
#endif
53+
54+
#define TRANSFER_PGM 0x80
55+
#define TRANSFER_RELEASE 0x40
56+
#define TRANSFER_ZERO 0x20
57+
58+
void Transfer(u8 ep, const u8* data, int len);
59+
void Recv(u8 ep, u8* dst, u8 len);
60+
void Program(u8 ep, u16 page, u8 count);
61+
62+
#define CDC_ENABLED
63+
//#define MSC_ENABLED
64+
//#define MSC_NO_MEDIA // MSC is enabled but not media inserted
65+
66+
#include "USBCore.h"
67+
#include "USBDesc.h"
68+
69+
Lines changed: 213 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,213 @@
1+
2+
3+
/* Copyright (c) 2010, Peter Barrett
4+
**
5+
** Permission to use, copy, modify, and/or distribute this software for
6+
** any purpose with or without fee is hereby granted, provided that the
7+
** above copyright notice and this permission notice appear in all copies.
8+
**
9+
** THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL
10+
** WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED
11+
** WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR
12+
** BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES
13+
** OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
14+
** WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,
15+
** ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
16+
** SOFTWARE.
17+
*/
18+
19+
#include "Platform.h"
20+
21+
#ifdef MSC_ENABLED
22+
23+
// SCSI Commands
24+
#define TEST_UNIT_READY 0x00
25+
#define REQUEST_SENSE 0x03
26+
#define FORMAT_UNIT 0x04
27+
#define INQUIRY 0x12
28+
#define MODE_SENSE 0x1A
29+
#define START_STOP_UNIT 0x1B
30+
#define MEDIA_REMOVAL 0x1E
31+
#define READ_FORMAT_CAPACITIES 0x23
32+
#define READ_CAPACITY 0x25
33+
#define READ10 0x28
34+
#define WRITE10 0x2A
35+
#define VERIFY10 0x2F
36+
37+
38+
// In FakeFAT
39+
void SendSector(u8 b);
40+
void RecvSector(u8 b);
41+
42+
// In USBCore
43+
u8 RecvPacket(u8 ep, u8* dst, u8 len);
44+
u8 SendPacket(u8 ep, const u8* src, u8 len);
45+
void Reboot();
46+
47+
#define MSC_TX MSC_ENDPOINT_IN
48+
#define MSC_RX MSC_ENDPOINT_OUT
49+
50+
extern const u8 P_Inquiry[36] PROGMEM;
51+
extern const u8 P_RequestSense[18] PROGMEM;
52+
extern const u8 P_ReadCapacity[8-3] PROGMEM;
53+
extern const u8 P_ModeSense[4] PROGMEM;
54+
55+
typedef struct
56+
{
57+
u8 signature[4];
58+
u8 tag[4];
59+
u32 transferLength;
60+
u8 flags;
61+
u8 lun;
62+
u8 cblength;
63+
u8 cb[16];
64+
} CBW;
65+
66+
const u8 P_Inquiry[36] =
67+
{
68+
0x00, // Direct Access Device
69+
0x80, // Removable Medium
70+
0x00, // Version
71+
0x01,
72+
73+
36-4, // Additional Length
74+
0x80, // Storage Controller Component
75+
0x00,
76+
0x00,
77+
78+
'r','o','s','s','u','m','u','r', // Vendor
79+
80+
'm','i','c','r','o','t','o','u', // Product
81+
'c','h',' ',' ',' ',' ',' ',' ',
82+
83+
'2','.','4',' ' // Revision
84+
};
85+
86+
const u8 P_RequestSense[18] =
87+
{
88+
0x70, // Response Code
89+
0x00,
90+
// 0x02, // Sense Key
91+
0x00,
92+
0x00,
93+
94+
0x00,
95+
0x00,
96+
0x00,
97+
0x0A, // Additional Length
98+
99+
0x00,
100+
0x00,
101+
0x00,
102+
0x00,
103+
104+
0x30, // ASC
105+
0x01, // ASCQ
106+
107+
0x00,
108+
0x00,
109+
0x00,
110+
0x00
111+
};
112+
113+
// Save 3 bytes if merging
114+
const u8 P_ReadCapacity[8-3] =
115+
{
116+
// 0x00,
117+
// 0x00,
118+
// 0x00, // Steal 3 zero bytes from end of P_RequestSense
119+
64+4, // block count 64 + 4
120+
0x00,
121+
0x00,
122+
0x02, // block size = 512
123+
0x00
124+
};
125+
126+
const u8 P_ModeSense[4] =
127+
{
128+
0x03,
129+
0x00,
130+
0x00,
131+
0x00
132+
};
133+
134+
extern volatile u8 _ejected;
135+
136+
void SCSI(CBW& cbw)
137+
{
138+
const u8* pgm = 0;
139+
u8 status = 0;
140+
u8 blen = 0;
141+
142+
u8 cmd = cbw.cb[0];
143+
if (START_STOP_UNIT == cmd)
144+
{
145+
_ejected = 1; // Stopped; going away
146+
}
147+
else if (TEST_UNIT_READY == cmd || MEDIA_REMOVAL == cmd)
148+
{
149+
#ifdef MSC_NO_MEDIA
150+
status = 1;
151+
#else
152+
status = _ejected;
153+
#endif
154+
}
155+
else if (REQUEST_SENSE == cmd)
156+
{
157+
pgm = P_RequestSense;
158+
blen = sizeof(P_RequestSense);
159+
}
160+
else if (INQUIRY == cmd)
161+
{
162+
pgm = P_Inquiry;
163+
blen = sizeof(P_Inquiry);
164+
}
165+
else if (WRITE10 == cmd || READ10 == cmd)
166+
{
167+
// Write 10 byte form
168+
// Read 10 byte form
169+
{
170+
u8 lba = cbw.cb[5];
171+
u8 count = cbw.cb[8];
172+
u8 read = READ10 == cbw.cb[0];
173+
while (count--)
174+
{
175+
if (read)
176+
SendSector(lba);
177+
else
178+
RecvSector(lba);
179+
lba++;
180+
cbw.transferLength -= 512; // 24 bytes
181+
}
182+
}
183+
}
184+
else if (MODE_SENSE == cmd)
185+
{
186+
pgm = P_ModeSense;
187+
blen = 4;
188+
}
189+
else
190+
{
191+
pgm = P_ReadCapacity-3;
192+
blen = 8;
193+
if (READ_CAPACITY != cmd)
194+
status = 1; // Faking a data phase for unknown commands
195+
}
196+
197+
if (pgm)
198+
Transfer(MSC_TX | TRANSFER_PGM | TRANSFER_RELEASE,pgm,blen);
199+
200+
cbw.flags = status;
201+
cbw.transferLength -= blen;
202+
cbw.signature[3] = 'S';
203+
Transfer(MSC_TX | TRANSFER_RELEASE,(u8*)&cbw,13);
204+
}
205+
206+
CBW cbw; // 38 byte savings by making this global
207+
void SCSITask()
208+
{
209+
Recv(MSC_RX,(u8*)&cbw,sizeof(CBW));
210+
SCSI(cbw);
211+
}
212+
213+
#endif
Lines changed: 130 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,130 @@
1+
2+
3+
/* Copyright (c) 2011, Peter Barrett
4+
**
5+
** Permission to use, copy, modify, and/or distribute this software for
6+
** any purpose with or without fee is hereby granted, provided that the
7+
** above copyright notice and this permission notice appear in all copies.
8+
**
9+
** THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL
10+
** WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED
11+
** WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR
12+
** BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES
13+
** OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
14+
** WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,
15+
** ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
16+
** SOFTWARE.
17+
*/
18+
19+
#include "Platform.h"
20+
21+
#ifdef MSC_ENABLED
22+
23+
extern const u8 Sector_0[] PROGMEM;
24+
extern const u8 Sector_1[] PROGMEM;
25+
extern const u8 Sector_2[] PROGMEM;
26+
extern const u8 Sector_3[] PROGMEM;
27+
extern const u8 Sector_Size[] PROGMEM;
28+
29+
30+
const u8 Sector_0[27] =
31+
{
32+
0xEB,0x3C,0x90,
33+
FAT_OEM_NAME,
34+
0x00,0x02, // Bytes per sector 512
35+
0x40, // Sectors per Cluster 32k per cluster
36+
0x01,0x00, // Reserved sectors
37+
0x02, // FATSs
38+
0x10,0x00, // root entries
39+
4+64,0x00, // number of sectors: 30k file
40+
0xF8, // Media type fixed
41+
0x01,0x00, // Sectors per FAT
42+
0x01,0x00, // Sectors per head
43+
0x01,//0x00, // Heads per cylinder
44+
//0x00,0x00,0x00,0x00, // Hidden
45+
//0x00,0x00,0x00,0x00, // long number of sectors
46+
//0x00,0x00, // Ext flags
47+
// 38 bytes
48+
};
49+
50+
// FAT12 clusters for 32k file
51+
const u8 Sector_1[] =
52+
{
53+
#if 0
54+
0xF8,0xFF,0xFF,
55+
0xFF,0x0F//,0x00
56+
#endif
57+
};
58+
59+
const u8 Sector_2[] =
60+
{
61+
};
62+
63+
const u8 Sector_3[] =
64+
{
65+
#if 0
66+
FAT_FILE_NAME, 0x00,0x00,0x00,0x00,0x00, // Uppercase name please
67+
0x43,0x3E,0x43,0x3E,0x00,0x00,0xAB,0x8C,0x40,0x3E,0x02,0x00,0x00,0x80,//0x00,0x00
68+
// 0x00,0x00,
69+
#endif
70+
71+
#ifdef FAT_DISK_LABEL
72+
FAT_DISK_LABEL,0x28
73+
#endif
74+
};
75+
76+
#if 0
77+
const u8 Sector_Size[] =
78+
{
79+
0,
80+
sizeof(Sector_0),
81+
sizeof(Sector_0) + sizeof(Sector_1),
82+
sizeof(Sector_0) + sizeof(Sector_1) + sizeof(Sector_2),
83+
sizeof(Sector_0) + sizeof(Sector_1) + sizeof(Sector_2) + sizeof(Sector_3)
84+
};
85+
#endif
86+
87+
const u8* LBAToROM(u8 b)
88+
{
89+
b -= 4;
90+
return (const u8*)(b << 9);
91+
}
92+
93+
void SendSector(u8 b)
94+
{
95+
if (b == 0)
96+
{
97+
u8 n = sizeof(Sector_0);
98+
Transfer(MSC_TX | TRANSFER_PGM,Sector_0,n);
99+
Transfer(MSC_TX | TRANSFER_ZERO,0,512-n);
100+
return;
101+
}
102+
103+
if ((b < 4) || (b >= 64))
104+
{
105+
Transfer(MSC_TX | TRANSFER_ZERO,0,512);
106+
return;
107+
}
108+
109+
// Copy body of file from ROM
110+
Transfer(MSC_TX | TRANSFER_PGM,LBAToROM(b),512);
111+
}
112+
113+
#define GENERATE_INVALID_ROM_ADDRESS 60
114+
115+
void RecvSector(u8 lba)
116+
{
117+
if ((lba < 4) || (lba >= 64))
118+
lba = GENERATE_INVALID_ROM_ADDRESS; // Only care about the first 30k
119+
120+
// Write sectors to flash
121+
u16 addr = (u16)LBAToROM(lba); // may generate out of range addresses, Program will fix
122+
u8 i = 4; // 4x128 is a sector
123+
while (i--)
124+
{
125+
Program(MSC_RX,addr,128);
126+
addr += 128;
127+
}
128+
}
129+
130+
#endif

‎hardware/arduino/bootloaders/diskloader/src/USBCore.cpp

Lines changed: 513 additions & 0 deletions
Large diffs are not rendered by default.
Lines changed: 261 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,261 @@
1+
2+
// Copyright (c) 2010, Peter Barrett
3+
/*
4+
** Permission to use, copy, modify, and/or distribute this software for
5+
** any purpose with or without fee is hereby granted, provided that the
6+
** above copyright notice and this permission notice appear in all copies.
7+
**
8+
** THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL
9+
** WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED
10+
** WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR
11+
** BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES
12+
** OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
13+
** WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,
14+
** ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
15+
** SOFTWARE.
16+
*/
17+
18+
#ifndef __USBCORE_H__
19+
#define __USBCORE_H__
20+
21+
#define GET_STATUS 0
22+
#define CLEAR_FEATURE 1
23+
#define SET_FEATURE 3
24+
#define SET_ADDRESS 5
25+
#define GET_DESCRIPTOR 6
26+
#define GET_CONFIGURATION 8
27+
#define SET_CONFIGURATION 9
28+
#define GET_INTERFACE 10
29+
#define SET_INTERFACE 11
30+
31+
// bmRequestType
32+
#define HOSTTODEVICE 0x00
33+
#define DEVICETOHOST 0x80
34+
#define STANDARD 0x00
35+
#define CLASS 0x20
36+
#define VENDOR 0x40
37+
#define DEVICE 0x00
38+
#define INTERFACE 0x01
39+
#define ENDPOINT 0x02
40+
#define OTHER 0x03
41+
42+
#define CDC_SET_LINE_CODING 0x20
43+
#define CDC_GET_LINE_CODING 0x21
44+
#define CDC_SET_CONTROL_LINE_STATE 0x22
45+
46+
#define MSC_RESET 0xFF
47+
#define MSC_GET_MAX_LUN 0xFE
48+
49+
50+
// Descriptors
51+
52+
#define USB_DEVICE_DESC_SIZE 18
53+
#define USB_CONFIGUARTION_DESC_SIZE 9
54+
#define USB_INTERFACE_DESC_SIZE 9
55+
#define USB_ENDPOINT_DESC_SIZE 7
56+
57+
#define USB_DEVICE_DESCRIPTOR_TYPE 1
58+
#define USB_CONFIGURATION_DESCRIPTOR_TYPE 2
59+
#define USB_STRING_DESCRIPTOR_TYPE 3
60+
#define USB_INTERFACE_DESCRIPTOR_TYPE 4
61+
#define USB_ENDPOINT_DESCRIPTOR_TYPE 5
62+
63+
#define USB_DEVICE_CLASS_COMMUNICATIONS 0x02
64+
#define USB_DEVICE_CLASS_HUMAN_INTERFACE 0x03
65+
#define USB_DEVICE_CLASS_STORAGE 0x08
66+
#define USB_DEVICE_CLASS_VENDOR_SPECIFIC 0xFF
67+
68+
#define USB_CONFIG_POWERED_MASK 0x40
69+
#define USB_CONFIG_BUS_POWERED 0x80
70+
#define USB_CONFIG_SELF_POWERED 0xC0
71+
#define USB_CONFIG_REMOTE_WAKEUP 0x20
72+
73+
// bMaxPower in Configuration Descriptor
74+
#define USB_CONFIG_POWER_MA(mA) ((mA)/2)
75+
76+
// bEndpointAddress in Endpoint Descriptor
77+
#define USB_ENDPOINT_DIRECTION_MASK 0x80
78+
#define USB_ENDPOINT_OUT(addr) ((addr) | 0x00)
79+
#define USB_ENDPOINT_IN(addr) ((addr) | 0x80)
80+
81+
#define USB_ENDPOINT_TYPE_MASK 0x03
82+
#define USB_ENDPOINT_TYPE_CONTROL 0x00
83+
#define USB_ENDPOINT_TYPE_ISOCHRONOUS 0x01
84+
#define USB_ENDPOINT_TYPE_BULK 0x02
85+
#define USB_ENDPOINT_TYPE_INTERRUPT 0x03
86+
87+
#define TOBYTES(x) ((x) & 0xFF),(((x) >> 8) & 0xFF)
88+
89+
#define CDC_V1_10 0x0110
90+
#define CDC_COMMUNICATION_INTERFACE_CLASS 0x02
91+
92+
#define CDC_CALL_MANAGEMENT 0x01
93+
#define CDC_ABSTRACT_CONTROL_MODEL 0x02
94+
#define CDC_HEADER 0x00
95+
#define CDC_ABSTRACT_CONTROL_MANAGEMENT 0x02
96+
#define CDC_UNION 0x06
97+
#define CDC_CS_INTERFACE 0x24
98+
#define CDC_CS_ENDPOINT 0x25
99+
#define CDC_DATA_INTERFACE_CLASS 0x0A
100+
101+
#define MSC_SUBCLASS_SCSI 0x06
102+
#define MSC_PROTOCOL_BULK_ONLY 0x50
103+
104+
105+
106+
// Device
107+
typedef struct {
108+
u8 len; // 18
109+
u8 dtype; // 1 USB_DEVICE_DESCRIPTOR_TYPE
110+
u16 usbVersion; // 0x200
111+
u8 deviceClass;
112+
u8 deviceSubClass;
113+
u8 deviceProtocol;
114+
u8 packetSize0; // Packet 0
115+
u16 idVendor;
116+
u16 idProduct;
117+
u16 deviceVersion; // 0x100
118+
u8 iManufacturer;
119+
u8 iProduct;
120+
u8 iSerialNumber;
121+
u8 bNumConfigurations;
122+
} DeviceDescriptor;
123+
124+
// Config
125+
typedef struct {
126+
u8 len; // 9
127+
u8 dtype; // 2
128+
u16 clen; // total length
129+
u8 numInterfaces;
130+
u8 config;
131+
u8 iconfig;
132+
u8 attributes;
133+
u8 maxPower;
134+
} ConfigDescriptor;
135+
136+
// String
137+
138+
// Interface
139+
typedef struct
140+
{
141+
u8 len; // 9
142+
u8 dtype; // 4
143+
u8 number;
144+
u8 alternate;
145+
u8 numEndpoints;
146+
u8 interfaceClass;
147+
u8 interfaceSubClass;
148+
u8 protocol;
149+
u8 iInterface;
150+
} InterfaceDescriptor;
151+
152+
// Endpoint
153+
typedef struct
154+
{
155+
u8 len; // 7
156+
u8 dtype; // 5
157+
u8 addr;
158+
u8 attr;
159+
u16 packetSize;
160+
u8 interval;
161+
} EndpointDescriptor;
162+
163+
// Interface Association Descriptor
164+
// Used to bind 2 interfaces together in CDC compostite device
165+
typedef struct
166+
{
167+
u8 len; // 8
168+
u8 dtype; // 11
169+
u8 firstInterface;
170+
u8 interfaceCount;
171+
u8 functionClass;
172+
u8 funtionSubClass;
173+
u8 functionProtocol;
174+
u8 iInterface;
175+
} IADDescriptor;
176+
177+
// CDC CS interface descriptor
178+
typedef struct
179+
{
180+
u8 len; // 5
181+
u8 dtype; // 0x24
182+
u8 subtype;
183+
u8 d0;
184+
u8 d1;
185+
} CDCCSInterfaceDescriptor;
186+
187+
typedef struct
188+
{
189+
u8 len; // 4
190+
u8 dtype; // 0x24
191+
u8 subtype;
192+
u8 d0;
193+
} CDCCSInterfaceDescriptor4;
194+
195+
typedef struct
196+
{
197+
IADDescriptor iad; // Only needed on compound device
198+
199+
// Control
200+
InterfaceDescriptor cif; //
201+
CDCCSInterfaceDescriptor header;
202+
CDCCSInterfaceDescriptor callManagement;
203+
CDCCSInterfaceDescriptor4 controlManagement;
204+
CDCCSInterfaceDescriptor functionalDescriptor;
205+
EndpointDescriptor cifin;
206+
207+
// Data
208+
InterfaceDescriptor dif;
209+
EndpointDescriptor in;
210+
EndpointDescriptor out;
211+
} CDCDescriptor;
212+
213+
typedef struct
214+
{
215+
InterfaceDescriptor msc;
216+
EndpointDescriptor in;
217+
EndpointDescriptor out;
218+
} MSCDescriptor;
219+
220+
typedef struct
221+
{
222+
u8 len; // 9
223+
u8 dtype; // 0x21
224+
u8 addr;
225+
u8 versionL; // 0x101
226+
u8 versionH; // 0x101
227+
u8 country;
228+
u8 desctype; // 0x22 report
229+
u8 descLenL;
230+
u8 descLenH;
231+
} HIDDescDescriptor;
232+
233+
typedef struct
234+
{
235+
InterfaceDescriptor hid;
236+
HIDDescDescriptor desc;
237+
EndpointDescriptor in;
238+
} HIDDescriptor;
239+
240+
#define D_DEVICE(_class,_subClass,_proto,_packetSize0,_vid,_pid,_version,_im,_ip,_is,_configs) \
241+
{ 18, 1, 0x200, _class,_subClass,_proto,_packetSize0,_vid,_pid,_version,_im,_ip,_is,_configs }
242+
243+
#define D_CONFIG(_totalLength,_interfaces) \
244+
{ 9, 2, _totalLength,_interfaces, 1, 0, USB_CONFIG_BUS_POWERED, USB_CONFIG_POWER_MA(100) }
245+
246+
#define D_INTERFACE(_n,_numEndpoints,_class,_subClass,_protocol) \
247+
{ 9, 4, _n, 0, _numEndpoints, _class,_subClass, _protocol, 0 }
248+
249+
#define D_ENDPOINT(_addr,_attr,_packetSize, _interval) \
250+
{ 7, 5, _addr,_attr,_packetSize, _interval }
251+
252+
#define D_IAD(_firstInterface, _count, _class, _subClass, _protocol) \
253+
{ 8, 11, _firstInterface, _count, _class, _subClass, _protocol, 0 }
254+
255+
#define D_HIDREPORT(_descriptorLength) \
256+
{ 9, 0x21, 0x1, 0x1, 0, 1, 0x22, _descriptorLength, 0 }
257+
258+
#define D_CDCCS(_subtype,_d0,_d1) { 5, 0x24, _subtype, _d0, _d1 }
259+
#define D_CDCCS4(_subtype,_d0) { 4, 0x24, _subtype, _d0 }
260+
261+
#endif
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
2+
3+
/* Copyright (c) 2011, Peter Barrett
4+
**
5+
** Permission to use, copy, modify, and/or distribute this software for
6+
** any purpose with or without fee is hereby granted, provided that the
7+
** above copyright notice and this permission notice appear in all copies.
8+
**
9+
** THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL
10+
** WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED
11+
** WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR
12+
** BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES
13+
** OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
14+
** WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,
15+
** ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
16+
** SOFTWARE.
17+
*/
18+
19+
#include "Platform.h"
20+
21+
//====================================================================================================
22+
//====================================================================================================
23+
// Actual device descriptors
24+
25+
const u16 STRING_LANGUAGE[2] = {
26+
(3<<8) | (2+2),
27+
0x0409 // English
28+
};
29+
30+
#if 0
31+
const u16 STRING_PRODUCT[] = {
32+
(3<<8) | (2+2*10),
33+
'M','i','c','r','o','t','o','u','c','h'
34+
};
35+
#endif
36+
37+
const u16 STRING_SERIAL[13] = {
38+
(3<<8) | (2+2*12),
39+
MSC_DISK_SERIAL
40+
};
41+
42+
43+
//#ifdef CDC_ENABLED
44+
DeviceDescriptor USB_DeviceDescriptorA = D_DEVICE(0X02,0X00,0X00,64,USB_VID,USB_PID,0x100,0,IPRODUCT,ISERIAL,1);
45+
//#else
46+
DeviceDescriptor USB_DeviceDescriptor = D_DEVICE(0x00,0x00,0x00,64,USB_VID,USB_PID,0x100,0,IPRODUCT,ISERIAL,1);
47+
//#endif
48+
49+
Config USB_ConfigDescriptor =
50+
{
51+
D_CONFIG(sizeof(Config),INTERFACE_COUNT),
52+
53+
#ifdef CDC_ENABLED
54+
// CDC
55+
{
56+
D_IAD(0,2,CDC_COMMUNICATION_INTERFACE_CLASS,CDC_ABSTRACT_CONTROL_MODEL,1),
57+
58+
// CDC communication interface
59+
D_INTERFACE(CDC_ACM_INTERFACE,1,CDC_COMMUNICATION_INTERFACE_CLASS,CDC_ABSTRACT_CONTROL_MODEL,0),
60+
D_CDCCS(CDC_HEADER,0x10,0x01), // Header (1.10 bcd)
61+
D_CDCCS(CDC_CALL_MANAGEMENT,1,1), // Device handles call management
62+
D_CDCCS4(CDC_ABSTRACT_CONTROL_MANAGEMENT,2), // SET_LINE_CODING, GET_LINE_CODING, SET_CONTROL_LINE_STATE supported
63+
D_CDCCS(CDC_UNION,CDC_ACM_INTERFACE,CDC_DATA_INTERFACE), // Communication interface is master, data interface is slave 0
64+
D_ENDPOINT(USB_ENDPOINT_IN (CDC_ENDPOINT_ACM),USB_ENDPOINT_TYPE_INTERRUPT,0x10,0x40),
65+
66+
// CDC data interface
67+
D_INTERFACE(CDC_DATA_INTERFACE,2,CDC_DATA_INTERFACE_CLASS,0,0),
68+
D_ENDPOINT(USB_ENDPOINT_OUT(CDC_ENDPOINT_OUT),USB_ENDPOINT_TYPE_BULK,0x40,0),
69+
D_ENDPOINT(USB_ENDPOINT_IN (CDC_ENDPOINT_IN ),USB_ENDPOINT_TYPE_BULK,0x40,0)
70+
},
71+
#endif
72+
// HID
73+
{
74+
D_INTERFACE(HID_INTERFACE,1,3,0,0),
75+
D_HIDREPORT(30),
76+
D_ENDPOINT(USB_ENDPOINT_IN (HID_ENDPOINT_INT),USB_ENDPOINT_TYPE_INTERRUPT,0x40,0x40)
77+
},
78+
79+
#ifdef MSC_ENABLED
80+
// Mass Storage
81+
{
82+
D_INTERFACE(MSC_INTERFACE,2,USB_DEVICE_CLASS_STORAGE,MSC_SUBCLASS_SCSI,MSC_PROTOCOL_BULK_ONLY),
83+
D_ENDPOINT(USB_ENDPOINT_OUT(MSC_ENDPOINT_OUT),USB_ENDPOINT_TYPE_BULK,0x40,0),
84+
D_ENDPOINT(USB_ENDPOINT_IN (MSC_ENDPOINT_IN ),USB_ENDPOINT_TYPE_BULK,0x40,0)
85+
}
86+
#endif
87+
};
88+
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
2+
3+
/* Copyright (c) 2011, Peter Barrett
4+
**
5+
** Permission to use, copy, modify, and/or distribute this software for
6+
** any purpose with or without fee is hereby granted, provided that the
7+
** above copyright notice and this permission notice appear in all copies.
8+
**
9+
** THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL
10+
** WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED
11+
** WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR
12+
** BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES
13+
** OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
14+
** WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,
15+
** ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
16+
** SOFTWARE.
17+
*/
18+
19+
20+
#ifndef CDC_ENABLED
21+
22+
#define MSC_INTERFACE 0 // MSC Interface
23+
#define MSC_ENDPOINT_OUT 1
24+
#define MSC_ENDPOINT_IN 2
25+
26+
#define INTERFACE_COUNT 1 // 1 for msc
27+
28+
#else
29+
30+
#define CDC_ACM_INTERFACE 0 // CDC ACM
31+
#define CDC_DATA_INTERFACE 1 // CDC Data
32+
#define CDC_ENDPOINT_ACM 1
33+
#define CDC_ENDPOINT_OUT 2
34+
#define CDC_ENDPOINT_IN 3
35+
36+
#define HID_INTERFACE 2 // HID Interface
37+
#define HID_ENDPOINT_INT 4
38+
39+
#ifdef MSC_ENABLED
40+
41+
#define MSC_INTERFACE 3 // MSC Interface
42+
#define MSC_ENDPOINT_OUT 5
43+
#define MSC_ENDPOINT_IN 6
44+
#define INTERFACE_COUNT 4 // 2 for cdc + 1 for hid + 1 for msc
45+
46+
#else
47+
48+
#define INTERFACE_COUNT 3 // 2 for cdc + 1 for hid
49+
50+
#endif
51+
52+
#endif
53+
54+
typedef struct
55+
{
56+
ConfigDescriptor config;
57+
#ifdef CDC_ENABLED
58+
CDCDescriptor cdc;
59+
#endif
60+
HIDDescriptor hid;
61+
#ifdef MSC_ENABLED
62+
MSCDescriptor msc;
63+
#endif
64+
} Config;
65+
66+
extern Config USB_ConfigDescriptor PROGMEM;
67+
extern DeviceDescriptor USB_DeviceDescriptor PROGMEM;
68+
extern DeviceDescriptor USB_DeviceDescriptorA PROGMEM;
69+
70+
extern const u16 STRING_LANGUAGE[2] PROGMEM;
71+
extern const u16 STRING_SERIAL[13] PROGMEM;
72+
73+
#define IMANUFACTURER 0
74+
#define IPRODUCT 0
75+
#define ISERIAL 1 // Only need this for MSC
76+
77+
78+
#define MSC_TX MSC_ENDPOINT_IN
79+
#define MSC_RX MSC_ENDPOINT_OUT
80+
#define CDC_TX CDC_ENDPOINT_IN
81+
#define CDC_RX CDC_ENDPOINT_OUT

0 commit comments

Comments
 (0)
Please sign in to comment.