Skip to content

Commit 67c3763

Browse files
author
BiffoBear
committed
Setup common registers.
1 parent a39d458 commit 67c3763

File tree

1 file changed

+41
-24
lines changed

1 file changed

+41
-24
lines changed

adafruit_wiznet5k/adafruit_wiznet5k.py

Lines changed: 41 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -56,18 +56,33 @@
5656
import adafruit_wiznet5k.adafruit_wiznet5k_dns as dns
5757
from adafruit_wiznet5k.adafruit_wiznet5k_debug import debug_msg
5858

59-
# Wiznet5k Registers
60-
_REG_MR = const(0x0000) # Mode
61-
_REG_GAR = const(0x0001) # Gateway IP Address
62-
_REG_SUBR = const(0x0005) # Subnet Mask Address
63-
_REG_VERSIONR = {"w5100s": const(0x0080), "w5500": const(0x0039)}
64-
_REG_SHAR = const(0x0009) # Source Hardware Address
65-
_REG_SIPR = const(0x000F) # Source IP Address
66-
_REG_PHYCFGR = {"w5100s": const(0x003C), "w5500": const(0x002E)}
67-
_REG_RCR = {"w5100s": const(0x0019), "w5500": const(0x001B)}
68-
_REG_RTR = {"w5100s": const(0x0017), "w5500": const(0x0019)}
69-
70-
# Wiznet5k Socket Registers
59+
# *** Wiznet Common Registers ***
60+
# Mode (used only for initialization and soft reset).
61+
_REG_MR = {"w5100s": const(0x0000), "w5500": const(0x0000), "w6100": const(0x4000)}
62+
# Gateway IPv4 Address.
63+
_REG_GAR = {"w5100s": const(0x0001), "w5500": const(0x0001), "w6100": const(0x4130)}
64+
# Subnet Mask Address
65+
_REG_SUBR = {"w5100s": const(0x0005), "w5500": const(0x0005), "w6100": const(0x4134)}
66+
# Chip version.
67+
_REG_VERSIONR = {
68+
"w5100s": const(0x0080),
69+
"w5500": const(0x0039),
70+
"w6100": const(0x0000),
71+
}
72+
# Source Hardware Address
73+
_REG_SHAR = {"w5100s": const(0x0009), "w5500": const(0x0009), "w6100": const(0x4120)}
74+
# Source IP Address
75+
_REG_SIPR = {"w5100s": const(0x000F), "w5500": const(0x000F), "w6100": const(0x4138)}
76+
# Register with link status flag (PHYCFGR for 5xxxx, PHYSR for 6100).
77+
_REG_LINK_FLAG = {
78+
"w5100s": const(0x003C),
79+
"w5500": const(0x002E),
80+
"w6100": const(0x3000),
81+
}
82+
_REG_RCR = {"w5100s": const(0x0019), "w5500": const(0x001B), "w6100": const(0x4204)}
83+
_REG_RTR = {"w5100s": const(0x0017), "w5500": const(0x0019), "w6100": const(0x4200)}
84+
85+
# Wiznet Socket Registers.
7186
_REG_SNMR = const(0x0000) # Socket n Mode
7287
_REG_SNCR = const(0x0001) # Socket n Command
7388
_REG_SNIR = const(0x0002) # Socket n Interrupt
@@ -301,7 +316,7 @@ def ip_address(self) -> bytes:
301316
302317
:return bytes: IP address as four bytes.
303318
"""
304-
return self._read(_REG_SIPR, 0x00, 4)
319+
return self._read(_REG_SIPR[self._chip_type], 0x00, 4)
305320

306321
@staticmethod
307322
def pretty_ip(ipv4: bytes) -> str:
@@ -338,7 +353,7 @@ def mac_address(self) -> bytes:
338353
339354
:return bytes: Six byte MAC address.
340355
"""
341-
return self._read(_REG_SHAR, 0x00, 6)
356+
return self._read(_REG_SHAR[self._chip_type], 0x00, 6)
342357

343358
@mac_address.setter
344359
def mac_address(self, address: Union[MacAddressRaw, str]) -> None:
@@ -357,7 +372,7 @@ def mac_address(self, address: Union[MacAddressRaw, str]) -> None:
357372
if len(address) != 6:
358373
raise ValueError()
359374
# Bytes conversion will raise ValueError if values are not 0-255
360-
self._write(_REG_SHAR, 0x04, bytes(address))
375+
self._write(_REG_SHAR[self._chip_type], 0x04, bytes(address))
361376
except ValueError:
362377
# pylint: disable=raise-missing-from
363378
raise ValueError("Invalid MAC address.")
@@ -417,7 +432,7 @@ def link_status(self) -> bool:
417432
:return bool: True if the link is up, False if the link is down.
418433
"""
419434
return bool(
420-
int.from_bytes(self._read(_REG_PHYCFGR[self._chip_type], 0x00), "big")
435+
int.from_bytes(self._read(_REG_LINK_FLAG[self._chip_type], 0x00), "big")
421436
& 0x01
422437
)
423438

@@ -431,8 +446,8 @@ def ifconfig(self) -> Tuple[bytes, bytes, bytes, bytes]:
431446
"""
432447
return (
433448
self.ip_address,
434-
self._read(_REG_SUBR, 0x00, 4),
435-
self._read(_REG_GAR, 0x00, 4),
449+
self._read(_REG_SUBR[self._chip_type], 0x00, 4),
450+
self._read(_REG_GAR[self._chip_type], 0x00, 4),
436451
self._dns,
437452
)
438453

@@ -451,9 +466,9 @@ def ifconfig(
451466
raise ValueError("IPv4 address must be 4 bytes.")
452467
ip_address, subnet_mask, gateway_address, dns_server = params
453468

454-
self._write(_REG_SIPR, 0x04, bytes(ip_address))
455-
self._write(_REG_SUBR, 0x04, bytes(subnet_mask))
456-
self._write(_REG_GAR, 0x04, bytes(gateway_address))
469+
self._write(_REG_SIPR[self._chip_type], 0x04, bytes(ip_address))
470+
self._write(_REG_SUBR[self._chip_type], 0x04, bytes(subnet_mask))
471+
self._write(_REG_GAR[self._chip_type], 0x04, bytes(gateway_address))
457472

458473
self._dns = bytes(dns_server)
459474

@@ -999,11 +1014,11 @@ def _check_link_status(self):
9991014

10001015
def _read_mr(self) -> int:
10011016
"""Read from the Mode Register (MR)."""
1002-
return int.from_bytes(self._read(_REG_MR, 0x00), "big")
1017+
return int.from_bytes(self._read(_REG_MR[self._chip_type], 0x00), "big")
10031018

10041019
def _write_mr(self, data: int) -> None:
10051020
"""Write to the mode register (MR)."""
1006-
self._write(_REG_MR, 0x04, data)
1021+
self._write(_REG_MR[self._chip_type], 0x04, data)
10071022

10081023
# *** Low Level Methods ***
10091024

@@ -1131,7 +1146,9 @@ def _read_sndipr(self, sock) -> bytes:
11311146
"""Read socket destination IP address."""
11321147
data = []
11331148
for offset in range(4):
1134-
data.append(self._read_socket_register(sock, _REG_SIPR + offset))
1149+
data.append(
1150+
self._read_socket_register(sock, _REG_SIPR[self._chip_type] + offset)
1151+
)
11351152
return bytes(data)
11361153

11371154
def write_sndipr(self, sock: int, ip_addr: bytes) -> None:

0 commit comments

Comments
 (0)