Skip to content

Commit 9b954df

Browse files
committed
committed USB API, initial HardwareSerial-USBSerial integration
1 parent 87898da commit 9b954df

File tree

3 files changed

+183
-1
lines changed

3 files changed

+183
-1
lines changed

Diff for: hardware/arduino/cores/arduino/HardwareSerial.cpp

+26-1
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,10 @@ struct ring_buffer
5050
volatile int tail;
5151
};
5252

53+
#if defined(USBCON)
54+
ring_buffer rx_buffer = { { 0 }, 0, 0};
55+
ring_buffer tx_buffer = { { 0 }, 0, 0};
56+
#endif
5357
#if defined(UBRRH) || defined(UBRR0H)
5458
ring_buffer rx_buffer = { { 0 }, 0, 0 };
5559
ring_buffer tx_buffer = { { 0 }, 0, 0 };
@@ -81,6 +85,15 @@ inline void store_char(unsigned char c, ring_buffer *buffer)
8185
}
8286
}
8387

88+
#if defined(__AVR_ATmega32U4__)
89+
void serialEvent() __attribute__((weak));
90+
void serialEvent() {}
91+
SIGNAL(USART1_RX_vect) {
92+
unsigned char c = UDR1;
93+
store_char(c, &rx_buffer);
94+
serialEvent();
95+
}
96+
#else
8497
#if !defined(USART_RX_vect) && !defined(SIG_USART0_RECV) && \
8598
!defined(SIG_UART0_RECV) && !defined(USART0_RX_vect) && \
8699
!defined(SIG_UART_RECV)
@@ -150,8 +163,19 @@ inline void store_char(unsigned char c, ring_buffer *buffer)
150163
#elif defined(SIG_USART3_RECV)
151164
#error SIG_USART3_RECV
152165
#endif
166+
#endif
153167

154-
168+
#if defined(__AVR_ATmega32U4__)
169+
ISR(USART1_UDRE_vect) {
170+
if (tx_buffer.head == tx_buffer.tail) {
171+
cbi(UCSR1B, UDRIE1);
172+
} else {
173+
unsigned char c = tx_buffer.buffer[tx_buffer.tail];
174+
tx_buffer.tail = (tx_buffer.tail + 1) % SERIAL_BUFFER_SIZE;
175+
UDR1 = c;
176+
}
177+
}
178+
#else
155179
#if !defined(UART0_UDRE_vect) && !defined(UART_UDRE_vect) && !defined(USART0_UDRE_vect) && !defined(USART_UDRE_vect)
156180
#error Don't know what the Data Register Empty vector is called for the first UART
157181
#else
@@ -205,6 +229,7 @@ ISR(USART1_UDRE_vect)
205229
}
206230
}
207231
#endif
232+
#endif
208233

209234
#ifdef USART2_UDRE_vect
210235
ISR(USART2_UDRE_vect)

Diff for: hardware/arduino/cores/arduino/HardwareSerial.h

+1
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ class HardwareSerial : public Stream
6363
extern HardwareSerial Serial;
6464
#elif defined(USBCON)
6565
#include "usb_api.h"
66+
extern HardwareSerial Serial_;
6667
#endif
6768
#if defined(UBRR1H)
6869
extern HardwareSerial Serial1;

Diff for: hardware/arduino/cores/arduino/usb_api.h

