mirror of
https://github.com/esp8266/Arduino.git
synced 2025-06-17 22:23:10 +03:00
[sam] update USB device API
This commit is contained in:
@ -18,16 +18,25 @@
|
|||||||
|
|
||||||
#include "Platform.h"
|
#include "Platform.h"
|
||||||
#include "USBAPI.h"
|
#include "USBAPI.h"
|
||||||
|
#include <avr/wdt.h>
|
||||||
|
|
||||||
#if defined(USBCON)
|
#if defined(USBCON)
|
||||||
#ifdef CDC_ENABLED
|
#ifdef CDC_ENABLED
|
||||||
|
|
||||||
void Reboot()
|
#if (RAMEND < 1000)
|
||||||
|
#define SERIAL_BUFFER_SIZE 16
|
||||||
|
#else
|
||||||
|
#define SERIAL_BUFFER_SIZE 64
|
||||||
|
#endif
|
||||||
|
|
||||||
|
struct ring_buffer
|
||||||
{
|
{
|
||||||
USB.detach();
|
unsigned char buffer[SERIAL_BUFFER_SIZE];
|
||||||
cli();
|
volatile int head;
|
||||||
asm volatile("jmp 0x7800"); // jump to bootloader - DiskLoader takes up last 2 kB
|
volatile int tail;
|
||||||
}
|
};
|
||||||
|
|
||||||
|
ring_buffer cdc_rx_buffer = { { 0 }, 0, 0};
|
||||||
|
|
||||||
typedef struct
|
typedef struct
|
||||||
{
|
{
|
||||||
@ -91,9 +100,28 @@ bool WEAK CDC_Setup(Setup& setup)
|
|||||||
|
|
||||||
if (CDC_SET_CONTROL_LINE_STATE == r)
|
if (CDC_SET_CONTROL_LINE_STATE == r)
|
||||||
{
|
{
|
||||||
if (0 != _usbLineInfo.lineState && 1200 == _usbLineInfo.dwDTERate) // auto-reset is triggered when the port, already open at 1200 bps, is closed
|
|
||||||
Reboot();
|
|
||||||
_usbLineInfo.lineState = setup.wValueL;
|
_usbLineInfo.lineState = setup.wValueL;
|
||||||
|
|
||||||
|
// auto-reset into the bootloader is triggered when the port, already
|
||||||
|
// open at 1200 bps, is closed. this is the signal to start the watchdog
|
||||||
|
// with a relatively long period so it can finish housekeeping tasks
|
||||||
|
// like servicing endpoints before the sketch ends
|
||||||
|
if (1200 == _usbLineInfo.dwDTERate) {
|
||||||
|
// We check DTR state to determine if host port is open (bit 0 of lineState).
|
||||||
|
if ((_usbLineInfo.lineState & 0x01) == 0) {
|
||||||
|
*(uint16_t *)0x0800 = 0x7777;
|
||||||
|
wdt_enable(WDTO_120MS);
|
||||||
|
} else {
|
||||||
|
// Most OSs do some intermediate steps when configuring ports and DTR can
|
||||||
|
// twiggle more than once before stabilizing.
|
||||||
|
// To avoid spurious resets we set the watchdog to 250ms and eventually
|
||||||
|
// cancel if DTR goes back high.
|
||||||
|
|
||||||
|
wdt_disable();
|
||||||
|
wdt_reset();
|
||||||
|
*(uint16_t *)0x0800 = 0x0;
|
||||||
|
}
|
||||||
|
}
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -110,33 +138,49 @@ void Serial_::end(void)
|
|||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
int Serial_::available(void)
|
void Serial_::accept(void)
|
||||||
{
|
{
|
||||||
u8 avail = USB_Available(CDC_RX);
|
ring_buffer *buffer = &cdc_rx_buffer;
|
||||||
if (_serialPeek != -1)
|
int c = USB_Recv(CDC_RX);
|
||||||
avail++;
|
int i = (unsigned int)(buffer->head+1) % SERIAL_BUFFER_SIZE;
|
||||||
return avail;
|
|
||||||
|
// if we should be storing the received character into the location
|
||||||
|
// just before the tail (meaning that the head would advance to the
|
||||||
|
// current location of the tail), we're about to overflow the buffer
|
||||||
|
// and so we don't write the character or advance the head.
|
||||||
|
if (i != buffer->tail) {
|
||||||
|
buffer->buffer[buffer->head] = c;
|
||||||
|
buffer->head = i;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
int Serial_::available(void)
|
||||||
|
{
|
||||||
|
ring_buffer *buffer = &cdc_rx_buffer;
|
||||||
|
return (unsigned int)(SERIAL_BUFFER_SIZE + buffer->head - buffer->tail) % SERIAL_BUFFER_SIZE;
|
||||||
}
|
}
|
||||||
|
|
||||||
// peek is nasty
|
|
||||||
int Serial_::peek(void)
|
int Serial_::peek(void)
|
||||||
{
|
{
|
||||||
if (_serialPeek == -1)
|
ring_buffer *buffer = &cdc_rx_buffer;
|
||||||
_serialPeek = read();
|
if (buffer->head == buffer->tail) {
|
||||||
return _serialPeek;
|
return -1;
|
||||||
|
} else {
|
||||||
|
return buffer->buffer[buffer->tail];
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
int Serial_::read(void)
|
int Serial_::read(void)
|
||||||
{
|
{
|
||||||
int c;
|
ring_buffer *buffer = &cdc_rx_buffer;
|
||||||
if (_serialPeek != -1)
|
// if the head isn't ahead of the tail, we don't have any characters
|
||||||
{
|
if (buffer->head == buffer->tail) {
|
||||||
c = _serialPeek;
|
return -1;
|
||||||
_serialPeek = -1;
|
|
||||||
} else {
|
} else {
|
||||||
c = USB_Recv(CDC_RX);
|
unsigned char c = buffer->buffer[buffer->tail];
|
||||||
}
|
buffer->tail = (unsigned int)(buffer->tail + 1) % SERIAL_BUFFER_SIZE;
|
||||||
return c;
|
return c;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void Serial_::flush(void)
|
void Serial_::flush(void)
|
||||||
@ -168,6 +212,21 @@ size_t Serial_::write(uint8_t c)
|
|||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// This operator is a convenient way for a sketch to check whether the
|
||||||
|
// port has actually been configured and opened by the host (as opposed
|
||||||
|
// to just being connected to the host). It can be used, for example, in
|
||||||
|
// setup() before printing to ensure that an application on the host is
|
||||||
|
// actually ready to receive and display the data.
|
||||||
|
// We add a short delay before returning to fix a bug observed by Federico
|
||||||
|
// where the port is configured (lineState != 0) but not quite opened.
|
||||||
|
Serial_::operator bool() {
|
||||||
|
bool result = false;
|
||||||
|
if (_usbLineInfo.lineState > 0)
|
||||||
|
result = true;
|
||||||
|
delay(10);
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
Serial_ Serial;
|
Serial_ Serial;
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
@ -144,7 +144,6 @@ u8 _hid_protocol = 1;
|
|||||||
u8 _hid_idle = 1;
|
u8 _hid_idle = 1;
|
||||||
|
|
||||||
#define WEAK __attribute__ ((weak))
|
#define WEAK __attribute__ ((weak))
|
||||||
#define WEAK
|
|
||||||
|
|
||||||
int WEAK HID_GetInterface(u8* interfaceNum)
|
int WEAK HID_GetInterface(u8* interfaceNum)
|
||||||
{
|
{
|
||||||
@ -202,7 +201,15 @@ bool WEAK HID_Setup(Setup& setup)
|
|||||||
//================================================================================
|
//================================================================================
|
||||||
// Mouse
|
// Mouse
|
||||||
|
|
||||||
Mouse_::Mouse_() : _buttons(0)
|
Mouse_::Mouse_(void) : _buttons(0)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
void Mouse_::begin(void)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
void Mouse_::end(void)
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -245,7 +252,7 @@ void Mouse_::release(uint8_t b)
|
|||||||
|
|
||||||
bool Mouse_::isPressed(uint8_t b)
|
bool Mouse_::isPressed(uint8_t b)
|
||||||
{
|
{
|
||||||
if (b & _buttons > 0)
|
if ((b & _buttons) > 0)
|
||||||
return true;
|
return true;
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
@ -254,7 +261,15 @@ bool Mouse_::isPressed(uint8_t b)
|
|||||||
//================================================================================
|
//================================================================================
|
||||||
// Keyboard
|
// Keyboard
|
||||||
|
|
||||||
Keyboard_::Keyboard_() : _keyMap(0)
|
Keyboard_::Keyboard_(void)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
void Keyboard_::begin(void)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
void Keyboard_::end(void)
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -263,11 +278,6 @@ void Keyboard_::sendReport(KeyReport* keys)
|
|||||||
HID_SendReport(2,keys,sizeof(KeyReport));
|
HID_SendReport(2,keys,sizeof(KeyReport));
|
||||||
}
|
}
|
||||||
|
|
||||||
void Keyboard_::setKeyMap(KeyMap* keyMap)
|
|
||||||
{
|
|
||||||
_keyMap = keyMap;
|
|
||||||
}
|
|
||||||
|
|
||||||
extern
|
extern
|
||||||
const uint8_t _asciimap[128] PROGMEM;
|
const uint8_t _asciimap[128] PROGMEM;
|
||||||
|
|
||||||
@ -406,41 +416,105 @@ const uint8_t _asciimap[128] =
|
|||||||
};
|
};
|
||||||
|
|
||||||
uint8_t USBPutChar(uint8_t c);
|
uint8_t USBPutChar(uint8_t c);
|
||||||
size_t Keyboard_::write(uint8_t c)
|
|
||||||
|
// press() adds the specified key (printing, non-printing, or modifier)
|
||||||
|
// to the persistent key report and sends the report. Because of the way
|
||||||
|
// USB HID works, the host acts like the key remains pressed until we
|
||||||
|
// call release(), releaseAll(), or otherwise clear the report and resend.
|
||||||
|
size_t Keyboard_::press(uint8_t k)
|
||||||
{
|
{
|
||||||
// Keydown
|
uint8_t i;
|
||||||
{
|
if (k >= 136) { // it's a non-printing key (not a modifier)
|
||||||
KeyReport keys = {0};
|
k = k - 136;
|
||||||
if (_keyMap)
|
} else if (k >= 128) { // it's a modifier key
|
||||||
_keyMap->charToKey(c,&keys);
|
_keyReport.modifiers |= (1<<(k-128));
|
||||||
else
|
k = 0;
|
||||||
{
|
} else { // it's a printing key
|
||||||
if (c >= 128) {
|
k = pgm_read_byte(_asciimap + k);
|
||||||
setWriteError();
|
if (!k) {
|
||||||
return 0;
|
setWriteError();
|
||||||
}
|
return 0;
|
||||||
c = pgm_read_byte(_asciimap + c);
|
}
|
||||||
if (!c) {
|
if (k & 0x80) { // it's a capital letter or other character reached with shift
|
||||||
setWriteError();
|
_keyReport.modifiers |= 0x02; // the left shift modifier
|
||||||
return 0;
|
k &= 0x7F;
|
||||||
}
|
|
||||||
if (c & 0x80)
|
|
||||||
{
|
|
||||||
keys.modifiers |= KEY_MODIFIER_LEFT_SHIFT;
|
|
||||||
c &= 0x7F;
|
|
||||||
}
|
|
||||||
keys.keys[0] = c;
|
|
||||||
}
|
}
|
||||||
sendReport(&keys);
|
|
||||||
}
|
}
|
||||||
// Keyup
|
|
||||||
{
|
// Add k to the key report only if it's not already present
|
||||||
KeyReport keys = {0};
|
// and if there is an empty slot.
|
||||||
sendReport(&keys);
|
if (_keyReport.keys[0] != k && _keyReport.keys[1] != k &&
|
||||||
|
_keyReport.keys[2] != k && _keyReport.keys[3] != k &&
|
||||||
|
_keyReport.keys[4] != k && _keyReport.keys[5] != k) {
|
||||||
|
|
||||||
|
for (i=0; i<6; i++) {
|
||||||
|
if (_keyReport.keys[i] == 0x00) {
|
||||||
|
_keyReport.keys[i] = k;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (i == 6) {
|
||||||
|
setWriteError();
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
sendReport(&_keyReport);
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// release() takes the specified key out of the persistent key report and
|
||||||
|
// sends the report. This tells the OS the key is no longer pressed and that
|
||||||
|
// it shouldn't be repeated any more.
|
||||||
|
size_t Keyboard_::release(uint8_t k)
|
||||||
|
{
|
||||||
|
uint8_t i;
|
||||||
|
if (k >= 136) { // it's a non-printing key (not a modifier)
|
||||||
|
k = k - 136;
|
||||||
|
} else if (k >= 128) { // it's a modifier key
|
||||||
|
_keyReport.modifiers &= ~(1<<(k-128));
|
||||||
|
k = 0;
|
||||||
|
} else { // it's a printing key
|
||||||
|
k = pgm_read_byte(_asciimap + k);
|
||||||
|
if (!k) {
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
if (k & 0x80) { // it's a capital letter or other character reached with shift
|
||||||
|
_keyReport.modifiers &= ~(0x02); // the left shift modifier
|
||||||
|
k &= 0x7F;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Test the key report to see if k is present. Clear it if it exists.
|
||||||
|
// Check all positions in case the key is present more than once (which it shouldn't be)
|
||||||
|
for (i=0; i<6; i++) {
|
||||||
|
if (0 != k && _keyReport.keys[i] == k) {
|
||||||
|
_keyReport.keys[i] = 0x00;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
sendReport(&_keyReport);
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
void Keyboard_::releaseAll(void)
|
||||||
|
{
|
||||||
|
_keyReport.keys[0] = 0;
|
||||||
|
_keyReport.keys[1] = 0;
|
||||||
|
_keyReport.keys[2] = 0;
|
||||||
|
_keyReport.keys[3] = 0;
|
||||||
|
_keyReport.keys[4] = 0;
|
||||||
|
_keyReport.keys[5] = 0;
|
||||||
|
_keyReport.modifiers = 0;
|
||||||
|
sendReport(&_keyReport);
|
||||||
|
}
|
||||||
|
|
||||||
|
size_t Keyboard_::write(uint8_t c)
|
||||||
|
{
|
||||||
|
uint8_t p = press(c); // Keydown
|
||||||
|
uint8_t r = release(c); // Keyup
|
||||||
|
return (p); // just return the result of press() since release() almost always returns 1
|
||||||
|
}
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#endif /* if defined(USBCON) */
|
#endif /* if defined(USBCON) */
|
@ -27,15 +27,19 @@ extern USB_ USB;
|
|||||||
|
|
||||||
class Serial_ : public Stream
|
class Serial_ : public Stream
|
||||||
{
|
{
|
||||||
|
private:
|
||||||
|
ring_buffer *_cdc_rx_buffer;
|
||||||
public:
|
public:
|
||||||
void begin(uint16_t baud_count);
|
void begin(uint16_t baud_count);
|
||||||
void end(void);
|
void end(void);
|
||||||
|
|
||||||
virtual int available(void);
|
virtual int available(void);
|
||||||
|
virtual void accept(void);
|
||||||
virtual int peek(void);
|
virtual int peek(void);
|
||||||
virtual int read(void);
|
virtual int read(void);
|
||||||
virtual void flush(void);
|
virtual void flush(void);
|
||||||
virtual void write(uint8_t);
|
virtual size_t write(uint8_t);
|
||||||
|
operator bool();
|
||||||
};
|
};
|
||||||
extern Serial_ Serial;
|
extern Serial_ Serial;
|
||||||
|
|
||||||
@ -43,10 +47,10 @@ extern Serial_ Serial;
|
|||||||
//================================================================================
|
//================================================================================
|
||||||
// Mouse
|
// Mouse
|
||||||
|
|
||||||
#define MOUSE_LEFT (1u)
|
#define MOUSE_LEFT 1
|
||||||
#define MOUSE_RIGHT (2u)
|
#define MOUSE_RIGHT 2
|
||||||
#define MOUSE_MIDDLE (4u)
|
#define MOUSE_MIDDLE 4
|
||||||
#define MOUSE_ALL (MOUSE_LEFT | MOUSE_RIGHT | MOUSE_MIDDLE)
|
#define MOUSE_ALL (MOUSE_LEFT | MOUSE_RIGHT | MOUSE_MIDDLE)
|
||||||
|
|
||||||
class Mouse_
|
class Mouse_
|
||||||
{
|
{
|
||||||
@ -54,7 +58,9 @@ private:
|
|||||||
uint8_t _buttons;
|
uint8_t _buttons;
|
||||||
void buttons(uint8_t b);
|
void buttons(uint8_t b);
|
||||||
public:
|
public:
|
||||||
Mouse_();
|
Mouse_(void);
|
||||||
|
void begin(void);
|
||||||
|
void end(void);
|
||||||
void click(uint8_t b = MOUSE_LEFT);
|
void click(uint8_t b = MOUSE_LEFT);
|
||||||
void move(signed char x, signed char y, signed char wheel = 0);
|
void move(signed char x, signed char y, signed char wheel = 0);
|
||||||
void press(uint8_t b = MOUSE_LEFT); // press LEFT by default
|
void press(uint8_t b = MOUSE_LEFT); // press LEFT by default
|
||||||
@ -67,14 +73,42 @@ extern Mouse_ Mouse;
|
|||||||
//================================================================================
|
//================================================================================
|
||||||
// Keyboard
|
// Keyboard
|
||||||
|
|
||||||
#define KEY_MODIFIER_LEFT_CTRL (0x01u)
|
#define KEY_LEFT_CTRL 0x80
|
||||||
#define KEY_MODIFIER_LEFT_SHIFT (0x02u)
|
#define KEY_LEFT_SHIFT 0x81
|
||||||
#define KEY_MODIFIER_LEFT_ALT (0x04u)
|
#define KEY_LEFT_ALT 0x82
|
||||||
#define KEY_MODIFIER_LEFT_GUI (0x08u)
|
#define KEY_LEFT_GUI 0x83
|
||||||
#define KEY_MODIFIER_RIGHT_CTRL (0x010u)
|
#define KEY_RIGHT_CTRL 0x84
|
||||||
#define KEY_MODIFIER_RIGHT_SHIFT (0x020u)
|
#define KEY_RIGHT_SHIFT 0x85
|
||||||
#define KEY_MODIFIER_RIGHT_ALT (0x040u)
|
#define KEY_RIGHT_ALT 0x86
|
||||||
#define KEY_MODIFIER_RIGHT_GUI (0x080u)
|
#define KEY_RIGHT_GUI 0x87
|
||||||
|
|
||||||
|
#define KEY_UP_ARROW 0xDA
|
||||||
|
#define KEY_DOWN_ARROW 0xD9
|
||||||
|
#define KEY_LEFT_ARROW 0xD8
|
||||||
|
#define KEY_RIGHT_ARROW 0xD7
|
||||||
|
#define KEY_BACKSPACE 0xB2
|
||||||
|
#define KEY_TAB 0xB3
|
||||||
|
#define KEY_RETURN 0xB0
|
||||||
|
#define KEY_ESC 0xB1
|
||||||
|
#define KEY_INSERT 0xD1
|
||||||
|
#define KEY_DELETE 0xD4
|
||||||
|
#define KEY_PAGE_UP 0xD3
|
||||||
|
#define KEY_PAGE_DOWN 0xD6
|
||||||
|
#define KEY_HOME 0xD2
|
||||||
|
#define KEY_END 0xD5
|
||||||
|
#define KEY_CAPS_LOCK 0xC1
|
||||||
|
#define KEY_F1 0xC2
|
||||||
|
#define KEY_F2 0xC3
|
||||||
|
#define KEY_F3 0xC4
|
||||||
|
#define KEY_F4 0xC5
|
||||||
|
#define KEY_F5 0xC6
|
||||||
|
#define KEY_F6 0xC7
|
||||||
|
#define KEY_F7 0xC8
|
||||||
|
#define KEY_F8 0xC9
|
||||||
|
#define KEY_F9 0xCA
|
||||||
|
#define KEY_F10 0xCB
|
||||||
|
#define KEY_F11 0xCC
|
||||||
|
#define KEY_F12 0xCD
|
||||||
|
|
||||||
// Low level key report: up to 6 keys and shift, ctrl etc at once
|
// Low level key report: up to 6 keys and shift, ctrl etc at once
|
||||||
typedef struct
|
typedef struct
|
||||||
@ -84,24 +118,19 @@ typedef struct
|
|||||||
uint8_t keys[6];
|
uint8_t keys[6];
|
||||||
} KeyReport;
|
} KeyReport;
|
||||||
|
|
||||||
// Map a character into a key report
|
|
||||||
// Called from Print to map text to keycodes
|
|
||||||
class KeyMap
|
|
||||||
{
|
|
||||||
public:
|
|
||||||
virtual void charToKey(int c, KeyReport* keyReport) = 0;
|
|
||||||
};
|
|
||||||
|
|
||||||
//
|
|
||||||
class Keyboard_ : public Print
|
class Keyboard_ : public Print
|
||||||
{
|
{
|
||||||
private:
|
private:
|
||||||
KeyMap* _keyMap;
|
KeyReport _keyReport;
|
||||||
void sendReport(KeyReport* keys);
|
void sendReport(KeyReport* keys);
|
||||||
void setKeyMap(KeyMap* keyMap);
|
|
||||||
public:
|
public:
|
||||||
Keyboard_();
|
Keyboard_(void);
|
||||||
virtual void write(uint8_t);
|
void begin(void);
|
||||||
|
void end(void);
|
||||||
|
virtual size_t write(uint8_t k);
|
||||||
|
virtual size_t press(uint8_t k);
|
||||||
|
virtual size_t release(uint8_t k);
|
||||||
|
virtual void releaseAll(void);
|
||||||
};
|
};
|
||||||
extern Keyboard_ Keyboard;
|
extern Keyboard_ Keyboard;
|
||||||
|
|
||||||
@ -148,6 +177,10 @@ bool CDC_Setup(Setup& setup);
|
|||||||
//================================================================================
|
//================================================================================
|
||||||
//================================================================================
|
//================================================================================
|
||||||
|
|
||||||
|
#define TRANSFER_PGM 0x80
|
||||||
|
#define TRANSFER_RELEASE 0x40
|
||||||
|
#define TRANSFER_ZERO 0x20
|
||||||
|
|
||||||
int USB_SendControl(uint8_t flags, const void* d, int len);
|
int USB_SendControl(uint8_t flags, const void* d, int len);
|
||||||
int USB_RecvControl(void* d, int len);
|
int USB_RecvControl(void* d, int len);
|
||||||
|
|
||||||
@ -157,6 +190,6 @@ int USB_Recv(uint8_t ep, void* data, int len); // non-blocking
|
|||||||
int USB_Recv(uint8_t ep); // non-blocking
|
int USB_Recv(uint8_t ep); // non-blocking
|
||||||
void USB_Flush(uint8_t ep);
|
void USB_Flush(uint8_t ep);
|
||||||
|
|
||||||
#endif /* if defined(USBCON) */
|
#endif
|
||||||
|
|
||||||
#endif /* ifndef __USBAPI__ */
|
#endif /* if defined(USBCON) */
|
672
hardware/arduino/sam/cores/sam/USB/USBCore.cpp.old
Normal file
672
hardware/arduino/sam/cores/sam/USB/USBCore.cpp.old
Normal file
@ -0,0 +1,672 @@
|
|||||||
|
|
||||||
|
|
||||||
|
/* 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) */
|
@ -248,7 +248,6 @@ typedef struct
|
|||||||
InterfaceDescriptor dif;
|
InterfaceDescriptor dif;
|
||||||
EndpointDescriptor in;
|
EndpointDescriptor in;
|
||||||
EndpointDescriptor out;
|
EndpointDescriptor out;
|
||||||
|
|
||||||
} CDCDescriptor;
|
} CDCDescriptor;
|
||||||
|
|
||||||
typedef struct
|
typedef struct
|
||||||
@ -301,4 +300,4 @@ typedef struct
|
|||||||
#define D_CDCCS4(_subtype,_d0) { 4, 0x24, _subtype, _d0 }
|
#define D_CDCCS4(_subtype,_d0) { 4, 0x24, _subtype, _d0 }
|
||||||
|
|
||||||
|
|
||||||
#endif
|
#endif
|
@ -39,9 +39,9 @@
|
|||||||
#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_IN (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_ACM (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)
|
||||||
@ -58,11 +58,6 @@
|
|||||||
#define HID_TX HID_ENDPOINT_INT
|
#define HID_TX HID_ENDPOINT_INT
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#define IMANUFACTURER (1u)
|
#define IMANUFACTURER 1
|
||||||
#define IPRODUCT (2u)
|
#define IPRODUCT 2
|
||||||
#define USB_PID_LEONARDO (0x0034u)
|
|
||||||
#define USB_PID_MICRO (0x0035u)
|
|
||||||
#define USB_PID_DUE (0x0036u)
|
|
||||||
|
|
||||||
#define USB_VID (0x2341u) // Arduino LLC vendor id
|
|
||||||
#define USB_PID USB_PID_DUE
|
|
||||||
|
Reference in New Issue
Block a user