Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependents: F401RE-USBHostMIDI_RecieveExample
Fork of F401RE-USBHost by
Diff: USBHostMSD.h
- Revision:
- 1:c072d9e580b0
- Child:
- 2:0cdac6bcc534
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/USBHostMSD.h Tue Jan 21 08:59:28 2014 +0000
@@ -0,0 +1,191 @@
+/* mbed USBHost Library
+ * Copyright (c) 2006-2013 ARM Limited
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#ifndef USBHOSTMSD_H
+#define USBHOSTMSD_H
+
+#if 0
+#define USB_DBG(...) do{fprintf(stderr,"[%s@%d] ",__PRETTY_FUNCTION__,__LINE__);fprintf(stderr,__VA_ARGS__);fprintf(stderr,"\r\n");} while(0);
+#else
+#define USB_DBG(...) while(0);
+#endif
+
+#define USB_DBG2(...) do{fprintf(stderr,"[%s@%d] ",__PRETTY_FUNCTION__,__LINE__);fprintf(stderr,__VA_ARGS__);fprintf(stderr,"\r\n");} while(0);
+
+#if 1
+#define USB_INFO(...) do{fprintf(stderr,__VA_ARGS__);fprintf(stderr,"\r\n");}while(0);
+#else
+#define USB_INFO(...) while(0);
+#endif
+
+//#include "USBHostConf.h"
+enum USB_TYPE {
+ USB_TYPE_OK = 0,
+ // completion code
+ USB_TYPE_STALL_ERROR = 4,
+ // general usb state
+ USB_TYPE_IDLE = 16,
+ USB_TYPE_ERROR = 18,
+};
+
+// ----------- Control RequestType Fields -----------
+#define USB_DEVICE_TO_HOST 0x80
+#define USB_HOST_TO_DEVICE 0x00
+#define USB_REQUEST_TYPE_CLASS 0x20
+#define USB_REQUEST_TYPE_STANDARD 0x00
+#define USB_RECIPIENT_DEVICE 0x00
+#define USB_RECIPIENT_INTERFACE 0x01
+#define USB_RECIPIENT_ENDPOINT 0x02
+
+// -------------- USB Standard Requests --------------
+#define CLEAR_FEATURE 0x01
+
+#include "USBHost.h"
+#include "FATFileSystem.h"
+
+class USBDeviceConnected { //dummy
+};
+
+struct USBEndpoint { // dummy
+ void setState(uint8_t st){};
+ uint8_t getAddress(){ return 0; };
+};
+
+/**
+ * A class to communicate a USB flash disk
+ */
+class USBHostMSD : public FATFileSystem {
+public:
+ /**
+ * Constructor
+ *
+ * @param rootdir mount name
+ */
+ USBHostMSD(const char * rootdir);
+
+ /**
+ * Check if a MSD device is connected
+ *
+ * @return true if a MSD device is connected
+ */
+ bool connected();
+
+ /**
+ * Try to connect to a MSD device
+ *
+ * @return true if connection was successful
+ */
+ bool connect();
+
+protected:
+
+ // From FATFileSystem
+ virtual int disk_initialize();
+ virtual int disk_status() {return 0;};
+ virtual int disk_read(uint8_t * buffer, uint64_t sector);
+ virtual int disk_write(const uint8_t * buffer, uint64_t sector);
+ virtual int disk_sync() {return 0;};
+ virtual uint64_t disk_sectors();
+
+private:
+ USBHost * host;
+ USBDeviceConnected * dev;
+ bool dev_connected;
+ USBEndpoint * bulk_in;
+ USBEndpoint * bulk_out;
+ uint8_t nb_ep;
+
+ // Bulk-only CBW
+ typedef __packed struct {
+ uint32_t Signature;
+ uint32_t Tag;
+ uint32_t DataLength;
+ uint8_t Flags;
+ uint8_t LUN;
+ uint8_t CBLength;
+ uint8_t CB[16];
+ } CBW;
+
+ // Bulk-only CSW
+ typedef __packed struct {
+ uint32_t Signature;
+ uint32_t Tag;
+ uint32_t DataResidue;
+ uint8_t Status;
+ } CSW;
+
+ CBW cbw;
+ CSW csw;
+
+ int SCSITransfer(uint8_t * cmd, uint8_t cmd_len, int flags, uint8_t * data, uint32_t transfer_len);
+ int testUnitReady();
+ int readCapacity();
+ int inquiry(uint8_t lun, uint8_t page_code);
+ int SCSIRequestSense();
+ int dataTransfer(uint8_t * buf, uint32_t block, uint8_t nbBlock, int direction);
+ int checkResult(uint8_t res, USBEndpoint * ep);
+ int getMaxLun();
+
+ int blockSize;
+ uint64_t blockCount;
+
+ int msd_intf;
+ bool msd_device_found;
+ bool disk_init;
+
+ void init();
+
+ // KL46Z-USBHost interface
+ USB_TYPE controlRead(USBDeviceConnected* dev, uint8_t requestType, uint8_t request, uint32_t value, uint32_t index, uint8_t * buf, uint32_t len) {
+ SETUP_PACKET setup = {requestType, request, value, index};
+ int result = host->ControlRead(&setup, buf, len);
+ USB_DBG2("result=%d %02x", result, host->LastStatus);
+ return (result >= 0) ? USB_TYPE_OK : USB_TYPE_ERROR;
+ }
+
+ USB_TYPE controlWrite(USBDeviceConnected* dev, uint8_t requestType, uint8_t request, uint32_t value, uint32_t index, uint8_t * buf, uint32_t len) {
+ SETUP_PACKET setup = {requestType, request, value, index};
+ int result = host->ControlWrite(&setup, buf, len);
+ USB_DBG2("result=%d %02x", result, host->LastStatus);
+ return (result >= 0) ? USB_TYPE_OK : USB_TYPE_ERROR;
+ }
+
+ USB_TYPE bulkRead(USBDeviceConnected * dev, USBEndpoint * ep, uint8_t * buf, uint32_t len, bool blocking = true) {
+ for(int retry = 2; retry > 0; retry--) {
+ int result = host->BulkRead(buf, len);
+ if (result >= 0) {
+ return USB_TYPE_OK;
+ }
+ USB_DBG2("result=%d %02x", result, host->LastStatus);
+ if (host->LastStatus != Bus_Timeout) {
+ break;
+ }
+ wait_ms(10);
+ }
+ return USB_TYPE_ERROR;
+ }
+
+ USB_TYPE bulkWrite(USBDeviceConnected * dev, USBEndpoint * ep, uint8_t * buf, uint32_t len, bool blocking = true) {
+ int result = host->BulkWrite(buf, len);
+ if (result >= 0) {
+ return USB_TYPE_OK;
+ }
+ USB_DBG2("result=%d %02x", result, host->LastStatus);
+ return USB_TYPE_ERROR;
+ }
+};
+
+#endif
