From c9d989d136bd54ff8c8a527369e319f547ec6d70 Mon Sep 17 00:00:00 2001 From: bjackson312006 Date: Tue, 17 Feb 2026 09:55:09 -0500 Subject: [PATCH 1/2] Made it so DevAddr gets hardcoded in LAN8670_Init() --- general/include/lan8670.h | 6 ++---- general/src/lan8670.c | 11 ++++++++--- 2 files changed, 10 insertions(+), 7 deletions(-) diff --git a/general/include/lan8670.h b/general/include/lan8670.h index e8af66b5..91350654 100644 --- a/general/include/lan8670.h +++ b/general/include/lan8670.h @@ -47,12 +47,10 @@ typedef struct { /** * @brief Initializes a LAN8670 instance. * @param lan Pointer to the lan8670_t instance. - * @param device_address The address of the LAN8670. - * @param read Function pointer for reading data from the LAN8670. - * @param write Function pointer for writing data to the LAN8670. + * @param DevAddr The device address of the LAN8670. This is a 5-bit value indicated by the PHYAD0-PHYAD4 pins and their pull-up/pull-down configuration. * @return Status. */ -int32_t LAN8670_Init(lan8670_t *lan); // Initializes a LAN8670 instance. +int32_t LAN8670_Init(lan8670_t *lan, uint32_t DevAddr); // Initializes a LAN8670 instance. /** * @brief Performs a software reset of the LAN8670 Ethernet PHY. diff --git a/general/src/lan8670.c b/general/src/lan8670.c index 238e097c..530d187b 100644 --- a/general/src/lan8670.c +++ b/general/src/lan8670.c @@ -411,16 +411,21 @@ static int mmd_write_register_field(lan8670_t *lan, uint16_t mmd_addr, uint16_t /**** API FUNCTIONS ****/ -int32_t LAN8670_Init(lan8670_t *lan) +int32_t LAN8670_Init(lan8670_t *lan, uint32_t DevAddr) { - // Set the device address to the SMIADR[4:0] field of the Strap Control 0 Register. + // Store the DevAddr + lan->DevAddr = DevAddr; + + // Double-check that DevAddr is the same as what's stored in the SMIADR[4:0] field of the Strap Control 0 Register. uint32_t buffer = 0; int32_t status = read_register_field(lan, REG_STRAP_CTRL0, 0, 4, &buffer); if(status != 0) { debug(lan, "ERROR 0000: LAN8670_Init() failed to read Strap Control Register 0 (Status: %d)\n", status); return LAN8670_STATUS_READ_ERROR; } - lan->DevAddr = buffer; + if(lan->DevAddr != buffer) { + PRINTLN_WARNING("The hardcoded DevAddr value isn't the same as what's stored in SMIADR[4:0]. Something weird is probably going on (how did it even read the register in the first place)? (DevAddr=%d, SMIADR[4:0]=%d).", lan->DevAddr, buffer); + } lan->debug = false; // Default to no debugging. From 3b8ce48a11a45cd078c0015d85de4ff2454b253b Mon Sep 17 00:00:00 2001 From: bjackson312006 Date: Tue, 17 Feb 2026 11:07:20 -0500 Subject: [PATCH 2/2] Added LAN8670_Read_PHY_DevAddr() --- general/include/lan8670.h | 8 ++++++++ general/src/lan8670.c | 15 +++++++++++++++ 2 files changed, 23 insertions(+) diff --git a/general/include/lan8670.h b/general/include/lan8670.h index 91350654..427af195 100644 --- a/general/include/lan8670.h +++ b/general/include/lan8670.h @@ -226,4 +226,12 @@ int32_t LAN8670_Read_PHY_ID1(lan8670_t *lan, uint16_t *data); */ int32_t LAN8670_Read_Model_Number(lan8670_t *lan, uint8_t *data); +/** + * @brief Returns the PHY's device address according to the STRAP_CTRL0 register. This is a 5-bit value and should be consistent with how the device address is configured via the hardware pins. + * + * @param lan Pointer to the lan8670_t instance. + * @param data Buffer for the value. + * @return Status. + */ +int32_t LAN8670_Read_PHY_DevAddr(lan8670_t *lan, uint8_t *data); // clang-format on \ No newline at end of file diff --git a/general/src/lan8670.c b/general/src/lan8670.c index 530d187b..26c2ea6e 100644 --- a/general/src/lan8670.c +++ b/general/src/lan8670.c @@ -595,4 +595,19 @@ int32_t LAN8670_Read_Model_Number(lan8670_t *lan, uint8_t *data) { return LAN8670_STATUS_OK; } +/* Returns the 5-bit PHY device address. */ +int32_t LAN8670_Read_PHY_DevAddr(lan8670_t *lan, uint8_t *data) { + // Read bits 4:0 of the STRAP_CTRL0 register (containing the DevAddr configured by the hardware pins). + uint32_t buffer = 0; + int32_t status = read_register_field(lan, REG_STRAP_CTRL0, 0, 4, &buffer); + if(status != 0) { + PRINTLN_ERROR("Failed to call read_register_field() to read the STRAP_CTRL0 register (Status: %d).", status); + return LAN8670_STATUS_READ_ERROR; + } + + // Store the value + *data = (uint8_t)buffer; + return LAN8670_STATUS_OK; +} + // clang-format on \ No newline at end of file