1 /* mbed USBHost 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.
17 #ifndef USBHOSTSERIAL_H
18 #define USBHOSTSERIAL_H
20 #include "USBHostConf.h"
26 #include "MtxCircBuffer.h"
29 * A class to communicate a USB virtual serial port
31 class USBHostSerialPort : public Stream {
51 void connect(USBHost* _host, USBDeviceConnected * _dev,
52 uint8_t _serial_intf, USBEndpoint* _bulk_in, USBEndpoint* _bulk_out);
55 * Check the number of bytes available.
57 * @returns the number of bytes available
62 * Attach a member function to call when a packet is received.
64 * @param tptr pointer to the object to call the member function on
65 * @param mptr pointer to the member function to be called
69 inline void attach(T* tptr, void (T::*mptr)(void), IrqType irq = RxIrq) {
70 if ((mptr != NULL) && (tptr != NULL)) {
72 rx.attach(tptr, mptr);
74 tx.attach(tptr, mptr);
80 * Attach a callback called when a packet is received
82 * @param ptr function pointer
84 inline void attach(void (*fn)(void), IrqType irq = RxIrq) {
94 /** Set the baud rate of the serial port
96 * @param baudrate The baudrate of the serial port (default = 9600).
98 void baud(int baudrate = 9600);
100 /** Set the transmission format used by the Serial port
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)
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);
112 virtual int _putc(int c);
116 USBDeviceConnected * dev;
118 USBEndpoint * bulk_in;
119 USBEndpoint * bulk_out;
120 uint32_t size_bulk_in;
121 uint32_t size_bulk_out;
125 MtxCircBuffer<uint8_t, 128> circ_buf;
134 } PACKED LINE_CODING;
136 LINE_CODING line_coding;
146 #if (USBHOST_SERIAL <= 1)
148 class USBHostSerial : public IUSBEnumerator, public USBHostSerialPort
154 * Try to connect a serial device
156 * @return true if connection was successful
163 * Check if a any serial port is connected
165 * @returns true if a serial device is connected
171 USBDeviceConnected* dev;
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
184 #else // (USBHOST_SERIAL > 1)
186 class USBHostMultiSerial : public IUSBEnumerator {
188 USBHostMultiSerial();
189 virtual ~USBHostMultiSerial();
191 USBHostSerialPort* getPort(int port)
193 return port < USBHOST_SERIAL ? ports[port] : NULL;
197 * Try to connect a serial device
199 * @return true if connection was successful
206 * Check if a any serial port is connected
208 * @returns true if a serial device is connected
214 USBDeviceConnected* dev;
215 USBHostSerialPort* ports[USBHOST_SERIAL];
216 uint8_t port_intf[USBHOST_SERIAL];
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
227 #endif // (USBHOST_SERIAL <= 1)