aboutsummaryrefslogtreecommitdiff
path: root/bootloaders/diskloader/src/USBCore.cpp
diff options
context:
space:
mode:
authorZach Eveland <zeveland@blacklabel-development.com>2011-11-09 14:04:59 -0500
committerZach Eveland <zeveland@blacklabel-development.com>2011-11-09 14:04:59 -0500
commit4b7771a8159f47891311bbac6d97eac33868bc65 (patch)
tree729f0940bad89ec074aeb4cdb189f1b2839e744a /bootloaders/diskloader/src/USBCore.cpp
parentbca358b9739c8bf7e4dd9808b97acc90cf0b6ab3 (diff)
restored original non-interrupt-driven DiskLoader
Interrupt-driven version was a dead-end - too many bugs with that approach.
Diffstat (limited to 'bootloaders/diskloader/src/USBCore.cpp')
-rw-r--r--bootloaders/diskloader/src/USBCore.cpp589
1 files changed, 229 insertions, 360 deletions
diff --git a/bootloaders/diskloader/src/USBCore.cpp b/bootloaders/diskloader/src/USBCore.cpp
index 6518ae9..ca74721 100644
--- a/bootloaders/diskloader/src/USBCore.cpp
+++ b/bootloaders/diskloader/src/USBCore.cpp
@@ -1,4 +1,5 @@
+
/* Copyright (c) 2010, Peter Barrett
**
** Permission to use, copy, modify, and/or distribute this software for
@@ -16,10 +17,9 @@
*/
#include "Platform.h"
-#include "USBAPI.h"
-#include "USBDesc.h"
-#if defined(USBCON)
+#define CDC_TX CDC_ENDPOINT_IN
+#define CDC_RX CDC_ENDPOINT_OUT
#define EP_TYPE_CONTROL 0x00
#define EP_TYPE_BULK_IN 0x81
@@ -31,50 +31,32 @@
/** 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 */
+u8 TxLEDPulse; /**< Milliseconds remaining for data Tx LED pulse */
+u8 RxLEDPulse; /**< Milliseconds remaining for data Rx LED pulse */
+
+void Reboot();
//==================================================================
//==================================================================
-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'
-};
-
-#define DEVICE_CLASS 0x02
-
-// DEVICE DESCRIPTOR
-const DeviceDescriptor USB_DeviceDescriptor =
- D_DEVICE(0x00,0x00,0x00,64,USB_VID,USB_PID,0x100,IMANUFACTURER,IPRODUCT,0,1);
+typedef struct
+{
+ u32 dwDTERate;
+ u8 bCharFormat;
+ u8 bParityType;
+ u8 bDataBits;
+ u8 lineState;
+} LineInfo;
-const DeviceDescriptor USB_DeviceDescriptorA =
- D_DEVICE(DEVICE_CLASS,0x00,0x00,64,USB_VID,USB_PID,0x100,IMANUFACTURER,IPRODUCT,0,1);
+static volatile LineInfo _usbLineInfo = { 57600, 0x00, 0x00, 0x00, 0x00 };
//==================================================================
//==================================================================
-volatile u8 _usbConfiguration = 0;
+// 4 bytes of RAM
+volatile u8 _usbConfiguration;
+volatile u8 _ejected;
+volatile u16 _timeout;
static inline void WaitIN(void)
{
@@ -104,25 +86,34 @@ 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;
-
- RX_LED_ON();
- RxLEDPulse = TX_RX_LED_PULSE_MS;
}
static inline u8 Recv8()
{
- RX_LED_ON();
+ RXLED1; // light the RX LED
RxLEDPulse = TX_RX_LED_PULSE_MS;
-
- return UEDATX;
+ return UEDATX;
}
static inline void Send8(u8 d)
{
+ TXLED1; // light the TX LED
+ TxLEDPulse = TX_RX_LED_PULSE_MS;
UEDATX = d;
}
@@ -184,37 +175,48 @@ static inline u8 FrameNumber()
//==================================================================
//==================================================================
-u8 USBGetConfiguration(void)
+#define EP_SINGLE_64 0x32 // EP0
+#define EP_DOUBLE_64 0x36 // Other endpoints
+
+static void InitEP(u8 index, u8 type, u8 size)
{
- return _usbConfiguration;
+ UENUM = index;
+ UECONX = 1;
+ UECFG0X = type;
+ UECFG1X = size;
}
-#define USB_RECV_TIMEOUT
-class LockEP
+// API
+void USBInit(void)
{
- u8 _sreg;
-public:
- LockEP(u8 ep) : _sreg(SREG)
- {
- cli();
- SetEP(ep & 7);
- }
- ~LockEP()
- {
- SREG = _sreg;
- }
-};
+ _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
+}
-// Number of bytes, assumes a rx endpoint
-u8 USB_Available(u8 ep)
+u8 USBGetConfiguration(void)
{
- LockEP lock(ep);
- return FifoByteCount();
+ return _usbConfiguration;
+}
+
+u8 HasData(u8 ep)
+{
+ SetEP(ep);
+ return ReadWriteAllowed(); // count in fifo
}
-void USB_Recv_block(u8 ep, u8* dst, int len)
+int USBGetChar();
+void Recv(u8 ep, u8* dst, u8 len)
{
- LockEP lock(ep);
+ SetEP(ep);
while (len--)
{
while (!ReadWriteAllowed())
@@ -222,119 +224,47 @@ void USB_Recv_block(u8 ep, u8* dst, int len)
*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)
+// Transmit a packet to endpoint
+void Transfer(u8 ep, const u8* data, int len)
{
- if (!_usbConfiguration)
- return -1;
-
- int r = len;
- const u8* data = (const u8*)d;
- while (len)
+ u8 zero = ep & TRANSFER_ZERO;
+ SetEP(ep & 7);
+ while (len--)
{
- u8 n = USB_SendSpace(ep);
- if (n == 0)
- {
- _delay_ms(1);
- continue;
- }
+ while (!ReadWriteAllowed())
+ ; // TODO Check for STALL etc
- 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();
- }
+ 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();
-
- 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
-};
-
-#define EP_SINGLE_64 0x32 // EP0
-#define EP_DOUBLE_64 0x36 // Other endpoints
+#endif
-static
-void InitEP(u8 index, u8 type, u8 size)
-{
- UENUM = index;
- UECONX = 1;
- UECFG0X = type;
- UECFG1X = size;
-}
+ EP_TYPE_INTERRUPT_IN, // HID_ENDPOINT_INT
+};
-static
-void InitEndpoints()
+static void InitEndpoints()
{
for (u8 i = 1; i < sizeof(_initEndpoints); i++)
{
@@ -347,113 +277,99 @@ void InitEndpoints()
UERST = 0;
}
-// Handle CLASS_INTERFACE requests
-static
-bool ClassInterfaceRequest(Setup& setup)
+typedef struct
{
- u8 i = setup.wIndex;
-
- if (CDC_ACM_INTERFACE == i)
- return CDC_Setup(setup);
-
- return false;
-}
+ u8 bmRequestType;
+ u8 bRequest;
+ u8 wValueL;
+ u8 wValueH;
+ u16 wIndex;
+ u16 wLength;
+} Setup;
+Setup _setup;
-int _cmark;
-int _cend;
-void InitControl(int end)
+//bool USBHook(Setup& setup)
+bool USBHook()
{
- SetEP(0);
- _cmark = 0;
- _cend = end;
-}
+ Setup& setup = _setup;
+ u8 r = setup.bRequest;
-static
-bool SendControl(u8 d)
-{
- if (_cmark < _cend)
+ // CDC Requests
+ if (CDC_GET_LINE_CODING == r)
{
- if (!WaitForINOrOUT())
- return false;
- Send8(d);
- if (!((_cmark + 1) & 0x3F))
- ClearIN(); // Fifo is full, release this packet
+ Send((const volatile u8*)&_usbLineInfo,7);
}
- _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--)
+ else if (CDC_SET_LINE_CODING == r)
{
- u8 c = pgm ? pgm_read_byte(data++) : *data++;
- if (!SendControl(c))
- return -1;
+ WaitOUT();
+ Recv((volatile u8*)&_usbLineInfo,7);
+ ClearOUT();
}
- 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;
-}
+ else if (CDC_SET_CONTROL_LINE_STATE == r)
+ {
+ _usbLineInfo.lineState = setup.wValueL;
+ }
-int SendInterfaces()
-{
- int total = 0;
- u8 interfaces = 0;
- total = CDC_GetInterface(&interfaces);
- return interfaces;
+ return true;
}
-// Construct a dynamic configuration descriptor
-// This really needs dynamic endpoint allocation etc
-// TODO
-static
-bool SendConfiguration(int maxlen)
+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[] =
{
- // Count and measure interfaces
- InitControl(0);
- int interfaces = SendInterfaces();
- ConfigDescriptor config = D_CONFIG(_cmark + sizeof(ConfigDescriptor),interfaces);
+ // RAW HID
+ 0x06, LSB(RAWHID_USAGE_PAGE), MSB(RAWHID_USAGE_PAGE), // 30
+ 0x0A, LSB(RAWHID_USAGE), MSB(RAWHID_USAGE),
- // Now send them
- InitControl(maxlen);
- USB_SendControl(0,&config,sizeof(ConfigDescriptor));
- SendInterfaces();
- return true;
-}
+ 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
-u8 _cdcComposite = 0;
+ 0x95, 64, // report count TX
+ 0x09, 0x01, // usage
+ 0x81, 0x02, // Input (array)
-static
-bool SendDescriptor(Setup& setup)
-{
- u8 t = setup.wValueH;
- if (USB_CONFIGURATION_DESCRIPTOR_TYPE == t)
- return SendConfiguration(setup.wLength);
+ 0x95, 64, // report count RX
+ 0x09, 0x02, // usage
+ 0x91, 0x02, // Output (array)
+ 0xC0 // end collection
+};
- InitControl(setup.wLength);
+u8 _cdcComposite = 0;
+bool SendDescriptor()
+{
+ Setup& setup = _setup;
u8 desc_length = 0;
const u8* desc_addr = 0;
- if (USB_DEVICE_DESCRIPTOR_TYPE == t)
+
+ 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)
@@ -464,106 +380,81 @@ bool SendDescriptor(Setup& setup)
desc_addr = (const u8*)&STRING_IMANUFACTURER;
else
return false;
- }
-
- if (desc_addr == 0)
+ } else
return false;
+
if (desc_length == 0)
desc_length = pgm_read_byte(desc_addr);
-
- USB_SendControl(TRANSFER_PGM,desc_addr,desc_length);
+ 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;
}
-// Endpoint 0 interrupt
-ISR(USB_COM_vect)
+void USBSetupInterrupt()
{
SetEP(0);
if (!ReceivedSetupInt())
return;
- Setup setup;
+ Setup& setup = _setup; // global saves ~30 bytes
Recv((u8*)&setup,8);
ClearSetupInt();
- u8 requestType = setup.bmRequestType;
- if (requestType & REQUEST_DEVICETOHOST)
+ if (setup.bmRequestType & DEVICETOHOST)
WaitIN();
else
ClearIN();
bool ok = true;
- if (REQUEST_STANDARD == (requestType & REQUEST_TYPE))
+ u8 r = setup.bRequest;
+ if (SET_ADDRESS == r)
{
- // 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)
- {
- }
+ 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
{
- InitControl(setup.wLength); // Max length of transfer
- ok = ClassInterfaceRequest(setup);
+ ok = USBHook();
}
if (ok)
ClearIN();
else
- {
Stall();
- }
}
-void USB_Flush(u8 ep)
-{
- SetEP(ep);
- if (FifoByteCount())
- ReleaseTX();
-}
-
-// General interrupt
-ISR(USB_GEN_vect)
+void USBGeneralInterrupt()
{
u8 udint = UDINT;
UDINT = 0;
@@ -573,67 +464,45 @@ ISR(USB_GEN_vect)
{
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))
{
- USB_Flush(CDC_TX); // Send a tx frame if found
-
// check whether the one-shot period has elapsed. if so, turn off the LED
if (TxLEDPulse && !(--TxLEDPulse))
- TX_LED_OFF();
+ TXLED0;
if (RxLEDPulse && !(--RxLEDPulse))
- RX_LED_OFF();
+ RXLED0;
+
+ if (!_ejected)
+ _timeout = 0;
}
}
-// 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 LEDPulse();
+int USBGetChar()
{
-}
-
-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;
-}
+ for(;;)
+ {
+ USBSetupInterrupt();
+ USBGeneralInterrupt();
-void USB_::poll()
-{
+ // 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;
}
-
-#endif /* if defined(USBCON) */