469,926 Members | 2,361 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,926 developers. It's quick & easy.

proper use of (volatile unsigned short *)

I'm trying to write what should be a simple program, and it keeps
hanging if I use volatile....

The program, stripped of its error checking, is this:

unsigned short * start;
unsigned short * ctl;

int fd = open("/dev/mem", O_RDWR);
start = mmap(0, getpagesize()*2, PROT_READ|PROT_WRITE, MAP_SHARED, fd,
0x72000000);

ctl = (volatile unsigned short *)(start + 0x0410);
printf("got my pointer %p\n",ctl);
sleep(1);
printf("got ctl reg: 0x%02x\n",*ctl);

The program hangs as soon as it hits the last printf - it prints the
"got my pointer" message but hard-locks before printing the "got ctl
reg" message.

Before I start digging into various other issues, is this a proper use
of volatile?

--Yan
May 23 '07 #1
2 2876
In article <13*************@corp.supernews.com>,
CptDondo <ya*@NsOeSiPnAeMr.comwrote:
>I'm trying to write what should be a simple program, and it keeps
hanging if I use volatile....
>The program, stripped of its error checking, is this:

unsigned short * start;
unsigned short * ctl;
> int fd = open("/dev/mem", O_RDWR);
open() is not part of the C language; it is an OS extension.
> start = mmap(0, getpagesize()*2, PROT_READ|PROT_WRITE, MAP_SHARED, fd,
0x72000000);
mmap() is not part of the C language; it is an OS extension.

> ctl = (volatile unsigned short *)(start + 0x0410);
Note that start + 0x0410 means the address of the 0x0410'th
unsigned short past start, not the address of the unsigned
short 0x0410 bytes after short. If you intend a byte offset
rather than a short offset, then you will need to cast start
to be a character pointer before doing the pointer arithmetic.
> printf("got my pointer %p\n",ctl);
sleep(1);
printf("got ctl reg: 0x%02x\n",*ctl);
>The program hangs as soon as it hits the last printf - it prints the
"got my pointer" message but hard-locks before printing the "got ctl
reg" message.
>Before I start digging into various other issues, is this a proper use
of volatile?
In itself, it looks okay, but the pointer arithmetic is questionable.
--
"It is important to remember that when it comes to law, computers
never make copies, only human beings make copies. Computers are given
commands, not permission. Only people can be given permission."
-- Brad Templeton
May 23 '07 #2
Walter Roberson wrote:
In article <13*************@corp.supernews.com>,
CptDondo <ya*@NsOeSiPnAeMr.comwrote:
>I'm trying to write what should be a simple program, and it keeps
hanging if I use volatile....
>The program, stripped of its error checking, is this:

unsigned short * start;
unsigned short * ctl;
> int fd = open("/dev/mem", O_RDWR);

open() is not part of the C language; it is an OS extension.
> start = mmap(0, getpagesize()*2, PROT_READ|PROT_WRITE, MAP_SHARED, fd,
0x72000000);

mmap() is not part of the C language; it is an OS extension.

> ctl = (volatile unsigned short *)(start + 0x0410);

Note that start + 0x0410 means the address of the 0x0410'th
unsigned short past start, not the address of the unsigned
short 0x0410 bytes after short. If you intend a byte offset
rather than a short offset, then you will need to cast start
to be a character pointer before doing the pointer arithmetic.
OK, thanks for the explanation.... I got too hung up on the volatile
stuff to check the basic stuff....

>
> printf("got my pointer %p\n",ctl);
sleep(1);
printf("got ctl reg: 0x%02x\n",*ctl);
>The program hangs as soon as it hits the last printf - it prints the
"got my pointer" message but hard-locks before printing the "got ctl
reg" message.
>Before I start digging into various other issues, is this a proper use
of volatile?

In itself, it looks okay, but the pointer arithmetic is questionable.
The pointer arithmatic is what got me....

--Yan
May 23 '07 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

6 posts views Thread by Marcin Kalicinski | last post: by
9 posts views Thread by Tim Rentsch | last post: by
14 posts views Thread by John Temples | last post: by
3 posts views Thread by Mark A. Odell | last post: by
14 posts views Thread by google-newsgroups | last post: by
17 posts views Thread by dingoatemydonut | last post: by
13 posts views Thread by yaron | last post: by
6 posts views Thread by red floyd | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.