Use USB msd class with SD card

cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Use USB msd class with SD card

Jump to solution
7,339 Views
Kuroro
Contributor II

Hi everybody

 

I'm currently working on a project on 52259 who need to use the USB port as a mass storage device, but no to acces to ram like the example provided with MQX, but to a SD card.

 

I look around in the MQX demo but i don't see anything about that.

The Freescale PHD USB stack give a example of suck a thing but they don't work on my board.

 

So i want to know if anybody have allready try to do the same think's, and if it's possible on MQX ?

 

Best Regard

Labels (1)
Tags (1)
0 Kudos
1 Solution
4,052 Views
PetrM
Senior Contributor I

Hello,

 

such example does not exist, but it shouldn't be so hard to implement.

First, in TestApp_Init() you need to open spi and sdcard devices as in mfs/sdcard example and store file descriptor of the sdcard in some global variable or as argument for Bulk_Transaction_Callback().

Then, in Bulk_Transaction_Callback(), within each #if RAM_DISK_APP, you need to perform fread, fwrite, or IO_IOCTL_GET_BLOCK_SIZE * IO_IOCTL_GET_NUM_SECTORS on sdcard with respect to given USB request.

Note that sdcard fread/fwrite functions operate with 512B blocks only (you pass number of blocks to read/write).

 

Regards,

PetrM

 

View solution in original post

0 Kudos
20 Replies
4,048 Views
mohwaqas12
Contributor III

Hello Petr

Regarding your post on how to access SD Card on MSD USB. Could you please elebrotate on how to perform fread/fwrite on sdcard with respect to usb request.

If i use fread/fwrite , how do i set the blocks?.Do i have to use fseek to set the start block and then read the number of blocks. Does this method calls appropriate ioctl calls itself , or do  i have to use ioctl to read and transfer data from sdcard to usb msd.

Regards

0 Kudos
4,048 Views
pramodk_g_
Contributor III

Hi everyone,

                          I have done MSD onSDcard by changing the usb\device\example\msd and its working. But I think  its data transfer speed over usb is slow, current speed is around 100Kbps, is that correct? . Please reply your thoughts if we can improve speed above this.

Thanks,

Pramod

0 Kudos
4,024 Views
sudhakarp
Contributor V

Hi Pramod,

i am struggling with SD card mass storage can you share your idea. its useful to us

regards,

sudhakar p

0 Kudos
4,048 Views
loicguillain
Contributor II

Hi PRAMOD,

I am blocked on the same problem for 1 week. I would like to get my FRDM appeared as MSD storage with a SD card.

Could you share your project example?

Best regards,

Loïc GUILLAIN

0 Kudos
4,052 Views
mircopizzichini
Contributor III

Hi pramodk.g.

What have you changed in msd disk example to access SD card?

Thank you

Mirco

0 Kudos
4,052 Views
yangyu
NXP Employee
NXP Employee

Hi Pramod,

Could you share the changing with us,thanks.

Best regards,

Yu Yang

0 Kudos
4,052 Views
juanee
Contributor III

Hello

 

Did you ever get this to work or find an example?

 

Thanks,

Juan

0 Kudos
4,052 Views
juanee
Contributor III

Hello all,

 

I believe I have the same request as above.

 

I wanted to use the USB (on MCF52259) in DEVICE mode and have the SD CARD (which is attached to the MCF52259) appear as a MSD (Mass Storage Device) when connected to a PC.

 

What I need is similar to the example located at:

 

“Freescale MQX 3.6\usb\device\examples\msd\disk”

 

except the example uses a RAM DISK to appear as a MSD (Mass Storage Device) on the PC, not a SD CARD.

 

Is there an example to do this?  And if not, is it doable?

 

Replies are appreciated,

Juan

 

Codewarrior 10 Special Edition

Windows XP Pro

MQX 3.6.2

PEMicro USB ColdFire Multilink.

Custom Target based on Coldfire MCF52259.

0 Kudos
4,053 Views
PetrM
Senior Contributor I

Hello,

 

such example does not exist, but it shouldn't be so hard to implement.

First, in TestApp_Init() you need to open spi and sdcard devices as in mfs/sdcard example and store file descriptor of the sdcard in some global variable or as argument for Bulk_Transaction_Callback().

Then, in Bulk_Transaction_Callback(), within each #if RAM_DISK_APP, you need to perform fread, fwrite, or IO_IOCTL_GET_BLOCK_SIZE * IO_IOCTL_GET_NUM_SECTORS on sdcard with respect to given USB request.

Note that sdcard fread/fwrite functions operate with 512B blocks only (you pass number of blocks to read/write).

 

Regards,

PetrM

 

0 Kudos
4,052 Views
danielchai
Senior Contributor I

Hi Petr,

What is a better way to implement SD card fread/fwrite in USB stack?

Thank you.

-Daniel

0 Kudos
4,052 Views
sherine
Contributor I

I've done as you have said, but it didn't work.  Looking at my usb device library, usb_ddk_twrk40x256, I noticed that none of the msd functionality was compiling or linking because MSD_CONFIG #define was not defined.  I defined it in Properties/Tool Settings/ARM Compiler/Input/Defined Marcros : MSD_CONFIG = 1.  Now the functionality is trying to compile, but when building the library, I get an error compiling/linking the file USB Low Level Driver/callback_usbfs.obj with the error "Nothing to do: no source or object files specified"  Opening up that file, all that is in there is a look up table defined.

 

