Fork of SerialHalfDuplex library

Dependents:   PR_RobotArm lighthouse_2

Fork of SerialHalfDuplex by Aimen Al-Refai

Committer:
jah128
Date:
Thu Feb 16 23:58:49 2017 +0000
Revision:
3:ff073057f373
Parent:
2:5ecc72a47df0
Added comments

Who changed what in which revision?

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