[Icc-avr] Casting
Johannes Assenbaum
jassenbaum at htp-tel.de
Tue Jan 15 15:44:01 PST 2008
Thank you, Albert.
I think there has been too much emphasis on not only the endians in this thread, too. I mean, this thread actually was started with an obviously array related question about programming an AVR - at90usb162 as posted before by same author -, and far away from excusing sloppy programming, endians and portability never will be really interesting if I'm coding for unique parts like USB AVRs...
Johannes
> I think there has been too much emphasis on the endians in this thread.
> Nearly always these conversions are wanted for storage in byte-arrays or
> comms buffers, and as long as storing and retrieving uses the same
> method (and a few were mentioned here!) it doesn't matter where msb or
> lsb is. Using Motorola (oops.. Freescale), TMS, PIC & AVR, I haven't
> stumbled over it yet.
> I hope this doesn't upset Ton's "There is no excuse for sloppy
> programming." in which he is perfectly correct of course.
> Albert.
> -----Original Message-----
> From: icc-avr-bounces at imagecraft.com
> [mailto:icc-avr-bounces at imagecraft.com] On Behalf Of Johan H. Bodin
> Sent: Tuesday, January 15, 2008 09:50 PM
> To: Discussion list for ICCAVR and ICCtiny Users. You do NOT need
> tosubscribe to icc-announce if you are a member of this.
> Subject: Re: [Icc-avr] Casting
> This works for me:
> -----------
> #ifndef BYTEPICK_INCLUDED
> #define BYTEPICK_INCLUDED
> /***********************************/
> /* Johan Bodin SM6LKM 2007-06-01 */
> /***********************************/
> // This should ideally be predefined by the compiler...:
> // ICCAVR7 uses little endian (LSByte first)
> #define LITTLE_ENDIAN
> //#define BIG_ENDIAN
> // Sanity check:
> #ifdef LITTLE_ENDIAN
> #ifdef BIG_ENDIAN
> #error "You can't have both endian types in bytepick.h!"
> #endif
> #endif
> #ifndef LITTLE_ENDIAN
> #ifndef BIG_ENDIAN
> #error "You must select big/little endian in bytepick.h!"
> #endif
> #endif
> // Here we go:
> #ifdef BIG_ENDIAN
> #define HIGH_BYTE_32(longvar) (*((unsigned char *)(&longvar)))
> #define MID_H_BYTE_32(longvar) (*((unsigned char *)(&longvar) + 1))
> #define MID_L_BYTE_32(longvar) (*((unsigned char *)(&longvar) + 2))
> #define LOW_BYTE_32(longvar) (*((unsigned char *)(&longvar) + 3))
> #define HIGH_BYTE_16(shortvar) (*((unsigned char *)(&shortvar)))
> #define LOW_BYTE_16(shortvar) (*((unsigned char *)(&shortvar) + 1))
> #else
> #define HIGH_BYTE_32(longvar) (*((unsigned char *)(&longvar) + 3))
> #define MID_H_BYTE_32(longvar) (*((unsigned char *)(&longvar) + 2))
> #define MID_L_BYTE_32(longvar) (*((unsigned char *)(&longvar) + 1))
> #define LOW_BYTE_32(longvar) (*((unsigned char *)(&longvar)))
> #define HIGH_BYTE_16(shortvar) (*((unsigned char *)(&shortvar) + 1))
> #define LOW_BYTE_16(shortvar) (*((unsigned char *)(&shortvar)))
> #endif
> #endif // BYTEPICK_INCLUDED
> -----------
> Regards
> Johan Bodin
> _______________________________________________
> Icc-avr mailing list
> Icc-avr at imagecraft.com
> http://dragonsgate.net/mailman/listinfo/icc-avr
> Scanned by Bizo Email Filter
> _______________________________________________
> Icc-avr mailing list
> Icc-avr at imagecraft.com
> http://dragonsgate.net/mailman/listinfo/icc-avr
> --
> No virus found in this incoming message.
> Checked by AVG Free Edition.
> Version: 7.5.516 / Virus Database: 269.19.2/1224 - Release Date: 14.01.08 17:39
More information about the Icc-avr
mailing list