|
| 1 | +/* |
| 2 | + * This example has been tested with Arduino Nano 33 IoT and with |
| 3 | + * Mikroe Two-Wire ETH Click board (MicroChip LAN8651). |
| 4 | + * |
| 5 | + * arduino-cli compile -b arduino:samd:nano_33_iot -v examples/tools/AT24MAC402-Read-MAC -u -p /dev/ttyACM0 |
| 6 | + * |
| 7 | + * Author: |
| 8 | + * Alexander Entinger |
| 9 | + */ |
| 10 | + |
| 11 | +/************************************************************************************** |
| 12 | + * INCLUDE |
| 13 | + **************************************************************************************/ |
| 14 | + |
| 15 | +#include <Wire.h> |
| 16 | + |
| 17 | +#include <Arduino_10BASE_T1S.h> |
| 18 | + |
| 19 | +/************************************************************************************** |
| 20 | + * SETUP/LOOP |
| 21 | + **************************************************************************************/ |
| 22 | + |
| 23 | +void setup() |
| 24 | +{ |
| 25 | + Serial.begin(115200); |
| 26 | + while (!Serial) { } |
| 27 | + delay(1000); |
| 28 | + |
| 29 | + /* I2C (Wire) is needed to obtain an individual MAC |
| 30 | + * address from the AT24MAC402 EEPROM located on the |
| 31 | + * Mikroe Two-Wire ETH click board. |
| 32 | + */ |
| 33 | + Wire.begin(); |
| 34 | + |
| 35 | + /* Obtain MAC address stored on EEPROM of Mikroe |
| 36 | + * Two-Wire ETH Click board. |
| 37 | + */ |
| 38 | + MacAddress mac_addr; |
| 39 | + if (!get_mac_address(mac_addr.data())) { |
| 40 | + Serial.println("'get_mac_address(...)' failed."); |
| 41 | + for(;;) { } |
| 42 | + } |
| 43 | + |
| 44 | + Serial.print("AT24MAC402 MAC address: "); |
| 45 | + Serial.println(mac_addr); |
| 46 | +} |
| 47 | + |
| 48 | +void loop() { |
| 49 | +} |
| 50 | + |
| 51 | +static bool get_mac_address(uint8_t * p_mac) |
| 52 | +{ |
| 53 | + static uint8_t const MAC_EEPROM_I2C_SLAVE_ADDR = 0x58; |
| 54 | + static uint8_t const MAC_EEPROM_EUI_REG_ADDR = 0x9A; |
| 55 | + static uint8_t const MAC_EEPROM_MAC_SIZE = 6; |
| 56 | + bool success = false; |
| 57 | + |
| 58 | + Wire.beginTransmission(MAC_EEPROM_I2C_SLAVE_ADDR); |
| 59 | + Wire.write(MAC_EEPROM_EUI_REG_ADDR); |
| 60 | + Wire.endTransmission(); |
| 61 | + |
| 62 | + Wire.requestFrom(MAC_EEPROM_I2C_SLAVE_ADDR, MAC_EEPROM_MAC_SIZE); |
| 63 | + |
| 64 | + uint32_t const start = millis(); |
| 65 | + |
| 66 | + size_t bytes_read = 0; |
| 67 | + while (bytes_read < MAC_EEPROM_MAC_SIZE && ((millis() - start) < 1000)) { |
| 68 | + if (Wire.available()) { |
| 69 | + p_mac[bytes_read] = Wire.read(); |
| 70 | + bytes_read++; |
| 71 | + } |
| 72 | + } |
| 73 | + |
| 74 | + success = (bytes_read == MAC_EEPROM_MAC_SIZE); |
| 75 | + return success; |
| 76 | +} |
0 commit comments