Writing data to and from a USB stick

Dependencies:   mbed

Committer:
ms523
Date:
Sun Nov 06 15:45:12 2011 +0000
Revision:
0:5934350323b2
Working with a USB stick

Who changed what in which revision?

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