Skip to content

Commit 17c9ed8

Browse files
authored
Merge pull request #81 from BiffoBear/detect_5500_error
The detect_w5500 and detect_w5100s functions reset the Wiznet SOC
2 parents 2d7a38e + a485697 commit 17c9ed8

File tree

1 file changed

+51
-48
lines changed

1 file changed

+51
-48
lines changed

adafruit_wiznet5k/adafruit_wiznet5k.py

+51-48
Original file line numberDiff line numberDiff line change
@@ -187,7 +187,7 @@ def __init__(
187187

188188
# attempt to initialize the module
189189
self._ch_base_msb = 0
190-
assert self._w5100_init() == 1, "Failed to initialize WIZnet module."
190+
assert self._w5xxx_init() == 1, "Failed to initialize WIZnet module."
191191
# Set MAC address
192192
self.mac_address = mac
193193
self.src_port = 0
@@ -449,75 +449,78 @@ def ifconfig(
449449

450450
self._dns = dns_server
451451

452-
def _w5100_init(self) -> int:
452+
def _w5xxx_init(self) -> int:
453453
"""
454454
Detect and initialize a Wiznet5k ethernet module.
455455
456456
:return int: 1 if the initialization succeeds, 0 if it fails.
457457
"""
458+
459+
def _detect_and_reset_w5500() -> bool:
460+
"""
461+
Detect and reset a W5500 chip. Called at startup to initialize the
462+
interface hardware.
463+
464+
:return bool: True if a W5500 chip is detected, False if not.
465+
"""
466+
self._chip_type = "w5500"
467+
# assert self.sw_reset() == 0, "Chip not reset properly!"
468+
self._write_mr(0x08)
469+
# assert self._read_mr()[0] == 0x08, "Expected 0x08."
470+
if self._read_mr()[0] != 0x08:
471+
return False
472+
473+
self._write_mr(0x10)
474+
# assert self._read_mr()[0] == 0x10, "Expected 0x10."
475+
if self._read_mr()[0] != 0x10:
476+
return False
477+
478+
self._write_mr(0x00)
479+
# assert self._read_mr()[0] == 0x00, "Expected 0x00."
480+
if self._read_mr()[0] != 0x00:
481+
return False
482+
483+
if self.read(REG_VERSIONR_W5500, 0x00)[0] != 0x04:
484+
return False
485+
# self._chip_type = "w5500"
486+
# self._ch_base_msb = 0x10
487+
return True
488+
489+
def _detect_and_reset_w5100s() -> bool:
490+
"""
491+
Detect and reset a W5100S chip. Called at startup to initialize the
492+
interface hardware.
493+
494+
:return bool: True if a W5100 chip is detected, False if not.
495+
"""
496+
self._chip_type = "w5100s"
497+
# sw reset
498+
assert self.sw_reset() == 0, "Chip not reset properly!"
499+
if self.read(REG_VERSIONR_W5100S, 0x00)[0] != 0x51:
500+
return False
501+
502+
self._ch_base_msb = 0x0400
503+
return True
504+
458505
time.sleep(1)
459506
self._cs.switch_to_output()
460507
self._cs.value = 1
461508

462509
# Detect if chip is Wiznet W5500
463-
if self.detect_w5500() == 1:
510+
if _detect_and_reset_w5500():
464511
# perform w5500 initialization
465512
for i in range(0, W5200_W5500_MAX_SOCK_NUM):
466513
ctrl_byte = 0x0C + (i << 5)
467514
self.write(0x1E, ctrl_byte, 2)
468515
self.write(0x1F, ctrl_byte, 2)
469516
else:
470517
# Detect if chip is Wiznet W5100S
471-
if self.detect_w5100s() == 1:
518+
if _detect_and_reset_w5100s():
472519
pass
473520
else:
474521
return 0
475522
return 1
476523

477-
def detect_w5500(self) -> int:
478-
"""
479-
Detect W5500 chip.
480-
481-
:return int: 1 if a W5500 chip is detected, -1 if not.
482-
"""
483-
self._chip_type = "w5500"
484-
assert self.sw_reset() == 0, "Chip not reset properly!"
485-
self._write_mr(0x08)
486-
# assert self._read_mr()[0] == 0x08, "Expected 0x08."
487-
if self._read_mr()[0] != 0x08:
488-
return -1
489-
490-
self._write_mr(0x10)
491-
# assert self._read_mr()[0] == 0x10, "Expected 0x10."
492-
if self._read_mr()[0] != 0x10:
493-
return -1
494-
495-
self._write_mr(0x00)
496-
# assert self._read_mr()[0] == 0x00, "Expected 0x00."
497-
if self._read_mr()[0] != 0x00:
498-
return -1
499-
500-
if self.read(REG_VERSIONR_W5500, 0x00)[0] != 0x04:
501-
return -1
502-
# self._chip_type = "w5500"
503-
# self._ch_base_msb = 0x10
504-
return 1
505-
506-
def detect_w5100s(self) -> int:
507-
"""
508-
Detect W5100S chip.
509-
510-
:return int: 1 if a W5100 chip is detected, -1 if not.
511-
"""
512-
self._chip_type = "w5100s"
513-
# sw reset
514-
assert self.sw_reset() == 0, "Chip not reset properly!"
515-
if self.read(REG_VERSIONR_W5100S, 0x00)[0] != 0x51:
516-
return -1
517-
518-
self._ch_base_msb = 0x0400
519-
return 1
520-
521524
def sw_reset(self) -> int:
522525
"""Perform a soft-reset on the Wiznet chip.
523526

0 commit comments

Comments
 (0)