+156
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,156 @@
1+
2+
3+
#ifndef __USBAPI__
4+
#define __USBAPI__
5+
6+
//================================================================================
7+
//================================================================================
8+
// USB
9+
10+
class USB_
11+
{
12+
public:
13+
USB_();
14+
bool configured();
15+
16+
void attach();
17+
void detach(); // Serial port goes down too...
18+
void poll();
19+
};
20+
extern USB_ USB;
21+
22+
//================================================================================
23+
//================================================================================
24+
// Serial over CDC (Serial1 is the physical port)
25+
26+
class Serial_ : public Stream
27+
{
28+
public:
29+
void begin(uint16_t baud_count);
30+
void end(void);
31+
32+
virtual int available(void);
33+
virtual int peek(void);
34+
virtual int read(void);
35+
virtual void flush(void);
36+
virtual void write(uint8_t);
37+
};
38+
extern Serial_ Serial;
39+
40+
//================================================================================
41+
//================================================================================
42+
// Mouse
43+
44+
#define MOUSE_LEFT 1
45+
#define MOUSE_MIDDLE 2
46+
#define MOUSE_RIGHT 4
47+
48+
class Mouse_
49+
{
50+
uint8_t _buttons;
51+
public:
52+
Mouse_();
53+
void click(uint8_t b = MOUSE_LEFT);
54+
void move(signed char x, signed char y, signed char wheel = 0);
55+
void buttons(uint8_t b);
56+
};
57+
extern Mouse_ Mouse;
58+
59+
//================================================================================
60+
//================================================================================
61+
// Keyboard
62+
63+
#define KEY_MODIFIER_LEFT_CTRL 0x01
64+
#define KEY_MODIFIER_LEFT_SHIFT 0x02
65+
#define KEY_MODIFIER_LEFT_ALT 0x04
66+
#define KEY_MODIFIER_LEFT_GUI 0x08
67+
#define KEY_MODIFIER_RIGHT_CTRL 0x010
68+
#define KEY_MODIFIER_RIGHT_SHIFT 0x020
69+
#define KEY_MODIFIER_RIGHT_ALT 0x040
70+
#define KEY_MODIFIER_RIGHT_GUI 0x080
71+
72+
// Low level key report: up to 6 keys and shift, ctrl etc at once
73+
typedef struct
74+
{
75+
uint8_t modifiers;
76+
uint8_t reserved;
77+
uint8_t keys[6];
78+
} KeyReport;
79+
80+
// Map a character into a key report
81+
// Called from Print to map text to keycodes
82+
class KeyMap
83+
{
84+
public:
85+
virtual void charToKey(int c, KeyReport* keyReport) = 0;
86+
};
87+
88+
//
89+
class Keyboard_ : public Print
90+
{
91+
KeyMap* _keyMap;
92+
public:
93+
Keyboard_();
94+
void sendReport(KeyReport* keys);
95+
void setKeyMap(KeyMap* keyMap);
96+
virtual void write(uint8_t);
97+
};
98+
extern Keyboard_ Keyboard;
99+
100+
//================================================================================
101+
//================================================================================
102+
// Low level API
103+
104+
typedef struct
105+
{
106+
uint8_t bmRequestType;
107+
uint8_t bRequest;
108+
uint8_t wValueL;
109+
uint8_t wValueH;
110+
uint16_t wIndex;
111+
uint16_t wLength;
112+
} Setup;
113+
114+
//================================================================================
115+
//================================================================================
116+
// HID 'Driver'
117+
118+
int HID_GetInterface(uint8_t* interfaceNum);
119+
int HID_GetDescriptor(int i);
120+
bool HID_Setup(Setup& setup);
121+
void HID_SendReport(uint8_t id, const void* data, int len);
122+
123+
//================================================================================
124+
//================================================================================
125+
// MSC 'Driver'
126+
127+
int MSC_GetInterface(uint8_t* interfaceNum);
128+
int MSC_GetDescriptor(int i);
129+
bool MSC_Setup(Setup& setup);
130+
bool MSC_Data(uint8_t rx,uint8_t tx);
131+
132+
//================================================================================
133+
//================================================================================
134+
// CSC 'Driver'
135+
136+
int CDC_GetInterface(uint8_t* interfaceNum);
137+
int CDC_GetDescriptor(int i);
138+
bool CDC_Setup(Setup& setup);
139+
140+
//================================================================================
141+
//================================================================================
142+
143+
#define TRANSFER_PGM 0x80
144+
#define TRANSFER_RELEASE 0x40
145+
#define TRANSFER_ZERO 0x20
146+
147+
int USB_SendControl(uint8_t flags, const void* d, int len);
148+
int USB_RecvControl(void* d, int len);
149+
150+
uint8_t USB_Available(uint8_t ep);
151+
int USB_Send(uint8_t ep, const void* data, int len); // blocking
152+
int USB_Recv(uint8_t ep, void* data, int len); // non-blocking
153+
int USB_Recv(uint8_t ep); // non-blocking
154+
void USB_Flush(uint8_t ep);
155+
156+
#endif

0 commit comments

Comments
 (0)