1

A quick answer required !

#define PortA_Data (*(volatile unsigned long*)0x40004000)

here in this example I linked PortA_Data with an address, can any body explain the mechanism of this code, there is pointer to address, there is a pointer to return type, is there any function here? I can't understand what it mean. I know about pointers but there is something messy, really annoying me.

Sajjad Ahmed
  • 111
  • 6

4 Answers4

3

"0x40004000" is a hexidecimal integer. Presumablly the integer is a memory address from some CPU-specific documentation

"volatile unsigned long*" is a description of a type. Specifically the type it describes is a volatile pointer (volatile pointers disable certain optimisations that would be inappropriate for hardware access like this) to an unsigned long.

"(type)value" in C is a typecast.

So "(volatile unsigned long*)0x40004000" is taking 0x40004000 and typecasting it to a volatile unsigned long*.

The * near the beggining is telling the compiler to dereference the pointer. That is to access whatever is at the location pointed at by the pointer.

Finally the outer brackets are to make sure everything in the macro remains grouped together if it is used as part of a larger operation.

So putting it all together when we use your macro in an expresion we are telling the compiler to read/write memory address 0x40004000 as an unsigned long (most likely an unsigned 32-bit value)

Peter Green
  • 21,158
  • 1
  • 38
  • 76
  • Peter Green thanks for your great explanation, one last thing, typecast is used to convert data in to specific data type, right? and how an address can be typecast into datatype specially in pointer (unsigned long*)? where normally an address is used by pointer to point toward that address. – Sajjad Ahmed Jan 17 '16 at 10:37
  • A pointer is a representation of an address which we can dereference to access the thing at that address. The typecast is essentially saying "stop treating this number as a number and start treating it as an address, specifically the address of a value of type unsigned long". – Peter Green Jan 17 '16 at 11:21
0

No. There is an address, a typecast, and a dereference. The first is just an integer representing the location in memory in question. The latter two are C-specific mechanisms for having the compiler treat a value in a specific way.

Ignacio Vazquez-Abrams
  • 48,282
  • 4
  • 73
  • 102
0

Let's split it up to see what is going on (I agree with Ignacio, maybe this can help)

'#define PortA_Data' : Assign whatever comes next to the symbolic name PortA_Data

(* : it is a pointer

(volatile unsigned long *) : a typecast to define this as a specific type of pointer. The volatile keyword will prevent the variable being optimised out as the compiler may see no valid purpose for the variable.

0x40004000) : with this hexadecimal value

To use it we may do something like

unsigned long mydata; //declare a variable

mydata = *(PortA_Data); // retrieve whatever is at address 0x40004000 into an unsigned long datatype.

Peter Smith
  • 21,923
  • 1
  • 29
  • 64
0

Now I got the complete idea and concept of my question, now I can answer myself :)

typecast convert any data into other data type, like char var_1; /* convert 65 into char */ var_1 = (char)65; /* for pointer (my case) */ unsigned long *pointer; /* assigning address to this pointer required typecast */ pointer = (unsigned long*)0x40004000; /* now we can read and write at this address*/ /* or can use this concept to ease our self in different ways*/ /* assigning value to address */ *pointer = 233; printf("%i",*pointer);

and output will be: 233 at that address 0x40004000

Sajjad Ahmed
  • 111
  • 6