Soundharrajan

Fork of mbed by mbed official

Committer:
mrsoundhar
Date:
Sun Jun 12 16:45:04 2016 +0000
Revision:
92:f7fcbaa5f1b5
Parent:
30:3991a86798e3
Soundharrajan

Who changed what in which revision?

UserRevisionLine numberNew contents of line
simon 20:029aa53d7323 1 /* mbed Microcontroller Library - SerialHalfDuplex
emilmont 27:7110ebee3484 2 * Copyright (c) 2010-2011 ARM Limited. All rights reserved.
simon 20:029aa53d7323 3 */
simon 20:029aa53d7323 4
simon 20:029aa53d7323 5 #ifndef MBED_SERIALHALFDUPLEX_H
simon 20:029aa53d7323 6 #define MBED_SERIALHALFDUPLEX_H
simon 20:029aa53d7323 7
emilmont 27:7110ebee3484 8 #include "device.h"
emilmont 27:7110ebee3484 9
emilmont 30:3991a86798e3 10 #if DEVICE_SERIAL
emilmont 27:7110ebee3484 11
simon 20:029aa53d7323 12 #include "Serial.h"
simon 20:029aa53d7323 13 #include "PinNames.h"
simon 20:029aa53d7323 14 #include "PeripheralNames.h"
simon 20:029aa53d7323 15
simon 20:029aa53d7323 16 namespace mbed {
simon 20:029aa53d7323 17
simon 20:029aa53d7323 18 /* Class: SerialHalfDuplex
emilmont 27:7110ebee3484 19 * A serial port (UART) for communication with other devices using
emilmont 27:7110ebee3484 20 * Half-Duplex, allowing transmit and receive on a single
emilmont 27:7110ebee3484 21 * shared transmit and receive line. Only one end should be transmitting
emilmont 27:7110ebee3484 22 * at a time.
emilmont 27:7110ebee3484 23 *
emilmont 27:7110ebee3484 24 * Both the tx and rx pin should be defined, and wired together.
emilmont 27:7110ebee3484 25 * This is in addition to them being wired to the other serial
emilmont 27:7110ebee3484 26 * device to allow both read and write functions to operate.
simon 20:029aa53d7323 27 *
simon 20:029aa53d7323 28 * Example:
emilmont 27:7110ebee3484 29 * > // Send a byte to a second HalfDuplex device, and read the response
simon 20:029aa53d7323 30 * >
simon 20:029aa53d7323 31 * > #include "mbed.h"
simon 20:029aa53d7323 32 * >
emilmont 27:7110ebee3484 33 * > // p9 and p10 should be wired together to form "a"
emilmont 27:7110ebee3484 34 * > // p28 and p27 should be wired together to form "b"
emilmont 27:7110ebee3484 35 * > // p9/p10 should be wired to p28/p27 as the Half Duplex connection
simon 20:029aa53d7323 36 * >
emilmont 27:7110ebee3484 37 * > SerialHalfDuplex a(p9, p10);
emilmont 27:7110ebee3484 38 * > SerialHalfDuplex b(p28, p27);
emilmont 27:7110ebee3484 39 * >
emilmont 27:7110ebee3484 40 * > void b_rx() { // second device response
emilmont 27:7110ebee3484 41 * > b.putc(b.getc() + 4);
emilmont 27:7110ebee3484 42 * > }
emilmont 27:7110ebee3484 43 * >
simon 20:029aa53d7323 44 * > int main() {
emilmont 27:7110ebee3484 45 * > b.attach(&b_rx);
emilmont 27:7110ebee3484 46 * > for(int c = 'A'; c < 'Z'; c++) {
emilmont 27:7110ebee3484 47 * > a.putc(c);
emilmont 27:7110ebee3484 48 * > printf("sent [%c]\n", c);
emilmont 27:7110ebee3484 49 * > wait(0.5); // b should respond
emilmont 27:7110ebee3484 50 * > if(a.readable()) {
emilmont 27:7110ebee3484 51 * > printf("received [%c]\n", a.getc());
emilmont 27:7110ebee3484 52 * > }
emilmont 27:7110ebee3484 53 * > }
simon 20:029aa53d7323 54 * > }
emilmont 27:7110ebee3484 55 *
emilmont 27:7110ebee3484 56 * For Simplex and Full-Duplex Serial communication, see <Serial>
simon 20:029aa53d7323 57 */
simon 20:029aa53d7323 58 class SerialHalfDuplex : public Serial {
simon 20:029aa53d7323 59
simon 20:029aa53d7323 60 public:
simon 20:029aa53d7323 61 /* Constructor: SerialHalfDuplex
simon 20:029aa53d7323 62 * Create a half-duplex serial port, connected to the specified transmit
simon 20:029aa53d7323 63 * and receive pins.
simon 20:029aa53d7323 64 *
emilmont 27:7110ebee3484 65 * These pins should be wired together, as well as to the target device
emilmont 27:7110ebee3484 66 *
simon 20:029aa53d7323 67 * Variables:
simon 20:029aa53d7323 68 * tx - Transmit pin
simon 20:029aa53d7323 69 * rx - Receive pin
simon 20:029aa53d7323 70 */
simon 20:029aa53d7323 71 SerialHalfDuplex(PinName tx, PinName rx, const char *name = NULL);
simon 20:029aa53d7323 72
simon 20:029aa53d7323 73 #if 0 // Inherited from Serial class, for documentation
simon 20:029aa53d7323 74 /* Function: baud
simon 20:029aa53d7323 75 * Set the baud rate of the serial port
simon 20:029aa53d7323 76 *
simon 20:029aa53d7323 77 * Variables:
simon 20:029aa53d7323 78 * baudrate - The baudrate of the serial port (default = 9600).
simon 20:029aa53d7323 79 */
simon 20:029aa53d7323 80 void baud(int baudrate);
simon 20:029aa53d7323 81
simon 20:029aa53d7323 82 enum Parity {
simon 20:029aa53d7323 83 None = 0
simon 20:029aa53d7323 84 , Odd
simon 20:029aa53d7323 85 , Even
simon 20:029aa53d7323 86 , Forced1
simon 20:029aa53d7323 87 , Forced0
simon 20:029aa53d7323 88 };
simon 20:029aa53d7323 89
simon 20:029aa53d7323 90 /* Function: format
simon 20:029aa53d7323 91 * Set the transmission format used by the Serial port
simon 20:029aa53d7323 92 *
simon 20:029aa53d7323 93 * Variables:
simon 20:029aa53d7323 94 * bits - The number of bits in a word (5-8; default = 8)
simon 20:029aa53d7323 95 * parity - The parity used (Serial::None, Serial::Odd,
simon 20:029aa53d7323 96 Serial::Even, Serial::Forced1, Serial::Forced0; default = Serial::None)
simon 20:029aa53d7323 97 * stop - The number of stop bits (1 or 2; default = 1)
simon 20:029aa53d7323 98 */
mrsoundhar 92:f7fcbaa5f1b5 99 void format(int bits = 8, Parity parity = Serial::None, int stop_bits = 1);
simon 20:029aa53d7323 100
simon 20:029aa53d7323 101 /* Function: putc
simon 20:029aa53d7323 102 * Write a character
simon 20:029aa53d7323 103 *
simon 20:029aa53d7323 104 * Variables:
simon 20:029aa53d7323 105 * c - The character to write to the serial port
simon 20:029aa53d7323 106 */
simon 20:029aa53d7323 107 int putc(int c);
simon 20:029aa53d7323 108
simon 20:029aa53d7323 109 /* Function: getc
simon 20:029aa53d7323 110 * Read a character
simon 20:029aa53d7323 111 *
emilmont 27:7110ebee3484 112 * Read a character from the serial port. This call will block
emilmont 27:7110ebee3484 113 * until a character is available. For testing if a character is
emilmont 27:7110ebee3484 114 * available for reading, see <readable>.
emilmont 27:7110ebee3484 115 *
simon 20:029aa53d7323 116 * Variables:
simon 20:029aa53d7323 117 * returns - The character read from the serial port
simon 20:029aa53d7323 118 */
simon 20:029aa53d7323 119 int getc();
simon 20:029aa53d7323 120
simon 20:029aa53d7323 121 /* Function: printf
simon 20:029aa53d7323 122 * Write a formated string
simon 20:029aa53d7323 123 *
simon 20:029aa53d7323 124 * Variables:
simon 20:029aa53d7323 125 * format - A printf-style format string, followed by the
simon 20:029aa53d7323 126 * variables to use in formating the string.
simon 20:029aa53d7323 127 */
simon 20:029aa53d7323 128 int printf(const char* format, ...);
simon 20:029aa53d7323 129
simon 20:029aa53d7323 130 /* Function: scanf
simon 20:029aa53d7323 131 * Read a formated string
simon 20:029aa53d7323 132 *
simon 20:029aa53d7323 133 * Variables:
simon 20:029aa53d7323 134 * format - A scanf-style format string,
simon 20:029aa53d7323 135 * followed by the pointers to variables to store the results.
simon 20:029aa53d7323 136 */
simon 20:029aa53d7323 137 int scanf(const char* format, ...);
simon 20:029aa53d7323 138
simon 20:029aa53d7323 139 /* Function: readable
simon 20:029aa53d7323 140 * Determine if there is a character available to read
simon 20:029aa53d7323 141 *
simon 20:029aa53d7323 142 * Variables:
simon 20:029aa53d7323 143 * returns - 1 if there is a character available to read, else 0
simon 20:029aa53d7323 144 */
simon 20:029aa53d7323 145 int readable();
simon 20:029aa53d7323 146
simon 20:029aa53d7323 147 /* Function: writeable
simon 20:029aa53d7323 148 * Determine if there is space available to write a character
simon 20:029aa53d7323 149 *
simon 20:029aa53d7323 150 * Variables:
simon 20:029aa53d7323 151 * returns - 1 if there is space to write a character, else 0
simon 20:029aa53d7323 152 */
simon 20:029aa53d7323 153 int writeable();
simon 20:029aa53d7323 154
simon 20:029aa53d7323 155 /* Function: attach
simon 20:029aa53d7323 156 * Attach a function to call whenever a serial interrupt is generated
simon 20:029aa53d7323 157 *
simon 20:029aa53d7323 158 * Variables:
simon 20:029aa53d7323 159 * fptr - A pointer to a void function, or 0 to set as none
simon 20:029aa53d7323 160 */
simon 20:029aa53d7323 161 void attach(void (*fptr)(void));
simon 20:029aa53d7323 162
simon 20:029aa53d7323 163 /* Function: attach
simon 20:029aa53d7323 164 * Attach a member function to call whenever a serial interrupt is generated
simon 20:029aa53d7323 165 *
simon 20:029aa53d7323 166 * Variables:
simon 20:029aa53d7323 167 * tptr - pointer to the object to call the member function on
simon 20:029aa53d7323 168 * mptr - pointer to the member function to be called
simon 20:029aa53d7323 169 */
simon 20:029aa53d7323 170 template<typename T>
simon 20:029aa53d7323 171 void attach(T* tptr, void (T::*mptr)(void));
simon 20:029aa53d7323 172
simon 20:029aa53d7323 173 #endif
simon 20:029aa53d7323 174
simon 20:029aa53d7323 175 protected:
simon 20:029aa53d7323 176 PinName _txpin;
simon 20:029aa53d7323 177
simon 20:029aa53d7323 178 virtual int _putc(int c);
simon 20:029aa53d7323 179 virtual int _getc(void);
simon 20:029aa53d7323 180
simon 20:029aa53d7323 181 }; // End class SerialHalfDuplex
simon 20:029aa53d7323 182
simon 20:029aa53d7323 183 } // End namespace
simon 20:029aa53d7323 184
simon 20:029aa53d7323 185 #endif
emilmont 27:7110ebee3484 186
emilmont 27:7110ebee3484 187 #endif