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