1 /* mbed Microcontroller Library
2 * Copyright (c) 2006-2013 ARM Limited
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
16 #ifndef MBED_SERIALBASE_H
17 #define MBED_SERIALBASE_H
24 #include "FunctionPointer.h"
25 #include "serial_api.h"
29 /** A base class for serial port implementations
30 * Can't be instantiated directly (use Serial or RawSerial)
35 /** Set the baud rate of the serial port
37 * @param baudrate The baudrate of the serial port (default = 9600).
39 void baud(int baudrate);
61 /** Set the transmission format used by the serial port
63 * @param bits The number of bits in a word (5-8; default = 8)
64 * @param parity The parity used (SerialBase::None, SerialBase::Odd, SerialBase::Even, SerialBase::Forced1, SerialBase::Forced0; default = SerialBase::None)
65 * @param stop The number of stop bits (1 or 2; default = 1)
67 void format(int bits=8, Parity parity=SerialBase::None, int stop_bits=1);
69 /** Determine if there is a character available to read
72 * 1 if there is a character available to read,
77 /** Determine if there is space available to write a character
80 * 1 if there is space to write a character,
85 /** Attach a function to call whenever a serial interrupt is generated
87 * @param fptr A pointer to a void function, or 0 to set as none
88 * @param type Which serial interrupt to attach the member function to (Seriall::RxIrq for receive, TxIrq for transmit buffer empty)
90 void attach(void (*fptr)(void), IrqType type=RxIrq);
92 /** Attach a member function to call whenever a serial interrupt is generated
94 * @param tptr pointer to the object to call the member function on
95 * @param mptr pointer to the member function to be called
96 * @param type Which serial interrupt to attach the member function to (Seriall::RxIrq for receive, TxIrq for transmit buffer empty)
99 void attach(T* tptr, void (T::*mptr)(void), IrqType type=RxIrq) {
100 if((mptr != NULL) && (tptr != NULL)) {
101 _irq[type].attach(tptr, mptr);
102 serial_irq_set(&_serial, (SerialIrq)type, 1);
106 /** Generate a break condition on the serial line
111 /** Set the flow control type on the serial port
113 * @param type the flow control type (Disabled, RTS, CTS, RTSCTS)
114 * @param flow1 the first flow control pin (RTS for RTS or RTSCTS, CTS for CTS)
115 * @param flow2 the second flow control pin (CTS for RTSCTS)
117 void set_flow_control(Flow type, PinName flow1=NC, PinName flow2=NC);
120 static void _irq_handler(uint32_t id, SerialIrq irq_type);
123 SerialBase(PinName tx, PinName rx);
124 virtual ~SerialBase() {
128 int _base_putc(int c);
131 FunctionPointer _irq[2];