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 #if (MBED_MAJOR_VERSION == 5) && (MBED_MINOR_VERSION >= 2) 00031 #elif (MBED_MAJOR_VERSION == 2) && (MBED_PATCH_VERSION > 130) 00032 #else 00033 #error "BufferedSerial version 13 and newer requires use of Mbed OS 5.2.0 and newer or Mbed 2 version 130 and newer. Use BufferedSerial version 12 and older or upgrade the Mbed version. 00034 #endif 00035 00036 /** A serial port (UART) for communication with other serial devices 00037 * 00038 * Can be used for Full Duplex communication, or Simplex by specifying 00039 * one pin as NC (Not Connected) 00040 * 00041 * Example: 00042 * @code 00043 * #include "mbed.h" 00044 * #include "BufferedSerial.h" 00045 * 00046 * BufferedSerial pc(USBTX, USBRX); 00047 * 00048 * int main() 00049 * { 00050 * while(1) 00051 * { 00052 * Timer s; 00053 * 00054 * s.start(); 00055 * pc.printf("Hello World - buffered\n"); 00056 * int buffered_time = s.read_us(); 00057 * wait(0.1f); // give time for the buffer to empty 00058 * 00059 * s.reset(); 00060 * printf("Hello World - blocking\n"); 00061 * int polled_time = s.read_us(); 00062 * s.stop(); 00063 * wait(0.1f); // give time for the buffer to empty 00064 * 00065 * pc.printf("printf buffered took %d us\n", buffered_time); 00066 * pc.printf("printf blocking took %d us\n", polled_time); 00067 * wait(0.5f); 00068 * } 00069 * } 00070 * @endcode 00071 */ 00072 00073 /** 00074 * @class BufferedSerial 00075 * @brief Software buffers and interrupt driven tx and rx for Serial 00076 */ 00077 class BufferedSerial : public RawSerial 00078 { 00079 private: 00080 MyBuffer <char> _rxbuf; 00081 MyBuffer <char> _txbuf; 00082 uint32_t _buf_size; 00083 uint32_t _tx_multiple; 00084 00085 void rxIrq(void); 00086 void txIrq(void); 00087 void prime(void); 00088 00089 public: 00090 /** Create a BufferedSerial port, connected to the specified transmit and receive pins 00091 * @param tx Transmit pin 00092 * @param rx Receive pin 00093 * @param buf_size printf() buffer size 00094 * @param tx_multiple amount of max printf() present in the internal ring buffer at one time 00095 * @param name optional name 00096 * @note Either tx or rx may be specified as NC if unused 00097 */ 00098 BufferedSerial(PinName tx, PinName rx, uint32_t buf_size = 256, uint32_t tx_multiple = 4,const char* name=NULL); 00099 00100 /** Destroy a BufferedSerial port 00101 */ 00102 virtual ~BufferedSerial(void); 00103 00104 /** Check on how many bytes are in the rx buffer 00105 * @return 1 if something exists, 0 otherwise 00106 */ 00107 virtual int readable(void); 00108 00109 /** Check to see if the tx buffer has room 00110 * @return 1 always has room and can overwrite previous content if too small / slow 00111 */ 00112 virtual int writeable(void); 00113 00114 /** Get a single byte from the BufferedSerial Port. 00115 * Should check readable() before calling this. 00116 * @return A byte that came in on the Serial Port 00117 */ 00118 virtual int getc(void); 00119 00120 /** Write a single byte to the BufferedSerial Port. 00121 * @param c The byte to write to the Serial Port 00122 * @return The byte that was written to the Serial Port Buffer 00123 */ 00124 virtual int putc(int c); 00125 00126 /** Write a string to the BufferedSerial Port. Must be NULL terminated 00127 * @param s The string to write to the Serial Port 00128 * @return The number of bytes written to the Serial Port Buffer 00129 */ 00130 virtual int puts(const char *s); 00131 00132 /** Write a formatted string to the BufferedSerial Port. 00133 * @param format The string + format specifiers to write to the Serial Port 00134 * @return The number of bytes written to the Serial Port Buffer 00135 */ 00136 virtual int printf(const char* format, ...); 00137 00138 /** Write data to the Buffered Serial Port 00139 * @param s A pointer to data to send 00140 * @param length The amount of data being pointed to 00141 * @return The number of bytes written to the Serial Port Buffer 00142 */ 00143 virtual ssize_t write(const void *s, std::size_t length); 00144 }; 00145 00146 #endif
Generated on Thu Jul 14 2022 20:23:26 by
1.7.2