EmbeddedRelated.com
Forums

USB Mass Storage Device refresh

Started by Jakub Szumacher August 17, 2010
Hello,

I've made Mass Storage Device (MSD) on LPC214x (using Keil code to usb-bootloader). Practically files on MSD are some sectors of RAM (so thay are small - but it is not a problem).

Problem is that windows opens any file once.

For example: Windows open file "example.txt" and show contents "Hello world!". This text is practically in RAM and it is OK. But if I change content of file - changing RAM values (without disconnecting usb), Windows still see "Hello world!". It seems that Windows read file once - not every time it is opened, but only at first time. So I'm trying close and open file again but Windows still see the same first text.

Is it any method to make Windows to refresh/reload files (or usb device)?

Maybe it is depends on usb device - usb device can make usb host (PC) to refresh/reload?
best regards
Jakub

An Engineer's Guide to the LPC2100 Series

----Original Message----
From: l...
[mailto:l...] On Behalf Of Jakub
Szumacher Sent: 17 August 2008 07:59 To:
l... Subject: [lpc2000] USB Mass Storage
Device refresh

> Hello,
>
> I've made Mass Storage Device (MSD) on LPC214x (using
> Keil code to usb-bootloader). Practically files on MSD
> are some sectors of RAM (so thay are small - but it is
> not a problem).
>
> Problem is that windows opens any file once.
>
> For example: Windows open file "example.txt" and show
> contents "Hello world!". This text is practically in RAM
> and it is OK. But if I change content of file - changing
> RAM values (without disconnecting usb), Windows still see
> "Hello world!". It seems that Windows read file once -
> not every time it is opened, but only at first time. So
> I'm trying close and open file again but Windows still
> see the same first text.
>
> Is it any method to make Windows to refresh/reload files
> (or usb device)?
>
> Maybe it is depends on usb device - usb device can make
> usb host (PC) to refresh/reload?

This is true, windows caches the file.

Your device cannot safely change the file while connected as mass storage device, as you don't know what Windows is doing with it. You have to give full control over to the USB code and do nothing else. Once the USB is disconnected you can then safely change the file.

This is why digital cameras etc go into a sort of "dumb slave" mode when USB is connected to them.
--
Tim Mitchell

Hi,

Followink link says you can disable cacheing in your software.

http://msdn.microsoft.com/en-us/library/aa365199%28v=VS.85%29.aspx
Gurkan

To: l...
From: t...@sabretechnology.co.uk
Date: Tue, 17 Aug 2010 08:57:19 +0100
Subject: RE: [lpc2000] USB Mass Storage Device refresh

----Original Message----

From: l...

[mailto:l...] On Behalf Of Jakub

Szumacher Sent: 17 August 2008 07:59 To:

l... Subject: [lpc2000] USB Mass Storage

Device refresh

> Hello,

>

> I've made Mass Storage Device (MSD) on LPC214x (using

> Keil code to usb-bootloader). Practically files on MSD

> are some sectors of RAM (so thay are small - but it is

> not a problem).

>

> Problem is that windows opens any file once.

>

> For example: Windows open file "example.txt" and show

> contents "Hello world!". This text is practically in RAM

> and it is OK. But if I change content of file - changing

> RAM values (without disconnecting usb), Windows still see

> "Hello world!". It seems that Windows read file once -

> not every time it is opened, but only at first time. So

> I'm trying close and open file again but Windows still

> see the same first text.

>

> Is it any method to make Windows to refresh/reload files

> (or usb device)?

>

> Maybe it is depends on usb device - usb device can make

> usb host (PC) to refresh/reload?

This is true, windows caches the file.

Your device cannot safely change the file while connected as mass storage device, as you don't know what Windows is doing with it. You have to give full control over to the USB code and do nothing else. Once the USB is disconnected you can then safely change the file.

This is why digital cameras etc go into a sort of "dumb slave" mode when USB is connected to them.

--

Tim Mitchell
----Original Message----
From: l...
[mailto:l...] On Behalf Of Gurkan
Karakus Sent: 17 August 2010 09:07 To:
l... Subject: RE: [lpc2000] USB Mass
Storage Device refresh

> Hi,
>
> Followink link says you can disable cacheing in your
> software.
>
> http://msdn.microsoft.com/en-us/library/aa365199%28v=VS.85%29.aspx

You can disable the caching but you still have the problem that Windows may read the file while you are in the middle of changing it, or while you are updating the FAT. Or Windows may change the file, you don't have any control over what windows is doing with your filesystem.

I've tried to do this and you get random failures. The only safe way I have found is to disallow access to the filesystem while the device is enumerated as mass storage.

--
Tim Mitchell

Hi,

Maybe you can await usb commands so your device do not respond to Windows a short period time. So you can make your changes in safety.

Gurkan

To: l...
From: t...@sabretechnology.co.uk
Date: Tue, 17 Aug 2010 09:28:45 +0100
Subject: RE: [lpc2000] USB Mass Storage Device refresh

----Original Message----

From: l...

[mailto:l...] On Behalf Of Gurkan

Karakus Sent: 17 August 2010 09:07 To:

