proper use of (volatile unsigned short *)

C

CptDondo

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
 
W

Walter Roberson

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.
 
C

CptDondo

Walter said:
open() is not part of the C language; it is an OS extension.


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



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....

In itself, it looks okay, but the pointer arithmetic is questionable.

The pointer arithmatic is what got me....

--Yan
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,999
Messages
2,570,243
Members
46,836
Latest member
login dogas

Latest Threads

Top