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