Apr 23, 2013

Little endian and Big endian - A real world example.

This post exemplifies how a little endian and a big endian processors are interfaced, What is little endian and what is big endian..
Atmega328 is a processor that follows little endian ordering and w5100 is an ethernet chip which follows big endian ordering.

I was trying to interface a W5100 ethernet chip with Atmega328. The communication protocol is SPI. One byte of data is transferred from Atmega328 to W5100 at a time. I wanted to store a value 0xABCD starting at location 'a' of W5100.

So if I initialise 0xABCD as a 16 bit value

uint16_t value = 0xABCD;

Atmega328 follows little endian. 0xCD will be in first location(a) and 0xAB will be in the second location(a+1). I have to first transfer byte at  'a + 1' to say location 'b' of W5100 and byte at 'a' to location 'b+1' of w5100.

So fitst value is AND with 0xFF00 and left shift 8 bits gives AB at location 'a'. Then transfer it via SPI and then transfer value which is nothing but CD.















Write to W5100 from Atmega328

uint16_t value = 0xABCD;
uint16_t temp = value;

spi_write ( a , ( temp & 0xFF00 ) >> 8 );
spi_write ( a ,  temp );

Read from W5100 to Atmega328

uint16_t value;
uint16_t temp = value;

value = spi_read ( a );
value = ( value & 0x00FF ) << 8 + spi_read ( a + 1 );








For Web Developer

  open -a "Google Chrome" --args --disable-web-security