Dependencies:   mbed

Committer:
CE
Date:
Thu Jan 06 19:01:44 2011 +0000
Revision:
0:0732b16d9a92
R1A

Who changed what in which revision?

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