USBMSD example using a file system located in RAM

Dependents:   USBMSD_RAM

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers USBMSD_Ram.cpp Source File

USBMSD_Ram.cpp

00001 /* mbed USBMSD_Ram Library
00002  *
00003  * Permission is hereby granted, free of charge, to any person obtaining a copy
00004  * of this software and associated documentation files (the "Software"), to deal
00005  * in the Software without restriction, including without limitation the rights
00006  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
00007  * copies of the Software, and to permit persons to whom the Software is
00008  * furnished to do so, subject to the following conditions:
00009  *
00010  * The above copyright notice and this permission notice shall be included in
00011  * all copies or substantial portions of the Software.
00012  *
00013  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
00014  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
00015  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
00016  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
00017  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
00018  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
00019  * THE SOFTWARE.
00020  */
00021 
00022 #include "USBMSD_Ram.h"
00023 
00024 typedef struct sector {
00025     const char * sect;
00026     unsigned int length;
00027 } SECTOR;
00028 
00029 #define LSByte(x) (x & 0xff)
00030 #define MSByte(x) ((x >> 8) & 0xff)
00031 
00032 const char mbr[] = {
00033     //sector 0: boot sector
00034     0xEB,0x3C,0x90,0x4D,0x53,0x44,0x4F,0x53,0x35,0x2E,0x30,    0x00,0x02,/*bytes per sector: 512*/   0x01, /*sectors per cluster*/ 0x01,0x00,/*number of reserved sector*/
00035     0x01, /* number of FATs*/ 0x10,0x00, /*Maximum number of root directory entries: 16*/ LSByte(NB_SECTORS),MSByte(NB_SECTORS), /*Total sector count*/ 0xF0,  0x01,0x00,/*sectors per FAT: 1*/  0x01,0x00,/*Sectors per track*/  0x01,0x00, /*Number of heads*/  0x00,0x00,0x00,0x00,
00036     0x00,0x01,0x00,0x00,0x00,0x00,  0x29, /*boot signature*/ 0x74,0x19,0x02,0x27, /*volume ID*/ 'M','b','e','d',' ',
00037     'U','S','B',' ',' ',' ', /*volume label: Mbed USB*/     0x46,0x41,0x54,0x31,0x32,0x20,0x20,0x20,0x33,0xC9,
00038     0x8E,0xD1,0xBC,0xF0,0x7B,0x8E,0xD9,0xB8,0x00,0x20,0x8E,0xC0,0xFC,0xBD,0x00,0x7C,
00039     0x38,0x4E,0x24,0x7D,0x24,0x8B,0xC1,0x99,0xE8,0x3C,0x01,0x72,0x1C,0x83,0xEB,0x3A,
00040     0x66,0xA1,0x1C,0x7C,0x26,0x66,0x3B,0x07,0x26,0x8A,0x57,0xFC,0x75,0x06,0x80,0xCA,
00041     0x02,0x88,0x56,0x02,0x80,0xC3,0x10,0x73,0xEB,0x33,0xC9,0x8A,0x46,0x10,0x98,0xF7,
00042     0x66,0x16,0x03,0x46,0x1C,0x13,0x56,0x1E,0x03,0x46,0x0E,0x13,0xD1,0x8B,0x76,0x11,
00043     0x60,0x89,0x46,0xFC,0x89,0x56,0xFE,0xB8,0x20,0x00,0xF7,0xE6,0x8B,0x5E,0x0B,0x03,
00044     0xC3,0x48,0xF7,0xF3,0x01,0x46,0xFC,0x11,0x4E,0xFE,0x61,0xBF,0x00,0x00,0xE8,0xE6,
00045     0x00,0x72,0x39,0x26,0x38,0x2D,0x74,0x17,0x60,0xB1,0x0B,0xBE,0xA1,0x7D,0xF3,0xA6,
00046     0x61,0x74,0x32,0x4E,0x74,0x09,0x83,0xC7,0x20,0x3B,0xFB,0x72,0xE6,0xEB,0xDC,0xA0,
00047     0xFB,0x7D,0xB4,0x7D,0x8B,0xF0,0xAC,0x98,0x40,0x74,0x0C,0x48,0x74,0x13,0xB4,0x0E,
00048     0xBB,0x07,0x00,0xCD,0x10,0xEB,0xEF,0xA0,0xFD,0x7D,0xEB,0xE6,0xA0,0xFC,0x7D,0xEB,
00049     0xE1,0xCD,0x16,0xCD,0x19,0x26,0x8B,0x55,0x1A,0x52,0xB0,0x01,0xBB,0x00,0x00,0xE8,
00050     0x3B,0x00,0x72,0xE8,0x5B,0x8A,0x56,0x24,0xBE,0x0B,0x7C,0x8B,0xFC,0xC7,0x46,0xF0,
00051     0x3D,0x7D,0xC7,0x46,0xF4,0x29,0x7D,0x8C,0xD9,0x89,0x4E,0xF2,0x89,0x4E,0xF6,0xC6,
00052     0x06,0x96,0x7D,0xCB,0xEA,0x03,0x00,0x00,0x20,0x0F,0xB6,0xC8,0x66,0x8B,0x46,0xF8,
00053     0x66,0x03,0x46,0x1C,0x66,0x8B,0xD0,0x66,0xC1,0xEA,0x10,0xEB,0x5E,0x0F,0xB6,0xC8,
00054     0x4A,0x4A,0x8A,0x46,0x0D,0x32,0xE4,0xF7,0xE2,0x03,0x46,0xFC,0x13,0x56,0xFE,0xEB,
00055     0x4A,0x52,0x50,0x06,0x53,0x6A,0x01,0x6A,0x10,0x91,0x8B,0x46,0x18,0x96,0x92,0x33,
00056     0xD2,0xF7,0xF6,0x91,0xF7,0xF6,0x42,0x87,0xCA,0xF7,0x76,0x1A,0x8A,0xF2,0x8A,0xE8,
00057     0xC0,0xCC,0x02,0x0A,0xCC,0xB8,0x01,0x02,0x80,0x7E,0x02,0x0E,0x75,0x04,0xB4,0x42,
00058     0x8B,0xF4,0x8A,0x56,0x24,0xCD,0x13,0x61,0x61,0x72,0x0B,0x40,0x75,0x01,0x42,0x03,
00059     0x5E,0x0B,0x49,0x75,0x06,0xF8,0xC3,0x41,0xBB,0x00,0x00,0x60,0x66,0x6A,0x00,0xEB,
00060     0xB0,0x4E,0x54,0x4C,0x44,0x52,0x20,0x20,0x20,0x20,0x20,0x20,0x0D,0x0A,0x52,0x65,
00061     0x6D,0x6F,0x76,0x65,0x20,0x64,0x69,0x73,0x6B,0x73,0x20,0x6F,0x72,0x20,0x6F,0x74,
00062     0x68,0x65,0x72,0x20,0x6D,0x65,0x64,0x69,0x61,0x2E,0xFF,0x0D,0x0A,0x44,0x69,0x73,
00063     0x6B,0x20,0x65,0x72,0x72,0x6F,0x72,0xFF,0x0D,0x0A,0x50,0x72,0x65,0x73,0x73,0x20,
00064     0x61,0x6E,0x79,0x20,0x6B,0x65,0x79,0x20,0x74,0x6F,0x20,0x72,0x65,0x73,0x74,0x61,
00065     0x72,0x74,0x0D,0x0A,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0xAC,0xCB,0xD8,0x55,0xAA,
00066 };
00067 
00068 const char fat[] = { 0xF8,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0x0F };
00069 
00070 const char root_dir[] = {
00071     //sector 3: root directory
00072     //entry 1
00073     'M','b','e','d',' ','U','S','B',' ',' ',' ',   0x28,0x00,0x00,0x00,0x00,
00074     0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
00075     //entry 2
00076     'R','E','A','D','M','E',0x20,0x20,'T','X','T', /*readme.txt*/ 0x20,0x00,0x00,0x00,0x00,
00077     0x21,0x00,0xBB,0x32,0x00,0x00,   0xDC,0x83,/*hour/min/doublesec*/  0x6A,0x3F,/*year/month/day*/  0x02,0x00, /*starting cluster*/ 0x1e,0x00,0x00,0x00, /*size*/
00078 };
00079 
00080 const char data[] = {
00081     //sector 4: data
00082     'H','e','l','l','o',' ','f','r','o','m',' ','M','b','e','d','!',
00083     '\r','\n','U','S','B',' ','M','S','D',' ','d','e','m','o',0x00,0x00
00084 };
00085 
00086 //3 sectors
00087 SECTOR sectors[4] = {
00088     {mbr , 512},
00089     {fat, 8},
00090     {root_dir, 4*16},
00091 
00092     // contains readme.txt
00093     {data, 2*16}
00094 };
00095 
00096 USBMSD_Ram::USBMSD_Ram()  {
00097     //no init
00098     _status = 0x01;
00099     memset(disk_image, 0, 512*4);
00100     for (uint8_t i = 0; i < 4; i++) {
00101         memcpy(disk_image + 512*i, sectors[i].sect, sectors[i].length);
00102     }
00103     connect();
00104 }
00105 
00106 int USBMSD_Ram::disk_initialize() {
00107     // OK
00108     _status = 0x00;
00109     return 0;
00110 }
00111 
00112 int USBMSD_Ram::disk_write(const uint8_t * buffer, uint64_t block_number) { 
00113     memcpy(disk_image + block_number*512, buffer, 512);
00114     return 0;    
00115 }
00116 
00117 int USBMSD_Ram::disk_read(uint8_t * buffer, uint64_t block_number) {
00118     memcpy(buffer, disk_image + block_number*512, 512);
00119     return 0;
00120 }
00121 
00122 int USBMSD_Ram::disk_status() { return _status; }
00123 int USBMSD_Ram::disk_sync() { return 0; }
00124 uint64_t USBMSD_Ram::disk_sectors() { return NB_SECTORS; }
00125 uint64_t USBMSD_Ram::disk_size() { return NB_SECTORS*512;}