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