USBMSD SD card Hello World for Mbed platforms
Dependencies: mbed USBMSD_SD USBDevice
USBDevice/USBMSD/USBMSD.h@2:27a7e7f8d399, 2011-11-11 (annotated)
- Committer:
- samux
- Date:
- Fri Nov 11 15:22:53 2011 +0000
- Revision:
- 2:27a7e7f8d399
- Child:
- 3:0ffb2eee9e06
we have 2MB with the sdcard!!
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
samux |
2:27a7e7f8d399 | 1 | /* USBMSD.h */ |
samux |
2:27a7e7f8d399 | 2 | /* USB mass storage device example */ |
samux |
2:27a7e7f8d399 | 3 | /* Copyright (c) 2011 ARM Limited. All rights reserved. */ |
samux |
2:27a7e7f8d399 | 4 | |
samux |
2:27a7e7f8d399 | 5 | /* |
samux |
2:27a7e7f8d399 | 6 | * Guide to adapt this class to your storage chip: |
samux |
2:27a7e7f8d399 | 7 | * |
samux |
2:27a7e7f8d399 | 8 | * - adapt the BlockSize symbol in USBMSD.h |
samux |
2:27a7e7f8d399 | 9 | * - adapt the MemorySize symbol in USBMSD.cpp |
samux |
2:27a7e7f8d399 | 10 | * - declare your own object to store data: here AT45 mem |
samux |
2:27a7e7f8d399 | 11 | * - Be sure to provide : |
samux |
2:27a7e7f8d399 | 12 | * - mem.blockread(page, block_number); |
samux |
2:27a7e7f8d399 | 13 | * - mem.blockwrite(page, block_number); |
samux |
2:27a7e7f8d399 | 14 | * These functions are used by USBMSD class to read or write data |
samux |
2:27a7e7f8d399 | 15 | */ |
samux |
2:27a7e7f8d399 | 16 | |
samux |
2:27a7e7f8d399 | 17 | #ifndef USBMSD_H |
samux |
2:27a7e7f8d399 | 18 | #define USBMSD_H |
samux |
2:27a7e7f8d399 | 19 | |
samux |
2:27a7e7f8d399 | 20 | /* These headers are included for child class. */ |
samux |
2:27a7e7f8d399 | 21 | #include "USBEndpoints.h" |
samux |
2:27a7e7f8d399 | 22 | #include "USBDescriptor.h" |
samux |
2:27a7e7f8d399 | 23 | #include "USBDevice_Types.h" |
samux |
2:27a7e7f8d399 | 24 | |
samux |
2:27a7e7f8d399 | 25 | #include "USBDevice.h" |
samux |
2:27a7e7f8d399 | 26 | #include "SDcard.h" |
samux |
2:27a7e7f8d399 | 27 | |
samux |
2:27a7e7f8d399 | 28 | #define DEFAULT_CONFIGURATION (1) |
samux |
2:27a7e7f8d399 | 29 | |
samux |
2:27a7e7f8d399 | 30 | // Block Size |
samux |
2:27a7e7f8d399 | 31 | #define BlockSize 512 |
samux |
2:27a7e7f8d399 | 32 | |
samux |
2:27a7e7f8d399 | 33 | // MSC Bulk-only Stage |
samux |
2:27a7e7f8d399 | 34 | enum Stage { |
samux |
2:27a7e7f8d399 | 35 | STATE_READ_CBW, // wait a CBW |
samux |
2:27a7e7f8d399 | 36 | STATE_PROCESS_CBW, // process a CBW request |
samux |
2:27a7e7f8d399 | 37 | STATE_SEND_CSW, // send a CSW |
samux |
2:27a7e7f8d399 | 38 | STATE_ERROR, // error: out of memory |
samux |
2:27a7e7f8d399 | 39 | STATE_WAIT_CSW, // wait that a CSW has been effectively sent |
samux |
2:27a7e7f8d399 | 40 | }; |
samux |
2:27a7e7f8d399 | 41 | |
samux |
2:27a7e7f8d399 | 42 | // Bulk-only CBW |
samux |
2:27a7e7f8d399 | 43 | typedef __packed struct { |
samux |
2:27a7e7f8d399 | 44 | uint32_t Signature; |
samux |
2:27a7e7f8d399 | 45 | uint32_t Tag; |
samux |
2:27a7e7f8d399 | 46 | uint32_t DataLength; |
samux |
2:27a7e7f8d399 | 47 | uint8_t Flags; |
samux |
2:27a7e7f8d399 | 48 | uint8_t LUN; |
samux |
2:27a7e7f8d399 | 49 | uint8_t CBLength; |
samux |
2:27a7e7f8d399 | 50 | uint8_t CB[16]; |
samux |
2:27a7e7f8d399 | 51 | } CBW; |
samux |
2:27a7e7f8d399 | 52 | |
samux |
2:27a7e7f8d399 | 53 | // Bulk-only CSW |
samux |
2:27a7e7f8d399 | 54 | typedef __packed struct { |
samux |
2:27a7e7f8d399 | 55 | uint32_t Signature; |
samux |
2:27a7e7f8d399 | 56 | uint32_t Tag; |
samux |
2:27a7e7f8d399 | 57 | uint32_t DataResidue; |
samux |
2:27a7e7f8d399 | 58 | uint8_t Status; |
samux |
2:27a7e7f8d399 | 59 | } CSW; |
samux |
2:27a7e7f8d399 | 60 | |
samux |
2:27a7e7f8d399 | 61 | /** |
samux |
2:27a7e7f8d399 | 62 | * USBMSD example |
samux |
2:27a7e7f8d399 | 63 | * |
samux |
2:27a7e7f8d399 | 64 | * @code |
samux |
2:27a7e7f8d399 | 65 | * #include "mbed.h" |
samux |
2:27a7e7f8d399 | 66 | * #include "USBMSD.h" |
samux |
2:27a7e7f8d399 | 67 | * |
samux |
2:27a7e7f8d399 | 68 | * USBMSD msd; |
samux |
2:27a7e7f8d399 | 69 | * |
samux |
2:27a7e7f8d399 | 70 | * int main() { |
samux |
2:27a7e7f8d399 | 71 | * while(1); |
samux |
2:27a7e7f8d399 | 72 | * } |
samux |
2:27a7e7f8d399 | 73 | * |
samux |
2:27a7e7f8d399 | 74 | * @endcode |
samux |
2:27a7e7f8d399 | 75 | */ |
samux |
2:27a7e7f8d399 | 76 | class USBMSD: public USBDevice { |
samux |
2:27a7e7f8d399 | 77 | public: |
samux |
2:27a7e7f8d399 | 78 | |
samux |
2:27a7e7f8d399 | 79 | /** |
samux |
2:27a7e7f8d399 | 80 | * Constructor |
samux |
2:27a7e7f8d399 | 81 | * |
samux |
2:27a7e7f8d399 | 82 | * @param vendor_id Your vendor_id |
samux |
2:27a7e7f8d399 | 83 | * @param product_id Your product_id |
samux |
2:27a7e7f8d399 | 84 | * @param product_release Your preoduct_release |
samux |
2:27a7e7f8d399 | 85 | */ |
samux |
2:27a7e7f8d399 | 86 | USBMSD(uint16_t vendor_id = 0x0703, uint16_t product_id = 0x0104, uint16_t product_release = 0x0001); |
samux |
2:27a7e7f8d399 | 87 | |
samux |
2:27a7e7f8d399 | 88 | |
samux |
2:27a7e7f8d399 | 89 | |
samux |
2:27a7e7f8d399 | 90 | protected: |
samux |
2:27a7e7f8d399 | 91 | |
samux |
2:27a7e7f8d399 | 92 | /* |
samux |
2:27a7e7f8d399 | 93 | * Get number of logical unit - 1 (here 0) |
samux |
2:27a7e7f8d399 | 94 | * |
samux |
2:27a7e7f8d399 | 95 | * @returns Pointer containing the number of logical unit - 1 |
samux |
2:27a7e7f8d399 | 96 | */ |
samux |
2:27a7e7f8d399 | 97 | uint8_t * getMaxLUN(); |
samux |
2:27a7e7f8d399 | 98 | |
samux |
2:27a7e7f8d399 | 99 | /* |
samux |
2:27a7e7f8d399 | 100 | * Get string product descriptor |
samux |
2:27a7e7f8d399 | 101 | * |
samux |
2:27a7e7f8d399 | 102 | * @returns pointer to the string product descriptor |
samux |
2:27a7e7f8d399 | 103 | */ |
samux |
2:27a7e7f8d399 | 104 | virtual uint8_t * stringIproductDesc(); |
samux |
2:27a7e7f8d399 | 105 | |
samux |
2:27a7e7f8d399 | 106 | /* |
samux |
2:27a7e7f8d399 | 107 | * Get string interface descriptor |
samux |
2:27a7e7f8d399 | 108 | * |
samux |
2:27a7e7f8d399 | 109 | * @returns pointer to the string interface descriptor |
samux |
2:27a7e7f8d399 | 110 | */ |
samux |
2:27a7e7f8d399 | 111 | virtual uint8_t * stringIinterfaceDesc(); |
samux |
2:27a7e7f8d399 | 112 | |
samux |
2:27a7e7f8d399 | 113 | /* |
samux |
2:27a7e7f8d399 | 114 | * Get configuration descriptor |
samux |
2:27a7e7f8d399 | 115 | * |
samux |
2:27a7e7f8d399 | 116 | * @returns pointer to the configuration descriptor |
samux |
2:27a7e7f8d399 | 117 | */ |
samux |
2:27a7e7f8d399 | 118 | virtual uint8_t * configurationDesc(); |
samux |
2:27a7e7f8d399 | 119 | |
samux |
2:27a7e7f8d399 | 120 | /* |
samux |
2:27a7e7f8d399 | 121 | * Callback called when a packet is received |
samux |
2:27a7e7f8d399 | 122 | */ |
samux |
2:27a7e7f8d399 | 123 | virtual bool EP2_OUT_callback(); |
samux |
2:27a7e7f8d399 | 124 | |
samux |
2:27a7e7f8d399 | 125 | /* |
samux |
2:27a7e7f8d399 | 126 | * Callback called when a packet has been sent |
samux |
2:27a7e7f8d399 | 127 | */ |
samux |
2:27a7e7f8d399 | 128 | virtual bool EP2_IN_callback(); |
samux |
2:27a7e7f8d399 | 129 | |
samux |
2:27a7e7f8d399 | 130 | /* |
samux |
2:27a7e7f8d399 | 131 | * Set configuration of device. Add endpoints |
samux |
2:27a7e7f8d399 | 132 | */ |
samux |
2:27a7e7f8d399 | 133 | virtual bool USBCallback_setConfiguration(uint8_t configuration); |
samux |
2:27a7e7f8d399 | 134 | |
samux |
2:27a7e7f8d399 | 135 | /* |
samux |
2:27a7e7f8d399 | 136 | * Callback called to process class specific requests |
samux |
2:27a7e7f8d399 | 137 | */ |
samux |
2:27a7e7f8d399 | 138 | virtual bool USBCallback_request(); |
samux |
2:27a7e7f8d399 | 139 | |
samux |
2:27a7e7f8d399 | 140 | |
samux |
2:27a7e7f8d399 | 141 | private: |
samux |
2:27a7e7f8d399 | 142 | //state of the bulk-only state machine |
samux |
2:27a7e7f8d399 | 143 | Stage stage; |
samux |
2:27a7e7f8d399 | 144 | |
samux |
2:27a7e7f8d399 | 145 | // current CBW |
samux |
2:27a7e7f8d399 | 146 | CBW cbw; |
samux |
2:27a7e7f8d399 | 147 | |
samux |
2:27a7e7f8d399 | 148 | // CSW which will be sent |
samux |
2:27a7e7f8d399 | 149 | CSW csw; |
samux |
2:27a7e7f8d399 | 150 | |
samux |
2:27a7e7f8d399 | 151 | // addr where will be read or written data |
samux |
2:27a7e7f8d399 | 152 | uint32_t addr; |
samux |
2:27a7e7f8d399 | 153 | |
samux |
2:27a7e7f8d399 | 154 | // length of a reading or writing |
samux |
2:27a7e7f8d399 | 155 | uint32_t length; |
samux |
2:27a7e7f8d399 | 156 | |
samux |
2:27a7e7f8d399 | 157 | // memory OK (after a memoryVerify) |
samux |
2:27a7e7f8d399 | 158 | bool memOK; |
samux |
2:27a7e7f8d399 | 159 | |
samux |
2:27a7e7f8d399 | 160 | // cache in RAM before writing in memory. Useful also to read a block. |
samux |
2:27a7e7f8d399 | 161 | uint8_t page[BlockSize]; |
samux |
2:27a7e7f8d399 | 162 | |
samux |
2:27a7e7f8d399 | 163 | // memory (Atmel AT45 family) |
samux |
2:27a7e7f8d399 | 164 | // |
samux |
2:27a7e7f8d399 | 165 | // You can change this memory to use another one (SDcard, ...) |
samux |
2:27a7e7f8d399 | 166 | // You need to provide : |
samux |
2:27a7e7f8d399 | 167 | // - mem.blockread(page, block_number); |
samux |
2:27a7e7f8d399 | 168 | // - mem.blockwrite(page, block_number); |
samux |
2:27a7e7f8d399 | 169 | SDcard mem; |
samux |
2:27a7e7f8d399 | 170 | |
samux |
2:27a7e7f8d399 | 171 | void CBWDecode(uint8_t * buf, uint16_t size); |
samux |
2:27a7e7f8d399 | 172 | void sendCSW (void); |
samux |
2:27a7e7f8d399 | 173 | bool inquiryRequest (void); |
samux |
2:27a7e7f8d399 | 174 | bool write (uint8_t * buf, uint16_t size); |
samux |
2:27a7e7f8d399 | 175 | bool readFormatCapacity(); |
samux |
2:27a7e7f8d399 | 176 | bool readCapacity (void); |
samux |
2:27a7e7f8d399 | 177 | bool infoTransfer (void); |
samux |
2:27a7e7f8d399 | 178 | void memoryRead (void); |
samux |
2:27a7e7f8d399 | 179 | bool modeSense6 (void); |
samux |
2:27a7e7f8d399 | 180 | void testUnitReady (void); |
samux |
2:27a7e7f8d399 | 181 | bool requestSense (void); |
samux |
2:27a7e7f8d399 | 182 | void memoryVerify (uint8_t * buf, uint16_t size); |
samux |
2:27a7e7f8d399 | 183 | void memoryWrite (uint8_t * buf, uint16_t size); |
samux |
2:27a7e7f8d399 | 184 | void reset(); |
samux |
2:27a7e7f8d399 | 185 | void fail(); |
samux |
2:27a7e7f8d399 | 186 | }; |
samux |
2:27a7e7f8d399 | 187 | |
samux |
2:27a7e7f8d399 | 188 | #endif |
samux |
2:27a7e7f8d399 | 189 |