From 4de497b72504c62cd41b9930db4a53fb2280e6c8 Mon Sep 17 00:00:00 2001 From: Cristian Maglie Date: Sat, 27 Jul 2013 12:06:42 +0200 Subject: Move buffers into USB CDC (look #947 and #1369 for reference) --- cores/robot/CDC.cpp | 41 +++++++++++------------------------------ cores/robot/USBAPI.h | 12 ++++++++++-- 2 files changed, 21 insertions(+), 32 deletions(-) (limited to 'cores/robot') diff --git a/cores/robot/CDC.cpp b/cores/robot/CDC.cpp index 701e483..fb25a96 100644 --- a/cores/robot/CDC.cpp +++ b/cores/robot/CDC.cpp @@ -23,21 +23,6 @@ #if defined(USBCON) #ifdef CDC_ENABLED -#if (RAMEND < 1000) -#define SERIAL_BUFFER_SIZE 16 -#else -#define SERIAL_BUFFER_SIZE 64 -#endif - -struct ring_buffer -{ - unsigned char buffer[SERIAL_BUFFER_SIZE]; - volatile int head; - volatile int tail; -}; - -ring_buffer cdc_rx_buffer = { { 0 }, 0, 0}; - typedef struct { u32 dwDTERate; @@ -140,8 +125,7 @@ void Serial_::end(void) void Serial_::accept(void) { - ring_buffer *buffer = &cdc_rx_buffer; - int i = (unsigned int)(buffer->head+1) % SERIAL_BUFFER_SIZE; + int i = (unsigned int)(_rx_buffer_head+1) % SERIAL_BUFFER_SIZE; // if we should be storing the received character into the location // just before the tail (meaning that the head would advance to the @@ -149,42 +133,39 @@ void Serial_::accept(void) // and so we don't write the character or advance the head. // while we have room to store a byte - while (i != buffer->tail) { + while (i != _rx_buffer_tail) { int c = USB_Recv(CDC_RX); if (c == -1) break; // no more data - buffer->buffer[buffer->head] = c; - buffer->head = i; + _rx_buffer[_rx_buffer_head] = c; + _rx_buffer_head = i; - i = (unsigned int)(buffer->head+1) % SERIAL_BUFFER_SIZE; + i = (unsigned int)(_rx_buffer_head+1) % SERIAL_BUFFER_SIZE; } } int Serial_::available(void) { - ring_buffer *buffer = &cdc_rx_buffer; - return (unsigned int)(SERIAL_BUFFER_SIZE + buffer->head - buffer->tail) % SERIAL_BUFFER_SIZE; + return (unsigned int)(SERIAL_BUFFER_SIZE + _rx_buffer_head - _rx_buffer_tail) % SERIAL_BUFFER_SIZE; } int Serial_::peek(void) { - ring_buffer *buffer = &cdc_rx_buffer; - if (buffer->head == buffer->tail) { + if (_rx_buffer_head == _rx_buffer_tail) { return -1; } else { - return buffer->buffer[buffer->tail]; + return _rx_buffer[_rx_buffer_tail]; } } int Serial_::read(void) { - ring_buffer *buffer = &cdc_rx_buffer; // if the head isn't ahead of the tail, we don't have any characters - if (buffer->head == buffer->tail) { + if (_rx_buffer_head == _rx_buffer_tail) { return -1; } else { - unsigned char c = buffer->buffer[buffer->tail]; - buffer->tail = (unsigned int)(buffer->tail + 1) % SERIAL_BUFFER_SIZE; + unsigned char c = _rx_buffer[_rx_buffer_tail]; + _rx_buffer_tail = (unsigned int)(_rx_buffer_tail + 1) % SERIAL_BUFFER_SIZE; return c; } } diff --git a/cores/robot/USBAPI.h b/cores/robot/USBAPI.h index 1a4183b..cabecf3 100644 --- a/cores/robot/USBAPI.h +++ b/cores/robot/USBAPI.h @@ -27,10 +27,14 @@ extern USBDevice_ USBDevice; struct ring_buffer; +#if (RAMEND < 1000) +#define SERIAL_BUFFER_SIZE 16 +#else +#define SERIAL_BUFFER_SIZE 64 +#endif + class Serial_ : public Stream { -private: - ring_buffer *_cdc_rx_buffer; public: void begin(uint16_t baud_count); void end(void); @@ -43,6 +47,10 @@ public: virtual size_t write(uint8_t); using Print::write; // pull in write(str) and write(buf, size) from Print operator bool(); + + volatile uint8_t _rx_buffer_head; + volatile uint8_t _rx_buffer_tail; + unsigned char _rx_buffer[SERIAL_BUFFER_SIZE]; }; extern Serial_ Serial; -- cgit v1.2.3-18-g5258