stm32duino / STM32Ethernet

Arduino library to support Ethernet for STM32 based board
163 stars 44 forks source link

fix: align MAC address api with Arduino reference #89

Closed fpistm closed 2 months ago

fpistm commented 2 months ago

References: https://www.arduino.cc/reference/en/libraries/ethernet/ethernet.macaddress/ https://www.arduino.cc/reference/en/libraries/ethernet/ethernet.setmacaddress/

[!Important] Pay attention that setMACAddress() have to be called before Begin(). Else new MAC will be ignored.

This PR also fixes with the default MAC address usage. It is used only if one or more MAC_ADDRx defined. The MAC address is also correct at all level.

Fixes #81

Test sketch

#include <LwIP.h>
#include <STM32Ethernet.h>

byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
IPAddress ip(192, 168, 1, 69);

void printMacAddress(byte* macBuffer) {
  Serial.print("The MAC address is: ");
  for (byte octet = 0; octet < 6; octet++) {
    Serial.printf("%02X", macBuffer[octet]);
    if (octet < 5) {
      Serial.print('-');
    } else {
      Serial.println();
    }
  }
}

void setup() {
  Serial.begin(9600);
  while (!Serial) {};
  byte macBuffer[6];  // create a buffer to hold the MAC address
  byte newMac[] = { 0x00, 0xAA, 0xBB, 0xCC, 0xDE, 0x02 };

  Ethernet.MACAddress(macBuffer);  // fill the buffer
  printMacAddress(macBuffer);      // print default MAC address
  // Selectdesired config for the test
  // Ethernet.setMACAddress(newMac);  // change the MAC address
  // Ethernet.begin(mac, ip);
  // Ethernet.begin(ip);
  Ethernet.MACAddress(macBuffer);  // fill the buffer
  printMacAddress(macBuffer);
  Ethernet.setMACAddress(newMac);  // change the MAC address
  Ethernet.MACAddress(macBuffer);  // fill the buffer
  printMacAddress(macBuffer);
}

void loop() {
}