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