Benjamin Hepp / BufferedSerial

Dependencies:   Buffer

Fork of BufferedSerial by Sam Grove

Revision:
14:2150f1edc9bc
Parent:
13:b4080afc8cd5
diff -r b4080afc8cd5 -r 2150f1edc9bc BufferedSerial.cpp
--- a/BufferedSerial.cpp	Sat Feb 13 17:10:39 2016 +0000
+++ b/BufferedSerial.cpp	Mon Apr 04 11:19:49 2016 +0000
@@ -29,7 +29,8 @@
     : RawSerial(tx, rx) , _rxbuf(buf_size), _txbuf(buf_size), _buffered_bytes(0)
 {
     RawSerial::attach(this, &BufferedSerial::rxIrq, Serial::RxIrq);
-    this->_buf_size = buf_size;
+    RawSerial::attach(this, &BufferedSerial::txIrq, RawSerial::TxIrq);
+    this->_buf_size = buf_size - 1;
 }
 
 BufferedSerial::BufferedSerial(PinName tx, PinName rx, int baud_rate, uint32_t buf_size)
@@ -37,7 +38,8 @@
 {
     baud(baud_rate);
     RawSerial::attach(this, &BufferedSerial::rxIrq, Serial::RxIrq);
-    this->_buf_size = buf_size;
+    RawSerial::attach(this, &BufferedSerial::txIrq, RawSerial::TxIrq);
+    this->_buf_size = buf_size - 1;
 }
 
 BufferedSerial::~BufferedSerial(void)
@@ -61,12 +63,62 @@
     return _rxbuf;
 }
 
+// TODO
+//int BufferedSerial::putc(int c)
+//{
+//    RawSerial::attach(this, &BufferedSerial::txIrq, RawSerial::TxIrq);
+//
+//    // Wait for free space in buffer
+////    __disable_irq();
+////    if (!RawSerial::writeable()) {
+////        blocking_printf("serial_writeable: %d\r\n", RawSerial::writeable());
+////        blocking_printf("putc(0x%x), buffered_bytes=%d\r\n", c, _buffered_bytes);
+////    }
+//
+//    while (_buffered_bytes >= this->_buf_size) {
+////        prime();
+////        __enable_irq();
+//        while (_buffered_bytes >= this->_buf_size) { }
+////        __disable_irq();
+//    }
+//
+//    _txbuf = (char)c;
+//    ++_buffered_bytes;
+//
+//    while (RawSerial::writeable() && _txbuf.available()) {
+//        RawSerial::putc((int)_txbuf.get());
+//        --_buffered_bytes;
+//    }
+//
+////    txIrq();
+////    RawSerial::attach(this, &BufferedSerial::txIrq, RawSerial::TxIrq);
+//
+////    while (_txbuf.available()) {
+////        while (!serial_writable(&_serial)) { }
+////        serial_putc(&_serial, (int)_txbuf.get());
+////        --_buffered_bytes;
+////    }
+//
+////    BufferedSerial::prime();
+//
+////    __enable_irq();
+//
+//    return c;
+//}
+
 int BufferedSerial::putc(int c)
 {
-    // Wait for free space in buffer
-    while (_buffered_bytes >= this->_buf_size) {}
+    while (_buffered_bytes >= this->_buf_size) {
+        BufferedSerial::prime();
+    }
+
+    __disable_irq();
+
     _txbuf = (char)c;
     ++_buffered_bytes;
+
+    __enable_irq();
+
     BufferedSerial::prime();
 
     return c;
@@ -160,8 +212,8 @@
 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) && _txbuf.available()) {
-        serial_putc(&_serial, (int)_txbuf.get());
+    while (_txbuf.available() && RawSerial::writeable()) {
+        RawSerial::putc((int)_txbuf.get());
         --_buffered_bytes;
     }
 }
@@ -169,11 +221,13 @@
 void BufferedSerial::prime(void)
 {
     // if already busy then the irq will pick this up
-    if (serial_writable(&_serial)) {
-        RawSerial::attach(NULL, RawSerial::TxIrq);    // make sure not to cause contention in the irq
+    if (RawSerial::writeable()) {
+        __disable_irq();
+        // TODO
+//        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);
+//        RawSerial::attach(this, &BufferedSerial::txIrq, RawSerial::TxIrq);
+        __enable_irq();
+
     }
 }
-
-