Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Fork of BufferedSerial by
Diff: BufferedSerial.cpp
- Revision:
- 12:c7947d444267
- Parent:
- 11:779304f9c5d2
- Child:
- 13:b4080afc8cd5
--- a/BufferedSerial.cpp Sun Jul 26 21:53:11 2015 +0000
+++ b/BufferedSerial.cpp Sat Feb 13 14:14:50 2016 +0000
@@ -1,4 +1,6 @@
/**
+ * Adapted by Benjamin Hepp (benjamin.hepp@inf.ethz.ch) to include blocking when write-buffer is full.
+ *
* @file BufferedSerial.cpp
* @brief Software Buffer - Extends mbed Serial functionallity adding irq driven TX and RX
* @author sam grove
@@ -23,21 +25,25 @@
#include "BufferedSerial.h"
#include <stdarg.h>
-BufferedSerial::BufferedSerial(PinName tx, PinName rx, uint32_t buf_size, uint32_t tx_multiple, const char* name)
- : RawSerial(tx, rx) , _rxbuf(buf_size), _txbuf((uint32_t)(tx_multiple*buf_size))
+BufferedSerial::BufferedSerial(PinName tx, PinName rx, uint32_t buf_size)
+ : RawSerial(tx, rx) , _rxbuf(buf_size), _txbuf(buf_size), _buffered_bytes(0)
{
RawSerial::attach(this, &BufferedSerial::rxIrq, Serial::RxIrq);
this->_buf_size = buf_size;
- this->_tx_multiple = tx_multiple;
- return;
+}
+
+BufferedSerial::BufferedSerial(PinName tx, PinName rx, int baud_rate, uint32_t buf_size)
+ : RawSerial(tx, rx) , _rxbuf(buf_size), _txbuf(buf_size), _buffered_bytes(0)
+{
+ baud(baud_rate);
+ RawSerial::attach(this, &BufferedSerial::rxIrq, Serial::RxIrq);
+ this->_buf_size = buf_size;
}
BufferedSerial::~BufferedSerial(void)
{
RawSerial::attach(NULL, RawSerial::RxIrq);
RawSerial::attach(NULL, RawSerial::TxIrq);
-
- return;
}
int BufferedSerial::readable(void)
@@ -47,7 +53,7 @@
int BufferedSerial::writeable(void)
{
- return 1; // buffer allows overwriting by design, always true
+ _buffered_bytes < this->_buf_size;
}
int BufferedSerial::getc(void)
@@ -57,7 +63,10 @@
int BufferedSerial::putc(int c)
{
+ // Wait for free space in buffer
+ while (_buffered_bytes >= this->_buf_size) {}
_txbuf = (char)c;
+ ++_buffered_bytes;
BufferedSerial::prime();
return c;
@@ -67,13 +76,16 @@
{
if (s != NULL) {
const char* ptr = s;
-
- while(*(ptr) != 0) {
+
+ while (*(ptr) != 0) {
+ // Wait for free space in buffer
+ while (_buffered_bytes >= this->_buf_size) {}
_txbuf = *(ptr++);
+ ++_buffered_bytes;
}
_txbuf = '\n'; // done per puts definition
BufferedSerial::prime();
-
+
return (ptr - s) + 1;
}
return 0;
@@ -82,15 +94,15 @@
int BufferedSerial::printf(const char* format, ...)
{
char buffer[this->_buf_size];
- memset(buffer,0,this->_buf_size);
+ memset(buffer, 0, this->_buf_size);
int r = 0;
va_list arg;
va_start(arg, format);
- r = vsprintf(buffer, format, arg);
+ r = vsnprintf(buffer, this->_buf_size, format, arg);
// this may not hit the heap but should alert the user anyways
if(r > this->_buf_size) {
- error("%s %d buffer overwrite (max_buf_size: %d exceeded: %d)!\r\n", __FILE__, __LINE__,this->_buf_size,r);
+ error("%s %d buffer to small (buf_size: %d, required: %d)!\r\n", __FILE__, __LINE__, this->_buf_size, r);
va_end(arg);
return 0;
}
@@ -105,12 +117,15 @@
if (s != NULL && length > 0) {
const char* ptr = (const char*)s;
const char* end = ptr + length;
-
+
while (ptr != end) {
+ // Wait for free space in buffer
+ while (_buffered_bytes >= this->_buf_size) {}
_txbuf = *(ptr++);
+ ++_buffered_bytes;
}
BufferedSerial::prime();
-
+
return ptr - (const char*)s;
}
return 0;
@@ -120,39 +135,30 @@
void BufferedSerial::rxIrq(void)
{
// read from the peripheral and make sure something is available
- if(serial_readable(&_serial)) {
+ if (serial_readable(&_serial)) {
_rxbuf = serial_getc(&_serial); // if so load them into a buffer
}
-
- return;
}
void BufferedSerial::txIrq(void)
{
// see if there is room in the hardware fifo and if something is in the software fifo
- while(serial_writable(&_serial)) {
- if(_txbuf.available()) {
+ while (serial_writable(&_serial)) {
+ if (_txbuf.available()) {
serial_putc(&_serial, (int)_txbuf.get());
- } else {
- // disable the TX interrupt when there is nothing left to send
- RawSerial::attach(NULL, RawSerial::TxIrq);
- break;
+ --_buffered_bytes;
}
}
-
- return;
}
void BufferedSerial::prime(void)
{
// if already busy then the irq will pick this up
- if(serial_writable(&_serial)) {
+ if (serial_writable(&_serial)) {
RawSerial::attach(NULL, RawSerial::TxIrq); // make sure not to cause contention in the irq
BufferedSerial::txIrq(); // only write to hardware in one place
RawSerial::attach(this, &BufferedSerial::txIrq, RawSerial::TxIrq);
}
-
- return;
}
