MSC_20200408_

Committer:
MPPT51
Date:
Wed Apr 08 01:17:45 2020 +0000
Revision:
0:f19563caba14
20200408_MSC_kikkawa

Who changed what in which revision?

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