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