Skip to content

Commit 4962a92

Browse files
author
BiffoBear
committed
Refactor chip reset.
1 parent d72a1bf commit 4962a92

File tree

1 file changed

+30
-36
lines changed

1 file changed

+30
-36
lines changed

adafruit_wiznet5k/adafruit_wiznet5k.py

Lines changed: 30 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -57,8 +57,7 @@
5757
from adafruit_wiznet5k.adafruit_wiznet5k_debug import debug_msg
5858

5959
# *** Wiznet Common Registers ***
60-
# Mode (used only for initialization and soft reset).
61-
_REG_MR = {"w5100s": const(0x0000), "w5500": const(0x0000), "w6100": const(0x4000)}
60+
_REG_MR = {"w5100s": const(0x0000), "w5500": const(0x0000)}
6261
# Gateway IPv4 Address.
6362
_REG_GAR = {"w5100s": const(0x0001), "w5500": const(0x0001), "w6100": const(0x4130)}
6463
# Subnet Mask Address
@@ -498,7 +497,7 @@ def socket_available(self, socket_num: int, sock_type: int = _SNMR_TCP) -> int:
498497
Number of bytes available to be read from the socket.
499498
500499
:param int socket_num: Socket to check for available bytes.
501-
:param int sock_type: Socket type. Use SNMR_TCP for TCP or SNMR_UDP for UDP, \
500+
:param int sock_type: Socket type. Use SNMR_TCP for TCP or SNMR_UDP for UDP,
502501
defaults to SNMR_TCP.
503502
504503
:return int: Number of bytes available to read.
@@ -946,18 +945,15 @@ def sw_reset(self) -> None:
946945
"""
947946
self._wiznet_chip_init()
948947

949-
def _sw_reset(self) -> None:
948+
def _sw_reset_5x00(self) -> bool:
950949
"""
951-
Perform a soft reset on the WIZnet chip.
950+
Perform a soft reset on the WIZnet 5100s and 5500 chips.
952951
953-
:raises RuntimeError: If reset fails.
952+
:returns bool: True if reset was success
954953
"""
955954
self._write_mr(_MR_RST)
956955
time.sleep(0.05)
957-
result = self._read_mr()
958-
expected_result = {"w5500": 0x00, "w5100s": 0x03}[self._chip_type]
959-
if result != expected_result:
960-
raise RuntimeError("WIZnet chip reset failed.")
956+
return self._read_mr() == {"w5500": 0x00, "w5100s": 0x03}[self._chip_type]
961957

962958
def _wiznet_chip_init(self) -> None:
963959
"""
@@ -966,6 +962,16 @@ def _wiznet_chip_init(self) -> None:
966962
:raises RuntimeError: If no WIZnet chip is detected.
967963
"""
968964

965+
def _setup_sockets() -> None:
966+
"""Initialise sockets for w5500 and w6100 chips."""
967+
for sock_num in range(_MAX_SOCK_NUM[self._chip_type]):
968+
ctrl_byte = 0x0C + (sock_num << 5)
969+
self._write(0x1E, ctrl_byte, 2)
970+
self._write(0x1F, ctrl_byte, 2)
971+
self._ch_base_msb = 0x00
972+
WIZNET5K._sockets_reserved = [False] * (_MAX_SOCK_NUM[self._chip_type] - 1)
973+
self._src_ports_in_use = [0] * _MAX_SOCK_NUM[self._chip_type]
974+
969975
def _detect_and_reset_w6100() -> bool:
970976
"""
971977
Detect and reset a W6100 chip. Called at startup to initialize the
@@ -974,22 +980,18 @@ def _detect_and_reset_w6100() -> bool:
974980
:return bool: True if a W6100 chip is detected, False if not.
975981
"""
976982
self._chip_type = "w6100"
977-
try:
978-
self._sw_reset()
979-
except RuntimeError:
980-
return False
983+
984+
# Reset w6100
985+
self._write(0x41F4, 0x04, 0xCE) # Unlock chip settings.
986+
time.sleep(0.05) # Wait for unlock.
987+
self._write(0x2004, 0x04, 0x00) # Reset chip.
988+
time.sleep(0.05) # Wait for reset.
981989

982990
if self._read(_REG_VERSIONR[self._chip_type], 0x00)[0] != 0x61:
983991
return False
984992
# Initialize w6100.
985993
self._write(0x41F5, 0x04, 0x3A) # Unlock network settings.
986-
for i in range(_MAX_SOCK_NUM[self._chip_type]):
987-
ctrl_byte = 0x0C + (i << 5)
988-
self._write(0x1E, ctrl_byte, 2)
989-
self._write(0x1F, ctrl_byte, 2)
990-
self._ch_base_msb = 0x00
991-
WIZNET5K._sockets_reserved = [False] * (_MAX_SOCK_NUM[self._chip_type] - 1)
992-
self._src_ports_in_use = [0] * _MAX_SOCK_NUM[self._chip_type]
994+
_setup_sockets()
993995
return True
994996

995997
def _detect_and_reset_w5500() -> bool:
@@ -1000,9 +1002,7 @@ def _detect_and_reset_w5500() -> bool:
10001002
:return bool: True if a W5500 chip is detected, False if not.
10011003
"""
10021004
self._chip_type = "w5500"
1003-
try:
1004-
self._sw_reset()
1005-
except RuntimeError:
1005+
if not self._sw_reset_5x00():
10061006
return False
10071007

