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