Software serial, for when you are out of serial pins

Dependents:   BufferedSoftSerial neurGAI_Seeed_BLUETOOTH LPC-SD-35 ESP-WROOM-02_test ... more

Committer:
Sissors
Date:
Sat Apr 26 20:04:49 2014 +0000
Revision:
4:c010265ed202
Parent:
3:7238e9bb74d2
Child:
5:acfd0329f648
IRQs added

Who changed what in which revision?

UserRevisionLine numberNew 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 4:c010265ed202 68 void attach(void (*fptr)(void), IrqType type=RxIrq) {
Sissors 4:c010265ed202 69 fpointer[type].attach(fptr);
Sissors 4:c010265ed202 70 }
Sissors 0:8edaa7abe724 71
Sissors 0:8edaa7abe724 72 /** Attach a member function to call whenever a serial interrupt is generated
Sissors 0:8edaa7abe724 73 *
Sissors 0:8edaa7abe724 74 * @param tptr pointer to the object to call the member function on
Sissors 0:8edaa7abe724 75 * @param mptr pointer to the member function to be called
Sissors 0:8edaa7abe724 76 * @param type Which serial interrupt to attach the member function to (Seriall::RxIrq for receive, TxIrq for transmit buffer empty)
Sissors 0:8edaa7abe724 77 */
Sissors 0:8edaa7abe724 78 template<typename T>
Sissors 4:c010265ed202 79 void attach(T* tptr, void (T::*mptr)(void), IrqType type=RxIrq) {
Sissors 4:c010265ed202 80 fpointer[type].attach(tptr, mptr);
Sissors 4:c010265ed202 81 }
Sissors 0:8edaa7abe724 82
Sissors 0:8edaa7abe724 83 /** Generate a break condition on the serial line
Sissors 0:8edaa7abe724 84 */
Sissors 0:8edaa7abe724 85 void send_break();
Sissors 0:8edaa7abe724 86
Sissors 0:8edaa7abe724 87 protected:
Sissors 0:8edaa7abe724 88 DigitalOut *tx;
Sissors 0:8edaa7abe724 89 InterruptIn *rx;
Sissors 0:8edaa7abe724 90
Sissors 0:8edaa7abe724 91 bool tx_en, rx_en;
Sissors 0:8edaa7abe724 92 int bit_period;
Sissors 2:9e01a38606b4 93 int _bits, _stop_bits, _total_bits;
Sissors 0:8edaa7abe724 94 Parity _parity;
Sissors 0:8edaa7abe724 95
Sissors 4:c010265ed202 96 FunctionPointer fpointer[2];
Sissors 4:c010265ed202 97
Sissors 4:c010265ed202 98 //rx
Sissors 0:8edaa7abe724 99 void rx_gpio_irq_handler(void);
Sissors 1:f8b4b764ace7 100 void rx_handler(void);
Sissors 1:f8b4b764ace7 101 int read_buffer, rx_bit;
Sissors 1:f8b4b764ace7 102 volatile int out_buffer;
Sissors 1:f8b4b764ace7 103 volatile bool out_valid;
Sissors 1:f8b4b764ace7 104 bool rx_error;
Sissors 1:f8b4b764ace7 105 Timeout rxout;
Sissors 3:7238e9bb74d2 106 Ticker rxticker;
Sissors 0:8edaa7abe724 107
Sissors 0:8edaa7abe724 108 //tx
Sissors 0:8edaa7abe724 109 void tx_handler(void);
Sissors 2:9e01a38606b4 110 void prepare_tx(int c);
Sissors 1:f8b4b764ace7 111 Timeout txout;
Sissors 0:8edaa7abe724 112 int _char;
Sissors 0:8edaa7abe724 113 volatile int tx_bit;
Sissors 0:8edaa7abe724 114
Sissors 0:8edaa7abe724 115
Sissors 0:8edaa7abe724 116
Sissors 0:8edaa7abe724 117 virtual int _getc();
Sissors 0:8edaa7abe724 118 virtual int _putc(int c);
Sissors 0:8edaa7abe724 119 };
Sissors 0:8edaa7abe724 120
Sissors 0:8edaa7abe724 121
Sissors 0:8edaa7abe724 122 #endif
Sissors 0:8edaa7abe724 123