Software serial, for when you are out of serial pins

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

Committer:
SonyPony
Date:
Tue Apr 12 16:58:22 2016 +0000
Revision:
11:a0029614de72
Parent:
9:4e4617c4a441
Fix: set read_buffer to 0 in cunstructor, it caused that first received byte was not correct

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