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