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();
}

iSerial.h

Committer:
ykuroda
Date:
2012-09-03
Revision:
6:8d4b95b90c3b
Parent:
5:d83fc550ccbc
Child:
7:5a25789c3b55

File content as of revision 6:8d4b95b90c3b:

//
//  iSerial.h ... Serial Driver with Interrupt Rec/Send
//
//  Copyright 2012  Yoji KURODA
//
//  2009.11.13 ... Originally written by Y.Kuroda for Renesas H83664
//  2012.08.31 ... Code convert for mbed in C++
//
#ifndef _ISERIAL_H
#define _ISERIAL_H

#include <string.h>
#include "RingBuffer.h"


class iSerial : public Serial {
  protected:

    PinName tx;
    PinName rx;
    const int txbufsize;
    const int rxbufsize;
    RingBuffer txbuf;
    RingBuffer rxbuf;
    char* str;

    void tx_handler(void);
    void rx_handler(void);
    void enable_uart_irq(void);

  public:

    enum TERMINL_CODES { CR=0x0D, LF=0x0A };

    iSerial(PinName _tx, PinName _rx, const char *_name=NULL, int _txbufsize=100, int _rxbufsize=100);
    virtual ~iSerial();
    
    short int putstr(const char* s);

    int readable(void);
    int getc(void);
    void putc(short ch);
    short int puts(const char* s);
    //void printf();
    char* printf(const char* format, ...);

    void flush(void);
};


#endif    /* _SCI_H */