printer

Dependents:   Good_Serial_HelloWorld_Mbed

Fork of mbed by gokmen ascioglu

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 
00100 = 1);
00101 
00102     /* Function: putc
00103      *  Write a character
00104      *
00105      * Variables:
00106      *  c - The character to write to the serial port
00107      */
00108     int putc(int c);
00109 
00110     /* Function: getc
00111      *  Read a character
00112      *
00113      * Read a character from the serial port. This call will block
00114      * until a character is available. For testing if a character is
00115      * available for reading, see <readable>.
00116      *
00117      * Variables:
00118      *  returns - The character read from the serial port
00119      */
00120     int getc();
00121 
00122     /* Function: printf
00123      *  Write a formated string
00124      *
00125      * Variables:
00126      *  format - A printf-style format string, followed by the
00127      *      variables to use in formating the string.
00128      */
00129     int printf(const char* format, ...);
00130 
00131     /* Function: scanf
00132      *  Read a formated string
00133      *
00134      * Variables:
00135      *  format - A scanf-style format string,
00136      *      followed by the pointers to variables to store the results.
00137      */
00138     int scanf(const char* format, ...);
00139 
00140     /* Function: readable
00141      *  Determine if there is a character available to read
00142      *
00143      * Variables:
00144      *  returns - 1 if there is a character available to read, else 0
00145      */
00146     int readable();
00147 
00148     /* Function: writeable
00149      *  Determine if there is space available to write a character
00150      *
00151      * Variables:
00152      *  returns - 1 if there is space to write a character, else 0
00153      */
00154     int writeable();
00155 
00156     /* Function: attach
00157      *  Attach a function to call whenever a serial interrupt is generated
00158      *
00159      * Variables:
00160      *  fptr - A pointer to a void function, or 0 to set as none
00161      */
00162     void attach(void (*fptr)(void));
00163 
00164     /* Function: attach
00165      *  Attach a member function to call whenever a serial interrupt is generated
00166      *
00167      * Variables:
00168      *  tptr - pointer to the object to call the member function on
00169      *  mptr - pointer to the member function to be called
00170      */
00171     template<typename T>
00172     void attach(T* tptr, void (T::*mptr)(void));
00173 
00174 #endif
00175 
00176 protected:
00177     PinName     _txpin;
00178 
00179     virtual int _putc(int c);
00180     virtual int _getc(void);
00181 
00182 }; // End class SerialHalfDuplex
00183 
00184 } // End namespace
00185 
00186 #endif
00187 
00188 #endif