Skip to content

Commit adad058

Browse files
Cleaned up stuff
1 parent 8280ed2 commit adad058

1 file changed

Lines changed: 45 additions & 73 deletions

File tree

general/src/lan8670.c

Lines changed: 45 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -112,11 +112,6 @@ static void debug(lan8670_t *lan, const char *format, ...) {
112112
printf("[LAN8670] "); // Every debug message starts with this tag
113113
vprintf(format, args);
114114
va_end(args);
115-
116-
// CONVENTION FOR DEBUG ERROR CODES:
117-
// Just start the message with something like "ERROR xxxx: ".
118-
// xxxx can be any 4-digit number, as long as it's not already used.
119-
// Just make sure it's something you can easily ctrl+f to find.
120115
}
121116

122117
/**
@@ -132,19 +127,19 @@ static int read_register_field(lan8670_t *lan, int reg, int start, int end, uint
132127
{
133128
/* Validate bit range (If the first bit is greater than the last bit, the field is invalid.) */
134129
if (start > end) {
135-
debug("ERROR 2002: read_register_field() invalid bit range: start (%d) > end (%d)\n", start, end);
130+
debug("ERROR 1000: read_register_field() invalid bit range: start (%d) > end (%d)\n", start, end);
136131
return -1;
137132
}
138133
if (start < 0 || end > 31) {
139-
debug("ERROR 2003: read_register_field() bit range out of bounds: start=%d, end=%d\n", start, end);
134+
debug("ERROR 1001: read_register_field() bit range out of bounds: start=%d, end=%d\n", start, end);
140135
return -1;
141136
}
142137

143138
/* Read the register */
144139
uint32_t data = 0;
145140
int status = lan->read(lan->device_address, reg, &data);
146141
if (status != 0) {
147-
debug("ERROR 2004: read_register_field() failed to read register 0x%X (Status: %d)\n", reg, status);
142+
debug("ERROR 1002: read_register_field() failed to read register 0x%X (Status: %d)\n", reg, status);
148143
return status;
149144
}
150145

