Software Serial lib
Dependents: BufferedSoftSerial
Fork of SoftSerial by
SoftSerial.h@3:7238e9bb74d2, 2014-04-26 (annotated)
- Committer:
- Sissors
- Date:
- Sat Apr 26 16:35:20 2014 +0000
- Revision:
- 3:7238e9bb74d2
- Parent:
- 2:9e01a38606b4
- Child:
- 4:c010265ed202
And RX to 19200 (todo: a fast mode which is not interrupt based)
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Sissors | 0:8edaa7abe724 | 1 | #ifndef SOFTSERIAL_H |
Sissors | 0:8edaa7abe724 | 2 | #define SOFTSERIAL_H |
Sissors | 0:8edaa7abe724 | 3 | |
Sissors | 0:8edaa7abe724 | 4 | #include "mbed.h" |
Sissors | 0:8edaa7abe724 | 5 | |
Sissors | 0:8edaa7abe724 | 6 | /** A software serial implementation |
Sissors | 0:8edaa7abe724 | 7 | * |
Sissors | 0:8edaa7abe724 | 8 | */ |
Sissors | 0:8edaa7abe724 | 9 | class SoftSerial: public Stream { |
Sissors | 0:8edaa7abe724 | 10 | |
Sissors | 0:8edaa7abe724 | 11 | public: |
Sissors | 0:8edaa7abe724 | 12 | /** |
Sissors | 0:8edaa7abe724 | 13 | * Constructor |
Sissors | 0:8edaa7abe724 | 14 | * |
Sissors | 0:8edaa7abe724 | 15 | * @param TX Name of the TX pin, NC for not connected |
Sissors | 0:8edaa7abe724 | 16 | * @param RX Name of the RX pin, NC for not connected, must be capable of being InterruptIn |
Sissors | 0:8edaa7abe724 | 17 | */ |
Sissors | 0:8edaa7abe724 | 18 | SoftSerial(PinName TX, PinName RX); |
Sissors | 0:8edaa7abe724 | 19 | |
Sissors | 0:8edaa7abe724 | 20 | /** Set the baud rate of the serial port |
Sissors | 0:8edaa7abe724 | 21 | * |
Sissors | 0:8edaa7abe724 | 22 | * @param baudrate The baudrate of the serial port (default = 9600). |
Sissors | 0:8edaa7abe724 | 23 | */ |
Sissors | 0:8edaa7abe724 | 24 | void baud(int baudrate); |
Sissors | 0:8edaa7abe724 | 25 | |
Sissors | 0:8edaa7abe724 | 26 | enum Parity { |
Sissors | 0:8edaa7abe724 | 27 | None = 0, |
Sissors | 0:8edaa7abe724 | 28 | Odd, |
Sissors | 0:8edaa7abe724 | 29 | Even, |
Sissors | 0:8edaa7abe724 | 30 | Forced1, |
Sissors | 0:8edaa7abe724 | 31 | Forced0 |
Sissors | 0:8edaa7abe724 | 32 | }; |
Sissors | 0:8edaa7abe724 | 33 | |
Sissors | 0:8edaa7abe724 | 34 | enum IrqType { |
Sissors | 0:8edaa7abe724 | 35 | RxIrq = 0, |
Sissors | 0:8edaa7abe724 | 36 | TxIrq |
Sissors | 0:8edaa7abe724 | 37 | }; |
Sissors | 0:8edaa7abe724 | 38 | |
Sissors | 0:8edaa7abe724 | 39 | /** Set the transmission format used by the serial port |
Sissors | 0:8edaa7abe724 | 40 | * |
Sissors | 0:8edaa7abe724 | 41 | * @param bits The number of bits in a word (default = 8) |
Sissors | 0:8edaa7abe724 | 42 | * @param parity The parity used (SerialBase::None, SerialBase::Odd, SerialBase::Even, SerialBase::Forced1, SerialBase::Forced0; default = SerialBase::None) |
Sissors | 0:8edaa7abe724 | 43 | * @param stop The number of stop bits (default = 1) |
Sissors | 0:8edaa7abe724 | 44 | */ |
Sissors | 0:8edaa7abe724 | 45 | void format(int bits=8, Parity parity=SoftSerial::None, int stop_bits=1); |
Sissors | 0:8edaa7abe724 | 46 | |
Sissors | 0:8edaa7abe724 | 47 | /** Determine if there is a character available to read |
Sissors | 0:8edaa7abe724 | 48 | * |
Sissors | 0:8edaa7abe724 | 49 | * @returns |
Sissors | 0:8edaa7abe724 | 50 | * 1 if there is a character available to read, |
Sissors | 0:8edaa7abe724 | 51 | * 0 otherwise |
Sissors | 0:8edaa7abe724 | 52 | */ |
Sissors | 0:8edaa7abe724 | 53 | int readable(); |
Sissors | 0:8edaa7abe724 | 54 | |
Sissors | 0:8edaa7abe724 | 55 | /** Determine if there is space available to write a character |
Sissors | 0:8edaa7abe724 | 56 | * |
Sissors | 0:8edaa7abe724 | 57 | * @returns |
Sissors | 0:8edaa7abe724 | 58 | * 1 if there is space to write a character, |
Sissors | 0:8edaa7abe724 | 59 | * 0 otherwise |
Sissors | 0:8edaa7abe724 | 60 | */ |
Sissors | 0:8edaa7abe724 | 61 | int writeable(); |
Sissors | 0:8edaa7abe724 | 62 | |
Sissors | 0:8edaa7abe724 | 63 | /** Attach a function to call whenever a serial interrupt is generated |
Sissors | 0:8edaa7abe724 | 64 | * |
Sissors | 0:8edaa7abe724 | 65 | * @param fptr A pointer to a void function, or 0 to set as none |
Sissors | 0:8edaa7abe724 | 66 | * @param type Which serial interrupt to attach the member function to (Seriall::RxIrq for receive, TxIrq for transmit buffer empty) |
Sissors | 0:8edaa7abe724 | 67 | */ |
Sissors | 0:8edaa7abe724 | 68 | void attach(void (*fptr)(void), IrqType type=RxIrq); |
Sissors | 0:8edaa7abe724 | 69 | |
Sissors | 0:8edaa7abe724 | 70 | /** Attach a member function to call whenever a serial interrupt is generated |
Sissors | 0:8edaa7abe724 | 71 | * |
Sissors | 0:8edaa7abe724 | 72 | * @param tptr pointer to the object to call the member function on |
Sissors | 0:8edaa7abe724 | 73 | * @param mptr pointer to the member function to be called |
Sissors | 0:8edaa7abe724 | 74 | * @param type Which serial interrupt to attach the member function to (Seriall::RxIrq for receive, TxIrq for transmit buffer empty) |
Sissors | 0:8edaa7abe724 | 75 | */ |
Sissors | 0:8edaa7abe724 | 76 | template<typename T> |
Sissors | 0:8edaa7abe724 | 77 | void attach(T* tptr, void (T::*mptr)(void), IrqType type=RxIrq); |
Sissors | 0:8edaa7abe724 | 78 | |
Sissors | 0:8edaa7abe724 | 79 | /** Generate a break condition on the serial line |
Sissors | 0:8edaa7abe724 | 80 | */ |
Sissors | 0:8edaa7abe724 | 81 | void send_break(); |
Sissors | 0:8edaa7abe724 | 82 | |
Sissors | 0:8edaa7abe724 | 83 | protected: |
Sissors | 0:8edaa7abe724 | 84 | DigitalOut *tx; |
Sissors | 0:8edaa7abe724 | 85 | InterruptIn *rx; |
Sissors | 0:8edaa7abe724 | 86 | |
Sissors | 0:8edaa7abe724 | 87 | bool tx_en, rx_en; |
Sissors | 0:8edaa7abe724 | 88 | int bit_period; |
Sissors | 2:9e01a38606b4 | 89 | int _bits, _stop_bits, _total_bits; |
Sissors | 0:8edaa7abe724 | 90 | Parity _parity; |
Sissors | 0:8edaa7abe724 | 91 | |
Sissors | 0:8edaa7abe724 | 92 | void rx_gpio_irq_handler(void); |
Sissors | 1:f8b4b764ace7 | 93 | void rx_handler(void); |
Sissors | 1:f8b4b764ace7 | 94 | int read_buffer, rx_bit; |
Sissors | 1:f8b4b764ace7 | 95 | volatile int out_buffer; |
Sissors | 1:f8b4b764ace7 | 96 | volatile bool out_valid; |
Sissors | 1:f8b4b764ace7 | 97 | bool rx_error; |
Sissors | 1:f8b4b764ace7 | 98 | Timeout rxout; |
Sissors | 3:7238e9bb74d2 | 99 | Ticker rxticker; |
Sissors | 0:8edaa7abe724 | 100 | |
Sissors | 0:8edaa7abe724 | 101 | //tx |
Sissors | 0:8edaa7abe724 | 102 | void tx_handler(void); |
Sissors | 2:9e01a38606b4 | 103 | void prepare_tx(int c); |
Sissors | 1:f8b4b764ace7 | 104 | Timeout txout; |
Sissors | 0:8edaa7abe724 | 105 | int _char; |
Sissors | 0:8edaa7abe724 | 106 | volatile int tx_bit; |
Sissors | 0:8edaa7abe724 | 107 | |
Sissors | 0:8edaa7abe724 | 108 | |
Sissors | 0:8edaa7abe724 | 109 | |
Sissors | 0:8edaa7abe724 | 110 | virtual int _getc(); |
Sissors | 0:8edaa7abe724 | 111 | virtual int _putc(int c); |
Sissors | 0:8edaa7abe724 | 112 | }; |
Sissors | 0:8edaa7abe724 | 113 | |
Sissors | 0:8edaa7abe724 | 114 | |
Sissors | 0:8edaa7abe724 | 115 | #endif |
Sissors | 0:8edaa7abe724 | 116 |