From 3e3e4be2a3a0e9ea4186a616f53fa15083b2214f Mon Sep 17 00:00:00 2001 From: Zach Eveland Date: Thu, 11 Aug 2011 10:31:39 -0400 Subject: diskloader source and binary for 32u4 --- bootloaders/diskloader/src/USBCore.cpp | 513 +++++++++++++++++++++++++++++++++ 1 file changed, 513 insertions(+) create mode 100644 bootloaders/diskloader/src/USBCore.cpp (limited to 'bootloaders/diskloader/src/USBCore.cpp') diff --git a/bootloaders/diskloader/src/USBCore.cpp b/bootloaders/diskloader/src/USBCore.cpp new file mode 100644 index 0000000..b6f8c44 --- /dev/null +++ b/bootloaders/diskloader/src/USBCore.cpp @@ -0,0 +1,513 @@ + + +/* Copyright (c) 2010, Peter Barrett +** +** Permission to use, copy, modify, and/or distribute this software for +** any purpose with or without fee is hereby granted, provided that the +** above copyright notice and this permission notice appear in all copies. +** +** THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL +** WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED +** WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR +** BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES +** OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, +** WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, +** ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS +** SOFTWARE. +*/ + +#include "Platform.h" + +#define MSC_TX MSC_ENDPOINT_IN +#define MSC_RX MSC_ENDPOINT_OUT +#define CDC_TX CDC_ENDPOINT_IN +#define CDC_RX CDC_ENDPOINT_OUT + +#define EP_TYPE_CONTROL 0x00 +#define EP_TYPE_BULK_IN 0x81 +#define EP_TYPE_BULK_OUT 0x80 +#define EP_TYPE_INTERRUPT_IN 0xC1 +#define EP_TYPE_INTERRUPT_OUT 0xC0 +#define EP_TYPE_ISOCHRONOUS_IN 0x41 +#define EP_TYPE_ISOCHRONOUS_OUT 0x40 + +void Reboot(); + +//================================================================== +//================================================================== + +typedef struct +{ + u32 dwDTERate; + u8 bCharFormat; + u8 bParityType; + u8 bDataBits; + u8 lineState; +} LineInfo; + +static volatile LineInfo _usbLineInfo = { 57600, 0x00, 0x00, 0x00, 0x00 }; + +//================================================================== +//================================================================== + +// 4 bytes of RAM +volatile u8 _usbConfiguration; +volatile u8 _ejected; +volatile u16 _timeout; + +static inline void WaitIN(void) +{ + while (!(UEINTX & (1<> 8) + +#define RAWHID_USAGE_PAGE 0xFFC0 +#define RAWHID_USAGE 0x0C00 +#define RAWHID_TX_SIZE 64 +#define RAWHID_RX_SIZE 64 + +const u8 _rawHID[] = +{ + // RAW HID + 0x06, LSB(RAWHID_USAGE_PAGE), MSB(RAWHID_USAGE_PAGE), // 30 + 0x0A, LSB(RAWHID_USAGE), MSB(RAWHID_USAGE), + + 0xA1, 0x01, // Collection 0x01 + 0x85, 0x03, // REPORT_ID (3) + 0x75, 0x08, // report size = 8 bits + 0x15, 0x00, // logical minimum = 0 + 0x26, 0xFF, 0x00, // logical maximum = 255 + + 0x95, 64, // report count TX + 0x09, 0x01, // usage + 0x81, 0x02, // Input (array) + + 0x95, 64, // report count RX + 0x09, 0x02, // usage + 0x91, 0x02, // Output (array) + 0xC0 // end collection +}; + +u8 _cdcComposite = 0; + +bool SendDescriptor() +{ + Setup& setup = _setup; + u8 desc_length = 0; + const u8* desc_addr = 0; + + u8 t = setup.wValueH; + if (0x22 == t) + { + desc_addr = _rawHID; + desc_length = sizeof(desc_length); + } else if (USB_DEVICE_DESCRIPTOR_TYPE == t) + { + if (setup.wLength == 8) + _cdcComposite = 1; + desc_addr = _cdcComposite ? (const u8*)&USB_DeviceDescriptorA : (const u8*)&USB_DeviceDescriptor; + } + else if (USB_CONFIGURATION_DESCRIPTOR_TYPE == t) + { + desc_addr = (const u8*)&USB_ConfigDescriptor; + desc_length = sizeof(USB_ConfigDescriptor); + } + else if (USB_STRING_DESCRIPTOR_TYPE == t) + { + if (setup.wValueL == 0) + desc_addr = (const u8*)&STRING_LANGUAGE; + else if (setup.wValueL == ISERIAL) + desc_addr = (const u8*)&STRING_SERIAL; + else + return false; + } else + return false; + + if (desc_length == 0) + desc_length = pgm_read_byte(desc_addr); + if ((u8)setup.wLength < desc_length) // bit of a cheat limiting to 255 bytes TODO (saved 8 bytes) + desc_length = (u8)setup.wLength; + + // Send descriptor + // EP0 is 64 bytes long + // RWAL and FIFOCON don't work on EP0 + u8 n = 0; + do + { + if (!WaitForINOrOUT()) + return false; + Send8(pgm_read_byte(&desc_addr[n++])); + u8 clr = n & 0x3F; + if (!clr) + ClearIN(); // Fifo is full, release this packet + } while (n < desc_length); + return true; +} + +void USBSetupInterrupt() +{ + SetEP(0); + if (!ReceivedSetupInt()) + return; + + Setup& setup = _setup; // global saves ~30 bytes + Recv((u8*)&setup,8); + ClearSetupInt(); + + if (setup.bmRequestType & DEVICETOHOST) + WaitIN(); + else + ClearIN(); + + bool ok = true; + u8 r = setup.bRequest; + if (SET_ADDRESS == r) + { + WaitIN(); + UDADDR = setup.wValueL | (1<