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