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\\

Dependents:   SoftwareUART

Committer:
chag
Date:
Sat Mar 17 17:25:55 2012 +0000
Revision:
0:25b087be2989

        

Who changed what in which revision?

UserRevisionLine numberNew contents of line
chag 0:25b087be2989 1 // modified from mbed library, original copyright:
chag 0:25b087be2989 2
chag 0:25b087be2989 3 /* mbed Microcontroller Library - Serial
chag 0:25b087be2989 4 * Copyright (c) 2007-2011 ARM Limited. All rights reserved.
chag 0:25b087be2989 5 */
chag 0:25b087be2989 6
chag 0:25b087be2989 7
chag 0:25b087be2989 8 #ifndef SOFTWARE_SERIAL_H
chag 0:25b087be2989 9 #define SOFTWARE_SERIAL_H
chag 0:25b087be2989 10
chag 0:25b087be2989 11 #ifndef SOFTWARE_SERIAL_RX_BUFFER_SIZE
chag 0:25b087be2989 12 #define SOFTWARE_SERIAL_RX_BUFFER_SIZE 256
chag 0:25b087be2989 13 #endif
chag 0:25b087be2989 14
chag 0:25b087be2989 15
chag 0:25b087be2989 16 #include "mbed.h"
chag 0:25b087be2989 17
chag 0:25b087be2989 18
chag 0:25b087be2989 19 namespace SoftwareSerial {
chag 0:25b087be2989 20
chag 0:25b087be2989 21 /* Class: SoftwareSerial
chag 0:25b087be2989 22 * A software implemented serial port (UART) for communication with other serial devices
chag 0:25b087be2989 23 *
chag 0:25b087be2989 24 * Can be used for Full Duplex communication, or Simplex by specifying
chag 0:25b087be2989 25 * one pin as NC (Not Connected)
chag 0:25b087be2989 26 *
chag 0:25b087be2989 27 * Example:
chag 0:25b087be2989 28 * > // Print "Hello World" to the PC
chag 0:25b087be2989 29 * >
chag 0:25b087be2989 30 * > #include "mbed.h"
chag 0:25b087be2989 31 * > #include "SoftwareSerial.h"
chag 0:25b087be2989 32 * >
chag 0:25b087be2989 33 * > SoftwareSerial ser(P19, P20);
chag 0:25b087be2989 34 * >
chag 0:25b087be2989 35 * > int main() {
chag 0:25b087be2989 36 * > ser.printf("Hello World\n");
chag 0:25b087be2989 37 * > }
chag 0:25b087be2989 38 */
chag 0:25b087be2989 39 class SoftwareSerial : public Stream {
chag 0:25b087be2989 40
chag 0:25b087be2989 41 public:
chag 0:25b087be2989 42
chag 0:25b087be2989 43 /* Constructor: SoftwareSerial
chag 0:25b087be2989 44 * Create a SoftwareSerial port, connected to the specified transmit and receive pins
chag 0:25b087be2989 45 *
chag 0:25b087be2989 46 * Variables:
chag 0:25b087be2989 47 * tx - Transmit pin
chag 0:25b087be2989 48 * rx - Receive pin
chag 0:25b087be2989 49 *
chag 0:25b087be2989 50 * Note: Either tx or rx may be specified as NC if unused
chag 0:25b087be2989 51 */
chag 0:25b087be2989 52 SoftwareSerial(PinName tx, PinName rx, const char *name = NULL);
chag 0:25b087be2989 53
chag 0:25b087be2989 54 /* Function: baud
chag 0:25b087be2989 55 * Set the baud rate of the serial port
chag 0:25b087be2989 56 *
chag 0:25b087be2989 57 * Variables:
chag 0:25b087be2989 58 * baudrate - The baudrate of the serial port (default = 9600).
chag 0:25b087be2989 59 */
chag 0:25b087be2989 60 void baud(int baudrate);
chag 0:25b087be2989 61
chag 0:25b087be2989 62 /*enum Parity {
chag 0:25b087be2989 63 None = 0
chag 0:25b087be2989 64 , Odd
chag 0:25b087be2989 65 , Even
chag 0:25b087be2989 66 , Forced1
chag 0:25b087be2989 67 , Forced0
chag 0:25b087be2989 68 };*/
chag 0:25b087be2989 69
chag 0:25b087be2989 70 enum IrqType {
chag 0:25b087be2989 71 RxIrq = 0
chag 0:25b087be2989 72 , TxIrq
chag 0:25b087be2989 73 };
chag 0:25b087be2989 74
chag 0:25b087be2989 75 /* Function: format
chag 0:25b087be2989 76 * Set the transmission format used by the Serial port
chag 0:25b087be2989 77 *
chag 0:25b087be2989 78 * Variables:
chag 0:25b087be2989 79 * bits - The number of bits in a word (5-8; default = 8)
chag 0:25b087be2989 80 * parity - The parity used (SoftwareSerial::None, SoftwareSerial::Odd, SoftwareSerial::Even, SoftwareSerial::Forced1, SoftwareSerial::Forced0; default = SoftwareSerial::None)
chag 0:25b087be2989 81 * stop - The number of stop bits (1 or 2; default = 1)
chag 0:25b087be2989 82 */
chag 0:25b087be2989 83 /*void format(int bits = 8, Parity parity = Serial::None, int stop_bits = 1); */
chag 0:25b087be2989 84
chag 0:25b087be2989 85 #if 0 // Inhereted from Stream, for documentation only
chag 0:25b087be2989 86
chag 0:25b087be2989 87 /* Function: putc
chag 0:25b087be2989 88 * Write a character
chag 0:25b087be2989 89 *
chag 0:25b087be2989 90 * Variables:
chag 0:25b087be2989 91 * c - The character to write to the serial port
chag 0:25b087be2989 92 */
chag 0:25b087be2989 93 int putc(int c);
chag 0:25b087be2989 94
chag 0:25b087be2989 95
chag 0:25b087be2989 96 /* Function: getc
chag 0:25b087be2989 97 * Read a character
chag 0:25b087be2989 98 *
chag 0:25b087be2989 99 * Reads a character from the serial port. This will block until
chag 0:25b087be2989 100 * a character is available. To see if a character is available,
chag 0:25b087be2989 101 * see <readable>
chag 0:25b087be2989 102 *
chag 0:25b087be2989 103 * Variables:
chag 0:25b087be2989 104 * returns - The character read from the serial port
chag 0:25b087be2989 105 */
chag 0:25b087be2989 106 int getc();
chag 0:25b087be2989 107
chag 0:25b087be2989 108 /* Function: printf
chag 0:25b087be2989 109 * Write a formated string
chag 0:25b087be2989 110 *
chag 0:25b087be2989 111 * Variables:
chag 0:25b087be2989 112 * format - A printf-style format string, followed by the
chag 0:25b087be2989 113 * variables to use in formating the string.
chag 0:25b087be2989 114 */
chag 0:25b087be2989 115 int printf(const char* format, ...);
chag 0:25b087be2989 116
chag 0:25b087be2989 117 /* Function: scanf
chag 0:25b087be2989 118 * Read a formated string
chag 0:25b087be2989 119 *
chag 0:25b087be2989 120 * Variables:
chag 0:25b087be2989 121 * format - A scanf-style format string,
chag 0:25b087be2989 122 * followed by the pointers to variables to store the results.
chag 0:25b087be2989 123 */
chag 0:25b087be2989 124 int scanf(const char* format, ...);
chag 0:25b087be2989 125
chag 0:25b087be2989 126 #endif
chag 0:25b087be2989 127
chag 0:25b087be2989 128
chag 0:25b087be2989 129 #if 1
chag 0:25b087be2989 130 /** putc
chag 0:25b087be2989 131 * @param int c The byte to write.
chag 0:25b087be2989 132 */
chag 0:25b087be2989 133 int putc(int c);
chag 0:25b087be2989 134
chag 0:25b087be2989 135 /** puts
chag 0:25b087be2989 136 * @param char * The string to print.
chag 0:25b087be2989 137 */
chag 0:25b087be2989 138 //void puts(char *s);
chag 0:25b087be2989 139
chag 0:25b087be2989 140 /** getc
chag 0:25b087be2989 141 * @return int c The byte read or -1 if no bytes to read.
chag 0:25b087be2989 142 */
chag 0:25b087be2989 143 int getc(void);
chag 0:25b087be2989 144
chag 0:25b087be2989 145 /** gets
chag 0:25b087be2989 146 * Get a string. Note, this method blocks until size bytes are read.
chag 0:25b087be2989 147 * @param char *s where to place the incoming bytes.
chag 0:25b087be2989 148 * @param int size How many bytes to read.
chag 0:25b087be2989 149 * @return char * The value of *s passed in.
chag 0:25b087be2989 150 */
chag 0:25b087be2989 151 //char *gets(char *s, int size);
chag 0:25b087be2989 152
chag 0:25b087be2989 153 /** peek
chag 0:25b087be2989 154 * like getc() but does NOT remove the byte from the buffer.
chag 0:25b087be2989 155 * @see getc*(
chag 0:25b087be2989 156 */
chag 0:25b087be2989 157 //int peek(void);
chag 0:25b087be2989 158
chag 0:25b087be2989 159 #endif
chag 0:25b087be2989 160
chag 0:25b087be2989 161 /* Function: readable
chag 0:25b087be2989 162 * Determine if there is a character available to read
chag 0:25b087be2989 163 *
chag 0:25b087be2989 164 * Variables:
chag 0:25b087be2989 165 * returns - 1 if there is a character available to read, else 0
chag 0:25b087be2989 166 */
chag 0:25b087be2989 167 int readable();
chag 0:25b087be2989 168
chag 0:25b087be2989 169 /* Function: writeable
chag 0:25b087be2989 170 * Determine if there is space available to write a character
chag 0:25b087be2989 171 *
chag 0:25b087be2989 172 * Variables:
chag 0:25b087be2989 173 * returns - 1 if there is space to write a character, else 0
chag 0:25b087be2989 174 */
chag 0:25b087be2989 175 int writeable();
chag 0:25b087be2989 176
chag 0:25b087be2989 177 /* Function: attach
chag 0:25b087be2989 178 * Attach a function to call whenever a serial interrupt is generated
chag 0:25b087be2989 179 *
chag 0:25b087be2989 180 * Variables:
chag 0:25b087be2989 181 * fptr - A pointer to a void function, or 0 to set as none
chag 0:25b087be2989 182 * type - Which serial interrupt to attach the member function to (Seriall::RxIrq for receive, TxIrq for transmit buffer empty)
chag 0:25b087be2989 183 */
chag 0:25b087be2989 184 //void attach(void (*fptr)(void), IrqType type = RxIrq);
chag 0:25b087be2989 185
chag 0:25b087be2989 186 /* Function: attach
chag 0:25b087be2989 187 * Attach a member function to call whenever a serial interrupt is generated
chag 0:25b087be2989 188 *
chag 0:25b087be2989 189 * Variables:
chag 0:25b087be2989 190 * tptr - pointer to the object to call the member function on
chag 0:25b087be2989 191 * mptr - pointer to the member function to be called
chag 0:25b087be2989 192 * type - Which serial interrupt to attach the member function to (Seriall::RxIrq for receive, TxIrq for transmit buffer empty)
chag 0:25b087be2989 193 */
chag 0:25b087be2989 194 template<typename T>
chag 0:25b087be2989 195 void attach(T* tptr, void (T::*mptr)(void), IrqType type = RxIrq) {
chag 0:25b087be2989 196 if((mptr != NULL) && (tptr != NULL)) {
chag 0:25b087be2989 197 _irq[type].attach(tptr, mptr);
chag 0:25b087be2989 198 setup_interrupt(type);
chag 0:25b087be2989 199 }
chag 0:25b087be2989 200 }
chag 0:25b087be2989 201
chag 0:25b087be2989 202 #ifdef MBED_RPC
chag 0:25b087be2989 203 //virtual const struct rpc_method *get_rpc_methods();
chag 0:25b087be2989 204 static struct rpc_class *get_rpc_class();
chag 0:25b087be2989 205 #endif
chag 0:25b087be2989 206
chag 0:25b087be2989 207 protected:
chag 0:25b087be2989 208
chag 0:25b087be2989 209 //void setup_interrupt(IrqType type);
chag 0:25b087be2989 210 //void remove_interrupt(IrqType type);
chag 0:25b087be2989 211
chag 0:25b087be2989 212 //UARTName _uart;
chag 0:25b087be2989 213 //FunctionPointer _irq[2];
chag 0:25b087be2989 214 //int _uidx;
chag 0:25b087be2989 215
chag 0:25b087be2989 216 virtual int _putc(int c) { return putc(c); }
chag 0:25b087be2989 217 virtual int _getc() { return getc(); }
chag 0:25b087be2989 218
chag 0:25b087be2989 219
chag 0:25b087be2989 220
chag 0:25b087be2989 221 int get_rx_pin_status();
chag 0:25b087be2989 222 void set_tx_pin_high();
chag 0:25b087be2989 223 void set_tx_pin_low();
chag 0:25b087be2989 224 void idle();
chag 0:25b087be2989 225 void timer_set( int baud );
chag 0:25b087be2989 226 void set_timer_interrupt( void (*timer_isr)(void) );
chag 0:25b087be2989 227
chag 0:25b087be2989 228
chag 0:25b087be2989 229
chag 0:25b087be2989 230 void timer_isr( void );
chag 0:25b087be2989 231 void init_uart( void );
chag 0:25b087be2989 232 //char _getchar( void );
chag 0:25b087be2989 233 //void _putchar( char ch );
chag 0:25b087be2989 234 void flush_input_buffer( void );
chag 0:25b087be2989 235 char kbhit( void );
chag 0:25b087be2989 236 void turn_rx_on( void );
chag 0:25b087be2989 237 void turn_rx_off( void );
chag 0:25b087be2989 238
chag 0:25b087be2989 239
chag 0:25b087be2989 240 private:
chag 0:25b087be2989 241 DigitalOut *tx;
chag 0:25b087be2989 242 DigitalIn *rx;
chag 0:25b087be2989 243
chag 0:25b087be2989 244 Ticker ticker;
chag 0:25b087be2989 245
chag 0:25b087be2989 246 int baud_rate;
chag 0:25b087be2989 247
chag 0:25b087be2989 248 unsigned char inbuf[SOFTWARE_SERIAL_RX_BUFFER_SIZE];
chag 0:25b087be2989 249 unsigned char qin;
chag 0:25b087be2989 250 unsigned char qout;
chag 0:25b087be2989 251 char flag_rx_waiting_for_stop_bit;
chag 0:25b087be2989 252 bool flag_rx_off;
chag 0:25b087be2989 253 char rx_mask;
chag 0:25b087be2989 254 bool flag_rx_ready;
chag 0:25b087be2989 255 bool flag_tx_ready;
chag 0:25b087be2989 256 char timer_rx_ctr;
chag 0:25b087be2989 257 char timer_tx_ctr;
chag 0:25b087be2989 258 char bits_left_in_rx;
chag 0:25b087be2989 259 char bits_left_in_tx;
chag 0:25b087be2989 260 char rx_num_of_bits;
chag 0:25b087be2989 261 char tx_num_of_bits;
chag 0:25b087be2989 262 char internal_rx_buffer;
chag 0:25b087be2989 263 char internal_tx_buffer;
chag 0:25b087be2989 264 char user_tx_buffer;
chag 0:25b087be2989 265
chag 0:25b087be2989 266 };
chag 0:25b087be2989 267
chag 0:25b087be2989 268 } // namespace
chag 0:25b087be2989 269
chag 0:25b087be2989 270 using namespace SoftwareSerial;
chag 0:25b087be2989 271
chag 0:25b087be2989 272 #endif