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