176
176
# pylint: enable=bad-whitespace
177
177
178
178
179
+ def _reset (pin ):
180
+ pin .direction = Direction .OUTPUT
181
+ pin .value = True
182
+ time .sleep (0.1 )
183
+ pin .value = False
184
+ time .sleep (0.5 )
185
+ pin .value = True
186
+ time .sleep (0.1 )
179
187
180
188
def reverse_bit (num ):
181
189
"""Turn an LSB byte to an MSB byte, and vice versa. Used for SPI as
@@ -201,13 +209,10 @@ def __init__(self, *, debug=False, reset=None):
201
209
"""
202
210
self .debug = debug
203
211
if reset :
204
- reset .direction = Direction .OUTPUT
205
- reset .value = True
206
- time .sleep (0.1 )
207
- reset .value = False
208
- time .sleep (0.5 )
209
- reset .value = True
210
- time .sleep (0.1 )
212
+ if debug :
213
+ print ("Resetting" )
214
+ _reset (reset )
215
+
211
216
try :
212
217
self ._wakeup ()
213
218
self .get_firmware_version () # first time often fails, try 2ce
@@ -311,7 +316,11 @@ def call_function(self, command, response_length=0, params=[], timeout=1): # pyl
311
316
for i , val in enumerate (params ):
312
317
data [2 + i ] = val
313
318
# Send frame and wait for response.
314
- self ._write_frame (data )
319
+ try :
320
+ self ._write_frame (data )
321
+ except OSError :
322
+ self ._wakeup ()
323
+ return None
315
324
if not self ._wait_ready (timeout ):
316
325
return None
317
326
# Verify ACK response and wait to be ready for function response.
@@ -470,27 +479,41 @@ def _write_data(self, framebytes):
470
479
471
480
class PN532_I2C (PN532 ):
472
481
"""Driver for the PN532 connected over I2C."""
473
- def __init__ (self , i2c , * , irq = None , reset = None , debug = False ):
482
+ def __init__ (self , i2c , * , irq = None , reset = None , req = None , debug = False ):
474
483
"""Create an instance of the PN532 class using I2C. Note that PN532
475
484
uses clock stretching. Optional IRQ pin (not used),
476
485
reset pin and debugging output.
477
486
"""
478
487
self .debug = debug
479
488
self ._irq = irq
489
+ self ._req = req
490
+ if reset :
491
+ _reset (reset )
480
492
self ._i2c = i2c_device .I2CDevice (i2c , _I2C_ADDRESS )
481
493
super ().__init__ (debug = debug , reset = reset )
482
494
483
495
def _wakeup (self ): # pylint: disable=no-self-use
484
496
"""Send any special commands/data to wake up PN532"""
497
+ if self ._req :
498
+ self ._req .direction = Direction .OUTPUT
499
+ self ._req .value = True
500
+ time .sleep (0.1 )
501
+ self ._req .value = False
502
+ time .sleep (0.1 )
503
+ self ._req .value = True
485
504
time .sleep (0.5 )
486
505
487
506
def _wait_ready (self , timeout = 1 ):
488
507
"""Poll PN532 if status byte is ready, up to `timeout` seconds"""
489
508
status = bytearray (1 )
490
509
timestamp = time .monotonic ()
491
510
while (time .monotonic () - timestamp ) < timeout :
492
- with self ._i2c :
493
- self ._i2c .readinto (status )
511
+ try :
512
+ with self ._i2c :
513
+ self ._i2c .readinto (status )
514
+ except OSError :
515
+ self ._wakeup ()
516
+ continue
494
517
if status == b'\x01 ' :
495
518
return True # No longer busy
496
519
else :
0 commit comments