@@ -174,19 +169,19 @@ static int write_register_field(lan8670_t *lan, int reg, int start, int end, uin
174169
{
175170
/* Validate bit range (If the first bit is greater than the last bit, the field is invalid.) */
176171
if (start > end) {
177-
debug("ERROR 2005: write_register_field() invalid bit range: start (%d) > end (%d)\n", start, end);
172+
debug("ERROR 2000: write_register_field() invalid bit range: start (%d) > end (%d)\n", start, end);
178173
return -1;
179174
}
180175
if (start < 0 || end > 31) {
181-
debug("ERROR 2006: write_register_field() bit range out of bounds: start=%d, end=%d\n", start, end);
176+
debug("ERROR 2001: write_register_field() bit range out of bounds: start=%d, end=%d\n", start, end);
182177
return -1;
183178
}
184179

185180
/* Calculate field width and validate value */
186181
int field_width = end - start + 1;
187182
uint32_t max_value = (1U << field_width) - 1;
188183
if (value > max_value) {
189-
debug("ERROR 2007: write_register_field() value 0x%X exceeds maximum 0x%X for %d-bit field\n",
184+
debug("ERROR 2002: write_register_field() value 0x%X exceeds maximum 0x%X for %d-bit field\n",
190185
value, max_value, field_width);
191186
return -1;
192187
}
@@ -195,7 +190,7 @@ static int write_register_field(lan8670_t *lan, int reg, int start, int end, uin
195190
uint32_t data = 0;
196191
int status = lan->read(lan->device_address, reg, &data);
197192
if (status != 0) {
198-
debug("ERROR 2008: write_register_field() failed to read register 0x%X (Status: %d)\n", reg, status);
193+
debug("ERROR 3000: write_register_field() failed to read register 0x%X (Status: %d)\n", reg, status);
199194
return status;
200195
}
201196

@@ -206,7 +201,7 @@ static int write_register_field(lan8670_t *lan, int reg, int start, int end, uin
206201
/* Write back the modified value */
207202
status = lan->write(lan->device_address, reg, data);
208203
if (status != 0) {
209-
debug("ERROR 2009: write_register_field() failed to write register 0x%X (Status: %d)\n", reg, status);
204+
debug("ERROR 3001: write_register_field() failed to write register 0x%X (Status: %d)\n", reg, status);
210205
return status;
211206
}
212207

@@ -231,29 +226,29 @@ static int mmd_read_register(lan8670_t *lan, uint16_t mmd_addr, uint16_t registe
231226
uint16_t mmd_ctrl = mmd_addr & 0x1F;
232227
int status = lan->write(lan->device_address, REG_MMDCTRL, mmd_ctrl);
233228
if (status != 0) {
234-
debug("ERROR 2024: mmd_read_register() failed when writing REG_MMDCTRL (Status: %d)\n", status);
229+
debug("ERROR 4000: mmd_read_register() failed when writing REG_MMDCTRL (Status: %d)\n", status);
235230
return status;
236231
}
237232

238233
/* Tell the MMDAD register what specific register you want to access */
239234
status = lan->write(lan->device_address, REG_MMDAD, register_offset);
240235
if (status != 0) {
241-
debug("ERROR 2025: mmd_read_register() failed when writing REG_MMDAD (Status: %d)\n", status);
236+
debug("ERROR 4001: mmd_read_register() failed when writing REG_MMDAD (Status: %d)\n", status);
242237
return status;
243238
}
244239

245240
/* Set the MMD function to 'Data - No post increment' */
246241
mmd_ctrl = (mmd_addr & 0x1F) | (1 << 14);
247242
status = lan->write(lan->device_address, REG_MMDCTRL, mmd_ctrl);
248243
if (status != 0) {
249-
debug("ERROR 2026: mmd_read_register() failed when writing REG_MMDCTRL (Status: %d)\n", status);
244+
debug("ERROR 4002: mmd_read_register() failed when writing REG_MMDCTRL (Status: %d)\n", status);
250245
return status;
251246
}
252247

253248
/* Read data from MMDAD */
254249
status = lan->read(lan->device_address, REG_MMDAD, value);
255250
if (status != 0) {
256-
debug("ERROR 2027: mmd_read_register() failed when reading REG_MMDAD (Status: %d)\n", status);
251+
debug("ERROR 4003: mmd_read_register() failed when reading REG_MMDAD (Status: %d)\n", status);
257252
return status;
258253
}
259254

@@ -278,29 +273,29 @@ static int mmd_write_register(lan8670_t *lan, uint16_t mmd_addr, uint16_t regist
278273
uint16_t mmd_ctrl = mmd_addr & 0x1F;
279274
int status = lan->write(lan->device_address, REG_MMDCTRL, mmd_ctrl);
280275
if (status != 0) {
281-
debug("ERROR 2028: mmd_write_register() failed when writing REG_MMDCTRL (Status: %d)\n", status);
276+
debug("ERROR 5000: mmd_write_register() failed when writing REG_MMDCTRL (Status: %d)\n", status);
282277
return status;
283278
}
284279

285280
/* Tell the MMDAD register what specific register you want to access */
286281
status = lan->write(lan->device_address, REG_MMDAD, register_offset);
287282
if (status != 0) {
288-
debug("ERROR 2029: mmd_write_register() failed when writing REG_MMDAD (Status: %d)\n", status);
283+
debug("ERROR 5001: mmd_write_register() failed when writing REG_MMDAD (Status: %d)\n", status);
289284
return status;
290285
}
291286

292287
/* Set the MMD function to 'Data - No post increment' */
293288
mmd_ctrl = (mmd_addr & 0x1F) | (1 << 14);
294289
status = lan->write(lan->device_address, REG_MMDCTRL, mmd_ctrl);
295290
if (status != 0) {
296-
debug("ERROR 2030: mmd_write_register() failed when writing REG_MMDCTRL (Status: %d)\n", status);
291+
debug("ERROR 5002: mmd_write_register() failed when writing REG_MMDCTRL (Status: %d)\n", status);
297292
return status;
298293
}
299294

300295
/* Write data to MMDAD */
301296
status = lan->write(lan->device_address, REG_MMDAD, value);
302297
if (status != 0) {
303-
debug("ERROR 2031: mmd_write_register() failed when writing REG_MMDAD (Status: %d)\n", status);
298+
debug("ERROR 5003: mmd_write_register() failed when writing REG_MMDAD (Status: %d)\n", status);
304299
return status;
305300
}
306301

@@ -325,19 +320,19 @@ static int mmd_read_register_field(lan8670_t *lan, uint16_t mmd_addr, uint16_t r
325320
{
326321
/* Validate bit range */
327322
if (start > end) {
328-
debug("ERROR 2010: mmd_read_register_field() invalid bit range: start (%d) > end (%d)\n", start, end);
323+
debug("ERROR 6000: mmd_read_register_field() invalid bit range: start (%d) > end (%d)\n", start, end);
329324
return -1;
330325
}
331326
if (start < 0 || end > 15) {
332-
debug("ERROR 2011: mmd_read_register_field() bit range out of bounds: start=%d, end=%d\n", start, end);
327+
debug("ERROR 6001: mmd_read_register_field() bit range out of bounds: start=%d, end=%d\n", start, end);
333328
return -1;
334329
}
335330

336331
/* Read the full register */
337332
uint16_t data = 0;
338333
int status = mmd_read_register(lan, mmd_addr, register_offset, &data);
339334
if (status != 0) {
340-
debug("ERROR 2032: mmd_read_register_field() failed to read register (Status: %d)\n", status);
335+
debug("ERROR 6002: mmd_read_register_field() failed to read register (Status: %d)\n", status);
341336
return status;
342337
}
343338

@@ -368,19 +363,19 @@ static int mmd_write_register_field(lan8670_t *lan, uint16_t mmd_addr, uint16_t
368363
{
369364
/* Validate bit range */
370365
if (start > end) {
371-
debug("ERROR 2016: mmd_write_register_field() invalid bit range: start (%d) > end (%d)\n", start, end);
366+
debug("ERROR 7000: mmd_write_register_field() invalid bit range: start (%d) > end (%d)\n", start, end);
372367
return -1;
373368
}
374369
if (start < 0 || end > 15) {
375-
debug("ERROR 2017: mmd_write_register_field() bit range out of bounds: start=%d, end=%d\n", start, end);
370+
debug("ERROR 7001: mmd_write_register_field() bit range out of bounds: start=%d, end=%d\n", start, end);
376371
return -1;
377372
}
378373

379374
/* Calculate field width and validate value */
380375
int field_width = end - start + 1;
381376
uint16_t max_value = (1U << field_width) - 1;
382377
if (value > max_value) {
383-
debug("ERROR 2018: mmd_write_register_field() value 0x%X exceeds maximum 0x%X for %d-bit field\n",
378+
debug("ERROR 7002: mmd_write_register_field() value 0x%X exceeds maximum 0x%X for %d-bit field\n",
384379
value, max_value, field_width);
385380
return -1;
386381
}
@@ -389,7 +384,7 @@ static int mmd_write_register_field(lan8670_t *lan, uint16_t mmd_addr, uint16_t
389384
uint16_t data = 0;
390385
int status = mmd_read_register(lan, mmd_addr, register_offset, &data);
391386
if (status != 0) {
392-
debug("ERROR 2033: mmd_write_register_field() failed to read register (Status: %d)\n", status);
387+
debug("ERROR 7003: mmd_write_register_field() failed to read register (Status: %d)\n", status);
393388
return status;
394389
}
395390

@@ -400,7 +395,7 @@ static int mmd_write_register_field(lan8670_t *lan, uint16_t mmd_addr, uint16_t
400395
/* Write back the modified value */
401396
status = mmd_write_register(lan, mmd_addr, register_offset, data);
402397
if (status != 0) {
403-
debug("ERROR 2034: mmd_write_register_field() failed to write register (Status: %d)\n", status);
398+
debug("ERROR 7004: mmd_write_register_field() failed to write register (Status: %d)\n", status);
404399
return status;
405400
}
406401

@@ -423,81 +418,58 @@ void lan8670_init(lan8670_t *lan, uint32_t device_address, ReadFunction read, Wr
423418

424419
int lan8670_reset(lan8670_t *lan)
425420
{
426-
int status = lan->write(lan->device_address, REG_BASIC_CONTROL, 0x8000); // Set bit 15 in the Basic Control Register, and clear all other bits. This starts a software reset of the PHY.
427-
if(status != 0) {
428-
debug("ERROR 1453: lan8670_reset() failed while trying to write REG_BASIC_CONTROL with lan->write() (Status: %d).\n", status);
429-
return status;
430-
}
421+
// Set bit 15 in the Basic Control Register, and clear all other bits.
422+
// This starts a software reset of the PHY.
423+
return lan->write(lan->device_address, REG_BASIC_CONTROL, 0x8000);
431424
}
432425

433426
int lan8670_loopback(lan8670_t *lan, bool setting)
434427
{
435-
int status = write_register_field(lan, REG_BASIC_CONTROL, 14, 14, setting); // Modify bit 14 of the Basic Control Register to whatever 'setting' is.
436-
if (status != 0) {
437-
debug("ERROR 1454: lan8670_loopback() failed while trying to write REG_BASIC_CONTROL with write_register_field() (Status: %d).\n", status);
438-
return status;
439-
}
428+
// Modify bit 14 of the Basic Control Register to whatever 'setting' is.
429+
return write_register_field(lan, REG_BASIC_CONTROL, 14, 14, setting);
440430
}
441431

442432
int lan8670_low_power_mode(lan8670_t *lan, bool setting)
443433
{
444-
int status = write_register_field(lan, REG_BASIC_CONTROL, 11, 11, setting); // Modify bit 11 of the Basic Control Register to whatever 'setting' is.
445-
if (status != 0) {
446-
debug("ERROR 1455: lan8670_low_power_mode() failed while trying to write REG_BASIC_CONTROL with write_register_field() (Status: %d).\n", status);
447-
return status;
448-
}
434+
// Modify bit 11 of the Basic Control Register to whatever 'setting' is.
435+
return write_register_field(lan, REG_BASIC_CONTROL, 11, 11, setting);
449436
}
450437

451438
int lan8670_isolate(lan8670_t *lan, bool setting)
452439
{
453-
int status = write_register_field(lan, REG_BASIC_CONTROL, 10, 10, setting); // Modify bit 10 of the Basic Control Register to whatever 'setting' is.
454-
if (status != 0) {
455-
debug("ERROR 1456: lan8670_isolate() failed while trying to write REG_BASIC_CONTROL with write_register_field() (Status: %d).\n", status);
456-
return status;
457-
}
440+
// Modify bit 10 of the Basic Control Register to whatever 'setting' is.
441+
return write_register_field(lan, REG_BASIC_CONTROL, 10, 10, setting);
458442
}
459443

460444
int lan8670_collision_test(lan8670_t *lan, bool setting)
461445
{
462-
int status = write_register_field(lan, REG_BASIC_CONTROL, 7, 7, setting); // Modify bit 7 of the Basic Control Register to whatever 'setting' is.
463-
if (status != 0) {
464-
debug("ERROR 1457: lan8670_collision_test() failed while trying to write REG_BASIC_CONTROL with write_register_field() (Status: %d).\n", status);
465-
return status;
466-
}
446+
// Modify bit 7 of the Basic Control Register to whatever 'setting' is.
447+
return write_register_field(lan, REG_BASIC_CONTROL, 7, 7, setting);
467448
}
468449

469450
int lan8670_detect_jabber(lan8670_t *lan, bool *jabber_status)
470451
{
471-
int status = read_register_field(lan, REG_BASIC_STATUS, 1, 1, jabber_status); // Read bit 1 of the Basic Status Register to 'jabber_status'. If it's 1, jabber is detected.
472-
if (status != 0) {
473-
debug("ERROR 1458: lan8670_detect_jabber() failed while trying to read REG_BASIC_STATUS with read_register_field() (Status: %d).\n", status);
474-
return status;
475-
}
476-
return 0;
452+
// Read bit 1 of the Basic Status Register to 'jabber_status'. If it's 1, jabber is detected.
453+
return read_register_field(lan, REG_BASIC_STATUS, 1, 1, jabber_status);
477454
}
478455

479456
int lan8670_plca_reset(lan8670_t *lan)
480457
{
481-
uint32_t data = 0x4000; // Set bit 14 in the PLCA Control 0 Register, and clear all other bits. This starts a software reset of the PLCA reconciliation sublayer.
482-
int status = mmd_write_register(lan, MMD_MISC, MISC_PLCA_CTRL0, data);
483-
if(status != 0) {
484-
debug("ERROR 1492: lan8670_plca_reset() failed while calling mmd_write_register() (Status: %d).\n", status);
485-
return status;
486-
}
458+
// Set bit 14 in the PLCA Control 0 Register, and clear all other bits.
459+
// This starts a software reset of the PLCA reconciliation sublayer.
460+
return mmd_write_register(lan, MMD_MISC, MISC_PLCA_CTRL0, 0x4000);
487461
}
488462

489463
int lan8670_plca(lan8670_t *lan, bool setting)
490464
{
491-
int status = mmd_write_register_field(lan, MMD_MISC, MISC_PLCA_CTRL0, 15, 15, setting); // Set/clear bit 15 of the PLCA Control 0 Register to whatever 'setting' is.
492-
if(status != 0) {
493-
debug("ERROR 1776: lan8670_plca() failed while calling mmd_modify_register_bit() (Status: %d).\n", status);
494-
return status;
495-
}
465+
// Set/clear bit 15 of the PLCA Control 0 Register to whatever 'setting' is.
466+
return mmd_write_register_field(lan, MMD_MISC, MISC_PLCA_CTRL0, 15, 15, setting);
496467
}
497468

498-
int lan8670_set_node_count(lan8670_t *lan, uint8_t node_count)
469+
int lan8670_plca_set_node_count(lan8670_t *lan, uint8_t node_count)
499470
{
500-
uint16_t write = node_count << 8; // The node count is stored in bits 8-15 of register PLCA_CTRL1
471+
// Set bits 8-15 of the PLCA Control 1 Register to whatever 'node_count' is.
472+
return mmd_write_register_field(lan, MMD_MISC, MISC_PLCA_CTRL1, 8, 15, node_count);
501473
}
502474

503475
// clang-format on

0 commit comments

Comments
 (0)