Skip to content
Open
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
14 changes: 10 additions & 4 deletions general/include/lan8670.h
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -228,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
26 changes: 23 additions & 3 deletions general/src/lan8670.c
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Expand Down Expand Up @@ -590,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
Loading