Skip to content

The detect_w5500 and detect_w5100s functions reset the Wiznet SOC #81

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Dec 20, 2022
Merged
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
99 changes: 51 additions & 48 deletions adafruit_wiznet5k/adafruit_wiznet5k.py
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,7 @@ def __init__(

# attempt to initialize the module
self._ch_base_msb = 0
assert self._w5100_init() == 1, "Failed to initialize WIZnet module."
assert self._w5xxx_init() == 1, "Failed to initialize WIZnet module."
# Set MAC address
self.mac_address = mac
self.src_port = 0
Expand Down Expand Up @@ -449,75 +449,78 @@ def ifconfig(

self._dns = dns_server

def _w5100_init(self) -> int:
def _w5xxx_init(self) -> int:
"""
Detect and initialize a Wiznet5k ethernet module.

:return int: 1 if the initialization succeeds, 0 if it fails.
"""

def _detect_and_reset_w5500() -> bool:
"""
Detect and reset a W5500 chip. Called at startup to initialize the
interface hardware.

:return bool: True if a W5500 chip is detected, False if not.
"""
self._chip_type = "w5500"
# assert self.sw_reset() == 0, "Chip not reset properly!"
self._write_mr(0x08)
# assert self._read_mr()[0] == 0x08, "Expected 0x08."
if self._read_mr()[0] != 0x08:
return False

self._write_mr(0x10)
# assert self._read_mr()[0] == 0x10, "Expected 0x10."
if self._read_mr()[0] != 0x10:
return False

self._write_mr(0x00)
# assert self._read_mr()[0] == 0x00, "Expected 0x00."
if self._read_mr()[0] != 0x00:
return False

if self.read(REG_VERSIONR_W5500, 0x00)[0] != 0x04:
return False
# self._chip_type = "w5500"
# self._ch_base_msb = 0x10
return True

def _detect_and_reset_w5100s() -> bool:
"""
Detect and reset a W5100S chip. Called at startup to initialize the
interface hardware.

:return bool: True if a W5100 chip is detected, False if not.
"""
self._chip_type = "w5100s"
# sw reset
assert self.sw_reset() == 0, "Chip not reset properly!"
if self.read(REG_VERSIONR_W5100S, 0x00)[0] != 0x51:
return False

self._ch_base_msb = 0x0400
return True

time.sleep(1)
self._cs.switch_to_output()
self._cs.value = 1

# Detect if chip is Wiznet W5500
if self.detect_w5500() == 1:
if _detect_and_reset_w5500():
# perform w5500 initialization
for i in range(0, W5200_W5500_MAX_SOCK_NUM):
ctrl_byte = 0x0C + (i << 5)
self.write(0x1E, ctrl_byte, 2)
self.write(0x1F, ctrl_byte, 2)
else:
# Detect if chip is Wiznet W5100S
if self.detect_w5100s() == 1:
if _detect_and_reset_w5100s():
pass
else:
return 0
return 1

def detect_w5500(self) -> int:
"""
Detect W5500 chip.

:return int: 1 if a W5500 chip is detected, -1 if not.
"""
self._chip_type = "w5500"
assert self.sw_reset() == 0, "Chip not reset properly!"
self._write_mr(0x08)
# assert self._read_mr()[0] == 0x08, "Expected 0x08."
if self._read_mr()[0] != 0x08:
return -1

self._write_mr(0x10)
# assert self._read_mr()[0] == 0x10, "Expected 0x10."
if self._read_mr()[0] != 0x10:
return -1

self._write_mr(0x00)
# assert self._read_mr()[0] == 0x00, "Expected 0x00."
if self._read_mr()[0] != 0x00:
return -1

if self.read(REG_VERSIONR_W5500, 0x00)[0] != 0x04:
return -1
# self._chip_type = "w5500"
# self._ch_base_msb = 0x10
return 1

def detect_w5100s(self) -> int:
"""
Detect W5100S chip.

:return int: 1 if a W5100 chip is detected, -1 if not.
"""
self._chip_type = "w5100s"
# sw reset
assert self.sw_reset() == 0, "Chip not reset properly!"
if self.read(REG_VERSIONR_W5100S, 0x00)[0] != 0x51:
return -1

self._ch_base_msb = 0x0400
return 1

def sw_reset(self) -> int:
"""Perform a soft-reset on the Wiznet chip.

Expand Down