Based on myBlueUSB and rosserial_mbed

Dependencies:   mbed myUSBHost AvailableMemory myBlueUSB

Committer:
OTL
Date:
Sat Sep 17 14:24:13 2011 +0000
Revision:
1:18139954944b
Parent:
0:7684b95768c7
remove m3pi and main

Who changed what in which revision?

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