Better Serial C++ class library with interrupt

Dependencies:   RingBuffer

Dependents:   Dumb_box_rev2 mbed_GPSproject

iSerial class library can easy be used just by swapping Serial declarations.

API

iSerial

Examples

#include "mbed.h"
#include "iSerial.h"

char s[] = "12345678901234567890123456789012345678901234567890";

int main() {

    iSerial pc(USBTX,USBRX);
    char* c = s;

    while(1) {
        pc.puts(s);
        
        while(pc.readable()){
            int ret = pc.getc();
            if(ret)
                *c++ = ret;
            if(c >= s+sizeof(s)-1)
                c = s;
        }
        wait(1);
    }
}

Example 2

Comparing speed of ordinary Serial functions

#include "mbed.h"
#include "iSerial.h"

char s[] = "12345678901234567890123456789012345678901234567890";    // 50 bytes

int main() {

    Timer t;

    Serial pc(USBTX,USBRX);
    pc.baud(921600);
    
    t.reset();
    t.start();
    for(int i=0; i<30; i++) {
        pc.printf(s);
        wait(0.001);
    }
    t.stop();
    
    int t1 = t.read_us();

    pc.puts("\n\n\n");


    iSerial ipc(USBTX,USBRX,NULL,1000,100); // try NOT to set the buffer over flowed
//    iSerial ipc(USBTX,USBRX);   // set as default = 100, 100
    ipc.baud(921600);

    t.reset();
    t.start();
    for(int i=0; i<30; i++) {
        ipc.printf(s);
        wait(0.001);
    }
    t.stop();
    
    int t2 = t.read_us();

    ipc.printf("\n\n\nThe time %d, %d [us] was taken.\n", t1, t2);
    ipc.puts("End.");
    ipc.flush();
}
Revision:
8:20759f992d48
Parent:
7:5a25789c3b55
--- a/iSerial.cpp	Mon Sep 03 17:26:38 2012 +0000
+++ b/iSerial.cpp	Mon Sep 03 17:59:00 2012 +0000
@@ -9,8 +9,8 @@
 #include "RingBuffer.h"
 #include "iSerial.h"
 
-
-DigitalOut led4(LED4);
+//DigitalOut led3(LED3);
+//DigitalOut led4(LED4);
 
 
 void
@@ -83,22 +83,19 @@
 void
 iSerial::rx_handler(void)
 {
-//    disable_uart_irq();
-    if(Serial::readable()){
+//  led3 = 1;
+    while(Serial::readable())
         rxbuf.save(Serial::getc());
-    }
-//    enable_uart_irq();
+//  led3 = 0;
 }
 
 void
 iSerial::tx_handler(void)
 {
-led4 = 1;
-//    disable_uart_irq();
+//  led4 = 1;
     while(Serial::writeable() && txbuf.check())
         Serial::putc( txbuf.read() );
-//    enable_uart_irq();
-led4 = 0;
+//  led4 = 0;
 }
 
 iSerial::iSerial(PinName _tx, PinName _rx, const char *_name, int _txbufsize, int _rxbufsize)
@@ -139,9 +136,10 @@
 {
     unsigned short int c;
 
-    while(!rxbuf.check())   // wait receiving a character
-        enable_uart_irq();
+    while(!rxbuf.check());   // wait receiving a character
+    disable_uart_irq();
     c = rxbuf.read();
+    enable_uart_irq();
 
     return c;
 }