Dependencies:   mbed

Committer:
joe
Date:
Fri Aug 20 15:38:52 2010 +0000
Revision:
2:a079de4fd5b9
Parent:
0:960b355eaa84

        

Who changed what in which revision?

UserRevisionLine numberNew contents of line
joe 0:960b355eaa84 1 /* USB Mass Storage device file system
joe 0:960b355eaa84 2 * Copyrigh (c) 2010, Igor Skochinsky
joe 0:960b355eaa84 3 * based on SDFileStorage
joe 0:960b355eaa84 4 * Copyright (c) 2008-2009, sford
joe 0:960b355eaa84 5 */
joe 0:960b355eaa84 6
joe 0:960b355eaa84 7 /* Introduction
joe 0:960b355eaa84 8 * ------------
joe 0:960b355eaa84 9 * TODO: write one
joe 0:960b355eaa84 10 * we're basically using NXP's USBHotLite sample code, just plugging in our own FAT library
joe 0:960b355eaa84 11 */
joe 0:960b355eaa84 12
joe 0:960b355eaa84 13 #include "MSCFileSystem.h"
joe 0:960b355eaa84 14 #include "usbhost_inc.h"
joe 0:960b355eaa84 15
joe 0:960b355eaa84 16 MSCFileSystem::MSCFileSystem(const char* name) :
joe 0:960b355eaa84 17 FATFileSystem(name)
joe 0:960b355eaa84 18 {
joe 0:960b355eaa84 19 }
joe 0:960b355eaa84 20
joe 0:960b355eaa84 21 void print_inquiry(USB_INT08U *inqReply)
joe 0:960b355eaa84 22 {
joe 0:960b355eaa84 23 // see USB Mass Storage Class – UFI Command Specification,
joe 0:960b355eaa84 24 // 4.2 INQUIRY Command
joe 0:960b355eaa84 25 printf("Inquiry reply:\n");
joe 0:960b355eaa84 26 uint8_t tmp = inqReply[0]&0x1F;
joe 0:960b355eaa84 27 printf("Peripheral device type: %02Xh\n", tmp);
joe 0:960b355eaa84 28 if ( tmp == 0 )
joe 0:960b355eaa84 29 printf("\t- Direct access (floppy)\n");
joe 0:960b355eaa84 30 else if ( tmp == 0x1F )
joe 0:960b355eaa84 31 printf("\t- none (no FDD connected)\n");
joe 0:960b355eaa84 32 else
joe 0:960b355eaa84 33 printf("\t- unknown type\n");
joe 0:960b355eaa84 34 tmp = inqReply[1] >> 7;
joe 0:960b355eaa84 35 printf("Removable Media Bit: %d\n", tmp);
joe 0:960b355eaa84 36 tmp = inqReply[2] & 3;
joe 0:960b355eaa84 37 printf("ANSI Version: %02Xh\n", tmp);
joe 0:960b355eaa84 38 if ( tmp != 0 )
joe 0:960b355eaa84 39 printf("\t- warning! must be 0\n");
joe 0:960b355eaa84 40 tmp = (inqReply[2]>>3) & 3;
joe 0:960b355eaa84 41 printf("ECMA Version: %02Xh\n", tmp);
joe 0:960b355eaa84 42 if ( tmp != 0 )
joe 0:960b355eaa84 43 printf("\t- warning! should be 0\n");
joe 0:960b355eaa84 44 tmp = inqReply[2]>>6;
joe 0:960b355eaa84 45 printf("ISO Version: %02Xh\n", tmp);
joe 0:960b355eaa84 46 if ( tmp != 0 )
joe 0:960b355eaa84 47 printf("\t- warning! should be 0\n");
joe 0:960b355eaa84 48 tmp = inqReply[3] & 0xF;
joe 0:960b355eaa84 49 printf("Response Data Format: %02Xh\n", tmp);
joe 0:960b355eaa84 50 if ( tmp != 1 )
joe 0:960b355eaa84 51 printf("\t- warning! should be 1\n");
joe 0:960b355eaa84 52 tmp = inqReply[4];
joe 0:960b355eaa84 53 printf("Additional length: %02Xh\n", tmp);
joe 0:960b355eaa84 54 if ( tmp != 0x1F )
joe 0:960b355eaa84 55 printf("\t- warning! should be 1Fh\n");
joe 0:960b355eaa84 56 printf("Vendor Information: '%.8s'\n", &inqReply[8]);
joe 0:960b355eaa84 57 printf("Product Identification: '%.16s'\n", &inqReply[16]);
joe 0:960b355eaa84 58 printf("Product Revision: '%.4s'\n", &inqReply[32]);
joe 0:960b355eaa84 59 }
joe 0:960b355eaa84 60
joe 0:960b355eaa84 61 int MSCFileSystem::initialise_msc()
joe 0:960b355eaa84 62 {
joe 0:960b355eaa84 63 USB_INT32S rc;
joe 0:960b355eaa84 64 USB_INT08U inquiryResult[INQUIRY_LENGTH];
joe 0:960b355eaa84 65
joe 0:960b355eaa84 66 //print_clock();
joe 0:960b355eaa84 67 Host_Init(); /* Initialize the host controller */
joe 0:960b355eaa84 68 rc = Host_EnumDev(); /* Enumerate the device connected */
joe 0:960b355eaa84 69 if (rc != OK)
joe 0:960b355eaa84 70 {
joe 0:960b355eaa84 71 fprintf(stderr, "Could not enumerate device: %d\n", rc);
joe 0:960b355eaa84 72 return rc;
joe 0:960b355eaa84 73 }
joe 0:960b355eaa84 74
joe 0:960b355eaa84 75
joe 0:960b355eaa84 76 /* Initialize the mass storage and scsi interfaces */
joe 0:960b355eaa84 77 rc = MS_Init( &_blkSize, &_numBlks, inquiryResult );
joe 0:960b355eaa84 78 if (rc != OK)
joe 0:960b355eaa84 79 {
joe 0:960b355eaa84 80 fprintf(stderr, "Could not initialize mass storage interface: %d\n", rc);
joe 0:960b355eaa84 81 return rc;
joe 0:960b355eaa84 82 }
joe 0:960b355eaa84 83 printf("Successfully initialized mass storage interface; %d blocks of size %d\n", _numBlks, _blkSize);
joe 0:960b355eaa84 84 print_inquiry(inquiryResult);
joe 0:960b355eaa84 85 // FATFileSystem supports only 512-byte blocks
joe 0:960b355eaa84 86 return _blkSize == 512 ? OK : 1;
joe 0:960b355eaa84 87 }
joe 0:960b355eaa84 88
joe 0:960b355eaa84 89 int MSCFileSystem::disk_initialize()
joe 0:960b355eaa84 90 {
joe 0:960b355eaa84 91 if ( initialise_msc() != OK )
joe 0:960b355eaa84 92 return 1;
joe 0:960b355eaa84 93
joe 0:960b355eaa84 94 return 0;
joe 0:960b355eaa84 95 }
joe 0:960b355eaa84 96
joe 0:960b355eaa84 97 int MSCFileSystem::disk_write(const char *buffer, int block_number)
joe 0:960b355eaa84 98 {
joe 0:960b355eaa84 99 if ( OK == MS_BulkSend(block_number, 1, (USB_INT08U *)buffer) )
joe 0:960b355eaa84 100 return 0;
joe 0:960b355eaa84 101 return 1;
joe 0:960b355eaa84 102 }
joe 0:960b355eaa84 103
joe 0:960b355eaa84 104 int MSCFileSystem::disk_read(char *buffer, int block_number)
joe 0:960b355eaa84 105 {
joe 0:960b355eaa84 106 if ( OK == MS_BulkRecv(block_number, 1, (USB_INT08U *)buffer) )
joe 0:960b355eaa84 107 return 0;
joe 0:960b355eaa84 108 return 1;
joe 0:960b355eaa84 109 }
joe 0:960b355eaa84 110
joe 0:960b355eaa84 111 int MSCFileSystem::disk_status() { return 0; }
joe 0:960b355eaa84 112 int MSCFileSystem::disk_sync() { return 0; }
joe 0:960b355eaa84 113 int MSCFileSystem::disk_sectors() { return _numBlks; }