Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Fork of USBHost by
USBHostSerial.h
00001 /* mbed USBHost 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 00017 #ifndef USBHOSTSERIAL_H 00018 #define USBHOSTSERIAL_H 00019 00020 #include "USBHostConf.h" 00021 00022 #if USBHOST_SERIAL 00023 00024 #include "USBHost.h" 00025 #include "Stream.h" 00026 #include "MtxCircBuffer.h" 00027 00028 /** 00029 * A class to communicate a USB virtual serial port 00030 */ 00031 class USBHostSerial : public IUSBEnumerator, public Stream { 00032 public: 00033 /** 00034 * Constructor 00035 */ 00036 USBHostSerial(); 00037 00038 enum IrqType { 00039 RxIrq, 00040 TxIrq 00041 }; 00042 00043 enum Parity { 00044 None = 0, 00045 Odd, 00046 Even, 00047 Mark, 00048 Space 00049 }; 00050 00051 /** 00052 * Check if a virtual serial port is connected 00053 * 00054 * @returns true if a serial device is connected 00055 */ 00056 bool connected(); 00057 00058 /** 00059 * Try to connect a serial device 00060 * 00061 * @return true if connection was successful 00062 */ 00063 bool connect(); 00064 00065 /** 00066 * Check the number of bytes available. 00067 * 00068 * @returns the number of bytes available 00069 */ 00070 uint8_t available(); 00071 00072 /** 00073 * Attach a member function to call when a packet is received. 00074 * 00075 * @param tptr pointer to the object to call the member function on 00076 * @param mptr pointer to the member function to be called 00077 * @param irq irq type 00078 */ 00079 template<typename T> 00080 inline void attach(T* tptr, void (T::*mptr)(void), IrqType irq = RxIrq) { 00081 if ((mptr != NULL) && (tptr != NULL)) { 00082 if (irq == RxIrq) { 00083 rx.attach(tptr, mptr); 00084 } else { 00085 tx.attach(tptr, mptr); 00086 } 00087 } 00088 } 00089 00090 /** 00091 * Attach a callback called when a packet is received 00092 * 00093 * @param ptr function pointer 00094 */ 00095 inline void attach(void (*fn)(void), IrqType irq = RxIrq) { 00096 if (fn != NULL) { 00097 if (irq == RxIrq) { 00098 rx.attach(fn); 00099 } else { 00100 tx.attach(fn); 00101 } 00102 } 00103 } 00104 00105 /** Set the baud rate of the serial port 00106 * 00107 * @param baudrate The baudrate of the serial port (default = 9600). 00108 */ 00109 void baud(int baudrate = 9600); 00110 00111 /** Set the transmission format used by the Serial port 00112 * 00113 * @param bits The number of bits in a word (default = 8) 00114 * @param parity The parity used (USBHostSerial::None, USBHostSerial::Odd, USBHostSerial::Even, USBHostSerial::Mark, USBHostSerial::Space; default = USBHostSerial::None) 00115 * @param stop The number of stop bits (1 or 2; default = 1) 00116 */ 00117 void format(int bits = 8, Parity parity = USBHostSerial::None, int stop_bits = 1); 00118 00119 00120 protected: 00121 //From IUSBEnumerator 00122 virtual void setVidPid(uint16_t vid, uint16_t pid); 00123 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 00124 virtual bool useEndpoint(uint8_t intf_nb, ENDPOINT_TYPE type, ENDPOINT_DIRECTION dir); //Must return true if the endpoint will be used 00125 00126 virtual int _getc(); 00127 virtual int _putc(int c); 00128 00129 private: 00130 USBHost * host; 00131 USBDeviceConnected * dev; 00132 USBEndpoint * bulk_in; 00133 USBEndpoint * bulk_out; 00134 uint32_t size_bulk_in; 00135 uint32_t size_bulk_out; 00136 00137 bool dev_connected; 00138 00139 void init(); 00140 00141 MtxCircBuffer<uint8_t, 64> circ_buf; 00142 00143 uint8_t buf[64]; 00144 00145 typedef __packed struct { 00146 uint32_t baudrate; 00147 uint8_t stop_bits; 00148 uint8_t parity; 00149 uint8_t data_bits; 00150 } LINE_CODING; 00151 00152 LINE_CODING line_coding; 00153 00154 void rxHandler(); 00155 void txHandler(); 00156 FunctionPointer rx; 00157 FunctionPointer tx; 00158 00159 int serial_intf; 00160 bool serial_device_found; 00161 00162 }; 00163 00164 #endif 00165 00166 #endif
Generated on Sat Jul 16 2022 04:39:31 by