l... Subject: RE: [lpc2000] USB Mass

Storage Device refresh

> Hi,

>

> Followink link says you can disable cacheing in your

> software.

>

> http://msdn.microsoft.com/en-us/library/aa365199%28v=VS.85%29.aspx

>

>

You can disable the caching but you still have the problem that Windows may read the file while you are in the middle of changing it, or while you are updating the FAT. Or Windows may change the file, you don't have any control over what windows is doing with your filesystem.

I've tried to do this and you get random failures. The only safe way I have found is to disallow access to the filesystem while the device is enumerated as mass storage.

--

Tim Mitchell
----Original Message----
From: l...
[mailto:l...] On Behalf Of Gurkan
Karakus Sent: 17 August 2010 09:36 To:
l... Subject: RE: [lpc2000] USB Mass
Storage Device refresh

> Hi,
>
> Maybe you can await usb commands so your device do not
> respond to Windows a short period time. So you can make
> your changes in safety.

I tried that too, but you still get problems - e.g. windows starts to read the file, then goes off to do something else for a bit. You come in and change the file. Windows then comes back and carries on reading where it left off, but now the data has changed so it reads the wrong thing, or half of it is old and half of it is new.

I am pretty sure there is no safe way of doing this other than disconnecting yourself from the USB while you make the changes.

--
Tim Mitchell

What with using 2 files - first only to write (for windows), second only do read (for windows)?

MCU and PC dont write to file in the same time.

Is it solve the problem with failures?

Pozdrawiam / Regards
Inzynier aplikacyjny / Software engineer
Jakub Szumacher
j...@fellows.pl

FELLOWS S.C.
Poland
www.fellows.pl

pozdrawiam
Jakub Szumacher
Fellows S.C.
----- Original Message -----
From: Tim Mitchell
To: l...
Sent: Tuesday, August 17, 2010 10:44 AM
Subject: RE: [lpc2000] USB Mass Storage Device refresh

----Original Message----
From: l...
[mailto:l...] On Behalf Of Gurkan
Karakus Sent: 17 August 2010 09:36 To:
l... Subject: RE: [lpc2000] USB Mass
Storage Device refresh

> Hi,
>
> Maybe you can await usb commands so your device do not
> respond to Windows a short period time. So you can make
> your changes in safety.

I tried that too, but you still get problems - e.g. windows starts to read the file, then goes off to do something else for a bit. You come in and change the file. Windows then comes back and carries on reading where it left off, but now the data has changed so it reads the wrong thing, or half of it is old and half of it is new.

I am pretty sure there is no safe way of doing this other than disconnecting yourself from the USB while you make the changes.

--
Tim Mitchell
----Original Message----
From: l...
[mailto:l...] On Behalf Of Jakub
Szumacher Sent: 17 August 2008 09:53 To:
l... Subject: Re: [lpc2000] USB Mass
Storage Device refresh

> What with using 2 files - first only to write (for
> windows), second only do read (for windows)?
>
> MCU and PC dont write to file in the same time.
>
> Is it solve the problem with failures?
>

The trouble is you don't know what Windows is going to do with either file. As far as it knows it has complete control over the filesystem and it just issues low level sector read/write commands. So you can't tell from the device end what it is doing, you just get requests to read or update "disk" sectors. It could move all the file sectors around, rename the files, or anything.

If there was a way of telling when Windows had completed a file operation, then it would maybe be safe to lock out windows from the USB for a time and make your own changes (assuming you had disabled disk caching). But I can't think of a way of doing this. All my attempts have ended in trouble sooner or later.

What is the purpose of changing the file? Another type of USB comms would probably be better suited to this task.

--
Tim Mitchell

Hi,
If you write/read with your own pc software with caching disabled, data validity should not be a problem. You can create a status flag in another readonly file to check your file status.

You may also need a synchronization algorithm to keep your pc software and device software working concurrent on this status file .
Gurkan

To: l...
From: t...@sabretechnology.co.uk
Date: Tue, 17 Aug 2010 10:08:15 +0100
Subject: RE: [lpc2000] USB Mass Storage Device refresh

----Original Message----

From: l...

[mailto:l...] On Behalf Of Jakub

Szumacher Sent: 17 August 2008 09:53 To:

l... Subject: Re: [lpc2000] USB Mass

Storage Device refresh

> What with using 2 files - first only to write (for

> windows), second only do read (for windows)?

>

> MCU and PC dont write to file in the same time.

>

> Is it solve the problem with failures?

>

The trouble is you don't know what Windows is going to do with either file. As far as it knows it has complete control over the filesystem and it just issues low level sector read/write commands. So you can't tell from the device end what it is doing, you just get requests to read or update "disk" sectors. It could move all the file sectors around, rename the files, or anything.

If there was a way of telling when Windows had completed a file operation, then it would maybe be safe to lock out windows from the USB for a time and make your own changes (assuming you had disabled disk caching). But I can't think of a way of doing this. All my attempts have ended in trouble sooner or later.

What is the purpose of changing the file? Another type of USB comms would probably be better suited to this task.

--

Tim Mitchell