]> git.gir.st - tmk_keyboard.git/blob - tmk_core/tool/mbed/mbed-sdk/libraries/USBHost/USBHostSerial/USBHostSerial.h
Merge commit '1fe4406f374291ab2e86e95a97341fd9c475fcb8'
[tmk_keyboard.git] / tmk_core / tool / mbed / mbed-sdk / libraries / USBHost / USBHostSerial / USBHostSerial.h
1 /* mbed USBHost 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
17 #ifndef USBHOSTSERIAL_H
18 #define USBHOSTSERIAL_H
19
20 #include "USBHostConf.h"
21
22 #if USBHOST_SERIAL
23
24 #include "USBHost.h"
25 #include "Stream.h"
26 #include "MtxCircBuffer.h"
27
28 /**
29 * A class to communicate a USB virtual serial port
30 */
31 class USBHostSerialPort : public Stream {
32 public:
33 /**
34 * Constructor
35 */
36 USBHostSerialPort();
37
38 enum IrqType {
39 RxIrq,
40 TxIrq
41 };
42
43 enum Parity {
44 None = 0,
45 Odd,
46 Even,
47 Mark,
48 Space
49 };
50
51 void connect(USBHost* _host, USBDeviceConnected * _dev,
52 uint8_t _serial_intf, USBEndpoint* _bulk_in, USBEndpoint* _bulk_out);
53
54 /**
55 * Check the number of bytes available.
56 *
57 * @returns the number of bytes available
58 */
59 uint8_t available();
60
61 /**
62 * Attach a member function to call when a packet is received.
63 *
64 * @param tptr pointer to the object to call the member function on
65 * @param mptr pointer to the member function to be called
66 * @param irq irq type
67 */
68 template<typename T>
69 inline void attach(T* tptr, void (T::*mptr)(void), IrqType irq = RxIrq) {
70 if ((mptr != NULL) && (tptr != NULL)) {
71 if (irq == RxIrq) {
72 rx.attach(tptr, mptr);
73 } else {
74 tx.attach(tptr, mptr);
75 }
76 }
77 }
78
79 /**
80 * Attach a callback called when a packet is received
81 *
82 * @param ptr function pointer
83 */
84 inline void attach(void (*fn)(void), IrqType irq = RxIrq) {
85 if (fn != NULL) {
86 if (irq == RxIrq) {
87 rx.attach(fn);
88 } else {
89 tx.attach(fn);
90 }
91 }
92 }
93
94 /** Set the baud rate of the serial port
95 *
96 * @param baudrate The baudrate of the serial port (default = 9600).
97 */
98 void baud(int baudrate = 9600);
99
100 /** Set the transmission format used by the Serial port
101 *
102 * @param bits The number of bits in a word (default = 8)
103 * @param parity The parity used (USBHostSerialPort::None, USBHostSerialPort::Odd, USBHostSerialPort::Even, USBHostSerialPort::Mark, USBHostSerialPort::Space; default = USBHostSerialPort::None)
104 * @param stop The number of stop bits (1 or 2; default = 1)
105 */
106 void format(int bits = 8, Parity parity = USBHostSerialPort::None, int stop_bits = 1);
107 virtual int writeBuf(const char* b, int s);
108 virtual int readBuf(char* b, int s);
109
110 protected:
111 virtual int _getc();
112 virtual int _putc(int c);
113
114 private:
115 USBHost * host;
116 USBDeviceConnected * dev;
117
118 USBEndpoint * bulk_in;
119 USBEndpoint * bulk_out;
120 uint32_t size_bulk_in;
121 uint32_t size_bulk_out;
122
123 void init();
124
125 MtxCircBuffer<uint8_t, 128> circ_buf;
126
127 uint8_t buf[64];
128
129 typedef struct {
130 uint32_t baudrate;
131 uint8_t stop_bits;
132 uint8_t parity;
133 uint8_t data_bits;
134 } PACKED LINE_CODING;
135
136 LINE_CODING line_coding;
137
138 void rxHandler();
139 void txHandler();
140 FunctionPointer rx;
141 FunctionPointer tx;
142
143 uint8_t serial_intf;
144 };
145
146 #if (USBHOST_SERIAL <= 1)
147
148 class USBHostSerial : public IUSBEnumerator, public USBHostSerialPort
149 {
150 public:
151 USBHostSerial();
152
153 /**
154 * Try to connect a serial device
155 *
156 * @return true if connection was successful
157 */
158 bool connect();
159
160 void disconnect();
161
162 /**
163 * Check if a any serial port is connected
164 *
165 * @returns true if a serial device is connected
166 */
167 bool connected();
168
169 protected:
170 USBHost* host;
171 USBDeviceConnected* dev;
172 uint8_t port_intf;
173 int ports_found;
174
175 //From IUSBEnumerator
176 virtual void setVidPid(uint16_t vid, uint16_t pid);
177 virtual bool parseInterface(uint8_t intf_nb, uint8_t intf_class, uint8_t intf_subclass, uint8_t intf_protocol); //Must return true if the interface should be parsed
178 virtual bool useEndpoint(uint8_t intf_nb, ENDPOINT_TYPE type, ENDPOINT_DIRECTION dir); //Must return true if the endpoint will be used
179
180 private:
181 bool dev_connected;
182 };
183
184 #else // (USBHOST_SERIAL > 1)
185
186 class USBHostMultiSerial : public IUSBEnumerator {
187 public:
188 USBHostMultiSerial();
189 virtual ~USBHostMultiSerial();
190
191 USBHostSerialPort* getPort(int port)
192 {
193 return port < USBHOST_SERIAL ? ports[port] : NULL;
194 }
195
196 /**
197 * Try to connect a serial device
198 *
199 * @return true if connection was successful
200 */
201 bool connect();
202
203 void disconnect();
204
205 /**
206 * Check if a any serial port is connected
207 *
208 * @returns true if a serial device is connected
209 */
210 bool connected();
211
212 protected:
213 USBHost* host;
214 USBDeviceConnected* dev;
215 USBHostSerialPort* ports[USBHOST_SERIAL];
216 uint8_t port_intf[USBHOST_SERIAL];
217 int ports_found;
218
219 //From IUSBEnumerator
220 virtual void setVidPid(uint16_t vid, uint16_t pid);
221 virtual bool parseInterface(uint8_t intf_nb, uint8_t intf_class, uint8_t intf_subclass, uint8_t intf_protocol); //Must return true if the interface should be parsed
222 virtual bool useEndpoint(uint8_t intf_nb, ENDPOINT_TYPE type, ENDPOINT_DIRECTION dir); //Must return true if the endpoint will be used
223
224 private:
225 bool dev_connected;
226 };
227 #endif // (USBHOST_SERIAL <= 1)
228
229 #endif
230
231 #endif
Imprint / Impressum