Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: align MAC address api with Arduino reference #89

Merged
merged 3 commits into from
Sep 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ extend the default one by adding some extra configuration in a file named `lwipo

## New alternative init procedure **!!!**

There are alternative inits of the Ethernetinterface with following orders:
There are alternative inits of the Ethernet interface with following orders:

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

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

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

uint8_t *mac;
Ethernet.begin();
mac = Ethernet.MACAddress();
Ethernet.MACAddress(mac);

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

uint8_t newMAC[] = {0x00, 0x80, 0xE1, 0x01, 0x01, 0x01};
Ethernet.MACAddress(newMAC);
Ethernet.setMACAddress(newMAC);
Ethernet.begin();

## Note
Expand Down
2 changes: 2 additions & 0 deletions keywords.txt
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,11 @@ localPort KEYWORD2
maintain KEYWORD2
linkStatus KEYWORD2
MACAddress KEYWORD2
setMACAddress KEYWORD2
subnetMask KEYWORD2
gatewayIP KEYWORD2
dnsServerIP KEYWORD2
setDnsServerIP KEYWORD2
setConnectionTimeout KEYWORD2

#######################################
Expand Down
8 changes: 6 additions & 2 deletions src/Dhcp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,12 @@ int DhcpClass::beginWithDHCP(uint8_t *mac, unsigned long timeout, unsigned long
// zero out _dhcpMacAddr
memset(_dhcpMacAddr, 0, 6);
reset_DHCP_lease();

memcpy((void *)_dhcpMacAddr, (void *)mac, 6);
if (mac == NULL) {
// use mac from Ethernet chip
stm32_eth_get_macaddr(_dhcpMacAddr);
} else {
memcpy((void *)_dhcpMacAddr, (void *)mac, 6);
}
_dhcp_state = STATE_DHCP_START;
stm32_set_DHCP_state(_dhcp_state);
return request_DHCP_lease();
Expand Down
39 changes: 9 additions & 30 deletions src/STM32Ethernet.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@ int EthernetClass::begin(unsigned long timeout, unsigned long responseTimeout)
{
static DhcpClass s_dhcp;
_dhcp = &s_dhcp;
stm32_eth_init(MACAddressDefault(), NULL, NULL, NULL);
stm32_eth_init(NULL, NULL, NULL, NULL);

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

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

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

void EthernetClass::begin(uint8_t *mac, IPAddress local_ip, IPAddress dns_server, IPAddress gateway, IPAddress subnet)
void EthernetClass::begin(uint8_t *mac_address, IPAddress local_ip, IPAddress dns_server, IPAddress gateway, IPAddress subnet)
{
stm32_eth_init(mac, local_ip.raw_address(), gateway.raw_address(), subnet.raw_address());
stm32_eth_init(mac_address, local_ip.raw_address(), gateway.raw_address(), subnet.raw_address());
/* If there is a local DHCP informs it of our manual IP configuration to
prevent IP conflict */
stm32_DHCP_manual_config();
_dnsServerAddress = dns_server;
MACAddress(mac);
}

EthernetLinkStatus EthernetClass::linkStatus()
Expand Down Expand Up @@ -133,33 +131,14 @@ void EthernetClass::schedule(void)
stm32_eth_scheduler();
}

uint8_t *EthernetClass::MACAddressDefault(void)
{
if ((mac_address[0] + mac_address[1] + mac_address[2] + mac_address[3] + mac_address[4] + mac_address[5]) == 0) {
uint32_t baseUID = *(uint32_t *)UID_BASE;
mac_address[0] = 0x00;
mac_address[1] = 0x80;
mac_address[2] = 0xE1;
mac_address[3] = (baseUID & 0x00FF0000) >> 16;
mac_address[4] = (baseUID & 0x0000FF00) >> 8;
mac_address[5] = (baseUID & 0x000000FF);
}
return mac_address;
}

void EthernetClass::MACAddress(uint8_t *mac)
void EthernetClass::setMACAddress(const uint8_t *mac_address)
{
mac_address[0] = mac[0];
mac_address[1] = mac[1];
mac_address[2] = mac[2];
mac_address[3] = mac[3];
mac_address[4] = mac[4];
mac_address[5] = mac[5];
stm32_eth_set_macaddr(mac_address);
}

uint8_t *EthernetClass::MACAddress(void)
void EthernetClass::MACAddress(uint8_t *mac_address)
{
return mac_address;
stm32_eth_get_macaddr(mac_address);
}

IPAddress EthernetClass::localIP()
Expand Down
6 changes: 2 additions & 4 deletions src/STM32Ethernet.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,6 @@ class EthernetClass {
private:
IPAddress _dnsServerAddress;
DhcpClass *_dhcp;
uint8_t mac_address[6];
uint8_t *MACAddressDefault(void);

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

void MACAddress(uint8_t *mac);
uint8_t *MACAddress(void);
void MACAddress(uint8_t *mac_address);
IPAddress localIP();
IPAddress subnetMask();
IPAddress gatewayIP();
IPAddress dnsServerIP();

void setMACAddress(const uint8_t *mac_address);
void setDnsServerIP(const IPAddress dns_server);

friend class EthernetClient;
Expand Down
33 changes: 32 additions & 1 deletion src/utility/ethernetif.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,25 @@ __ALIGN_BEGIN uint8_t Tx_Buff[ETH_TXBUFNB][ETH_TX_BUF_SIZE] __ALIGN_END; /* Ethe

static ETH_HandleTypeDef EthHandle;

/* If default MAC fields is not defined use default values based on UID */
#if !defined(MAC_ADDR0)
#define MAC_ADDR0 0x00
#endif
#if !defined(MAC_ADDR1)
#define MAC_ADDR1 0x80
#endif
#if !defined(MAC_ADDR2)
#define MAC_ADDR2 0xE1
#endif
#if !defined(MAC_ADDR3)
#define MAC_ADDR3 ((uint8_t)(((*(uint32_t *)UID_BASE) & 0x00FF0000) >> 16))
#endif
#if !defined(MAC_ADDR4)
#define MAC_ADDR4 ((uint8_t)(((*(uint32_t *)UID_BASE) & 0x0000FF00) >> 8))
#endif
#if !defined(MAC_ADDR5)
#define MAC_ADDR5 ((uint8_t)((*(uint32_t *)UID_BASE) & 0x000000FF))
#endif
static uint8_t macaddress[6] = { MAC_ADDR0, MAC_ADDR1, MAC_ADDR2, MAC_ADDR3, MAC_ADDR4, MAC_ADDR5 };

#if LWIP_IGMP
Expand Down Expand Up @@ -608,11 +627,23 @@ __weak void ethernetif_notify_conn_changed(struct netif *netif)
*/
void ethernetif_set_mac_addr(const uint8_t *mac)
{
if (mac != NULL) {
if ((mac != NULL) && !(ethernetif_is_init())) {
memcpy(macaddress, mac, 6);
}
}

/**
* @brief This function get the current MAC address.
* @param mac: mac address
* @retval None
*/
void ethernetif_get_mac_addr(uint8_t *mac)
{
if (mac != NULL) {
memcpy(mac, macaddress, 6);
}
}

#if LWIP_IGMP
err_t igmp_mac_filter(struct netif *netif, const ip4_addr_t *ip4_addr, netif_mac_filter_action action)
{
Expand Down
1 change: 1 addition & 0 deletions src/utility/ethernetif.h
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ void ethernetif_update_config(struct netif *netif);
void ethernetif_notify_conn_changed(struct netif *netif);

void ethernetif_set_mac_addr(const uint8_t *mac);
void ethernetif_get_mac_addr(uint8_t *mac);

#if LWIP_IGMP
err_t igmp_mac_filter(struct netif *netif, const ip4_addr_t *ip4_addr, netif_mac_filter_action action);
Expand Down
21 changes: 20 additions & 1 deletion src/utility/stm32_eth.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ static void TIM_scheduler_Config(void);
#endif

/**
* @brief Configurates the network interface
* @brief Configure the network interface
* @param None
* @retval None
*/
Expand Down Expand Up @@ -268,6 +268,25 @@ uint8_t stm32_eth_is_init(void)
return ethernetif_is_init();
}

/**
* @brief Set Ethernet MAC address
* @param mac: mac address
* @retval None
*/
void stm32_eth_set_macaddr(const uint8_t *mac)
{
ethernetif_set_mac_addr(mac);
}
/**
* @brief Return Ethernet MAC address
* @param mac: mac address
* @retval None
*/
void stm32_eth_get_macaddr(uint8_t *mac)
{
return ethernetif_get_mac_addr(mac);
}

/**
* @brief Return Ethernet link status
* @param None
Expand Down
2 changes: 2 additions & 0 deletions src/utility/stm32_eth.h
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,8 @@ struct tcp_struct {
/* Exported functions ------------------------------------------------------- */
void stm32_eth_init(const uint8_t *mac, const uint8_t *ip, const uint8_t *gw, const uint8_t *netmask);
uint8_t stm32_eth_is_init(void);
void stm32_eth_get_macaddr(uint8_t *mac);
void stm32_eth_set_macaddr(const uint8_t *mac);
uint8_t stm32_eth_link_up(void);
void stm32_eth_scheduler(void);

Expand Down