Skip to content

Commit

Permalink
Added method to set the EEPROM offset where network configuration dat…
Browse files Browse the repository at this point in the history
…a will be stored.

Existing NET_EEPROM_OFFSET #define removed as it never worked.
  • Loading branch information
gregington committed Feb 24, 2016
1 parent c3a7eaf commit 236c6d5
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 12 deletions.
20 changes: 19 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
NetEEPROM
=========

Arduino library that reads stores network settings to EEPROM.
Arduino library that reads stores network settings to EEPROM. This is
useful to associate a network configuration with a specific Arduino
board which can then be read by many sketches without having to be
set in each sketch.

This library stores to EEPROM:
* the MAC address of the interface
Expand Down Expand Up @@ -47,3 +50,18 @@ following line:

NetEeprom.begin();

If you wish to configure the start address in EEPROM where the network
configuration resides, call the `setEepromOffset()` following method
**before** calling any other methods. If you do not call this method, then
the default EEPROM location set set to 0. To set the network configuration at
a custom EEPROM offset, use the following code.

NetEeprom.setEepromOffet(128);
byte mac[6] = { 0xCA, 0xFE, 0xBA, 0xBE, 0x00, 0x00 };
NetEeprom.writeDhcpConfig(mac);

To read network configuration at a custom EEPROM offset, use the following
code:

NetEeprom.setEepromOffset(128);
NetEeprom.begin();
1 change: 1 addition & 0 deletions keywords.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
NetEEPROM KEYWORD1
setEepromOffset KEYWORD2
begin KEYWORD2
writeDhcpConfig KEYWORD2
writeManualConfig KEYWORD2
Expand Down
14 changes: 11 additions & 3 deletions src/NetEEPROM.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,21 @@
#include "Ethernet.h"


#define MAC_OFFSET (NET_EEPROM_OFFSET + 1)
#define MAC_OFFSET (eepromOffset + 1)
#define DHCP_OFFSET (MAC_OFFSET + 6)
#define IP_OFFSET (DHCP_OFFSET + 1)
#define DNS_OFFSET (IP_OFFSET + 4)
#define GW_OFFSET (DNS_OFFSET + 4)
#define SUBNET_OFFSET (GW_OFFSET + 4)

NetEEPROM::NetEEPROM(void) {
eepromOffset = 0;
}

void NetEEPROM::setEepromOffset(int offset) {
eepromOffset = offset;
}

void NetEEPROM::begin() {
delay(250); // For ethernet instantiation
byte mac[6];
Expand Down Expand Up @@ -76,7 +84,7 @@ void NetEEPROM::readSubnet(byte subnet[]) {
}

int NetEEPROM::checkMagic() {
return EEPROM.read(NET_EEPROM_OFFSET) == NET_EEPROM_MAGIC;
return EEPROM.read(eepromOffset) == NET_EEPROM_MAGIC;
}

void NetEEPROM::generateRandomMac(byte mac[]) {
Expand All @@ -102,7 +110,7 @@ void NetEEPROM::writeEEPROM(byte data[], int offset, int length) {
}

void NetEEPROM::writeMac(byte mac[]) {
EEPROM.write(NET_EEPROM_OFFSET, NET_EEPROM_MAGIC);
EEPROM.write(eepromOffset, NET_EEPROM_MAGIC);
writeEEPROM(mac, MAC_OFFSET, 6);
}

Expand Down
18 changes: 10 additions & 8 deletions src/NetEEPROM.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,6 @@
* 24 bytes of EEPROM are required to store the configuration.
*/

/* The EEPROM offset at which the network configuration starts.
* Sketches can override this by defining NET_EEPROM_OFFSET before
* importing NetEEPROM.h.
*/
#ifndef NET_EEPROM_OFFSET
#define NET_EEPROM_OFFSET 0
#endif

/* The pin from which an anlog reading will be taken to generate a random MAC. */
#ifndef NET_RANDOM_ANALOG_PIN
#define NET_RANDOM_ANALOG_PIN 0
Expand All @@ -31,6 +23,15 @@
class NetEEPROM {

public:

NetEEPROM();

/* Sets the location in EEPROM that network configuration data will be
* stored. If not called, then it defaults to zero. This method should
* generally be called before any other methods.
*/
void setEepromOffset(int offset);

/* Configures the network adapter from settings stored in EEPROM.
* If no settings can be found in EEPROM then a random MAC address
* will be generated and the adapter will use DHCP for IP configuration,
Expand Down Expand Up @@ -97,6 +98,7 @@ class NetEEPROM {
void readSubnet(byte subnet[]);

private:
int eepromOffset;
void generateRandomMac(byte mac[]);
void readEEPROM(byte data[], int offset, int length);
void writeEEPROM(byte data[], int offset, int length);
Expand Down

0 comments on commit 236c6d5

Please sign in to comment.