Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
BufferedSerial.h
00001 00002 /** 00003 * @file BufferedSerial.h 00004 * @brief Software Buffer - Extends mbed Serial functionallity adding irq driven TX and RX 00005 * @author sam grove 00006 * @version 1.0 00007 * @see 00008 * 00009 * Copyright (c) 2013 00010 * 00011 * Licensed under the Apache License, Version 2.0 (the "License"); 00012 * you may not use this file except in compliance with the License. 00013 * You may obtain a copy of the License at 00014 * 00015 * http://www.apache.org/licenses/LICENSE-2.0 00016 * 00017 * Unless required by applicable law or agreed to in writing, software 00018 * distributed under the License is distributed on an "AS IS" BASIS, 00019 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 00020 * See the License for the specific language governing permissions and 00021 * limitations under the License. 00022 */ 00023 00024 #ifndef BUFFEREDSERIAL_H 00025 #define BUFFEREDSERIAL_H 00026 00027 #include "mbed.h" 00028 #include "MyBuffer.h" 00029 00030 /** A serial port (UART) for communication with other serial devices 00031 * 00032 * Can be used for Full Duplex communication, or Simplex by specifying 00033 * one pin as NC (Not Connected) 00034 * 00035 * Example: 00036 * @code 00037 * #include "mbed.h" 00038 * #include "BufferedSerial.h" 00039 * 00040 * BufferedSerial pc(USBTX, USBRX); 00041 * 00042 * int main() 00043 * { 00044 * while(1) 00045 * { 00046 * Timer s; 00047 * 00048 * s.start(); 00049 * pc.printf("Hello World - buffered\n"); 00050 * int buffered_time = s.read_us(); 00051 * wait(0.1f); // give time for the buffer to empty 00052 * 00053 * s.reset(); 00054 * printf("Hello World - blocking\n"); 00055 * int polled_time = s.read_us(); 00056 * s.stop(); 00057 * wait(0.1f); // give time for the buffer to empty 00058 * 00059 * pc.printf("printf buffered took %d us\n", buffered_time); 00060 * pc.printf("printf blocking took %d us\n", polled_time); 00061 * wait(0.5f); 00062 * } 00063 * } 00064 * @endcode 00065 */ 00066 00067 /** 00068 * @class BufferedSerial 00069 * @brief Software buffers and interrupt driven tx and rx for Serial 00070 */ 00071 class BufferedSerial : public RawSerial 00072 { 00073 private: 00074 MyBuffer <char> _rxbuf; 00075 MyBuffer <char> _txbuf; 00076 uint32_t _buf_size; 00077 uint32_t _tx_multiple; 00078 00079 void rxIrq(void); 00080 void txIrq(void); 00081 void prime(void); 00082 00083 Callback<void()> _cbs[2]; 00084 00085 public: 00086 /** Create a BufferedSerial port, connected to the specified transmit and receive pins 00087 * @param tx Transmit pin 00088 * @param rx Receive pin 00089 * @param buf_size printf() buffer size 00090 * @param tx_multiple amount of max printf() present in the internal ring buffer at one time 00091 * @param name optional name 00092 * @note Either tx or rx may be specified as NC if unused 00093 */ 00094 BufferedSerial(PinName tx, PinName rx, uint32_t buf_size = 256, uint32_t tx_multiple = 4,const char* name=NULL); 00095 00096 /** Destroy a BufferedSerial port 00097 */ 00098 virtual ~BufferedSerial(void); 00099 00100 /** Check on how many bytes are in the rx buffer 00101 * @return 1 if something exists, 0 otherwise 00102 */ 00103 virtual int readable(void); 00104 00105 /** Check to see if the tx buffer has room 00106 * @return 1 always has room and can overwrite previous content if too small / slow 00107 */ 00108 virtual int writeable(void); 00109 00110 /** Get a single byte from the BufferedSerial Port. 00111 * Should check readable() before calling this. 00112 * @return A byte that came in on the Serial Port 00113 */ 00114 virtual int getc(void); 00115 00116 /** Write a single byte to the BufferedSerial Port. 00117 * @param c The byte to write to the Serial Port 00118 * @return The byte that was written to the Serial Port Buffer 00119 */ 00120 virtual int putc(int c); 00121 00122 /** Write a string to the BufferedSerial Port. Must be NULL terminated 00123 * @param s The string to write to the Serial Port 00124 * @return The number of bytes written to the Serial Port Buffer 00125 */ 00126 virtual int puts(const char *s); 00127 00128 /** Write a formatted string to the BufferedSerial Port. 00129 * @param format The string + format specifiers to write to the Serial Port 00130 * @return The number of bytes written to the Serial Port Buffer 00131 */ 00132 virtual int printf(const char* format, ...); 00133 00134 /** Write data to the Buffered Serial Port 00135 * @param s A pointer to data to send 00136 * @param length The amount of data being pointed to 00137 * @return The number of bytes written to the Serial Port Buffer 00138 */ 00139 virtual ssize_t write(const void *s, std::size_t length); 00140 00141 /** Attach a function to call whenever a serial interrupt is generated 00142 * @param func A pointer to a void function, or 0 to set as none 00143 * @param type Which serial interrupt to attach the member function to (Serial::RxIrq for receive, TxIrq for transmit buffer empty) 00144 */ 00145 virtual void attach(Callback<void()> func, IrqType type=RxIrq); 00146 00147 /** Attach a member function to call whenever a serial interrupt is generated 00148 * @param obj pointer to the object to call the member function on 00149 * @param method pointer to the member function to call 00150 * @param type Which serial interrupt to attach the member function to (Serial::RxIrq for receive, TxIrq for transmit buffer empty) 00151 */ 00152 template <typename T> 00153 void attach(T *obj, void (T::*method)(), IrqType type=RxIrq) { 00154 attach(Callback<void()>(obj, method), type); 00155 } 00156 00157 /** Attach a member function to call whenever a serial interrupt is generated 00158 * @param obj pointer to the object to call the member function on 00159 * @param method pointer to the member function to call 00160 * @param type Which serial interrupt to attach the member function to (Serial::RxIrq for receive, TxIrq for transmit buffer empty) 00161 */ 00162 template <typename T> 00163 void attach(T *obj, void (*method)(T*), IrqType type=RxIrq) { 00164 attach(Callback<void()>(obj, method), type); 00165 } 00166 }; 00167 00168 #endif
Generated on Tue Jul 12 2022 15:14:29 by
