473,383 Members | 1,853 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,383 software developers and data experts.

variables at specified memory locations ...

I'm porting code from one embedded system to another.

Rather than declaring a pointer to memory/peripheral of interest and
dereferencing it, the original system had provisions for locating
variables at a specific address using proprietary c extensions.

The linker on the new tool set is all but inaccessible I'd I really
not rather modify the code to use a dereferenced pointer. The best I
can fathom is below:

typedef struct peripheral_tag
{
...
int some_val;
...
} peripheral;

#define P_ADD 0x1000
#define Peripheral_var (*((peripheral *)P_ADD))

So that in use, I'd have

Peripheral_var.some_val++;

If anyone considers this a revolting solution I'd appreciate they're
alternative.
Nov 14 '05 #1
1 1375
On 18 Oct 2004 13:26:40 -0700
ri**@advancedbioelectric.com (Jimbo) wrote:
I'm porting code from one embedded system to another.

Rather than declaring a pointer to memory/peripheral of interest and
dereferencing it, the original system had provisions for locating
variables at a specific address using proprietary c extensions.

The linker on the new tool set is all but inaccessible I'd I really
not rather modify the code to use a dereferenced pointer. The best I
can fathom is below:

typedef struct peripheral_tag
{
...
int some_val;
...
} peripheral;
You need to read the documentation to find out if the compiler will
introduce and padding to the struct which might move things from where
you think they are.
#define P_ADD 0x1000
#define Peripheral_var (*((peripheral *)P_ADD))
You should probably also use the volatile keyword to prevent accesses to
the data from being optimised away or the order being changed.
So that in use, I'd have

Peripheral_var.some_val++;

If anyone considers this a revolting solution I'd appreciate they're
alternative.


I think that I would not use the struct. I would probably do something
like:
#define DEVICE_BASE (0x1000)
#define DEVICE_VAR (*(volatile int *)(DEVICE_BASE+0x04))
....
DEVICE_VAR++;

This avoids as many changes if structure padding changes, and obviously
it is up to you to ensure that everything is properly aligned, but then
you already have problems if you have a hardware register that is not
properly aligned for the processor.

Obviously we are well in to implementation defined (or is it undefined?)
behaviour when constructing and using pointers to fixed addresses.
--
Flash Gordon
Sometimes I think shooting would be far too good for some people.
Although my email address says spam, it is real and I read it.
Nov 14 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

11
by: srinivas reddy | last post by:
Hi, Is there any chance that a program doesn' work properly even after a variable is declared as volatile? I remember somebody mentioning a scenario involving L1, L2 caches. Could anybody throw...
11
by: Neo | last post by:
Where does global, static, local, register variables, free memory and C Program instructions get stored? -Neo
122
by: Einar | last post by:
Hi, I wonder if there is a nice bit twiddling hack to compare a large number of variables? If you first store them in an array, you can do: for (i = 0; i < n; i++) { if (array != value) {...
5
by: elzacho | last post by:
I would like to (and commonly do) define my variables in the most specific scope I can. For example... int foo(int a, int b, int c) { /* don't declare temp here if we can help it */ ... ...
27
by: Madhav | last post by:
Hi all, I did not understand why do the global vars are initialized to NULL where as the block level variables have random values? I know that the C standard requires this as was mentioned in a...
18
by: akhil.misra | last post by:
where does a static variable inside a function get stored?? where does it go in memory?? like Global goes in RAM, Local variable goes on Stack... but how does static variable takes place in memory??
1
by: techie82 | last post by:
Hello all I'm a fresher to Microcontroller programming. I have defined certain global variables that I need to store at consecutive memory locations, say starting from 0x8000h . Will appreciate...
41
by: none | last post by:
Hello, IIRC, I once saw an explanation how Python doesn't have "variables" in the sense that, say, C does, and instead has bindings from names to objects. Does anyone have a link? Thanks, ...
43
by: Kislay | last post by:
Which of the following is correct regarding the storage of global variables : 1. Global variables exist in a memory area that exists from before the first reference in a program until after the...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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...

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.