Mickaël THEOT / AX12
Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers SerialHalfDuplex.h Source File

SerialHalfDuplex.h

00001 /* mbed Microcontroller Library
00002  * Copyright (c) 2006-2012 ARM Limited
00003  *
00004  * Permission is hereby granted, free of charge, to any person obtaining a copy
00005  * of this software and associated documentation files (the "Software"), to deal
00006  * in the Software without restriction, including without limitation the rights
00007  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
00008  * copies of the Software, and to permit persons to whom the Software is
00009  * furnished to do so, subject to the following conditions:
00010  *
00011  * The above copyright notice and this permission notice shall be included in
00012  * all copies or substantial portions of the Software.
00013  *
00014  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
00015  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
00016  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
00017  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
00018  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
00019  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
00020  * SOFTWARE.
00021  *
00022  * NOTE: This is an unsupported legacy untested library.
00023  */
00024 #ifndef MBED_SERIALHALFDUPLEX_H
00025 #define MBED_SERIALHALFDUPLEX_H
00026 
00027 #define BUF_SIZE 16
00028 
00029 #include "device.h"
00030 
00031 #if 1
00032 
00033 #include "SerialBase.h"
00034 #include "PinNames.h"
00035 #include "PeripheralNames.h"
00036 
00037 namespace mbed {
00038 
00039 /* Class: SerialHalfDuplex
00040  * A serial port (UART) for communication with other devices using
00041  * Half-Duplex, allowing transmit and receive on a single
00042  * shared transmit and receive line. Only one end should be transmitting
00043  * at a time.
00044  *
00045  * Both the tx and rx pin should be defined, and wired together.
00046  * This is in addition to them being wired to the other serial
00047  * device to allow both read and write functions to operate.
00048  *
00049  *  Example:
00050  *  > // Send a byte to a second HalfDuplex device, and read the response
00051  *  >
00052  *  > #include "mbed.h"
00053  *  >
00054  *  > // p9 and p10 should be wired together to form "a"
00055  *  > // p28 and p27 should be wired together to form "b"
00056  *  > // p9/p10 should be wired to p28/p27 as the Half Duplex connection
00057  *  >
00058  *  > SerialHalfDuplex a(p9, p10);
00059  *  > SerialHalfDuplex b(p28, p27);
00060  *  >
00061  *  > void b_rx() { // second device response
00062  *  >     b.putc(b.getc() + 4);
00063  *  > }
00064  *  >
00065  *  > int main() {
00066  *  >     b.attach(&b_rx);
00067  *  >     for(int c = 'A'; c < 'Z'; c++) {
00068  *  >         a.putc(c);
00069  *  >         printf("sent [%c]\n", c);
00070  *  >         wait(0.5);   // b should respond
00071  *  >         if(a.readable()) {
00072  *  >             printf("received [%c]\n", a.getc());
00073  *  >         }
00074  *  >     }
00075  *  > }
00076  *
00077  * For Simplex and Full-Duplex Serial communication, see <Serial>
00078  */
00079 class SerialHalfDuplex : public SerialBase {
00080 
00081 public:
00082     /* Constructor: SerialHalfDuplex
00083      * Create a half-duplex serial port, connected to the specified transmit
00084      * and receive pins.
00085      *
00086      * These pins should be wired together, as well as to the target device
00087      *
00088      * Variables:
00089      *  tx - Transmit pin
00090      *  rx - Receive pin
00091      */
00092     SerialHalfDuplex(PinName tx, PinName rx, int baud);
00093 
00094 #if 0       // Inherited from Serial class, for documentation
00095     /* Function: baud
00096      *  Set the baud rate of the serial port
00097      *
00098      * Variables:
00099      *  baudrate - The baudrate of the serial port (default = 9600).
00100      */
00101     void baud(int baudrate);
00102 
00103     enum Parity {
00104         None = 0
00105         , Odd
00106         , Even
00107         , Forced1
00108         , Forced0
00109     };
00110 
00111     /* Function: format
00112      *  Set the transmission format used by the Serial port
00113      *
00114      * Variables:
00115      *  bits - The number of bits in a word (5-8; default = 8)
00116      *  parity - The parity used (Serial::None, Serial::Odd,
00117 Serial::Even, Serial::Forced1, Serial::Forced0; default = Serial::None)
00118      *  stop - The number of stop bits (1 or 2; default = 1)
00119      */
00120     void format(int bits = 8, Parity parity = Serial::None, int stop_bits = 1);
00121 
00122     /* Function: putc
00123      *  Write a character
00124      *
00125      * Variables:
00126      *  c - The character to write to the serial port
00127      */
00128     int putc(int c);
00129 
00130     /* Function: getc
00131      *  Read a character
00132      *
00133      * Read a character from the serial port. This call will block
00134      * until a character is available. For testing if a character is
00135      * available for reading, see <readable>.
00136      *
00137      * Variables:
00138      *  returns - The character read from the serial port
00139      */
00140     int getc();
00141 
00142     /* Function: printf
00143      *  Write a formated string
00144      *
00145      * Variables:
00146      *  format - A printf-style format string, followed by the
00147      *      variables to use in formating the string.
00148      */
00149     int printf(const char* format, ...);
00150 
00151     /* Function: scanf
00152      *  Read a formated string
00153      *
00154      * Variables:
00155      *  format - A scanf-style format string,
00156      *      followed by the pointers to variables to store the results.
00157      */
00158     int scanf(const char* format, ...);
00159 
00160     /* Function: readable
00161      *  Determine if there is a character available to read
00162      *
00163      * Variables:
00164      *  returns - 1 if there is a character available to read, else 0
00165      */
00166     int readable();
00167 
00168     /* Function: writeable
00169      *  Determine if there is space available to write a character
00170      *
00171      * Variables:
00172      *  returns - 1 if there is space to write a character, else 0
00173      */
00174     int writeable();
00175 
00176     /* Function: attach
00177      *  Attach a function to call whenever a serial interrupt is generated
00178      *
00179      * Variables:
00180      *  fptr - A pointer to a void function, or 0 to set as none
00181      */
00182     void attach(void (*fptr)(void));
00183 
00184     /* Function: attach
00185      *  Attach a member function to call whenever a serial interrupt is generated
00186      *
00187      * Variables:
00188      *  tptr - pointer to the object to call the member function on
00189      *  mptr - pointer to the member function to be called
00190      */
00191     template<typename T>
00192     void attach(T* tptr, void (T::*mptr)(void));
00193 
00194 #endif
00195 
00196     virtual int putc(int c);
00197     virtual int getc(int i);
00198 
00199 private :
00200 
00201     PinName     _txpin;
00202     int buf[BUF_SIZE];
00203     short idx;
00204     virtual void RXinterrupt(void);
00205     virtual bool eraseBuffer(int *buffer);
00206 }; // End class SerialHalfDuplex
00207 
00208 } // End namespace
00209 
00210 #endif
00211 
00212 #endif