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

Dependents:   SeeedTouchLCD

Fork of mbed by mbed official

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers SerialBase.h Source File

SerialBase.h

00001 /* mbed Microcontroller Library
00002  * Copyright (c) 2006-2013 ARM Limited
00003  *
00004  * Licensed under the Apache License, Version 2.0 (the "License");
00005  * you may not use this file except in compliance with the License.
00006  * You may obtain a copy of the License at
00007  *
00008  *     http://www.apache.org/licenses/LICENSE-2.0
00009  *
00010  * Unless required by applicable law or agreed to in writing, software
00011  * distributed under the License is distributed on an "AS IS" BASIS,
00012  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00013  * See the License for the specific language governing permissions and
00014  * limitations under the License.
00015  */
00016 #ifndef MBED_SERIALBASE_H
00017 #define MBED_SERIALBASE_H
00018 
00019 #include "platform.h"
00020 
00021 #if DEVICE_SERIAL
00022 
00023 #include "Stream.h"
00024 #include "FunctionPointer.h"
00025 #include "serial_api.h"
00026 
00027 namespace mbed {
00028 
00029 /** A base class for serial port implementations
00030  * Can't be instantiated directly (use Serial or RawSerial)
00031  */
00032 class SerialBase {
00033 
00034 public:
00035     /** Set the baud rate of the serial port
00036      *
00037      *  @param baudrate The baudrate of the serial port (default = 9600).
00038      */
00039     void baud(int baudrate);
00040 
00041     enum Parity {
00042         None = 0,
00043         Odd,
00044         Even,
00045         Forced1,
00046         Forced0
00047     };
00048 
00049     enum IrqType {
00050         RxIrq = 0,
00051         TxIrq
00052     };
00053 
00054     /** Set the transmission format used by the serial port
00055      *
00056      *  @param bits The number of bits in a word (5-8; default = 8)
00057      *  @param parity The parity used (SerialBase::None, SerialBase::Odd, SerialBase::Even, SerialBase::Forced1, SerialBase::Forced0; default = SerialBase::None)
00058      *  @param stop The number of stop bits (1 or 2; default = 1)
00059      */
00060     void format(int bits=8, Parity parity=SerialBase::None, int stop_bits=1);
00061 
00062     /** Determine if there is a character available to read
00063      *
00064      *  @returns
00065      *    1 if there is a character available to read,
00066      *    0 otherwise
00067      */
00068     int readable();
00069 
00070     /** Determine if there is space available to write a character
00071      *
00072      *  @returns
00073      *    1 if there is space to write a character,
00074      *    0 otherwise
00075      */
00076     int writeable();
00077 
00078     /** Attach a function to call whenever a serial interrupt is generated
00079      *
00080      *  @param fptr A pointer to a void function, or 0 to set as none
00081      *  @param type Which serial interrupt to attach the member function to (Seriall::RxIrq for receive, TxIrq for transmit buffer empty)
00082      */
00083     void attach(void (*fptr)(void), IrqType type=RxIrq);
00084 
00085     /** Attach a member function to call whenever a serial interrupt is generated
00086      *
00087      *  @param tptr pointer to the object to call the member function on
00088      *  @param mptr pointer to the member function to be called
00089      *  @param type Which serial interrupt to attach the member function to (Seriall::RxIrq for receive, TxIrq for transmit buffer empty)
00090      */
00091     template<typename T>
00092     void attach(T* tptr, void (T::*mptr)(void), IrqType type=RxIrq) {
00093         if((mptr != NULL) && (tptr != NULL)) {
00094             _irq[type].attach(tptr, mptr);
00095             serial_irq_set(&_serial, (SerialIrq)type, 1);
00096         }
00097     }
00098 
00099     /** Generate a break condition on the serial line
00100      */
00101     void send_break();
00102 
00103     static void _irq_handler(uint32_t id, SerialIrq irq_type);
00104 
00105 protected:
00106     SerialBase(PinName tx, PinName rx);
00107  
00108     int _base_getc();
00109     int _base_putc(int c);
00110 
00111     serial_t        _serial;
00112     FunctionPointer _irq[2];
00113     int             _baud;
00114 };
00115 
00116 } // namespace mbed
00117 
00118 #endif
00119 
00120 #endif