EmbeddedRelated.com
Forums

How to read/write an 'address' from a memory location......

Started by Unknown October 18, 2007
On Oct 20, 12:04 am, Hans-Bernhard Br=F6ker <HBBroe...@t-online.de>
wrote:
> aeroboro...@rediffmail.com wrote: > > I am trying to read an address from a memory location and then write > > into that memory location. > > Which "that" --- the one you read the address from, or the one the > address specifies? > > > unsigned long * mem_pointer =3D 0x93700020; > > unsigned long mem_value =3D *mem_pointer; > > > The question is - How do i use the value at <i> mem_value </i> as an > > address? > > An address kept in a variable is called a pointer, in C programs. So > mem_value should be of a pointer type, rather than an integer. > Consequentially, mem_pointer should be a pointer-to-pointer-to-something > type, e.g. > > some_type **mem_pointer =3D ...; > some_type *mem_value =3D *mem_pointer; > some_type the_actual_thing =3D *mem_value; > > > Basically, I want to do something like. > > *(unsigned char *)mem_value =3D 10. > > And what's keeping you from doing exactly that? > > But this all hides a probably more important question: why do you think > you should be doing this?
My problem is: I have two processors exchanging data over a shared section of memory. Now before processor 1 can write the data for processor 2 (in the shared memory), processor 1 needs to tell processor 2 where exactly it will be writing. Thats where i was coming from - processor 1 basically needs to write an "address" into a predefined location for processor 2 and that "address" will hold the actual data for processor 2. Thanks a lot for all your clarifications. I should try some of them now! Warm regards, Rintu
aeroboro221@rediffmail.com wrote:
>
... snip ...
> > My problem is: I have two processors exchanging data over a shared > section of memory. Now before processor 1 can write the data for > processor 2 (in the shared memory), processor 1 needs to tell > processor 2 where exactly it will be writing. Thats where i was > coming from - processor 1 basically needs to write an "address" > into a predefined location for processor 2 and that "address" will > hold the actual data for processor 2.
That is outside the abilities of the C language, which has no intrinsic knowledge of multiple processes and managing them. However, if your description is adequate, the following might work: Global (common memory): int running = 1; Type *ptr = NULL; Sender code fragment: do { /* prepare data */ while (ptr && running) continue; if (running) { ptr = malloc(sizeof *ptr); *ptr = data; } } while (running)' Receiver code fragment: do { while (!ptr && running) continue; if (running) { rcvdata = *ptr; free(*ptr); ptr = NULL; /* process rcvdata */ } } while (running); Note that either process can shut down the interaction by setting running to zero. Sender has a local variable named data, and receiver has one named rcvdata. The processes are in strict sync, i.e. one item of data causes one item to be transmitted, and nothing more is done until that item is received. Similarly the receiver can do nothing while waiting for new data to be prepared. Note that malloc failing in sender is a fatal error. The initialization values ensure that the sender runs first. The definition of 'Type' controls the amount of data transferred. The 'continue' statements can be changed into function calls to do other things. -- Chuck F (cbfalconer at maineline dot net) Available for consulting/temporary embedded and systems. <http://cbfalconer.home.att.net> -- Posted via a free Usenet account from http://www.teranews.com
On 22/10/2007 aeroboro221@rediffmail.com wrote:

.
.
.
> My problem is: I have two processors exchanging data over a shared > section of memory. Now before processor 1 can write the data for > processor 2 (in the shared memory), processor 1 needs to tell > processor 2 where exactly it will be writing. Thats where i was coming > from - processor 1 basically needs to write an "address" into a > predefined location for processor 2 and that "address" will hold the > actual data for processor 2. > > Thanks a lot for all your clarifications. I should try some of them > now! > > Warm regards, > Rintu
Ah, the pleasures of using dual-port RAM. You need to have a good handle on the use of semaphores so that the data is not overwritten before the second processor has read it. -- John B
aeroboro221@rediffmail.com wrote:

