USB composite device example program, drag-and-drop flash writer.

Dependencies:   SWD USBDevice mbed BaseDAP

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?

UserRevisionLine numberNew 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 #include "USBMSD2.h"
va009039 1:ea8e179320d7 21
va009039 1:ea8e179320d7 22 // MSC class specific requests
va009039 1:ea8e179320d7 23 #define MSC_REQUEST_RESET 0xFF
va009039 1:ea8e179320d7 24 #define MSC_REQUEST_GET_MAX_LUN 0xFE
va009039 1:ea8e179320d7 25
va009039 1:ea8e179320d7 26 /**
va009039 1:ea8e179320d7 27 * USBMSD2 class: generic class in order to use all kinds of blocks storage chip
va009039 1:ea8e179320d7 28 *
va009039 1:ea8e179320d7 29 * Introduction
va009039 1:ea8e179320d7 30 *
va009039 1:ea8e179320d7 31 * The USBMSD implements the MSD protocol. It permits to access a memory chip (flash, sdcard,...)
va009039 1:ea8e179320d7 32 * from a computer over USB. But this class doesn't work standalone, you need to subclass this class
va009039 1:ea8e179320d7 33 * and define virtual functions which are called in USBMSD.
va009039 1:ea8e179320d7 34 *
va009039 1:ea8e179320d7 35 * How to use this class with your chip ?
va009039 1:ea8e179320d7 36 *
va009039 1:ea8e179320d7 37 * You have to inherit and define some pure virtual functions (mandatory step):
va009039 1:ea8e179320d7 38 * - virtual int disk_read(char * data, int block): function to read a block
va009039 1:ea8e179320d7 39 * - virtual int disk_write(const char * data, int block): function to write a block
va009039 1:ea8e179320d7 40 * - virtual int disk_initialize(): function to initialize the memory
va009039 1:ea8e179320d7 41 * - virtual int disk_sectors(): return the number of blocks
va009039 1:ea8e179320d7 42 * - virtual int disk_size(): return the memory size
va009039 1:ea8e179320d7 43 * - 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 44 *
va009039 1:ea8e179320d7 45 * All functions names are compatible with the fat filesystem library. So you can imagine using your own class with
va009039 1:ea8e179320d7 46 * USBMSD and the fat filesystem library in the same program. Just be careful because there are two different parts which
va009039 1:ea8e179320d7 47 * will access the sd card. You can do a master/slave system using the disk_status method.
va009039 1:ea8e179320d7 48 *
va009039 1:ea8e179320d7 49 * Once these functions defined, you can call connect() (at the end of the constructor of your class for instance)
va009039 1:ea8e179320d7 50 * of USBMSD to connect your mass storage device. connect() will first call disk_status() to test the status of the disk.
va009039 1:ea8e179320d7 51 * If disk_status() returns 1 (disk not initialized), then disk_initialize() is called. After this step, connect() will collect information
va009039 1:ea8e179320d7 52 * such as the number of blocks and the memory size.
va009039 1:ea8e179320d7 53 */
va009039 1:ea8e179320d7 54 class USB_MSD {
va009039 1:ea8e179320d7 55 public:
va009039 1:ea8e179320d7 56 USB_MSD(USBMSD2* device, USBMSD2* disk);
va009039 1:ea8e179320d7 57
va009039 1:ea8e179320d7 58 /**
va009039 1:ea8e179320d7 59 * Connect the USB MSD device. Establish disk initialization before really connect the device.
va009039 1:ea8e179320d7 60 *
va009039 1:ea8e179320d7 61 * @returns true if successful
va009039 1:ea8e179320d7 62 */
va009039 1:ea8e179320d7 63 bool connect();
va009039 1:ea8e179320d7 64
va009039 1:ea8e179320d7 65 /**
va009039 1:ea8e179320d7 66 * Disconnect the USB MSD device.
va009039 1:ea8e179320d7 67 */
va009039 1:ea8e179320d7 68 void disconnect();
va009039 1:ea8e179320d7 69
va009039 1:ea8e179320d7 70 /**
va009039 1:ea8e179320d7 71 * Destructor
va009039 1:ea8e179320d7 72 */
va009039 1:ea8e179320d7 73 ~USB_MSD();
va009039 1:ea8e179320d7 74
va009039 1:ea8e179320d7 75 bool Request_callback(CONTROL_TRANSFER* transfer);
va009039 1:ea8e179320d7 76 bool EPBULK_OUT_callback();
va009039 1:ea8e179320d7 77 bool EPBULK_IN_callback();
va009039 1:ea8e179320d7 78 private:
va009039 1:ea8e179320d7 79 USBMSD2* _device;
va009039 1:ea8e179320d7 80 DiskInterface* _disk;
va009039 1:ea8e179320d7 81
va009039 1:ea8e179320d7 82 // MSC Bulk-only Stage
va009039 1:ea8e179320d7 83 enum Stage {
va009039 1:ea8e179320d7 84 READ_CBW, // wait a CBW
va009039 1:ea8e179320d7 85 ERROR, // error
va009039 1:ea8e179320d7 86 PROCESS_CBW, // process a CBW request
va009039 1:ea8e179320d7 87 SEND_CSW, // send a CSW
va009039 1:ea8e179320d7 88 WAIT_CSW, // wait that a CSW has been effectively sent
va009039 1:ea8e179320d7 89 };
va009039 1:ea8e179320d7 90
va009039 1:ea8e179320d7 91 // Bulk-only CBW
va009039 1:ea8e179320d7 92 typedef struct {
va009039 1:ea8e179320d7 93 uint32_t Signature;
va009039 1:ea8e179320d7 94 uint32_t Tag;
va009039 1:ea8e179320d7 95 uint32_t DataLength;
va009039 1:ea8e179320d7 96 uint8_t Flags;
va009039 1:ea8e179320d7 97 uint8_t LUN;
va009039 1:ea8e179320d7 98 uint8_t CBLength;
va009039 1:ea8e179320d7 99 uint8_t CB[16];
va009039 1:ea8e179320d7 100 } PACKED CBW;
va009039 1:ea8e179320d7 101
va009039 1:ea8e179320d7 102 // Bulk-only CSW
va009039 1:ea8e179320d7 103 typedef struct {
va009039 1:ea8e179320d7 104 uint32_t Signature;
va009039 1:ea8e179320d7 105 uint32_t Tag;
va009039 1:ea8e179320d7 106 uint32_t DataResidue;
va009039 1:ea8e179320d7 107 uint8_t Status;
va009039 1:ea8e179320d7 108 } PACKED CSW;
va009039 1:ea8e179320d7 109
va009039 1:ea8e179320d7 110 //state of the bulk-only state machine
va009039 1:ea8e179320d7 111 Stage stage;
va009039 1:ea8e179320d7 112
va009039 1:ea8e179320d7 113 // current CBW
va009039 1:ea8e179320d7 114 CBW cbw;
va009039 1:ea8e179320d7 115
va009039 1:ea8e179320d7 116 // CSW which will be sent
va009039 1:ea8e179320d7 117 CSW csw;
va009039 1:ea8e179320d7 118
va009039 1:ea8e179320d7 119 // addr where will be read or written data
va009039 1:ea8e179320d7 120 uint32_t addr;
va009039 1:ea8e179320d7 121
va009039 1:ea8e179320d7 122 // length of a reading or writing
va009039 1:ea8e179320d7 123 uint32_t length;
va009039 1:ea8e179320d7 124
va009039 1:ea8e179320d7 125 // memory OK (after a memoryVerify)
va009039 1:ea8e179320d7 126 bool memOK;
va009039 1:ea8e179320d7 127
va009039 1:ea8e179320d7 128 // cache in RAM before writing in memory. Useful also to read a block.
va009039 1:ea8e179320d7 129 uint8_t * page;
va009039 1:ea8e179320d7 130
va009039 1:ea8e179320d7 131 int BlockSize;
va009039 1:ea8e179320d7 132 uint64_t MemorySize;
va009039 1:ea8e179320d7 133 uint64_t BlockCount;
va009039 1:ea8e179320d7 134
va009039 1:ea8e179320d7 135 void reset();
va009039 1:ea8e179320d7 136 void CBWDecode(uint8_t * buf, uint16_t size);
va009039 1:ea8e179320d7 137 void sendCSW (void);
va009039 1:ea8e179320d7 138 bool inquiryRequest (void);
va009039 1:ea8e179320d7 139 bool write (uint8_t * buf, uint16_t size);
va009039 1:ea8e179320d7 140 bool readFormatCapacity();
va009039 1:ea8e179320d7 141 bool readCapacity (void);
va009039 1:ea8e179320d7 142 bool infoTransfer (void);
va009039 1:ea8e179320d7 143 void memoryRead (void);
va009039 1:ea8e179320d7 144 bool modeSense6 (void);
va009039 1:ea8e179320d7 145 void testUnitReady (void);
va009039 1:ea8e179320d7 146 bool requestSense (void);
va009039 1:ea8e179320d7 147 void memoryVerify (uint8_t * buf, uint16_t size);
va009039 1:ea8e179320d7 148 void memoryWrite (uint8_t * buf, uint16_t size);
va009039 1:ea8e179320d7 149 void fail();
va009039 1:ea8e179320d7 150 };