10081008
self._write_mr(0x08)
@@ -1020,13 +1020,7 @@ def _detect_and_reset_w5500() -> bool:
10201020
if self._read(_REG_VERSIONR[self._chip_type], 0x00)[0] != 0x04:
10211021
return False
10221022
# Initialize w5500
1023-
for i in range(_MAX_SOCK_NUM[self._chip_type]):
1024-
ctrl_byte = 0x0C + (i << 5)
1025-
self._write(0x1E, ctrl_byte, 2)
1026-
self._write(0x1F, ctrl_byte, 2)
1027-
self._ch_base_msb = 0x00
1028-
WIZNET5K._sockets_reserved = [False] * (_MAX_SOCK_NUM[self._chip_type] - 1)
1029-
self._src_ports_in_use = [0] * _MAX_SOCK_NUM[self._chip_type]
1023+
_setup_sockets()
10301024
return True
10311025

10321026
def _detect_and_reset_w5100s() -> bool:
@@ -1037,13 +1031,13 @@ def _detect_and_reset_w5100s() -> bool:
10371031
:return bool: True if a W5100 chip is detected, False if not.
10381032
"""
10391033
self._chip_type = "w5100s"
1040-
try:
1041-
self._sw_reset()
1042-
except RuntimeError:
1034+
if not self._sw_reset_5x00():
10431035
return False
10441036

10451037
if self._read(_REG_VERSIONR[self._chip_type], 0x00)[0] != 0x51:
10461038
return False
1039+
1040+
# Initialise w5100s
10471041
self._ch_base_msb = 0x0400
10481042
WIZNET5K._sockets_reserved = [False] * (_MAX_SOCK_NUM[self._chip_type] - 1)
10491043
self._src_ports_in_use = [0] * _MAX_SOCK_NUM[self._chip_type]
@@ -1123,7 +1117,7 @@ def _write(self, addr: int, callback: int, data: Union[int, bytes]) -> None:
11231117

11241118
def _write_socket_register(self, sock: int, address: int, data: int) -> None:
11251119
"""Write to a WIZnet 5k socket register."""
1126-
if self._chip_type == "w5500":
1120+
if self._chip_type in ("w5500", "w6100"):
11271121
cntl_byte = (sock << 5) + 0x0C
11281122
self._write(address, cntl_byte, data)
11291123
elif self._chip_type == "w5100s":
@@ -1132,7 +1126,7 @@ def _write_socket_register(self, sock: int, address: int, data: int) -> None:
11321126

11331127
def _read_socket_register(self, sock: int, address: int) -> int:
11341128
"""Read a WIZnet 5k socket register."""
1135-
if self._chip_type == "w5500":
1129+
if self._chip_type in ("w5500", "w6100"):
11361130
cntl_byte = (sock << 5) + 0x08
11371131
register = self._read(address, cntl_byte)
11381132
elif self._chip_type == "w5100s":

0 commit comments

Comments
 (0)