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