1
0
mirror of https://github.com/arduino/Arduino.git synced 2024-11-29 10:24:12 +01:00

[sam] fixed CDC com issue in Arduino IDE

This commit is contained in:
Thibault RICHARD 2012-06-22 12:29:21 +02:00
parent 21ee4f0774
commit 96e8db0299
11 changed files with 116 additions and 802 deletions

View File

@ -21,6 +21,7 @@
#define CDC_SERIAL_BUFFER_SIZE 64 #define CDC_SERIAL_BUFFER_SIZE 64
/* For information purpose only since RTS is not always handled by the terminal application */
#define CDC_LINESTATE_DTR 0x01 // Data Terminal Ready #define CDC_LINESTATE_DTR 0x01 // Data Terminal Ready
#define CDC_LINESTATE_RTS 0x02 // Ready to Send #define CDC_LINESTATE_RTS 0x02 // Ready to Send
@ -209,7 +210,7 @@ size_t Serial_::write(uint8_t c)
// TODO - ZE - check behavior on different OSes and test what happens if an // TODO - ZE - check behavior on different OSes and test what happens if an
// open connection isn't broken cleanly (cable is yanked out, host dies // open connection isn't broken cleanly (cable is yanked out, host dies
// or locks up, or host virtual serial port hangs) // or locks up, or host virtual serial port hangs)
if (_usbLineInfo.lineState == CDC_LINESTATE_READY) if (_usbLineInfo.lineState > 0)
{ {
int r = USBD_Send(CDC_TX,&c,1); int r = USBD_Send(CDC_TX,&c,1);
@ -238,7 +239,7 @@ Serial_::operator bool()
{ {
bool result = false; bool result = false;
if (_usbLineInfo.lineState == CDC_LINESTATE_READY) if (_usbLineInfo.lineState > 0)
{ {
result = true; result = true;
} }

View File

@ -502,9 +502,12 @@ void Keyboard_::releaseAll(void)
size_t Keyboard_::write(uint8_t c) size_t Keyboard_::write(uint8_t c)
{ {
uint8_t p = press(c); // Keydown uint8_t p = 0;
uint8_t r = release(c); // Keyup
return (p); // just return the result of press() since release() almost always returns 1 p = press(c); // Keydown
release(c); // Keyup
return (p); // Just return the result of press() since release() almost always returns 1
} }
#endif #endif

View File

@ -1,5 +1,5 @@
/* Copyright (c) 2010, Peter Barrett // Copyright (c) 2010, Peter Barrett
** /*
** Permission to use, copy, modify, and/or distribute this software for ** Permission to use, copy, modify, and/or distribute this software for
** any purpose with or without fee is hereby granted, provided that the ** any purpose with or without fee is hereby granted, provided that the
** above copyright notice and this permission notice appear in all copies. ** above copyright notice and this permission notice appear in all copies.
@ -18,8 +18,8 @@
#include "USBAPI.h" #include "USBAPI.h"
#include <stdio.h> #include <stdio.h>
#define TRACE_CORE(x) x //#define TRACE_CORE(x) x
//#define TRACE_CORE(x) #define TRACE_CORE(x)
static const uint32_t EndPoints[] = static const uint32_t EndPoints[] =
{ {
@ -44,11 +44,11 @@ volatile uint8_t RxLEDPulse; /**< Milliseconds remaining for data Rx LED pulse *
//================================================================== //==================================================================
//================================================================== //==================================================================
extern const uint16_t STRING_LANGUAGE[] ; extern const uint16_t STRING_LANGUAGE[];
extern const uint16_t STRING_IPRODUCT[] ; extern const uint16_t STRING_IPRODUCT[];
extern const uint16_t STRING_IMANUFACTURER[] ; extern const uint16_t STRING_IMANUFACTURER[];
extern const DeviceDescriptor USB_DeviceDescriptor ; extern const DeviceDescriptor USB_DeviceDescriptor;
extern const DeviceDescriptor USB_DeviceDescriptorA ; extern const DeviceDescriptor USB_DeviceDescriptorA;
const uint16_t STRING_LANGUAGE[2] = { const uint16_t STRING_LANGUAGE[2] = {
(3<<8) | (2+2), (3<<8) | (2+2),
@ -86,8 +86,6 @@ const DeviceDescriptor USB_DeviceDescriptor =
const DeviceDescriptor USB_DeviceDescriptorA = const DeviceDescriptor USB_DeviceDescriptorA =
D_DEVICE(DEVICE_CLASS,0x00,0x00,64,USB_VID,USB_PID,0x100,IMANUFACTURER,IPRODUCT,0,1); D_DEVICE(DEVICE_CLASS,0x00,0x00,64,USB_VID,USB_PID,0x100,IMANUFACTURER,IPRODUCT,0,1);
//================================================================== //==================================================================
//================================================================== //==================================================================
@ -233,11 +231,11 @@ int USBD_SendControl(uint8_t flags, const void* d, uint32_t len)
// TODO // TODO
int USBD_RecvControl(void* d, uint32_t len) int USBD_RecvControl(void* d, uint32_t len)
{ {
UDD_WaitOUT() ; UDD_WaitOUT();
UDD_Recv(EP0, (uint8_t*)d, len ) ; UDD_Recv(EP0, (uint8_t*)d, len);
UDD_ClearOUT() ; UDD_ClearOUT();
return len ; return len;
} }
// Handle CLASS_INTERFACE requests // Handle CLASS_INTERFACE requests
@ -248,20 +246,20 @@ bool USBD_ClassInterfaceRequest(Setup& setup)
TRACE_CORE(printf("=> USBD_ClassInterfaceRequest\r\n");) TRACE_CORE(printf("=> USBD_ClassInterfaceRequest\r\n");)
#ifdef CDC_ENABLED #ifdef CDC_ENABLED
if ( CDC_ACM_INTERFACE == i ) if (CDC_ACM_INTERFACE == i)
{ {
return CDC_Setup(setup); return CDC_Setup(setup);
} }
#endif #endif
#ifdef HID_ENABLED #ifdef HID_ENABLED
if ( HID_INTERFACE == i ) if (HID_INTERFACE == i)
{ {
return HID_Setup(setup); return HID_Setup(setup);
} }
#endif #endif
return false ; return false;
} }
int USBD_SendInterfaces(void) int USBD_SendInterfaces(void)
@ -270,11 +268,11 @@ int USBD_SendInterfaces(void)
uint8_t interfaces = 0; uint8_t interfaces = 0;
#ifdef CDC_ENABLED #ifdef CDC_ENABLED
total = CDC_GetInterface(&interfaces) ; total = CDC_GetInterface(&interfaces);
#endif #endif
#ifdef HID_ENABLED #ifdef HID_ENABLED
total += HID_GetInterface(&interfaces) ; total += HID_GetInterface(&interfaces);
#endif #endif
total = total; // Get rid of compiler warning total = total; // Get rid of compiler warning
@ -314,7 +312,7 @@ static bool USBD_SendDescriptor(Setup& setup)
uint8_t desc_length = 0; uint8_t desc_length = 0;
const uint8_t* desc_addr = 0; const uint8_t* desc_addr = 0;
if ( USB_CONFIGURATION_DESCRIPTOR_TYPE == t ) if (USB_CONFIGURATION_DESCRIPTOR_TYPE == t)
{ {
TRACE_CORE(printf("=> USBD_SendDescriptor : USB_CONFIGURATION_DESCRIPTOR_TYPE length=%d\r\n", setup.wLength);) TRACE_CORE(printf("=> USBD_SendDescriptor : USB_CONFIGURATION_DESCRIPTOR_TYPE length=%d\r\n", setup.wLength);)
return USBD_SendConfiguration(setup.wLength); return USBD_SendConfiguration(setup.wLength);
@ -322,17 +320,17 @@ static bool USBD_SendDescriptor(Setup& setup)
USBD_InitControl(setup.wLength); USBD_InitControl(setup.wLength);
#ifdef HID_ENABLED #ifdef HID_ENABLED
if ( HID_REPORT_DESCRIPTOR_TYPE == t ) if (HID_REPORT_DESCRIPTOR_TYPE == t)
{ {
TRACE_CORE(puts("=> USBD_SendDescriptor : HID_REPORT_DESCRIPTOR_TYPE\r\n");) TRACE_CORE(puts("=> USBD_SendDescriptor : HID_REPORT_DESCRIPTOR_TYPE\r\n");)
return HID_GetDescriptor( t ) ; return HID_GetDescriptor(t);
} }
#endif #endif
if (USB_DEVICE_DESCRIPTOR_TYPE == t) if (USB_DEVICE_DESCRIPTOR_TYPE == t)
{ {
TRACE_CORE(puts("=> USBD_SendDescriptor : USB_DEVICE_DESCRIPTOR_TYPE\r\n");) TRACE_CORE(puts("=> USBD_SendDescriptor : USB_DEVICE_DESCRIPTOR_TYPE\r\n");)
if ( setup.wLength == 8 ) if (setup.wLength == 8)
{ {
_cdcComposite = 1; _cdcComposite = 1;
} }
@ -351,17 +349,17 @@ static bool USBD_SendDescriptor(Setup& setup)
return false; return false;
} }
if ( desc_addr == 0 ) if (desc_addr == 0)
{ {
return false ; return false;
} }
if ( desc_length == 0 ) if (desc_length == 0)
{ {
desc_length = *desc_addr; desc_length = *desc_addr;
} }
TRACE_CORE(printf("=> USBD_SendDescriptor : desc_addr=%x desc_length=%d\r\n", desc_addr, desc_length);) TRACE_CORE(printf("=> USBD_SendDescriptor : desc_addr=%p desc_length=%d\r\n", desc_addr, desc_length);)
USBD_SendControl(0, desc_addr, desc_length); USBD_SendControl(0, desc_addr, desc_length);
return true; return true;
@ -397,7 +395,7 @@ static void USB_ISR(void)
while (USBD_Available(CDC_RX)) while (USBD_Available(CDC_RX))
Serial.accept(); Serial.accept();
udd_ack_fifocon(CDC_RX) ; udd_ack_fifocon(CDC_RX);
} }
#endif #endif
@ -405,12 +403,12 @@ static void USB_ISR(void)
if (Is_udd_endpoint_interrupt(0)) if (Is_udd_endpoint_interrupt(0))
{ {
if ( !UDD_ReceivedSetupInt() ) if (!UDD_ReceivedSetupInt())
{ {
return; return;
} }
Setup setup ; Setup setup;
UDD_Recv(EP0, (uint8_t*)&setup, 8); UDD_Recv(EP0, (uint8_t*)&setup, 8);
UDD_ClearSetupInt(); UDD_ClearSetupInt();
@ -426,7 +424,7 @@ static void USB_ISR(void)
UDD_ClearIN(); UDD_ClearIN();
} }
bool ok = true ; bool ok = true;
if (REQUEST_STANDARD == (requestType & REQUEST_TYPE)) if (REQUEST_STANDARD == (requestType & REQUEST_TYPE))
{ {
// Standard Requests // Standard Requests
@ -544,36 +542,36 @@ USB_::USB_()
{ {
UDD_SetStack(&USB_ISR); UDD_SetStack(&USB_ISR);
if ( UDD_Init() == 0UL ) if (UDD_Init() == 0UL)
{ {
_usbInitialized=1UL ; _usbInitialized=1UL;
} }
} }
bool USB_::attach(void) bool USB_::attach(void)
{ {
if ( _usbInitialized != 0UL ) if (_usbInitialized != 0UL)
{ {
UDD_Attach() ; UDD_Attach();
_usbConfiguration = 0; _usbConfiguration = 0;
return true; return true;
} }
else else
{ {
return false ; return false;
} }
} }
bool USB_::detach(void) bool USB_::detach(void)
{ {
if ( _usbInitialized != 0UL ) if (_usbInitialized != 0UL)
{ {
UDD_Detach() ; UDD_Detach();
return true ; return true;
} }
else else
{ {
return false ; return false;
} }
} }

View File

@ -1,672 +0,0 @@
/* Copyright (c) 2010, Peter Barrett
**
** Permission to use, copy, modify, and/or distribute this software for
** any purpose with or without fee is hereby granted, provided that the
** above copyright notice and this permission notice appear in all copies.
**
** THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL
** WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED
** WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR
** BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES
** OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
** WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,
** ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
** SOFTWARE.
*/
#include "Platform.h"
#include "USBAPI.h"
#include "USBDesc.h"
#if defined(USBCON)
#define EP_TYPE_CONTROL 0x00
#define EP_TYPE_BULK_IN 0x81
#define EP_TYPE_BULK_OUT 0x80
#define EP_TYPE_INTERRUPT_IN 0xC1
#define EP_TYPE_INTERRUPT_OUT 0xC0
#define EP_TYPE_ISOCHRONOUS_IN 0x41
#define EP_TYPE_ISOCHRONOUS_OUT 0x40
/** Pulse generation counters to keep track of the number of milliseconds remaining for each pulse type */
#define TX_RX_LED_PULSE_MS 100
volatile u8 TxLEDPulse; /**< Milliseconds remaining for data Tx LED pulse */
volatile u8 RxLEDPulse; /**< Milliseconds remaining for data Rx LED pulse */
//==================================================================
//==================================================================
extern const u16 STRING_LANGUAGE[] PROGMEM;
extern const u16 STRING_IPRODUCT[] PROGMEM;
extern const u16 STRING_IMANUFACTURER[] PROGMEM;
extern const DeviceDescriptor USB_DeviceDescriptor PROGMEM;
extern const DeviceDescriptor USB_DeviceDescriptorA PROGMEM;
const u16 STRING_LANGUAGE[2] = {
(3<<8) | (2+2),
0x0409 // English
};
const u16 STRING_IPRODUCT[17] = {
(3<<8) | (2+2*16),
#if USB_PID == 0x8034
'A','r','d','u','i','n','o',' ','L','e','o','n','a','r','d','o'
#else
'U','S','B',' ','I','O',' ','B','O','A','R','D',' ',' ',' ',' '
#endif
};
const u16 STRING_IMANUFACTURER[12] = {
(3<<8) | (2+2*11),
#if USB_VID == 0x2341
'A','r','d','u','i','n','o',' ','L','L','C'
#else
'U','n','k','n','o','w','n',' ',' ',' ',' '
#endif
};
#ifdef CDC_ENABLED
#define DEVICE_CLASS 0x02
#else
#define DEVICE_CLASS 0x00
#endif
// DEVICE DESCRIPTOR
const DeviceDescriptor USB_DeviceDescriptor =
D_DEVICE(0x00,0x00,0x00,64,USB_VID,USB_PID,0x100,IMANUFACTURER,IPRODUCT,0,1);
const DeviceDescriptor USB_DeviceDescriptorA =
D_DEVICE(DEVICE_CLASS,0x00,0x00,64,USB_VID,USB_PID,0x100,IMANUFACTURER,IPRODUCT,0,1);
//==================================================================
//==================================================================
volatile u8 _usbConfiguration = 0;
static inline void WaitIN(void)
{
while (!(UEINTX & (1<<TXINI)));
}
static inline void ClearIN(void)
{
UEINTX = ~(1<<TXINI);
}
static inline void WaitOUT(void)
{
while (!(UEINTX & (1<<RXOUTI)))
;
}
static inline u8 WaitForINOrOUT()
{
while (!(UEINTX & ((1<<TXINI)|(1<<RXOUTI))))
;
return (UEINTX & (1<<RXOUTI)) == 0;
}
static inline void ClearOUT(void)
{
UEINTX = ~(1<<RXOUTI);
}
void Recv(volatile u8* data, u8 count)
{
while (count--)
*data++ = UEDATX;
RXLED1; // light the RX LED
RxLEDPulse = TX_RX_LED_PULSE_MS;
}
static inline u8 Recv8()
{
RXLED1; // light the RX LED
RxLEDPulse = TX_RX_LED_PULSE_MS;
return UEDATX;
}
static inline void Send8(u8 d)
{
UEDATX = d;
}
static inline void SetEP(u8 ep)
{
UENUM = ep;
}
static inline u8 FifoByteCount()
{
return UEBCLX;
}
static inline u8 ReceivedSetupInt()
{
return UEINTX & (1<<RXSTPI);
}
static inline void ClearSetupInt()
{
UEINTX = ~((1<<RXSTPI) | (1<<RXOUTI) | (1<<TXINI));
}
static inline void Stall()
{
UECONX = (1<<STALLRQ) | (1<<EPEN);
}
static inline u8 ReadWriteAllowed()
{
return UEINTX & (1<<RWAL);
}
static inline u8 Stalled()
{
return UEINTX & (1<<STALLEDI);
}
static inline u8 FifoFree()
{
return UEINTX & (1<<FIFOCON);
}
static inline void ReleaseRX()
{
UEINTX = 0x6B; // FIFOCON=0 NAKINI=1 RWAL=1 NAKOUTI=0 RXSTPI=1 RXOUTI=0 STALLEDI=1 TXINI=1
}
static inline void ReleaseTX()
{
UEINTX = 0x3A; // FIFOCON=0 NAKINI=0 RWAL=1 NAKOUTI=1 RXSTPI=1 RXOUTI=0 STALLEDI=1 TXINI=0
}
static inline u8 FrameNumber()
{
return UDFNUML;
}
//==================================================================
//==================================================================
u8 USBGetConfiguration(void)
{
return _usbConfiguration;
}
#define USB_RECV_TIMEOUT
class LockEP
{
u8 _sreg;
public:
LockEP(u8 ep) : _sreg(SREG)
{
cli();
SetEP(ep & 7);
}
~LockEP()
{
SREG = _sreg;
}
};
// Number of bytes, assumes a rx endpoint
u8 USB_Available(u8 ep)
{
LockEP lock(ep);
return FifoByteCount();
}
// Non Blocking receive
// Return number of bytes read
int USB_Recv(u8 ep, void* d, int len)
{
if (!_usbConfiguration || len < 0)
return -1;
LockEP lock(ep);
u8 n = FifoByteCount();
len = min(n,len);
n = len;
u8* dst = (u8*)d;
while (n--)
*dst++ = Recv8();
if (len && !FifoByteCount()) // release empty buffer
ReleaseRX();
return len;
}
// Recv 1 byte if ready
int USB_Recv(u8 ep)
{
u8 c;
if (USB_Recv(ep,&c,1) != 1)
return -1;
return c;
}
// Space in send EP
u8 USB_SendSpace(u8 ep)
{
LockEP lock(ep);
if (!ReadWriteAllowed())
return 0;
return 64 - FifoByteCount();
}
// Blocking Send of data to an endpoint
int USB_Send(u8 ep, const void* d, int len)
{
if (!_usbConfiguration)
return -1;
int r = len;
const u8* data = (const u8*)d;
u8 zero = ep & TRANSFER_ZERO;
u8 timeout = 250; // 250ms timeout on send? TODO
while (len)
{
u8 n = USB_SendSpace(ep);
if (n == 0)
{
if (!(--timeout))
return -1;
delay(1);
continue;
}
if (n > len)
n = len;
len -= n;
{
LockEP lock(ep);
if (ep & TRANSFER_ZERO)
{
while (n--)
Send8(0);
}
else if (ep & TRANSFER_PGM)
{
while (n--)
Send8(pgm_read_byte(data++));
}
else
{
while (n--)
Send8(*data++);
}
if (!ReadWriteAllowed() || ((len == 0) && (ep & TRANSFER_RELEASE))) // Release full buffer
ReleaseTX();
}
}
TXLED1; // light the TX LED
TxLEDPulse = TX_RX_LED_PULSE_MS;
return r;
}
extern const u8 _initEndpoints[] PROGMEM;
const u8 _initEndpoints[] =
{
0,
#ifdef CDC_ENABLED
EP_TYPE_INTERRUPT_IN, // CDC_ENDPOINT_ACM
EP_TYPE_BULK_OUT, // CDC_ENDPOINT_OUT
EP_TYPE_BULK_IN, // CDC_ENDPOINT_IN
#endif
#ifdef HID_ENABLED
EP_TYPE_INTERRUPT_IN // HID_ENDPOINT_INT
#endif
};
#define EP_SINGLE_64 0x32 // EP0
#define EP_DOUBLE_64 0x36 // Other endpoints
static
void InitEP(u8 index, u8 type, u8 size)
{
UENUM = index;
UECONX = 1;
UECFG0X = type;
UECFG1X = size;
}
static
void InitEndpoints()
{
for (u8 i = 1; i < sizeof(_initEndpoints); i++)
{
UENUM = i;
UECONX = 1;
UECFG0X = pgm_read_byte(_initEndpoints+i);
UECFG1X = EP_DOUBLE_64;
}
UERST = 0x7E; // And reset them
UERST = 0;
}
// Handle CLASS_INTERFACE requests
static
bool ClassInterfaceRequest(Setup& setup)
{
u8 i = setup.wIndex;
#ifdef CDC_ENABLED
if (CDC_ACM_INTERFACE == i)
return CDC_Setup(setup);
#endif
#ifdef HID_ENABLED
if (HID_INTERFACE == i)
return HID_Setup(setup);
#endif
return false;
}
int _cmark;
int _cend;
void InitControl(int end)
{
SetEP(0);
_cmark = 0;
_cend = end;
}
static
bool SendControl(u8 d)
{
if (_cmark < _cend)
{
if (!WaitForINOrOUT())
return false;
Send8(d);
if (!((_cmark + 1) & 0x3F))
ClearIN(); // Fifo is full, release this packet
}
_cmark++;
return true;
};
// Clipped by _cmark/_cend
int USB_SendControl(u8 flags, const void* d, int len)
{
int sent = len;
const u8* data = (const u8*)d;
bool pgm = flags & TRANSFER_PGM;
while (len--)
{
u8 c = pgm ? pgm_read_byte(data++) : *data++;
if (!SendControl(c))
return -1;
}
return sent;
}
// Does not timeout or cross fifo boundaries
// Will only work for transfers <= 64 bytes
// TODO
int USB_RecvControl(void* d, int len)
{
WaitOUT();
Recv((u8*)d,len);
ClearOUT();
return len;
}
int SendInterfaces()
{
int total = 0;
u8 interfaces = 0;
#ifdef CDC_ENABLED
total = CDC_GetInterface(&interfaces);
#endif
#ifdef HID_ENABLED
total += HID_GetInterface(&interfaces);
#endif
return interfaces;
}
// Construct a dynamic configuration descriptor
// This really needs dynamic endpoint allocation etc
// TODO
static
bool SendConfiguration(int maxlen)
{
// Count and measure interfaces
InitControl(0);
int interfaces = SendInterfaces();
ConfigDescriptor config = D_CONFIG(_cmark + sizeof(ConfigDescriptor),interfaces);
// Now send them
InitControl(maxlen);
USB_SendControl(0,&config,sizeof(ConfigDescriptor));
SendInterfaces();
return true;
}
u8 _cdcComposite = 0;
static
bool SendDescriptor(Setup& setup)
{
u8 t = setup.wValueH;
if (USB_CONFIGURATION_DESCRIPTOR_TYPE == t)
return SendConfiguration(setup.wLength);
InitControl(setup.wLength);
#ifdef HID_ENABLED
if (HID_REPORT_DESCRIPTOR_TYPE == t)
return HID_GetDescriptor(t);
#endif
u8 desc_length = 0;
const u8* desc_addr = 0;
if (USB_DEVICE_DESCRIPTOR_TYPE == t)
{
if (setup.wLength == 8)
_cdcComposite = 1;
desc_addr = _cdcComposite ? (const u8*)&USB_DeviceDescriptorA : (const u8*)&USB_DeviceDescriptor;
}
else if (USB_STRING_DESCRIPTOR_TYPE == t)
{
if (setup.wValueL == 0)
desc_addr = (const u8*)&STRING_LANGUAGE;
else if (setup.wValueL == IPRODUCT)
desc_addr = (const u8*)&STRING_IPRODUCT;
else if (setup.wValueL == IMANUFACTURER)
desc_addr = (const u8*)&STRING_IMANUFACTURER;
else
return false;
}
if (desc_addr == 0)
return false;
if (desc_length == 0)
desc_length = pgm_read_byte(desc_addr);
USB_SendControl(TRANSFER_PGM,desc_addr,desc_length);
return true;
}
// Endpoint 0 interrupt
ISR(USB_COM_vect)
{
SetEP(0);
if (!ReceivedSetupInt())
return;
Setup setup;
Recv((u8*)&setup,8);
ClearSetupInt();
u8 requestType = setup.bmRequestType;
if (requestType & REQUEST_DEVICETOHOST)
WaitIN();
else
ClearIN();
bool ok = true;
if (REQUEST_STANDARD == (requestType & REQUEST_TYPE))
{
// Standard Requests
u8 r = setup.bRequest;
if (GET_STATUS == r)
{
Send8(0); // TODO
Send8(0);
}
else if (CLEAR_FEATURE == r)
{
}
else if (SET_FEATURE == r)
{
}
else if (SET_ADDRESS == r)
{
WaitIN();
UDADDR = setup.wValueL | (1<<ADDEN);
}
else if (GET_DESCRIPTOR == r)
{
ok = SendDescriptor(setup);
}
else if (SET_DESCRIPTOR == r)
{
ok = false;
}
else if (GET_CONFIGURATION == r)
{
Send8(1);
}
else if (SET_CONFIGURATION == r)
{
if (REQUEST_DEVICE == (requestType & REQUEST_RECIPIENT))
{
InitEndpoints();
_usbConfiguration = setup.wValueL;
} else
ok = false;
}
else if (GET_INTERFACE == r)
{
}
else if (SET_INTERFACE == r)
{
}
}
else
{
InitControl(setup.wLength); // Max length of transfer
ok = ClassInterfaceRequest(setup);
}
if (ok)
ClearIN();
else
{
Stall();
}
}
void USB_Flush(u8 ep)
{
SetEP(ep);
if (FifoByteCount())
ReleaseTX();
}
// General interrupt
ISR(USB_GEN_vect)
{
u8 udint = UDINT;
UDINT = 0;
// End of Reset
if (udint & (1<<EORSTI))
{
InitEP(0,EP_TYPE_CONTROL,EP_SINGLE_64); // init ep0
_usbConfiguration = 0; // not configured yet
UEIENX = 1 << RXSTPE; // Enable interrupts for ep0
}
// Start of Frame - happens every millisecond so we use it for TX and RX LED one-shot timing, too
if (udint & (1<<SOFI))
{
#ifdef CDC_ENABLED
USB_Flush(CDC_TX); // Send a tx frame if found
while (USB_Available(CDC_RX)) // Handle received bytes (if any)
Serial.accept();
#endif
// check whether the one-shot period has elapsed. if so, turn off the LED
if (TxLEDPulse && !(--TxLEDPulse))
TXLED0;
if (RxLEDPulse && !(--RxLEDPulse))
RXLED0;
}
}
// VBUS or counting frames
// Any frame counting?
u8 USBConnected()
{
u8 f = UDFNUML;
delay(3);
return f != UDFNUML;
}
//=======================================================================
//=======================================================================
USB_ USB;
USB_::USB_()
{
}
void USB_::attach()
{
_usbConfiguration = 0;
UHWCON = 0x01; // power internal reg
USBCON = (1<<USBE)|(1<<FRZCLK); // clock frozen, usb enabled
PLLCSR = 0x12; // Need 16 MHz xtal
while (!(PLLCSR & (1<<PLOCK))) // wait for lock pll
;
// Some tests on specific versions of macosx (10.7.3), reported some
// strange behaviuors when the board is reset using the serial
// port touch at 1200 bps. This delay fixes this behaviour.
delay(1);
USBCON = ((1<<USBE)|(1<<OTGPADE)); // start USB clock
UDIEN = (1<<EORSTE)|(1<<SOFE); // Enable interrupts for EOR (End of Reset) and SOF (start of frame)
UDCON = 0; // enable attach resistor
TX_RX_LED_INIT;
}
void USB_::detach()
{
}
// Check for interrupts
// TODO: VBUS detection
bool USB_::configured()
{
return _usbConfiguration;
}
void USB_::poll()
{
}
#endif /* if defined(USBCON) */

View File

@ -1,4 +1,3 @@
// Copyright (c) 2010, Peter Barrett // Copyright (c) 2010, Peter Barrett
/* /*
** Permission to use, copy, modify, and/or distribute this software for ** Permission to use, copy, modify, and/or distribute this software for
@ -19,33 +18,33 @@
#define __USBCORE_H__ #define __USBCORE_H__
// Standard requests // Standard requests
#define GET_STATUS 0 #define GET_STATUS 0
#define CLEAR_FEATURE 1 #define CLEAR_FEATURE 1
#define SET_FEATURE 3 #define SET_FEATURE 3
#define SET_ADDRESS 5 #define SET_ADDRESS 5
#define GET_DESCRIPTOR 6 #define GET_DESCRIPTOR 6
#define SET_DESCRIPTOR 7 #define SET_DESCRIPTOR 7
#define GET_CONFIGURATION 8 #define GET_CONFIGURATION 8
#define SET_CONFIGURATION 9 #define SET_CONFIGURATION 9
#define GET_INTERFACE 10 #define GET_INTERFACE 10
#define SET_INTERFACE 11 #define SET_INTERFACE 11
// bmRequestType // bmRequestType
#define REQUEST_HOSTTODEVICE 0x00 #define REQUEST_HOSTTODEVICE 0x00
#define REQUEST_DEVICETOHOST 0x80 #define REQUEST_DEVICETOHOST 0x80
#define REQUEST_DIRECTION 0x80 #define REQUEST_DIRECTION 0x80
#define REQUEST_STANDARD 0x00 #define REQUEST_STANDARD 0x00
#define REQUEST_CLASS 0x20 #define REQUEST_CLASS 0x20
#define REQUEST_VENDOR 0x40 #define REQUEST_VENDOR 0x40
#define REQUEST_TYPE 0x60 #define REQUEST_TYPE 0x60
#define REQUEST_DEVICE 0x00 #define REQUEST_DEVICE 0x00
#define REQUEST_INTERFACE 0x01 #define REQUEST_INTERFACE 0x01
#define REQUEST_ENDPOINT 0x02 #define REQUEST_ENDPOINT 0x02
#define REQUEST_OTHER 0x03 #define REQUEST_OTHER 0x03
#define REQUEST_RECIPIENT 0x1F #define REQUEST_RECIPIENT 0x1F
#define REQUEST_DEVICETOHOST_CLASS_INTERFACE (REQUEST_DEVICETOHOST + REQUEST_CLASS + REQUEST_INTERFACE) #define REQUEST_DEVICETOHOST_CLASS_INTERFACE (REQUEST_DEVICETOHOST + REQUEST_CLASS + REQUEST_INTERFACE)
#define REQUEST_HOSTTODEVICE_CLASS_INTERFACE (REQUEST_HOSTTODEVICE + REQUEST_CLASS + REQUEST_INTERFACE) #define REQUEST_HOSTTODEVICE_CLASS_INTERFACE (REQUEST_HOSTTODEVICE + REQUEST_CLASS + REQUEST_INTERFACE)
@ -129,12 +128,12 @@ _Pragma("pack(1)")
// Device // Device
typedef struct { typedef struct {
uint8_t len; // 18 uint8_t len; // 18
uint8_t dtype; // 1 USB_DEVICE_DESCRIPTOR_TYPE uint8_t dtype; // 1 USB_DEVICE_DESCRIPTOR_TYPE
uint16_t usbVersion; // 0x200 uint16_t usbVersion; // 0x200
uint8_t deviceClass; uint8_t deviceClass;
uint8_t deviceSubClass; uint8_t deviceSubClass;
uint8_t deviceProtocol; uint8_t deviceProtocol;
uint8_t packetSize0; // Packet 0 uint8_t packetSize0; // Packet 0
uint16_t idVendor; uint16_t idVendor;
uint16_t idProduct; uint16_t idProduct;
uint16_t deviceVersion; // 0x100 uint16_t deviceVersion; // 0x100
@ -162,7 +161,7 @@ typedef struct {
typedef struct typedef struct
{ {
uint8_t len; // 9 uint8_t len; // 9
uint8_t dtype; // 4 uint8_t dtype; // 4
uint8_t number; uint8_t number;
uint8_t alternate; uint8_t alternate;
uint8_t numEndpoints; uint8_t numEndpoints;
@ -176,7 +175,7 @@ typedef struct
typedef struct typedef struct
{ {
uint8_t len; // 7 uint8_t len; // 7
uint8_t dtype; // 5 uint8_t dtype; // 5
uint8_t addr; uint8_t addr;
uint8_t attr; uint8_t attr;
uint16_t packetSize; uint16_t packetSize;
@ -188,7 +187,7 @@ typedef struct
typedef struct typedef struct
{ {
uint8_t len; // 8 uint8_t len; // 8
uint8_t dtype; // 11 uint8_t dtype; // 11
uint8_t firstInterface; uint8_t firstInterface;
uint8_t interfaceCount; uint8_t interfaceCount;
uint8_t functionClass; uint8_t functionClass;
@ -201,7 +200,7 @@ typedef struct
typedef struct typedef struct
{ {
uint8_t len; // 5 uint8_t len; // 5
uint8_t dtype; // 0x24 uint8_t dtype; // 0x24
uint8_t subtype; uint8_t subtype;
uint8_t d0; uint8_t d0;
uint8_t d1; uint8_t d1;
@ -210,7 +209,7 @@ typedef struct
typedef struct typedef struct
{ {
uint8_t len; // 4 uint8_t len; // 4
uint8_t dtype; // 0x24 uint8_t dtype; // 0x24
uint8_t subtype; uint8_t subtype;
uint8_t d0; uint8_t d0;
} CDCCSInterfaceDescriptor4; } CDCCSInterfaceDescriptor4;
@ -238,7 +237,7 @@ typedef struct
IADDescriptor iad; // Only needed on compound device IADDescriptor iad; // Only needed on compound device
// Control // Control
InterfaceDescriptor cif; // InterfaceDescriptor cif;
CDCCSInterfaceDescriptor header; CDCCSInterfaceDescriptor header;
CMFunctionalDescriptor callManagement; // Call Management CMFunctionalDescriptor callManagement; // Call Management
ACMFunctionalDescriptor controlManagement; // ACM ACMFunctionalDescriptor controlManagement; // ACM
@ -261,21 +260,21 @@ typedef struct
typedef struct typedef struct
{ {
uint8_t len; // 9 uint8_t len; // 9
uint8_t dtype; // 0x21 uint8_t dtype; // 0x21
uint8_t addr; uint8_t addr;
uint8_t versionL; // 0x101 uint8_t versionL; // 0x101
uint8_t versionH; // 0x101 uint8_t versionH; // 0x101
uint8_t country; uint8_t country;
uint8_t desctype; // 0x22 report uint8_t desctype; // 0x22 report
uint8_t descLenL; uint8_t descLenL;
uint8_t descLenH; uint8_t descLenH;
} HIDDescDescriptor; } HIDDescDescriptor;
typedef struct typedef struct
{ {
InterfaceDescriptor hid; InterfaceDescriptor hid;
HIDDescDescriptor desc; HIDDescDescriptor desc;
EndpointDescriptor in; EndpointDescriptor in;
} HIDDescriptor; } HIDDescriptor;
_Pragma("pack()") _Pragma("pack()")

View File

@ -1,7 +1,5 @@
// Copyright (c) 2010, Peter Barrett
/*
/* Copyright (c) 2011, Peter Barrett
**
** Permission to use, copy, modify, and/or distribute this software for ** Permission to use, copy, modify, and/or distribute this software for
** any purpose with or without fee is hereby granted, provided that the ** any purpose with or without fee is hereby granted, provided that the
** above copyright notice and this permission notice appear in all copies. ** above copyright notice and this permission notice appear in all copies.
@ -16,10 +14,12 @@
** SOFTWARE. ** SOFTWARE.
*/ */
#ifndef __USBDESC_H__
#define __USBDESC_H__
#define CDC_ENABLED #define CDC_ENABLED
#define HID_ENABLED #define HID_ENABLED
#ifdef CDC_ENABLED #ifdef CDC_ENABLED
#define CDC_INTERFACE_COUNT 2 #define CDC_INTERFACE_COUNT 2
#define CDC_ENPOINT_COUNT 3 #define CDC_ENPOINT_COUNT 3
@ -39,11 +39,11 @@
#define CDC_ACM_INTERFACE 0 // CDC ACM #define CDC_ACM_INTERFACE 0 // CDC ACM
#define CDC_DATA_INTERFACE 1 // CDC Data #define CDC_DATA_INTERFACE 1 // CDC Data
#define CDC_FIRST_ENDPOINT 1 #define CDC_FIRST_ENDPOINT 1
#define CDC_ENDPOINT_ACM (CDC_FIRST_ENDPOINT) // CDC First #define CDC_ENDPOINT_ACM (CDC_FIRST_ENDPOINT) // CDC First
#define CDC_ENDPOINT_OUT (CDC_FIRST_ENDPOINT+1) #define CDC_ENDPOINT_OUT (CDC_FIRST_ENDPOINT+1)
#define CDC_ENDPOINT_IN (CDC_FIRST_ENDPOINT+2) #define CDC_ENDPOINT_IN (CDC_FIRST_ENDPOINT+2)
#define HID_INTERFACE (CDC_ACM_INTERFACE + CDC_INTERFACE_COUNT) // HID Interface #define HID_INTERFACE (CDC_ACM_INTERFACE + CDC_INTERFACE_COUNT) // HID Interface
#define HID_FIRST_ENDPOINT (CDC_FIRST_ENDPOINT + CDC_ENPOINT_COUNT) #define HID_FIRST_ENDPOINT (CDC_FIRST_ENDPOINT + CDC_ENPOINT_COUNT)
#define HID_ENDPOINT_INT (HID_FIRST_ENDPOINT) #define HID_ENDPOINT_INT (HID_FIRST_ENDPOINT)
@ -60,3 +60,5 @@
#define IMANUFACTURER 1 #define IMANUFACTURER 1
#define IPRODUCT 2 #define IPRODUCT 2
#endif /* __USBDESC_H__ */

View File

@ -25,7 +25,6 @@ void setup() {
} }
void loop() { void loop() {
Mouse.move(1, 0, 0); Mouse.move(1, 0, 0);
if (Serial.available() > 0) if (Serial.available() > 0)

View File

@ -32,49 +32,35 @@
#include <stdint.h> #include <stdint.h>
extern void UDD_WaitIN(void) ; extern void UDD_WaitIN(void);
extern void UDD_WaitOUT(void) ; extern void UDD_WaitOUT(void);
extern void UDD_ClearIN(void) ; extern void UDD_ClearIN(void);
extern void UDD_ClearOUT(void) ; extern void UDD_ClearOUT(void);
extern uint32_t UDD_WaitForINOrOUT(void) ; extern uint32_t UDD_WaitForINOrOUT(void);
extern void UDD_ClearRxFlag(unsigned char bEndpoint);
extern void UDD_ClearRxFlag( unsigned char bEndpoint ) ;
extern uint32_t UDD_ReceivedSetupInt(void); extern uint32_t UDD_ReceivedSetupInt(void);
extern void UDD_ClearSetupInt(void); extern void UDD_ClearSetupInt(void);
extern uint32_t UDD_ReadWriteAllowed(uint32_t ep);
extern uint32_t UDD_ReadWriteAllowed(uint32_t ep) ; extern uint32_t UDD_FifoByteCount(uint32_t ep);
extern uint8_t UDD_FifoFree(void);
extern void UDD_ReleaseRX(uint32_t ep);
extern uint32_t UDD_FifoByteCount(uint32_t ep) ; extern void UDD_ReleaseTX(uint32_t ep);
extern uint8_t UDD_FifoFree(void) ; extern uint8_t UDD_FrameNumber(void);
extern uint8_t UDD_GetConfiguration(void);
extern void UDD_ReleaseRX(uint32_t ep) ;
extern void UDD_ReleaseTX(uint32_t ep) ;
extern uint8_t UDD_FrameNumber(void) ;
extern uint8_t UDD_GetConfiguration(void) ;
extern uint32_t UDD_Send(uint32_t ep, const void* data, uint32_t len); extern uint32_t UDD_Send(uint32_t ep, const void* data, uint32_t len);
extern void UDD_Send8(uint32_t ep, uint8_t data ); extern void UDD_Send8(uint32_t ep, uint8_t data );
extern uint8_t UDD_Recv8(uint32_t ep); extern uint8_t UDD_Recv8(uint32_t ep);
extern void UDD_Recv(uint32_t ep, uint8_t* data, uint32_t len); extern void UDD_Recv(uint32_t ep, uint8_t* data, uint32_t len);
extern void UDD_InitEndpoints(const uint32_t* eps_table, const uint32_t ul_eps_table_size); extern void UDD_InitEndpoints(const uint32_t* eps_table, const uint32_t ul_eps_table_size);
extern void UDD_InitControl(int end);
extern void UDD_InitControl(int end) ; extern uint32_t UDD_Init(void);
extern uint32_t UDD_Init(void) ;
extern void UDD_InitEP( uint32_t ul_ep, uint32_t ul_ep_cfg ); extern void UDD_InitEP( uint32_t ul_ep, uint32_t ul_ep_cfg );
extern void UDD_Attach(void) ; extern void UDD_Attach(void);
extern void UDD_Detach(void) ; extern void UDD_Detach(void);
extern void UDD_SetStack(void (*pf_isr)(void)); extern void UDD_SetStack(void (*pf_isr)(void));
extern void UDD_SetAddress(uint32_t addr); extern void UDD_SetAddress(uint32_t addr);
extern void UDD_Stall(void); extern void UDD_Stall(void);
@ -91,8 +77,8 @@ typedef unsigned char Bool; //!< Boolean.
typedef unsigned char bool; //!< Boolean. typedef unsigned char bool; //!< Boolean.
#endif #endif
#endif #endif
typedef int8_t S8 ; //!< 8-bit signed integer. typedef int8_t S8; //!< 8-bit signed integer.
typedef uint8_t U8 ; //!< 8-bit unsigned integer. typedef uint8_t U8; //!< 8-bit unsigned integer.
typedef int16_t S16; //!< 16-bit signed integer. typedef int16_t S16; //!< 16-bit signed integer.
typedef uint16_t U16; //!< 16-bit unsigned integer. typedef uint16_t U16; //!< 16-bit unsigned integer.
typedef uint16_t le16_t; typedef uint16_t le16_t;

View File

@ -32,8 +32,8 @@
#if SAM3XA_SERIES #if SAM3XA_SERIES
#define TRACE_UOTGHS_DEVICE(x) x //#define TRACE_UOTGHS_DEVICE(x) x
//#define TRACE_UOTGHS_DEVICE(x) #define TRACE_UOTGHS_DEVICE(x)
extern void (*gpf_isr)(void); extern void (*gpf_isr)(void);

View File

@ -376,12 +376,10 @@ uotghs_device.o:
00000000 T UDD_WaitOUT 00000000 T UDD_WaitOUT
U g_interrupt_enabled U g_interrupt_enabled
U gpf_isr U gpf_isr
U iprintf
U pmc_enable_periph_clk U pmc_enable_periph_clk
U pmc_enable_udpck U pmc_enable_udpck
U pmc_enable_upll_clock U pmc_enable_upll_clock
U pmc_switch_udpck_to_upllck U pmc_switch_udpck_to_upllck
U puts
00000000 b ul_recv_fifo_ptr 00000000 b ul_recv_fifo_ptr
00000000 b ul_send_fifo_ptr 00000000 b ul_send_fifo_ptr