suu pen / MSCUsbHost

Dependencies:   FatFileSystemCpp

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers MSCFileSystem.cpp Source File

MSCFileSystem.cpp

00001 /* USB Mass Storage device file system
00002  * Copyrigh (c) 2010, Igor Skochinsky
00003  * based on SDFileStorage
00004  * Copyright (c) 2008-2009, sford
00005  */
00006 
00007 /* Introduction
00008  * ------------
00009  * TODO: write one
00010  * we're basically using NXP's USBHotLite sample code, just plugging in our own FAT library
00011  */
00012 
00013 #include "MSCFileSystem.h"
00014 #include "usbhost_inc.h"
00015 
00016 /*
00017 **************************************************************************************************************
00018 *                                        PRINT CONFIGURATION
00019 **************************************************************************************************************
00020 */
00021 //#define DEBUG
00022 #ifdef DEBUG
00023 #define DEBUG_PRINT(...) printf(__VA_ARGS__)
00024 #else
00025 #define DEBUG_PRINT(...)
00026 #endif // DEBUG
00027 
00028 
00029 MSCFileSystem::MSCFileSystem(const char* name) :
00030     FATFileSystem(name)
00031 {
00032 }
00033 
00034 void print_inquiry(USB_INT08U *inqReply)
00035 {
00036     // see USB Mass Storage Class – UFI Command Specification,
00037     // 4.2 INQUIRY Command
00038     DEBUG_PRINT("Inquiry reply:\n");
00039     uint8_t tmp = inqReply[0]&0x1F;
00040     DEBUG_PRINT("Peripheral device type: %02Xh\n", tmp);
00041     if ( tmp == 0 )
00042         DEBUG_PRINT("\t- Direct access (floppy)\n");
00043     else if ( tmp == 0x1F )
00044         DEBUG_PRINT("\t- none (no FDD connected)\n");
00045     else
00046         DEBUG_PRINT("\t- unknown type\n");
00047     tmp = inqReply[1] >> 7;
00048     DEBUG_PRINT("Removable Media Bit: %d\n", tmp);
00049     tmp = inqReply[2] & 3;
00050     DEBUG_PRINT("ANSI Version: %02Xh\n", tmp);
00051     if ( tmp != 0 )
00052         DEBUG_PRINT("\t- warning! must be 0\n");
00053     tmp = (inqReply[2]>>3) & 3;
00054     DEBUG_PRINT("ECMA Version: %02Xh\n", tmp);
00055     if ( tmp != 0 )
00056         DEBUG_PRINT("\t- warning! should be 0\n");
00057     tmp = inqReply[2]>>6;
00058     DEBUG_PRINT("ISO Version: %02Xh\n", tmp);
00059     if ( tmp != 0 )
00060         DEBUG_PRINT("\t- warning! should be 0\n");
00061     tmp = inqReply[3] & 0xF;
00062     DEBUG_PRINT("Response Data Format: %02Xh\n", tmp);
00063     if ( tmp != 1 )
00064         DEBUG_PRINT("\t- warning! should be 1\n");
00065     tmp = inqReply[4];
00066     DEBUG_PRINT("Additional length: %02Xh\n", tmp);
00067     if ( tmp != 0x1F )
00068         DEBUG_PRINT("\t- warning! should be 1Fh\n");
00069     DEBUG_PRINT("Vendor Information: '%.8s'\n", &inqReply[8]);
00070     DEBUG_PRINT("Product Identification: '%.16s'\n", &inqReply[16]);
00071     DEBUG_PRINT("Product Revision: '%.4s'\n", &inqReply[32]);
00072 }
00073 
00074 int MSCFileSystem::initialise_msc()
00075 {
00076     USB_INT32S  rc;
00077     USB_INT08U  inquiryResult[INQUIRY_LENGTH];
00078 
00079     //print_clock();
00080     Host_Init();               /* Initialize the  host controller                                    */
00081     rc = Host_EnumDev();       /* Enumerate the device connected    ベースでは、USB memoryがつながるまで待っている (HOST_RhscIntr 1:deviceあり 0:なし)                                        */
00082     if (rc != OK) {
00083         fprintf(stderr, "Could not enumerate device: %d\n", rc);
00084         return rc;
00085     }
00086 
00087 
00088     /* Initialize the mass storage and scsi interfaces */
00089     rc = MS_Init( &_blkSize, &_numBlks, inquiryResult );
00090     if (rc != OK) {
00091         fprintf(stderr, "Could not initialize mass storage interface: %d\n", rc);
00092         return rc;
00093     }
00094     DEBUG_PRINT("Successfully initialized mass storage interface; %d blocks of size %d\n", _numBlks, _blkSize);
00095     print_inquiry(inquiryResult);
00096     // FATFileSystem supports only 512-byte blocks
00097     return _blkSize == 512 ? OK : 1;
00098 }
00099 
00100 /** USBの存在確認
00101  * @return 1:ない 0:ない 
00102  */
00103 int MSCFileSystem::disk_usbCheck()
00104 {
00105     if(Host_Check() != OK){
00106         return 1;
00107         }
00108         return 0;
00109 
00110     }
00111 
00112 
00113 int MSCFileSystem::disk_initialize()    // 元からある関数。複
00114 {
00115         if ( initialise_msc() != OK )
00116             return 1;
00117 
00118         return 0;
00119 }
00120 
00121 int MSCFileSystem::disk_write(const char *buffer, int block_number)
00122 {
00123     if ( OK == MS_BulkSend(block_number, 1, (USB_INT08U *)buffer) )
00124         return 0;
00125     return 1;
00126 }
00127 
00128 int MSCFileSystem::disk_read(char *buffer, int block_number)
00129 {
00130     if ( OK == MS_BulkRecv(block_number, 1, (USB_INT08U *)buffer) )
00131         return 0;
00132     return 1;
00133 }
00134 
00135 int MSCFileSystem::disk_status()
00136 {
00137     return 0;
00138 }
00139 int MSCFileSystem::disk_sync()
00140 {
00141     return 0;
00142 }
00143 int MSCFileSystem::disk_sectors()
00144 {
00145     return _numBlks;
00146 }