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