Pendrive to sd card data transfer coding

Dependencies:   FatFileSystem TextLCD mbed

Fork of MSCUsbHost by gavin beardall

Committer:
sathguru
Date:
Thu Apr 16 05:12:20 2015 +0000
Revision:
1:77e50c06ea01
Parent:
0:e6a539e59b52
Pendrive to sd card data transfer coding

Who changed what in which revision?

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