Skip to content

Commit 36331fa

Browse files
committed
Merge branch 'usbcdc-improved' into ide-1.5.x
2 parents cc2a9a6 + ddbb6b3 commit 36331fa

File tree

3 files changed

+25
-40
lines changed

3 files changed

+25
-40
lines changed

hardware/arduino/avr/cores/arduino/CDC.cpp

Lines changed: 18 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,6 @@ bool WEAK CDC_Setup(Setup& setup)
114114
}
115115

116116

117-
int _serialPeek = -1;
118117
void Serial_::begin(unsigned long /* baud_count */)
119118
{
120119
}
@@ -127,51 +126,29 @@ void Serial_::end(void)
127126
{
128127
}
129128

130-
void Serial_::accept(void)
131-
{
132-
int i = (unsigned int)(_rx_buffer_head+1) % SERIAL_BUFFER_SIZE;
133-
134-
// if we should be storing the received character into the location
135-
// just before the tail (meaning that the head would advance to the
136-
// current location of the tail), we're about to overflow the buffer
137-
// and so we don't write the character or advance the head.
138-
139-
// while we have room to store a byte
140-
while (i != _rx_buffer_tail) {
141-
int c = USB_Recv(CDC_RX);
142-
if (c == -1)
143-
break; // no more data
144-
_rx_buffer[_rx_buffer_head] = c;
145-
_rx_buffer_head = i;
146-
147-
i = (unsigned int)(_rx_buffer_head+1) % SERIAL_BUFFER_SIZE;
148-
}
149-
}
150-
151129
int Serial_::available(void)
152130
{
153-
return (unsigned int)(SERIAL_BUFFER_SIZE + _rx_buffer_head - _rx_buffer_tail) % SERIAL_BUFFER_SIZE;
131+
if (peek_buffer >= 0) {
132+
return 1;
133+
}
134+
return USB_Available(CDC_RX);
154135
}
155136

156137
int Serial_::peek(void)
157138
{
158-
if (_rx_buffer_head == _rx_buffer_tail) {
159-
return -1;
160-
} else {
161-
return _rx_buffer[_rx_buffer_tail];
162-
}
139+
if (peek_buffer < 0)
140+
peek_buffer = USB_Recv(CDC_RX);
141+
return peek_buffer;
163142
}
164143

165144
int Serial_::read(void)
166145
{
167-
// if the head isn't ahead of the tail, we don't have any characters
168-
if (_rx_buffer_head == _rx_buffer_tail) {
169-
return -1;
170-
} else {
171-
unsigned char c = _rx_buffer[_rx_buffer_tail];
172-
_rx_buffer_tail = (unsigned int)(_rx_buffer_tail + 1) % SERIAL_BUFFER_SIZE;
146+
if (peek_buffer >= 0) {
147+
int c = peek_buffer;
148+
peek_buffer = -1;
173149
return c;
174-
}
150+
}
151+
return USB_Recv(CDC_RX);
175152
}
176153

177154
void Serial_::flush(void)
@@ -180,6 +157,11 @@ void Serial_::flush(void)
180157
}
181158

182159
size_t Serial_::write(uint8_t c)
160+
{
161+
return write(&c, 1);
162+
}
163+
164+
size_t Serial_::write(const uint8_t *buffer, size_t size)
183165
{
184166
/* only try to send bytes if the high-level CDC connection itself
185167
is open (not just the pipe) - the OS should set lineState when the port
@@ -191,7 +173,7 @@ size_t Serial_::write(uint8_t c)
191173
// open connection isn't broken cleanly (cable is yanked out, host dies
192174
// or locks up, or host virtual serial port hangs)
193175
if (_usbLineInfo.lineState > 0) {
194-
int r = USB_Send(CDC_TX,&c,1);
176+
int r = USB_Send(CDC_TX,buffer,size);
195177
if (r > 0) {
196178
return r;
197179
} else {

hardware/arduino/avr/cores/arduino/USBAPI.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,17 +35,19 @@ struct ring_buffer;
3535

3636
class Serial_ : public Stream
3737
{
38+
private:
39+
int peek_buffer;
3840
public:
3941
void begin(unsigned long);
4042
void begin(unsigned long, uint8_t);
4143
void end(void);
4244

4345
virtual int available(void);
44-
virtual void accept(void);
4546
virtual int peek(void);
4647
virtual int read(void);
4748
virtual void flush(void);
4849
virtual size_t write(uint8_t);
50+
virtual size_t write(const uint8_t*, size_t);
4951
using Print::write; // pull in write(str) and write(buf, size) from Print
5052
operator bool();
5153

hardware/arduino/avr/cores/arduino/USBCore.cpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -289,9 +289,12 @@ int USB_Send(u8 ep, const void* d, int len)
289289

290290
if (n > len)
291291
n = len;
292-
len -= n;
293292
{
294293
LockEP lock(ep);
294+
// Frame may have been released by the SOF interrupt handler
295+
if (!ReadWriteAllowed())
296+
continue;
297+
len -= n;
295298
if (ep & TRANSFER_ZERO)
296299
{
297300
while (n--)
@@ -627,8 +630,6 @@ ISR(USB_GEN_vect)
627630
{
628631
#ifdef CDC_ENABLED
629632
USB_Flush(CDC_TX); // Send a tx frame if found
630-
if (USB_Available(CDC_RX)) // Handle received bytes (if any)
631-
Serial.accept();
632633
#endif
633634

634635
// check whether the one-shot period has elapsed. if so, turn off the LED

0 commit comments

Comments
 (0)