Skip to content

Commit 305ed6f

Browse files
Added get_link_state()
1 parent 1ba6dd3 commit 305ed6f

2 files changed

Lines changed: 33 additions & 0 deletions

File tree

general/include/lan8670.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,4 +145,12 @@ int lan8670_plca_set_node_count(lan8670_t *lan, uint8_t node_count);
145145
*/
146146
int lan8670_plca_set_node_id(lan8670_t *lan, uint8_t id);
147147

148+
/**
149+
* @brief Gets the current link state of the LAN8670.
150+
* @param lan Pointer to the lan8670_t instance.
151+
* @param link_up Pointer to a boolean variable to store the link state (true if link is up, false if down).
152+
* @return 0 on success, or a non-zero error code.
153+
*/
154+
int lan8670_get_link_state(lan8670_t *lan, bool *link_up);
155+
148156
// clang-format on

general/src/lan8670.c

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -481,4 +481,29 @@ int lan8670_plca_set_node_id(lan8670_t *lan, uint8_t id)
481481
// Modify bits 0-7 of the PLCA Control 1 Register to whatever 'id' is.
482482
return mmd_write_register_field(lan, MMD_MISC, MISC_PLCA_CTRL1, 0, 7, id);
483483
}
484+
485+
int lan8670_get_link_state(lan8670_t *lan, bool *link_up)
486+
{
487+
uint32_t link_status = 0;
488+
489+
// Read bit 2 of the Basic Status Register
490+
// Bit 2: Link Status (1 = link is up, 0 = link is down)
491+
int status = read_register_field(lan, REG_BASIC_STATUS, 2, 2, &link_status);
492+
if (status != 0) {
493+
debug(lan, "ERROR 8000: lan8670_get_link_state() failed to read link status (Status: %d)\n", status);
494+
return status;
495+
}
496+
497+
// The link status bit is latched low, so we need to read it twice to get the current state
498+
// First read clears the latch, second read gets the current state
499+
status = read_register_field(lan, REG_BASIC_STATUS, 2, 2, &link_status);
500+
if (status != 0) {
501+
debug(lan, "ERROR 8001: lan8670_get_link_state() failed to read link status (Status: %d)\n", status);
502+
return status;
503+
}
504+
505+
*link_up = (link_status == 1);
506+
return 0;
507+
}
508+
484509
// clang-format on

0 commit comments

Comments
 (0)