Merge pull request #4230 from jamike/USBHostMSD_BockDevice

Usb host msd block device
pull/3069/merge
Sam Grove 2017-05-26 17:19:07 -05:00 committed by GitHub
commit 3122abee84
3 changed files with 163 additions and 88 deletions

View File

@ -17,7 +17,6 @@
#include "USBHostMSD.h"
#if USBHOST_MSD
#include "dbg.h"
#define CBW_SIGNATURE 0x43425355
@ -29,13 +28,16 @@
#define GET_MAX_LUN (0xFE)
#define BO_MASS_STORAGE_RESET (0xFF)
USBHostMSD::USBHostMSD(const char * rootdir) : FATFileSystem(rootdir)
USBHostMSD::USBHostMSD()
{
host = USBHost::getHostInst();
init();
/* register an object in FAT */
init_usb();
}
void USBHostMSD::init() {
void USBHostMSD::init_usb()
{
dev_connected = false;
dev = NULL;
bulk_in = NULL;
@ -72,6 +74,11 @@ bool USBHostMSD::connect()
break;
if (msd_device_found) {
/* As this is done in a specific thread
* this lock is taken to avoid to process a disconnection in
* usb process during the device registering */
USBHost::Lock Lock(host);
bulk_in = dev->getEndpoint(msd_intf, BULK_ENDPOINT, IN);
bulk_out = dev->getEndpoint(msd_intf, BULK_ENDPOINT, OUT);
@ -80,14 +87,14 @@ bool USBHostMSD::connect()
USB_INFO("New MSD device: VID:%04x PID:%04x [dev: %p - intf: %d]", dev->getVid(), dev->getPid(), dev, msd_intf);
dev->setName("MSD", msd_intf);
host->registerDriver(dev, msd_intf, this, &USBHostMSD::init);
host->registerDriver(dev, msd_intf, this, &USBHostMSD::init_usb);
dev_connected = true;
return true;
}
} //if()
} //for()
init();
init_usb();
return false;
}
@ -99,9 +106,9 @@ bool USBHostMSD::connect()
/*virtual*/ bool USBHostMSD::parseInterface(uint8_t intf_nb, uint8_t intf_class, uint8_t intf_subclass, uint8_t intf_protocol) //Must return true if the interface should be parsed
{
if ((msd_intf == -1) &&
(intf_class == MSD_CLASS) &&
(intf_subclass == 0x06) &&
(intf_protocol == 0x50)) {
(intf_class == MSD_CLASS) &&
(intf_subclass == 0x06) &&
(intf_protocol == 0x50)) {
msd_intf = intf_nb;
return true;
}
@ -122,13 +129,15 @@ bool USBHostMSD::connect()
}
int USBHostMSD::testUnitReady() {
int USBHostMSD::testUnitReady()
{
USB_DBG("Test unit ready");
return SCSITransfer(NULL, 6, DEVICE_TO_HOST, 0, 0);
}
int USBHostMSD::readCapacity() {
int USBHostMSD::readCapacity()
{
USB_DBG("Read capacity");
uint8_t cmd[10] = {0x25,0,0,0,0,0,0,0,0,0};
uint8_t result[8];
@ -142,7 +151,8 @@ int USBHostMSD::readCapacity() {
}
int USBHostMSD::SCSIRequestSense() {
int USBHostMSD::SCSIRequestSense()
{
USB_DBG("Request sense");
uint8_t cmd[6] = {0x03,0,0,0,18,0};
uint8_t result[18];
@ -151,7 +161,8 @@ int USBHostMSD::SCSIRequestSense() {
}
int USBHostMSD::inquiry(uint8_t lun, uint8_t page_code) {
int USBHostMSD::inquiry(uint8_t lun, uint8_t page_code)
{
USB_DBG("Inquiry");
uint8_t evpd = (page_code == 0) ? 0 : 1;
uint8_t cmd[6] = {0x12, uint8_t((lun << 5) | evpd), page_code, 0, 36, 0};
@ -174,7 +185,8 @@ int USBHostMSD::inquiry(uint8_t lun, uint8_t page_code) {
return status;
}
int USBHostMSD::checkResult(uint8_t res, USBEndpoint * ep) {
int USBHostMSD::checkResult(uint8_t res, USBEndpoint * ep)
{
// if ep stalled: send clear feature
if (res == USB_TYPE_STALL_ERROR) {
res = host->controlWrite( dev,
@ -194,7 +206,8 @@ int USBHostMSD::checkResult(uint8_t res, USBEndpoint * ep) {
}
int USBHostMSD::SCSITransfer(uint8_t * cmd, uint8_t cmd_len, int flags, uint8_t * data, uint32_t transfer_len) {
int USBHostMSD::SCSITransfer(uint8_t * cmd, uint8_t cmd_len, int flags, uint8_t * data, uint32_t transfer_len)
{
int res = 0;
@ -277,7 +290,8 @@ int USBHostMSD::SCSITransfer(uint8_t * cmd, uint8_t cmd_len, int flags, uint8_t
}
int USBHostMSD::dataTransfer(uint8_t * buf, uint32_t block, uint8_t nbBlock, int direction) {
int USBHostMSD::dataTransfer(uint8_t * buf, uint32_t block, uint8_t nbBlock, int direction)
{
uint8_t cmd[10];
memset(cmd,0,10);
cmd[0] = (direction == DEVICE_TO_HOST) ? 0x28 : 0x2A;
@ -293,7 +307,8 @@ int USBHostMSD::dataTransfer(uint8_t * buf, uint32_t block, uint8_t nbBlock, int
return SCSITransfer(cmd, 10, direction, buf, blockSize*nbBlock);
}
int USBHostMSD::getMaxLun() {
int USBHostMSD::getMaxLun()
{
uint8_t buf[1], res;
res = host->controlRead( dev, USB_RECIPIENT_INTERFACE | USB_DEVICE_TO_HOST | USB_REQUEST_TYPE_CLASS,
0xfe, 0, msd_intf, buf, 1);
@ -301,12 +316,11 @@ int USBHostMSD::getMaxLun() {
return res;
}
int USBHostMSD::disk_initialize() {
int USBHostMSD::init()
{
USB_DBG("FILESYSTEM: init");
uint16_t i, timeout = 10;
uint16_t i, timeout = 10, ret;
getMaxLun();
for (i = 0; i < timeout; i++) {
Thread::wait(100);
if (!testUnitReady())
@ -323,44 +337,70 @@ int USBHostMSD::disk_initialize() {
return readCapacity();
}
int USBHostMSD::disk_write(const uint8_t* buffer, uint32_t block_number, uint32_t count) {
USB_DBG("FILESYSTEM: write block: %lld, count: %d", block_number, count);
int USBHostMSD::program(const void *buffer, bd_addr_t addr, bd_size_t size)
{
uint32_t block_number, count;
uint8_t *buf = (uint8_t *)buffer;
if (!disk_init) {
disk_initialize();
init();
}
if (!disk_init)
if (!disk_init) {
return -1;
}
block_number = addr / blockSize;
count = size /blockSize;
for (uint32_t b = block_number; b < block_number + count; b++) {
if (dataTransfer((uint8_t*)buffer, b, 1, HOST_TO_DEVICE))
if (dataTransfer(buf, b, 1, HOST_TO_DEVICE))
return -1;
buffer += 512;
buf += blockSize;
}
return 0;
}
int USBHostMSD::disk_read(uint8_t* buffer, uint32_t block_number, uint32_t count) {
USB_DBG("FILESYSTEM: read block: %lld, count: %d", block_number, count);
int USBHostMSD::read(void *buffer, bd_addr_t addr, bd_size_t size)
{
uint32_t block_number, count;
uint8_t *buf = (uint8_t *)buffer;
if (!disk_init) {
disk_initialize();
init();
}
if (!disk_init)
if (!disk_init) {
return -1;
}
block_number = addr / blockSize;
count = size / blockSize;
for (uint32_t b = block_number; b < block_number + count; b++) {
if (dataTransfer((uint8_t*)buffer, b, 1, DEVICE_TO_HOST))
if (dataTransfer(buf, b, 1, DEVICE_TO_HOST))
return -1;
buffer += 512;
buf += blockSize;
}
return 0;
}
uint32_t USBHostMSD::disk_sectors() {
USB_DBG("FILESYSTEM: sectors");
if (!disk_init) {
disk_initialize();
}
if (!disk_init)
return 0;
return blockCount;
int USBHostMSD::erase(bd_addr_t addr, bd_size_t size)
{
return 0;
}
bd_size_t USBHostMSD::get_read_size() const
{
return (disk_init ? (bd_size_t)blockSize : -1);
}
bd_size_t USBHostMSD::get_program_size() const
{
return (disk_init ? (bd_size_t)blockSize : -1);
}
bd_size_t USBHostMSD::get_erase_size() const
{
return (disk_init ? (bd_size_t)blockSize : -1);
}
bd_size_t USBHostMSD::size() const
{
USB_DBG("FILESYSTEM: size ");
return (disk_init ? (bd_size_t)blockSize : 0);
}
#endif

View File

@ -23,24 +23,26 @@
#include "USBHost.h"
#include "FATFileSystem.h"
#include "BlockDevice.h"
/**
* A class to communicate a USB flash disk
*/
class USBHostMSD : public IUSBEnumerator, public FATFileSystem {
class USBHostMSD : public IUSBEnumerator, public BlockDevice
{
public:
/**
* Constructor
*
* @param rootdir mount name
*/
USBHostMSD(const char * rootdir);
* Constructor
*
* @param rootdir mount name
*/
USBHostMSD();
/**
* Check if a MSD device is connected
*
* @return true if a MSD device is connected
*/
* Check if a MSD device is connected
*
* @return true if a MSD device is connected
*/
bool connected();
/**
@ -49,6 +51,20 @@ public:
* @return true if connection was successful
*/
bool connect();
virtual int init();
virtual int deinit()
{
return BD_ERROR_OK;
};
virtual int read(void *buffer, bd_addr_t addr, bd_size_t size);
virtual int program(const void *buffer, bd_addr_t addr, bd_size_t size);
virtual int erase(bd_addr_t addr, bd_size_t size);
virtual bd_size_t get_read_size() const;
virtual bd_size_t get_program_size() const;
virtual bd_size_t get_erase_size() const;
virtual bd_size_t size() const;
protected:
//From IUSBEnumerator
@ -56,13 +72,6 @@ protected:
virtual bool parseInterface(uint8_t intf_nb, uint8_t intf_class, uint8_t intf_subclass, uint8_t intf_protocol); //Must return true if the interface should be parsed
virtual bool useEndpoint(uint8_t intf_nb, ENDPOINT_TYPE type, ENDPOINT_DIRECTION dir); //Must return true if the endpoint will be used
// From FATFileSystem
virtual int disk_initialize();
virtual int disk_status() {return 0;};
virtual int disk_read(uint8_t* buffer, uint32_t sector, uint32_t count);
virtual int disk_write(const uint8_t* buffer, uint32_t sector, uint32_t count);
virtual int disk_sync() {return 0;};
virtual uint32_t disk_sectors();
private:
USBHost * host;
@ -110,7 +119,7 @@ private:
bool msd_device_found;
bool disk_init;
void init();
void init_usb();
};

View File

@ -1,42 +1,68 @@
#include "mbed.h"
#include "USBHostMSD.h"
DigitalOut led(LED1);
void msd_task(void const *) {
printf("init msd\n");
USBHostMSD msd("usb");
int i = 0;
printf("wait for usb memory stick insertion\n");
while(1) {
#include "FATFileSystem.h"
#include <stdlib.h>
// try to connect a MSD device
while(!msd.connect()) {
Thread::wait(500);
}
// in a loop, append a file
// if the device is disconnected, we try to connect it again
void msd_task(void const *)
{
// append a file
FILE * fp = fopen("/usb/test1.txt", "a");
USBHostMSD msd;
int i = 0;
FATFileSystem fs("usb");
int err;
printf("wait for usb memory stick insertion\n");
while(1) {
if (fp != NULL) {
fprintf(fp, "Hello fun SD Card World: %d!\r\n", i++);
printf("Goodbye World!\r\n");
fclose(fp);
} else {
printf("FILE == NULL\r\n");
}
Thread::wait(500);
printf("again\n");
// if device disconnected, try to connect again
while (msd.connected()) {
Thread::wait(500);
}
}
// try to connect a MSD device
while(!msd.connect()) {
Thread::wait(500);
}
if (fs.mount(&msd) != 0) {
continue;
} else {
printf("file system mounted\n");
}
if (!msd.connect()) {
continue;
}
// in a loop, append a file
// if the device is disconnected, we try to connect it again
// append a file
File file;
err = file.open(&fs, "test1.txt", O_WRONLY | O_CREAT |O_APPEND);
if (err == 0) {
char tmp[100];
sprintf(tmp,"Hello fun USB stick World: %d!\r\n", i++);
file.write(tmp,strlen(tmp));
sprintf(tmp,"Goodbye World!\r\n");
file.write(tmp,strlen(tmp));
file.close();
} else {
printf("FILE == NULL\r\n");
}
Thread::wait(500);
printf("again\n");
// if device disconnected, try to connect again
while (msd.connected()) {
Thread::wait(500);
}
while (fs.unmount() < 0) {
Thread::wait(500);
printf("unmount\n");
}
}
}
int main() {
int main()
{
Thread msdTask(msd_task, NULL, osPriorityNormal, 1024 * 4);
while(1) {
led=!led;
Thread::wait(500);