ON Semiconductor / mbed-os

Dependents:   mbed-TFT-example-NCS36510 mbed-Accelerometer-example-NCS36510 mbed-Accelerometer-example-NCS36510

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers SerialHalfDuplex.h Source File

SerialHalfDuplex.h

00001 /* mbed Microcontroller Library - SerialHalfDuplex
00002  * Copyright (c) 2010-2011 ARM Limited. All rights reserved.
00003  */
00004 #ifndef MBED_SERIALHALFDUPLEX_H
00005 #define MBED_SERIALHALFDUPLEX_H
00006 
00007 #include "platform.h"
00008 
00009 #if DEVICE_SERIAL
00010 
00011 #include "Serial.h"
00012 #include "gpio_api.h"
00013 
00014 namespace mbed {
00015 
00016 /** A serial port (UART) for communication with other devices using
00017  * Half-Duplex, allowing transmit and receive on a single
00018  * shared transmit and receive line. Only one end should be transmitting
00019  * at a time.
00020  *
00021  * Both the tx and rx pin should be defined, and wired together.
00022  * This is in addition to them being wired to the other serial
00023  * device to allow both read and write functions to operate.
00024  *
00025  *  For Simplex and Full-Duplex Serial communication, see Serial()
00026  *
00027  *  Example:
00028  * @code
00029  * // Send a byte to a second HalfDuplex device, and read the response
00030  *
00031  * #include "mbed.h"
00032  *
00033  * // p9 and p10 should be wired together to form "a"
00034  * // p28 and p27 should be wired together to form "b"
00035  * // p9/p10 should be wired to p28/p27 as the Half Duplex connection
00036  *
00037  * SerialHalfDuplex a(p9, p10);
00038  * SerialHalfDuplex b(p28, p27);
00039  *
00040  * void b_rx() { // second device response
00041  *     b.putc(b.getc() + 4);
00042  * }
00043  *
00044  * int main() {
00045  *     b.attach(&b_rx);
00046  *     for (int c = 'A'; c < 'Z'; c++) {
00047  *         a.putc(c);
00048  *         printf("sent [%c]\n", c);
00049  *         wait(0.5);   // b should respond
00050  *         if (a.readable()) {
00051  *             printf("received [%c]\n", a.getc());
00052  *         }
00053  *     }
00054  * }
00055  * @endcode
00056  */
00057 class SerialHalfDuplex : public Serial {
00058 
00059 public:
00060     /** Create a half-duplex serial port, connected to the specified transmit
00061      * and receive pins.
00062      *
00063      * These pins should be wired together, as well as to the target device
00064      *
00065      *  @param tx Transmit pin
00066      *  @param rx Receive pin
00067      */
00068     SerialHalfDuplex(PinName tx, PinName rx);
00069 
00070 protected:
00071     gpio_object gpio;
00072 
00073     virtual int _putc(int c);
00074     virtual int _getc(void);
00075 
00076 }; // End class SerialHalfDuplex
00077 
00078 } // End namespace
00079 
00080 #endif
00081 
00082 #endif