Inherit from Serial and use software buffers for TX and RX. This allows the UART peripherals to operate in a IRQ driven mode. Overrides most (but not all) stdio functions as Serial did

Dependencies:   Buffer

Dependents:   buffered_serial_test BLE_Police_HRM evena_BLE_Police_HRM df-2014-workshop-rfid-case-generator-k64f ... more

Example

 #include "mbed.h"
 #include "BufferedSerial.h"

 BufferedSerial pc(USBTX, USBRX);

 int main()
 {
     pc.baud(115200);
   
     while(1)
     {
         Timer s;
       
         s.start();
         pc.printf("Hello World - buff\n");
         int buffered_time = s.read_us();
         wait(0.1f); // give time for the buffer to empty
       
         s.reset();
         printf("Hello World - poll\n");
         int polled_time = s.read_us();
         s.stop();
         wait(0.1f); // give time for the buffer to empty
       
         pc.printf("printf buffered took %d us\n", buffered_time);
         pc.printf("printf polled took %d us\n", polled_time);
         wait(0.5f);
     }
 }

Files at this revision

API Documentation at this revision

Comitter:
ansond
Date:
Tue Sep 09 20:20:38 2014 +0000
Parent:
5:4d6a311fc8bf
Child:
7:6fa214b41d73
Commit message:
added #define to change the base Serial class to RawSerial if desired

Changed in this revision

BufferedSerial.cpp Show annotated file Show diff for this revision Revisions of this file
BufferedSerial.h Show annotated file Show diff for this revision Revisions of this file
--- a/BufferedSerial.cpp	Mon Mar 24 19:56:57 2014 +0000
+++ b/BufferedSerial.cpp	Tue Sep 09 20:20:38 2014 +0000
@@ -24,17 +24,17 @@
 #include <stdarg.h>
 
 BufferedSerial::BufferedSerial(PinName tx, PinName rx, const char* name)
-    : Serial(tx, rx, name)
+    : SERIAL_BASE(tx, rx)
 {
-    Serial::attach(this, &BufferedSerial::rxIrq, Serial::RxIrq);
+    SERIAL_BASE::attach(this, &BufferedSerial::rxIrq, Serial::RxIrq);
 
     return;
 }
 
 BufferedSerial::~BufferedSerial(void)
 {
-    Serial::attach(NULL, Serial::RxIrq);
-    Serial::attach(NULL, Serial::TxIrq);
+    SERIAL_BASE::attach(NULL, SERIAL_BASE::RxIrq);
+    SERIAL_BASE::attach(NULL, SERIAL_BASE::TxIrq);
 
     return;
 }
@@ -125,7 +125,7 @@
             serial_putc(&_serial, (int)_txbuf.get());
         } else {
             // disable the TX interrupt when there is nothing left to send
-            Serial::attach(NULL, Serial::TxIrq);
+            SERIAL_BASE::attach(NULL, SERIAL_BASE::TxIrq);
             break;
         }
     }
@@ -137,9 +137,9 @@
 {
     // if already busy then the irq will pick this up
     if(serial_writable(&_serial)) {
-        Serial::attach(NULL, Serial::TxIrq);    // make sure not to cause contention in the irq
+        SERIAL_BASE::attach(NULL, SERIAL_BASE::TxIrq);    // make sure not to cause contention in the irq
         BufferedSerial::txIrq();                // only write to hardware in one place
-        Serial::attach(this, &BufferedSerial::txIrq, Serial::TxIrq);
+        SERIAL_BASE::attach(this, &BufferedSerial::txIrq, SERIAL_BASE::TxIrq);
     }
 
     return;
--- a/BufferedSerial.h	Mon Mar 24 19:56:57 2014 +0000
+++ b/BufferedSerial.h	Tue Sep 09 20:20:38 2014 +0000
@@ -27,6 +27,8 @@
 #include "mbed.h"
 #include "Buffer.h"
 
+#define SERIAL_BASE  RawSerial
+
 /** A serial port (UART) for communication with other serial devices
  *
  * Can be used for Full Duplex communication, or Simplex by specifying
@@ -68,7 +70,7 @@
  *  @class BufferedSerial
  *  @brief Software buffers and interrupt driven tx and rx for Serial
  */  
-class BufferedSerial : public Serial 
+class BufferedSerial : public SERIAL_BASE 
 {
 private:
     Buffer <char> _rxbuf;