The official Mbed 2 C/C++ SDK provides the software platform and libraries to build your applications.

Dependents:   hello SerialTestv11 SerialTestv12 Sierpinski ... more

mbed 2

This is the mbed 2 library. If you'd like to learn about Mbed OS please see the mbed-os docs.

Committer:
emilmont
Date:
Fri Oct 26 17:40:46 2012 +0100
Revision:
43:e2ed12d17f06
Parent:
30:3991a86798e3
Update documentation

Who changed what in which revision?

UserRevisionLine numberNew 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
emilmont 43:e2ed12d17f06 18 /** A serial port (UART) for communication with other devices using
emilmont 43:e2ed12d17f06 19 * Half-Duplex, allowing transmit and receive on a single
emilmont 43:e2ed12d17f06 20 * shared transmit and receive line. Only one end should be transmitting
emilmont 43:e2ed12d17f06 21 * at a time.
emilmont 27:7110ebee3484 22 *
emilmont 43:e2ed12d17f06 23 * Both the tx and rx pin should be defined, and wired together.
emilmont 43:e2ed12d17f06 24 * This is in addition to them being wired to the other serial
emilmont 43:e2ed12d17f06 25 * device to allow both read and write functions to operate.
emilmont 43:e2ed12d17f06 26 *
emilmont 43:e2ed12d17f06 27 * For Simplex and Full-Duplex Serial communication, see Serial()
emilmont 43:e2ed12d17f06 28 *
emilmont 43:e2ed12d17f06 29 * Example:
emilmont 43:e2ed12d17f06 30 * @code
emilmont 43:e2ed12d17f06 31 * // Send a byte to a second HalfDuplex device, and read the response
emilmont 43:e2ed12d17f06 32 *
emilmont 43:e2ed12d17f06 33 * #include "mbed.h"
simon 20:029aa53d7323 34 *
emilmont 43:e2ed12d17f06 35 * // p9 and p10 should be wired together to form "a"
emilmont 43:e2ed12d17f06 36 * // p28 and p27 should be wired together to form "b"
emilmont 43:e2ed12d17f06 37 * // p9/p10 should be wired to p28/p27 as the Half Duplex connection
emilmont 43:e2ed12d17f06 38 *
emilmont 43:e2ed12d17f06 39 * SerialHalfDuplex a(p9, p10);
emilmont 43:e2ed12d17f06 40 * SerialHalfDuplex b(p28, p27);
emilmont 43:e2ed12d17f06 41 *
emilmont 43:e2ed12d17f06 42 * void b_rx() { // second device response
emilmont 43:e2ed12d17f06 43 * b.putc(b.getc() + 4);
emilmont 43:e2ed12d17f06 44 * }
emilmont 43:e2ed12d17f06 45 *
emilmont 43:e2ed12d17f06 46 * int main() {
emilmont 43:e2ed12d17f06 47 * b.attach(&b_rx);
emilmont 43:e2ed12d17f06 48 * for (int c = 'A'; c < 'Z'; c++) {
emilmont 43:e2ed12d17f06 49 * a.putc(c);
emilmont 43:e2ed12d17f06 50 * printf("sent [%c]\n", c);
emilmont 43:e2ed12d17f06 51 * wait(0.5); // b should respond
emilmont 43:e2ed12d17f06 52 * if (a.readable()) {
emilmont 43:e2ed12d17f06 53 * printf("received [%c]\n", a.getc());
emilmont 43:e2ed12d17f06 54 * }
emilmont 43:e2ed12d17f06 55 * }
emilmont 43:e2ed12d17f06 56 * }
emilmont 43:e2ed12d17f06 57 * @endcode
simon 20:029aa53d7323 58 */
simon 20:029aa53d7323 59 class SerialHalfDuplex : public Serial {
simon 20:029aa53d7323 60
simon 20:029aa53d7323 61 public:
emilmont 43:e2ed12d17f06 62 /** Create a half-duplex serial port, connected to the specified transmit
emilmont 43:e2ed12d17f06 63 * and receive pins.
simon 20:029aa53d7323 64 *
emilmont 43:e2ed12d17f06 65 * These pins should be wired together, as well as to the target device
emilmont 27:7110ebee3484 66 *
emilmont 43:e2ed12d17f06 67 * @param tx Transmit pin
emilmont 43:e2ed12d17f06 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
emilmont 43:e2ed12d17f06 73 /** Set the baud rate of the serial port
simon 20:029aa53d7323 74 *
emilmont 43:e2ed12d17f06 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
emilmont 43:e2ed12d17f06 87 /** Set the transmission format used by the Serial port
simon 20:029aa53d7323 88 *
emilmont 43:e2ed12d17f06 89 * @param bits The number of bits in a word (5-8; default = 8)
emilmont 43:e2ed12d17f06 90 * @param parity The parity used (Serial::None, Serial::Odd,
emilmont 43:e2ed12d17f06 91 * Serial::Even, Serial::Forced1, Serial::Forced0; default = Serial::None)
emilmont 43:e2ed12d17f06 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
emilmont 43:e2ed12d17f06 97 /** Write a character
simon 20:029aa53d7323 98 *
emilmont 43:e2ed12d17f06 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
emilmont 43:e2ed12d17f06 103 /** Read a character
simon 20:029aa53d7323 104 *
emilmont 43:e2ed12d17f06 105 * Read a character from the serial port. This call will block
emilmont 43:e2ed12d17f06 106 * until a character is available. For testing if a character is
emilmont 43:e2ed12d17f06 107 * available for reading, see <readable>.
emilmont 27:7110ebee3484 108 *
emilmont 43:e2ed12d17f06 109 * @returns
emilmont 43:e2ed12d17f06 110 * The character read from the serial port
simon 20:029aa53d7323 111 */
simon 20:029aa53d7323 112 int getc();
simon 20:029aa53d7323 113
emilmont 43:e2ed12d17f06 114 /** Write a formated string
simon 20:029aa53d7323 115 *
emilmont 43:e2ed12d17f06 116 * @param format A printf-style format string, followed by the
emilmont 43:e2ed12d17f06 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
emilmont 43:e2ed12d17f06 121 /** Read a formated string
simon 20:029aa53d7323 122 *
emilmont 43:e2ed12d17f06 123 * @param format A scanf-style format string,
emilmont 43:e2ed12d17f06 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
emilmont 43:e2ed12d17f06 128 /** Determine if there is a character available to read
simon 20:029aa53d7323 129 *
emilmont 43:e2ed12d17f06 130 * @returns
emilmont 43:e2ed12d17f06 131 * 1 if there is a character available to read,
emilmont 43:e2ed12d17f06 132 * 0 otherwise
simon 20:029aa53d7323 133 */
simon 20:029aa53d7323 134 int readable();
simon 20:029aa53d7323 135
emilmont 43:e2ed12d17f06 136 /** Determine if there is space available to write a character
simon 20:029aa53d7323 137 *
emilmont 43:e2ed12d17f06 138 * @returns
emilmont 43:e2ed12d17f06 139 * 1 if there is space to write a character,
emilmont 43:e2ed12d17f06 140 * 0 otherwise
simon 20:029aa53d7323 141 */
simon 20:029aa53d7323 142 int writeable();
simon 20:029aa53d7323 143
emilmont 43:e2ed12d17f06 144 /** Attach a function to call whenever a serial interrupt is generated
simon 20:029aa53d7323 145 *
emilmont 43:e2ed12d17f06 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
emilmont 43:e2ed12d17f06 150 /** Attach a member function to call whenever a serial interrupt is generated
simon 20:029aa53d7323 151 *
emilmont 43:e2ed12d17f06 152 * @param tptr pointer to the object to call the member function on
emilmont 43:e2ed12d17f06 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