Skip to content

Commit d8e484d

Browse files
authored
Merge pull request #2812 from jeromecoutant/PR_Add_MAC
[STM32 NUCLEO] Init MAC address
2 parents 2dd5571 + 7ec5de8 commit d8e484d

File tree

2 files changed

+47
-1
lines changed

2 files changed

+47
-1
lines changed

features/net/FEATURE_IPV4/lwip-interface/lwip-eth/arch/TARGET_STM/TARGET_STM32F4/TARGET_UBLOX_C029/stm32f4_eth_init.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ void HAL_ETH_MspDeInit(ETH_HandleTypeDef* heth)
119119
}
120120
}
121121

122-
void mbed_mac_address(char *mac)
122+
uint8_t mbed_otp_mac_address(char *mac)
123123
{
124124
C029_OTP_Header *pFound = NULL;
125125
C029_OTP_Header *pTemp = (C029_OTP_Header*)C029_OTP_START_ADDRESS;
@@ -140,4 +140,6 @@ void mbed_mac_address(char *mac)
140140
}
141141
}
142142
memcpy(mac, _macAddr, 6);
143+
144+
return 1;
143145
}

features/net/FEATURE_IPV4/lwip-interface/lwip-eth/arch/TARGET_STM/stm32xx_emac.c

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,8 @@ static err_t _eth_arch_netif_output_ipv6(struct netif *netif, struct pbuf *q, co
5050

5151
static err_t _eth_arch_low_level_output(struct netif *netif, struct pbuf *p);
5252
static struct pbuf * _eth_arch_low_level_input(struct netif *netif);
53+
__weak uint8_t mbed_otp_mac_address(char *mac);
54+
void mbed_default_mac_address(char *mac);
5355

5456
/**
5557
* Ethernet Rx Transfer completed callback
@@ -468,3 +470,45 @@ void eth_arch_disable_interrupts(void)
468470
{
469471
NVIC_DisableIRQ(ETH_IRQn);
470472
}
473+
474+
/** This returns a unique 6-byte MAC address, based on the device UID
475+
* This function overrides hal/common/mbed_interface.c function
476+
* @param mac A 6-byte array to write the MAC address
477+
*/
478+
479+
void mbed_mac_address(char *mac) {
480+
if (mbed_otp_mac_address(mac)) {
481+
return;
482+
} else {
483+
mbed_default_mac_address(mac);
484+
}
485+
return;
486+
}
487+
488+
__weak uint8_t mbed_otp_mac_address(char *mac) {
489+
return 0;
490+
}
491+
492+
void mbed_default_mac_address(char *mac) {
493+
unsigned char ST_mac_addr[3] = {0x00, 0x80, 0xe1}; // default STMicro mac address
494+
495+
// Read unic id
496+
#if defined (TARGET_STM32F2)
497+
uint32_t word0 = *(uint32_t *)0x1FFF7A10;
498+
#elif defined (TARGET_STM32F4)
499+
uint32_t word0 = *(uint32_t *)0x1FFF7A10;
500+
#elif defined (TARGET_STM32F7)
501+
uint32_t word0 = *(uint32_t *)0x1FF0F420;
502+
#else
503+
#error MAC address can not be derived from target unique Id
504+
#endif
505+
506+
mac[0] = ST_mac_addr[0];
507+
mac[1] = ST_mac_addr[1];
508+
mac[2] = ST_mac_addr[2];
509+
mac[3] = (word0 & 0x00ff0000) >> 16;
510+
mac[4] = (word0 & 0x0000ff00) >> 8;
511+
mac[5] = (word0 & 0x000000ff);
512+
513+
return;
514+
}

0 commit comments

Comments
 (0)