Mbed SDK for XRange SX1272 LoRa module

Dependents:   XRangePingPong XRange-LoRaWAN-lmic-app lora-transceiver

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     enum Flow {
00055         Disabled = 0,
00056         RTS,
00057         CTS,
00058         RTSCTS
00059     };
00060 
00061     /** Set the transmission format used by the serial port
00062      *
00063      *  @param bits The number of bits in a word (5-8; default = 8)
00064      *  @param parity The parity used (SerialBase::None, SerialBase::Odd, SerialBase::Even, SerialBase::Forced1, SerialBase::Forced0; default = SerialBase::None)
00065      *  @param stop The number of stop bits (1 or 2; default = 1)
00066      */
00067     void format(int bits=8, Parity parity=SerialBase::None, int stop_bits=1);
00068 
00069     /** Determine if there is a character available to read
00070      *
00071      *  @returns
00072      *    1 if there is a character available to read,
00073      *    0 otherwise
00074      */
00075     int readable();
00076 
00077     /** Determine if there is space available to write a character
00078      *
00079      *  @returns
00080      *    1 if there is space to write a character,
00081      *    0 otherwise
00082      */
00083     int writeable();
00084 
00085     /** Attach a function to call whenever a serial interrupt is generated
00086      *
00087      *  @param fptr A pointer to a void function, or 0 to set as none
00088      *  @param type Which serial interrupt to attach the member function to (Seriall::RxIrq for receive, TxIrq for transmit buffer empty)
00089      */
00090     void attach(void (*fptr)(void), IrqType type=RxIrq);
00091 
00092     /** Attach a member function to call whenever a serial interrupt is generated
00093      *
00094      *  @param tptr pointer to the object to call the member function on
00095      *  @param mptr pointer to the member function to be called
00096      *  @param type Which serial interrupt to attach the member function to (Seriall::RxIrq for receive, TxIrq for transmit buffer empty)
00097      */
00098     template<typename T>
00099     void attach(T* tptr, void (T::*mptr)(void), IrqType type=RxIrq) {
00100         if((mptr != NULL) && (tptr != NULL)) {
00101             _irq[type].attach(tptr, mptr);
00102             serial_irq_set(&_serial, (SerialIrq)type, 1);
00103         }
00104     }
00105 
00106     /** Generate a break condition on the serial line
00107      */
00108     void send_break();
00109 
00110 #if DEVICE_SERIAL_FC
00111     /** Set the flow control type on the serial port
00112      *
00113      *  @param type the flow control type (Disabled, RTS, CTS, RTSCTS)
00114      *  @param flow1 the first flow control pin (RTS for RTS or RTSCTS, CTS for CTS)
00115      *  @param flow2 the second flow control pin (CTS for RTSCTS)
00116      */
00117     void set_flow_control(Flow type, PinName flow1=NC, PinName flow2=NC);
00118 #endif
00119 
00120     static void _irq_handler(uint32_t id, SerialIrq irq_type);
00121 
00122 protected:
00123     SerialBase(PinName tx, PinName rx);
00124     virtual ~SerialBase() {
00125     }
00126 
00127     int _base_getc();
00128     int _base_putc(int c);
00129 
00130     serial_t        _serial;
00131     FunctionPointer _irq[2];
00132     int             _baud;
00133 };
00134 
00135 } // namespace mbed
00136 
00137 #endif
00138 
00139 #endif