From 0370bec69d8a5051f14fbac1ec71daae5bd57231 Mon Sep 17 00:00:00 2001 From: Felix Rusu Date: Thu, 29 May 2014 14:32:12 -0400 Subject: [PATCH] Add readUniqueId() to get unique 64 bit MAC Reads 64 bit MAC unique ID --- SPIFlash.cpp | 21 +++++++++++++++++++++ SPIFlash.h | 6 +++++- keywords.txt | 2 ++ 3 files changed, 28 insertions(+), 1 deletion(-) diff --git a/SPIFlash.cpp b/SPIFlash.cpp index 906a795..a0538da 100644 --- a/SPIFlash.cpp +++ b/SPIFlash.cpp @@ -14,6 +14,8 @@ #include +byte SPIFlash::UNIQUEID[8]; + /// IMPORTANT: NAND FLASH memory requires erase before write, because /// it can only transition from 1s to 0s and only the erase command can reset all 0s to 1s /// See http://en.wikipedia.org/wiki/Flash_memory @@ -74,6 +76,25 @@ word SPIFlash::readDeviceId() return jedecid; } +/// Get the 64 bit unique identifier, stores it in UNIQUEID[8]. Only needs to be called once, ie after initialize +/// Returns the byte pointer to the UNIQUEID byte array +/// Read UNIQUEID like this: +/// flash.readUniqueId(); for (byte i=0;i<8;i++) { Serial.print(flash.UNIQUEID[i], HEX); Serial.print(' '); } +/// or like this: +/// flash.readUniqueId(); byte* MAC = flash.readUniqueId(); for (byte i=0;i<8;i++) { Serial.print(MAC[i], HEX); Serial.print(' '); } +byte* SPIFlash::readUniqueId() +{ + command(SPIFLASH_MACREAD); + SPI.transfer(0); + SPI.transfer(0); + SPI.transfer(0); + SPI.transfer(0); + for (byte i=0;i<8;i++) + UNIQUEID[i] = SPI.transfer(0); + unselect(); + return UNIQUEID; +} + /// read 1 byte from flash memory byte SPIFlash::readByte(long addr) { command(SPIFLASH_ARRAYREADLOWFREQ); diff --git a/SPIFlash.h b/SPIFlash.h index 5432ce3..be049d4 100644 --- a/SPIFlash.h +++ b/SPIFlash.h @@ -65,9 +65,11 @@ #define SPIFLASH_IDREAD 0x9F // read JEDEC manufacturer and device ID (2 bytes, specific bytes for each manufacturer and device) // Example for Atmel-Adesto 4Mbit AT25DF041A: 0x1F44 (page 27: http://www.adestotech.com/sites/default/files/datasheets/doc3668.pdf) // Example for Winbond 4Mbit W25X40CL: 0xEF30 (page 14: http://www.winbond.com/NR/rdonlyres/6E25084C-0BFE-4B25-903D-AE10221A0929/0/W25X40CL.pdf) - +#define SPIFLASH_MACREAD 0x4B // read unique ID number (MAC) + class SPIFlash { public: + static byte UNIQUEID[8]; SPIFlash(byte slaveSelectPin, uint16_t jedecID=0); boolean initialize(); void command(byte cmd, boolean isWrite=false); @@ -81,6 +83,8 @@ public: void blockErase4K(long address); void blockErase32K(long address); word readDeviceId(); + byte* readUniqueId(); + void sleep(); void wakeup(); void end(); diff --git a/keywords.txt b/keywords.txt index 6c6903c..652da98 100644 --- a/keywords.txt +++ b/keywords.txt @@ -11,6 +11,8 @@ chipErase KEYWORD2 blockErase4K KEYWORD2 blockErase32K KEYWORD2 readDeviceId KEYWORD2 +readUniqueId KEYWORD2 +UNIQUEID KEYWORD2 sleep KEYWORD2 wakeup KEYWORD2 end KEYWORD2 \ No newline at end of file