Amalus / iSerial

Dependencies:   RingBuffer

Fork of iSerial by Yoji KURODA

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers iSerial.cpp Source File

iSerial.cpp

00001 //
00002 //  iSerial.cpp ... Serial Driver with Interrupt Rec/Send
00003 //
00004 //  2009.11.13 ... Originally written by Y.Kuroda for Renesas H83664
00005 //  2012.08.31 ... Code convert for mbed in C++
00006 //
00007 #include <stdarg.h>
00008 #include "mbed.h"
00009 #include "RingBuffer.h"
00010 #include "iSerial.h"
00011 
00012 //DigitalOut led3(LED3);
00013 //DigitalOut led4(LED4);
00014 
00015 
00016 void
00017 iSerial::enable_uart_irq(void)
00018 {
00019     switch(tx){
00020     case PA_9:
00021         #if defined(TARGET_LPC1768)
00022             NVIC_EnableIRQ(UART2_IRQn);
00023         #elif defined(TARGET_LPC11U24)
00024             NVIC_EnableIRQ(UART_IRQn);
00025         #endif
00026 //        led1 = !led1;
00027         break;
00028         
00029     }
00030 }
00031 
00032 void
00033 iSerial::disable_uart_irq(void)
00034 {
00035     switch(tx){
00036     case PA_9:
00037         #if defined(TARGET_LPC1768)
00038             NVIC_DisableIRQ(UART2_IRQn);
00039         #elif defined(TARGET_LPC11U24)
00040             NVIC_DisableIRQ(UART_IRQn);
00041         #endif
00042 //        led1 = !led1;
00043         break;
00044     }
00045 }
00046 
00047 /*
00048  *    Interrupt Function
00049  */
00050 void
00051 iSerial::rx_handler(void)
00052 {
00053 //  led3 = 1;
00054     while(Serial::readable())
00055         rxbuf.save(Serial::getc());
00056 //  led3 = 0;
00057 }
00058 
00059 void
00060 iSerial::tx_handler(void)
00061 {
00062 //  led4 = 1;
00063     while(Serial::writeable() && txbuf.check())
00064         Serial::putc( txbuf.read() );
00065 //  led4 = 0;
00066 }
00067 
00068 iSerial::iSerial(PinName _tx, PinName _rx, const char *_name, int _txbufsize, int _rxbufsize)
00069 :   Serial(_tx, _rx, _name),
00070     tx(_tx),
00071     rx(_rx),
00072     txbufsize(_txbufsize),
00073     rxbufsize(_rxbufsize),
00074     txbuf(RingBuffer(txbufsize)),
00075     rxbuf(RingBuffer(rxbufsize)),
00076     str(new char [txbufsize])    
00077 {
00078     __disable_irq();
00079 
00080     attach(this, &iSerial::tx_handler, Serial::TxIrq);
00081     attach(this, &iSerial::rx_handler, Serial::RxIrq);
00082 
00083 //  format(8,Serial::None,1);   // default
00084 //  baud(baudrate);
00085 
00086     __enable_irq();
00087     enable_uart_irq();    
00088 }
00089 
00090 iSerial::~iSerial()
00091 {
00092     delete [] str;
00093 }
00094 
00095 int
00096 iSerial::readable(void)
00097 {
00098     return rxbuf.check();
00099 }
00100 
00101 int
00102 iSerial::getc(void)
00103 {
00104     unsigned short int c;
00105 
00106     while(!rxbuf.check());   // wait receiving a character
00107     disable_uart_irq();
00108     c = rxbuf.read();
00109     enable_uart_irq();
00110 
00111     return c;
00112 }
00113 
00114 void
00115 iSerial::putc(short ch)
00116 {
00117     if(txbuf.check()==0 && Serial::writeable()){
00118         Serial::putc(ch);
00119         
00120     } else {
00121         while(txbuf.full()){
00122             disable_uart_irq();
00123             tx_handler();
00124             enable_uart_irq();
00125         }
00126         
00127         disable_uart_irq();
00128         txbuf.save(ch);
00129         enable_uart_irq();
00130     }
00131 }
00132 
00133 short int
00134 iSerial::putstr(const char* s)
00135 {
00136     int i=0;
00137     for(; ; i++){
00138         if(*s==0) break;
00139         putc(*s++);
00140     }
00141     return i;
00142 }
00143 
00144 short int
00145 iSerial::puts(const char* s)
00146 {
00147     short int n = putstr(s);
00148     putc(CR);
00149     putc(LF);
00150     return n;
00151 }
00152 
00153 char*
00154 iSerial::printf(const char* format, ...)
00155 {
00156     va_list arg;
00157     va_start(arg,format);
00158     vsprintf(str, format, arg);
00159     va_end(arg);
00160     putstr(str);
00161     return str;
00162 }
00163 
00164 
00165 void
00166 iSerial::flush(void)
00167 {
00168     while(txbuf.check())
00169         enable_uart_irq();
00170 }
00171 
00172