1 /* Copyright (c) 2010-2011 mbed.org, MIT License
3 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software
4 * and associated documentation files (the "Software"), to deal in the Software without
5 * restriction, including without limitation the rights to use, copy, modify, merge, publish,
6 * distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the
7 * Software is furnished to do so, subject to the following conditions:
9 * The above copyright notice and this permission notice shall be included in all copies or
10 * substantial portions of the Software.
12 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
13 * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
14 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
15 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
16 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23 /* These headers are included for child class. */
24 #include "USBEndpoints.h"
25 #include "USBDescriptor.h"
26 #include "USBDevice_Types.h"
28 #include "USBDevice.h"
31 * USBMSD class: generic class in order to use all kinds of blocks storage chip
35 * The USBMSD implements the MSD protocol. It permits to access a memory chip (flash, sdcard,...)
36 * from a computer over USB. But this class doesn't work standalone, you need to subclass this class
37 * and define virtual functions which are called in USBMSD.
39 * How to use this class with your chip ?
41 * You have to inherit and define some pure virtual functions (mandatory step):
42 * - virtual int disk_read(char * data, int block): function to read a block
43 * - virtual int disk_write(const char * data, int block): function to write a block
44 * - virtual int disk_initialize(): function to initialize the memory
45 * - virtual int disk_sectors(): return the number of blocks
46 * - virtual int disk_size(): return the memory size
47 * - 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)
49 * All functions names are compatible with the fat filesystem library. So you can imagine using your own class with
50 * USBMSD and the fat filesystem library in the same program. Just be careful because there are two different parts which
51 * will access the sd card. You can do a master/slave system using the disk_status method.
53 * Once these functions defined, you can call connect() (at the end of the constructor of your class for instance)
54 * of USBMSD to connect your mass storage device. connect() will first call disk_status() to test the status of the disk.
55 * If disk_status() returns 1 (disk not initialized), then disk_initialize() is called. After this step, connect() will collect information
56 * such as the number of blocks and the memory size.
58 class USBMSD: public USBDevice {
64 * @param vendor_id Your vendor_id
65 * @param product_id Your product_id
66 * @param product_release Your preoduct_release
68 USBMSD(uint16_t vendor_id = 0x0703, uint16_t product_id = 0x0104, uint16_t product_release = 0x0001);
71 * Connect the USB MSD device. Establish disk initialization before really connect the device.
73 * @param blocking if not configured
74 * @returns true if successful
76 bool connect(bool blocking = true);
79 * Disconnect the USB MSD device.
91 * read one or more blocks on a storage chip
93 * @param data pointer where will be stored read data
94 * @param block starting block number
95 * @param count number of blocks to read
96 * @returns 0 if successful
98 virtual int disk_read(uint8_t* data, uint64_t block, uint8_t count) = 0;
101 * write one or more blocks on a storage chip
103 * @param data data to write
104 * @param block starting block number
105 * @param count number of blocks to write
106 * @returns 0 if successful
108 virtual int disk_write(const uint8_t* data, uint64_t block, uint8_t count) = 0;
113 virtual int disk_initialize() = 0;
116 * Return the number of blocks
118 * @returns number of blocks
120 virtual uint64_t disk_sectors() = 0;
125 * @returns memory size
127 virtual uint64_t disk_size() = 0;
131 * To check the status of the storage chip
133 * @returns status: 0: OK, 1: disk not initialized, 2: no medium in the drive, 4: write protected
135 virtual int disk_status() = 0;
138 * Get string product descriptor
140 * @returns pointer to the string product descriptor
142 virtual uint8_t * stringIproductDesc();
145 * Get string interface descriptor
147 * @returns pointer to the string interface descriptor
149 virtual uint8_t * stringIinterfaceDesc();
152 * Get configuration descriptor
154 * @returns pointer to the configuration descriptor
156 virtual uint8_t * configurationDesc();
159 * Callback called when a packet is received
161 virtual bool EPBULK_OUT_callback();
164 * Callback called when a packet has been sent
166 virtual bool EPBULK_IN_callback();
169 * Set configuration of device. Add endpoints
171 virtual bool USBCallback_setConfiguration(uint8_t configuration);
174 * Callback called to process class specific requests
176 virtual bool USBCallback_request();
181 // MSC Bulk-only Stage
183 READ_CBW, // wait a CBW
185 PROCESS_CBW, // process a CBW request
186 SEND_CSW, // send a CSW
187 WAIT_CSW, // wait that a CSW has been effectively sent
205 uint32_t DataResidue;
209 //state of the bulk-only state machine
215 // CSW which will be sent
218 // addr where will be read or written data
221 // length of a reading or writing
224 // memory OK (after a memoryVerify)
227 // cache in RAM before writing in memory. Useful also to read a block.
234 void CBWDecode(uint8_t * buf, uint16_t size);
236 bool inquiryRequest (void);
237 bool write (uint8_t * buf, uint16_t size);
238 bool readFormatCapacity();
239 bool readCapacity (void);
240 bool infoTransfer (void);
241 void memoryRead (void);
242 bool modeSense6 (void);
243 void testUnitReady (void);
244 bool requestSense (void);
245 void memoryVerify (uint8_t * buf, uint16_t size);
246 void memoryWrite (uint8_t * buf, uint16_t size);