0
\$\begingroup\$

I have two macros:

 #define POKE(addr, val) (*(volatile unsigned int *)(addr) = (val))
 #define PEEK(addr) (*(volatile unsigned int *)(addr))

What they do if I call them like this:

ulKey = PEEK(NETX_IO_CFG_ACCESS_KEY);
POKE(NETX_IO_CFG_ACCESS_KEY, ulKey);

If #define NETX_IO_CFG_ACCESS_KEY 0x00100070U

\$\endgroup\$
  • \$\begingroup\$ Poke places the value 'val' into address 'addr'; 'peek' returns the value in address 'addr' in the nominated variable, ulKey. \$\endgroup\$ – Chu Jul 24 '15 at 7:54
2
\$\begingroup\$

background:

In computing,

PEEK is a BASIC programming language extension used for reading the contents of a memory cell at a specified address.

The corresponding command to set the contents of a memory cell is POKE.

peek will read the content of the address defined by:

#define NETX_IO_CFG_ACCESS_KEY     0x00100070U

and store it in "ulKey" which is supposed to be a variable declared as unsigned int (as defined in the pointer )

the next line:

POKE(NETX_IO_CFG_ACCESS_KEY, ulKey);

is to set the content of the address NETX_IO_CFG_ACCESS_KEY with the value of "ulKey"again, I guess this code is just for example purposes

most probably you should have a look at pointers

http://www.cplusplus.com/doc/tutorial/pointers/

the pointers part: pointers: a variable which stores the address of another variable is called a pointer

(volatile unsigned int" * this one")(addr) the asterisk in this line is to tell the compiler that this is a pointer,

then the asterisk in the begginging is for derefrencing ("* this one "(volatile unsigned int *)(addr) = (val)) which is to derefrence the "value pointed to by "addr"

we should tell the compiler that, this address is storing or will store which type of data and here comes unsigned int

volatile is to

Volatile tells the compiler not to optimize anything that has to do with "addr"

you can have a better understanding from here :

https://stackoverflow.com/questions/246127/why-is-volatile-needed-in-c

\$\endgroup\$
  • \$\begingroup\$ why 0x00100070U but not the 0x00100070 ?(without U?) @Sabir Moglad \$\endgroup\$ – user3826752 Jul 24 '15 at 8:47
  • \$\begingroup\$ @user3826752 U is just a suffix to tell the compiler that the constant is unsigned \$\endgroup\$ – m.Alin Jul 24 '15 at 9:09
1
\$\begingroup\$

PEEK reads the content of a 32-bit* segment of memory located at address addr (0x00100070 in your case).

POKE set the content of a 32-bit segment of memory located addr to be val.


*Assuming that for your system/compiler unsigned int is 4 bytes long.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.