esp_eth: Fix w5500 to correctly read registers on -Os
Reading SPI data may come in 4-byte units and thus result in unwanted
overwrites if smaller size registers read, especially if multiple placed
one after another. Fixed by using direct reads to `trans` structure for
sizes smaller or equal to 4.
Closes
@@ -93,6 +93,7 @@ static esp_err_t w5500_read(emac_w5500_t *emac, uint32_t address, void *value, u
esp_err_t ret = ESP_OK;
spi_transaction_t trans = {
+ .flags = len <= 4 ? SPI_TRANS_USE_RXDATA : 0, // use direct reads for registers to prevent overwrites by 4-byte boundary writes
.cmd = (address >> W5500_ADDR_OFFSET),
.addr = ((address & 0xFFFF) | (W5500_ACCESS_MODE_READ << W5500_RWB_OFFSET) | W5500_SPI_OP_MODE_VDM),
.length = 8 * len,
@@ -107,6 +108,9 @@ static esp_err_t w5500_read(emac_w5500_t *emac, uint32_t address, void *value, u
} else {
ret = ESP_ERR_TIMEOUT;
}
+ if ((trans.flags&SPI_TRANS_USE_RXDATA) && len <= 4) {
+ memcpy(value, trans.rx_data, len); // copy register values to output
+ }
return ret;
}
