Revised to support both SD and USB file system

Dependents:   Multi-FileSystem Multi-FileSystem

Fork of MSCFileSystem by Chris Styles

Committer:
WiredHome
Date:
Mon Oct 17 00:57:08 2016 +0000
Revision:
10:4072b4b1c6f4
Parent:
9:de4ea8f3349b
Minor change in how a buffer was allocated in memory to not be hard-coded address.

Who changed what in which revision?

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