ethernet sheild not setting mac correctly

Hi all,

This is either a really obscure problem, or so simple everyone knows answer but me.

I'm using IDE 1.05, and a freetronics etherten controller, which is same as Arduino + ethernet sheild using W5100 chip. When I set the mac address using code below, the actual mac address on the LAN has the first octet forced to 0 so it becomes 00:AD:BE:EF:FE:ED

byte mac[6] = {0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
  
  // DHCP, can take many seconds
  Ethernet.begin(mac);

I've checked the mac address that's being reported to network using both wireshark and windows arp -a command and both report mac address as above, with first octet 0. I've also tried various combinations of mac address, but first octet is always being forced to 0.

Am I missing something incredibly simple?

Thanks in advance

No other SPI devices connected? No card in the SD slot?

edit: If that isn't it, try this test code. Does it display the correct MAC? Insure your serial monitor is set to 115200 baud or set a new baud rate in Serial.begin().

#include <SPI.h>
#include <Ethernet.h>
#include <utility/w5100.h>

// Enter a MAC address for your controller below.
// Newer Ethernet shields have a MAC address printed on a sticker on the shield
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
byte storedMac[6];

// Initialize the Ethernet client library
// with the IP address and port of the server 
// that you want to connect to (port 80 is default for HTTP):
EthernetClient client;

int freeRam() {
  extern int __heap_start,*__brkval;
  int v;
  return (int)&v - (__brkval == 0 ? (int)&__heap_start : (int) __brkval);  
}

void setup() {
  // start the serial library:
  Serial.begin(115200);

  pinMode(4,OUTPUT);
  digitalWrite(4,HIGH);

  Serial.print(F("SRAM left: "));
  Serial.println(freeRam());

  // start the Ethernet connection:
  Serial.println("Starting ethernet");
  while (Ethernet.begin(mac) == 0) {
    Serial.println("Failed to configure Ethernet using DHCP");
    delay(5000);

  Serial.print(F("SRAM left: "));
  Serial.println(freeRam());


    Serial.println("trying DHCP again");
  }
  // print your local IP address:
  Serial.print("My IP address: ");
  Serial.println(Ethernet.localIP());

  W5100.readSHAR(storedMac);

  for (byte thisByte = 0; thisByte < 6; thisByte++) {
    // print the value of each byte of the MAC address:
    Serial.print(storedMac[thisByte], HEX);
    if(thisByte < 5) Serial.print(":"); 
  }
  Serial.println();
}

void loop() {
  byte updateVal = Ethernet.maintain();

  if(updateVal) {
      
    Serial.print(F("DHCP maintain..."));
  
    switch(updateVal) {
      case 0:  Serial.println(F("no action"));
              break;

      case 1:  Serial.println(F("renew failed"));     
              break;
              
      case 2:  Serial.println(F("renew success"));           
              break;

      case 3:  Serial.println(F("rebind failed"));           
              break;

      case 4:  Serial.println(F("rebind success"));           
              break;

      default:  Serial.println(F("unknown"));
    }      
  }

  delay(1000);
}

Thanks Tim,

Awesome insight into some of the low level functions available, I really need to get more familiar with this stuff. I wanted to check the W5100 registers but didn't realise the low level driver was that easily accessible, thought I'd have to hack into the source to do that.

No SPI or SD issues but did find that something is overwriting the mac buffer before writing to device. I have some code that reads params from eeprom and have traced the problem to when I read gateway string and convert to address, but not sure why yet.

  // init ethernet  
  byte mac[6];
  byte ip[4];
  byte sub[4];
  byte gate[4];
  
  char* pStr = sysvar_read(SYSVAR_MAC_IDX)->c;  // returns mac string "XX:XX:XX:XX:XX:XX"
  for(i=0; i<6; i++) {
    sscanf(pStr,"%x", &mac[i]);
    do {
      ++pStr;
    } while((*pStr != ':') && (*pStr != 0));
    if(*pStr == ':') ++pStr;
  }
  
  pStr = sysvar_read(SYSVAR_IP_IDX)->c;       // returns ip string "ddd.ddd.ddd.ddd"
  sscanf(pStr,"%d.%d.%d.%d",&ip[0],&ip[1],&ip[2],&ip[3]);
  pStr = sysvar_read(SYSVAR_SUB_IDX)->c;
  sscanf(pStr,"%d.%d.%d.%d",&sub[0],&sub[1],&sub[2],&sub[3]);

 // not sure why but this this section is clearing first byte of mac
  pStr = sysvar_read(SYSVAR_GATE_IDX)->c;
  sscanf(pStr,"%d.%d.%d.%d",&gate[0],&gate[1],&gate[2],&gate[3]);

  for (i = 0; i < 6; i++) {
    // print the value of each byte of the MAC address:
    Serial.print(mac[i], HEX);
    if(i < 5) Serial.print(":"); 
  }
  Serial.println();
  
  // DHCP, can take many seconds
  if(ip[0] == 0) {
    Ethernet.begin(mac); 
  }
  else {  
    // if static address is defined - (mac, ip, dns, gateway, subnet)
    Ethernet.begin(mac, ip, gate, gate, sub);
  }

Thanks for help

FYI, fixed problem. I added an extra byte to array for mac, ip, gateway & subnet variables and problem dissapeared. Code below works as expected. I can only surmise that the gate and mac array are actually next to each other, and that the sscanf function was corrupting the first byte of mac when it was scanning data into gate array.

// init ethernet  
  byte mac[7];
  byte ip[5];
  byte sub[5];
  byte gate[5];
  
  char* pStr = sysvar_read(SYSVAR_MAC_IDX)->c;
  sscanf(pStr,"%X:%X:%X:%X:%X:%X",&mac[0],&mac[1],&mac[2],&mac[3],&mac[4],&mac[5]);
  pStr = sysvar_read(SYSVAR_IP_IDX)->c;
  sscanf(pStr,"%d.%d.%d.%d",&ip[0],&ip[1],&ip[2],&ip[3]);
  pStr = sysvar_read(SYSVAR_SUB_IDX)->c;
  sscanf(pStr,"%d.%d.%d.%d",&sub[0],&sub[1],&sub[2],&sub[3]);
  pStr = sysvar_read(SYSVAR_GATE_IDX)->c;
  sscanf(pStr,"%d.%d.%d.%d",&gate[0],&gate[1],&gate[2],&gate[3]);
  
  // DHCP, can take many seconds
  if(ip[0] == 0) {
    Ethernet.begin(mac); 
  }
  else {  
    // if static address is defined - (mac, ip, dns, gateway, subnet)
    Ethernet.begin(mac, ip, gate, gate, sub);
  }