Lab 1 Program C
Fork of mbed by
SerialHalfDuplex.h@30:3991a86798e3, 2012-01-04 (annotated)
- Committer:
- emilmont
- Date:
- Wed Jan 04 16:29:47 2012 +0000
- Revision:
- 30:3991a86798e3
- Parent:
- 27:7110ebee3484
- Child:
- 43:aff670d0d510
Add latest ARM GCC toolchain build
Who changed what in which revision?
User | Revision | Line number | New 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 | */ |
simon | 20:029aa53d7323 | 99 | void format(int bits = 8, Parity parity = Serial::None, int stop_bits |
simon | 20:029aa53d7323 | 100 | = 1); |
simon | 20:029aa53d7323 | 101 | |
simon | 20:029aa53d7323 | 102 | /* Function: putc |
simon | 20:029aa53d7323 | 103 | * Write a character |
simon | 20:029aa53d7323 | 104 | * |
simon | 20:029aa53d7323 | 105 | * Variables: |
simon | 20:029aa53d7323 | 106 | * c - The character to write to the serial port |
simon | 20:029aa53d7323 | 107 | */ |
simon | 20:029aa53d7323 | 108 | int putc(int c); |
simon | 20:029aa53d7323 | 109 | |
simon | 20:029aa53d7323 | 110 | /* Function: getc |
simon | 20:029aa53d7323 | 111 | * Read a character |
simon | 20:029aa53d7323 | 112 | * |
emilmont | 27:7110ebee3484 | 113 | * Read a character from the serial port. This call will block |
emilmont | 27:7110ebee3484 | 114 | * until a character is available. For testing if a character is |
emilmont | 27:7110ebee3484 | 115 | * available for reading, see <readable>. |
emilmont | 27:7110ebee3484 | 116 | * |
simon | 20:029aa53d7323 | 117 | * Variables: |
simon | 20:029aa53d7323 | 118 | * returns - The character read from the serial port |
simon | 20:029aa53d7323 | 119 | */ |
simon | 20:029aa53d7323 | 120 | int getc(); |
simon | 20:029aa53d7323 | 121 | |
simon | 20:029aa53d7323 | 122 | /* Function: printf |
simon | 20:029aa53d7323 | 123 | * Write a formated string |
simon | 20:029aa53d7323 | 124 | * |
simon | 20:029aa53d7323 | 125 | * Variables: |
simon | 20:029aa53d7323 | 126 | * format - A printf-style format string, followed by the |
simon | 20:029aa53d7323 | 127 | * variables to use in formating the string. |
simon | 20:029aa53d7323 | 128 | */ |
simon | 20:029aa53d7323 | 129 | int printf(const char* format, ...); |
simon | 20:029aa53d7323 | 130 | |
simon | 20:029aa53d7323 | 131 | /* Function: scanf |
simon | 20:029aa53d7323 | 132 | * Read a formated string |
simon | 20:029aa53d7323 | 133 | * |
simon | 20:029aa53d7323 | 134 | * Variables: |
simon | 20:029aa53d7323 | 135 | * format - A scanf-style format string, |
simon | 20:029aa53d7323 | 136 | * followed by the pointers to variables to store the results. |
simon | 20:029aa53d7323 | 137 | */ |
simon | 20:029aa53d7323 | 138 | int scanf(const char* format, ...); |
simon | 20:029aa53d7323 | 139 | |
simon | 20:029aa53d7323 | 140 | /* Function: readable |
simon | 20:029aa53d7323 | 141 | * Determine if there is a character available to read |
simon | 20:029aa53d7323 | 142 | * |
simon | 20:029aa53d7323 | 143 | * Variables: |
simon | 20:029aa53d7323 | 144 | * returns - 1 if there is a character available to read, else 0 |
simon | 20:029aa53d7323 | 145 | */ |
simon | 20:029aa53d7323 | 146 | int readable(); |
simon | 20:029aa53d7323 | 147 | |
simon | 20:029aa53d7323 | 148 | /* Function: writeable |
simon | 20:029aa53d7323 | 149 | * Determine if there is space available to write a character |
simon | 20:029aa53d7323 | 150 | * |
simon | 20:029aa53d7323 | 151 | * Variables: |
simon | 20:029aa53d7323 | 152 | * returns - 1 if there is space to write a character, else 0 |
simon | 20:029aa53d7323 | 153 | */ |
simon | 20:029aa53d7323 | 154 | int writeable(); |
simon | 20:029aa53d7323 | 155 | |
simon | 20:029aa53d7323 | 156 | /* Function: attach |
simon | 20:029aa53d7323 | 157 | * Attach a function to call whenever a serial interrupt is generated |
simon | 20:029aa53d7323 | 158 | * |
simon | 20:029aa53d7323 | 159 | * Variables: |
simon | 20:029aa53d7323 | 160 | * fptr - A pointer to a void function, or 0 to set as none |
simon | 20:029aa53d7323 | 161 | */ |
simon | 20:029aa53d7323 | 162 | void attach(void (*fptr)(void)); |
simon | 20:029aa53d7323 | 163 | |
simon | 20:029aa53d7323 | 164 | /* Function: attach |
simon | 20:029aa53d7323 | 165 | * Attach a member function to call whenever a serial interrupt is generated |
simon | 20:029aa53d7323 | 166 | * |
simon | 20:029aa53d7323 | 167 | * Variables: |
simon | 20:029aa53d7323 | 168 | * tptr - pointer to the object to call the member function on |
simon | 20:029aa53d7323 | 169 | * mptr - pointer to the member function to be called |
simon | 20:029aa53d7323 | 170 | */ |
simon | 20:029aa53d7323 | 171 | template<typename T> |
simon | 20:029aa53d7323 | 172 | void attach(T* tptr, void (T::*mptr)(void)); |
simon | 20:029aa53d7323 | 173 | |
simon | 20:029aa53d7323 | 174 | #endif |
simon | 20:029aa53d7323 | 175 | |
simon | 20:029aa53d7323 | 176 | protected: |
simon | 20:029aa53d7323 | 177 | PinName _txpin; |
simon | 20:029aa53d7323 | 178 | |
simon | 20:029aa53d7323 | 179 | virtual int _putc(int c); |
simon | 20:029aa53d7323 | 180 | virtual int _getc(void); |
simon | 20:029aa53d7323 | 181 | |
simon | 20:029aa53d7323 | 182 | }; // End class SerialHalfDuplex |
simon | 20:029aa53d7323 | 183 | |
simon | 20:029aa53d7323 | 184 | } // End namespace |
simon | 20:029aa53d7323 | 185 | |
simon | 20:029aa53d7323 | 186 | #endif |
emilmont | 27:7110ebee3484 | 187 | |
emilmont | 27:7110ebee3484 | 188 | #endif |