Fork of my MQTTGateway

Dependencies:   mbed-http

Committer:
vpcola
Date:
Sat Apr 08 14:45:51 2017 +0000
Revision:
0:f1d3878b8dd9
Initial commit

Who changed what in which revision?

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