Buffered Serial Port Driver for RTOS

Dependents:   nucleo_cannonball PiballNeoController

Buffered Serial Port Driver for RTOS

  • ISR driven, ring buffered IO operation
  • IO operations are idle waiting, don't waste time in RTOS :D
  • Can use external buffers
  • Based on mbed RawSerial

Example

SerialDriver Example

#include "SerialDriver.h"

SerialDriver pc(USBTX, USBRX);

int main()
{
    // setup serial port
    pc.baud(9600);
    
    // print some text
    pc.puts("This is just a string.\r\n");
    pc.printf("But this is a %s with integer %i and float %f.\r\n", "formatted text", 123, 0.456f);
    
    // now lets behave like a null modem 
    while(1)
       pc.putc(pc.getc());
}

Look at the API Documentation for more Examples.

Dependencies

Import librarymbed

The official Mbed 2 C/C++ SDK provides the software platform and libraries to build your applications.

Import librarymbed-rtos

Official mbed Real Time Operating System based on the RTX implementation of the CMSIS-RTOS API open standard.

If you find a bug, please help me to fix it. Send me a message. You can help me a lot: Write a demo program that causes the bug reproducible.

Files at this revision

API Documentation at this revision

Comitter:
BlazeX
Date:
Mon Jan 26 19:21:56 2015 +0000
Parent:
2:012c0c2fac52
Child:
4:a41e47716932
Commit message:
Added dropped bytes counters, they indicate too small buffers.

Changed in this revision

SerialDriver.cpp Show annotated file Show diff for this revision Revisions of this file
SerialDriver.h Show annotated file Show diff for this revision Revisions of this file
--- a/SerialDriver.cpp	Thu Jan 15 09:53:09 2015 +0000
+++ b/SerialDriver.cpp	Mon Jan 26 19:21:56 2015 +0000
@@ -35,10 +35,13 @@
     rxIn= rxOut= 0;
     txCount= rxCount= 0;
     
+    // reset drop counters
+    numTxDrops= 0;
+    numRxDrops= 0;
+    
     // attach interrupt routines
     attach(this, &SerialDriver::onTxIrq, TxIrq);
     attach(this, &SerialDriver::onRxIrq, RxIrq);
-    
 }
 
 int SerialDriver::putc(int c, unsigned int timeoutMs)
@@ -58,6 +61,7 @@
         disableTxInterrupt();
         if(isTxBufferFull()) // still full? drop byte!
         {
+            numTxDrops++;
             enableTxInterrupt();
             return 0;
         }
@@ -152,7 +156,9 @@
             rxBuffer[rxIn]= (unsigned char)c;
             rxIn= (rxIn+1) % rxBufferLength;
             rxCount++;
-        }   // else drop byte :(
+        }
+        else    // drop byte :(
+            numRxDrops++;
     }
     
     if(wasEmpty && !isRxBufferEmpty())   // more bytes can go
@@ -212,4 +218,4 @@
     return length;
 } 
 
-// for XTN
+// still thinking of XTN
--- a/SerialDriver.h	Thu Jan 15 09:53:09 2015 +0000
+++ b/SerialDriver.h	Mon Jan 26 19:21:56 2015 +0000
@@ -46,6 +46,8 @@
     Semaphore semTxBufferFull;   // used by putc to wait
     Semaphore semRxBufferEmpty;  // used by getc to wait
     
+    // drop counters
+    volatile int numTxDrops, numRxDrops;
     
 public:
     /// @brief Prepares ring buffer and irq
@@ -170,4 +172,13 @@
     /// For compatibility with mbed Serial.
     /// @return true - RX buffer is readable, false - else
     inline bool readable()      {   return !isRxBufferEmpty();   }
+    
+    
+    /// @brief Returns number of dropped bytes that did not fit into TX buffer
+    /// @return number of dropped tx bytes
+    inline int getNumTxDrops()  {   return numTxDrops;  }
+    
+    /// @brief Returns number of dropped bytes that did not fit into RX buffer
+    /// @return number of dropped rx bytes
+    inline int getNumRxDrops()  {   return numRxDrops;  }
 };