Yoji KURODA / iSerial

Dependencies:   RingBuffer

Dependents:   Dumb_box_rev2 mbed_GPSproject

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 USBTX:
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     case p9:
00030         #if defined(TARGET_LPC1768)
00031             NVIC_EnableIRQ(UART1_IRQn);
00032         #elif defined(TARGET_LPC11U24)
00033             NVIC_EnableIRQ(UART_IRQn);
00034         #endif
00035         break;
00036 
00037     #if defined(TARGET_LPC1768)
00038     case p13:
00039         NVIC_EnableIRQ(UART3_IRQn);
00040         break;
00041     case p28:
00042         NVIC_EnableIRQ(UART0_IRQn);
00043         break;
00044     #endif
00045     }
00046 }
00047 
00048 void
00049 iSerial::disable_uart_irq(void)
00050 {
00051     switch(tx){
00052     case USBTX:
00053         #if defined(TARGET_LPC1768)
00054             NVIC_DisableIRQ(UART2_IRQn);
00055         #elif defined(TARGET_LPC11U24)
00056             NVIC_DisableIRQ(UART_IRQn);
00057         #endif
00058 //        led1 = !led1;
00059         break;
00060         
00061     case p9:
00062         #if defined(TARGET_LPC1768)
00063             NVIC_DisableIRQ(UART1_IRQn);
00064         #elif defined(TARGET_LPC11U24)
00065             NVIC_DisableIRQ(UART_IRQn);
00066         #endif
00067         break;
00068 
00069     #if defined(TARGET_LPC1768)
00070     case p13:
00071         NVIC_DisableIRQ(UART3_IRQn);
00072         break;
00073     case p28:
00074         NVIC_DisableIRQ(UART0_IRQn);
00075         break;
00076     #endif
00077     }
00078 }
00079 
00080 /*
00081  *    Interrupt Function
00082  */
00083 void
00084 iSerial::rx_handler(void)
00085 {
00086 //  led3 = 1;
00087     while(Serial::readable())
00088         rxbuf.save(Serial::getc());
00089 //  led3 = 0;
00090 }
00091 
00092 void
00093 iSerial::tx_handler(void)
00094 {
00095 //  led4 = 1;
00096     while(Serial::writeable() && txbuf.check())
00097         Serial::putc( txbuf.read() );
00098 //  led4 = 0;
00099 }
00100 
00101 iSerial::iSerial(PinName _tx, PinName _rx, const char *_name, int _txbufsize, int _rxbufsize)
00102 :   Serial(_tx, _rx, _name),
00103     tx(_tx),
00104     rx(_rx),
00105     txbufsize(_txbufsize),
00106     rxbufsize(_rxbufsize),
00107     txbuf(RingBuffer(txbufsize)),
00108     rxbuf(RingBuffer(rxbufsize)),
00109     str(new char [txbufsize])    
00110 {
00111     __disable_irq();
00112 
00113     attach(this, &iSerial::tx_handler, Serial::TxIrq);
00114     attach(this, &iSerial::rx_handler, Serial::RxIrq);
00115 
00116 //  format(8,Serial::None,1);   // default
00117 //  baud(baudrate);
00118 
00119     __enable_irq();
00120     enable_uart_irq();    
00121 }
00122 
00123 iSerial::~iSerial()
00124 {
00125     delete [] str;
00126 }
00127 
00128 int
00129 iSerial::readable(void)
00130 {
00131     return rxbuf.check();
00132 }
00133 
00134 int
00135 iSerial::getc(void)
00136 {
00137     unsigned short int c;
00138 
00139     while(!rxbuf.check());   // wait receiving a character
00140     disable_uart_irq();
00141     c = rxbuf.read();
00142     enable_uart_irq();
00143 
00144     return c;
00145 }
00146 
00147 void
00148 iSerial::putc(short ch)
00149 {
00150     if(txbuf.check()==0 && Serial::writeable()){
00151         Serial::putc(ch);
00152         
00153     } else {
00154         while(txbuf.full()){
00155             disable_uart_irq();
00156             tx_handler();
00157             enable_uart_irq();
00158         }
00159         
00160         disable_uart_irq();
00161         txbuf.save(ch);
00162         enable_uart_irq();
00163     }
00164 }
00165 
00166 short int
00167 iSerial::putstr(const char* s)
00168 {
00169     int i=0;
00170     for(; ; i++){
00171         if(*s==0) break;
00172         putc(*s++);
00173     }
00174     return i;
00175 }
00176 
00177 short int
00178 iSerial::puts(const char* s)
00179 {
00180     short int n = putstr(s);
00181     putc(CR);
00182     putc(LF);
00183     return n;
00184 }
00185 
00186 char*
00187 iSerial::printf(const char* format, ...)
00188 {
00189     va_list arg;
00190     va_start(arg,format);
00191     vsprintf(str, format, arg);
00192     va_end(arg);
00193     putstr(str);
00194     return str;
00195 }
00196 
00197 
00198 void
00199 iSerial::flush(void)
00200 {
00201     while(txbuf.check())
00202         enable_uart_irq();
00203 }
00204 
00205