
Originally Posted by
cmason
After a conversation with Paul, I think I've figured out how to access the unique
Ethernet media access (MAC) address burnt into the Teensy3. This number will be different on every teensy 3, and isn't overwritten by the bootloader flashing process like the EEPROM is.
Given this class:
------- snip -------
Hope this is useful.
-c
I made a few changes to this, here is the results:
Code:
// macAddress.h
#ifndef _MACADDR_H_
#define _MACADDR_H_
/** Retrieve Ethernet MAC from Teensy 3 */
/*
from http://forum.pjrc.com/threads/91-teensy-3-MAC-address
Edited by: Gene Reeves 04/23/2014
* added [] operator
* added uint8_t * operator for use in Ethernet.begin()
*/
class macAddress : public Printable {
protected:
uint8_t _m[6];
public:
macAddress() : _m({0}) {
// Retrieve the 6 byte MAC address Paul burnt into two 32 bit words
// at the end of the "READ ONCE" area of the flash controller.
read(0xe,0);
read(0xf,3);
}
// gives access to individual elements
uint8_t operator[](int index) const {return _m[index];}
uint8_t& operator[](int index) {return _m[index];}
// gives access to uint8_t array, use like this: Ethernet.begin(mac_address)
operator uint8_t *() {return &(_m[0]);}
void read(uint8_t word, uint8_t loc) {
// To understand what's going on here, see
// "Kinetis Peripheral Module Quick Reference" page 85 and
// "K20 Sub-Family Reference Manual" page 548.
cli();
FTFL_FCCOB0 = 0x41; // Selects the READONCE command
FTFL_FCCOB1 = word; // read the given word of read once area
// -- this is one half of the mac addr.
FTFL_FSTAT = FTFL_FSTAT_CCIF; // Launch command sequence
while(!(FTFL_FSTAT & FTFL_FSTAT_CCIF)) {
// Wait for command completion
}
*(_m+loc) = FTFL_FCCOB5; // collect only the top three bytes,
*(_m+loc+1) = FTFL_FCCOB6; // in the right orientation (big endian).
*(_m+loc+2) = FTFL_FCCOB7; // Skip FTFL_FCCOB4 as it's always 0.
sei();
}
virtual size_t printTo(Print & p) const {
size_t count = 0;
for(uint8_t i = 0; i < 6; ++i) {
if (i!=0) count += p.print(":");
count += p.print((*(_m+i) & 0xF0) >> 4, 16);
count += p.print(*(_m+i) & 0x0F, 16);
}
return count;
}
friend class EthernetClass;
friend class UDP;
friend class Client;
friend class Server;
friend class DhcpClass;
friend class DNSClient;
};
#endif
with the above code you can do this:
Code:
macAddress myMac;
// print our MAC
Serial.print("My MAC is ");
Serial.println(myMac);
Serial.println("Starting Network using DHCP.");
if (Ethernet.begin(myMac)==0)
{
Serial.println("Failed to acquire IP address using DHCP, halting.");
while(1) {;}
}
Serial.print("My IP is ");
Serial.println(Ethernet.localIP());
//
// rest of your code here
Hope someone finds this useful. 
if you see anything that does not look correct, please post a reply,
Gene_R