Not work original Lib on F446RE & L432KC . Change usage prime() & setNext() for both TX and RX function due to unexpected behavior (maybe os TimerEvent function problem)
Dependents: BufferedSoftSerial
SoftSerial.h@13:6399b30798a5, 2020-05-12 (annotated)
- Committer:
- kenjiArai
- Date:
- Tue May 12 05:27:07 2020 +0000
- Revision:
- 13:6399b30798a5
- Parent:
- 12:cd58d03b8559
expand for several CPU's.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
kenjiArai | 12:cd58d03b8559 | 1 | /* |
kenjiArai | 12:cd58d03b8559 | 2 | Original Library by Erik- & SonyPony |
kenjiArai | 12:cd58d03b8559 | 3 | https://os.mbed.com/users/Sissors/code/SoftSerial/ |
kenjiArai | 12:cd58d03b8559 | 4 | |
kenjiArai | 13:6399b30798a5 | 5 | Modified by K.Arai / JH1PJL May 12th, 2020 |
kenjiArai | 12:cd58d03b8559 | 6 | |
kenjiArai | 12:cd58d03b8559 | 7 | modified functions |
kenjiArai | 12:cd58d03b8559 | 8 | tx_handler() |
kenjiArai | 12:cd58d03b8559 | 9 | prepare_tx() |
kenjiArai | 12:cd58d03b8559 | 10 | prime() |
kenjiArai | 12:cd58d03b8559 | 11 | rx_gpio_irq_handler() and others |
kenjiArai | 12:cd58d03b8559 | 12 | */ |
kenjiArai | 12:cd58d03b8559 | 13 | |
Sissors | 0:8edaa7abe724 | 14 | #ifndef SOFTSERIAL_H |
Sissors | 0:8edaa7abe724 | 15 | #define SOFTSERIAL_H |
Sissors | 0:8edaa7abe724 | 16 | |
Sissors | 0:8edaa7abe724 | 17 | #include "mbed.h" |
Sissors | 6:517082212c00 | 18 | #include "SoftSerial_Ticker.h" |
kenjiArai | 12:cd58d03b8559 | 19 | |
kenjiArai | 12:cd58d03b8559 | 20 | #define DEBUG_TIMING 0 // 0=no debug, 1=tx, 2=rx |
kenjiArai | 12:cd58d03b8559 | 21 | |
kenjiArai | 12:cd58d03b8559 | 22 | #if (MBED_MAJOR_VERSION == 2) |
kenjiArai | 12:cd58d03b8559 | 23 | # define YIELD {;} |
kenjiArai | 12:cd58d03b8559 | 24 | #elif (MBED_MAJOR_VERSION == 5) |
kenjiArai | 12:cd58d03b8559 | 25 | # define YIELD {ThisThread::yield();} |
kenjiArai | 12:cd58d03b8559 | 26 | #endif |
kenjiArai | 12:cd58d03b8559 | 27 | |
Sissors | 0:8edaa7abe724 | 28 | /** A software serial implementation |
Sissors | 0:8edaa7abe724 | 29 | * |
Sissors | 0:8edaa7abe724 | 30 | */ |
kenjiArai | 12:cd58d03b8559 | 31 | class SoftSerial: public Stream |
kenjiArai | 12:cd58d03b8559 | 32 | { |
Sissors | 0:8edaa7abe724 | 33 | |
Sissors | 0:8edaa7abe724 | 34 | public: |
Sissors | 0:8edaa7abe724 | 35 | /** |
Sissors | 0:8edaa7abe724 | 36 | * Constructor |
Sissors | 0:8edaa7abe724 | 37 | * |
Sissors | 0:8edaa7abe724 | 38 | * @param TX Name of the TX pin, NC for not connected |
kenjiArai | 12:cd58d03b8559 | 39 | * @param RX Name of the RX pin, NC for not connected, |
kenjiArai | 12:cd58d03b8559 | 40 | * must be capable of being InterruptIn |
Sissors | 5:acfd0329f648 | 41 | * @param name Name of the connection |
Sissors | 0:8edaa7abe724 | 42 | */ |
Sissors | 5:acfd0329f648 | 43 | SoftSerial(PinName TX, PinName RX, const char* name = NULL); |
Sissors | 9:4e4617c4a441 | 44 | virtual ~SoftSerial(); |
kenjiArai | 12:cd58d03b8559 | 45 | |
Sissors | 0:8edaa7abe724 | 46 | /** Set the baud rate of the serial port |
Sissors | 0:8edaa7abe724 | 47 | * |
Sissors | 0:8edaa7abe724 | 48 | * @param baudrate The baudrate of the serial port (default = 9600). |
Sissors | 0:8edaa7abe724 | 49 | */ |
Sissors | 0:8edaa7abe724 | 50 | void baud(int baudrate); |
Sissors | 0:8edaa7abe724 | 51 | |
Sissors | 0:8edaa7abe724 | 52 | enum Parity { |
Sissors | 0:8edaa7abe724 | 53 | None = 0, |
Sissors | 0:8edaa7abe724 | 54 | Odd, |
Sissors | 0:8edaa7abe724 | 55 | Even, |
Sissors | 0:8edaa7abe724 | 56 | Forced1, |
Sissors | 0:8edaa7abe724 | 57 | Forced0 |
Sissors | 0:8edaa7abe724 | 58 | }; |
Sissors | 0:8edaa7abe724 | 59 | |
Sissors | 0:8edaa7abe724 | 60 | enum IrqType { |
Sissors | 0:8edaa7abe724 | 61 | RxIrq = 0, |
Sissors | 0:8edaa7abe724 | 62 | TxIrq |
Sissors | 0:8edaa7abe724 | 63 | }; |
Sissors | 0:8edaa7abe724 | 64 | |
Sissors | 0:8edaa7abe724 | 65 | /** Set the transmission format used by the serial port |
Sissors | 0:8edaa7abe724 | 66 | * |
Sissors | 0:8edaa7abe724 | 67 | * @param bits The number of bits in a word (default = 8) |
kenjiArai | 12:cd58d03b8559 | 68 | * @param parity The parity used |
kenjiArai | 12:cd58d03b8559 | 69 | * (SoftSerial::None, SoftSerial::Odd, SoftSerial::Even, |
kenjiArai | 12:cd58d03b8559 | 70 | * SoftSerial::Forced1, SoftSerial::Forced0; default = SoftSerial::None) |
Sissors | 0:8edaa7abe724 | 71 | * @param stop The number of stop bits (default = 1) |
Sissors | 0:8edaa7abe724 | 72 | */ |
Sissors | 0:8edaa7abe724 | 73 | void format(int bits=8, Parity parity=SoftSerial::None, int stop_bits=1); |
Sissors | 0:8edaa7abe724 | 74 | |
Sissors | 0:8edaa7abe724 | 75 | /** Determine if there is a character available to read |
Sissors | 0:8edaa7abe724 | 76 | * |
Sissors | 0:8edaa7abe724 | 77 | * @returns |
Sissors | 0:8edaa7abe724 | 78 | * 1 if there is a character available to read, |
Sissors | 0:8edaa7abe724 | 79 | * 0 otherwise |
Sissors | 0:8edaa7abe724 | 80 | */ |
Sissors | 0:8edaa7abe724 | 81 | int readable(); |
Sissors | 0:8edaa7abe724 | 82 | |
Sissors | 0:8edaa7abe724 | 83 | /** Determine if there is space available to write a character |
Sissors | 0:8edaa7abe724 | 84 | * |
Sissors | 0:8edaa7abe724 | 85 | * @returns |
Sissors | 0:8edaa7abe724 | 86 | * 1 if there is space to write a character, |
Sissors | 0:8edaa7abe724 | 87 | * 0 otherwise |
Sissors | 0:8edaa7abe724 | 88 | */ |
Sissors | 0:8edaa7abe724 | 89 | int writeable(); |
Sissors | 0:8edaa7abe724 | 90 | |
Sissors | 0:8edaa7abe724 | 91 | /** Attach a function to call whenever a serial interrupt is generated |
Sissors | 0:8edaa7abe724 | 92 | * |
Sissors | 0:8edaa7abe724 | 93 | * @param fptr A pointer to a void function, or 0 to set as none |
kenjiArai | 12:cd58d03b8559 | 94 | * @param type Which serial interrupt to attach the member function |
kenjiArai | 12:cd58d03b8559 | 95 | * to (Seriall::RxIrq for receive, TxIrq for transmit buffer empty) |
Sissors | 0:8edaa7abe724 | 96 | */ |
kenjiArai | 12:cd58d03b8559 | 97 | void attach(void (*fptr)(void), IrqType type=RxIrq) |
kenjiArai | 12:cd58d03b8559 | 98 | { |
kenjiArai | 12:cd58d03b8559 | 99 | fpointer[type] = Callback<void()>(fptr); |
Sissors | 4:c010265ed202 | 100 | } |
Sissors | 0:8edaa7abe724 | 101 | |
kenjiArai | 12:cd58d03b8559 | 102 | /** Attach a member function to call |
kenjiArai | 12:cd58d03b8559 | 103 | * whenever a serial interrupt is generated |
Sissors | 0:8edaa7abe724 | 104 | * |
Sissors | 0:8edaa7abe724 | 105 | * @param tptr pointer to the object to call the member function on |
Sissors | 0:8edaa7abe724 | 106 | * @param mptr pointer to the member function to be called |
kenjiArai | 12:cd58d03b8559 | 107 | * @param type Which serial interrupt to attach the member function |
kenjiArai | 12:cd58d03b8559 | 108 | * to (Seriall::RxIrq for receive, TxIrq for transmit buffer empty) |
Sissors | 0:8edaa7abe724 | 109 | */ |
Sissors | 0:8edaa7abe724 | 110 | template<typename T> |
kenjiArai | 12:cd58d03b8559 | 111 | void attach(T* tptr, void (T::*mptr)(void), IrqType type=RxIrq) |
kenjiArai | 12:cd58d03b8559 | 112 | { |
kenjiArai | 12:cd58d03b8559 | 113 | fpointer[type] = Callback<void()>(tptr, mptr); |
Sissors | 4:c010265ed202 | 114 | } |
Sissors | 0:8edaa7abe724 | 115 | |
Sissors | 0:8edaa7abe724 | 116 | /** Generate a break condition on the serial line |
Sissors | 0:8edaa7abe724 | 117 | */ |
Sissors | 0:8edaa7abe724 | 118 | void send_break(); |
Sissors | 0:8edaa7abe724 | 119 | |
Sissors | 0:8edaa7abe724 | 120 | protected: |
Sissors | 0:8edaa7abe724 | 121 | DigitalOut *tx; |
Sissors | 0:8edaa7abe724 | 122 | InterruptIn *rx; |
kenjiArai | 12:cd58d03b8559 | 123 | |
Sissors | 0:8edaa7abe724 | 124 | bool tx_en, rx_en; |
Sissors | 0:8edaa7abe724 | 125 | int bit_period; |
Sissors | 2:9e01a38606b4 | 126 | int _bits, _stop_bits, _total_bits; |
Sissors | 0:8edaa7abe724 | 127 | Parity _parity; |
kenjiArai | 12:cd58d03b8559 | 128 | |
kenjiArai | 12:cd58d03b8559 | 129 | Callback<void()> fpointer[2]; |
kenjiArai | 12:cd58d03b8559 | 130 | |
Sissors | 4:c010265ed202 | 131 | //rx |
Sissors | 0:8edaa7abe724 | 132 | void rx_gpio_irq_handler(void); |
Sissors | 1:f8b4b764ace7 | 133 | void rx_handler(void); |
Sissors | 1:f8b4b764ace7 | 134 | int read_buffer, rx_bit; |
Sissors | 1:f8b4b764ace7 | 135 | volatile int out_buffer; |
Sissors | 1:f8b4b764ace7 | 136 | volatile bool out_valid; |
Sissors | 1:f8b4b764ace7 | 137 | bool rx_error; |
Sissors | 6:517082212c00 | 138 | FlexTicker rxticker; |
kenjiArai | 12:cd58d03b8559 | 139 | |
kenjiArai | 12:cd58d03b8559 | 140 | bool rx_1st; |
kenjiArai | 12:cd58d03b8559 | 141 | uint32_t rx_st_time; |
kenjiArai | 12:cd58d03b8559 | 142 | |
Sissors | 0:8edaa7abe724 | 143 | //tx |
Sissors | 0:8edaa7abe724 | 144 | void tx_handler(void); |
Sissors | 2:9e01a38606b4 | 145 | void prepare_tx(int c); |
Sissors | 6:517082212c00 | 146 | FlexTicker txticker; |
Sissors | 0:8edaa7abe724 | 147 | int _char; |
Sissors | 0:8edaa7abe724 | 148 | volatile int tx_bit; |
kenjiArai | 12:cd58d03b8559 | 149 | |
Sissors | 0:8edaa7abe724 | 150 | virtual int _getc(); |
Sissors | 0:8edaa7abe724 | 151 | virtual int _putc(int c); |
Sissors | 0:8edaa7abe724 | 152 | }; |
Sissors | 0:8edaa7abe724 | 153 | |
Sissors | 0:8edaa7abe724 | 154 | #endif |