Skip to content

Commit b436423

Browse files
Naming changes to make more similar to LAN8742 driver (supported by STM32 HAL)
1 parent 305ed6f commit b436423

2 files changed

Lines changed: 35 additions & 22 deletions

File tree

general/include/lan8670.h

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,27 @@
1111
#include <stdbool.h>
1212

1313
/* FUNCTION POINTERS */
14-
typedef int (*ReadFunction)(uint32_t device_address, uint32_t register_address, uint32_t *data); // Function pointer for reading data from the PHY. Analagous to HAL_ETH_ReadPHYRegister() in the STM32 HAL.
15-
typedef int (*WriteFunction)(uint32_t device_address, uint32_t register_address, uint32_t data); // Function pointer for writing data to the PHY. Analagous to HAL_ETH_WritePHYRegister() in the STM32 HAL.
14+
typedef int32_t (*lan8670_Init_Func) (void);
15+
typedef int32_t (*lan8670_DeInit_Func) (void);
16+
typedef int32_t (*lan8670_ReadReg_Func) (uint32_t, uint32_t, uint32_t *);
17+
typedef int32_t (*lan8670_WriteReg_Func) (uint32_t, uint32_t, uint32_t);
18+
typedef int32_t (*lan8670_GetTick_Func) (void);
19+
20+
/* IO STRUCT */
21+
typedef struct
22+
{
23+
lan8670_Init_Func Init;
24+
lan8670_DeInit_Func DeInit;
25+
lan8670_WriteReg_Func WriteReg;
26+
lan8670_ReadReg_Func ReadReg;
27+
lan8670_GetTick_Func GetTick;
28+
} lan8670_IOCtx_t;
1629

1730
/* LAN8670 STRUCT */
1831
typedef struct {
19-
uint32_t device_address; // The address of the LAN8670 device.
20-
ReadFunction read; // Function to read data from the PHY.
21-
WriteFunction write; // Function to write data to the PHY.
32+
uint32_t DevAddr; // The address of the LAN8670 device.
33+
uint32_t Is_Initialized; // Indicates if the device is initialized.
34+
lan8670_IOCtx_t IO; // I/O context for the device.
2235
bool debug; // Enables printfs for debugging. Defaults to false.
2336
} lan8670_t;
2437

