Skip to content

Commit 8c1ce45

Browse files
committed
Move buffers into USB CDC (look arduino#947 and arduino#1369 for reference)
1 parent 4055ac1 commit 8c1ce45

File tree

4 files changed

+42
-64
lines changed

4 files changed

+42
-64
lines changed

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

Lines changed: 11 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -23,21 +23,6 @@
2323
#if defined(USBCON)
2424
#ifdef CDC_ENABLED
2525

26-
#if (RAMEND < 1000)
27-
#define SERIAL_BUFFER_SIZE 16
28-
#else
29-
#define SERIAL_BUFFER_SIZE 64
30-
#endif
31-
32-
struct ring_buffer
33-
{
34-
unsigned char buffer[SERIAL_BUFFER_SIZE];
35-
volatile int head;
36-
volatile int tail;
37-
};
38-
39-
ring_buffer cdc_rx_buffer = { { 0 }, 0, 0};
40-
4126
typedef struct
4227
{
4328
u32 dwDTERate;
@@ -140,51 +125,47 @@ void Serial_::end(void)
140125

141126
void Serial_::accept(void)
142127
{
143-
ring_buffer *buffer = &cdc_rx_buffer;
144-
int i = (unsigned int)(buffer->head+1) % SERIAL_BUFFER_SIZE;
128+
int i = (unsigned int)(_rx_buffer_head+1) % SERIAL_BUFFER_SIZE;
145129

146130
// if we should be storing the received character into the location
147131
// just before the tail (meaning that the head would advance to the
148132
// current location of the tail), we're about to overflow the buffer
149133
// and so we don't write the character or advance the head.
150134

151135
// while we have room to store a byte
152-
while (i != buffer->tail) {
136+
while (i != _rx_buffer_tail) {
153137
int c = USB_Recv(CDC_RX);
154138
if (c == -1)
155139
break; // no more data
156-
buffer->buffer[buffer->head] = c;
157-
buffer->head = i;
140+
_rx_buffer[_rx_buffer_head] = c;
141+
_rx_buffer_head = i;
158142

159-
i = (unsigned int)(buffer->head+1) % SERIAL_BUFFER_SIZE;
143+
i = (unsigned int)(_rx_buffer_head+1) % SERIAL_BUFFER_SIZE;
160144
}
161145
}
162146

163147
int Serial_::available(void)
164148
{
165-
ring_buffer *buffer = &cdc_rx_buffer;
166-
return (unsigned int)(SERIAL_BUFFER_SIZE + buffer->head - buffer->tail) % SERIAL_BUFFER_SIZE;
149+
return (unsigned int)(SERIAL_BUFFER_SIZE + _rx_buffer_head - _rx_buffer_tail) % SERIAL_BUFFER_SIZE;
167150
}
168151

169152
int Serial_::peek(void)
170153
{
171-
ring_buffer *buffer = &cdc_rx_buffer;
172-
if (buffer->head == buffer->tail) {
154+
if (_rx_buffer_head == _rx_buffer_tail) {
173155
return -1;
174156
} else {
175-
return buffer->buffer[buffer->tail];
157+
return _rx_buffer[_rx_buffer_tail];
176158
}
177159
}
178160

179161
int Serial_::read(void)
180162
{
181-
ring_buffer *buffer = &cdc_rx_buffer;
182163
// if the head isn't ahead of the tail, we don't have any characters
183-
if (buffer->head == buffer->tail) {
164+
if (_rx_buffer_head == _rx_buffer_tail) {
184165
return -1;
185166
} else {
186-
unsigned char c = buffer->buffer[buffer->tail];
187-
buffer->tail = (unsigned int)(buffer->tail + 1) % SERIAL_BUFFER_SIZE;
167+
unsigned char c = _rx_buffer[_rx_buffer_tail];
168+
_rx_buffer_tail = (unsigned int)(_rx_buffer_tail + 1) % SERIAL_BUFFER_SIZE;
188169
return c;
189170
}
190171
}

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

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,14 @@ extern USBDevice_ USBDevice;
2727

2828
struct ring_buffer;
2929

30+
#if (RAMEND < 1000)
31+
#define SERIAL_BUFFER_SIZE 16
32+
#else
33+
#define SERIAL_BUFFER_SIZE 64
34+
#endif
35+
3036
class Serial_ : public Stream
3137
{
32-
private:
33-
ring_buffer *_cdc_rx_buffer;
3438
public:
3539
void begin(uint16_t baud_count);
3640
void end(void);
@@ -43,6 +47,10 @@ class Serial_ : public Stream
4347
virtual size_t write(uint8_t);
4448
using Print::write; // pull in write(str) and write(buf, size) from Print
4549
operator bool();
50+
51+
volatile uint8_t _rx_buffer_head;
52+
volatile uint8_t _rx_buffer_tail;
53+
unsigned char _rx_buffer[SERIAL_BUFFER_SIZE];
4654
};
4755
extern Serial_ Serial;
4856

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

Lines changed: 11 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -23,21 +23,6 @@
2323
#if defined(USBCON)
2424
#ifdef CDC_ENABLED
2525

26-
#if (RAMEND < 1000)
27-
#define SERIAL_BUFFER_SIZE 16
28-
#else
29-
#define SERIAL_BUFFER_SIZE 64
30-
#endif
31-
32-
struct ring_buffer
33-
{
34-
unsigned char buffer[SERIAL_BUFFER_SIZE];
35-
volatile int head;
36-
volatile int tail;
37-
};
38-
39-
ring_buffer cdc_rx_buffer = { { 0 }, 0, 0};
40-
4126
typedef struct
4227
{
4328
u32 dwDTERate;
@@ -140,51 +125,47 @@ void Serial_::end(void)
140125

141126
void Serial_::accept(void)
142127
{
143-
ring_buffer *buffer = &cdc_rx_buffer;
144-
int i = (unsigned int)(buffer->head+1) % SERIAL_BUFFER_SIZE;
128+
int i = (unsigned int)(_rx_buffer_head+1) % SERIAL_BUFFER_SIZE;
145129

146130
// if we should be storing the received character into the location
147131
// just before the tail (meaning that the head would advance to the
148132
// current location of the tail), we're about to overflow the buffer
149133
// and so we don't write the character or advance the head.
150134

151135
// while we have room to store a byte
152-
while (i != buffer->tail) {
136+
while (i != _rx_buffer_tail) {
153137
int c = USB_Recv(CDC_RX);
154138
if (c == -1)
155139
break; // no more data
156-
buffer->buffer[buffer->head] = c;
157-
buffer->head = i;
140+
_rx_buffer[_rx_buffer_head] = c;
141+
_rx_buffer_head = i;
158142

159-
i = (unsigned int)(buffer->head+1) % SERIAL_BUFFER_SIZE;
143+
i = (unsigned int)(_rx_buffer_head+1) % SERIAL_BUFFER_SIZE;
160144
}
161145
}
162146

163147
int Serial_::available(void)
164148
{
165-
ring_buffer *buffer = &cdc_rx_buffer;
166-
return (unsigned int)(SERIAL_BUFFER_SIZE + buffer->head - buffer->tail) % SERIAL_BUFFER_SIZE;
149+
return (unsigned int)(SERIAL_BUFFER_SIZE + _rx_buffer_head - _rx_buffer_tail) % SERIAL_BUFFER_SIZE;
167150
}
168151

169152
int Serial_::peek(void)
170153
{
171-
ring_buffer *buffer = &cdc_rx_buffer;
172-
if (buffer->head == buffer->tail) {
154+
if (_rx_buffer_head == _rx_buffer_tail) {
173155
return -1;
174156
} else {
175-
return buffer->buffer[buffer->tail];
157+
return _rx_buffer[_rx_buffer_tail];
176158
}
177159
}
178160

179161
int Serial_::read(void)
180162
{
181-
ring_buffer *buffer = &cdc_rx_buffer;
182163
// if the head isn't ahead of the tail, we don't have any characters
183-
if (buffer->head == buffer->tail) {
164+
if (_rx_buffer_head == _rx_buffer_tail) {
184165
return -1;
185166
} else {
186-
unsigned char c = buffer->buffer[buffer->tail];
187-
buffer->tail = (unsigned int)(buffer->tail + 1) % SERIAL_BUFFER_SIZE;
167+
unsigned char c = _rx_buffer[_rx_buffer_tail];
168+
_rx_buffer_tail = (unsigned int)(_rx_buffer_tail + 1) % SERIAL_BUFFER_SIZE;
188169
return c;
189170
}
190171
}

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

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,14 @@ extern USBDevice_ USBDevice;
2727

2828
struct ring_buffer;
2929

30+
#if (RAMEND < 1000)
31+
#define SERIAL_BUFFER_SIZE 16
32+
#else
33+
#define SERIAL_BUFFER_SIZE 64
34+
#endif
35+
3036
class Serial_ : public Stream
3137
{
32-
private:
33-
ring_buffer *_cdc_rx_buffer;
3438
public:
3539
void begin(uint16_t baud_count);
3640
void end(void);
@@ -43,6 +47,10 @@ class Serial_ : public Stream
4347
virtual size_t write(uint8_t);
4448
using Print::write; // pull in write(str) and write(buf, size) from Print
4549
operator bool();
50+
51+
volatile uint8_t _rx_buffer_head;
52+
volatile uint8_t _rx_buffer_tail;
53+
unsigned char _rx_buffer[SERIAL_BUFFER_SIZE];
4654
};
4755
extern Serial_ Serial;
4856

0 commit comments

Comments
 (0)