![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
Arm control program for Yozakura
SerialHalfDuplex/SerialHalfDuplex.h@0:6b3497b2f2ec, 2015-04-24 (annotated)
- Committer:
- masasin
- Date:
- Fri Apr 24 01:55:32 2015 +0000
- Revision:
- 0:6b3497b2f2ec
Initial commit
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
masasin | 0:6b3497b2f2ec | 1 | |
masasin | 0:6b3497b2f2ec | 2 | /* mbed Microcontroller Library |
masasin | 0:6b3497b2f2ec | 3 | * Copyright (c) 2006-2012 ARM Limited |
masasin | 0:6b3497b2f2ec | 4 | * |
masasin | 0:6b3497b2f2ec | 5 | * Permission is hereby granted, free of charge, to any person obtaining a copy |
masasin | 0:6b3497b2f2ec | 6 | * of this software and associated documentation files (the "Software"), to deal |
masasin | 0:6b3497b2f2ec | 7 | * in the Software without restriction, including without limitation the rights |
masasin | 0:6b3497b2f2ec | 8 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
masasin | 0:6b3497b2f2ec | 9 | * copies of the Software, and to permit persons to whom the Software is |
masasin | 0:6b3497b2f2ec | 10 | * furnished to do so, subject to the following conditions: |
masasin | 0:6b3497b2f2ec | 11 | * |
masasin | 0:6b3497b2f2ec | 12 | * The above copyright notice and this permission notice shall be included in |
masasin | 0:6b3497b2f2ec | 13 | * all copies or substantial portions of the Software. |
masasin | 0:6b3497b2f2ec | 14 | * |
masasin | 0:6b3497b2f2ec | 15 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
masasin | 0:6b3497b2f2ec | 16 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
masasin | 0:6b3497b2f2ec | 17 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
masasin | 0:6b3497b2f2ec | 18 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
masasin | 0:6b3497b2f2ec | 19 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
masasin | 0:6b3497b2f2ec | 20 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
masasin | 0:6b3497b2f2ec | 21 | * SOFTWARE. |
masasin | 0:6b3497b2f2ec | 22 | * |
masasin | 0:6b3497b2f2ec | 23 | * NOTE: This is an unsupported legacy untested library. |
masasin | 0:6b3497b2f2ec | 24 | */ |
masasin | 0:6b3497b2f2ec | 25 | #ifndef MBED_SERIALHALFDUPLEX_H |
masasin | 0:6b3497b2f2ec | 26 | #define MBED_SERIALHALFDUPLEX_H |
masasin | 0:6b3497b2f2ec | 27 | |
masasin | 0:6b3497b2f2ec | 28 | #include "device.h" |
masasin | 0:6b3497b2f2ec | 29 | |
masasin | 0:6b3497b2f2ec | 30 | #if DEVICE_SERIAL |
masasin | 0:6b3497b2f2ec | 31 | |
masasin | 0:6b3497b2f2ec | 32 | #include "Serial.h" |
masasin | 0:6b3497b2f2ec | 33 | #include "PinNames.h" |
masasin | 0:6b3497b2f2ec | 34 | #include "PeripheralNames.h" |
masasin | 0:6b3497b2f2ec | 35 | |
masasin | 0:6b3497b2f2ec | 36 | namespace mbed { |
masasin | 0:6b3497b2f2ec | 37 | |
masasin | 0:6b3497b2f2ec | 38 | /* Class: SerialHalfDuplex |
masasin | 0:6b3497b2f2ec | 39 | * A serial port (UART) for communication with other devices using |
masasin | 0:6b3497b2f2ec | 40 | * Half-Duplex, allowing transmit and receive on a single |
masasin | 0:6b3497b2f2ec | 41 | * shared transmit and receive line. Only one end should be transmitting |
masasin | 0:6b3497b2f2ec | 42 | * at a time. |
masasin | 0:6b3497b2f2ec | 43 | * |
masasin | 0:6b3497b2f2ec | 44 | * Both the tx and rx pin should be defined, and wired together. |
masasin | 0:6b3497b2f2ec | 45 | * This is in addition to them being wired to the other serial |
masasin | 0:6b3497b2f2ec | 46 | * device to allow both read and write functions to operate. |
masasin | 0:6b3497b2f2ec | 47 | * |
masasin | 0:6b3497b2f2ec | 48 | * Example: |
masasin | 0:6b3497b2f2ec | 49 | * > // Send a byte to a second HalfDuplex device, and read the response |
masasin | 0:6b3497b2f2ec | 50 | * > |
masasin | 0:6b3497b2f2ec | 51 | * > #include "mbed.h" |
masasin | 0:6b3497b2f2ec | 52 | * > |
masasin | 0:6b3497b2f2ec | 53 | * > // p9 and p10 should be wired together to form "a" |
masasin | 0:6b3497b2f2ec | 54 | * > // p28 and p27 should be wired together to form "b" |
masasin | 0:6b3497b2f2ec | 55 | * > // p9/p10 should be wired to p28/p27 as the Half Duplex connection |
masasin | 0:6b3497b2f2ec | 56 | * > |
masasin | 0:6b3497b2f2ec | 57 | * > SerialHalfDuplex a(p9, p10); |
masasin | 0:6b3497b2f2ec | 58 | * > SerialHalfDuplex b(p28, p27); |
masasin | 0:6b3497b2f2ec | 59 | * > |
masasin | 0:6b3497b2f2ec | 60 | * > void b_rx() { // second device response |
masasin | 0:6b3497b2f2ec | 61 | * > b.putc(b.getc() + 4); |
masasin | 0:6b3497b2f2ec | 62 | * > } |
masasin | 0:6b3497b2f2ec | 63 | * > |
masasin | 0:6b3497b2f2ec | 64 | * > int main() { |
masasin | 0:6b3497b2f2ec | 65 | * > b.attach(&b_rx); |
masasin | 0:6b3497b2f2ec | 66 | * > for(int c = 'A'; c < 'Z'; c++) { |
masasin | 0:6b3497b2f2ec | 67 | * > a.putc(c); |
masasin | 0:6b3497b2f2ec | 68 | * > printf("sent [%c]\n", c); |
masasin | 0:6b3497b2f2ec | 69 | * > wait(0.5); // b should respond |
masasin | 0:6b3497b2f2ec | 70 | * > if(a.readable()) { |
masasin | 0:6b3497b2f2ec | 71 | * > printf("received [%c]\n", a.getc()); |
masasin | 0:6b3497b2f2ec | 72 | * > } |
masasin | 0:6b3497b2f2ec | 73 | * > } |
masasin | 0:6b3497b2f2ec | 74 | * > } |
masasin | 0:6b3497b2f2ec | 75 | * |
masasin | 0:6b3497b2f2ec | 76 | * For Simplex and Full-Duplex Serial communication, see <Serial> |
masasin | 0:6b3497b2f2ec | 77 | */ |
masasin | 0:6b3497b2f2ec | 78 | class SerialHalfDuplex : public Serial { |
masasin | 0:6b3497b2f2ec | 79 | |
masasin | 0:6b3497b2f2ec | 80 | public: |
masasin | 0:6b3497b2f2ec | 81 | /* Constructor: SerialHalfDuplex |
masasin | 0:6b3497b2f2ec | 82 | * Create a half-duplex serial port, connected to the specified transmit |
masasin | 0:6b3497b2f2ec | 83 | * and receive pins. |
masasin | 0:6b3497b2f2ec | 84 | * |
masasin | 0:6b3497b2f2ec | 85 | * These pins should be wired together, as well as to the target device |
masasin | 0:6b3497b2f2ec | 86 | * |
masasin | 0:6b3497b2f2ec | 87 | * Variables: |
masasin | 0:6b3497b2f2ec | 88 | * tx - Transmit pin |
masasin | 0:6b3497b2f2ec | 89 | * rx - Receive pin |
masasin | 0:6b3497b2f2ec | 90 | */ |
masasin | 0:6b3497b2f2ec | 91 | SerialHalfDuplex(PinName tx, PinName rx, const char *name = NULL); |
masasin | 0:6b3497b2f2ec | 92 | |
masasin | 0:6b3497b2f2ec | 93 | #if 0 // Inherited from Serial class, for documentation |
masasin | 0:6b3497b2f2ec | 94 | /* Function: baud |
masasin | 0:6b3497b2f2ec | 95 | * Set the baud rate of the serial port |
masasin | 0:6b3497b2f2ec | 96 | * |
masasin | 0:6b3497b2f2ec | 97 | * Variables: |
masasin | 0:6b3497b2f2ec | 98 | * baudrate - The baudrate of the serial port (default = 9600). |
masasin | 0:6b3497b2f2ec | 99 | */ |
masasin | 0:6b3497b2f2ec | 100 | void baud(int baudrate); |
masasin | 0:6b3497b2f2ec | 101 | |
masasin | 0:6b3497b2f2ec | 102 | enum Parity { |
masasin | 0:6b3497b2f2ec | 103 | None = 0 |
masasin | 0:6b3497b2f2ec | 104 | , Odd |
masasin | 0:6b3497b2f2ec | 105 | , Even |
masasin | 0:6b3497b2f2ec | 106 | , Forced1 |
masasin | 0:6b3497b2f2ec | 107 | , Forced0 |
masasin | 0:6b3497b2f2ec | 108 | }; |
masasin | 0:6b3497b2f2ec | 109 | |
masasin | 0:6b3497b2f2ec | 110 | /* Function: format |
masasin | 0:6b3497b2f2ec | 111 | * Set the transmission format used by the Serial port |
masasin | 0:6b3497b2f2ec | 112 | * |
masasin | 0:6b3497b2f2ec | 113 | * Variables: |
masasin | 0:6b3497b2f2ec | 114 | * bits - The number of bits in a word (5-8; default = 8) |
masasin | 0:6b3497b2f2ec | 115 | * parity - The parity used (Serial::None, Serial::Odd, |
masasin | 0:6b3497b2f2ec | 116 | Serial::Even, Serial::Forced1, Serial::Forced0; default = Serial::None) |
masasin | 0:6b3497b2f2ec | 117 | * stop - The number of stop bits (1 or 2; default = 1) |
masasin | 0:6b3497b2f2ec | 118 | */ |
masasin | 0:6b3497b2f2ec | 119 | void format(int bits = 8, Parity parity = Serial::None, int stop_bits |
masasin | 0:6b3497b2f2ec | 120 | = 1); |
masasin | 0:6b3497b2f2ec | 121 | |
masasin | 0:6b3497b2f2ec | 122 | /* Function: putc |
masasin | 0:6b3497b2f2ec | 123 | * Write a character |
masasin | 0:6b3497b2f2ec | 124 | * |
masasin | 0:6b3497b2f2ec | 125 | * Variables: |
masasin | 0:6b3497b2f2ec | 126 | * c - The character to write to the serial port |
masasin | 0:6b3497b2f2ec | 127 | */ |
masasin | 0:6b3497b2f2ec | 128 | int putc(int c); |
masasin | 0:6b3497b2f2ec | 129 | |
masasin | 0:6b3497b2f2ec | 130 | /* Function: getc |
masasin | 0:6b3497b2f2ec | 131 | * Read a character |
masasin | 0:6b3497b2f2ec | 132 | * |
masasin | 0:6b3497b2f2ec | 133 | * Read a character from the serial port. This call will block |
masasin | 0:6b3497b2f2ec | 134 | * until a character is available. For testing if a character is |
masasin | 0:6b3497b2f2ec | 135 | * available for reading, see <readable>. |
masasin | 0:6b3497b2f2ec | 136 | * |
masasin | 0:6b3497b2f2ec | 137 | * Variables: |
masasin | 0:6b3497b2f2ec | 138 | * returns - The character read from the serial port |
masasin | 0:6b3497b2f2ec | 139 | */ |
masasin | 0:6b3497b2f2ec | 140 | int getc(); |
masasin | 0:6b3497b2f2ec | 141 | |
masasin | 0:6b3497b2f2ec | 142 | /* Function: printf |
masasin | 0:6b3497b2f2ec | 143 | * Write a formated string |
masasin | 0:6b3497b2f2ec | 144 | * |
masasin | 0:6b3497b2f2ec | 145 | * Variables: |
masasin | 0:6b3497b2f2ec | 146 | * format - A printf-style format string, followed by the |
masasin | 0:6b3497b2f2ec | 147 | * variables to use in formating the string. |
masasin | 0:6b3497b2f2ec | 148 | */ |
masasin | 0:6b3497b2f2ec | 149 | int printf(const char* format, ...); |
masasin | 0:6b3497b2f2ec | 150 | |
masasin | 0:6b3497b2f2ec | 151 | /* Function: scanf |
masasin | 0:6b3497b2f2ec | 152 | * Read a formated string |
masasin | 0:6b3497b2f2ec | 153 | * |
masasin | 0:6b3497b2f2ec | 154 | * Variables: |
masasin | 0:6b3497b2f2ec | 155 | * format - A scanf-style format string, |
masasin | 0:6b3497b2f2ec | 156 | * followed by the pointers to variables to store the results. |
masasin | 0:6b3497b2f2ec | 157 | */ |
masasin | 0:6b3497b2f2ec | 158 | int scanf(const char* format, ...); |
masasin | 0:6b3497b2f2ec | 159 | |
masasin | 0:6b3497b2f2ec | 160 | /* Function: readable |
masasin | 0:6b3497b2f2ec | 161 | * Determine if there is a character available to read |
masasin | 0:6b3497b2f2ec | 162 | * |
masasin | 0:6b3497b2f2ec | 163 | * Variables: |
masasin | 0:6b3497b2f2ec | 164 | * returns - 1 if there is a character available to read, else 0 |
masasin | 0:6b3497b2f2ec | 165 | */ |
masasin | 0:6b3497b2f2ec | 166 | int readable(); |
masasin | 0:6b3497b2f2ec | 167 | |
masasin | 0:6b3497b2f2ec | 168 | /* Function: writeable |
masasin | 0:6b3497b2f2ec | 169 | * Determine if there is space available to write a character |
masasin | 0:6b3497b2f2ec | 170 | * |
masasin | 0:6b3497b2f2ec | 171 | * Variables: |
masasin | 0:6b3497b2f2ec | 172 | * returns - 1 if there is space to write a character, else 0 |
masasin | 0:6b3497b2f2ec | 173 | */ |
masasin | 0:6b3497b2f2ec | 174 | int writeable(); |
masasin | 0:6b3497b2f2ec | 175 | |
masasin | 0:6b3497b2f2ec | 176 | /* Function: attach |
masasin | 0:6b3497b2f2ec | 177 | * Attach a function to call whenever a serial interrupt is generated |
masasin | 0:6b3497b2f2ec | 178 | * |
masasin | 0:6b3497b2f2ec | 179 | * Variables: |
masasin | 0:6b3497b2f2ec | 180 | * fptr - A pointer to a void function, or 0 to set as none |
masasin | 0:6b3497b2f2ec | 181 | */ |
masasin | 0:6b3497b2f2ec | 182 | void attach(void (*fptr)(void)); |
masasin | 0:6b3497b2f2ec | 183 | |
masasin | 0:6b3497b2f2ec | 184 | /* Function: attach |
masasin | 0:6b3497b2f2ec | 185 | * Attach a member function to call whenever a serial interrupt is generated |
masasin | 0:6b3497b2f2ec | 186 | * |
masasin | 0:6b3497b2f2ec | 187 | * Variables: |
masasin | 0:6b3497b2f2ec | 188 | * tptr - pointer to the object to call the member function on |
masasin | 0:6b3497b2f2ec | 189 | * mptr - pointer to the member function to be called |
masasin | 0:6b3497b2f2ec | 190 | */ |
masasin | 0:6b3497b2f2ec | 191 | template<typename T> |
masasin | 0:6b3497b2f2ec | 192 | void attach(T* tptr, void (T::*mptr)(void)); |
masasin | 0:6b3497b2f2ec | 193 | |
masasin | 0:6b3497b2f2ec | 194 | #endif |
masasin | 0:6b3497b2f2ec | 195 | |
masasin | 0:6b3497b2f2ec | 196 | protected: |
masasin | 0:6b3497b2f2ec | 197 | PinName _txpin; |
masasin | 0:6b3497b2f2ec | 198 | |
masasin | 0:6b3497b2f2ec | 199 | virtual int _putc(int c); |
masasin | 0:6b3497b2f2ec | 200 | virtual int _getc(void); |
masasin | 0:6b3497b2f2ec | 201 | |
masasin | 0:6b3497b2f2ec | 202 | }; // End class SerialHalfDuplex |
masasin | 0:6b3497b2f2ec | 203 | |
masasin | 0:6b3497b2f2ec | 204 | } // End namespace |
masasin | 0:6b3497b2f2ec | 205 | |
masasin | 0:6b3497b2f2ec | 206 | #endif |
masasin | 0:6b3497b2f2ec | 207 | |
masasin | 0:6b3497b2f2ec | 208 | #endif |