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