mayuresh bharmoria / Mbed OS mbed-os-example-wifi
Committer:
mayur098
Date:
Thu Jun 21 17:50:21 2018 +0000
Revision:
0:8f8e8f3cbd1c
first commit;

Who changed what in which revision?

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