CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to CCS Technical Support

Example 2432.c question

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
maxrate



Joined: 01 Jan 2007
Posts: 38
Location: Mississauga, Ontario, Canada

View user's profile Send private message

Example 2432.c question
PostPosted: Wed Oct 18, 2023 8:25 am     Reply with quote

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

View user's profile Send private message Visit poster's website

PostPosted: Wed Oct 18, 2023 8:05 pm     Reply with quote

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

View user's profile Send private message

PostPosted: Thu Oct 19, 2023 5:10 am     Reply with quote

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: 19539

View user's profile Send private message

PostPosted: Thu Oct 19, 2023 5:21 am     Reply with quote

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

View user's profile Send private message

PostPosted: Sun Oct 22, 2023 5:43 pm     Reply with quote

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.
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group