@@ -164,9 +164,12 @@ def __init__(self, spi_bus, cs, reset=None,
164
164
reset .value = False
165
165
time .sleep (0.1 )
166
166
167
- # Buffer for reading from module
167
+ # Buffer for reading params from module
168
168
self ._pbuff = bytearray (8 )
169
169
170
+
171
+ self ._rbuf = bytearray (1 )
172
+
170
173
# attempt to initialize the module
171
174
self ._ch_base_msb = 0
172
175
assert self ._w5100_init () == 1 , "Failed to initialize WIZnet module."
@@ -246,7 +249,7 @@ def mac_address(self, address):
246
249
:param tuple address: Hardware MAC address.
247
250
248
251
"""
249
- self ._write_n (REG_SHAR , 0x04 , address )
252
+ self .write (REG_SHAR , 0x04 , address )
250
253
251
254
def pretty_mac (self , mac ): # pylint: disable=no-self-use, invalid-name
252
255
"""Converts a bytearray MAC address to a
@@ -301,7 +304,7 @@ def ifconfig(self, params):
301
304
ip_address , subnet_mask , gateway_address , dns_server = params
302
305
303
306
# Set IP Address
304
- self ._write_n (REG_SIPR , 0x04 , ip_address )
307
+ self .write (REG_SIPR , 0x04 , ip_address )
305
308
306
309
# set subnet and gateway addresses
307
310
for octet in range (0 , 4 ):
@@ -385,40 +388,26 @@ def read(self, addr, callback, length=1, buffer=None):
385
388
bus_device .write (bytes ([addr & 0xFF ]))
386
389
bus_device .write (bytes ([callback ]))
387
390
if buffer is None :
388
- result = bytearray (length )
389
- bus_device .readinto (result )
390
- return result
391
+ self . _rxbuf = bytearray (length )
392
+ bus_device .readinto (self . _rxbuf )
393
+ return self . _rxbuf
391
394
bus_device .readinto (buffer , end = length )
392
395
return buffer
393
396
394
397
def write (self , addr , callback , data ):
395
- """Writes data to a register address.
396
- :param int addr: Register address.
397
- :param int cb: Common register block (?)
398
- :param int data: Data to write to the register.
399
-
400
- """
401
398
with self ._device as bus_device :
402
399
bus_device .write (bytes ([addr >> 8 ]))
403
400
bus_device .write (bytes ([addr & 0xFF ]))
404
401
bus_device .write (bytes ([callback ]))
405
- bus_device .write (bytes ([data ]))
406
402
407
- def _write_n (self , addr , callback , data ):
408
- """Writes data to a register address.
409
- :param int addr: Register address.
410
- :param int data: Data to write to the register.
411
- :param int len: Length of data to write.
412
-
413
- """
414
- with self ._device as bus_device :
415
- bus_device .write (bytes ([addr >> 8 ]))
416
- bus_device .write (bytes ([addr & 0xFF ]))
417
- bus_device .write (bytes ([callback ]))
418
- for i , _ in enumerate (data ):
419
- bus_device .write (bytes ([data [i ]]))
403
+ if hasattr (data ,'from_bytes' ):
404
+ bus_device .write (bytes ([data ]))
405
+ else :
406
+ for i , _ in enumerate (data ):
407
+ bus_device .write (bytes ([data [i ]]))
420
408
return len
421
409
410
+
422
411
# Socket-Register API
423
412
424
413
def _udp_remaining (self ):
@@ -689,7 +678,7 @@ def socket_write(self, socket_num, buffer):
689
678
self ._write_sntx_wr (socket_num , ptr )
690
679
691
680
cntl_byte = (0x14 + (socket_num << 5 ))
692
- self ._write_n (dst_addr , cntl_byte , buffer )
681
+ self .write (dst_addr , cntl_byte , buffer )
693
682
694
683
self ._write_sncr (socket_num , CMD_SOCK_SEND )
695
684
self ._read_sncr (socket_num )
@@ -815,7 +804,7 @@ def _write_socket(self, sock, address, data, length=None):
815
804
cntl_byte = (sock << 5 )+ 0x0C
816
805
if length is None :
817
806
return self .write (base + sock * CH_SIZE + address , cntl_byte , data )
818
- return self ._write_n (base + sock * CH_SIZE + address , cntl_byte , data )
807
+ return self .write (base + sock * CH_SIZE + address , cntl_byte , data )
819
808
820
809
def _read_socket (self , sock , address ):
821
810
"""Read a W5k socket register.
0 commit comments