Kenji Arai / USBHOST

Dependents:   DISCO-F469NI_USB_Disk STM32F4xx_USB_Memory

Fork of USBHOST by ST

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers USBHostMSD.h Source File

USBHostMSD.h

00001 /* mbed USBHost Library
00002  * Copyright (c) 2006-2013 ARM Limited
00003  *
00004  * Licensed under the Apache License, Version 2.0 (the "License");
00005  * you may not use this file except in compliance with the License.
00006  * You may obtain a copy of the License at
00007  *
00008  *     http://www.apache.org/licenses/LICENSE-2.0
00009  *
00010  * Unless required by applicable law or agreed to in writing, software
00011  * distributed under the License is distributed on an "AS IS" BASIS,
00012  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00013  * See the License for the specific language governing permissions and
00014  * limitations under the License.
00015  */
00016 
00017 #ifndef USBHOSTMSD_H
00018 #define USBHOSTMSD_H
00019 
00020 #include "USBHostConf.h"
00021 
00022 #if USBHOST_MSD
00023 
00024 #include "USBHost.h"
00025 #include "FATFileSystem.h"
00026 #include "BlockDevice.h"
00027 
00028 /**
00029  * A class to communicate a USB flash disk
00030  */
00031 class USBHostMSD : public IUSBEnumerator, public BlockDevice
00032 {
00033 public:
00034     /**
00035      * Constructor
00036      *
00037      * @param rootdir mount name
00038      */
00039     USBHostMSD();
00040 
00041     /**
00042      * Check if a MSD device is connected
00043      *
00044      * @return true if a MSD device is connected
00045      */
00046     bool connected();
00047 
00048     /**
00049      * Try to connect to a MSD device
00050      *
00051      * @return true if connection was successful
00052      */
00053     bool connect();
00054     virtual int init();
00055     virtual int deinit()
00056     {
00057         return BD_ERROR_OK;
00058     };
00059     virtual int read(void *buffer, bd_addr_t addr, bd_size_t size);
00060     virtual int program(const void *buffer, bd_addr_t addr, bd_size_t size);
00061     virtual int erase(bd_addr_t addr, bd_size_t size);
00062     virtual bd_size_t get_read_size() const;
00063     virtual bd_size_t get_program_size() const;
00064     virtual bd_size_t get_erase_size() const;
00065     virtual bd_size_t size() const;
00066     virtual const char *get_type() const;
00067 
00068 
00069 
00070 protected:
00071     //From IUSBEnumerator
00072     virtual void setVidPid(uint16_t vid, uint16_t pid);
00073     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
00074     virtual bool useEndpoint(uint8_t intf_nb, ENDPOINT_TYPE type, ENDPOINT_DIRECTION dir); //Must return true if the endpoint will be used
00075 
00076 
00077 private:
00078     USBHost * host;
00079     USBDeviceConnected * dev;
00080     bool dev_connected;
00081     USBEndpoint * bulk_in;
00082     USBEndpoint * bulk_out;
00083     uint8_t nb_ep;
00084 
00085     // Bulk-only CBW
00086     typedef struct {
00087         uint32_t Signature;
00088         uint32_t Tag;
00089         uint32_t DataLength;
00090         uint8_t  Flags;
00091         uint8_t  LUN;
00092         uint8_t  CBLength;
00093         uint8_t  CB[16];
00094     } PACKED CBW;
00095 
00096     // Bulk-only CSW
00097     typedef struct {
00098         uint32_t Signature;
00099         uint32_t Tag;
00100         uint32_t DataResidue;
00101         uint8_t  Status;
00102     } PACKED CSW;
00103 
00104     CBW cbw;
00105     CSW csw;
00106 
00107     int SCSITransfer(uint8_t * cmd, uint8_t cmd_len, int flags, uint8_t * data, uint32_t transfer_len);
00108     int testUnitReady();
00109     int readCapacity();
00110     int inquiry(uint8_t lun, uint8_t page_code);
00111     int SCSIRequestSense();
00112     int dataTransfer(uint8_t * buf, uint32_t block, uint8_t nbBlock, int direction);
00113     int checkResult(uint8_t res, USBEndpoint * ep);
00114     int getMaxLun();
00115 
00116     int blockSize;
00117     uint32_t blockCount;
00118 
00119     int msd_intf;
00120     bool msd_device_found;
00121     bool disk_init;
00122 
00123     void init_usb();
00124 
00125 };
00126 
00127 #endif
00128 
00129 #endif