Basic c question from a newbie

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,

Reply to
prashna
Loading thread data ...

Hi Prashna!

Not the parameter is declared volatile, but the unsigned char the po> Hi all,

Reply to
Peter Scholz

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
that are worse than the original problem.  Usage: "Windows ME
fixes many of the shortcomings of Windows 98 SE". - Hutchison
Reply to
CBFalconer

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 (broeker@physik.rwth-aachen.de)
Even if all the snow were burnt, ashes would remain.
Reply to
Hans-Bernhard Broeker

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               
                               visi.com
Reply to
Grant Edwards

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

Reply to
Paul Keinanen

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		| paul@pcserv.demon.co.uk
        Main Site
              GNU H8 & mailing list info.
             For those web sites you hate.
Reply to
Paul Carpenter

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.

Reply to
Karl Lu Shanghai

-- Karl Lu(MSN:karl snipped-for-privacy@hotmail.com) Embeded System Lover

-- Thanks Karl Lu

Reply to
Karl Lu Shanghai

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
removebalmerconsultingthis@att.net
Reply to
Alan Balmer

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

Reply to
Karl Lu Shanghai

Use "-- ". Note the space character.

Michael

Reply to
Michael Hofmann

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

--
Al Balmer
Balmer Consulting
removebalmerconsultingthis@att.net
Reply to
Alan Balmer

Thanks. I got it.

--
MSN: karl_lu25@hotmail.com
Embedded System Lover
Reply to
Karl Lu Shanghai

ElectronDepot website is not affiliated with any of the manufacturers or service providers discussed here. All logos and trade names are the property of their respective owners.