![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
USB composite device example program, drag-and-drop flash writer.
Dependencies: SWD USBDevice mbed BaseDAP
tests/USBMSD2/USBMSD2.h@1:ea8e179320d7, 2013-09-28 (annotated)
- Committer:
- va009039
- Date:
- Sat Sep 28 03:21:14 2013 +0000
- Revision:
- 1:ea8e179320d7
add USBMSD_Drop class. add CDC(Virtual COM) and HID(for example CMSIS-DAP), but KL25Z not work.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
va009039 | 1:ea8e179320d7 | 1 | /* Copyright (c) 2010-2011 mbed.org, MIT License |
va009039 | 1:ea8e179320d7 | 2 | * |
va009039 | 1:ea8e179320d7 | 3 | * Permission is hereby granted, free of charge, to any person obtaining a copy of this software |
va009039 | 1:ea8e179320d7 | 4 | * and associated documentation files (the "Software"), to deal in the Software without |
va009039 | 1:ea8e179320d7 | 5 | * restriction, including without limitation the rights to use, copy, modify, merge, publish, |
va009039 | 1:ea8e179320d7 | 6 | * distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the |
va009039 | 1:ea8e179320d7 | 7 | * Software is furnished to do so, subject to the following conditions: |
va009039 | 1:ea8e179320d7 | 8 | * |
va009039 | 1:ea8e179320d7 | 9 | * The above copyright notice and this permission notice shall be included in all copies or |
va009039 | 1:ea8e179320d7 | 10 | * substantial portions of the Software. |
va009039 | 1:ea8e179320d7 | 11 | * |
va009039 | 1:ea8e179320d7 | 12 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING |
va009039 | 1:ea8e179320d7 | 13 | * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
va009039 | 1:ea8e179320d7 | 14 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, |
va009039 | 1:ea8e179320d7 | 15 | * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
va009039 | 1:ea8e179320d7 | 16 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
va009039 | 1:ea8e179320d7 | 17 | */ |
va009039 | 1:ea8e179320d7 | 18 | |
va009039 | 1:ea8e179320d7 | 19 | #pragma once |
va009039 | 1:ea8e179320d7 | 20 | |
va009039 | 1:ea8e179320d7 | 21 | /* These headers are included for child class. */ |
va009039 | 1:ea8e179320d7 | 22 | #include "USBEndpoints.h" |
va009039 | 1:ea8e179320d7 | 23 | #include "USBDescriptor.h" |
va009039 | 1:ea8e179320d7 | 24 | #include "USBDevice_Types.h" |
va009039 | 1:ea8e179320d7 | 25 | #include "USBHID_Types.h" |
va009039 | 1:ea8e179320d7 | 26 | #include "USBDevice.h" |
va009039 | 1:ea8e179320d7 | 27 | #include "DiskInterface.h" |
va009039 | 1:ea8e179320d7 | 28 | |
va009039 | 1:ea8e179320d7 | 29 | /** |
va009039 | 1:ea8e179320d7 | 30 | * USBMSD2 class: generic class in order to use all kinds of blocks storage chip |
va009039 | 1:ea8e179320d7 | 31 | * |
va009039 | 1:ea8e179320d7 | 32 | * Introduction |
va009039 | 1:ea8e179320d7 | 33 | * |
va009039 | 1:ea8e179320d7 | 34 | * The USBMSD implements the MSD protocol. It permits to access a memory chip (flash, sdcard,...) |
va009039 | 1:ea8e179320d7 | 35 | * from a computer over USB. But this class doesn't work standalone, you need to subclass this class |
va009039 | 1:ea8e179320d7 | 36 | * and define virtual functions which are called in USBMSD. |
va009039 | 1:ea8e179320d7 | 37 | * |
va009039 | 1:ea8e179320d7 | 38 | * How to use this class with your chip ? |
va009039 | 1:ea8e179320d7 | 39 | * |
va009039 | 1:ea8e179320d7 | 40 | * You have to inherit and define some pure virtual functions (mandatory step): |
va009039 | 1:ea8e179320d7 | 41 | * - virtual int disk_read(char * data, int block): function to read a block |
va009039 | 1:ea8e179320d7 | 42 | * - virtual int disk_write(const char * data, int block): function to write a block |
va009039 | 1:ea8e179320d7 | 43 | * - virtual int disk_initialize(): function to initialize the memory |
va009039 | 1:ea8e179320d7 | 44 | * - virtual int disk_sectors(): return the number of blocks |
va009039 | 1:ea8e179320d7 | 45 | * - virtual int disk_size(): return the memory size |
va009039 | 1:ea8e179320d7 | 46 | * - virtual int disk_status(): return the status of the storage chip (0: OK, 1: not initialized, 2: no medium in the drive, 4: write protection) |
va009039 | 1:ea8e179320d7 | 47 | * |
va009039 | 1:ea8e179320d7 | 48 | * All functions names are compatible with the fat filesystem library. So you can imagine using your own class with |
va009039 | 1:ea8e179320d7 | 49 | * USBMSD and the fat filesystem library in the same program. Just be careful because there are two different parts which |
va009039 | 1:ea8e179320d7 | 50 | * will access the sd card. You can do a master/slave system using the disk_status method. |
va009039 | 1:ea8e179320d7 | 51 | * |
va009039 | 1:ea8e179320d7 | 52 | * Once these functions defined, you can call connect() (at the end of the constructor of your class for instance) |
va009039 | 1:ea8e179320d7 | 53 | * of USBMSD to connect your mass storage device. connect() will first call disk_status() to test the status of the disk. |
va009039 | 1:ea8e179320d7 | 54 | * If disk_status() returns 1 (disk not initialized), then disk_initialize() is called. After this step, connect() will collect information |
va009039 | 1:ea8e179320d7 | 55 | * such as the number of blocks and the memory size. |
va009039 | 1:ea8e179320d7 | 56 | */ |
va009039 | 1:ea8e179320d7 | 57 | class USB_MSD; |
va009039 | 1:ea8e179320d7 | 58 | class USB_CDC; |
va009039 | 1:ea8e179320d7 | 59 | class USB_HID; |
va009039 | 1:ea8e179320d7 | 60 | |
va009039 | 1:ea8e179320d7 | 61 | class USBMSD2: public DiskInterface, public USBDevice { |
va009039 | 1:ea8e179320d7 | 62 | public: |
va009039 | 1:ea8e179320d7 | 63 | /** |
va009039 | 1:ea8e179320d7 | 64 | * Constructor |
va009039 | 1:ea8e179320d7 | 65 | * |
va009039 | 1:ea8e179320d7 | 66 | * @param vendor_id Your vendor_id |
va009039 | 1:ea8e179320d7 | 67 | * @param product_id Your product_id |
va009039 | 1:ea8e179320d7 | 68 | * @param product_release Your preoduct_release |
va009039 | 1:ea8e179320d7 | 69 | */ |
va009039 | 1:ea8e179320d7 | 70 | USBMSD2(uint16_t vendor_id = 0x0d28, uint16_t product_id = 0x0204, uint16_t product_release = 0x0001); |
va009039 | 1:ea8e179320d7 | 71 | |
va009039 | 1:ea8e179320d7 | 72 | /** |
va009039 | 1:ea8e179320d7 | 73 | * Connect the USB MSD device. Establish disk initialization before really connect the device. |
va009039 | 1:ea8e179320d7 | 74 | * |
va009039 | 1:ea8e179320d7 | 75 | * @returns true if successful |
va009039 | 1:ea8e179320d7 | 76 | */ |
va009039 | 1:ea8e179320d7 | 77 | bool connect(); |
va009039 | 1:ea8e179320d7 | 78 | |
va009039 | 1:ea8e179320d7 | 79 | /** |
va009039 | 1:ea8e179320d7 | 80 | * Disconnect the USB MSD device. |
va009039 | 1:ea8e179320d7 | 81 | */ |
va009039 | 1:ea8e179320d7 | 82 | void disconnect(); |
va009039 | 1:ea8e179320d7 | 83 | |
va009039 | 1:ea8e179320d7 | 84 | /** |
va009039 | 1:ea8e179320d7 | 85 | * Destructor |
va009039 | 1:ea8e179320d7 | 86 | */ |
va009039 | 1:ea8e179320d7 | 87 | ~USBMSD2(); |
va009039 | 1:ea8e179320d7 | 88 | |
va009039 | 1:ea8e179320d7 | 89 | /** target to virtual COM |
va009039 | 1:ea8e179320d7 | 90 | */ |
va009039 | 1:ea8e179320d7 | 91 | void putc(int c); |
va009039 | 1:ea8e179320d7 | 92 | |
va009039 | 1:ea8e179320d7 | 93 | /** virtial COM to target |
va009039 | 1:ea8e179320d7 | 94 | */ |
va009039 | 1:ea8e179320d7 | 95 | int getc(); |
va009039 | 1:ea8e179320d7 | 96 | int readable(); |
va009039 | 1:ea8e179320d7 | 97 | int writeable(); |
va009039 | 1:ea8e179320d7 | 98 | |
va009039 | 1:ea8e179320d7 | 99 | |
va009039 | 1:ea8e179320d7 | 100 | /** |
va009039 | 1:ea8e179320d7 | 101 | * Read a report: non blocking |
va009039 | 1:ea8e179320d7 | 102 | * |
va009039 | 1:ea8e179320d7 | 103 | * @param report pointer to the report to fill |
va009039 | 1:ea8e179320d7 | 104 | * @returns true if successful |
va009039 | 1:ea8e179320d7 | 105 | */ |
va009039 | 1:ea8e179320d7 | 106 | bool readNB(HID_REPORT* report); |
va009039 | 1:ea8e179320d7 | 107 | |
va009039 | 1:ea8e179320d7 | 108 | /** |
va009039 | 1:ea8e179320d7 | 109 | * Send a Report. warning: blocking |
va009039 | 1:ea8e179320d7 | 110 | * |
va009039 | 1:ea8e179320d7 | 111 | * @param report Report which will be sent (a report is defined by all data and the length) |
va009039 | 1:ea8e179320d7 | 112 | * @returns true if successful |
va009039 | 1:ea8e179320d7 | 113 | */ |
va009039 | 1:ea8e179320d7 | 114 | bool send(HID_REPORT* report); |
va009039 | 1:ea8e179320d7 | 115 | |
va009039 | 1:ea8e179320d7 | 116 | protected: |
va009039 | 1:ea8e179320d7 | 117 | /* |
va009039 | 1:ea8e179320d7 | 118 | * Get device descriptor. Warning: this method has to store the length of the report descriptor in reportLength. |
va009039 | 1:ea8e179320d7 | 119 | * |
va009039 | 1:ea8e179320d7 | 120 | * @returns pointer to the device descriptor |
va009039 | 1:ea8e179320d7 | 121 | */ |
va009039 | 1:ea8e179320d7 | 122 | virtual uint8_t * deviceDesc(); |
va009039 | 1:ea8e179320d7 | 123 | |
va009039 | 1:ea8e179320d7 | 124 | /* |
va009039 | 1:ea8e179320d7 | 125 | * Get string product descriptor |
va009039 | 1:ea8e179320d7 | 126 | * |
va009039 | 1:ea8e179320d7 | 127 | * @returns pointer to the string product descriptor |
va009039 | 1:ea8e179320d7 | 128 | */ |
va009039 | 1:ea8e179320d7 | 129 | virtual uint8_t * stringIproductDesc(); |
va009039 | 1:ea8e179320d7 | 130 | |
va009039 | 1:ea8e179320d7 | 131 | /* |
va009039 | 1:ea8e179320d7 | 132 | * Get string interface descriptor |
va009039 | 1:ea8e179320d7 | 133 | * |
va009039 | 1:ea8e179320d7 | 134 | * @returns pointer to the string interface descriptor |
va009039 | 1:ea8e179320d7 | 135 | */ |
va009039 | 1:ea8e179320d7 | 136 | virtual uint8_t * stringIinterfaceDesc(); |
va009039 | 1:ea8e179320d7 | 137 | |
va009039 | 1:ea8e179320d7 | 138 | /* |
va009039 | 1:ea8e179320d7 | 139 | * Get configuration descriptor |
va009039 | 1:ea8e179320d7 | 140 | * |
va009039 | 1:ea8e179320d7 | 141 | * @returns pointer to the configuration descriptor |
va009039 | 1:ea8e179320d7 | 142 | */ |
va009039 | 1:ea8e179320d7 | 143 | virtual uint8_t * configurationDesc(); |
va009039 | 1:ea8e179320d7 | 144 | |
va009039 | 1:ea8e179320d7 | 145 | virtual bool EP2_OUT_callback(); // MSC Callback called when a packet is received |
va009039 | 1:ea8e179320d7 | 146 | virtual bool EP2_IN_callback(); // MSC Callback called when a packet has been sent |
va009039 | 1:ea8e179320d7 | 147 | virtual bool EP5_OUT_callback(); // CDC Callback called when a packet is received |
va009039 | 1:ea8e179320d7 | 148 | |
va009039 | 1:ea8e179320d7 | 149 | /* |
va009039 | 1:ea8e179320d7 | 150 | * Set configuration of device. Add endpoints |
va009039 | 1:ea8e179320d7 | 151 | */ |
va009039 | 1:ea8e179320d7 | 152 | virtual bool USBCallback_setConfiguration(uint8_t configuration); |
va009039 | 1:ea8e179320d7 | 153 | |
va009039 | 1:ea8e179320d7 | 154 | /* |
va009039 | 1:ea8e179320d7 | 155 | * Callback called to process class specific requests |
va009039 | 1:ea8e179320d7 | 156 | */ |
va009039 | 1:ea8e179320d7 | 157 | virtual bool USBCallback_request(); |
va009039 | 1:ea8e179320d7 | 158 | |
va009039 | 1:ea8e179320d7 | 159 | /* |
va009039 | 1:ea8e179320d7 | 160 | * Called by USBDevice on Endpoint0 request completion |
va009039 | 1:ea8e179320d7 | 161 | * if the 'notify' flag has been set to true. Warning: Called in ISR context |
va009039 | 1:ea8e179320d7 | 162 | * |
va009039 | 1:ea8e179320d7 | 163 | * In this case it is used to indicate that a HID report has |
va009039 | 1:ea8e179320d7 | 164 | * been received from the host on endpoint 0 |
va009039 | 1:ea8e179320d7 | 165 | * |
va009039 | 1:ea8e179320d7 | 166 | * @param buf buffer received on endpoint 0 |
va009039 | 1:ea8e179320d7 | 167 | * @param length length of this buffer |
va009039 | 1:ea8e179320d7 | 168 | */ |
va009039 | 1:ea8e179320d7 | 169 | virtual void USBCallback_requestCompleted(uint8_t * buf, uint32_t length); |
va009039 | 1:ea8e179320d7 | 170 | |
va009039 | 1:ea8e179320d7 | 171 | private: |
va009039 | 1:ea8e179320d7 | 172 | USB_MSD* _msd; |
va009039 | 1:ea8e179320d7 | 173 | USB_CDC* _cdc; |
va009039 | 1:ea8e179320d7 | 174 | USB_HID* _hid; |
va009039 | 1:ea8e179320d7 | 175 | }; |