How does USB work in K60?

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

How does USB work in K60?

1,378 Views
danielchai
Senior Contributor I

Hi All,

I read that there is USB stack. I read about it but still a little bit confused.

And I knew USB stack is not a OS. I am wondering how this work if I want to implement MQX on my application?

Thank you.

-Daniel

0 Kudos
17 Replies

1,043 Views
ZhangJennie
NXP TechSupport
NXP TechSupport
0 Kudos

1,043 Views
danielchai
Senior Contributor I

Hi Jun,

Thank you.

I found a USB demo code msd_usb under /freescale/Freescale_MQX_4_0/usb/example/. But this example is a USB Mass Storage based on RAM.

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 */
    #if RAM_DISK_APP
    USB_mem_copy(g_disk.storage_disk + lba_data_ptr->offset,lba_data_ptr->buff_ptr, lba_data_ptr->size);
    #endif        
    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 */
    #if RAM_DISK_APP
    USB_mem_copy(lba_data_ptr->buff_ptr,g_disk.storage_disk + lba_data_ptr->offset,lba_data_ptr->size);
    #endif                         
    break;

I want to implement it msd on a SD card. I read SD Card Driver has _io_read() and _io_write() function. My question is how could I use these functions here to implement msd on SD card rather than RAM? Or do I need call the low level driver function to achieve this?

Thank you.

-Daniel

0 Kudos

1,043 Views
ZhangJennie
NXP TechSupport
NXP TechSupport
0 Kudos

1,043 Views
ZhangJennie
NXP TechSupport
NXP TechSupport
0 Kudos

1,043 Views
danielchai
Senior Contributor I

Hi Jun,

Thank you for your reply.

I read the Erich blog, but I need my USB as a device and write file to a SD card on K60N512 tower.

I read a post  https://community.freescale.com/message/81750#81750

PetrM said it is possible to use fwrite and fread to copy the data from USB buffer to SD card. My question is that because USB is based on 64 bytes read and write but SD card is based on 512 bytes, how can I deal with this issue?

Thank you again.

-Daniel

0 Kudos

1,043 Views
BlackNight
NXP Employee
NXP Employee

Hi Daniel,

the FatFS is using internally a block size of 512 bytes. But this does not mean that you need 512 bytes to read/write data. You can write a single byte if you want. It is just that it performs an internal caching on 512 bytes, that's it. So if you get 64 bytes from the USB, write that 64 bytes to the FatFs, and it will care internally about this.

If you need to end up that 64 bytes on the SD card right away (without buffering), then call sync to flush the buffer.

I hope this helps.

Erich

0 Kudos

1,043 Views
danielchai
Senior Contributor I

Hi Erich,

Do you mean that when I get a USB_MSC_DEVICE_WRITE_REQUEST  from USB, I can directly copy it from USB buffer to SD card using fwrite?


Thank you.

-Daniel

0 Kudos

1,043 Views
BlackNight
NXP Employee
NXP Employee

Hi Daniel,

yes (unless I miss something?).

Erich

0 Kudos

1,042 Views
danielchai
Senior Contributor I

Hi Erich,

Now I did edit the code and recompile the project. But when I  plug it in the PC to format it, it can not format it.

Is there some part that I need to change for format USB MSD?

Thank you.

-Daniel

0 Kudos

1,043 Views
BlackNight
NXP Employee
NXP Employee

Hi Daniel,

normal USB memory sticks work for me, and I can properly format them.

You might have a look here for additional details on FAT:

FatFs - Generic FAT File System Module

0 Kudos

1,043 Views
danielchai
Senior Contributor I

Hi Erich,

I have a 2GB SD card. Is it because it cannot support 2GB?

-Daniel

0 Kudos

1,043 Views
BlackNight
NXP Employee
NXP Employee

I'm using FatFS with SD cards of any size, up to 8 GB.

But I had them formatted on the host, not on the device. FatFs has a format method, but I believe I have not used it.

Erich

0 Kudos

1,043 Views
danielchai
Senior Contributor I

Hi Erich,

For the msd_usb demo project, every time when I connect the tower board to my PC, it will ask me to format it. I don't know how should I do to make it not ask for format every time.

Thank you.

-Daniel

0 Kudos

1,043 Views
danielchai
Senior Contributor I

Hi Erich,

When I just use RAM, then the formatting works very well.

But when I edit it for using SD card, the formatting does not work.

Then I am wondering if I miss something.

In msd_usb demo project, I create a sdcard_Init() function to install the MFS on the SD card, after that how can the USB MSD know which one I am using SD or RAM? Do I need to set something for USB MSD handle to using SD card handle? If needed I can post the code here.

Thank you very much.

-Daniel

0 Kudos

1,043 Views
danielchai
Senior Contributor I

Hi Erich,

I will try it and let you know.

Thank you.

-Daniel

0 Kudos

1,043 Views
Monica
Senior Contributor III

Hello Daniel,

were you able to find those around? Please keep us posted! :smileywink:

Best regards,

Monica.

0 Kudos

1,043 Views
LuisCasado
NXP Employee
NXP Employee

Hi,

USB stack is already integrated in MQX distribution. Check MQX, in USB folder, you will find USB host and device examples.

Luis

0 Kudos