Serial Half Duplex implementation
Dependents: 4dofRoboticArmAX12 2014-Mx64 2014-ax12-test 2014-mx64-test
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 00119 = 1); 00120 00121 /* Function: putc 00122 * Write a character 00123 * 00124 * Variables: 00125 * c - The character to write to the serial port 00126 */ 00127 int putc(int c); 00128 00129 /* Function: getc 00130 * Read a character 00131 * 00132 * Read a character from the serial port. This call will block 00133 * until a character is available. For testing if a character is 00134 * available for reading, see <readable>. 00135 * 00136 * Variables: 00137 * returns - The character read from the serial port 00138 */ 00139 int getc(); 00140 00141 /* Function: printf 00142 * Write a formated string 00143 * 00144 * Variables: 00145 * format - A printf-style format string, followed by the 00146 * variables to use in formating the string. 00147 */ 00148 int printf(const char* format, ...); 00149 00150 /* Function: scanf 00151 * Read a formated string 00152 * 00153 * Variables: 00154 * format - A scanf-style format string, 00155 * followed by the pointers to variables to store the results. 00156 */ 00157 int scanf(const char* format, ...); 00158 00159 /* Function: readable 00160 * Determine if there is a character available to read 00161 * 00162 * Variables: 00163 * returns - 1 if there is a character available to read, else 0 00164 */ 00165 int readable(); 00166 00167 /* Function: writeable 00168 * Determine if there is space available to write a character 00169 * 00170 * Variables: 00171 * returns - 1 if there is space to write a character, else 0 00172 */ 00173 int writeable(); 00174 00175 /* Function: attach 00176 * Attach a function to call whenever a serial interrupt is generated 00177 * 00178 * Variables: 00179 * fptr - A pointer to a void function, or 0 to set as none 00180 */ 00181 void attach(void (*fptr)(void)); 00182 00183 /* Function: attach 00184 * Attach a member function to call whenever a serial interrupt is generated 00185 * 00186 * Variables: 00187 * tptr - pointer to the object to call the member function on 00188 * mptr - pointer to the member function to be called 00189 */ 00190 template<typename T> 00191 void attach(T* tptr, void (T::*mptr)(void)); 00192 00193 #endif 00194 00195 protected: 00196 PinName _txpin; 00197 00198 virtual int _putc(int c); 00199 virtual int _getc(void); 00200 00201 }; // End class SerialHalfDuplex 00202 00203 } // End namespace 00204 00205 #endif 00206 00207 #endif
Generated on Wed Jul 13 2022 09:09:24 by 1.7.2