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