Better Serial C++ class library with interrupt
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.cpp@4:b38ef9675d39, 2012-09-03 (annotated)
- Committer:
- ykuroda
- Date:
- Mon Sep 03 09:15:34 2012 +0000
- Revision:
- 4:b38ef9675d39
- Parent:
- 3:d5353b68105f
- Child:
- 5:d83fc550ccbc
printf using interrupt is implemented
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
ykuroda | 3:d5353b68105f | 1 | // |
ykuroda | 3:d5353b68105f | 2 | // iSerial.cpp ... Serial Driver with Interrupt Rec/Send |
ykuroda | 3:d5353b68105f | 3 | // |
ykuroda | 3:d5353b68105f | 4 | // 2009.11.13 ... Originally written by Y.Kuroda for Renesas H83664 |
ykuroda | 3:d5353b68105f | 5 | // 2012.08.31 ... Code convert for mbed in C++ |
ykuroda | 3:d5353b68105f | 6 | // |
ykuroda | 4:b38ef9675d39 | 7 | #include <stdarg.h> |
ykuroda | 3:d5353b68105f | 8 | #include "mbed.h" |
ykuroda | 3:d5353b68105f | 9 | #include "RingBuffer.h" |
ykuroda | 3:d5353b68105f | 10 | #include "iSerial.h" |
ykuroda | 3:d5353b68105f | 11 | |
ykuroda | 3:d5353b68105f | 12 | |
ykuroda | 4:b38ef9675d39 | 13 | |
ykuroda | 4:b38ef9675d39 | 14 | #if defined(TARGET_LPC1768) |
ykuroda | 4:b38ef9675d39 | 15 | |
ykuroda | 4:b38ef9675d39 | 16 | // UART1 = p9, p10 |
ykuroda | 4:b38ef9675d39 | 17 | |
ykuroda | 4:b38ef9675d39 | 18 | #define UART_IRQn UART2_IRQn |
ykuroda | 4:b38ef9675d39 | 19 | |
ykuroda | 4:b38ef9675d39 | 20 | #elif defined(TARGET_LPC11U24) |
ykuroda | 4:b38ef9675d39 | 21 | |
ykuroda | 4:b38ef9675d39 | 22 | #define UART_IRQn UART_IRQn |
ykuroda | 4:b38ef9675d39 | 23 | |
ykuroda | 4:b38ef9675d39 | 24 | #endif |
ykuroda | 4:b38ef9675d39 | 25 | |
ykuroda | 4:b38ef9675d39 | 26 | |
ykuroda | 4:b38ef9675d39 | 27 | |
ykuroda | 3:d5353b68105f | 28 | /* |
ykuroda | 3:d5353b68105f | 29 | * Interrupt Function |
ykuroda | 3:d5353b68105f | 30 | */ |
ykuroda | 3:d5353b68105f | 31 | void |
ykuroda | 3:d5353b68105f | 32 | iSerial::rx_handler(void) |
ykuroda | 3:d5353b68105f | 33 | { |
ykuroda | 3:d5353b68105f | 34 | if(Serial::readable()){ |
ykuroda | 3:d5353b68105f | 35 | rxbuf.save(Serial::getc()); |
ykuroda | 3:d5353b68105f | 36 | } |
ykuroda | 3:d5353b68105f | 37 | |
ykuroda | 3:d5353b68105f | 38 | NVIC_EnableIRQ(UART_IRQn); // UART1_IRQn); |
ykuroda | 3:d5353b68105f | 39 | } |
ykuroda | 3:d5353b68105f | 40 | |
ykuroda | 3:d5353b68105f | 41 | void |
ykuroda | 3:d5353b68105f | 42 | iSerial::tx_handler(void) |
ykuroda | 3:d5353b68105f | 43 | { |
ykuroda | 3:d5353b68105f | 44 | if(Serial::writeable()){ |
ykuroda | 3:d5353b68105f | 45 | if(txbuf.check()){ |
ykuroda | 3:d5353b68105f | 46 | Serial::putc( txbuf.read() ); |
ykuroda | 3:d5353b68105f | 47 | } |
ykuroda | 3:d5353b68105f | 48 | } |
ykuroda | 3:d5353b68105f | 49 | NVIC_EnableIRQ(UART_IRQn); // UART1_IRQn); |
ykuroda | 3:d5353b68105f | 50 | } |
ykuroda | 3:d5353b68105f | 51 | |
ykuroda | 3:d5353b68105f | 52 | iSerial::iSerial(PinName _tx, PinName _rx, const char *_name, int _txbufsize, int _rxbufsize) |
ykuroda | 3:d5353b68105f | 53 | : Serial(_tx, _rx, _name), |
ykuroda | 4:b38ef9675d39 | 54 | txbufsize(_txbufsize), |
ykuroda | 4:b38ef9675d39 | 55 | rxbufsize(_rxbufsize), |
ykuroda | 4:b38ef9675d39 | 56 | txbuf(RingBuffer(txbufsize)), |
ykuroda | 4:b38ef9675d39 | 57 | rxbuf(RingBuffer(rxbufsize)), |
ykuroda | 4:b38ef9675d39 | 58 | str(new char [txbufsize]) |
ykuroda | 3:d5353b68105f | 59 | { |
ykuroda | 3:d5353b68105f | 60 | __disable_irq(); |
ykuroda | 3:d5353b68105f | 61 | |
ykuroda | 3:d5353b68105f | 62 | attach(this, &iSerial::tx_handler, Serial::TxIrq); |
ykuroda | 3:d5353b68105f | 63 | attach(this, &iSerial::rx_handler, Serial::RxIrq); |
ykuroda | 3:d5353b68105f | 64 | |
ykuroda | 3:d5353b68105f | 65 | // format(8,Serial::None,1); // default |
ykuroda | 3:d5353b68105f | 66 | // baud(baudrate); |
ykuroda | 3:d5353b68105f | 67 | |
ykuroda | 3:d5353b68105f | 68 | __enable_irq(); |
ykuroda | 3:d5353b68105f | 69 | NVIC_EnableIRQ(UART_IRQn); // UART1_IRQn); |
ykuroda | 3:d5353b68105f | 70 | } |
ykuroda | 3:d5353b68105f | 71 | |
ykuroda | 4:b38ef9675d39 | 72 | iSerial::~iSerial() |
ykuroda | 4:b38ef9675d39 | 73 | { |
ykuroda | 4:b38ef9675d39 | 74 | delete [] str; |
ykuroda | 4:b38ef9675d39 | 75 | } |
ykuroda | 4:b38ef9675d39 | 76 | |
ykuroda | 3:d5353b68105f | 77 | int |
ykuroda | 3:d5353b68105f | 78 | iSerial::readable(void) |
ykuroda | 3:d5353b68105f | 79 | { |
ykuroda | 3:d5353b68105f | 80 | return rxbuf.check(); |
ykuroda | 3:d5353b68105f | 81 | } |
ykuroda | 3:d5353b68105f | 82 | |
ykuroda | 3:d5353b68105f | 83 | int |
ykuroda | 3:d5353b68105f | 84 | iSerial::getc(void) |
ykuroda | 3:d5353b68105f | 85 | { |
ykuroda | 3:d5353b68105f | 86 | unsigned short int c; |
ykuroda | 3:d5353b68105f | 87 | |
ykuroda | 3:d5353b68105f | 88 | while(!rxbuf.check()); |
ykuroda | 3:d5353b68105f | 89 | c = rxbuf.read(); |
ykuroda | 3:d5353b68105f | 90 | |
ykuroda | 3:d5353b68105f | 91 | return c; |
ykuroda | 3:d5353b68105f | 92 | } |
ykuroda | 3:d5353b68105f | 93 | |
ykuroda | 3:d5353b68105f | 94 | void |
ykuroda | 3:d5353b68105f | 95 | iSerial::putc(short ch) |
ykuroda | 3:d5353b68105f | 96 | { |
ykuroda | 3:d5353b68105f | 97 | if(txbuf.check()==0 && Serial::writeable()){ |
ykuroda | 3:d5353b68105f | 98 | Serial::putc(ch); |
ykuroda | 3:d5353b68105f | 99 | |
ykuroda | 3:d5353b68105f | 100 | } else { |
ykuroda | 3:d5353b68105f | 101 | while(txbuf.full()); |
ykuroda | 3:d5353b68105f | 102 | txbuf.save(ch); |
ykuroda | 3:d5353b68105f | 103 | NVIC_EnableIRQ(UART_IRQn); // UART1_IRQn); |
ykuroda | 3:d5353b68105f | 104 | } |
ykuroda | 3:d5353b68105f | 105 | } |
ykuroda | 3:d5353b68105f | 106 | |
ykuroda | 3:d5353b68105f | 107 | short int |
ykuroda | 3:d5353b68105f | 108 | iSerial::putstr(const char* s) |
ykuroda | 3:d5353b68105f | 109 | { |
ykuroda | 3:d5353b68105f | 110 | int i=0; |
ykuroda | 3:d5353b68105f | 111 | for(; ; i++){ |
ykuroda | 3:d5353b68105f | 112 | if(*s==0) break; |
ykuroda | 3:d5353b68105f | 113 | putc(*s++); |
ykuroda | 3:d5353b68105f | 114 | } |
ykuroda | 3:d5353b68105f | 115 | return i; |
ykuroda | 3:d5353b68105f | 116 | } |
ykuroda | 3:d5353b68105f | 117 | |
ykuroda | 3:d5353b68105f | 118 | short int |
ykuroda | 3:d5353b68105f | 119 | iSerial::puts(const char* s) |
ykuroda | 3:d5353b68105f | 120 | { |
ykuroda | 3:d5353b68105f | 121 | short int n = putstr(s); |
ykuroda | 3:d5353b68105f | 122 | putc(CR); |
ykuroda | 3:d5353b68105f | 123 | putc(LF); |
ykuroda | 3:d5353b68105f | 124 | return n; |
ykuroda | 3:d5353b68105f | 125 | } |
ykuroda | 3:d5353b68105f | 126 | |
ykuroda | 4:b38ef9675d39 | 127 | char* |
ykuroda | 4:b38ef9675d39 | 128 | iSerial::printf(const char* format, ...) |
ykuroda | 4:b38ef9675d39 | 129 | { |
ykuroda | 4:b38ef9675d39 | 130 | va_list arg; |
ykuroda | 4:b38ef9675d39 | 131 | va_start(arg,format); |
ykuroda | 4:b38ef9675d39 | 132 | vsprintf(str, format, arg); |
ykuroda | 4:b38ef9675d39 | 133 | va_end(arg); |
ykuroda | 4:b38ef9675d39 | 134 | putstr(str); |
ykuroda | 4:b38ef9675d39 | 135 | return str; |
ykuroda | 4:b38ef9675d39 | 136 | } |
ykuroda | 4:b38ef9675d39 | 137 | |
ykuroda | 4:b38ef9675d39 | 138 | |
ykuroda | 4:b38ef9675d39 | 139 | |
ykuroda | 4:b38ef9675d39 | 140 |