Change USBSerial.h constructor to allow to label the stream for, eg., redirecting stdin/out/err to a USBSerial object. Will not break existing default use as tested with mbed-os 5.11.5
Dependents: max32630fthr_stdout2usb_mbed_v5-11-5 firmware_framework1
USBSerial/USBSerial.h@72:6ef9c5c0250e, 2019-04-30 (annotated)
- Committer:
- psionprime
- Date:
- Tue Apr 30 21:38:03 2019 +0000
- Revision:
- 72:6ef9c5c0250e
- Parent:
- 71:bf73f57c4b9c
- updated docs
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
samux | 1:80ab0d068708 | 1 | /* Copyright (c) 2010-2011 mbed.org, MIT License |
samux | 1:80ab0d068708 | 2 | * |
samux | 1:80ab0d068708 | 3 | * Permission is hereby granted, free of charge, to any person obtaining a copy of this software |
samux | 1:80ab0d068708 | 4 | * and associated documentation files (the "Software"), to deal in the Software without |
samux | 1:80ab0d068708 | 5 | * restriction, including without limitation the rights to use, copy, modify, merge, publish, |
samux | 1:80ab0d068708 | 6 | * distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the |
samux | 1:80ab0d068708 | 7 | * Software is furnished to do so, subject to the following conditions: |
samux | 1:80ab0d068708 | 8 | * |
samux | 1:80ab0d068708 | 9 | * The above copyright notice and this permission notice shall be included in all copies or |
samux | 1:80ab0d068708 | 10 | * substantial portions of the Software. |
samux | 1:80ab0d068708 | 11 | * |
samux | 1:80ab0d068708 | 12 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING |
samux | 1:80ab0d068708 | 13 | * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
samux | 1:80ab0d068708 | 14 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, |
samux | 1:80ab0d068708 | 15 | * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
samux | 1:80ab0d068708 | 16 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
samux | 1:80ab0d068708 | 17 | */ |
samux | 1:80ab0d068708 | 18 | |
samux | 1:80ab0d068708 | 19 | #ifndef USBSERIAL_H |
samux | 1:80ab0d068708 | 20 | #define USBSERIAL_H |
samux | 1:80ab0d068708 | 21 | |
samux | 1:80ab0d068708 | 22 | #include "USBCDC.h" |
samux | 1:80ab0d068708 | 23 | #include "Stream.h" |
samux | 1:80ab0d068708 | 24 | #include "CircBuffer.h" |
samux | 1:80ab0d068708 | 25 | |
samux | 1:80ab0d068708 | 26 | |
samux | 1:80ab0d068708 | 27 | /** |
psionprime | 72:6ef9c5c0250e | 28 | * USBSerial original example |
samux | 1:80ab0d068708 | 29 | * |
samux | 1:80ab0d068708 | 30 | * @code |
samux | 1:80ab0d068708 | 31 | * #include "mbed.h" |
samux | 1:80ab0d068708 | 32 | * #include "USBSerial.h" |
samux | 1:80ab0d068708 | 33 | * |
samux | 1:80ab0d068708 | 34 | * //Virtual serial port over USB |
samux | 1:80ab0d068708 | 35 | * USBSerial serial; |
samux | 1:80ab0d068708 | 36 | * |
samux | 1:80ab0d068708 | 37 | * int main(void) { |
samux | 1:80ab0d068708 | 38 | * |
samux | 1:80ab0d068708 | 39 | * while(1) |
samux | 1:80ab0d068708 | 40 | * { |
samux | 1:80ab0d068708 | 41 | * serial.printf("I am a virtual serial port\n"); |
samux | 1:80ab0d068708 | 42 | * wait(1); |
samux | 1:80ab0d068708 | 43 | * } |
samux | 1:80ab0d068708 | 44 | * } |
samux | 1:80ab0d068708 | 45 | * @endcode |
samux | 1:80ab0d068708 | 46 | */ |
psionprime | 72:6ef9c5c0250e | 47 | /** |
psionprime | 72:6ef9c5c0250e | 48 | * USBSerial stdio redirect example |
psionprime | 72:6ef9c5c0250e | 49 | * |
psionprime | 72:6ef9c5c0250e | 50 | * @code |
psionprime | 72:6ef9c5c0250e | 51 | * #include "mbed.h" |
psionprime | 72:6ef9c5c0250e | 52 | * #include "USBSerial.h" |
psionprime | 72:6ef9c5c0250e | 53 | * #include <stdio.h> |
psionprime | 72:6ef9c5c0250e | 54 | * #include <errno.h> |
psionprime | 72:6ef9c5c0250e | 55 | * |
psionprime | 72:6ef9c5c0250e | 56 | * //Virtual serial port over USB |
psionprime | 72:6ef9c5c0250e | 57 | * USBSerial usbSerial("usb"); |
psionprime | 72:6ef9c5c0250e | 58 | * |
psionprime | 72:6ef9c5c0250e | 59 | * int main(void) { |
psionprime | 72:6ef9c5c0250e | 60 | * // redirect stdin, stdout, stderr to USBSerial object |
psionprime | 72:6ef9c5c0250e | 61 | * freopen("/usb", "r", stdin); |
psionprime | 72:6ef9c5c0250e | 62 | * freopen("/usb", "w", stdout); |
psionprime | 72:6ef9c5c0250e | 63 | * freopen("/usb", "w", stderr); |
psionprime | 72:6ef9c5c0250e | 64 | * |
psionprime | 72:6ef9c5c0250e | 65 | * // wait until user ready |
psionprime | 72:6ef9c5c0250e | 66 | * while(!usbSerial.terminal_connected) { |
psionprime | 72:6ef9c5c0250e | 67 | * ThisThread::yield(); |
psionprime | 72:6ef9c5c0250e | 68 | * } |
psionprime | 72:6ef9c5c0250e | 69 | * |
psionprime | 72:6ef9c5c0250e | 70 | * while(1) |
psionprime | 72:6ef9c5c0250e | 71 | * { |
psionprime | 72:6ef9c5c0250e | 72 | * printf("--- mbed-os 5.11.5 stdout redirect to USBSerial ---\n"); |
psionprime | 72:6ef9c5c0250e | 73 | * wait(1); |
psionprime | 72:6ef9c5c0250e | 74 | * } |
psionprime | 72:6ef9c5c0250e | 75 | * } |
psionprime | 72:6ef9c5c0250e | 76 | * @endcode |
psionprime | 72:6ef9c5c0250e | 77 | */ |
samux | 1:80ab0d068708 | 78 | class USBSerial: public USBCDC, public Stream { |
samux | 1:80ab0d068708 | 79 | public: |
samux | 1:80ab0d068708 | 80 | |
samux | 1:80ab0d068708 | 81 | /** |
samux | 1:80ab0d068708 | 82 | * Constructor |
samux | 1:80ab0d068708 | 83 | * |
samux | 1:80ab0d068708 | 84 | * @param vendor_id Your vendor_id (default: 0x1f00) |
samux | 1:80ab0d068708 | 85 | * @param product_id Your product_id (default: 0x2012) |
samux | 1:80ab0d068708 | 86 | * @param product_release Your preoduct_release (default: 0x0001) |
mbed_official | 19:fcb63a105965 | 87 | * @param connect_blocking define if the connection must be blocked if USB not plugged in |
samux | 1:80ab0d068708 | 88 | * |
samux | 1:80ab0d068708 | 89 | */ |
psionprime | 71:bf73f57c4b9c | 90 | USBSerial(uint16_t vendor_id = 0x1f00, uint16_t product_id = 0x2012, uint16_t product_release = 0x0001, bool connect_blocking = true): USBCDC(vendor_id, product_id, product_release, connect_blocking) { |
psionprime | 71:bf73f57c4b9c | 91 | settingsChangedCallback = 0; |
psionprime | 71:bf73f57c4b9c | 92 | }; |
psionprime | 71:bf73f57c4b9c | 93 | |
psionprime | 72:6ef9c5c0250e | 94 | /** |
psionprime | 72:6ef9c5c0250e | 95 | * Constructor |
psionprime | 72:6ef9c5c0250e | 96 | * |
psionprime | 72:6ef9c5c0250e | 97 | * @param name Your name (default: NULL) (passed to Stream to enable stdio redirection) |
psionprime | 72:6ef9c5c0250e | 98 | * @param vendor_id Your vendor_id (default: 0x1f00) |
psionprime | 72:6ef9c5c0250e | 99 | * @param product_id Your product_id (default: 0x2012) |
psionprime | 72:6ef9c5c0250e | 100 | * @param product_release Your preoduct_release (default: 0x0001) |
psionprime | 72:6ef9c5c0250e | 101 | * @param connect_blocking define if the connection must be blocked if USB not plugged in |
psionprime | 72:6ef9c5c0250e | 102 | * |
psionprime | 72:6ef9c5c0250e | 103 | */ |
psionprime | 70:ce08eda95816 | 104 | USBSerial(const char *name = NULL, uint16_t vendor_id = 0x1f00, uint16_t product_id = 0x2012, uint16_t product_release = 0x0001, bool connect_blocking = true): USBCDC(vendor_id, product_id, product_release, connect_blocking), Stream(name){ |
mbed_official | 15:849c0c0f2769 | 105 | settingsChangedCallback = 0; |
mbed_official | 15:849c0c0f2769 | 106 | }; |
samux | 1:80ab0d068708 | 107 | |
samux | 1:80ab0d068708 | 108 | |
samux | 1:80ab0d068708 | 109 | /** |
samux | 1:80ab0d068708 | 110 | * Send a character. You can use puts, printf. |
samux | 1:80ab0d068708 | 111 | * |
samux | 1:80ab0d068708 | 112 | * @param c character to be sent |
samux | 1:80ab0d068708 | 113 | * @returns true if there is no error, false otherwise |
samux | 1:80ab0d068708 | 114 | */ |
samux | 1:80ab0d068708 | 115 | virtual int _putc(int c); |
mbed_official | 25:7c72828865f3 | 116 | |
samux | 1:80ab0d068708 | 117 | /** |
samux | 1:80ab0d068708 | 118 | * Read a character: blocking |
samux | 1:80ab0d068708 | 119 | * |
samux | 1:80ab0d068708 | 120 | * @returns character read |
samux | 1:80ab0d068708 | 121 | */ |
samux | 1:80ab0d068708 | 122 | virtual int _getc(); |
mbed_official | 25:7c72828865f3 | 123 | |
samux | 1:80ab0d068708 | 124 | /** |
samux | 1:80ab0d068708 | 125 | * Check the number of bytes available. |
samux | 1:80ab0d068708 | 126 | * |
samux | 1:80ab0d068708 | 127 | * @returns the number of bytes available |
samux | 1:80ab0d068708 | 128 | */ |
mbed_official | 25:7c72828865f3 | 129 | uint8_t available(); |
mbed_official | 15:849c0c0f2769 | 130 | |
mbed_official | 15:849c0c0f2769 | 131 | /** Determine if there is a character available to read |
mbed_official | 15:849c0c0f2769 | 132 | * |
mbed_official | 15:849c0c0f2769 | 133 | * @returns |
mbed_official | 15:849c0c0f2769 | 134 | * 1 if there is a character available to read, |
mbed_official | 15:849c0c0f2769 | 135 | * 0 otherwise |
mbed_official | 15:849c0c0f2769 | 136 | */ |
mbed_official | 15:849c0c0f2769 | 137 | int readable() { return available() ? 1 : 0; } |
mbed_official | 25:7c72828865f3 | 138 | |
mbed_official | 15:849c0c0f2769 | 139 | /** Determine if there is space available to write a character |
mbed_official | 15:849c0c0f2769 | 140 | * |
mbed_official | 15:849c0c0f2769 | 141 | * @returns |
mbed_official | 15:849c0c0f2769 | 142 | * 1 if there is space to write a character, |
mbed_official | 15:849c0c0f2769 | 143 | * 0 otherwise |
mbed_official | 15:849c0c0f2769 | 144 | */ |
mbed_official | 15:849c0c0f2769 | 145 | int writeable() { return 1; } // always return 1, for write operation is blocking |
mbed_official | 25:7c72828865f3 | 146 | |
samux | 1:80ab0d068708 | 147 | /** |
mbed_official | 25:7c72828865f3 | 148 | * Write a block of data. |
samux | 1:80ab0d068708 | 149 | * |
samux | 1:80ab0d068708 | 150 | * For more efficiency, a block of size 64 (maximum size of a bulk endpoint) has to be written. |
samux | 1:80ab0d068708 | 151 | * |
samux | 1:80ab0d068708 | 152 | * @param buf pointer on data which will be written |
samux | 1:80ab0d068708 | 153 | * @param size size of the buffer. The maximum size of a block is limited by the size of the endpoint (64 bytes) |
samux | 1:80ab0d068708 | 154 | * |
samux | 1:80ab0d068708 | 155 | * @returns true if successfull |
samux | 1:80ab0d068708 | 156 | */ |
samux | 1:80ab0d068708 | 157 | bool writeBlock(uint8_t * buf, uint16_t size); |
samux | 1:80ab0d068708 | 158 | |
samux | 1:80ab0d068708 | 159 | /** |
mbed_official | 25:7c72828865f3 | 160 | * Attach a member function to call when a packet is received. |
samux | 1:80ab0d068708 | 161 | * |
samux | 1:80ab0d068708 | 162 | * @param tptr pointer to the object to call the member function on |
samux | 1:80ab0d068708 | 163 | * @param mptr pointer to the member function to be called |
samux | 1:80ab0d068708 | 164 | */ |
samux | 1:80ab0d068708 | 165 | template<typename T> |
samux | 1:80ab0d068708 | 166 | void attach(T* tptr, void (T::*mptr)(void)) { |
samux | 1:80ab0d068708 | 167 | if((mptr != NULL) && (tptr != NULL)) { |
samux | 1:80ab0d068708 | 168 | rx.attach(tptr, mptr); |
samux | 1:80ab0d068708 | 169 | } |
samux | 1:80ab0d068708 | 170 | } |
samux | 1:80ab0d068708 | 171 | |
samux | 1:80ab0d068708 | 172 | /** |
samux | 1:80ab0d068708 | 173 | * Attach a callback called when a packet is received |
samux | 1:80ab0d068708 | 174 | * |
samux | 1:80ab0d068708 | 175 | * @param fptr function pointer |
samux | 1:80ab0d068708 | 176 | */ |
mbed_official | 15:849c0c0f2769 | 177 | void attach(void (*fptr)(void)) { |
mbed_official | 15:849c0c0f2769 | 178 | if(fptr != NULL) { |
mbed_official | 15:849c0c0f2769 | 179 | rx.attach(fptr); |
samux | 1:80ab0d068708 | 180 | } |
samux | 1:80ab0d068708 | 181 | } |
samux | 1:80ab0d068708 | 182 | |
mbed_official | 15:849c0c0f2769 | 183 | /** |
mbed_official | 15:849c0c0f2769 | 184 | * Attach a callback to call when serial's settings are changed. |
mbed_official | 15:849c0c0f2769 | 185 | * |
mbed_official | 15:849c0c0f2769 | 186 | * @param fptr function pointer |
mbed_official | 15:849c0c0f2769 | 187 | */ |
mbed_official | 15:849c0c0f2769 | 188 | void attach(void (*fptr)(int baud, int bits, int parity, int stop)) { |
mbed_official | 15:849c0c0f2769 | 189 | settingsChangedCallback = fptr; |
mbed_official | 15:849c0c0f2769 | 190 | } |
samux | 1:80ab0d068708 | 191 | |
samux | 1:80ab0d068708 | 192 | protected: |
mbed_official | 47:a0cd9646ecd1 | 193 | virtual bool EPBULK_OUT_callback(); |
mbed_official | 15:849c0c0f2769 | 194 | virtual void lineCodingChanged(int baud, int bits, int parity, int stop){ |
mbed_official | 15:849c0c0f2769 | 195 | if (settingsChangedCallback) { |
mbed_official | 15:849c0c0f2769 | 196 | settingsChangedCallback(baud, bits, parity, stop); |
mbed_official | 15:849c0c0f2769 | 197 | } |
mbed_official | 15:849c0c0f2769 | 198 | } |
samux | 1:80ab0d068708 | 199 | |
samux | 1:80ab0d068708 | 200 | private: |
samux | 1:80ab0d068708 | 201 | FunctionPointer rx; |
mbed_official | 51:deafa44182d9 | 202 | CircBuffer<uint8_t,128> buf; |
mbed_official | 15:849c0c0f2769 | 203 | void (*settingsChangedCallback)(int baud, int bits, int parity, int stop); |
samux | 1:80ab0d068708 | 204 | }; |
samux | 1:80ab0d068708 | 205 | |
samux | 1:80ab0d068708 | 206 | #endif |