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.

Revision:
4:a41e47716932
Parent:
3:ea9719695b6a
Child:
5:cb9007ef1a30
--- a/SerialDriver.cpp	Mon Jan 26 19:21:56 2015 +0000
+++ b/SerialDriver.cpp	Fri Oct 23 13:49:19 2015 +0000
@@ -42,6 +42,9 @@
     // attach interrupt routines
     attach(this, &SerialDriver::onTxIrq, TxIrq);
     attach(this, &SerialDriver::onRxIrq, RxIrq);
+    
+    // we need tx interrupt not yet
+    disableTxInterrupt();
 }
 
 int SerialDriver::putc(int c, unsigned int timeoutMs)
@@ -88,7 +91,8 @@
     
     // write as long as you can
     bool wasFull= isTxBufferFull();
-    while(SerialBase::writeable() && !isTxBufferEmtpy())
+    bool wasEmpty= isTxBufferEmtpy();
+    while(SerialBase::writeable() && !wasEmpty)
     {
         // take byte from tx buffer and put it out
         SerialBase::_base_putc(txBuffer[txOut]);
@@ -100,7 +104,10 @@
         semTxBufferFull.release();
     
     // ok, let's wait for next writability
-    enableTxInterrupt();
+    // if the is something to send,  
+    // else we need tx interrupt not yet
+    if(wasEmpty);
+        enableTxInterrupt();
 }
 
 
@@ -210,6 +217,8 @@
     
     int length= vsnprintf(NULL, 0, format, arg);
     char *temp = new char[length + 1];
+    if(temp == NULL)
+        return 0;   // I can't work like this!    
     vsprintf(temp, format, arg);
     puts(temp, true);
     delete[] temp;