Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependents: KL46Z-lpc81isp lpcterm2
USB_MSD.h
00001 /* Copyright (c) 2010-2011 mbed.org, MIT License 00002 * 00003 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software 00004 * and associated documentation files (the "Software"), to deal in the Software without 00005 * restriction, including without limitation the rights to use, copy, modify, merge, publish, 00006 * distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the 00007 * Software is furnished to do so, subject to the following conditions: 00008 * 00009 * The above copyright notice and this permission notice shall be included in all copies or 00010 * substantial portions of the Software. 00011 * 00012 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING 00013 * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 00014 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, 00015 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 00016 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 00017 */ 00018 00019 #pragma once 00020 00021 #include "USBDevice.h" 00022 #include "DiskInterface.h" 00023 00024 // MSC class specific requests 00025 #define MSC_REQUEST_RESET 0xFF 00026 #define MSC_REQUEST_GET_MAX_LUN 0xFE 00027 00028 /** 00029 * USBMSD2 class: generic class in order to use all kinds of blocks storage chip 00030 * 00031 * Introduction 00032 * 00033 * The USBMSD implements the MSD protocol. It permits to access a memory chip (flash, sdcard,...) 00034 * from a computer over USB. But this class doesn't work standalone, you need to subclass this class 00035 * and define virtual functions which are called in USBMSD. 00036 * 00037 * How to use this class with your chip ? 00038 * 00039 * You have to inherit and define some pure virtual functions (mandatory step): 00040 * - virtual int disk_read(char * data, int block): function to read a block 00041 * - virtual int disk_write(const char * data, int block): function to write a block 00042 * - virtual int disk_initialize(): function to initialize the memory 00043 * - virtual int disk_sectors(): return the number of blocks 00044 * - virtual int disk_size(): return the memory size 00045 * - 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) 00046 * 00047 * All functions names are compatible with the fat filesystem library. So you can imagine using your own class with 00048 * USBMSD and the fat filesystem library in the same program. Just be careful because there are two different parts which 00049 * will access the sd card. You can do a master/slave system using the disk_status method. 00050 * 00051 * Once these functions defined, you can call connect() (at the end of the constructor of your class for instance) 00052 * of USBMSD to connect your mass storage device. connect() will first call disk_status() to test the status of the disk. 00053 * If disk_status() returns 1 (disk not initialized), then disk_initialize() is called. After this step, connect() will collect information 00054 * such as the number of blocks and the memory size. 00055 */ 00056 class USB_MSD { 00057 public: 00058 USB_MSD(USBDevice* device, DiskInterface* disk); 00059 00060 /** 00061 * Connect the USB MSD device. Establish disk initialization before really connect the device. 00062 * 00063 * @returns true if successful 00064 */ 00065 bool connect(); 00066 00067 /** 00068 * Disconnect the USB MSD device. 00069 */ 00070 void disconnect(); 00071 00072 /** 00073 * Destructor 00074 */ 00075 ~USB_MSD(); 00076 00077 bool Request_callback(CONTROL_TRANSFER* transfer); 00078 bool EPBULK_OUT_callback(); 00079 bool EPBULK_IN_callback(); 00080 00081 private: 00082 USBDevice* _device; 00083 DiskInterface* _disk; 00084 00085 // MSC Bulk-only Stage 00086 enum Stage { 00087 READ_CBW, // wait a CBW 00088 ERROR, // error 00089 PROCESS_CBW, // process a CBW request 00090 SEND_CSW, // send a CSW 00091 WAIT_CSW, // wait that a CSW has been effectively sent 00092 }; 00093 00094 // Bulk-only CBW 00095 typedef struct { 00096 uint32_t Signature; 00097 uint32_t Tag; 00098 uint32_t DataLength; 00099 uint8_t Flags; 00100 uint8_t LUN; 00101 uint8_t CBLength; 00102 uint8_t CB[16]; 00103 } PACKED CBW; 00104 00105 // Bulk-only CSW 00106 typedef struct { 00107 uint32_t Signature; 00108 uint32_t Tag; 00109 uint32_t DataResidue; 00110 uint8_t Status; 00111 } PACKED CSW; 00112 00113 //state of the bulk-only state machine 00114 Stage stage; 00115 00116 // current CBW 00117 CBW cbw; 00118 00119 // CSW which will be sent 00120 CSW csw; 00121 00122 // addr where will be read or written data 00123 uint32_t addr; 00124 00125 // length of a reading or writing 00126 uint32_t length; 00127 00128 // memory OK (after a memoryVerify) 00129 bool memOK; 00130 00131 // cache in RAM before writing in memory. Useful also to read a block. 00132 uint8_t * page; 00133 00134 int BlockSize; 00135 uint64_t MemorySize; 00136 uint64_t BlockCount; 00137 00138 void reset(); 00139 void CBWDecode(uint8_t * buf, uint16_t size); 00140 void sendCSW (void); 00141 bool inquiryRequest (void); 00142 bool write (uint8_t * buf, uint16_t size); 00143 bool readFormatCapacity(); 00144 bool readCapacity (void); 00145 bool infoTransfer (void); 00146 void memoryRead (void); 00147 bool modeSense6 (void); 00148 void testUnitReady (void); 00149 bool requestSense (void); 00150 void memoryVerify (uint8_t * buf, uint16_t size); 00151 void memoryWrite (uint8_t * buf, uint16_t size); 00152 void fail(); 00153 };
Generated on Tue Jul 12 2022 19:39:32 by
1.7.2