Simple USBHost library for STM32F746NG Discovery board. Only either the Fastspeed or the Highspeed port can be used( not both together)
Dependents: DISCO-F746NG_USB_Host
Fork of KL46Z-USBHost by
USBHostMSD/USBHostMSD.h@8:6463cd1964c0, 2014-01-31 (annotated)
- Committer:
- va009039
- Date:
- Fri Jan 31 13:45:07 2014 +0000
- Revision:
- 8:6463cd1964c0
- Parent:
- 3:a3872f7593e2
- Child:
- 9:7f9f64cf5ded
USB hub support.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
va009039 | 3:a3872f7593e2 | 1 | /* mbed USBHost Library |
va009039 | 3:a3872f7593e2 | 2 | * Copyright (c) 2006-2013 ARM Limited |
va009039 | 3:a3872f7593e2 | 3 | * |
va009039 | 3:a3872f7593e2 | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
va009039 | 3:a3872f7593e2 | 5 | * you may not use this file except in compliance with the License. |
va009039 | 3:a3872f7593e2 | 6 | * You may obtain a copy of the License at |
va009039 | 3:a3872f7593e2 | 7 | * |
va009039 | 3:a3872f7593e2 | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
va009039 | 3:a3872f7593e2 | 9 | * |
va009039 | 3:a3872f7593e2 | 10 | * Unless required by applicable law or agreed to in writing, software |
va009039 | 3:a3872f7593e2 | 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
va009039 | 3:a3872f7593e2 | 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
va009039 | 3:a3872f7593e2 | 13 | * See the License for the specific language governing permissions and |
va009039 | 3:a3872f7593e2 | 14 | * limitations under the License. |
va009039 | 3:a3872f7593e2 | 15 | */ |
va009039 | 3:a3872f7593e2 | 16 | |
va009039 | 3:a3872f7593e2 | 17 | #ifndef USBHOSTMSD_H |
va009039 | 3:a3872f7593e2 | 18 | #define USBHOSTMSD_H |
va009039 | 3:a3872f7593e2 | 19 | |
va009039 | 8:6463cd1964c0 | 20 | #if _USB_DBG |
va009039 | 3:a3872f7593e2 | 21 | #define USB_DBG(...) do{fprintf(stderr,"[%s@%d] ",__PRETTY_FUNCTION__,__LINE__);fprintf(stderr,__VA_ARGS__);fprintf(stderr,"\r\n");} while(0); |
va009039 | 3:a3872f7593e2 | 22 | #else |
va009039 | 3:a3872f7593e2 | 23 | #define USB_DBG(...) while(0); |
va009039 | 3:a3872f7593e2 | 24 | #endif |
va009039 | 3:a3872f7593e2 | 25 | |
va009039 | 8:6463cd1964c0 | 26 | #if 0 |
va009039 | 8:6463cd1964c0 | 27 | #define USB_DBG2(...) do{fprintf(stderr,"[%s@%d] ",__PRETTY_FUNCTION__,__LINE__);fprintf(stderr,__VA_ARGS__);fprintf(stderr,"\r\n");} while(0); |
va009039 | 8:6463cd1964c0 | 28 | #else |
va009039 | 8:6463cd1964c0 | 29 | #define USB_DBG2(...) while(0); |
va009039 | 8:6463cd1964c0 | 30 | #endif |
va009039 | 8:6463cd1964c0 | 31 | |
va009039 | 3:a3872f7593e2 | 32 | #if 1 |
va009039 | 3:a3872f7593e2 | 33 | #define USB_INFO(...) do{fprintf(stderr,__VA_ARGS__);fprintf(stderr,"\r\n");}while(0); |
va009039 | 3:a3872f7593e2 | 34 | #else |
va009039 | 3:a3872f7593e2 | 35 | #define USB_INFO(...) while(0); |
va009039 | 3:a3872f7593e2 | 36 | #endif |
va009039 | 3:a3872f7593e2 | 37 | |
va009039 | 3:a3872f7593e2 | 38 | #include "USBHostConf.h" |
va009039 | 3:a3872f7593e2 | 39 | #include "USBHost.h" |
va009039 | 3:a3872f7593e2 | 40 | #include "FATFileSystem.h" |
va009039 | 3:a3872f7593e2 | 41 | |
va009039 | 3:a3872f7593e2 | 42 | /** |
va009039 | 3:a3872f7593e2 | 43 | * A class to communicate a USB flash disk |
va009039 | 3:a3872f7593e2 | 44 | */ |
va009039 | 8:6463cd1964c0 | 45 | class USBHostMSD : public IUSBEnumerator, public FATFileSystem { |
va009039 | 3:a3872f7593e2 | 46 | public: |
va009039 | 3:a3872f7593e2 | 47 | /** |
va009039 | 3:a3872f7593e2 | 48 | * Constructor |
va009039 | 3:a3872f7593e2 | 49 | * |
va009039 | 3:a3872f7593e2 | 50 | * @param rootdir mount name |
va009039 | 3:a3872f7593e2 | 51 | */ |
va009039 | 3:a3872f7593e2 | 52 | USBHostMSD(const char * rootdir); |
va009039 | 3:a3872f7593e2 | 53 | |
va009039 | 3:a3872f7593e2 | 54 | /** |
va009039 | 3:a3872f7593e2 | 55 | * Check if a MSD device is connected |
va009039 | 3:a3872f7593e2 | 56 | * |
va009039 | 3:a3872f7593e2 | 57 | * @return true if a MSD device is connected |
va009039 | 3:a3872f7593e2 | 58 | */ |
va009039 | 3:a3872f7593e2 | 59 | bool connected(); |
va009039 | 3:a3872f7593e2 | 60 | |
va009039 | 3:a3872f7593e2 | 61 | /** |
va009039 | 3:a3872f7593e2 | 62 | * Try to connect to a MSD device |
va009039 | 3:a3872f7593e2 | 63 | * |
va009039 | 3:a3872f7593e2 | 64 | * @return true if connection was successful |
va009039 | 3:a3872f7593e2 | 65 | */ |
va009039 | 3:a3872f7593e2 | 66 | bool connect(); |
va009039 | 3:a3872f7593e2 | 67 | |
va009039 | 3:a3872f7593e2 | 68 | protected: |
va009039 | 8:6463cd1964c0 | 69 | //From IUSBEnumerator |
va009039 | 8:6463cd1964c0 | 70 | virtual void setVidPid(uint16_t vid, uint16_t pid); |
va009039 | 8:6463cd1964c0 | 71 | virtual bool parseInterface(uint8_t intf_nb, uint8_t intf_class, uint8_t intf_subclass, uint8_t intf_protocol); //Must return true if the interface should be parsed |
va009039 | 8:6463cd1964c0 | 72 | virtual bool useEndpoint(uint8_t intf_nb, ENDPOINT_TYPE type, ENDPOINT_DIRECTION dir); //Must return true if the endpoint will be used |
va009039 | 3:a3872f7593e2 | 73 | |
va009039 | 3:a3872f7593e2 | 74 | // From FATFileSystem |
va009039 | 3:a3872f7593e2 | 75 | virtual int disk_initialize(); |
va009039 | 3:a3872f7593e2 | 76 | virtual int disk_status() {return 0;}; |
va009039 | 3:a3872f7593e2 | 77 | virtual int disk_read(uint8_t * buffer, uint64_t sector); |
va009039 | 3:a3872f7593e2 | 78 | virtual int disk_write(const uint8_t * buffer, uint64_t sector); |
va009039 | 3:a3872f7593e2 | 79 | virtual int disk_sync() {return 0;}; |
va009039 | 3:a3872f7593e2 | 80 | virtual uint64_t disk_sectors(); |
va009039 | 3:a3872f7593e2 | 81 | |
va009039 | 3:a3872f7593e2 | 82 | private: |
va009039 | 3:a3872f7593e2 | 83 | USBHost * host; |
va009039 | 3:a3872f7593e2 | 84 | USBDeviceConnected * dev; |
va009039 | 3:a3872f7593e2 | 85 | bool dev_connected; |
va009039 | 3:a3872f7593e2 | 86 | USBEndpoint * bulk_in; |
va009039 | 3:a3872f7593e2 | 87 | USBEndpoint * bulk_out; |
va009039 | 3:a3872f7593e2 | 88 | uint8_t nb_ep; |
va009039 | 3:a3872f7593e2 | 89 | |
va009039 | 3:a3872f7593e2 | 90 | // Bulk-only CBW |
va009039 | 3:a3872f7593e2 | 91 | typedef __packed struct { |
va009039 | 3:a3872f7593e2 | 92 | uint32_t Signature; |
va009039 | 3:a3872f7593e2 | 93 | uint32_t Tag; |
va009039 | 3:a3872f7593e2 | 94 | uint32_t DataLength; |
va009039 | 3:a3872f7593e2 | 95 | uint8_t Flags; |
va009039 | 3:a3872f7593e2 | 96 | uint8_t LUN; |
va009039 | 3:a3872f7593e2 | 97 | uint8_t CBLength; |
va009039 | 3:a3872f7593e2 | 98 | uint8_t CB[16]; |
va009039 | 3:a3872f7593e2 | 99 | } CBW; |
va009039 | 3:a3872f7593e2 | 100 | |
va009039 | 3:a3872f7593e2 | 101 | // Bulk-only CSW |
va009039 | 3:a3872f7593e2 | 102 | typedef __packed struct { |
va009039 | 3:a3872f7593e2 | 103 | uint32_t Signature; |
va009039 | 3:a3872f7593e2 | 104 | uint32_t Tag; |
va009039 | 3:a3872f7593e2 | 105 | uint32_t DataResidue; |
va009039 | 3:a3872f7593e2 | 106 | uint8_t Status; |
va009039 | 3:a3872f7593e2 | 107 | } CSW; |
va009039 | 3:a3872f7593e2 | 108 | |
va009039 | 3:a3872f7593e2 | 109 | CBW cbw; |
va009039 | 3:a3872f7593e2 | 110 | CSW csw; |
va009039 | 3:a3872f7593e2 | 111 | |
va009039 | 3:a3872f7593e2 | 112 | int SCSITransfer(uint8_t * cmd, uint8_t cmd_len, int flags, uint8_t * data, uint32_t transfer_len); |
va009039 | 3:a3872f7593e2 | 113 | int testUnitReady(); |
va009039 | 3:a3872f7593e2 | 114 | int readCapacity(); |
va009039 | 3:a3872f7593e2 | 115 | int inquiry(uint8_t lun, uint8_t page_code); |
va009039 | 3:a3872f7593e2 | 116 | int SCSIRequestSense(); |
va009039 | 3:a3872f7593e2 | 117 | int dataTransfer(uint8_t * buf, uint32_t block, uint8_t nbBlock, int direction); |
va009039 | 3:a3872f7593e2 | 118 | int checkResult(uint8_t res, USBEndpoint * ep); |
va009039 | 3:a3872f7593e2 | 119 | int getMaxLun(); |
va009039 | 3:a3872f7593e2 | 120 | |
va009039 | 3:a3872f7593e2 | 121 | int blockSize; |
va009039 | 3:a3872f7593e2 | 122 | uint64_t blockCount; |
va009039 | 3:a3872f7593e2 | 123 | |
va009039 | 3:a3872f7593e2 | 124 | int msd_intf; |
va009039 | 3:a3872f7593e2 | 125 | bool msd_device_found; |
va009039 | 3:a3872f7593e2 | 126 | bool disk_init; |
va009039 | 3:a3872f7593e2 | 127 | |
va009039 | 3:a3872f7593e2 | 128 | void init(); |
va009039 | 3:a3872f7593e2 | 129 | }; |
va009039 | 3:a3872f7593e2 | 130 | |
va009039 | 3:a3872f7593e2 | 131 | #endif |