Soundharrajan

Fork of mbed by mbed official

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 
00005 #ifndef MBED_SERIALHALFDUPLEX_H
00006 #define MBED_SERIALHALFDUPLEX_H
00007 
00008 #include "device.h"
00009 
00010 #if DEVICE_SERIAL
00011 
00012 #include "Serial.h"
00013 #include "PinNames.h"
00014 #include "PeripheralNames.h"
00015 
00016 namespace mbed {
00017 
00018 /* Class: SerialHalfDuplex
00019  * A serial port (UART) for communication with other devices using  
00020  * Half-Duplex, allowing transmit and receive on a single
00021  * shared transmit and receive line. Only one end should be transmitting 
00022  * at a time.
00023  * 
00024  * Both the tx and rx pin should be defined, and wired together. 
00025  * This is in addition to them being wired to the other serial 
00026  * device to allow both read and write functions to operate.
00027  *
00028  *  Example:
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  * 
00056  * For Simplex and Full-Duplex Serial communication, see <Serial>
00057  */
00058 class SerialHalfDuplex : public Serial {
00059 
00060 public:
00061     /* Constructor: SerialHalfDuplex
00062      * Create a half-duplex serial port, connected to the specified transmit
00063      * and receive pins.
00064      *
00065      * These pins should be wired together, as well as to the target device
00066      *
00067      * Variables:
00068      *  tx - Transmit pin
00069      *  rx - Receive pin
00070      */
00071     SerialHalfDuplex(PinName tx, PinName rx, const char *name = NULL);
00072 
00073 #if 0       // Inherited from Serial class, for documentation
00074     /* Function: baud
00075      *  Set the baud rate of the serial port
00076      *
00077      * Variables:
00078      *  baudrate - The baudrate of the serial port (default = 9600).
00079      */
00080     void baud(int baudrate);
00081 
00082     enum Parity {
00083         None = 0
00084         , Odd
00085         , Even
00086         , Forced1
00087         , Forced0
00088     };
00089 
00090     /* Function: format
00091      *  Set the transmission format used by the Serial port
00092      *
00093      * Variables:
00094      *  bits - The number of bits in a word (5-8; default = 8)
00095      *  parity - The parity used (Serial::None, Serial::Odd, 
00096 Serial::Even, Serial::Forced1, Serial::Forced0; default = Serial::None)
00097      *  stop - The number of stop bits (1 or 2; default = 1)
00098      */
00099     void format(int bits = 8, Parity parity = Serial::None, int stop_bits = 1);
00100 
00101     /* Function: putc
00102      *  Write a character
00103      *
00104      * Variables:
00105      *  c - The character to write to the serial port
00106      */
00107     int putc(int c);
00108 
00109     /* Function: getc
00110      *  Read a character
00111      *
00112      * Read a character from the serial port. This call will block
00113      * until a character is available. For testing if a character is
00114      * available for reading, see <readable>.
00115      *
00116      * Variables:
00117      *  returns - The character read from the serial port
00118      */
00119     int getc();
00120 
00121     /* Function: printf
00122      *  Write a formated string
00123      *
00124      * Variables:
00125      *  format - A printf-style format string, followed by the
00126      *      variables to use in formating the string.
00127      */
00128     int printf(const char* format, ...);
00129 
00130     /* Function: scanf
00131      *  Read a formated string
00132      *
00133      * Variables:
00134      *  format - A scanf-style format string,
00135      *      followed by the pointers to variables to store the results.
00136      */
00137     int scanf(const char* format, ...);
00138 
00139     /* Function: readable
00140      *  Determine if there is a character available to read
00141      *
00142      * Variables:
00143      *  returns - 1 if there is a character available to read, else 0
00144      */
00145     int readable();
00146 
00147     /* Function: writeable
00148      *  Determine if there is space available to write a character
00149      *
00150      * Variables:
00151      *  returns - 1 if there is space to write a character, else 0
00152      */
00153     int writeable();
00154 
00155     /* Function: attach
00156      *  Attach a function to call whenever a serial interrupt is generated
00157      *
00158      * Variables:
00159      *  fptr - A pointer to a void function, or 0 to set as none
00160      */
00161     void attach(void (*fptr)(void));
00162 
00163     /* Function: attach
00164      *  Attach a member function to call whenever a serial interrupt is generated
00165      *
00166      * Variables:
00167      *  tptr - pointer to the object to call the member function on
00168      *  mptr - pointer to the member function to be called
00169      */
00170     template<typename T>
00171     void attach(T* tptr, void (T::*mptr)(void));
00172 
00173 #endif
00174 
00175 protected:
00176     PinName     _txpin;
00177 
00178     virtual int _putc(int c);
00179     virtual int _getc(void);
00180 
00181 }; // End class SerialHalfDuplex
00182 
00183 } // End namespace
00184 
00185 #endif
00186 
00187 #endif