Ram Gandikota
/
ABCD
A metronome using the FRDM K64F board
easy-connect/esp8266-driver/ESP8266/ATParser/BufferedSerial/BufferedSerial.h@0:a7a43371b306, 2017-05-14 (annotated)
- Committer:
- ram54288
- Date:
- Sun May 14 18:40:18 2017 +0000
- Revision:
- 0:a7a43371b306
Initial commit
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
ram54288 | 0:a7a43371b306 | 1 | |
ram54288 | 0:a7a43371b306 | 2 | /** |
ram54288 | 0:a7a43371b306 | 3 | * @file BufferedSerial.h |
ram54288 | 0:a7a43371b306 | 4 | * @brief Software Buffer - Extends mbed Serial functionallity adding irq driven TX and RX |
ram54288 | 0:a7a43371b306 | 5 | * @author sam grove |
ram54288 | 0:a7a43371b306 | 6 | * @version 1.0 |
ram54288 | 0:a7a43371b306 | 7 | * @see |
ram54288 | 0:a7a43371b306 | 8 | * |
ram54288 | 0:a7a43371b306 | 9 | * Copyright (c) 2013 |
ram54288 | 0:a7a43371b306 | 10 | * |
ram54288 | 0:a7a43371b306 | 11 | * Licensed under the Apache License, Version 2.0 (the "License"); |
ram54288 | 0:a7a43371b306 | 12 | * you may not use this file except in compliance with the License. |
ram54288 | 0:a7a43371b306 | 13 | * You may obtain a copy of the License at |
ram54288 | 0:a7a43371b306 | 14 | * |
ram54288 | 0:a7a43371b306 | 15 | * http://www.apache.org/licenses/LICENSE-2.0 |
ram54288 | 0:a7a43371b306 | 16 | * |
ram54288 | 0:a7a43371b306 | 17 | * Unless required by applicable law or agreed to in writing, software |
ram54288 | 0:a7a43371b306 | 18 | * distributed under the License is distributed on an "AS IS" BASIS, |
ram54288 | 0:a7a43371b306 | 19 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
ram54288 | 0:a7a43371b306 | 20 | * See the License for the specific language governing permissions and |
ram54288 | 0:a7a43371b306 | 21 | * limitations under the License. |
ram54288 | 0:a7a43371b306 | 22 | */ |
ram54288 | 0:a7a43371b306 | 23 | |
ram54288 | 0:a7a43371b306 | 24 | #ifndef BUFFEREDSERIAL_H |
ram54288 | 0:a7a43371b306 | 25 | #define BUFFEREDSERIAL_H |
ram54288 | 0:a7a43371b306 | 26 | |
ram54288 | 0:a7a43371b306 | 27 | #include "mbed.h" |
ram54288 | 0:a7a43371b306 | 28 | #include "MyBuffer.h" |
ram54288 | 0:a7a43371b306 | 29 | |
ram54288 | 0:a7a43371b306 | 30 | /** A serial port (UART) for communication with other serial devices |
ram54288 | 0:a7a43371b306 | 31 | * |
ram54288 | 0:a7a43371b306 | 32 | * Can be used for Full Duplex communication, or Simplex by specifying |
ram54288 | 0:a7a43371b306 | 33 | * one pin as NC (Not Connected) |
ram54288 | 0:a7a43371b306 | 34 | * |
ram54288 | 0:a7a43371b306 | 35 | * Example: |
ram54288 | 0:a7a43371b306 | 36 | * @code |
ram54288 | 0:a7a43371b306 | 37 | * #include "mbed.h" |
ram54288 | 0:a7a43371b306 | 38 | * #include "BufferedSerial.h" |
ram54288 | 0:a7a43371b306 | 39 | * |
ram54288 | 0:a7a43371b306 | 40 | * BufferedSerial pc(USBTX, USBRX); |
ram54288 | 0:a7a43371b306 | 41 | * |
ram54288 | 0:a7a43371b306 | 42 | * int main() |
ram54288 | 0:a7a43371b306 | 43 | * { |
ram54288 | 0:a7a43371b306 | 44 | * while(1) |
ram54288 | 0:a7a43371b306 | 45 | * { |
ram54288 | 0:a7a43371b306 | 46 | * Timer s; |
ram54288 | 0:a7a43371b306 | 47 | * |
ram54288 | 0:a7a43371b306 | 48 | * s.start(); |
ram54288 | 0:a7a43371b306 | 49 | * pc.printf("Hello World - buffered\n"); |
ram54288 | 0:a7a43371b306 | 50 | * int buffered_time = s.read_us(); |
ram54288 | 0:a7a43371b306 | 51 | * wait(0.1f); // give time for the buffer to empty |
ram54288 | 0:a7a43371b306 | 52 | * |
ram54288 | 0:a7a43371b306 | 53 | * s.reset(); |
ram54288 | 0:a7a43371b306 | 54 | * printf("Hello World - blocking\n"); |
ram54288 | 0:a7a43371b306 | 55 | * int polled_time = s.read_us(); |
ram54288 | 0:a7a43371b306 | 56 | * s.stop(); |
ram54288 | 0:a7a43371b306 | 57 | * wait(0.1f); // give time for the buffer to empty |
ram54288 | 0:a7a43371b306 | 58 | * |
ram54288 | 0:a7a43371b306 | 59 | * pc.printf("printf buffered took %d us\n", buffered_time); |
ram54288 | 0:a7a43371b306 | 60 | * pc.printf("printf blocking took %d us\n", polled_time); |
ram54288 | 0:a7a43371b306 | 61 | * wait(0.5f); |
ram54288 | 0:a7a43371b306 | 62 | * } |
ram54288 | 0:a7a43371b306 | 63 | * } |
ram54288 | 0:a7a43371b306 | 64 | * @endcode |
ram54288 | 0:a7a43371b306 | 65 | */ |
ram54288 | 0:a7a43371b306 | 66 | |
ram54288 | 0:a7a43371b306 | 67 | /** |
ram54288 | 0:a7a43371b306 | 68 | * @class BufferedSerial |
ram54288 | 0:a7a43371b306 | 69 | * @brief Software buffers and interrupt driven tx and rx for Serial |
ram54288 | 0:a7a43371b306 | 70 | */ |
ram54288 | 0:a7a43371b306 | 71 | class BufferedSerial : public RawSerial |
ram54288 | 0:a7a43371b306 | 72 | { |
ram54288 | 0:a7a43371b306 | 73 | private: |
ram54288 | 0:a7a43371b306 | 74 | MyBuffer <char> _rxbuf; |
ram54288 | 0:a7a43371b306 | 75 | MyBuffer <char> _txbuf; |
ram54288 | 0:a7a43371b306 | 76 | uint32_t _buf_size; |
ram54288 | 0:a7a43371b306 | 77 | uint32_t _tx_multiple; |
ram54288 | 0:a7a43371b306 | 78 | |
ram54288 | 0:a7a43371b306 | 79 | void rxIrq(void); |
ram54288 | 0:a7a43371b306 | 80 | void txIrq(void); |
ram54288 | 0:a7a43371b306 | 81 | void prime(void); |
ram54288 | 0:a7a43371b306 | 82 | |
ram54288 | 0:a7a43371b306 | 83 | Callback<void()> _cbs[2]; |
ram54288 | 0:a7a43371b306 | 84 | |
ram54288 | 0:a7a43371b306 | 85 | public: |
ram54288 | 0:a7a43371b306 | 86 | /** Create a BufferedSerial port, connected to the specified transmit and receive pins |
ram54288 | 0:a7a43371b306 | 87 | * @param tx Transmit pin |
ram54288 | 0:a7a43371b306 | 88 | * @param rx Receive pin |
ram54288 | 0:a7a43371b306 | 89 | * @param buf_size printf() buffer size |
ram54288 | 0:a7a43371b306 | 90 | * @param tx_multiple amount of max printf() present in the internal ring buffer at one time |
ram54288 | 0:a7a43371b306 | 91 | * @param name optional name |
ram54288 | 0:a7a43371b306 | 92 | * @note Either tx or rx may be specified as NC if unused |
ram54288 | 0:a7a43371b306 | 93 | */ |
ram54288 | 0:a7a43371b306 | 94 | BufferedSerial(PinName tx, PinName rx, uint32_t buf_size = 256, uint32_t tx_multiple = 4,const char* name=NULL); |
ram54288 | 0:a7a43371b306 | 95 | |
ram54288 | 0:a7a43371b306 | 96 | /** Destroy a BufferedSerial port |
ram54288 | 0:a7a43371b306 | 97 | */ |
ram54288 | 0:a7a43371b306 | 98 | virtual ~BufferedSerial(void); |
ram54288 | 0:a7a43371b306 | 99 | |
ram54288 | 0:a7a43371b306 | 100 | /** Check on how many bytes are in the rx buffer |
ram54288 | 0:a7a43371b306 | 101 | * @return 1 if something exists, 0 otherwise |
ram54288 | 0:a7a43371b306 | 102 | */ |
ram54288 | 0:a7a43371b306 | 103 | virtual int readable(void); |
ram54288 | 0:a7a43371b306 | 104 | |
ram54288 | 0:a7a43371b306 | 105 | /** Check to see if the tx buffer has room |
ram54288 | 0:a7a43371b306 | 106 | * @return 1 always has room and can overwrite previous content if too small / slow |
ram54288 | 0:a7a43371b306 | 107 | */ |
ram54288 | 0:a7a43371b306 | 108 | virtual int writeable(void); |
ram54288 | 0:a7a43371b306 | 109 | |
ram54288 | 0:a7a43371b306 | 110 | /** Get a single byte from the BufferedSerial Port. |
ram54288 | 0:a7a43371b306 | 111 | * Should check readable() before calling this. |
ram54288 | 0:a7a43371b306 | 112 | * @return A byte that came in on the Serial Port |
ram54288 | 0:a7a43371b306 | 113 | */ |
ram54288 | 0:a7a43371b306 | 114 | virtual int getc(void); |
ram54288 | 0:a7a43371b306 | 115 | |
ram54288 | 0:a7a43371b306 | 116 | /** Write a single byte to the BufferedSerial Port. |
ram54288 | 0:a7a43371b306 | 117 | * @param c The byte to write to the Serial Port |
ram54288 | 0:a7a43371b306 | 118 | * @return The byte that was written to the Serial Port Buffer |
ram54288 | 0:a7a43371b306 | 119 | */ |
ram54288 | 0:a7a43371b306 | 120 | virtual int putc(int c); |
ram54288 | 0:a7a43371b306 | 121 | |
ram54288 | 0:a7a43371b306 | 122 | /** Write a string to the BufferedSerial Port. Must be NULL terminated |
ram54288 | 0:a7a43371b306 | 123 | * @param s The string to write to the Serial Port |
ram54288 | 0:a7a43371b306 | 124 | * @return The number of bytes written to the Serial Port Buffer |
ram54288 | 0:a7a43371b306 | 125 | */ |
ram54288 | 0:a7a43371b306 | 126 | virtual int puts(const char *s); |
ram54288 | 0:a7a43371b306 | 127 | |
ram54288 | 0:a7a43371b306 | 128 | /** Write a formatted string to the BufferedSerial Port. |
ram54288 | 0:a7a43371b306 | 129 | * @param format The string + format specifiers to write to the Serial Port |
ram54288 | 0:a7a43371b306 | 130 | * @return The number of bytes written to the Serial Port Buffer |
ram54288 | 0:a7a43371b306 | 131 | */ |
ram54288 | 0:a7a43371b306 | 132 | virtual int printf(const char* format, ...); |
ram54288 | 0:a7a43371b306 | 133 | |
ram54288 | 0:a7a43371b306 | 134 | /** Write data to the Buffered Serial Port |
ram54288 | 0:a7a43371b306 | 135 | * @param s A pointer to data to send |
ram54288 | 0:a7a43371b306 | 136 | * @param length The amount of data being pointed to |
ram54288 | 0:a7a43371b306 | 137 | * @return The number of bytes written to the Serial Port Buffer |
ram54288 | 0:a7a43371b306 | 138 | */ |
ram54288 | 0:a7a43371b306 | 139 | virtual ssize_t write(const void *s, std::size_t length); |
ram54288 | 0:a7a43371b306 | 140 | |
ram54288 | 0:a7a43371b306 | 141 | /** Attach a function to call whenever a serial interrupt is generated |
ram54288 | 0:a7a43371b306 | 142 | * @param func A pointer to a void function, or 0 to set as none |
ram54288 | 0:a7a43371b306 | 143 | * @param type Which serial interrupt to attach the member function to (Serial::RxIrq for receive, TxIrq for transmit buffer empty) |
ram54288 | 0:a7a43371b306 | 144 | */ |
ram54288 | 0:a7a43371b306 | 145 | virtual void attach(Callback<void()> func, IrqType type=RxIrq); |
ram54288 | 0:a7a43371b306 | 146 | |
ram54288 | 0:a7a43371b306 | 147 | /** Attach a member function to call whenever a serial interrupt is generated |
ram54288 | 0:a7a43371b306 | 148 | * @param obj pointer to the object to call the member function on |
ram54288 | 0:a7a43371b306 | 149 | * @param method pointer to the member function to call |
ram54288 | 0:a7a43371b306 | 150 | * @param type Which serial interrupt to attach the member function to (Serial::RxIrq for receive, TxIrq for transmit buffer empty) |
ram54288 | 0:a7a43371b306 | 151 | */ |
ram54288 | 0:a7a43371b306 | 152 | template <typename T> |
ram54288 | 0:a7a43371b306 | 153 | void attach(T *obj, void (T::*method)(), IrqType type=RxIrq) { |
ram54288 | 0:a7a43371b306 | 154 | attach(Callback<void()>(obj, method), type); |
ram54288 | 0:a7a43371b306 | 155 | } |
ram54288 | 0:a7a43371b306 | 156 | |
ram54288 | 0:a7a43371b306 | 157 | /** Attach a member function to call whenever a serial interrupt is generated |
ram54288 | 0:a7a43371b306 | 158 | * @param obj pointer to the object to call the member function on |
ram54288 | 0:a7a43371b306 | 159 | * @param method pointer to the member function to call |
ram54288 | 0:a7a43371b306 | 160 | * @param type Which serial interrupt to attach the member function to (Serial::RxIrq for receive, TxIrq for transmit buffer empty) |
ram54288 | 0:a7a43371b306 | 161 | */ |
ram54288 | 0:a7a43371b306 | 162 | template <typename T> |
ram54288 | 0:a7a43371b306 | 163 | void attach(T *obj, void (*method)(T*), IrqType type=RxIrq) { |
ram54288 | 0:a7a43371b306 | 164 | attach(Callback<void()>(obj, method), type); |
ram54288 | 0:a7a43371b306 | 165 | } |
ram54288 | 0:a7a43371b306 | 166 | }; |
ram54288 | 0:a7a43371b306 | 167 | |
ram54288 | 0:a7a43371b306 | 168 | #endif |