Skip to content

Commit 6d06f5f

Browse files
committed
fix(esp_eth): fix openeth driver to consider MAC address set in QEMU
Openeth driver did not consider the possibility that the MAC address was specified when launching QEMU, and would always overwrite that address with the address obtained from esp_read_mac. When running QEMU, setting the MAC address via QEMU arguments is more convenient than crafting an eFuse file with the correct MAC address. This change modifies openeth driver to first check if an address has been set in QEMU and uses it if so. Otherwise it falls back to the address obtained from esp_read_mac. As part of this change, also removed the unnecessary variable emac_opencores_t::addr, the address is only kept in the registers of the emulated peripheral now. For full effect this also requires changes in QEMU, see espressif/qemu#107 for background. Without changes in QEMU, this commit keeps the same behavior.
1 parent 58f2dd5 commit 6d06f5f

File tree

1 file changed

+24
-7
lines changed

1 file changed

+24
-7
lines changed

components/esp_eth/src/openeth/esp_eth_mac_openeth.c

Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,6 @@ typedef struct {
3838
TaskHandle_t rx_task_hdl;
3939
int cur_rx_desc;
4040
int cur_tx_desc;
41-
uint8_t addr[6];
4241
uint8_t *rx_buf[RX_BUF_COUNT];
4342
uint8_t *tx_buf[TX_BUF_COUNT];
4443
} emac_opencores_t;
@@ -143,8 +142,6 @@ static esp_err_t emac_opencores_set_addr(esp_eth_mac_t *mac, uint8_t *addr)
143142
ESP_LOGV(TAG, "%s: " MACSTR, __func__, MAC2STR(addr));
144143
esp_err_t ret = ESP_OK;
145144
ESP_GOTO_ON_FALSE(addr, ESP_ERR_INVALID_ARG, err, TAG, "can't set mac addr to null");
146-
emac_opencores_t *emac = __containerof(mac, emac_opencores_t, parent);
147-
memcpy(emac->addr, addr, 6);
148145
const uint8_t mac0[4] = {addr[5], addr[4], addr[3], addr[2]};
149146
const uint8_t mac1[4] = {addr[1], addr[0]};
150147
uint32_t mac0_u32, mac1_u32;
@@ -162,8 +159,17 @@ static esp_err_t emac_opencores_get_addr(esp_eth_mac_t *mac, uint8_t *addr)
162159
ESP_LOGV(TAG, "%s: " MACSTR, __func__, MAC2STR(addr));
163160
esp_err_t ret = ESP_OK;
164161
ESP_GOTO_ON_FALSE(addr, ESP_ERR_INVALID_ARG, err, TAG, "can't set mac addr to null");
165-
emac_opencores_t *emac = __containerof(mac, emac_opencores_t, parent);
166-
memcpy(addr, emac->addr, 6);
162+
uint32_t mac0_u32 = REG_READ(OPENETH_MAC_ADDR0_REG);
163+
uint32_t mac1_u32 = REG_READ(OPENETH_MAC_ADDR1_REG);
164+
const uint8_t mac_addr[ETH_ADDR_LEN] = {
165+
(mac1_u32 >> 8) & 0xFF,
166+
mac1_u32 & 0xFF,
167+
(mac0_u32 >> 24) & 0xFF,
168+
(mac0_u32 >> 16) & 0xFF,
169+
(mac0_u32 >> 8) & 0xFF,
170+
mac0_u32 & 0xFF,
171+
};
172+
memcpy(addr, mac_addr, ETH_ADDR_LEN);
167173
return ESP_OK;
168174
err:
169175
return ret;
@@ -288,7 +294,6 @@ static esp_err_t emac_opencores_init(esp_eth_mac_t *mac)
288294
emac_opencores_t *emac = __containerof(mac, emac_opencores_t, parent);
289295
esp_eth_mediator_t *eth = emac->eth;
290296
ESP_GOTO_ON_ERROR(eth->on_state_changed(eth, ETH_STATE_LLINIT, NULL), err, TAG, "lowlevel init failed");
291-
ESP_GOTO_ON_ERROR(esp_read_mac(emac->addr, ESP_MAC_ETH), err, TAG, "fetch ethernet mac address failed");
292297

293298
// Sanity check
294299
if (REG_READ(OPENETH_MODER_REG) != OPENETH_MODER_DEFAULT) {
@@ -299,7 +304,19 @@ static esp_err_t emac_opencores_init(esp_eth_mac_t *mac)
299304
// Initialize the MAC
300305
openeth_reset();
301306
openeth_set_tx_desc_cnt(TX_BUF_COUNT);
302-
emac_opencores_set_addr(mac, emac->addr);
307+
308+
// Check if MAC address has been set in QEMU
309+
uint8_t mac_addr[ETH_ADDR_LEN];
310+
emac_opencores_get_addr(mac, mac_addr);
311+
const uint8_t zero_mac[ETH_ADDR_LEN] = {0};
312+
if (memcmp(mac_addr, zero_mac, ETH_ADDR_LEN) != 0) {
313+
ESP_LOGD(TAG, "Using MAC address " MACSTR " set in QEMU", MAC2STR(mac_addr));
314+
} else {
315+
// Fall back to the default MAC address
316+
ESP_GOTO_ON_ERROR(esp_read_mac(mac_addr, ESP_MAC_ETH), err, TAG, "fetch ethernet mac address failed");
317+
ESP_LOGD(TAG, "Using MAC address " MACSTR " from esp_read_mac", MAC2STR(mac_addr));
318+
emac_opencores_set_addr(mac, mac_addr);
319+
}
303320

304321
return ESP_OK;
305322
err:

0 commit comments

Comments
 (0)