Modification of mbed-src library only for STM32F030F4, very cheap microcontroller in 20-Pin TSSOP package, with 16Kbytes of Flash and 4Kbytes of Ram. **Target for online compilator must be Nucleo 32F030R8.**

Dependents:   STM32F031_blink_LED_2

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 // for 030F4 flow control not implemented (N.S.)
00020 #undef DEVICE_SERIAL_FC
00021 
00022 
00023 #include "platform.h"
00024 
00025 #if DEVICE_SERIAL
00026 
00027 #include "Stream.h"
00028 #include "FunctionPointer.h"
00029 #include "serial_api.h"
00030 
00031 namespace mbed {
00032 
00033 /** A base class for serial port implementations
00034  * Can't be instantiated directly (use Serial or RawSerial)
00035  */
00036 class SerialBase {
00037 
00038 public:
00039     /** Set the baud rate of the serial port
00040      *
00041      *  @param baudrate The baudrate of the serial port (default = 9600).
00042      */
00043     void baud(int baudrate);
00044 
00045     enum Parity {
00046         None = 0,
00047         Odd,
00048         Even,
00049         Forced1,
00050         Forced0
00051     };
00052 
00053     enum IrqType {
00054         RxIrq = 0,
00055         TxIrq
00056     };
00057 
00058     enum Flow {
00059         Disabled = 0,
00060         RTS,
00061         CTS,
00062         RTSCTS
00063     };
00064 
00065     /** Set the transmission format used by the serial port
00066      *
00067      *  @param bits The number of bits in a word (5-8; default = 8)
00068      *  @param parity The parity used (SerialBase::None, SerialBase::Odd, SerialBase::Even, SerialBase::Forced1, SerialBase::Forced0; default = SerialBase::None)
00069      *  @param stop The number of stop bits (1 or 2; default = 1)
00070      */
00071     void format(int bits=8, Parity parity=SerialBase::None, int stop_bits=1);
00072 
00073     /** Determine if there is a character available to read
00074      *
00075      *  @returns
00076      *    1 if there is a character available to read,
00077      *    0 otherwise
00078      */
00079     int readable();
00080 
00081     /** Determine if there is space available to write a character
00082      *
00083      *  @returns
00084      *    1 if there is space to write a character,
00085      *    0 otherwise
00086      */
00087     int writeable();
00088 
00089     /** Attach a function to call whenever a serial interrupt is generated
00090      *
00091      *  @param fptr A pointer to a void function, or 0 to set as none
00092      *  @param type Which serial interrupt to attach the member function to (Seriall::RxIrq for receive, TxIrq for transmit buffer empty)
00093      */
00094     void attach(void (*fptr)(void), IrqType type=RxIrq);
00095 
00096     /** Attach a member function to call whenever a serial interrupt is generated
00097      *
00098      *  @param tptr pointer to the object to call the member function on
00099      *  @param mptr pointer to the member function to be called
00100      *  @param type Which serial interrupt to attach the member function to (Seriall::RxIrq for receive, TxIrq for transmit buffer empty)
00101      */
00102     template<typename T>
00103     void attach(T* tptr, void (T::*mptr)(void), IrqType type=RxIrq) {
00104         if((mptr != NULL) && (tptr != NULL)) {
00105             _irq[type].attach(tptr, mptr);
00106             serial_irq_set(&_serial, (SerialIrq)type, 1);
00107         }
00108     }
00109 
00110     /** Generate a break condition on the serial line
00111      */
00112     void send_break();
00113 
00114 #if DEVICE_SERIAL_FC
00115     /** Set the flow control type on the serial port
00116      *
00117      *  @param type the flow control type (Disabled, RTS, CTS, RTSCTS)
00118      *  @param flow1 the first flow control pin (RTS for RTS or RTSCTS, CTS for CTS)
00119      *  @param flow2 the second flow control pin (CTS for RTSCTS)
00120      */
00121     void set_flow_control(Flow type, PinName flow1=NC, PinName flow2=NC);
00122 #endif
00123 
00124     static void _irq_handler(uint32_t id, SerialIrq irq_type);
00125 
00126 protected:
00127     SerialBase(PinName tx, PinName rx);
00128     virtual ~SerialBase() {
00129     }
00130 
00131     int _base_getc();
00132     int _base_putc(int c);
00133 
00134     serial_t        _serial;
00135     FunctionPointer _irq[2];
00136     int             _baud;
00137 };
00138 
00139 } // namespace mbed
00140 
00141 #endif
00142 
00143 #endif