USB CDC (serial) and USB MSC (strage) Composite Device. http://mbed.org/users/okini3939/notebook/USB_Device/
Dependencies: ChaNFSSD mbed ChaNFS
USBDevice/USBSERIAL/USBSerial.h@0:9b1d17d54055, 2011-12-16 (annotated)
- Committer:
- okini3939
- Date:
- Fri Dec 16 15:22:36 2011 +0000
- Revision:
- 0:9b1d17d54055
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
okini3939 | 0:9b1d17d54055 | 1 | /* Copyright (c) 2010-2011 mbed.org, MIT License |
okini3939 | 0:9b1d17d54055 | 2 | * |
okini3939 | 0:9b1d17d54055 | 3 | * Permission is hereby granted, free of charge, to any person obtaining a copy of this software |
okini3939 | 0:9b1d17d54055 | 4 | * and associated documentation files (the "Software"), to deal in the Software without |
okini3939 | 0:9b1d17d54055 | 5 | * restriction, including without limitation the rights to use, copy, modify, merge, publish, |
okini3939 | 0:9b1d17d54055 | 6 | * distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the |
okini3939 | 0:9b1d17d54055 | 7 | * Software is furnished to do so, subject to the following conditions: |
okini3939 | 0:9b1d17d54055 | 8 | * |
okini3939 | 0:9b1d17d54055 | 9 | * The above copyright notice and this permission notice shall be included in all copies or |
okini3939 | 0:9b1d17d54055 | 10 | * substantial portions of the Software. |
okini3939 | 0:9b1d17d54055 | 11 | * |
okini3939 | 0:9b1d17d54055 | 12 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING |
okini3939 | 0:9b1d17d54055 | 13 | * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
okini3939 | 0:9b1d17d54055 | 14 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, |
okini3939 | 0:9b1d17d54055 | 15 | * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
okini3939 | 0:9b1d17d54055 | 16 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
okini3939 | 0:9b1d17d54055 | 17 | */ |
okini3939 | 0:9b1d17d54055 | 18 | |
okini3939 | 0:9b1d17d54055 | 19 | #ifndef USBSERIAL_H |
okini3939 | 0:9b1d17d54055 | 20 | #define USBSERIAL_H |
okini3939 | 0:9b1d17d54055 | 21 | |
okini3939 | 0:9b1d17d54055 | 22 | #include "USBCDC.h" |
okini3939 | 0:9b1d17d54055 | 23 | #include "Stream.h" |
okini3939 | 0:9b1d17d54055 | 24 | #include "CircBuffer.h" |
okini3939 | 0:9b1d17d54055 | 25 | |
okini3939 | 0:9b1d17d54055 | 26 | |
okini3939 | 0:9b1d17d54055 | 27 | /** |
okini3939 | 0:9b1d17d54055 | 28 | * USBSerial example |
okini3939 | 0:9b1d17d54055 | 29 | * |
okini3939 | 0:9b1d17d54055 | 30 | * @code |
okini3939 | 0:9b1d17d54055 | 31 | * #include "mbed.h" |
okini3939 | 0:9b1d17d54055 | 32 | * #include "USBSerial.h" |
okini3939 | 0:9b1d17d54055 | 33 | * |
okini3939 | 0:9b1d17d54055 | 34 | * //Virtual serial port over USB |
okini3939 | 0:9b1d17d54055 | 35 | * USBSerial serial; |
okini3939 | 0:9b1d17d54055 | 36 | * |
okini3939 | 0:9b1d17d54055 | 37 | * int main(void) { |
okini3939 | 0:9b1d17d54055 | 38 | * |
okini3939 | 0:9b1d17d54055 | 39 | * while(1) |
okini3939 | 0:9b1d17d54055 | 40 | * { |
okini3939 | 0:9b1d17d54055 | 41 | * serial.printf("I am a virtual serial port\n"); |
okini3939 | 0:9b1d17d54055 | 42 | * wait(1); |
okini3939 | 0:9b1d17d54055 | 43 | * } |
okini3939 | 0:9b1d17d54055 | 44 | * } |
okini3939 | 0:9b1d17d54055 | 45 | * @endcode |
okini3939 | 0:9b1d17d54055 | 46 | */ |
okini3939 | 0:9b1d17d54055 | 47 | class USBSerial: public USBCDC, public Stream { |
okini3939 | 0:9b1d17d54055 | 48 | public: |
okini3939 | 0:9b1d17d54055 | 49 | |
okini3939 | 0:9b1d17d54055 | 50 | /** |
okini3939 | 0:9b1d17d54055 | 51 | * Constructor |
okini3939 | 0:9b1d17d54055 | 52 | * |
okini3939 | 0:9b1d17d54055 | 53 | * @param vendor_id Your vendor_id (default: 0x1f00) |
okini3939 | 0:9b1d17d54055 | 54 | * @param product_id Your product_id (default: 0x2012) |
okini3939 | 0:9b1d17d54055 | 55 | * @param product_release Your preoduct_release (default: 0x0001) |
okini3939 | 0:9b1d17d54055 | 56 | * |
okini3939 | 0:9b1d17d54055 | 57 | */ |
okini3939 | 0:9b1d17d54055 | 58 | USBSerial(uint16_t vendor_id = 0x1f00, uint16_t product_id = 0x2012, uint16_t product_release = 0x0001): USBCDC(vendor_id, product_id, product_release), buf(128){ }; |
okini3939 | 0:9b1d17d54055 | 59 | |
okini3939 | 0:9b1d17d54055 | 60 | |
okini3939 | 0:9b1d17d54055 | 61 | /** |
okini3939 | 0:9b1d17d54055 | 62 | * Send a character. You can use puts, printf. |
okini3939 | 0:9b1d17d54055 | 63 | * |
okini3939 | 0:9b1d17d54055 | 64 | * @param c character to be sent |
okini3939 | 0:9b1d17d54055 | 65 | * @returns true if there is no error, false otherwise |
okini3939 | 0:9b1d17d54055 | 66 | */ |
okini3939 | 0:9b1d17d54055 | 67 | virtual int _putc(int c); |
okini3939 | 0:9b1d17d54055 | 68 | |
okini3939 | 0:9b1d17d54055 | 69 | /** |
okini3939 | 0:9b1d17d54055 | 70 | * Read a character: blocking |
okini3939 | 0:9b1d17d54055 | 71 | * |
okini3939 | 0:9b1d17d54055 | 72 | * @returns character read |
okini3939 | 0:9b1d17d54055 | 73 | */ |
okini3939 | 0:9b1d17d54055 | 74 | virtual int _getc(); |
okini3939 | 0:9b1d17d54055 | 75 | |
okini3939 | 0:9b1d17d54055 | 76 | /** |
okini3939 | 0:9b1d17d54055 | 77 | * Check the number of bytes available. |
okini3939 | 0:9b1d17d54055 | 78 | * |
okini3939 | 0:9b1d17d54055 | 79 | * @returns the number of bytes available |
okini3939 | 0:9b1d17d54055 | 80 | */ |
okini3939 | 0:9b1d17d54055 | 81 | uint8_t available(); |
okini3939 | 0:9b1d17d54055 | 82 | |
okini3939 | 0:9b1d17d54055 | 83 | /** |
okini3939 | 0:9b1d17d54055 | 84 | * Write a block of data. |
okini3939 | 0:9b1d17d54055 | 85 | * |
okini3939 | 0:9b1d17d54055 | 86 | * For more efficiency, a block of size 64 (maximum size of a bulk endpoint) has to be written. |
okini3939 | 0:9b1d17d54055 | 87 | * |
okini3939 | 0:9b1d17d54055 | 88 | * @param buf pointer on data which will be written |
okini3939 | 0:9b1d17d54055 | 89 | * @param size size of the buffer. The maximum size of a block is limited by the size of the endpoint (64 bytes) |
okini3939 | 0:9b1d17d54055 | 90 | * |
okini3939 | 0:9b1d17d54055 | 91 | * @returns true if successfull |
okini3939 | 0:9b1d17d54055 | 92 | */ |
okini3939 | 0:9b1d17d54055 | 93 | bool writeBlock(uint8_t * buf, uint16_t size); |
okini3939 | 0:9b1d17d54055 | 94 | |
okini3939 | 0:9b1d17d54055 | 95 | /** |
okini3939 | 0:9b1d17d54055 | 96 | * Attach a member function to call when a packet is received. |
okini3939 | 0:9b1d17d54055 | 97 | * |
okini3939 | 0:9b1d17d54055 | 98 | * @param tptr pointer to the object to call the member function on |
okini3939 | 0:9b1d17d54055 | 99 | * @param mptr pointer to the member function to be called |
okini3939 | 0:9b1d17d54055 | 100 | */ |
okini3939 | 0:9b1d17d54055 | 101 | template<typename T> |
okini3939 | 0:9b1d17d54055 | 102 | void attach(T* tptr, void (T::*mptr)(void)) { |
okini3939 | 0:9b1d17d54055 | 103 | if((mptr != NULL) && (tptr != NULL)) { |
okini3939 | 0:9b1d17d54055 | 104 | rx.attach(tptr, mptr); |
okini3939 | 0:9b1d17d54055 | 105 | } |
okini3939 | 0:9b1d17d54055 | 106 | } |
okini3939 | 0:9b1d17d54055 | 107 | |
okini3939 | 0:9b1d17d54055 | 108 | /** |
okini3939 | 0:9b1d17d54055 | 109 | * Attach a callback called when a packet is received |
okini3939 | 0:9b1d17d54055 | 110 | * |
okini3939 | 0:9b1d17d54055 | 111 | * @param fptr function pointer |
okini3939 | 0:9b1d17d54055 | 112 | */ |
okini3939 | 0:9b1d17d54055 | 113 | void attach(void (*fn)(void)) { |
okini3939 | 0:9b1d17d54055 | 114 | if(fn != NULL) { |
okini3939 | 0:9b1d17d54055 | 115 | rx.attach(fn); |
okini3939 | 0:9b1d17d54055 | 116 | } |
okini3939 | 0:9b1d17d54055 | 117 | } |
okini3939 | 0:9b1d17d54055 | 118 | |
okini3939 | 0:9b1d17d54055 | 119 | |
okini3939 | 0:9b1d17d54055 | 120 | protected: |
okini3939 | 0:9b1d17d54055 | 121 | virtual bool EP2_OUT_callback(); |
okini3939 | 0:9b1d17d54055 | 122 | |
okini3939 | 0:9b1d17d54055 | 123 | private: |
okini3939 | 0:9b1d17d54055 | 124 | FunctionPointer rx; |
okini3939 | 0:9b1d17d54055 | 125 | CircBuffer<uint8_t> buf; |
okini3939 | 0:9b1d17d54055 | 126 | }; |
okini3939 | 0:9b1d17d54055 | 127 | |
okini3939 | 0:9b1d17d54055 | 128 | #endif |