From 0ed2d3c95309ecf5905ec06624fc68955fdbd2d0 Mon Sep 17 00:00:00 2001 From: Zach Eveland Date: Tue, 10 Jan 2012 16:29:47 -0500 Subject: renamed DiskLoader to Caterina (after Leonardo's mom) --- bootloaders/caterina/Caterina-Leonardo.hex | 115 +++++++ bootloaders/caterina/Makefile | 105 ++++++ bootloaders/caterina/src/Caterina.cpp | 251 ++++++++++++++ bootloaders/caterina/src/Platform.h | 49 +++ bootloaders/caterina/src/USBCore.cpp | 510 +++++++++++++++++++++++++++++ bootloaders/caterina/src/USBCore.h | 246 ++++++++++++++ bootloaders/caterina/src/USBDesc.cpp | 83 +++++ bootloaders/caterina/src/USBDesc.h | 60 ++++ 8 files changed, 1419 insertions(+) create mode 100644 bootloaders/caterina/Caterina-Leonardo.hex create mode 100644 bootloaders/caterina/Makefile create mode 100644 bootloaders/caterina/src/Caterina.cpp create mode 100644 bootloaders/caterina/src/Platform.h create mode 100644 bootloaders/caterina/src/USBCore.cpp create mode 100644 bootloaders/caterina/src/USBCore.h create mode 100644 bootloaders/caterina/src/USBDesc.cpp create mode 100644 bootloaders/caterina/src/USBDesc.h (limited to 'bootloaders/caterina') diff --git a/bootloaders/caterina/Caterina-Leonardo.hex b/bootloaders/caterina/Caterina-Leonardo.hex new file mode 100644 index 0000000..95ca57b --- /dev/null +++ b/bootloaders/caterina/Caterina-Leonardo.hex @@ -0,0 +1,115 @@ +:1078000011241FBECFEFDAE0DEBFCDBFF5C04101CE +:1078100042144505560455026403740300001E9586 +:1078200087020110030000C1808106C0FF0A000C1E +:10783000A10185037508150026FF00954009018107 +:1078400002954009029102C0040309041A033000A2 +:1078500030003000300030003000300030003100A7 +:1078600037003000310038034100720064007500B9 +:1078700069006E006F0020004C0065006F006E0014 +:107880006100720064006F00200062006F006F00F2 +:1078900074006C006F0061006400650072001803E2 +:1078A000410072006400750069006E006F002000E6 +:1078B0004C004C0043001201000202000040412332 +:1078C000300000010002030109024B0002010080A8 +:1078D00032080B000202020100090400000102024A +:1078E0000000052400100105240101010424020206 +:1078F000052406000107058103100040090401006A +:10790000020A00000007050202400000070583028A +:107910004000000020918A0130918B012C5F3F4F85 +:1079200030938B0120938A01C901892F992786956D +:10793000982F803418F08FE7891B982F990F921792 +:1079400010F447980895479A08955D9A289A81E01F +:107950008093E0001092E200EE27FF27099408953B +:1079600028E088E190E00FB6F894A8958093600035 +:107970000FBE20936000FFCF0F931F93CF93DF9331 +:10798000982FEB01042F10E088E76030780730F47F +:1079900011E083E0FB0180935700E895892F68E0B0 +:1079A00071E0402F06D1112311F107B600FCFDCF85 +:1079B000402F4695FE01A8E0B1E020E031E009C08B +:1079C0008D919D910C0130935700E89511243296CA +:1079D0002F5F2417A8F385E0FE0180935700E895F8 +:1079E00007B600FCFDCF81E180935700E895DF9159 +:1079F000CF911F910F91089584B794B7977F94BF4B +:107A000098E10FB6F89490936000109260000FBE5A +:107A10005D9A289A479883FF08C0E0E0F0E08591DE +:107A200094918F5F9F4F09F090DF3F9A209A559A6B +:107A300090E8909361001092610081E885BF95BF46 +:107A40009FD084E18093880180E180938901E0E008 +:107A5000F0E0859194918F5F9F4F19F081E08093C2 +:107A60008F01EE24FF24BB24B39454EFC52E51E0C4 +:107A7000D52E0DD2082F8EE098E7FC012491319687 +:107A800002964491201711F02223B9F71092910128 +:107A90001092900182E068E071E08BD0013479F4BB +:107AA000609108016058633028F0683111F064E09B +:107AB00001C063E0C62FD0E0CF5DD7480EC00635C9 +:107AC00071F480910801803311F011E022C080919F +:107AD0000A01C82FD0E0C25ED74811E022C00537A6 +:107AE00021F413E0CEE1D8E71CC0053539F4E0906D +:107AF0000801F0900901EE0CFF1C0AC0043631F4B5 +:107B000082E0B7014091090137DF02C0043721F05C +:107B100010E0C5E2D8E705C010910901E701E10EC8 +:107B2000F11CB5D18097B1F483E068E871E041E0E1 +:107B300050E059D0112329F0412F50E083E8BE01D5 +:107B400052D083E469E871E041E050E04CD0013567 +:107B500009F08FCFD0929101C0929001B0928F0125 +:107B6000EE24FF2486CFFC01289884E680938D01C3 +:107B700004C08091F100819361506623D1F708958C +:107B8000109291011092900110928E0110928F012B +:107B900081E08093D70080EA8093D80082E189BD9C +:107BA00009B400FEFDCF80E98093D8001092E00078 +:107BB0000895FB018093E90024E69BE611C08091C3 +:107BC000E80085FFFCCF289820938D018091F1007B +:107BD00081938091E80085FD02C09093E8004150B8 +:107BE000442369F70895982FFB01282F207287708E +:107BF0008093E90064E63AE317C08091E80085FFCE +:107C0000FCCF97FF02C0849101C080813196211181 +:107C100080E05D9860938C018093F1008091E80092 +:107C200085FD02C03093E800415050408FEF4F3F38 +:107C3000580719F796FF03C08AE38093E800089578 +:107C400080919301813299F45D9884E680938C0150 +:107C500020E030E003C080818093F100F901E05022 +:107C6000FF4F2F5F3F4F28303105A9F714C08032F6 +:107C700061F48091E80082FFFCCF80E091E067E052 +:107C800072DF8BEF8093E80006C0823221F480918E +:107C900094018093070181E00895809195018232DB +:107CA00019F420E030E024C0813019F426EB38E7E5 +:107CB0001FC0823029F48BE490E028EC38E71BC029 +:107CC000833009F048C080919401882319F428E496 +:107CD00038E70EC0823019F426E638E709C0833051 +:107CE00019F42CE438E704C08130A9F52EE938E70F +:107CF000F901849190E04091980150919901BC0163 +:107D00004817590708F4BA0190918C0140E050E0FF +:107D1000AEEF8091E8008570E1F38091E80082FD8C +:107D200016C0F901E40FF51F4F5F5F4FE4915D98B6 +:107D3000E093F100842F8F7311F4A093E80094E690 +:107D40004617570730F390938C0181E00895909384 +:107D50008C0180E0089580E008951092E900809100 +:107D6000E80083FF61C082E991E068E0FCDE82EF19 +:107D70008093E8008091920187FF05C08091E80020 +:107D800080FFFCCF03C08EEF8093E80080919301C9 +:107D9000853051F48091E80080FFFCCF8091940100 +:107DA00080688093E30039C08930E1F480919401C8 +:107DB00080938E01E7E2F8E791E031E026E39093CB +:107DC000E9003093EB0084918093EC002093ED0068 +:107DD0009F5F3196943099F78EE78093EA00109276 +:107DE000EA001BC0883049F490918E015D9884E6CA +:107DF00080938C019093F10010C0882339F45D9832 +:107E000084E680938C011092F10007C0863011F453 +:107E100044DF01C015DF882321F08EEF8093E80056 +:107E2000089581E28093EB0008958091E100109223 +:107E3000E100282F83FF0CC01092E90081E08093BD +:107E4000EB001092EC0082E38093ED0010928E0123 +:107E500022FF1CC080918C01882331F081508093D7 +:107E60008C01882309F45D9A80918D01882331F07B +:107E7000815080938D01882309F4289A80918F0185 +:107E8000882321F4109291011092900108951F937C +:107E9000CF93DF9312E0C0E9D1E05FDFC6DF10933C +:107EA000E9008091E80085FF13C0289884E680935C +:107EB0008D019091F1008091E80085FD03C08BE673 +:107EC0008093E800892F90E0DF91CF911F91089572 +:107ED00080919001909191010197909391018093ED +:107EE00090018091900190919101892B09F438DDE6 +:0A7EF000CE010197F1F70EDDD0CFAF +:087EFA0000E10000000000009F +:040000030000780081 +:00000001FF diff --git a/bootloaders/caterina/Makefile b/bootloaders/caterina/Makefile new file mode 100644 index 0000000..4bb41c8 --- /dev/null +++ b/bootloaders/caterina/Makefile @@ -0,0 +1,105 @@ +############################################################################### +# Makefile for Caterina (formerly DiskLoader) +############################################################################### + +## General Flags +PROJECT = Caterina +TARGET = Caterina.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 bootloader PID +ARDUINO_MODEL_PID = 0x0030 +# Arduino Micro bootloader PID +#ARDUINO_MODEL_PID = 0x0031 + +# Change if your programmer is different +AVRDUDE_PROGRAMMER = avrispmkII +AVRDUDE_PORT = usb + +# program name should not be changed... +PROGRAM = Caterina + +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=Caterina.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) Caterina.hex Caterina.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 \ No newline at end of file diff --git a/bootloaders/caterina/src/Caterina.cpp b/bootloaders/caterina/src/Caterina.cpp new file mode 100644 index 0000000..7ef6fa8 --- /dev/null +++ b/bootloaders/caterina/src/Caterina.cpp @@ -0,0 +1,251 @@ + + +#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 + ::); +} + +uint8_t _flashbuf[128]; +uint8_t _inSync; +uint8_t _ok; +extern volatile uint8_t _ejected; +extern volatile uint16_t _timeout; + +void Program(uint8_t ep, uint16_t page, uint8_t count) +{ + uint8_t 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; + uint16_t* p = (uint16_t*)page; + uint16_t* b = (uint16_t*)_flashbuf; + for (uint8_t i = 0; i < count; i++) + boot_page_fill(p++, b[i]); + + boot_page_write(page); + boot_spm_busy_wait(); + boot_rww_enable (); +} + +void StartSketch(); +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 uint8_t _readSize[] PROGMEM; +const uint8_t _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 uint8_t _consts[] PROGMEM; +const uint8_t _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() +{ + uint8_t MCUSR_state = MCUSR; // store the reason for the reset + MCUSR &= ~(1 << WDRF); // must clear the watchdog reset flag before disabling and reenabling WDT + wdt_disable(); + TX_LED_OFF(); + RX_LED_OFF(); + L_LED_OFF(); + if (MCUSR_state & (1< 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 = *((uint16_t*)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 uint8_t*)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 +uint16_t _pulse; +void LEDPulse() +{ + _pulse += 4; + uint8_t p = _pulse >> 9; + if (p > 63) + p = 127-p; + p += p; + if (((uint8_t)_pulse) > p) + L_LED_OFF(); + else + L_LED_ON(); +} + +void StartSketch() +{ + TX_LED_OFF(); // switch off the RX and TX LEDs before starting the user sketch + RX_LED_OFF(); + UDCON = 1; // Detach USB + UDIEN = 0; + asm volatile ( // Reset vector to run firmware + "clr r30\n" + "clr r31\n" + "ijmp\n" + ::); +} + +void Reset() +{ + wdt_enable(WDTO_15MS); // reset the microcontroller to reinitialize all IO and other registers + for (;;) + ; +} diff --git a/bootloaders/caterina/src/Platform.h b/bootloaders/caterina/src/Platform.h new file mode 100644 index 0000000..9d18e80 --- /dev/null +++ b/bootloaders/caterina/src/Platform.h @@ -0,0 +1,49 @@ + +#include +#include +#include +#include +#include +#include +#include + +#include +#include +#include + +#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_BOOTLOADER 0x0030 +#define USB_PID_MICRO_BOOTLOADER 0x0031 +#define USB_VID 0x2341 // arduino LLC vid +#define USB_PID ARDUINO_MODEL_PID // passed in by Makefile - 0x0034 for Leonardo, 0x0035 for MIcro + +#define USB_SERIAL_STRING '0','0','0','0','0','0','0','0','1','7','0','1' + +#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 L_LED_OFF() PORTC &= ~(1<<7) +#define L_LED_ON() PORTC |= (1<<7) +#define TX_LED_OFF() PORTD |= (1<<5) +#define TX_LED_ON() PORTD &= ~(1<<5) +#define RX_LED_OFF() PORTB |= (1<<0) +#define RX_LED_ON() PORTB &= ~(1<<0) + +#define TRANSFER_PGM 0x80 +#define TRANSFER_RELEASE 0x40 +#define TRANSFER_ZERO 0x20 + +void Transfer(uint8_t ep, const uint8_t* data, int len); +void Recv(uint8_t ep, uint8_t* dst, uint8_t len); +void Program(uint8_t ep, uint16_t page, uint8_t count); + +/* HID is not fully-supported in the bootloader - can be enabled + for testing, but note the descriptor report and other parts are + not complete */ +//#define HID_ENABLED + +#include "USBCore.h" +#include "USBDesc.h" + + diff --git a/bootloaders/caterina/src/USBCore.cpp b/bootloaders/caterina/src/USBCore.cpp new file mode 100644 index 0000000..ac5d081 --- /dev/null +++ b/bootloaders/caterina/src/USBCore.cpp @@ -0,0 +1,510 @@ + + +/* 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 +uint8_t TxLEDPulse; /**< Milliseconds remaining for data Tx LED pulse */ +uint8_t RxLEDPulse; /**< Milliseconds remaining for data Rx LED pulse */ + +void Reset(); + +//================================================================== +//================================================================== + +typedef struct +{ + uint32_t dwDTERate; + uint8_t bCharFormat; + uint8_t bParityType; + uint8_t bDataBits; + uint8_t lineState; +} LineInfo; + +static volatile LineInfo _usbLineInfo = { 57600, 0x00, 0x00, 0x00, 0x00 }; + +//================================================================== +//================================================================== + +// 4 bytes of RAM +volatile uint8_t _usbConfiguration; +volatile uint8_t _ejected; +volatile uint16_t _timeout; + +static inline void WaitIN(void) +{ + while (!(UEINTX & (1<> 8) + +#define RAWHID_USAGE_PAGE 0xFFC0 +#define RAWHID_USAGE 0x0C00 +#define RAWHID_TX_SIZE 64 +#define RAWHID_RX_SIZE 64 + +const uint8_t _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 +}; + +uint8_t _cdcComposite = 0; + +bool SendDescriptor() +{ + Setup& setup = _setup; + uint16_t desc_length = 0; + const uint8_t* desc_addr = 0; + + uint8_t t = setup.wValueH; + if (0x22 == t) + { +#ifdef HID_ENABLED + desc_addr = _rawHID; + desc_length = sizeof(desc_length); +#endif + } else if (USB_DEVICE_DESCRIPTOR_TYPE == t) + { + desc_addr = (const uint8_t*)&USB_DeviceDescriptor; + } + else if (USB_CONFIGURATION_DESCRIPTOR_TYPE == t) + { + desc_addr = (const uint8_t*)&USB_ConfigDescriptor; + desc_length = sizeof(USB_ConfigDescriptor); + } + else if (USB_STRING_DESCRIPTOR_TYPE == t) + { + if (setup.wValueL == 0) + desc_addr = (const uint8_t*)&STRING_LANGUAGE; + else if (setup.wValueL == IPRODUCT) + desc_addr = (const uint8_t*)&STRING_IPRODUCT; + else if (setup.wValueL == ISERIAL) + desc_addr = (const uint8_t*)&STRING_SERIAL; + else if (setup.wValueL == IMANUFACTURER) + desc_addr = (const uint8_t*)&STRING_IMANUFACTURER; + else + return false; + } else + return false; + + if (desc_length == 0) + desc_length = pgm_read_byte(desc_addr); + if (setup.wLength < desc_length) + desc_length = setup.wLength; + + // Send descriptor + // EP0 is 64 bytes long + // RWAL and FIFOCON don't work on EP0 + uint16_t n = 0; + do + { + if (!WaitForINOrOUT()) + return false; + Send8(pgm_read_byte(&desc_addr[n++])); + uint8_t 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((uint8_t*)&setup,8); + ClearSetupInt(); + + if (setup.bmRequestType & DEVICETOHOST) + WaitIN(); + else + ClearIN(); + + bool ok = true; + uint8_t r = setup.bRequest; + if (SET_ADDRESS == r) + { + WaitIN(); + UDADDR = setup.wValueL | (1<> 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 { + uint8_t len; // 18 + uint8_t dtype; // 1 USB_DEVICE_DESCRIPTOR_TYPE + uint16_t usbVersion; // 0x200 + uint8_t deviceClass; + uint8_t deviceSubClass; + uint8_t deviceProtocol; + uint8_t packetSize0; // Packet 0 + uint16_t idVendor; + uint16_t idProduct; + uint16_t deviceVersion; // 0x100 + uint8_t iManufacturer; + uint8_t iProduct; + uint8_t iSerialNumber; + uint8_t bNumConfigurations; +} DeviceDescriptor; + +// Config +typedef struct { + uint8_t len; // 9 + uint8_t dtype; // 2 + uint16_t clen; // total length + uint8_t numInterfaces; + uint8_t config; + uint8_t iconfig; + uint8_t attributes; + uint8_t maxPower; +} ConfigDescriptor; + +// String + +// Interface +typedef struct +{ + uint8_t len; // 9 + uint8_t dtype; // 4 + uint8_t number; + uint8_t alternate; + uint8_t numEndpoints; + uint8_t interfaceClass; + uint8_t interfaceSubClass; + uint8_t protocol; + uint8_t iInterface; +} InterfaceDescriptor; + +// Endpoint +typedef struct +{ + uint8_t len; // 7 + uint8_t dtype; // 5 + uint8_t addr; + uint8_t attr; + uint16_t packetSize; + uint8_t interval; +} EndpointDescriptor; + +// Interface Association Descriptor +// Used to bind 2 interfaces together in CDC compostite device +typedef struct +{ + uint8_t len; // 8 + uint8_t dtype; // 11 + uint8_t firstInterface; + uint8_t interfaceCount; + uint8_t functionClass; + uint8_t funtionSubClass; + uint8_t functionProtocol; + uint8_t iInterface; +} IADDescriptor; + +// CDC CS interface descriptor +typedef struct +{ + uint8_t len; // 5 + uint8_t dtype; // 0x24 + uint8_t subtype; + uint8_t d0; + uint8_t d1; +} CDCCSInterfaceDescriptor; + +typedef struct +{ + uint8_t len; // 4 + uint8_t dtype; // 0x24 + uint8_t subtype; + uint8_t 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 +{ + uint8_t len; // 9 + uint8_t dtype; // 0x21 + uint8_t addr; + uint8_t versionL; // 0x101 + uint8_t versionH; // 0x101 + uint8_t country; + uint8_t desctype; // 0x22 report + uint8_t descLenL; + uint8_t 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/caterina/src/USBDesc.cpp b/bootloaders/caterina/src/USBDesc.cpp new file mode 100644 index 0000000..a634d4d --- /dev/null +++ b/bootloaders/caterina/src/USBDesc.cpp @@ -0,0 +1,83 @@ + + +/* 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 uint16_t STRING_LANGUAGE[2] = { + (3<<8) | (2+2), + 0x0409 // English +}; + +const uint16_t STRING_SERIAL[13] = { + (3<<8) | (2+2*12), + USB_SERIAL_STRING +}; + +const uint16_t STRING_IPRODUCT[28] = { + (3<<8) | (2+2*27), +#if USB_PID == USB_PID_LEONARDO_BOOTLOADER + '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_BOOTLOADER + 'A','r','d','u','i','n','o',' ','M','i','c','r','o',' ','b','o','o','t','l','o','a','d','e','r',' ',' ',' ' +#endif +}; + +const uint16_t STRING_IMANUFACTURER[12] = { + (3<<8) | (2+2*11), + 'A','r','d','u','i','n','o',' ','L','L','C' +}; + + +DeviceDescriptor USB_DeviceDescriptor = D_DEVICE(0X02,0X00,0X00,64,USB_VID,USB_PID,0x100,0,IPRODUCT,ISERIAL,1); + +Config USB_ConfigDescriptor = +{ + D_CONFIG(sizeof(Config),INTERFACE_COUNT), + + // 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) + }, + +#ifdef HID_ENABLED + // 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) + } +#endif +}; + diff --git a/bootloaders/caterina/src/USBDesc.h b/bootloaders/caterina/src/USBDesc.h new file mode 100644 index 0000000..fa962e9 --- /dev/null +++ b/bootloaders/caterina/src/USBDesc.h @@ -0,0 +1,60 @@ + + +/* Copyright (c) 2011, Peter Barrett +** +** Permission to use, copy, modify, and/or distribute this software for +** any purpose with or without fee is hereby granted, provided that the +** above copyright notice and this permission notice appear in all copies. +** +** THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL +** WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED +** WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR +** BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES +** OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, +** WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, +** ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS +** SOFTWARE. +*/ + +#ifndef HID_ENABLED // HID is NOT 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 INTERFACE_COUNT 2 // 2 for CDC +#else // HID is 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 +#endif + +typedef struct +{ + ConfigDescriptor config; + CDCDescriptor cdc; +#ifdef HID_ENABLED + HIDDescriptor hid; +#endif +} Config; + +extern Config USB_ConfigDescriptor PROGMEM; +extern DeviceDescriptor USB_DeviceDescriptor PROGMEM; +extern DeviceDescriptor USB_DeviceDescriptorA PROGMEM; + +extern const uint16_t STRING_LANGUAGE[2] PROGMEM; +extern const uint16_t STRING_IPRODUCT[28] PROGMEM; +extern const uint16_t STRING_IMANUFACTURER[12] PROGMEM; +extern const uint16_t STRING_SERIAL[13] PROGMEM; + +#define IMANUFACTURER 1 +#define IPRODUCT 2 +#define ISERIAL 3 + +#define CDC_TX CDC_ENDPOINT_IN +#define CDC_RX CDC_ENDPOINT_OUT \ No newline at end of file -- cgit v1.2.3-18-g5258