Skip to content

Commit 71acc48

Browse files
committed
1 parent 52aba52 commit 71acc48

File tree

4 files changed

+45
-32
lines changed

4 files changed

+45
-32
lines changed

README.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ extend the default one by adding some extra configuration in a file named `lwipo
2929

3030
## New alternative init procedure **!!!**
3131

32-
There are alternative inits of the Ethernetinterface with following orders:
32+
There are alternative inits of the Ethernet interface with following orders:
3333

3434
Ethernet.begin();
3535
Ethernet.begin(ip);
@@ -39,16 +39,16 @@ There are alternative inits of the Ethernetinterface with following orders:
3939

4040
This is more logical. A MAC address is no more needed and will retrieved internally by the mbed MAC address!
4141

42-
You can get the MAC address with following function, this must done after Ethernet.Begin()
42+
You can get the MAC address with following function, this must be done after Ethernet.Begin()
4343

4444
uint8_t *mac;
4545
Ethernet.begin();
46-
mac = Ethernet.MACAddress();
46+
Ethernet.MACAddress(mac);
4747

48-
You can also set a new user based MAC address, this must done before Ethernet.begin()
48+
You can also set a new user based MAC address, this must be done before Ethernet.begin()
4949

5050
uint8_t newMAC[] = {0x00, 0x80, 0xE1, 0x01, 0x01, 0x01};
51-
Ethernet.MACAddress(newMAC);
51+
Ethernet.setMACAddress(newMAC);
5252
Ethernet.begin();
5353

5454
## Note

keywords.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,9 +38,11 @@ localPort KEYWORD2
3838
maintain KEYWORD2
3939
linkStatus KEYWORD2
4040
MACAddress KEYWORD2
41+
setMACAddress KEYWORD2
4142
subnetMask KEYWORD2
4243
gatewayIP KEYWORD2
4344
dnsServerIP KEYWORD2
45+
setDnsServerIP KEYWORD2
4446
setConnectionTimeout KEYWORD2
4547

4648
#######################################

src/STM32Ethernet.cpp

Lines changed: 34 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,19 @@
11
#include "STM32Ethernet.h"
22
#include "Dhcp.h"
33

4+
EthernetClass::EthernetClass()
5+
{
6+
MACAddressDefault();
7+
}
8+
49
int EthernetClass::begin(unsigned long timeout, unsigned long responseTimeout)
510
{
611
static DhcpClass s_dhcp;
712
_dhcp = &s_dhcp;
8-
stm32_eth_init(MACAddressDefault(), NULL, NULL, NULL);
13+
stm32_eth_init(_mac_address, NULL, NULL, NULL);
914

1015
// Now try to get our config info from a DHCP server
11-
int ret = _dhcp->beginWithDHCP(mac_address, timeout, responseTimeout);
16+
int ret = _dhcp->beginWithDHCP(_mac_address, timeout, responseTimeout);
1217
if (ret == 1) {
1318
_dnsServerAddress = _dhcp->getDnsServerIp();
1419
}
@@ -39,7 +44,7 @@ void EthernetClass::begin(IPAddress local_ip, IPAddress subnet, IPAddress gatewa
3944

4045
void EthernetClass::begin(IPAddress local_ip, IPAddress subnet, IPAddress gateway, IPAddress dns_server)
4146
{
42-
stm32_eth_init(MACAddressDefault(), local_ip.raw_address(), gateway.raw_address(), subnet.raw_address());
47+
stm32_eth_init(_mac_address, local_ip.raw_address(), gateway.raw_address(), subnet.raw_address());
4348
/* If there is a local DHCP informs it of our manual IP configuration to
4449
prevent IP conflict */
4550
stm32_DHCP_manual_config();
@@ -58,7 +63,7 @@ int EthernetClass::begin(uint8_t *mac_address, unsigned long timeout, unsigned l
5863
if (ret == 1) {
5964
_dnsServerAddress = _dhcp->getDnsServerIp();
6065
}
61-
MACAddress(mac_address);
66+
setMACAddress(mac_address);
6267
return ret;
6368
}
6469

@@ -86,14 +91,14 @@ void EthernetClass::begin(uint8_t *mac_address, IPAddress local_ip, IPAddress dn
8691
begin(mac_address, local_ip, dns_server, gateway, subnet);
8792
}
8893

89-
void EthernetClass::begin(uint8_t *mac, IPAddress local_ip, IPAddress dns_server, IPAddress gateway, IPAddress subnet)
94+
void EthernetClass::begin(uint8_t *mac_address, IPAddress local_ip, IPAddress dns_server, IPAddress gateway, IPAddress subnet)
9095
{
91-
stm32_eth_init(mac, local_ip.raw_address(), gateway.raw_address(), subnet.raw_address());
96+
stm32_eth_init(mac_address, local_ip.raw_address(), gateway.raw_address(), subnet.raw_address());
9297
/* If there is a local DHCP informs it of our manual IP configuration to
9398
prevent IP conflict */
9499
stm32_DHCP_manual_config();
95100
_dnsServerAddress = dns_server;
96-
MACAddress(mac);
101+
setMACAddress(mac_address);
97102
}
98103

99104
EthernetLinkStatus EthernetClass::linkStatus()
@@ -135,31 +140,36 @@ void EthernetClass::schedule(void)
135140

136141
uint8_t *EthernetClass::MACAddressDefault(void)
137142
{
138-
if ((mac_address[0] + mac_address[1] + mac_address[2] + mac_address[3] + mac_address[4] + mac_address[5]) == 0) {
143+
if ((_mac_address[0] + _mac_address[1] + _mac_address[2] + _mac_address[3] + _mac_address[4] + _mac_address[5]) == 0) {
139144
uint32_t baseUID = *(uint32_t *)UID_BASE;
140-
mac_address[0] = 0x00;
141-
mac_address[1] = 0x80;
142-
mac_address[2] = 0xE1;
143-
mac_address[3] = (baseUID & 0x00FF0000) >> 16;
144-
mac_address[4] = (baseUID & 0x0000FF00) >> 8;
145-
mac_address[5] = (baseUID & 0x000000FF);
145+
_mac_address[0] = 0x00;
146+
_mac_address[1] = 0x80;
147+
_mac_address[2] = 0xE1;
148+
_mac_address[3] = (baseUID & 0x00FF0000) >> 16;
149+
_mac_address[4] = (baseUID & 0x0000FF00) >> 8;
150+
_mac_address[5] = (baseUID & 0x000000FF);
146151
}
147-
return mac_address;
152+
return _mac_address;
148153
}
149154

150-
void EthernetClass::MACAddress(uint8_t *mac)
155+
void EthernetClass::setMACAddress(const uint8_t *mac_address)
151156
{
152-
mac_address[0] = mac[0];
153-
mac_address[1] = mac[1];
154-
mac_address[2] = mac[2];
155-
mac_address[3] = mac[3];
156-
mac_address[4] = mac[4];
157-
mac_address[5] = mac[5];
157+
_mac_address[0] = mac_address[0];
158+
_mac_address[1] = mac_address[1];
159+
_mac_address[2] = mac_address[2];
160+
_mac_address[3] = mac_address[3];
161+
_mac_address[4] = mac_address[4];
162+
_mac_address[5] = mac_address[5];
158163
}
159164

160-
uint8_t *EthernetClass::MACAddress(void)
165+
void EthernetClass::MACAddress(uint8_t *mac_address)
161166
{
162-
return mac_address;
167+
mac_address[0] = _mac_address[0];
168+
mac_address[1] = _mac_address[1];
169+
mac_address[2] = _mac_address[2];
170+
mac_address[3] = _mac_address[3];
171+
mac_address[4] = _mac_address[4];
172+
mac_address[5] = _mac_address[5];
163173
}
164174

165175
IPAddress EthernetClass::localIP()

src/STM32Ethernet.h

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,11 @@ class EthernetClass {
1717
private:
1818
IPAddress _dnsServerAddress;
1919
DhcpClass *_dhcp;
20-
uint8_t mac_address[6];
20+
uint8_t _mac_address[6];
2121
uint8_t *MACAddressDefault(void);
2222

2323
public:
24+
EthernetClass();
2425
// Initialise the Ethernet with the internal provided MAC address and gain the rest of the
2526
// configuration through DHCP.
2627
// Returns 0 if the DHCP configuration failed, and 1 if it succeeded
@@ -43,13 +44,13 @@ class EthernetClass {
4344
int maintain();
4445
void schedule(void);
4546

46-
void MACAddress(uint8_t *mac);
47-
uint8_t *MACAddress(void);
47+
void MACAddress(uint8_t *mac_address);
4848
IPAddress localIP();
4949
IPAddress subnetMask();
5050
IPAddress gatewayIP();
5151
IPAddress dnsServerIP();
5252

53+
void setMACAddress(const uint8_t *mac_address);
5354
void setDnsServerIP(const IPAddress dns_server);
5455

5556
friend class EthernetClient;

0 commit comments

Comments
 (0)