Basic c question from a newbie

Do you have a question? Post it now! No Registration Necessary

Translate This Thread From English to

Threaded View
Hi all,
I am new to embedded field and I found following function prototype.I
am not getting why the parameter is declared as volatile.what happens
if it is not volatile.Any pointers on this will be appreciated.
unsigned char Get_backup(volatile unsigned char *addr);
This function is used to get the data from Flash located at addr.
Thanks in advance,

Re: Basic c question from a newbie
Hi Prashna!

Not the parameter is declared volatile, but the unsigned char the
pointer points at.
This is normally done to make sure the value of the unsigned char is not
held in a register inside the function. ( It may change unexpectedly
because of the hardware or interrupts )
So it has to be fetched freshly on every access.

prashna wrote:
Quoted text here. Click to load it


Re: Basic c question from a newbie
Peter Scholz wrote: *** top-posting fixed ***
Quoted text here. Click to load it

If this is only used to access a flash memory, the 'volatile'
seems unnecessary.  The usual reason is that such a location is
actually something like a memory mapped status bit, which can
change without warning.  Without 'volatile' a statement such as:

    while (volatilething) continue; /* wait for it */

could be optimized into:

    if (volatilething)
       while (1) continue;   /* never retesting it */

--
fix (vb.): 1. to paper over, obscure, hide from public view; 2.
to work around, in a way that produces unintended consequences
We've slightly trimmed the long signature. Click to see the full one.
Re: Basic c question from a newbie

Quoted text here. Click to load it

The only reason I can thinkof would be that the routine also
wants to program the flash and needs to make sure that
read/write cycles happen correctly.

--
Grant Edwards                   grante             Yow!  Why was I BORN?
                                  at              
We've slightly trimmed the long signature. Click to see the full one.
Re: Basic c question from a newbie

Quoted text here. Click to load it

Even with a flash memory, I can think of two situations, in which the
use of "volatile" makes sense.
 
Assuming that the memory can be physically replaced at any time and
the intention is to access the currently installed device (not the
cached copy of the previous device), then "volatile" will force access
to the currently installed device.

Assuming there is some memory mapping hardware involved that might
change the logical access window in the fly to point to different
physical locations, the use of "volatile" makes a lot of sense.

Paul


Re: Basic c question from a newbie
On Thursday, in article

Quoted text here. Click to load it

Simpler still if you were doing a programme/verify of the flash a single
location at a time (with some devices this is easier to do), then a
volatile is essential.

--
Paul Carpenter        | snipped-for-privacy@pcserv.demon.co.uk
<http://www.pcserv.demon.co.uk/ Main Site
We've slightly trimmed the long signature. Click to see the full one.
Re: Basic c question from a newbie
->paul$@pcserv.demon.co.uk (Paul Carpenter) wrote in message
Hi Paul,
Would you like to tell me how to post my own signature with the message?
Just like this:
--
Karl Lu(MSN:karl snipped-for-privacy@hotmail.com)
Embeded System Lover
--
Thanks
Karl Lu

Re: Basic c question from a newbie
On 7 May 2004 22:23:48 -0700, snipped-for-privacy@etang.com (Karl Lu Shanghai)

Quoted text here. Click to load it

No, he didn't.

Quoted text here. Click to load it

I'd like to tell you how to quote enough of the post you're commenting
on that others can understand the comment.

--
Al Balmer
Balmer Consulting
We've slightly trimmed the long signature. Click to see the full one.
Re: Basic c question from a newbie
Quoted text here. Click to load it

Thanks.
Would you like to tell me how to post the siganture with the message as you do?
--
MSN: karl snipped-for-privacy@hotmail.com
Embedded System Lover

Re: Basic c question from a newbie

Quoted text here. Click to load it

Use "-- ". Note the space character.

Michael


Re: Basic c question from a newbie
On 11 May 2004 01:43:18 -0700, snipped-for-privacy@etang.com (Karl Lu Shanghai)

Quoted text here. Click to load it

Looks like you already have it right. The separator is "-- " dash,
dash, space.

--
Al Balmer
Balmer Consulting
We've slightly trimmed the long signature. Click to see the full one.
Re: Basic c question from a newbie
Quoted text here. Click to load it

Thanks. I got it.
--
MSN: karl snipped-for-privacy@hotmail.com
Embedded System Lover

Re: Basic c question from a newbie

Quoted text here. Click to load it

This would make sense only if the data at that address in your flash
memory is rewritten by an independent thread of control (an interrupt
handler, typically).  Informally, the meaning of "volatile" is that
the object qualified by it can change for reasons other than what is
manifestly spelled out by the C code itself.  The compiler has to know
that to avoid optimizations breaking the semantics of the C code.

--
Hans-Bernhard Broeker ( snipped-for-privacy@physik.rwth-aachen.de)
Even if all the snow were burnt, ashes would remain.

Re: Basic c question from a newbie
If this function is used by flash interface only, I think the reason
may be lied in flash driver itself. Many kind of flash can be queried
or programmed by writing a command sequences to arbitrary address. For
example, to query get the manufacture ID of an Intel flash
TE28F160C3B, One needs to write following code:
int sysFlashGetMfgID()
{
  unsigned short *chipAddress=0x80000000; /* flash is mapped to
address space started from 0x80000000 */
  unsigned short mfg;
  /* Issue a CFI command sequence to the Flash chip */
  chipAddress[0] = 0x90;
  mfg = chipAddress[0x0];
  return mfg;
}
So the content on the flash can be volatile too if the flash
programming code is running.

Site Timeline