[Icc-avr] Casting

Jaspers, Ton t.jaspers at cpseurope.com
Thu Jan 17 00:38:57 PST 2008


At some point the question was raised what the "best" way to do it is.
All solution given work but when one thinks about "the best solution"
tings like portability, interfaces and networking spring to mind. I
believe that there is no such thing as an AVR only engineer, we do not
live on an isolated island. In real life programming today we almost
always have to interface with other systems and hence we have to deal
with endianness en portability. Many devices have several interfaced
CPU's on a single PCB, just have a look inside any digital TV-set. 
 
Here is a real life example: When I design a SPI interface between two
processors, I only write the code once. If it is done right it will run
on both systems. It is by far the quickest way to ensure that both ends
of the data link talk the same language. And nobody wants to write the
same code twice.  
 
Cheers,
Ton
    

> -----Original Message-----
> From: icc-avr-bounces at imagecraft.com 
> [mailto:icc-avr-bounces at imagecraft.com] On Behalf Of Johannes 
> Assenbaum
> Sent: woensdag 16 januari 2008 0:44
> To: icc-avr at imagecraft.com
> Subject: RE: [Icc-avr] Casting
> 
> 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
> 
> 
> 
> _______________________________________________
> Icc-avr mailing list
> Icc-avr at imagecraft.com
> http://dragonsgate.net/mailman/listinfo/icc-avr
> 



More information about the Icc-avr mailing list