@@ -29,7 +42,7 @@ typedef struct {
2942
* @param read Function pointer for reading data from the LAN8670.
3043
* @param write Function pointer for writing data to the LAN8670.
3144
*/
32-
void lan8670_init(lan8670_t *lan, uint32_t device_address, ReadFunction read, WriteFunction write); // Initializes a LAN8670 instance.
45+
void lan8670_init(lan8670_t *lan, uint32_t device_address, lan8670_ReadReg_Func read, lan8670_WriteReg_Func write); // Initializes a LAN8670 instance.
3346

3447
/**
3548
* @brief Performs a software reset of the LAN8670 Ethernet PHY.

general/src/lan8670.c

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ static int read_register_field(lan8670_t *lan, int reg, int start, int end, uint
136136

137137
/* Read the register */
138138
uint32_t data = 0;
139-
int status = lan->read(lan->device_address, reg, &data);
139+
int status = lan->IO.ReadReg(lan->DevAddr, reg, &data);
140140
if (status != 0) {
141141
debug("ERROR 1002: read_register_field() failed to read register 0x%X (Status: %d)\n", reg, status);
142142
return status;
@@ -187,7 +187,7 @@ static int write_register_field(lan8670_t *lan, int reg, int start, int end, uin
187187

188188
/* Read the current register value */
189189
uint32_t data = 0;
190-
int status = lan->read(lan->device_address, reg, &data);
190+
int status = lan->IO.ReadReg(lan->DevAddr, reg, &data);
191191
if (status != 0) {
192192
debug("ERROR 3000: write_register_field() failed to read register 0x%X (Status: %d)\n", reg, status);
193193
return status;
@@ -198,7 +198,7 @@ static int write_register_field(lan8670_t *lan, int reg, int start, int end, uin
198198
data = (data & ~mask) | ((value << start) & mask);
199199

200200
/* Write back the modified value */
201-
status = lan->write(lan->device_address, reg, data);
201+
status = lan->IO.WriteReg(lan->DevAddr, reg, data);
202202
if (status != 0) {
203203
debug("ERROR 3001: write_register_field() failed to write register 0x%X (Status: %d)\n", reg, status);
204204
return status;
@@ -223,29 +223,29 @@ static int mmd_read_register(lan8670_t *lan, uint16_t mmd_addr, uint16_t registe
223223
{
224224
/* Tell the MMDCTRL register what MMD device you intend to access */
225225
uint16_t mmd_ctrl = mmd_addr & 0x1F;
226-
int status = lan->write(lan->device_address, REG_MMDCTRL, mmd_ctrl);
226+
int status = lan->IO.WriteReg(lan->DevAddr, REG_MMDCTRL, mmd_ctrl);
227227
if (status != 0) {
228228
debug("ERROR 4000: mmd_read_register() failed when writing REG_MMDCTRL (Status: %d)\n", status);
229229
return status;
230230
}
231231

232232
/* Tell the MMDAD register what specific register you want to access */
233-
status = lan->write(lan->device_address, REG_MMDAD, register_offset);
233+
status = lan->IO.WriteReg(lan->DevAddr, REG_MMDAD, register_offset);
234234
if (status != 0) {
235235
debug("ERROR 4001: mmd_read_register() failed when writing REG_MMDAD (Status: %d)\n", status);
236236
return status;
237237
}
238238

239239
/* Set the MMD function to 'Data - No post increment' */
240240
mmd_ctrl = (mmd_addr & 0x1F) | (1 << 14);
241-
status = lan->write(lan->device_address, REG_MMDCTRL, mmd_ctrl);
241+
status = lan->IO.WriteReg(lan->DevAddr, REG_MMDCTRL, mmd_ctrl);
242242
if (status != 0) {
243243
debug("ERROR 4002: mmd_read_register() failed when writing REG_MMDCTRL (Status: %d)\n", status);
244244
return status;
245245
}
246246

247247
/* Read data from MMDAD */
248-
status = lan->read(lan->device_address, REG_MMDAD, value);
248+
status = lan->IO.ReadReg(lan->DevAddr, REG_MMDAD, value);
249249
if (status != 0) {
250250
debug("ERROR 4003: mmd_read_register() failed when reading REG_MMDAD (Status: %d)\n", status);
251251
return status;
@@ -270,29 +270,29 @@ static int mmd_write_register(lan8670_t *lan, uint16_t mmd_addr, uint16_t regist
270270
{
271271
/* Tell the MMDCTRL register what MMD device you intend to access */
272272
uint16_t mmd_ctrl = mmd_addr & 0x1F;
273-
int status = lan->write(lan->device_address, REG_MMDCTRL, mmd_ctrl);
273+
int status = lan->IO.WriteReg(lan->DevAddr, REG_MMDCTRL, mmd_ctrl);
274274
if (status != 0) {
275275
debug("ERROR 5000: mmd_write_register() failed when writing REG_MMDCTRL (Status: %d)\n", status);
276276
return status;
277277
}
278278

279279
/* Tell the MMDAD register what specific register you want to access */
280-
status = lan->write(lan->device_address, REG_MMDAD, register_offset);
280+
status = lan->IO.WriteReg(lan->DevAddr, REG_MMDAD, register_offset);
281281
if (status != 0) {
282282
debug("ERROR 5001: mmd_write_register() failed when writing REG_MMDAD (Status: %d)\n", status);
283283
return status;
284284
}
285285

286286
/* Set the MMD function to 'Data - No post increment' */
287287
mmd_ctrl = (mmd_addr & 0x1F) | (1 << 14);
288-
status = lan->write(lan->device_address, REG_MMDCTRL, mmd_ctrl);
288+
status = lan->IO.WriteReg(lan->DevAddr, REG_MMDCTRL, mmd_ctrl);
289289
if (status != 0) {
290290
debug("ERROR 5002: mmd_write_register() failed when writing REG_MMDCTRL (Status: %d)\n", status);
291291
return status;
292292
}
293293

294294
/* Write data to MMDAD */
295-
status = lan->write(lan->device_address, REG_MMDAD, value);
295+
status = lan->IO.WriteReg(lan->DevAddr, REG_MMDAD, value);
296296
if (status != 0) {
297297
debug("ERROR 5003: mmd_write_register() failed when writing REG_MMDAD (Status: %d)\n", status);
298298
return status;
@@ -407,19 +407,19 @@ static int mmd_write_register_field(lan8670_t *lan, uint16_t mmd_addr, uint16_t
407407

408408
/**** API FUNCTIONS ****/
409409

410-
void lan8670_init(lan8670_t *lan, uint32_t device_address, ReadFunction read, WriteFunction write)
410+
void lan8670_init(lan8670_t *lan, uint32_t device_address, lan8670_ReadReg_Func read, lan8670_WriteReg_Func write)
411411
{
412-
lan->write = write;
413-
lan->read = read;
414-
lan->device_address = device_address;
412+
lan->IO.WriteReg = write;
413+
lan->IO.ReadReg = read;
414+
lan->DevAddr = device_address;
415415
lan->debug = false; // Default to no debugging. Set this to true (after calling lan8670_init()) if you want debugging enabled.
416416
}
417417

418418
int lan8670_reset(lan8670_t *lan)
419419
{
420420
// Set bit 15 in the Basic Control Register, and clear all other bits.
421421
// This starts a software reset of the PHY.
422-
return lan->write(lan->device_address, REG_BASIC_CONTROL, 0x8000);
422+
return lan->IO.WriteReg(lan->DevAddr, REG_BASIC_CONTROL, 0x8000);
423423
}
424424

425425
int lan8670_loopback(lan8670_t *lan, bool setting)

0 commit comments

Comments
 (0)