473,407 Members | 2,546 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,407 software developers and data experts.

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 3235
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Marcin Kalicinski | last post by:
Hi all, I have a problem with 'volatile' use in C++. The function get_clocks() below tries to use a 16 bit hardware counter to count time. The counter overflows very often. But an interrupt is...
9
by: Tim Rentsch | last post by:
I have a question about what ANSI C allows/requires in a particular context related to 'volatile'. Consider the following: volatile int x; int x_remainder_arg( int y ){ return x % y; }
14
by: John Temples | last post by:
Given this code: extern volatile unsigned char v; int main(void) { v; return 0; }
3
by: Mark A. Odell | last post by:
If I have a structure that may point to a volatile "region (e.g. device)" or a context in memory what would be the best way to use the volatile keyword? E.g. a) volatile on struct objects ...
14
by: google-newsgroups | last post by:
Hello, even (or because?) reading the standard (ISO/IEC 9899/1999) I do not understand some issues with volatile. The background is embedded programming where data is exchanged between main...
17
by: dingoatemydonut | last post by:
The C99 standard states: "In the abstract machine, all expressions are evaluated as specified by the semantics. An actual implementation need not evaluate part of an expression if it can deduce...
13
by: yaron | last post by:
Hi all, let be focus on sigle processor machine 32 bits. 1. with multi-threaded on single processor machine 32bit do i have to sync access to atomic get/set properties of type less then 32 bits...
6
by: red floyd | last post by:
I have a struct that maps onto a set of memory mapped registers. I access this via a pointer. Is it better to declare it as pointer to a volatile struct, or to declare the individual members as...
3
by: C++Liliput | last post by:
It seems that the keyword "volatile" is used to make sure that threads reading (or writing to) the same data should see a consistent picture of the variable i.e. updates made to the common data...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.