Skip to content

Commit 3b62592

Browse files
2 parents f1086b6 + 32649c8 commit 3b62592

6 files changed

Lines changed: 79 additions & 14 deletions

File tree

STM32F4/cores/maple/libmaple/HardwareSerial.cpp

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -138,10 +138,15 @@ int HardwareSerial::read(void) {
138138
}
139139
}
140140

141-
uint32 HardwareSerial::available(void) {
141+
int HardwareSerial::available(void) {
142142
return usart_data_available(usart_device);
143143
}
144144

145+
int HardwareSerial::peek(void)
146+
{
147+
return usart_peek(usart_device);
148+
}
149+
145150
uint32 HardwareSerial::pending(void) {
146151
return usart_data_pending(usart_device);
147152
}

STM32F4/cores/maple/libmaple/HardwareSerial.h

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@
3535
#include "libmaple_types.h"
3636
#include "usart.h"
3737

38-
#include "Print.h"
38+
#include "Stream.h"
3939

4040
/*
4141
* IMPORTANT:
@@ -47,7 +47,7 @@
4747
* the documentation accordingly.
4848
*/
4949

50-
class HardwareSerial : public Print {
50+
class HardwareSerial : public Stream {
5151
public:
5252
HardwareSerial(usart_dev *usart_device,
5353
uint8 tx_pin,
@@ -58,10 +58,11 @@ class HardwareSerial : public Print {
5858
void end(void);
5959

6060
/* I/O */
61-
uint32 available(void);
61+
virtual int available(void);
62+
virtual int peek(void);
63+
virtual void flush(void);
6264
uint32 pending(void);
63-
int read(void);
64-
void flush(void);
65+
virtual int read(void);
6566
virtual size_t write(unsigned char);
6667
using Print::write;
6768

STM32F4/cores/maple/libmaple/ring_buffer.h

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,25 @@ static inline uint8 rb_remove(ring_buffer *rb) {
126126
return ch;
127127
}
128128

129+
/*
130+
* Roger Clark. 20141125,
131+
* added peek function.
132+
* @brief Return the first item from a ring buffer, without removing it
133+
* @param rb Buffer to remove from, must contain at least one element.
134+
*/
135+
static inline int rb_peek(ring_buffer *rb)
136+
{
137+
if (rb->head == rb->tail)
138+
{
139+
return -1;
140+
}
141+
else
142+
{
143+
return rb->buf[rb->head];
144+
}
145+
}
146+
147+
129148
/**
130149
* @brief Attempt to remove the first item from a ring buffer.
131150
*

STM32F4/cores/maple/libmaple/usart.h

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -323,6 +323,18 @@ static inline uint8 usart_getc(usart_dev *dev) {
323323
return rb_remove(&dev->rbRX);
324324
}
325325

326+
/*
327+
* Roger Clark. 20141125,
328+
* added peek function.
329+
* @param dev Serial port to read from
330+
* @return byte read
331+
*/
332+
static inline int usart_peek(usart_dev *dev)
333+
{
334+
return rb_peek(&dev->rbRX);
335+
}
336+
337+
326338
/**
327339
* @brief Return the amount of data available in a serial port's RX buffer.
328340
* @param dev Serial port to check

STM32F4/cores/maple/usb_serial.cpp

Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -78,11 +78,11 @@ size_t USBSerial::write(const void *buf, uint32 len) {
7878
return txed;
7979
}
8080

81-
uint32 USBSerial::available(void) {
81+
int USBSerial::available(void) {
8282
return usbBytesAvailable();
8383
}
8484

85-
uint32 USBSerial::read(void *buf, uint32 len) {
85+
int USBSerial::read(void *buf, uint32 len) {
8686
if (!buf) {
8787
return 0;
8888
}
@@ -96,12 +96,38 @@ uint32 USBSerial::read(void *buf, uint32 len) {
9696
}
9797

9898
/* Blocks forever until 1 byte is received */
99-
uint8 USBSerial::read(void) {
99+
int USBSerial::read(void) {
100100
uint8 buf[1];
101101
this->read(buf, 1);
102102
return buf[0];
103103
}
104104

105+
int USBSerial::peek(void)
106+
{
107+
// ThingToDo : Don't do any thing yet, since F4 doesn't have usb_cdcacm_peek() yet.
108+
/*
109+
uint8 b;
110+
if (usb_cdcacm_peek(&b, 1)==1)
111+
{
112+
return b;
113+
}
114+
else */
115+
{
116+
return -1;
117+
}
118+
}
119+
120+
void USBSerial::flush(void)
121+
{
122+
/*Roger Clark. Rather slow method. Need to improve this */
123+
uint8 b;
124+
while(usbBytesAvailable())
125+
{
126+
this->read(&b, 1);
127+
}
128+
return;
129+
}
130+
105131
uint8 USBSerial::pending(void) {
106132
return usbGetPending();
107133
}

STM32F4/cores/maple/usb_serial.h

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -31,23 +31,25 @@
3131
#ifndef _USB_SERIAL_H_
3232
#define _USB_SERIAL_H_
3333

34-
#include "Print.h"
34+
#include "Stream.h"
3535

3636
/**
3737
* @brief Virtual serial terminal.
3838
*/
39-
class USBSerial : public Print {
39+
class USBSerial : public Stream {
4040
public:
4141
USBSerial(void);
4242

4343
void begin(void);
4444
void begin(int);
4545
void end(void);
4646

47-
uint32 available(void);
47+
virtual int available(void);
48+
virtual int peek(void);
49+
virtual void flush(void);
4850

49-
uint32 read(void *buf, uint32 len);
50-
uint8 read(void);
51+
virtual int read(void *buf, uint32 len);
52+
virtual int read(void);
5153

5254
size_t write(uint8);
5355
size_t write(const char *str);

0 commit comments

Comments
 (0)