My question is this: Do I need MSD_CONFIG defined to get MSD functionality for my SD card?  And if so, is there a solution for this error?

0 Kudos
4,047 Views
Fabi
Contributor III

@sherine: With #define MSD_CONFIG 1 in usb\device\source\classes\include\usb_stack_config.h the SD card works for me. There're some user config files in MQX outside your BSP and the usb_stack_config.h belongs to it.

0 Kudos
4,047 Views
chogh
Contributor I

Hi Petr,

 

I'm working on a similar project, and I have the SD Card working, and the USB Mass Storage Device connected.  Now I'm trying to get the two to communicate to each other.  Assuming you have MFS and PartMan installed over the SD Card driver, for the fread, fwrite, and ioctl functions, do you pass the SD Card handle, or the MFS file handle?  I attached a screenshot showing proof of the sd card connected, the USB device connected, and a small portion of the read code I'm trying to get working.

 

I'm using the TWRK40x256, the TWR-SER board, an SD card using the SDHC interface, and IAR EWARM 6.21 and MQX 3.7

 

Thanks,

 

Chris

0 Kudos
4,047 Views
BryGuyH
Contributor IV

 

I have run into this question as well. I don't think they get what we are asking for. But to answer your question more succinctly:

 

No you may not run MFS with the device you intend to expose as a MSD on USB. This is because USB requires direct access to the block mode device that MFS requires access to. Because MFS doesn't have a layer for USB or a means of triggering a FAT refresh you have to make sure to close all file pointers and unmount the disk before handing the driver over to USB (you provide the SD card handle then to USB).

 

Another option is if you want to partition the disk you can use the partition manager to create two seperate partitions the same disk, one for use by MFS the other for use with USB.

0 Kudos
4,047 Views
timias
Contributor IV

My firmware writes data to the SDCard (using MFS), and then I connect to Windows through USB -using the MSD driver to upload the data into a PC.

I didn't unmount MFS, I just connected partion manager directly to the USB  (after I do a 512byte - 64 byte cache routine), because partion manager uses 512 byte blocks and USB uses 64 byte transfers.

 

I just write whatever data is passed to me, and read whatever is requested using addresses

 

*** Here is a snippet from my USB driver ********

        case USB_MSC_DEVICE_READ_REQUEST :          
            /* copy data from storage device before sending it on USB Bus
               (Called before calling send_data on BULK IN endpoints)*/
            lba_data_ptr = (PTR_LBA_APP_STRUCT)val;
            /* read data from mass storage device to driver buffer */
            SDCard_Read_Blocks(lba_data_ptr->buff_ptr,(int32_t)lba_data_ptr->size, (int32_t)lba_data_ptr->offset);
    
            break;
        case USB_MSC_DEVICE_WRITE_REQUEST :
            /* copy data from USb buffer to Storage device
               (Called before after recv_data on BULK OUT endpoints)*/
            lba_data_ptr = (PTR_LBA_APP_STRUCT)val;
            /* read data from driver buffer to mass storage device */

                SDCard_Write_Blocks(lba_data_ptr->buff_ptr,(int32_t)lba_data_ptr->size, (int32_t)lba_data_ptr->offset);
                      
            break;

 

***** Here is a snippet from the 512byte - 64 byte cache routine *******

    /* I know that the api calls for number of bytes
      but the driver expects number of blocks
     so 1 is correct not 512*/

    status =_io_read(partman_handle, sdCache.cache, 1) ;

0 Kudos
4,048 Views
danielchai
Senior Contributor I

Hi Robert,

I also want to implement msd on SD card. Could you give me more detail how did you implement SDCard_Write_Blocks, SDCard_Read_Blocks, and how does 512 bytes - 64 bytes cache work?

Thank you.

-Daniel

0 Kudos
4,048 Views
BryGuyH
Contributor IV

This is an EXTREMELY dangerous method. It only works because your application has nothing being written to disk from the device when USB is connected. The partition manager is not doing anything to insure the integrity of the filesystem. THEY WILL BE OUT OF SYNC.

0 Kudos
4,048 Views
timias
Contributor IV

After observing USB traffic, I don't believe Windows normally supports the ability for a Mass Storage device to be written too while it is conencted to the host pc with USB. It only appears to pull FAT records after intially connecting to the drive (one time), so it wouldn't matter anyways, if you were writing from a diferent channel it would get out of sync anyways.

 

Either way the processor I use is too slow to allow dual simulataneous access, and my application doesn't require it so I disabled that sort of access without bothering to test it.

 

My testing suggests it is reliable, and it is also the only way I got it to work using MQX. Windows needs the raw blocks MFS doesn't give them to you.

0 Kudos
4,048 Views
chogh
Contributor I

Thanks for the tips BryGuyH!  I think you answered my question.  Now at least I know what road to go down.  I will try providing the SD Card handle to the USB calls and let you know how it works.

0 Kudos
4,047 Views
Dekiru
Contributor IV

The USB MSD device example in USB PHDC Bare metal stack can do exactly the same that you want.

So I think you can take that as a reference to implement it in MQX...

 

Regards

0 Kudos