diff --git a/adafruit_wiznet5k/adafruit_wiznet5k.py b/adafruit_wiznet5k/adafruit_wiznet5k.py index b4c3408..62f8085 100644 --- a/adafruit_wiznet5k/adafruit_wiznet5k.py +++ b/adafruit_wiznet5k/adafruit_wiznet5k.py @@ -107,9 +107,9 @@ # Socket n Interrupt Register _SNIR_SEND_OK = const(0x10) -_SNIR_TIMEOUT = const(0x08) +SNIR_TIMEOUT = const(0x08) _SNIR_RECV = const(0x04) -_SNIR_DISCON = const(0x02) +SNIR_DISCON = const(0x02) _SNIR_CON = const(0x01) _CH_SIZE = const(0x100) @@ -883,7 +883,7 @@ def socket_open(self, socket_num: int, conn_mode: int = _SNMR_TCP) -> int: time.sleep(0.00025) self._write_snmr(socket_num, conn_mode) - self._write_snir(socket_num, 0xFF) + self.write_snir(socket_num, 0xFF) if self.src_port > 0: # write to socket source port @@ -1104,7 +1104,7 @@ def socket_write( return 0 time.sleep(0.01) - self._write_snir(socket_num, _SNIR_SEND_OK) + self.write_snir(socket_num, _SNIR_SEND_OK) return ret # Socket-Register Methods @@ -1170,11 +1170,15 @@ def _read_snsr(self, sock: int) -> Optional[bytearray]: """Read Socket n Status Register.""" return self._read_socket(sock, _REG_SNSR) + def read_snir(self, sock: int) -> Optional[bytearray]: + """Read Socket n Status Register.""" + return self._read_socket(sock, _REG_SNIR) + def _write_snmr(self, sock: int, protocol: int) -> None: """Write to Socket n Mode Register.""" self._write_socket(sock, _REG_SNMR, protocol) - def _write_snir(self, sock: int, data: int) -> None: + def write_snir(self, sock: int, data: int) -> None: """Write to Socket n Interrupt Register.""" self._write_socket(sock, _REG_SNIR, data) diff --git a/adafruit_wiznet5k/adafruit_wiznet5k_socket.py b/adafruit_wiznet5k/adafruit_wiznet5k_socket.py index 96598b3..860ccc1 100644 --- a/adafruit_wiznet5k/adafruit_wiznet5k_socket.py +++ b/adafruit_wiznet5k/adafruit_wiznet5k_socket.py @@ -245,16 +245,25 @@ def __enter__(self): def __exit__(self, exc_type, exc_val, exc_tb) -> None: _the_interface.release_socket(self._socknum) if self._sock_type == SOCK_STREAM: - self._disconnect() - stamp = time.monotonic() - while self._status == wiznet5k.adafruit_wiznet5k.SNSR_SOCK_FIN_WAIT: - if time.monotonic() - stamp > 1000: - raise RuntimeError("Failed to disconnect socket") - self.close() - stamp = time.monotonic() - while self._status != wiznet5k.adafruit_wiznet5k.SNSR_SOCK_CLOSED: - if time.monotonic() - stamp > 1000: - raise RuntimeError("Failed to close socket") + _the_interface.write_snir( + self._socknum, 0xFF + ) # Reset socket interrupt register. + _the_interface.socket_disconnect(self._socknum) + mask = ( + wiznet5k.adafruit_wiznet5k.SNIR_TIMEOUT + | wiznet5k.adafruit_wiznet5k.SNIR_DISCON + ) + while not _the_interface.read_snir(self._socknum)[0] & mask: + pass + _the_interface.write_snir( + self._socknum, 0xFF + ) # Reset socket interrupt register. + _the_interface.socket_close(self._socknum) + while ( + _the_interface.socket_status(self._socknum)[0] + != wiznet5k.adafruit_wiznet5k.SNSR_SOCK_CLOSED + ): + pass # This works around problems with using a class method as a decorator. def _check_socket_closed(func): # pylint: disable=no-self-argument