USBHOST lib for STM

Dependents:   door-access-controller-dev

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 
00067 
00068 
00069 protected:
00070     //From IUSBEnumerator
00071     virtual void setVidPid(uint16_t vid, uint16_t pid);
00072     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
00073     virtual bool useEndpoint(uint8_t intf_nb, ENDPOINT_TYPE type, ENDPOINT_DIRECTION dir); //Must return true if the endpoint will be used
00074 
00075 
00076 private:
00077     USBHost * host;
00078     USBDeviceConnected * dev;
00079     bool dev_connected;
00080     USBEndpoint * bulk_in;
00081     USBEndpoint * bulk_out;
00082     uint8_t nb_ep;
00083 
00084     // Bulk-only CBW
00085     typedef struct {
00086         uint32_t Signature;
00087         uint32_t Tag;
00088         uint32_t DataLength;
00089         uint8_t  Flags;
00090         uint8_t  LUN;
00091         uint8_t  CBLength;
00092         uint8_t  CB[16];
00093     } PACKED CBW;
00094 
00095     // Bulk-only CSW
00096     typedef struct {
00097         uint32_t Signature;
00098         uint32_t Tag;
00099         uint32_t DataResidue;
00100         uint8_t  Status;
00101     } PACKED CSW;
00102 
00103     CBW cbw;
00104     CSW csw;
00105 
00106     int SCSITransfer(uint8_t * cmd, uint8_t cmd_len, int flags, uint8_t * data, uint32_t transfer_len);
00107     int testUnitReady();
00108     int readCapacity();
00109     int inquiry(uint8_t lun, uint8_t page_code);
00110     int SCSIRequestSense();
00111     int dataTransfer(uint8_t * buf, uint32_t block, uint8_t nbBlock, int direction);
00112     int checkResult(uint8_t res, USBEndpoint * ep);
00113     int getMaxLun();
00114 
00115     int blockSize;
00116     uint32_t blockCount;
00117 
00118     int msd_intf;
00119     bool msd_device_found;
00120     bool disk_init;
00121 
00122     void init_usb();
00123 
00124 };
00125 
00126 #endif
00127 
00128 #endif