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:
Sun Apr 27 08:33:17 2014 +0000
Revision:
6:517082212c00
Parent:
5:acfd0329f648
Child:
9:4e4617c4a441
Added custom ticker lib to allow for baudrate up to 57600

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 6:517082212c00 5 #include "SoftSerial_Ticker.h"
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 5:acfd0329f648 17 * @param name Name of the connection
Sissors 0:8edaa7abe724 18 */
Sissors 5:acfd0329f648 19 SoftSerial(PinName TX, PinName RX, const char* name = NULL);
Sissors 0:8edaa7abe724 20
Sissors 0:8edaa7abe724 21 /** Set the baud rate of the serial port
Sissors 0:8edaa7abe724 22 *
Sissors 0:8edaa7abe724 23 * @param baudrate The baudrate of the serial port (default = 9600).
Sissors 0:8edaa7abe724 24 */
Sissors 0:8edaa7abe724 25 void baud(int baudrate);
Sissors 0:8edaa7abe724 26
Sissors 0:8edaa7abe724 27 enum Parity {
Sissors 0:8edaa7abe724 28 None = 0,
Sissors 0:8edaa7abe724 29 Odd,
Sissors 0:8edaa7abe724 30 Even,
Sissors 0:8edaa7abe724 31 Forced1,
Sissors 0:8edaa7abe724 32 Forced0
Sissors 0:8edaa7abe724 33 };
Sissors 0:8edaa7abe724 34
Sissors 0:8edaa7abe724 35 enum IrqType {
Sissors 0:8edaa7abe724 36 RxIrq = 0,
Sissors 0:8edaa7abe724 37 TxIrq
Sissors 0:8edaa7abe724 38 };
Sissors 0:8edaa7abe724 39
Sissors 0:8edaa7abe724 40 /** Set the transmission format used by the serial port
Sissors 0:8edaa7abe724 41 *
Sissors 0:8edaa7abe724 42 * @param bits The number of bits in a word (default = 8)
Sissors 0:8edaa7abe724 43 * @param parity The parity used (SerialBase::None, SerialBase::Odd, SerialBase::Even, SerialBase::Forced1, SerialBase::Forced0; default = SerialBase::None)
Sissors 0:8edaa7abe724 44 * @param stop The number of stop bits (default = 1)
Sissors 0:8edaa7abe724 45 */
Sissors 0:8edaa7abe724 46 void format(int bits=8, Parity parity=SoftSerial::None, int stop_bits=1);
Sissors 0:8edaa7abe724 47
Sissors 0:8edaa7abe724 48 /** Determine if there is a character available to read
Sissors 0:8edaa7abe724 49 *
Sissors 0:8edaa7abe724 50 * @returns
Sissors 0:8edaa7abe724 51 * 1 if there is a character available to read,
Sissors 0:8edaa7abe724 52 * 0 otherwise
Sissors 0:8edaa7abe724 53 */
Sissors 0:8edaa7abe724 54 int readable();
Sissors 0:8edaa7abe724 55
Sissors 0:8edaa7abe724 56 /** Determine if there is space available to write a character
Sissors 0:8edaa7abe724 57 *
Sissors 0:8edaa7abe724 58 * @returns
Sissors 0:8edaa7abe724 59 * 1 if there is space to write a character,
Sissors 0:8edaa7abe724 60 * 0 otherwise
Sissors 0:8edaa7abe724 61 */
Sissors 0:8edaa7abe724 62 int writeable();
Sissors 0:8edaa7abe724 63
Sissors 0:8edaa7abe724 64 /** Attach a function to call whenever a serial interrupt is generated
Sissors 0:8edaa7abe724 65 *
Sissors 0:8edaa7abe724 66 * @param fptr A pointer to a void function, or 0 to set as none
Sissors 0:8edaa7abe724 67 * @param type Which serial interrupt to attach the member function to (Seriall::RxIrq for receive, TxIrq for transmit buffer empty)
Sissors 0:8edaa7abe724 68 */
Sissors 4:c010265ed202 69 void attach(void (*fptr)(void), IrqType type=RxIrq) {
Sissors 4:c010265ed202 70 fpointer[type].attach(fptr);
Sissors 4:c010265ed202 71 }
Sissors 0:8edaa7abe724 72
Sissors 0:8edaa7abe724 73 /** Attach a member function to call whenever a serial interrupt is generated
Sissors 0:8edaa7abe724 74 *
Sissors 0:8edaa7abe724 75 * @param tptr pointer to the object to call the member function on
Sissors 0:8edaa7abe724 76 * @param mptr pointer to the member function to be called
Sissors 0:8edaa7abe724 77 * @param type Which serial interrupt to attach the member function to (Seriall::RxIrq for receive, TxIrq for transmit buffer empty)
Sissors 0:8edaa7abe724 78 */
Sissors 0:8edaa7abe724 79 template<typename T>
Sissors 4:c010265ed202 80 void attach(T* tptr, void (T::*mptr)(void), IrqType type=RxIrq) {
Sissors 4:c010265ed202 81 fpointer[type].attach(tptr, mptr);
Sissors 4:c010265ed202 82 }
Sissors 0:8edaa7abe724 83
Sissors 0:8edaa7abe724 84 /** Generate a break condition on the serial line
Sissors 0:8edaa7abe724 85 */
Sissors 0:8edaa7abe724 86 void send_break();
Sissors 0:8edaa7abe724 87
Sissors 0:8edaa7abe724 88 protected:
Sissors 0:8edaa7abe724 89 DigitalOut *tx;
Sissors 0:8edaa7abe724 90 InterruptIn *rx;
Sissors 0:8edaa7abe724 91
Sissors 0:8edaa7abe724 92 bool tx_en, rx_en;
Sissors 0:8edaa7abe724 93 int bit_period;
Sissors 2:9e01a38606b4 94 int _bits, _stop_bits, _total_bits;
Sissors 0:8edaa7abe724 95 Parity _parity;
Sissors 0:8edaa7abe724 96
Sissors 4:c010265ed202 97 FunctionPointer fpointer[2];
Sissors 4:c010265ed202 98
Sissors 4:c010265ed202 99 //rx
Sissors 0:8edaa7abe724 100 void rx_gpio_irq_handler(void);
Sissors 1:f8b4b764ace7 101 void rx_handler(void);
Sissors 1:f8b4b764ace7 102 int read_buffer, rx_bit;
Sissors 1:f8b4b764ace7 103 volatile int out_buffer;
Sissors 1:f8b4b764ace7 104 volatile bool out_valid;
Sissors 1:f8b4b764ace7 105 bool rx_error;
Sissors 6:517082212c00 106 FlexTicker 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 6:517082212c00 111 FlexTicker txticker;
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