Dependencies:   mbed

Committer:
abe00makoto
Date:
Thu May 26 19:39:37 2011 +0000
Revision:
1:237cfff63ef8
Parent:
0:e939856c1939

        

Who changed what in which revision?

UserRevisionLine numberNew contents of line
abe00makoto 0:e939856c1939 1 /*
abe00makoto 0:e939856c1939 2 Copyright (c) 2010 Peter Barrett
abe00makoto 0:e939856c1939 3
abe00makoto 0:e939856c1939 4 Permission is hereby granted, free of charge, to any person obtaining a copy
abe00makoto 0:e939856c1939 5 of this software and associated documentation files (the "Software"), to deal
abe00makoto 0:e939856c1939 6 in the Software without restriction, including without limitation the rights
abe00makoto 0:e939856c1939 7 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
abe00makoto 0:e939856c1939 8 copies of the Software, and to permit persons to whom the Software is
abe00makoto 0:e939856c1939 9 furnished to do so, subject to the following conditions:
abe00makoto 0:e939856c1939 10
abe00makoto 0:e939856c1939 11 The above copyright notice and this permission notice shall be included in
abe00makoto 0:e939856c1939 12 all copies or substantial portions of the Software.
abe00makoto 0:e939856c1939 13
abe00makoto 0:e939856c1939 14 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
abe00makoto 0:e939856c1939 15 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
abe00makoto 0:e939856c1939 16 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
abe00makoto 0:e939856c1939 17 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
abe00makoto 0:e939856c1939 18 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
abe00makoto 0:e939856c1939 19 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
abe00makoto 0:e939856c1939 20 THE SOFTWARE.
abe00makoto 0:e939856c1939 21 */
abe00makoto 0:e939856c1939 22
abe00makoto 0:e939856c1939 23 #include "mbed.h"
abe00makoto 0:e939856c1939 24 #include "USBHost.h"
abe00makoto 0:e939856c1939 25 #include "Utils.h"
abe00makoto 0:e939856c1939 26 #include "FATFileSystem.h"
abe00makoto 0:e939856c1939 27
abe00makoto 0:e939856c1939 28 int MassStorage_ReadCapacity(int device, u32* blockCount, u32* blockSize);
abe00makoto 0:e939856c1939 29 int MassStorage_Read(int device, u32 blockAddr, u32 blockCount, u8* dst, u32 blockSize);
abe00makoto 0:e939856c1939 30 int MassStorage_Write(int device, u32 blockAddr, u32 blockCount, u8* dst, u32 blockSize);
abe00makoto 0:e939856c1939 31
abe00makoto 0:e939856c1939 32 class USBFileSystem : public FATFileSystem
abe00makoto 0:e939856c1939 33 {
abe00makoto 0:e939856c1939 34 int _device;
abe00makoto 0:e939856c1939 35 u32 _blockSize;
abe00makoto 0:e939856c1939 36 u32 _blockCount;
abe00makoto 0:e939856c1939 37
abe00makoto 0:e939856c1939 38 public:
abe00makoto 0:e939856c1939 39 USBFileSystem() : FATFileSystem("usb"),_device(0),_blockSize(0),_blockCount(0)
abe00makoto 0:e939856c1939 40 {
abe00makoto 0:e939856c1939 41 }
abe00makoto 0:e939856c1939 42
abe00makoto 0:e939856c1939 43 void SetDevice(int device)
abe00makoto 0:e939856c1939 44 {
abe00makoto 0:e939856c1939 45 _device = device;
abe00makoto 0:e939856c1939 46 }
abe00makoto 0:e939856c1939 47
abe00makoto 0:e939856c1939 48 virtual int disk_initialize()
abe00makoto 0:e939856c1939 49 {
abe00makoto 0:e939856c1939 50 return MassStorage_ReadCapacity(_device,&_blockCount,&_blockSize);
abe00makoto 0:e939856c1939 51 }
abe00makoto 0:e939856c1939 52
abe00makoto 0:e939856c1939 53 virtual int disk_write(const char *buffer, int block_number)
abe00makoto 0:e939856c1939 54 {
abe00makoto 0:e939856c1939 55 return MassStorage_Write(_device,block_number,1,(u8*)buffer,_blockSize);
abe00makoto 0:e939856c1939 56 }
abe00makoto 0:e939856c1939 57
abe00makoto 0:e939856c1939 58 virtual int disk_read(char *buffer, int block_number)
abe00makoto 0:e939856c1939 59 {
abe00makoto 0:e939856c1939 60 return MassStorage_Read(_device,block_number,1,(u8*)buffer,_blockSize);
abe00makoto 0:e939856c1939 61 }
abe00makoto 0:e939856c1939 62
abe00makoto 0:e939856c1939 63 virtual int disk_sectors()
abe00makoto 0:e939856c1939 64 {
abe00makoto 0:e939856c1939 65 return _blockCount;
abe00makoto 0:e939856c1939 66 }
abe00makoto 0:e939856c1939 67 };
abe00makoto 0:e939856c1939 68
abe00makoto 0:e939856c1939 69 void DumpFS(int depth, int count)
abe00makoto 0:e939856c1939 70 {
abe00makoto 0:e939856c1939 71 DIR *d = opendir("/usb");
abe00makoto 0:e939856c1939 72 if (!d)
abe00makoto 0:e939856c1939 73 {
abe00makoto 0:e939856c1939 74 printf("USB file system borked\r\n");
abe00makoto 0:e939856c1939 75 return;
abe00makoto 0:e939856c1939 76 }
abe00makoto 0:e939856c1939 77
abe00makoto 0:e939856c1939 78 printf("\nDumping root dir\r\n");
abe00makoto 0:e939856c1939 79 struct dirent *p;
abe00makoto 0:e939856c1939 80 for(;;)
abe00makoto 0:e939856c1939 81 {
abe00makoto 0:e939856c1939 82 p = readdir(d);
abe00makoto 0:e939856c1939 83 if (!p)
abe00makoto 0:e939856c1939 84 break;
abe00makoto 0:e939856c1939 85 int len = sizeof( dirent);
abe00makoto 0:e939856c1939 86 printf("%s %d\n", p->d_name, len);
abe00makoto 0:e939856c1939 87 }
abe00makoto 0:e939856c1939 88 closedir(d);
abe00makoto 0:e939856c1939 89 }
abe00makoto 0:e939856c1939 90
abe00makoto 0:e939856c1939 91 int OnDiskInsert(int device)
abe00makoto 0:e939856c1939 92 {
abe00makoto 0:e939856c1939 93 USBFileSystem fs;
abe00makoto 0:e939856c1939 94 fs.SetDevice(device);
abe00makoto 0:e939856c1939 95 DumpFS(0,0);
abe00makoto 0:e939856c1939 96 return 0;
abe00makoto 0:e939856c1939 97 }
abe00makoto 0:e939856c1939 98
abe00makoto 0:e939856c1939 99 /*
abe00makoto 0:e939856c1939 100 Simple test shell to exercise mouse,keyboard,mass storage and hubs.
abe00makoto 0:e939856c1939 101 Add 2 15k pulldown resistors between D+/D- and ground, attach a usb socket and have at it.
abe00makoto 0:e939856c1939 102 */
abe00makoto 0:e939856c1939 103
abe00makoto 0:e939856c1939 104 Serial pc(USBTX, USBRX);
abe00makoto 0:e939856c1939 105 int GetConsoleChar()
abe00makoto 0:e939856c1939 106 {
abe00makoto 0:e939856c1939 107 if (!pc.readable())
abe00makoto 0:e939856c1939 108 return -1;
abe00makoto 0:e939856c1939 109 char c = pc.getc();
abe00makoto 0:e939856c1939 110 pc.putc(c); // echo
abe00makoto 0:e939856c1939 111 return c;
abe00makoto 0:e939856c1939 112 }
abe00makoto 0:e939856c1939 113
abe00makoto 0:e939856c1939 114 void TestShell();
abe00makoto 0:e939856c1939 115
abe00makoto 0:e939856c1939 116 int main()
abe00makoto 0:e939856c1939 117 {
abe00makoto 0:e939856c1939 118 pc.baud(9600);
abe00makoto 0:e939856c1939 119 printf("BlueUSB\nNow get a bunch of usb or bluetooth things and plug them in\r\n");
abe00makoto 0:e939856c1939 120 TestShell();
abe00makoto 0:e939856c1939 121 }