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.
SerialHalfDuplex.h
00001 00002 /* mbed Microcontroller Library 00003 * Copyright (c) 2006-2012 ARM Limited 00004 * 00005 * Permission is hereby granted, free of charge, to any person obtaining a copy 00006 * of this software and associated documentation files (the "Software"), to deal 00007 * in the Software without restriction, including without limitation the rights 00008 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 00009 * copies of the Software, and to permit persons to whom the Software is 00010 * furnished to do so, subject to the following conditions: 00011 * 00012 * The above copyright notice and this permission notice shall be included in 00013 * all copies or substantial portions of the Software. 00014 * 00015 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 00016 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 00017 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 00018 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 00019 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 00020 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 00021 * SOFTWARE. 00022 * 00023 * NOTE: This is an unsupported legacy untested library. 00024 */ 00025 #ifndef MBED_SERIALHALFDUPLEX_H 00026 #define MBED_SERIALHALFDUPLEX_H 00027 00028 #include "device.h" 00029 00030 #if DEVICE_SERIAL 00031 00032 #include "Serial.h" 00033 #include "PinNames.h" 00034 #include "PeripheralNames.h" 00035 00036 namespace mbed { 00037 00038 /* Class: SerialHalfDuplex 00039 * A serial port (UART) for communication with other devices using 00040 * Half-Duplex, allowing transmit and receive on a single 00041 * shared transmit and receive line. Only one end should be transmitting 00042 * at a time. 00043 * 00044 * Both the tx and rx pin should be defined, and wired together. 00045 * This is in addition to them being wired to the other serial 00046 * device to allow both read and write functions to operate. 00047 * 00048 * Example: 00049 * > // Send a byte to a second HalfDuplex device, and read the response 00050 * > 00051 * > #include "mbed.h" 00052 * > 00053 * > // p9 and p10 should be wired together to form "a" 00054 * > // p28 and p27 should be wired together to form "b" 00055 * > // p9/p10 should be wired to p28/p27 as the Half Duplex connection 00056 * > 00057 * > SerialHalfDuplex a(p9, p10); 00058 * > SerialHalfDuplex b(p28, p27); 00059 * > 00060 * > void b_rx() { // second device response 00061 * > b.putc(b.getc() + 4); 00062 * > } 00063 * > 00064 * > int main() { 00065 * > b.attach(&b_rx); 00066 * > for(int c = 'A'; c < 'Z'; c++) { 00067 * > a.putc(c); 00068 * > printf("sent [%c]\n", c); 00069 * > wait(0.5); // b should respond 00070 * > if(a.readable()) { 00071 * > printf("received [%c]\n", a.getc()); 00072 * > } 00073 * > } 00074 * > } 00075 * 00076 * For Simplex and Full-Duplex Serial communication, see <Serial> 00077 */ 00078 class SerialHalfDuplex : public Serial { 00079 00080 public: 00081 /* Constructor: SerialHalfDuplex 00082 * Create a half-duplex serial port, connected to the specified transmit 00083 * and receive pins. 00084 * 00085 * These pins should be wired together, as well as to the target device 00086 * 00087 * Variables: 00088 * tx - Transmit pin 00089 * rx - Receive pin 00090 */ 00091 SerialHalfDuplex(PinName tx, PinName rx, const char *name = NULL); 00092 00093 #if 0 // Inherited from Serial class, for documentation 00094 /* Function: baud 00095 * Set the baud rate of the serial port 00096 * 00097 * Variables: 00098 * baudrate - The baudrate of the serial port (default = 9600). 00099 */ 00100 void baud(int baudrate); 00101 00102 enum Parity { 00103 None = 0 00104 , Odd 00105 , Even 00106 , Forced1 00107 , Forced0 00108 }; 00109 00110 /* Function: format 00111 * Set the transmission format used by the Serial port 00112 * 00113 * Variables: 00114 * bits - The number of bits in a word (5-8; default = 8) 00115 * parity - The parity used (Serial::None, Serial::Odd, 00116 Serial::Even, Serial::Forced1, Serial::Forced0; default = Serial::None) 00117 * stop - The number of stop bits (1 or 2; default = 1) 00118 */ 00119 void format(int bits = 8, Parity parity = Serial::None, int stop_bits 00120 = 1); 00121 00122 /* Function: putc 00123 * Write a character 00124 * 00125 * Variables: 00126 * c - The character to write to the serial port 00127 */ 00128 int putc(int c); 00129 00130 /* Function: getc 00131 * Read a character 00132 * 00133 * Read a character from the serial port. This call will block 00134 * until a character is available. For testing if a character is 00135 * available for reading, see <readable>. 00136 * 00137 * Variables: 00138 * returns - The character read from the serial port 00139 */ 00140 int getc(); 00141 00142 /* Function: printf 00143 * Write a formated string 00144 * 00145 * Variables: 00146 * format - A printf-style format string, followed by the 00147 * variables to use in formating the string. 00148 */ 00149 int printf(const char* format, ...); 00150 00151 /* Function: scanf 00152 * Read a formated string 00153 * 00154 * Variables: 00155 * format - A scanf-style format string, 00156 * followed by the pointers to variables to store the results. 00157 */ 00158 int scanf(const char* format, ...); 00159 00160 /* Function: readable 00161 * Determine if there is a character available to read 00162 * 00163 * Variables: 00164 * returns - 1 if there is a character available to read, else 0 00165 */ 00166 int readable(); 00167 00168 /* Function: writeable 00169 * Determine if there is space available to write a character 00170 * 00171 * Variables: 00172 * returns - 1 if there is space to write a character, else 0 00173 */ 00174 int writeable(); 00175 00176 /* Function: attach 00177 * Attach a function to call whenever a serial interrupt is generated 00178 * 00179 * Variables: 00180 * fptr - A pointer to a void function, or 0 to set as none 00181 */ 00182 void attach(void (*fptr)(void)); 00183 00184 /* Function: attach 00185 * Attach a member function to call whenever a serial interrupt is generated 00186 * 00187 * Variables: 00188 * tptr - pointer to the object to call the member function on 00189 * mptr - pointer to the member function to be called 00190 */ 00191 template<typename T> 00192 void attach(T* tptr, void (T::*mptr)(void)); 00193 00194 #endif 00195 00196 protected: 00197 PinName _txpin; 00198 00199 virtual int _putc(int c); 00200 virtual int _getc(void); 00201 00202 }; // End class SerialHalfDuplex 00203 00204 } // End namespace 00205 00206 #endif 00207 00208 #endif
Generated on Wed Jul 20 2022 02:00:07 by
1.7.2