USB Host Driver with Socket Modem support. Works with revision 323 of mbed-src but broken with any later version.
Dependencies: FATFileSystem
Fork of F401RE-USBHost by
USBHostMSD.h@1:c072d9e580b0, 2014-01-21 (annotated)
- Committer:
- va009039
- Date:
- Tue Jan 21 08:59:28 2014 +0000
- Revision:
- 1:c072d9e580b0
- Child:
- 2:0cdac6bcc534
add USBHostMSD
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
va009039 | 1:c072d9e580b0 | 1 | /* mbed USBHost Library |
va009039 | 1:c072d9e580b0 | 2 | * Copyright (c) 2006-2013 ARM Limited |
va009039 | 1:c072d9e580b0 | 3 | * |
va009039 | 1:c072d9e580b0 | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
va009039 | 1:c072d9e580b0 | 5 | * you may not use this file except in compliance with the License. |
va009039 | 1:c072d9e580b0 | 6 | * You may obtain a copy of the License at |
va009039 | 1:c072d9e580b0 | 7 | * |
va009039 | 1:c072d9e580b0 | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
va009039 | 1:c072d9e580b0 | 9 | * |
va009039 | 1:c072d9e580b0 | 10 | * Unless required by applicable law or agreed to in writing, software |
va009039 | 1:c072d9e580b0 | 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
va009039 | 1:c072d9e580b0 | 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
va009039 | 1:c072d9e580b0 | 13 | * See the License for the specific language governing permissions and |
va009039 | 1:c072d9e580b0 | 14 | * limitations under the License. |
va009039 | 1:c072d9e580b0 | 15 | */ |
va009039 | 1:c072d9e580b0 | 16 | |
va009039 | 1:c072d9e580b0 | 17 | #ifndef USBHOSTMSD_H |
va009039 | 1:c072d9e580b0 | 18 | #define USBHOSTMSD_H |
va009039 | 1:c072d9e580b0 | 19 | |
va009039 | 1:c072d9e580b0 | 20 | #if 0 |
va009039 | 1:c072d9e580b0 | 21 | #define USB_DBG(...) do{fprintf(stderr,"[%s@%d] ",__PRETTY_FUNCTION__,__LINE__);fprintf(stderr,__VA_ARGS__);fprintf(stderr,"\r\n");} while(0); |
va009039 | 1:c072d9e580b0 | 22 | #else |
va009039 | 1:c072d9e580b0 | 23 | #define USB_DBG(...) while(0); |
va009039 | 1:c072d9e580b0 | 24 | #endif |
va009039 | 1:c072d9e580b0 | 25 | |
va009039 | 1:c072d9e580b0 | 26 | #define USB_DBG2(...) do{fprintf(stderr,"[%s@%d] ",__PRETTY_FUNCTION__,__LINE__);fprintf(stderr,__VA_ARGS__);fprintf(stderr,"\r\n");} while(0); |
va009039 | 1:c072d9e580b0 | 27 | |
va009039 | 1:c072d9e580b0 | 28 | #if 1 |
va009039 | 1:c072d9e580b0 | 29 | #define USB_INFO(...) do{fprintf(stderr,__VA_ARGS__);fprintf(stderr,"\r\n");}while(0); |
va009039 | 1:c072d9e580b0 | 30 | #else |
va009039 | 1:c072d9e580b0 | 31 | #define USB_INFO(...) while(0); |
va009039 | 1:c072d9e580b0 | 32 | #endif |
va009039 | 1:c072d9e580b0 | 33 | |
va009039 | 1:c072d9e580b0 | 34 | //#include "USBHostConf.h" |
va009039 | 1:c072d9e580b0 | 35 | enum USB_TYPE { |
va009039 | 1:c072d9e580b0 | 36 | USB_TYPE_OK = 0, |
va009039 | 1:c072d9e580b0 | 37 | // completion code |
va009039 | 1:c072d9e580b0 | 38 | USB_TYPE_STALL_ERROR = 4, |
va009039 | 1:c072d9e580b0 | 39 | // general usb state |
va009039 | 1:c072d9e580b0 | 40 | USB_TYPE_IDLE = 16, |
va009039 | 1:c072d9e580b0 | 41 | USB_TYPE_ERROR = 18, |
va009039 | 1:c072d9e580b0 | 42 | }; |
va009039 | 1:c072d9e580b0 | 43 | |
va009039 | 1:c072d9e580b0 | 44 | // ----------- Control RequestType Fields ----------- |
va009039 | 1:c072d9e580b0 | 45 | #define USB_DEVICE_TO_HOST 0x80 |
va009039 | 1:c072d9e580b0 | 46 | #define USB_HOST_TO_DEVICE 0x00 |
va009039 | 1:c072d9e580b0 | 47 | #define USB_REQUEST_TYPE_CLASS 0x20 |
va009039 | 1:c072d9e580b0 | 48 | #define USB_REQUEST_TYPE_STANDARD 0x00 |
va009039 | 1:c072d9e580b0 | 49 | #define USB_RECIPIENT_DEVICE 0x00 |
va009039 | 1:c072d9e580b0 | 50 | #define USB_RECIPIENT_INTERFACE 0x01 |
va009039 | 1:c072d9e580b0 | 51 | #define USB_RECIPIENT_ENDPOINT 0x02 |
va009039 | 1:c072d9e580b0 | 52 | |
va009039 | 1:c072d9e580b0 | 53 | // -------------- USB Standard Requests -------------- |
va009039 | 1:c072d9e580b0 | 54 | #define CLEAR_FEATURE 0x01 |
va009039 | 1:c072d9e580b0 | 55 | |
va009039 | 1:c072d9e580b0 | 56 | #include "USBHost.h" |
va009039 | 1:c072d9e580b0 | 57 | #include "FATFileSystem.h" |
va009039 | 1:c072d9e580b0 | 58 | |
va009039 | 1:c072d9e580b0 | 59 | class USBDeviceConnected { //dummy |
va009039 | 1:c072d9e580b0 | 60 | }; |
va009039 | 1:c072d9e580b0 | 61 | |
va009039 | 1:c072d9e580b0 | 62 | struct USBEndpoint { // dummy |
va009039 | 1:c072d9e580b0 | 63 | void setState(uint8_t st){}; |
va009039 | 1:c072d9e580b0 | 64 | uint8_t getAddress(){ return 0; }; |
va009039 | 1:c072d9e580b0 | 65 | }; |
va009039 | 1:c072d9e580b0 | 66 | |
va009039 | 1:c072d9e580b0 | 67 | /** |
va009039 | 1:c072d9e580b0 | 68 | * A class to communicate a USB flash disk |
va009039 | 1:c072d9e580b0 | 69 | */ |
va009039 | 1:c072d9e580b0 | 70 | class USBHostMSD : public FATFileSystem { |
va009039 | 1:c072d9e580b0 | 71 | public: |
va009039 | 1:c072d9e580b0 | 72 | /** |
va009039 | 1:c072d9e580b0 | 73 | * Constructor |
va009039 | 1:c072d9e580b0 | 74 | * |
va009039 | 1:c072d9e580b0 | 75 | * @param rootdir mount name |
va009039 | 1:c072d9e580b0 | 76 | */ |
va009039 | 1:c072d9e580b0 | 77 | USBHostMSD(const char * rootdir); |
va009039 | 1:c072d9e580b0 | 78 | |
va009039 | 1:c072d9e580b0 | 79 | /** |
va009039 | 1:c072d9e580b0 | 80 | * Check if a MSD device is connected |
va009039 | 1:c072d9e580b0 | 81 | * |
va009039 | 1:c072d9e580b0 | 82 | * @return true if a MSD device is connected |
va009039 | 1:c072d9e580b0 | 83 | */ |
va009039 | 1:c072d9e580b0 | 84 | bool connected(); |
va009039 | 1:c072d9e580b0 | 85 | |
va009039 | 1:c072d9e580b0 | 86 | /** |
va009039 | 1:c072d9e580b0 | 87 | * Try to connect to a MSD device |
va009039 | 1:c072d9e580b0 | 88 | * |
va009039 | 1:c072d9e580b0 | 89 | * @return true if connection was successful |
va009039 | 1:c072d9e580b0 | 90 | */ |
va009039 | 1:c072d9e580b0 | 91 | bool connect(); |
va009039 | 1:c072d9e580b0 | 92 | |
va009039 | 1:c072d9e580b0 | 93 | protected: |
va009039 | 1:c072d9e580b0 | 94 | |
va009039 | 1:c072d9e580b0 | 95 | // From FATFileSystem |
va009039 | 1:c072d9e580b0 | 96 | virtual int disk_initialize(); |
va009039 | 1:c072d9e580b0 | 97 | virtual int disk_status() {return 0;}; |
va009039 | 1:c072d9e580b0 | 98 | virtual int disk_read(uint8_t * buffer, uint64_t sector); |
va009039 | 1:c072d9e580b0 | 99 | virtual int disk_write(const uint8_t * buffer, uint64_t sector); |
va009039 | 1:c072d9e580b0 | 100 | virtual int disk_sync() {return 0;}; |
va009039 | 1:c072d9e580b0 | 101 | virtual uint64_t disk_sectors(); |
va009039 | 1:c072d9e580b0 | 102 | |
va009039 | 1:c072d9e580b0 | 103 | private: |
va009039 | 1:c072d9e580b0 | 104 | USBHost * host; |
va009039 | 1:c072d9e580b0 | 105 | USBDeviceConnected * dev; |
va009039 | 1:c072d9e580b0 | 106 | bool dev_connected; |
va009039 | 1:c072d9e580b0 | 107 | USBEndpoint * bulk_in; |
va009039 | 1:c072d9e580b0 | 108 | USBEndpoint * bulk_out; |
va009039 | 1:c072d9e580b0 | 109 | uint8_t nb_ep; |
va009039 | 1:c072d9e580b0 | 110 | |
va009039 | 1:c072d9e580b0 | 111 | // Bulk-only CBW |
va009039 | 1:c072d9e580b0 | 112 | typedef __packed struct { |
va009039 | 1:c072d9e580b0 | 113 | uint32_t Signature; |
va009039 | 1:c072d9e580b0 | 114 | uint32_t Tag; |
va009039 | 1:c072d9e580b0 | 115 | uint32_t DataLength; |
va009039 | 1:c072d9e580b0 | 116 | uint8_t Flags; |
va009039 | 1:c072d9e580b0 | 117 | uint8_t LUN; |
va009039 | 1:c072d9e580b0 | 118 | uint8_t CBLength; |
va009039 | 1:c072d9e580b0 | 119 | uint8_t CB[16]; |
va009039 | 1:c072d9e580b0 | 120 | } CBW; |
va009039 | 1:c072d9e580b0 | 121 | |
va009039 | 1:c072d9e580b0 | 122 | // Bulk-only CSW |
va009039 | 1:c072d9e580b0 | 123 | typedef __packed struct { |
va009039 | 1:c072d9e580b0 | 124 | uint32_t Signature; |
va009039 | 1:c072d9e580b0 | 125 | uint32_t Tag; |
va009039 | 1:c072d9e580b0 | 126 | uint32_t DataResidue; |
va009039 | 1:c072d9e580b0 | 127 | uint8_t Status; |
va009039 | 1:c072d9e580b0 | 128 | } CSW; |
va009039 | 1:c072d9e580b0 | 129 | |
va009039 | 1:c072d9e580b0 | 130 | CBW cbw; |
va009039 | 1:c072d9e580b0 | 131 | CSW csw; |
va009039 | 1:c072d9e580b0 | 132 | |
va009039 | 1:c072d9e580b0 | 133 | int SCSITransfer(uint8_t * cmd, uint8_t cmd_len, int flags, uint8_t * data, uint32_t transfer_len); |
va009039 | 1:c072d9e580b0 | 134 | int testUnitReady(); |
va009039 | 1:c072d9e580b0 | 135 | int readCapacity(); |
va009039 | 1:c072d9e580b0 | 136 | int inquiry(uint8_t lun, uint8_t page_code); |
va009039 | 1:c072d9e580b0 | 137 | int SCSIRequestSense(); |
va009039 | 1:c072d9e580b0 | 138 | int dataTransfer(uint8_t * buf, uint32_t block, uint8_t nbBlock, int direction); |
va009039 | 1:c072d9e580b0 | 139 | int checkResult(uint8_t res, USBEndpoint * ep); |
va009039 | 1:c072d9e580b0 | 140 | int getMaxLun(); |
va009039 | 1:c072d9e580b0 | 141 | |
va009039 | 1:c072d9e580b0 | 142 | int blockSize; |
va009039 | 1:c072d9e580b0 | 143 | uint64_t blockCount; |
va009039 | 1:c072d9e580b0 | 144 | |
va009039 | 1:c072d9e580b0 | 145 | int msd_intf; |
va009039 | 1:c072d9e580b0 | 146 | bool msd_device_found; |
va009039 | 1:c072d9e580b0 | 147 | bool disk_init; |
va009039 | 1:c072d9e580b0 | 148 | |
va009039 | 1:c072d9e580b0 | 149 | void init(); |
va009039 | 1:c072d9e580b0 | 150 | |
va009039 | 1:c072d9e580b0 | 151 | // KL46Z-USBHost interface |
va009039 | 1:c072d9e580b0 | 152 | USB_TYPE controlRead(USBDeviceConnected* dev, uint8_t requestType, uint8_t request, uint32_t value, uint32_t index, uint8_t * buf, uint32_t len) { |
va009039 | 1:c072d9e580b0 | 153 | SETUP_PACKET setup = {requestType, request, value, index}; |
va009039 | 1:c072d9e580b0 | 154 | int result = host->ControlRead(&setup, buf, len); |
va009039 | 1:c072d9e580b0 | 155 | USB_DBG2("result=%d %02x", result, host->LastStatus); |
va009039 | 1:c072d9e580b0 | 156 | return (result >= 0) ? USB_TYPE_OK : USB_TYPE_ERROR; |
va009039 | 1:c072d9e580b0 | 157 | } |
va009039 | 1:c072d9e580b0 | 158 | |
va009039 | 1:c072d9e580b0 | 159 | USB_TYPE controlWrite(USBDeviceConnected* dev, uint8_t requestType, uint8_t request, uint32_t value, uint32_t index, uint8_t * buf, uint32_t len) { |
va009039 | 1:c072d9e580b0 | 160 | SETUP_PACKET setup = {requestType, request, value, index}; |
va009039 | 1:c072d9e580b0 | 161 | int result = host->ControlWrite(&setup, buf, len); |
va009039 | 1:c072d9e580b0 | 162 | USB_DBG2("result=%d %02x", result, host->LastStatus); |
va009039 | 1:c072d9e580b0 | 163 | return (result >= 0) ? USB_TYPE_OK : USB_TYPE_ERROR; |
va009039 | 1:c072d9e580b0 | 164 | } |
va009039 | 1:c072d9e580b0 | 165 | |
va009039 | 1:c072d9e580b0 | 166 | USB_TYPE bulkRead(USBDeviceConnected * dev, USBEndpoint * ep, uint8_t * buf, uint32_t len, bool blocking = true) { |
va009039 | 1:c072d9e580b0 | 167 | for(int retry = 2; retry > 0; retry--) { |
va009039 | 1:c072d9e580b0 | 168 | int result = host->BulkRead(buf, len); |
va009039 | 1:c072d9e580b0 | 169 | if (result >= 0) { |
va009039 | 1:c072d9e580b0 | 170 | return USB_TYPE_OK; |
va009039 | 1:c072d9e580b0 | 171 | } |
va009039 | 1:c072d9e580b0 | 172 | USB_DBG2("result=%d %02x", result, host->LastStatus); |
va009039 | 1:c072d9e580b0 | 173 | if (host->LastStatus != Bus_Timeout) { |
va009039 | 1:c072d9e580b0 | 174 | break; |
va009039 | 1:c072d9e580b0 | 175 | } |
va009039 | 1:c072d9e580b0 | 176 | wait_ms(10); |
va009039 | 1:c072d9e580b0 | 177 | } |
va009039 | 1:c072d9e580b0 | 178 | return USB_TYPE_ERROR; |
va009039 | 1:c072d9e580b0 | 179 | } |
va009039 | 1:c072d9e580b0 | 180 | |
va009039 | 1:c072d9e580b0 | 181 | USB_TYPE bulkWrite(USBDeviceConnected * dev, USBEndpoint * ep, uint8_t * buf, uint32_t len, bool blocking = true) { |
va009039 | 1:c072d9e580b0 | 182 | int result = host->BulkWrite(buf, len); |
va009039 | 1:c072d9e580b0 | 183 | if (result >= 0) { |
va009039 | 1:c072d9e580b0 | 184 | return USB_TYPE_OK; |
va009039 | 1:c072d9e580b0 | 185 | } |
va009039 | 1:c072d9e580b0 | 186 | USB_DBG2("result=%d %02x", result, host->LastStatus); |
va009039 | 1:c072d9e580b0 | 187 | return USB_TYPE_ERROR; |
va009039 | 1:c072d9e580b0 | 188 | } |
va009039 | 1:c072d9e580b0 | 189 | }; |
va009039 | 1:c072d9e580b0 | 190 | |
va009039 | 1:c072d9e580b0 | 191 | #endif |