Skip to content

Commit 055af9a

Browse files
authored
Merge pull request #89 from fpistm/MACAddress
fix: align MAC address api with Arduino reference
2 parents 52aba52 + 348c968 commit 055af9a

9 files changed

+79
-43
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/Dhcp.cpp

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,12 @@ int DhcpClass::beginWithDHCP(uint8_t *mac, unsigned long timeout, unsigned long
1616
// zero out _dhcpMacAddr
1717
memset(_dhcpMacAddr, 0, 6);
1818
reset_DHCP_lease();
19-
20-
memcpy((void *)_dhcpMacAddr, (void *)mac, 6);
19+
if (mac == NULL) {
20+
// use mac from Ethernet chip
21+
stm32_eth_get_macaddr(_dhcpMacAddr);
22+
} else {
23+
memcpy((void *)_dhcpMacAddr, (void *)mac, 6);
24+
}
2125
_dhcp_state = STATE_DHCP_START;
2226
stm32_set_DHCP_state(_dhcp_state);
2327
return request_DHCP_lease();

src/STM32Ethernet.cpp

Lines changed: 9 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,10 @@ int EthernetClass::begin(unsigned long timeout, unsigned long responseTimeout)
55
{
66
static DhcpClass s_dhcp;
77
_dhcp = &s_dhcp;
8-
stm32_eth_init(MACAddressDefault(), NULL, NULL, NULL);
8+
stm32_eth_init(NULL, NULL, NULL, NULL);
99

1010
// Now try to get our config info from a DHCP server
11-
int ret = _dhcp->beginWithDHCP(mac_address, timeout, responseTimeout);
11+
int ret = _dhcp->beginWithDHCP(NULL, timeout, responseTimeout);
1212
if (ret == 1) {
1313
_dnsServerAddress = _dhcp->getDnsServerIp();
1414
}
@@ -39,7 +39,7 @@ void EthernetClass::begin(IPAddress local_ip, IPAddress subnet, IPAddress gatewa
3939

4040
void EthernetClass::begin(IPAddress local_ip, IPAddress subnet, IPAddress gateway, IPAddress dns_server)
4141
{
42-
stm32_eth_init(MACAddressDefault(), local_ip.raw_address(), gateway.raw_address(), subnet.raw_address());
42+
stm32_eth_init(NULL, local_ip.raw_address(), gateway.raw_address(), subnet.raw_address());
4343
/* If there is a local DHCP informs it of our manual IP configuration to
4444
prevent IP conflict */
4545
stm32_DHCP_manual_config();
@@ -58,7 +58,6 @@ int EthernetClass::begin(uint8_t *mac_address, unsigned long timeout, unsigned l
5858
if (ret == 1) {
5959
_dnsServerAddress = _dhcp->getDnsServerIp();
6060
}
61-
MACAddress(mac_address);
6261
return ret;
6362
}
6463

@@ -86,14 +85,13 @@ void EthernetClass::begin(uint8_t *mac_address, IPAddress local_ip, IPAddress dn
8685
begin(mac_address, local_ip, dns_server, gateway, subnet);
8786
}
8887

89-
void EthernetClass::begin(uint8_t *mac, IPAddress local_ip, IPAddress dns_server, IPAddress gateway, IPAddress subnet)
88+
void EthernetClass::begin(uint8_t *mac_address, IPAddress local_ip, IPAddress dns_server, IPAddress gateway, IPAddress subnet)
9089
{
91-
stm32_eth_init(mac, local_ip.raw_address(), gateway.raw_address(), subnet.raw_address());
90+
stm32_eth_init(mac_address, local_ip.raw_address(), gateway.raw_address(), subnet.raw_address());
9291
/* If there is a local DHCP informs it of our manual IP configuration to
9392
prevent IP conflict */
9493
stm32_DHCP_manual_config();
9594
_dnsServerAddress = dns_server;
96-
MACAddress(mac);
9795
}
9896

9997
EthernetLinkStatus EthernetClass::linkStatus()
@@ -133,33 +131,14 @@ void EthernetClass::schedule(void)
133131
stm32_eth_scheduler();
134132
}
135133

136-
uint8_t *EthernetClass::MACAddressDefault(void)
137-
{
138-
if ((mac_address[0] + mac_address[1] + mac_address[2] + mac_address[3] + mac_address[4] + mac_address[5]) == 0) {
139-
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);
146-
}
147-
return mac_address;
148-
}
149-
150-
void EthernetClass::MACAddress(uint8_t *mac)
134+
void EthernetClass::setMACAddress(const uint8_t *mac_address)
151135
{
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];
136+
stm32_eth_set_macaddr(mac_address);
158137
}
159138

160-
uint8_t *EthernetClass::MACAddress(void)
139+
void EthernetClass::MACAddress(uint8_t *mac_address)
161140
{
162-
return mac_address;
141+
stm32_eth_get_macaddr(mac_address);
163142
}
164143

165144
IPAddress EthernetClass::localIP()

src/STM32Ethernet.h

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

2321
public:
2422
// Initialise the Ethernet with the internal provided MAC address and gain the rest of the
@@ -43,13 +41,13 @@ class EthernetClass {
4341
int maintain();
4442
void schedule(void);
4543

46-
void MACAddress(uint8_t *mac);
47-
uint8_t *MACAddress(void);
44+
void MACAddress(uint8_t *mac_address);
4845
IPAddress localIP();
4946
IPAddress subnetMask();
5047
IPAddress gatewayIP();
5148
IPAddress dnsServerIP();
5249

50+
void setMACAddress(const uint8_t *mac_address);
5351
void setDnsServerIP(const IPAddress dns_server);
5452

5553
friend class EthernetClient;

src/utility/ethernetif.cpp

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,25 @@ __ALIGN_BEGIN uint8_t Tx_Buff[ETH_TXBUFNB][ETH_TX_BUF_SIZE] __ALIGN_END; /* Ethe
9696

9797
static ETH_HandleTypeDef EthHandle;
9898

99+
/* If default MAC fields is not defined use default values based on UID */
100+
#if !defined(MAC_ADDR0)
101+
#define MAC_ADDR0 0x00
102+
#endif
103+
#if !defined(MAC_ADDR1)
104+
#define MAC_ADDR1 0x80
105+
#endif
106+
#if !defined(MAC_ADDR2)
107+
#define MAC_ADDR2 0xE1
108+
#endif
109+
#if !defined(MAC_ADDR3)
110+
#define MAC_ADDR3 ((uint8_t)(((*(uint32_t *)UID_BASE) & 0x00FF0000) >> 16))
111+
#endif
112+
#if !defined(MAC_ADDR4)
113+
#define MAC_ADDR4 ((uint8_t)(((*(uint32_t *)UID_BASE) & 0x0000FF00) >> 8))
114+
#endif
115+
#if !defined(MAC_ADDR5)
116+
#define MAC_ADDR5 ((uint8_t)((*(uint32_t *)UID_BASE) & 0x000000FF))
117+
#endif
99118
static uint8_t macaddress[6] = { MAC_ADDR0, MAC_ADDR1, MAC_ADDR2, MAC_ADDR3, MAC_ADDR4, MAC_ADDR5 };
100119

101120
#if LWIP_IGMP
@@ -608,11 +627,23 @@ __weak void ethernetif_notify_conn_changed(struct netif *netif)
608627
*/
609628
void ethernetif_set_mac_addr(const uint8_t *mac)
610629
{
611-
if (mac != NULL) {
630+
if ((mac != NULL) && !(ethernetif_is_init())) {
612631
memcpy(macaddress, mac, 6);
613632
}
614633
}
615634

635+
/**
636+
* @brief This function get the current MAC address.
637+
* @param mac: mac address
638+
* @retval None
639+
*/
640+
void ethernetif_get_mac_addr(uint8_t *mac)
641+
{
642+
if (mac != NULL) {
643+
memcpy(mac, macaddress, 6);
644+
}
645+
}
646+
616647
#if LWIP_IGMP
617648
err_t igmp_mac_filter(struct netif *netif, const ip4_addr_t *ip4_addr, netif_mac_filter_action action)
618649
{

src/utility/ethernetif.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ void ethernetif_update_config(struct netif *netif);
6161
void ethernetif_notify_conn_changed(struct netif *netif);
6262

6363
void ethernetif_set_mac_addr(const uint8_t *mac);
64+
void ethernetif_get_mac_addr(uint8_t *mac);
6465

6566
#if LWIP_IGMP
6667
err_t igmp_mac_filter(struct netif *netif, const ip4_addr_t *ip4_addr, netif_mac_filter_action action);

src/utility/stm32_eth.cpp

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ static void TIM_scheduler_Config(void);
117117
#endif
118118

119119
/**
120-
* @brief Configurates the network interface
120+
* @brief Configure the network interface
121121
* @param None
122122
* @retval None
123123
*/
@@ -268,6 +268,25 @@ uint8_t stm32_eth_is_init(void)
268268
return ethernetif_is_init();
269269
}
270270

271+
/**
272+
* @brief Set Ethernet MAC address
273+
* @param mac: mac address
274+
* @retval None
275+
*/
276+
void stm32_eth_set_macaddr(const uint8_t *mac)
277+
{
278+
ethernetif_set_mac_addr(mac);
279+
}
280+
/**
281+
* @brief Return Ethernet MAC address
282+
* @param mac: mac address
283+
* @retval None
284+
*/
285+
void stm32_eth_get_macaddr(uint8_t *mac)
286+
{
287+
return ethernetif_get_mac_addr(mac);
288+
}
289+
271290
/**
272291
* @brief Return Ethernet link status
273292
* @param None

src/utility/stm32_eth.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,8 @@ struct tcp_struct {
119119
/* Exported functions ------------------------------------------------------- */
120120
void stm32_eth_init(const uint8_t *mac, const uint8_t *ip, const uint8_t *gw, const uint8_t *netmask);
121121
uint8_t stm32_eth_is_init(void);
122+
void stm32_eth_get_macaddr(uint8_t *mac);
123+
void stm32_eth_set_macaddr(const uint8_t *mac);
122124
uint8_t stm32_eth_link_up(void);
123125
void stm32_eth_scheduler(void);
124126

0 commit comments

Comments
 (0)