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