Implementation of USB Host for mbed 1768 which can access USB flash drives. See notebook page for details.

Dependencies:   FatFileSystemCpp mbed

Committer:
igorsk
Date:
Mon Jul 30 13:49:56 2012 +0000
Revision:
3:95e55809ecdb
Parent:
0:e294af8d0e07
Switched to FatFileSystemCpp

Who changed what in which revision?

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