Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependents: Frequency_Counter_w_GPS_1PPS FreqCntr_GPS1PPS_F746F4xx_w_recipro
Fork of iSerial by
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 // 2015.01.02 ... Added other mbed code by JH1PJL 00007 // 2016.11.11 ... small modification 00008 // 00009 00010 #include <stdarg.h> 00011 #include "mbed.h" 00012 #include "RingBuffer.h" 00013 #include "iSerial.h" 00014 // Device header files 00015 #include "iSerial_DISCOF746.h" 00016 #include "iSerial_LPC11U24.h" 00017 #include "iSerial_LPC1768.h" 00018 #include "iSerial_NucleoF4xx.h" 00019 #include "iSerial_NucleoL152.h" 00020 00021 /* 00022 * Interrupt Function 00023 */ 00024 void 00025 iSerial::rx_handler(void) 00026 { 00027 while(Serial::readable()) 00028 rxbuf.save(Serial::getc()); 00029 } 00030 00031 void 00032 iSerial::tx_handler(void) 00033 { 00034 while(Serial::writeable() && txbuf.check()) 00035 Serial::putc( txbuf.read() ); 00036 } 00037 00038 iSerial::iSerial(PinName _tx, PinName _rx, int _txbufsize, int _rxbufsize) 00039 : Serial(_tx, _rx), 00040 tx(_tx), 00041 rx(_rx), 00042 txbufsize(_txbufsize), 00043 rxbufsize(_rxbufsize), 00044 txbuf(RingBuffer(txbufsize)), 00045 rxbuf(RingBuffer(rxbufsize)), 00046 str(new char [txbufsize]) 00047 { 00048 __disable_irq(); 00049 00050 attach(this, &iSerial::tx_handler, Serial::TxIrq); 00051 attach(this, &iSerial::rx_handler, Serial::RxIrq); 00052 00053 // format(8,Serial::None,1); // default 00054 // baud(baudrate); 00055 00056 __enable_irq(); 00057 enable_uart_irq(); 00058 } 00059 00060 iSerial::~iSerial() 00061 { 00062 delete [] str; 00063 } 00064 00065 int 00066 iSerial::readable(void) 00067 { 00068 return rxbuf.check(); 00069 } 00070 00071 int 00072 iSerial::getc(void) 00073 { 00074 unsigned short int c; 00075 00076 while(!rxbuf.check()); // wait receiving a character 00077 disable_uart_irq(); 00078 c = rxbuf.read(); 00079 enable_uart_irq(); 00080 00081 return c; 00082 } 00083 00084 void 00085 iSerial::putc(short ch) 00086 { 00087 if(txbuf.check()==0 && Serial::writeable()) { 00088 Serial::putc(ch); 00089 00090 } else { 00091 while(txbuf.full()) { 00092 disable_uart_irq(); 00093 tx_handler(); 00094 enable_uart_irq(); 00095 } 00096 00097 disable_uart_irq(); 00098 txbuf.save(ch); 00099 enable_uart_irq(); 00100 } 00101 } 00102 00103 short int 00104 iSerial::putstr(const char* s) 00105 { 00106 int i=0; 00107 for(; ; i++) { 00108 if(*s==0) break; 00109 putc(*s++); 00110 } 00111 return i; 00112 } 00113 00114 short int 00115 iSerial::puts(const char* s) 00116 { 00117 short int n = putstr(s); 00118 putc(CR); 00119 putc(LF); 00120 return n; 00121 } 00122 00123 char* 00124 iSerial::printf(const char* format, ...) 00125 { 00126 va_list arg; 00127 va_start(arg,format); 00128 vsprintf(str, format, arg); 00129 va_end(arg); 00130 putstr(str); 00131 return str; 00132 } 00133 00134 void 00135 iSerial::flush(void) 00136 { 00137 while(txbuf.check()) 00138 enable_uart_irq(); 00139 }
Generated on Wed Jul 13 2022 12:22:36 by
1.7.2
