]> git.donarmstrong.com Git - qmk_firmware.git/blob - tool/mbed/mbed-sdk/libraries/mbed/api/SerialBase.h
Squashed 'tmk_core/' changes from 7967731..b9e0ea0
[qmk_firmware.git] / tool / mbed / mbed-sdk / libraries / mbed / api / SerialBase.h
1 /* mbed Microcontroller Library
2  * Copyright (c) 2006-2013 ARM Limited
3  *
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
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16 #ifndef MBED_SERIALBASE_H
17 #define MBED_SERIALBASE_H
18
19 #include "platform.h"
20
21 #if DEVICE_SERIAL
22
23 #include "Stream.h"
24 #include "FunctionPointer.h"
25 #include "serial_api.h"
26
27 namespace mbed {
28
29 /** A base class for serial port implementations
30  * Can't be instantiated directly (use Serial or RawSerial)
31  */
32 class SerialBase {
33
34 public:
35     /** Set the baud rate of the serial port
36      *
37      *  @param baudrate The baudrate of the serial port (default = 9600).
38      */
39     void baud(int baudrate);
40
41     enum Parity {
42         None = 0,
43         Odd,
44         Even,
45         Forced1,
46         Forced0
47     };
48
49     enum IrqType {
50         RxIrq = 0,
51         TxIrq
52     };
53
54     enum Flow {
55         Disabled = 0,
56         RTS,
57         CTS,
58         RTSCTS
59     };
60
61     /** Set the transmission format used by the serial port
62      *
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)
66      */
67     void format(int bits=8, Parity parity=SerialBase::None, int stop_bits=1);
68
69     /** Determine if there is a character available to read
70      *
71      *  @returns
72      *    1 if there is a character available to read,
73      *    0 otherwise
74      */
75     int readable();
76
77     /** Determine if there is space available to write a character
78      *
79      *  @returns
80      *    1 if there is space to write a character,
81      *    0 otherwise
82      */
83     int writeable();
84
85     /** Attach a function to call whenever a serial interrupt is generated
86      *
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)
89      */
90     void attach(void (*fptr)(void), IrqType type=RxIrq);
91
92     /** Attach a member function to call whenever a serial interrupt is generated
93      *
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)
97      */
98     template<typename T>
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);
103         }
104     }
105
106     /** Generate a break condition on the serial line
107      */
108     void send_break();
109
110 #if DEVICE_SERIAL_FC
111     /** Set the flow control type on the serial port
112      *
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)
116      */
117     void set_flow_control(Flow type, PinName flow1=NC, PinName flow2=NC);
118 #endif
119
120     static void _irq_handler(uint32_t id, SerialIrq irq_type);
121
122 protected:
123     SerialBase(PinName tx, PinName rx);
124     virtual ~SerialBase() {
125     }
126
127     int _base_getc();
128     int _base_putc(int c);
129
130     serial_t        _serial;
131     FunctionPointer _irq[2];
132     int             _baud;
133 };
134
135 } // namespace mbed
136
137 #endif
138
139 #endif