From 82f15f091a8f16948b39d08a8e0144ad3ac2b701 Mon Sep 17 00:00:00 2001 From: Martin Ayotte Date: Tue, 11 Aug 2015 18:50:38 -0400 Subject: [PATCH] convert USBSerial and HardwareSerial to derived from Stream instead of Print --- .../cores/maple/libmaple/HardwareSerial.cpp | 7 +++- STM32F4/cores/maple/libmaple/HardwareSerial.h | 11 ++++--- STM32F4/cores/maple/libmaple/ring_buffer.h | 19 +++++++++++ STM32F4/cores/maple/libmaple/usart.h | 12 +++++++ STM32F4/cores/maple/usb_serial.cpp | 32 +++++++++++++++++-- STM32F4/cores/maple/usb_serial.h | 12 ++++--- 6 files changed, 79 insertions(+), 14 deletions(-) diff --git a/STM32F4/cores/maple/libmaple/HardwareSerial.cpp b/STM32F4/cores/maple/libmaple/HardwareSerial.cpp index f6fdcbb..59ce71d 100644 --- a/STM32F4/cores/maple/libmaple/HardwareSerial.cpp +++ b/STM32F4/cores/maple/libmaple/HardwareSerial.cpp @@ -138,10 +138,15 @@ int HardwareSerial::read(void) { } } -uint32 HardwareSerial::available(void) { +int HardwareSerial::available(void) { return usart_data_available(usart_device); } +int HardwareSerial::peek(void) +{ + return usart_peek(usart_device); +} + uint32 HardwareSerial::pending(void) { return usart_data_pending(usart_device); } diff --git a/STM32F4/cores/maple/libmaple/HardwareSerial.h b/STM32F4/cores/maple/libmaple/HardwareSerial.h index 9cad064..c09fe54 100644 --- a/STM32F4/cores/maple/libmaple/HardwareSerial.h +++ b/STM32F4/cores/maple/libmaple/HardwareSerial.h @@ -35,7 +35,7 @@ #include "libmaple_types.h" #include "usart.h" -#include "Print.h" +#include "Stream.h" /* * IMPORTANT: @@ -47,7 +47,7 @@ * the documentation accordingly. */ -class HardwareSerial : public Print { +class HardwareSerial : public Stream { public: HardwareSerial(usart_dev *usart_device, uint8 tx_pin, @@ -58,10 +58,11 @@ public: void end(void); /* I/O */ - uint32 available(void); + virtual int available(void); + virtual int peek(void); + virtual void flush(void); uint32 pending(void); - int read(void); - void flush(void); + virtual int read(void); virtual size_t write(unsigned char); using Print::write; diff --git a/STM32F4/cores/maple/libmaple/ring_buffer.h b/STM32F4/cores/maple/libmaple/ring_buffer.h index c443bc3..b018522 100644 --- a/STM32F4/cores/maple/libmaple/ring_buffer.h +++ b/STM32F4/cores/maple/libmaple/ring_buffer.h @@ -126,6 +126,25 @@ static inline uint8 rb_remove(ring_buffer *rb) { return ch; } +/* + * Roger Clark. 20141125, + * added peek function. + * @brief Return the first item from a ring buffer, without removing it + * @param rb Buffer to remove from, must contain at least one element. + */ +static inline int rb_peek(ring_buffer *rb) +{ + if (rb->head == rb->tail) + { + return -1; + } + else + { + return rb->buf[rb->head]; + } +} + + /** * @brief Attempt to remove the first item from a ring buffer. * diff --git a/STM32F4/cores/maple/libmaple/usart.h b/STM32F4/cores/maple/libmaple/usart.h index a3dfc31..3f7c885 100644 --- a/STM32F4/cores/maple/libmaple/usart.h +++ b/STM32F4/cores/maple/libmaple/usart.h @@ -323,6 +323,18 @@ static inline uint8 usart_getc(usart_dev *dev) { return rb_remove(&dev->rbRX); } +/* + * Roger Clark. 20141125, + * added peek function. + * @param dev Serial port to read from + * @return byte read + */ +static inline int usart_peek(usart_dev *dev) +{ + return rb_peek(&dev->rbRX); +} + + /** * @brief Return the amount of data available in a serial port's RX buffer. * @param dev Serial port to check diff --git a/STM32F4/cores/maple/usb_serial.cpp b/STM32F4/cores/maple/usb_serial.cpp index 71d3b37..d606855 100644 --- a/STM32F4/cores/maple/usb_serial.cpp +++ b/STM32F4/cores/maple/usb_serial.cpp @@ -78,11 +78,11 @@ size_t USBSerial::write(const void *buf, uint32 len) { return txed; } -uint32 USBSerial::available(void) { +int USBSerial::available(void) { return usbBytesAvailable(); } -uint32 USBSerial::read(void *buf, uint32 len) { +int USBSerial::read(void *buf, uint32 len) { if (!buf) { return 0; } @@ -96,12 +96,38 @@ uint32 USBSerial::read(void *buf, uint32 len) { } /* Blocks forever until 1 byte is received */ -uint8 USBSerial::read(void) { +int USBSerial::read(void) { uint8 buf[1]; this->read(buf, 1); return buf[0]; } +int USBSerial::peek(void) +{ + // ThingToDo : Don't do any thing yet, since F4 doesn't have usb_cdcacm_peek() yet. + /* + uint8 b; + if (usb_cdcacm_peek(&b, 1)==1) + { + return b; + } + else */ + { + return -1; + } +} + +void USBSerial::flush(void) +{ + /*Roger Clark. Rather slow method. Need to improve this */ + uint8 b; + while(usbBytesAvailable()) + { + this->read(&b, 1); + } + return; +} + uint8 USBSerial::pending(void) { return usbGetPending(); } diff --git a/STM32F4/cores/maple/usb_serial.h b/STM32F4/cores/maple/usb_serial.h index 44bd1e8..a551e9d 100644 --- a/STM32F4/cores/maple/usb_serial.h +++ b/STM32F4/cores/maple/usb_serial.h @@ -31,12 +31,12 @@ #ifndef _USB_SERIAL_H_ #define _USB_SERIAL_H_ -#include "Print.h" +#include "Stream.h" /** * @brief Virtual serial terminal. */ -class USBSerial : public Print { +class USBSerial : public Stream { public: USBSerial(void); @@ -44,10 +44,12 @@ public: void begin(int); void end(void); - uint32 available(void); + virtual int available(void); + virtual int peek(void); + virtual void flush(void); - uint32 read(void *buf, uint32 len); - uint8 read(void); + virtual int read(void *buf, uint32 len); + virtual int read(void); size_t write(uint8); size_t write(const char *str);