> My problem is: I have two processors exchanging data over a shared > section of memory. Now before processor 1 can write the data for > processor 2 (in the shared memory), processor 1 needs to tell > processor 2 where exactly it will be writing.
No. It only needs to do that as it *finishes* writing there. Allowing the reader to find out you've started writing, before you're finished doing it, will cause nasty race conditions.
> Thats where i was coming from - processor 1 basically needs to write > an "address" into a predefined location for processor 2 and that > "address" will hold the actual data for processor 2.
No. You rather probably shouldn't be using addresses for that part of the communication --- not even in "quotes". Use indices into a shared array of bytes instead. The difference being that the two processors don't have to agree on where in their respective address space that shared memory actually is. Pointers are pretty much *always* the wrong thing to export from any program to the outside world (or another program, for that matter).
On Oct 23, 2:28 am, CBFalconer <cbfalco...@yahoo.com> wrote:
> aeroboro...@rediffmail.com wrote: > > ... snip ... > > > My problem is: I have two processors exchanging data over a shared > > section of memory. Now before processor 1 can write the data for > > processor 2 (in the shared memory), processor 1 needs to tell > > processor 2 where exactly it will be writing. Thats where i was > > coming from - processor 1 basically needs to write an "address" > > into a predefined location for processor 2 and that "address" will > > hold the actual data for processor 2. > > That is outside the abilities of the C language, which has no > intrinsic knowledge of multiple processes and managing them. > However, if your description is adequate, the following might work: > > Global (common memory): > int running = 1; > Type *ptr = NULL; > > Sender code fragment: > do { > /* prepare data */ > while (ptr && running) continue; > if (running) { > ptr = malloc(sizeof *ptr); > *ptr = data; > } > } while (running)' > > Receiver code fragment: > do { > while (!ptr && running) continue; > if (running) { > rcvdata = *ptr; > free(*ptr); > ptr = NULL; > /* process rcvdata */ > } > } while (running); > > Note that either process can shut down the interaction by setting > running to zero. Sender has a local variable named data, and > receiver has one named rcvdata. The processes are in strict sync, > i.e. one item of data causes one item to be transmitted, and > nothing more is done until that item is received. Similarly the > receiver can do nothing while waiting for new data to be prepared. > > Note that malloc failing in sender is a fatal error. The > initialization values ensure that the sender runs first. The > definition of 'Type' controls the amount of data transferred. The > 'continue' statements can be changed into function calls to do > other things. > > -- > Chuck F (cbfalconer at maineline dot net) > Available for consulting/temporary embedded and systems. > <http://cbfalconer.home.att.net> > > -- > Posted via a free Usenet account fromhttp://www.teranews.com
The algorithm of exchanging data once the addresses are known to both processors is pretty much done. The way to communicate the address is where I was kind of stuck. I just tested one of the ways described here and it seems to work! Thanks again! Regards, Rintu
By the way, there is no concept of semaphores between two processors
right? What I can think of is a flag shared between the two processors
by means of shared RAM or a messaging unit.

>The algorithm of exchanging data once the addresses are known to both >processors is pretty much done. >The way to communicate the address is where I was kind of stuck. >I just tested one of the ways described here and it seems to work!
So now you have a program that *seems to work. -- mac the na&#4294967295;f
On 2007-10-23, aeroboro221@rediffmail.com <aeroboro221@rediffmail.com> wrote:

> By the way, there is no concept of semaphores between two > processors right?
Sure there is.
> What I can think of is a flag shared between the two > processors by means of shared RAM or a messaging unit.
A flag + a couple atomic operations == semaphore. -- Grant Edwards grante Yow! People humiliating at a salami! visi.com
aeroboro221@rediffmail.com wrote:
> > By the way, there is no concept of semaphores between two processors > right? What I can think of is a flag shared between the two > processors by means of shared RAM or a messaging unit.
I have no idea what this refers to - you have omitted the quotation portion. However, if you are referring to my earlier answer, the state of 'ptr' functions as a data_ready flag. It is not a good process for simultaneous action. -- Chuck F (cbfalconer at maineline dot net) Available for consulting/temporary embedded and systems. <http://cbfalconer.home.att.net> -- Posted via a free Usenet account from http://www.teranews.com