ALPHA WORK IN PROGRESS. DO NOT USE IN PRODUCTION CODE. based on:\\ IAR Application Note G - 001 Generic Software UART\\ mbed/trunk/Serial.h\\ the venerable Andy Kirkham's MAX3100\\
SoftwareSerial.h
00001 // modified from mbed library, original copyright: 00002 00003 /* mbed Microcontroller Library - Serial 00004 * Copyright (c) 2007-2011 ARM Limited. All rights reserved. 00005 */ 00006 00007 00008 #ifndef SOFTWARE_SERIAL_H 00009 #define SOFTWARE_SERIAL_H 00010 00011 #ifndef SOFTWARE_SERIAL_RX_BUFFER_SIZE 00012 #define SOFTWARE_SERIAL_RX_BUFFER_SIZE 256 00013 #endif 00014 00015 00016 #include "mbed.h" 00017 00018 00019 namespace SoftwareSerial { 00020 00021 /* Class: SoftwareSerial 00022 * A software implemented serial port (UART) for communication with other serial devices 00023 * 00024 * Can be used for Full Duplex communication, or Simplex by specifying 00025 * one pin as NC (Not Connected) 00026 * 00027 * Example: 00028 * > // Print "Hello World" to the PC 00029 * > 00030 * > #include "mbed.h" 00031 * > #include "SoftwareSerial.h" 00032 * > 00033 * > SoftwareSerial ser(P19, P20); 00034 * > 00035 * > int main() { 00036 * > ser.printf("Hello World\n"); 00037 * > } 00038 */ 00039 class SoftwareSerial : public Stream { 00040 00041 public: 00042 00043 /* Constructor: SoftwareSerial 00044 * Create a SoftwareSerial port, connected to the specified transmit and receive pins 00045 * 00046 * Variables: 00047 * tx - Transmit pin 00048 * rx - Receive pin 00049 * 00050 * Note: Either tx or rx may be specified as NC if unused 00051 */ 00052 SoftwareSerial(PinName tx, PinName rx, const char *name = NULL); 00053 00054 /* Function: baud 00055 * Set the baud rate of the serial port 00056 * 00057 * Variables: 00058 * baudrate - The baudrate of the serial port (default = 9600). 00059 */ 00060 void baud(int baudrate); 00061 00062 /*enum Parity { 00063 None = 0 00064 , Odd 00065 , Even 00066 , Forced1 00067 , Forced0 00068 };*/ 00069 00070 enum IrqType { 00071 RxIrq = 0 00072 , TxIrq 00073 }; 00074 00075 /* Function: format 00076 * Set the transmission format used by the Serial port 00077 * 00078 * Variables: 00079 * bits - The number of bits in a word (5-8; default = 8) 00080 * parity - The parity used (SoftwareSerial::None, SoftwareSerial::Odd, SoftwareSerial::Even, SoftwareSerial::Forced1, SoftwareSerial::Forced0; default = SoftwareSerial::None) 00081 * stop - The number of stop bits (1 or 2; default = 1) 00082 */ 00083 /*void format(int bits = 8, Parity parity = Serial::None, int stop_bits = 1); */ 00084 00085 #if 0 // Inhereted from Stream, for documentation only 00086 00087 /* Function: putc 00088 * Write a character 00089 * 00090 * Variables: 00091 * c - The character to write to the serial port 00092 */ 00093 int putc(int c); 00094 00095 00096 /* Function: getc 00097 * Read a character 00098 * 00099 * Reads a character from the serial port. This will block until 00100 * a character is available. To see if a character is available, 00101 * see <readable> 00102 * 00103 * Variables: 00104 * returns - The character read from the serial port 00105 */ 00106 int getc(); 00107 00108 /* Function: printf 00109 * Write a formated string 00110 * 00111 * Variables: 00112 * format - A printf-style format string, followed by the 00113 * variables to use in formating the string. 00114 */ 00115 int printf(const char* format, ...); 00116 00117 /* Function: scanf 00118 * Read a formated string 00119 * 00120 * Variables: 00121 * format - A scanf-style format string, 00122 * followed by the pointers to variables to store the results. 00123 */ 00124 int scanf(const char* format, ...); 00125 00126 #endif 00127 00128 00129 #if 1 00130 /** putc 00131 * @param int c The byte to write. 00132 */ 00133 int putc(int c); 00134 00135 /** puts 00136 * @param char * The string to print. 00137 */ 00138 //void puts(char *s); 00139 00140 /** getc 00141 * @return int c The byte read or -1 if no bytes to read. 00142 */ 00143 int getc(void); 00144 00145 /** gets 00146 * Get a string. Note, this method blocks until size bytes are read. 00147 * @param char *s where to place the incoming bytes. 00148 * @param int size How many bytes to read. 00149 * @return char * The value of *s passed in. 00150 */ 00151 //char *gets(char *s, int size); 00152 00153 /** peek 00154 * like getc() but does NOT remove the byte from the buffer. 00155 * @see getc*( 00156 */ 00157 //int peek(void); 00158 00159 #endif 00160 00161 /* Function: readable 00162 * Determine if there is a character available to read 00163 * 00164 * Variables: 00165 * returns - 1 if there is a character available to read, else 0 00166 */ 00167 int readable(); 00168 00169 /* Function: writeable 00170 * Determine if there is space available to write a character 00171 * 00172 * Variables: 00173 * returns - 1 if there is space to write a character, else 0 00174 */ 00175 int writeable(); 00176 00177 /* Function: attach 00178 * Attach a function to call whenever a serial interrupt is generated 00179 * 00180 * Variables: 00181 * fptr - A pointer to a void function, or 0 to set as none 00182 * type - Which serial interrupt to attach the member function to (Seriall::RxIrq for receive, TxIrq for transmit buffer empty) 00183 */ 00184 //void attach(void (*fptr)(void), IrqType type = RxIrq); 00185 00186 /* Function: attach 00187 * Attach a member function to call whenever a serial interrupt is generated 00188 * 00189 * Variables: 00190 * tptr - pointer to the object to call the member function on 00191 * mptr - pointer to the member function to be called 00192 * type - Which serial interrupt to attach the member function to (Seriall::RxIrq for receive, TxIrq for transmit buffer empty) 00193 */ 00194 template<typename T> 00195 void attach(T* tptr, void (T::*mptr)(void), IrqType type = RxIrq) { 00196 if((mptr != NULL) && (tptr != NULL)) { 00197 _irq[type].attach(tptr, mptr); 00198 setup_interrupt(type); 00199 } 00200 } 00201 00202 #ifdef MBED_RPC 00203 //virtual const struct rpc_method *get_rpc_methods(); 00204 static struct rpc_class *get_rpc_class(); 00205 #endif 00206 00207 protected: 00208 00209 //void setup_interrupt(IrqType type); 00210 //void remove_interrupt(IrqType type); 00211 00212 //UARTName _uart; 00213 //FunctionPointer _irq[2]; 00214 //int _uidx; 00215 00216 virtual int _putc(int c) { return putc(c); } 00217 virtual int _getc() { return getc(); } 00218 00219 00220 00221 int get_rx_pin_status(); 00222 void set_tx_pin_high(); 00223 void set_tx_pin_low(); 00224 void idle(); 00225 void timer_set( int baud ); 00226 void set_timer_interrupt( void (*timer_isr)(void) ); 00227 00228 00229 00230 void timer_isr( void ); 00231 void init_uart( void ); 00232 //char _getchar( void ); 00233 //void _putchar( char ch ); 00234 void flush_input_buffer( void ); 00235 char kbhit( void ); 00236 void turn_rx_on( void ); 00237 void turn_rx_off( void ); 00238 00239 00240 private: 00241 DigitalOut *tx; 00242 DigitalIn *rx; 00243 00244 Ticker ticker; 00245 00246 int baud_rate; 00247 00248 unsigned char inbuf[SOFTWARE_SERIAL_RX_BUFFER_SIZE]; 00249 unsigned char qin; 00250 unsigned char qout; 00251 char flag_rx_waiting_for_stop_bit; 00252 bool flag_rx_off; 00253 char rx_mask; 00254 bool flag_rx_ready; 00255 bool flag_tx_ready; 00256 char timer_rx_ctr; 00257 char timer_tx_ctr; 00258 char bits_left_in_rx; 00259 char bits_left_in_tx; 00260 char rx_num_of_bits; 00261 char tx_num_of_bits; 00262 char internal_rx_buffer; 00263 char internal_tx_buffer; 00264 char user_tx_buffer; 00265 00266 }; 00267 00268 } // namespace 00269 00270 using namespace SoftwareSerial; 00271 00272 #endif
Generated on Tue Jul 12 2022 21:57:00 by 1.7.2