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