View previous topic :: View next topic |
Author |
Message |
maxrate
Joined: 01 Jan 2007 Posts: 38 Location: Mississauga, Ontario, Canada
|
Example 2432.c question |
Posted: Wed Oct 18, 2023 8:25 am |
|
|
Hello all, I'm having some trouble wrapping my head around a code snippet from the 2432.c driver. I'd really like to understand this opposed to just merrily using the driver without a clue.
#define hi(x) (*((int8 *)&x+1))
I see that this is for the upper portion/word of the memory address beyond 255 decimal (beyond 8 bit addressing). I see that this points to a memory location containing the target address of the eeprom. When writing I2C to the eeprom, this would be for the high set of bits of the 16 bit target memory address of the eeprom. Another I2C address instruction would be sent for the lower set of bits.
I do not understand the use of 'x' precisely. I believe x contains the 16 bit address value (in the driver) and not a pointer to the address - I could easily be wrong about this. I do not understand the use of & (address unary). I believe it might shift into the next set of bits (after the 8th bit) of the address or the next byte in memory.
I'm terribly fuzzy on how this macro works. If anyone could walk me through this, it would be greatly appreciated. |
|
|
Jerson
Joined: 31 Jul 2009 Posts: 125 Location: Bombay, India
|
|
Posted: Wed Oct 18, 2023 8:05 pm |
|
|
Quote: | #define hi(x) (*((int8 *)&x+1))
|
Try to read it like this
define macro
hi of x
as
the value pointed at (*
which is of type int8 pointer (int8 *)
whose location is address x+1 (&x+1)
Hope you can now understand it. |
|
|
maxrate
Joined: 01 Jan 2007 Posts: 38 Location: Mississauga, Ontario, Canada
|
|
Posted: Thu Oct 19, 2023 5:10 am |
|
|
Jerson, this helps a lot, thank you. I do have a question... is the 'x' type defined by (* or (int8 *) portion of the snippet? |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19538
|
|
Posted: Thu Oct 19, 2023 5:21 am |
|
|
Neither.
X is just any variable. It's type is what it is declared as.
So:
Code: |
int16 fred=0xaa55;
int8 val;
val=hi(fred);
|
Will have val holding 170 (0xaa).
You can actually use 'hi' defined this way on any variable.
On an int8, it will return garbage - pulling whatever is in the next
variable in memory. However on any other variable, it will return what
is in the second byte of the variable.
Macros fundamentally are 'type less'.
The key point about the int8 *, is it casts the address of 'x' to be of
type int8 *. This then means when you increment it, it increments by
just one. Pointers generically increment by the size of the variable
they point to. |
|
|
maxrate
Joined: 01 Jan 2007 Posts: 38 Location: Mississauga, Ontario, Canada
|
|
Posted: Sun Oct 22, 2023 5:43 pm |
|
|
Thank you Ttelmah and Jerson, I believe this to be crystal clear now. I didn't consider using this macro/function on an int8, as it is designed to retrieve the more significant portion of the int16 and cast to an int8. I agree that would pull garbage if used against an int8. Really fantastic of you both to offer some assistance. |
|
|
|