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.
Dependents: Example_WatchDog_Timer
USBSerial.h
00001 /* Copyright (c) 2010-2011 mbed.org, MIT License 00002 * 00003 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software 00004 * and associated documentation files (the "Software"), to deal in the Software without 00005 * restriction, including without limitation the rights to use, copy, modify, merge, publish, 00006 * distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the 00007 * Software is furnished to do so, subject to the following conditions: 00008 * 00009 * The above copyright notice and this permission notice shall be included in all copies or 00010 * substantial portions of the Software. 00011 * 00012 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING 00013 * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 00014 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, 00015 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 00016 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 00017 */ 00018 00019 #ifndef USBSERIAL_H 00020 #define USBSERIAL_H 00021 00022 #include "USBCDC.h" 00023 #include "Stream.h" 00024 #include "CircBuffer.h" 00025 #include "Callback.h" 00026 00027 /** 00028 * USBSerial example 00029 * 00030 * @code 00031 * #include "mbed.h" 00032 * #include "USBSerial.h" 00033 * 00034 * //Virtual serial port over USB 00035 * USBSerial serial; 00036 * 00037 * int main(void) { 00038 * 00039 * while(1) 00040 * { 00041 * serial.printf("I am a virtual serial port\n"); 00042 * wait(1); 00043 * } 00044 * } 00045 * @endcode 00046 */ 00047 class USBSerial: public USBCDC, public Stream { 00048 public: 00049 00050 /** 00051 * Constructor 00052 * 00053 * @param vendor_id Your vendor_id (default: 0x1f00) 00054 * @param product_id Your product_id (default: 0x2012) 00055 * @param product_release Your preoduct_release (default: 0x0001) 00056 * @param connect_blocking define if the connection must be blocked if USB not plugged in 00057 * 00058 */ 00059 USBSerial(uint16_t vendor_id = 0x1f00, uint16_t product_id = 0x2012, uint16_t product_release = 0x0001, bool connect_blocking = false): USBCDC(vendor_id, product_id, product_release, connect_blocking){ 00060 settingsChangedCallback = 0; 00061 }; 00062 00063 00064 /** 00065 * Send a character. You can use puts, printf. 00066 * 00067 * @param c character to be sent 00068 * @returns true if there is no error, false otherwise 00069 */ 00070 virtual int _putc(int c); 00071 00072 /** 00073 * Read a character: blocking 00074 * 00075 * @returns character read 00076 */ 00077 virtual int _getc(); 00078 00079 /** 00080 * Read a string of characters 00081 * 00082 * @returns character read 00083 */ 00084 virtual int _read(void* buffer, int length); 00085 virtual int _write(uint8_t * buf, uint16_t size); 00086 /** 00087 * Check the number of bytes available. 00088 * 00089 * @returns the number of bytes available 00090 */ 00091 uint8_t available(); 00092 00093 /** 00094 * Check if the terminal is connected. 00095 * 00096 * @returns connection status 00097 */ 00098 bool connected(); 00099 00100 /** Determine if there is a character available to read 00101 * 00102 * @returns 00103 * 1 if there is a character available to read, 00104 * 0 otherwise 00105 */ 00106 int readable() { return available() ? 1 : 0; } 00107 00108 /** Determine if there is space available to write a character 00109 * 00110 * @returns 00111 * 1 if there is space to write a character, 00112 * 0 otherwise 00113 */ 00114 int writeable() { return 1; } // always return 1, for write operation is blocking 00115 00116 /** 00117 * Write a block of data. 00118 * 00119 * For more efficiency, a block of size 64 (maximum size of a bulk endpoint) has to be written. 00120 * 00121 * @param buf pointer on data which will be written 00122 * @param size size of the buffer. The maximum size of a block is limited by the size of the endpoint (64 bytes) 00123 * 00124 * @returns true if successfull 00125 */ 00126 bool writeBlock(uint8_t * buf, uint16_t size); 00127 00128 /** 00129 * Attach a member function to call when a packet is received. 00130 * 00131 * @param tptr pointer to the object to call the member function on 00132 * @param mptr pointer to the member function to be called 00133 */ 00134 template<typename T> 00135 void attach(T* tptr, void (T::*mptr)(void)) { 00136 if((mptr != NULL) && (tptr != NULL)) { 00137 rx = Callback<void()>(mptr, tptr); 00138 } 00139 } 00140 00141 /** 00142 * Attach a callback called when a packet is received 00143 * 00144 * @param fptr function pointer 00145 */ 00146 void attach(void (*fptr)(void)) { 00147 if(fptr != NULL) { 00148 rx = Callback<void()>(fptr); 00149 } 00150 } 00151 00152 /** 00153 * Attach a Callback called when a packet is received 00154 * 00155 * @param cb Callback to attach 00156 */ 00157 void attach(Callback<void()> &cb) { 00158 rx = cb; 00159 } 00160 00161 /** 00162 * Attach a callback to call when serial's settings are changed. 00163 * 00164 * @param fptr function pointer 00165 */ 00166 void attach(void (*fptr)(int baud, int bits, int parity, int stop)) { 00167 settingsChangedCallback = fptr; 00168 } 00169 00170 protected: 00171 virtual bool EPBULK_OUT_callback(); 00172 virtual void lineCodingChanged(int baud, int bits, int parity, int stop){ 00173 if (settingsChangedCallback) { 00174 settingsChangedCallback(baud, bits, parity, stop); 00175 } 00176 } 00177 00178 private: 00179 Callback<void()> rx; 00180 CircBuffer<uint8_t,128> buf; 00181 void (*settingsChangedCallback)(int baud, int bits, int parity, int stop); 00182 }; 00183 00184 #endif
Generated on Mon Jul 18 2022 23:08:53 by
1.7.2