my customized lib

Committer:
DuyLionTran
Date:
Sun Nov 26 15:08:14 2017 +0000
Revision:
0:8094b249013c
Initial commit

Who changed what in which revision?

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