25
25
"""
26
26
27
27
from collections import namedtuple
28
- from struct import unpack_from , pack_into # pylint:disable=unused-import
29
- from time import sleep , monotonic , monotonic_ns # pylint:disable=unused-import
28
+ from struct import unpack_from , pack_into
29
+ from time import sleep
30
30
from micropython import const
31
31
import adafruit_bus_device .spi_device as spi_device
32
32
from .canio import *
189
189
}
190
190
191
191
192
- def _has_elapsed (start , timeout_ms ):
193
- return (monotonic_ns () - start ) / 1000000 > timeout_ms
194
-
195
-
196
192
def _tx_buffer_status_decode (status_byte ):
197
193
out_str = "Status: "
198
194
# when CAN_H is disconnected?: 0x18
@@ -328,7 +324,7 @@ def initialize(self):
328
324
329
325
self ._set_mode (new_mode )
330
326
331
- def send (self , message_obj , wait_sent = True ): # pylint:disable=too-many-arguments
327
+ def send (self , message_obj , wait_sent = True ):
332
328
"""Send a message on the bus with the given data and id. If the message could not be sent
333
329
due to a full fifo or a bus error condition, RuntimeError is raised.
334
330
@@ -345,16 +341,16 @@ def send(self, message_obj, wait_sent=True): # pylint:disable=too-many-argument
345
341
sleep (0.010 )
346
342
347
343
send_confirmed = False
348
- start = monotonic_ns ()
349
- while not send_confirmed :
350
- if _has_elapsed (start , _SEND_TIMEOUT_MS ):
351
- raise RuntimeError ("Timeout occoured waiting for transmit confirmation" )
344
+ self ._timer .rewind_to (0.005 )
345
+ while not self ._timer .expired :
352
346
# the status register address is whatever tx_buff_n is, minus one?
353
347
tx_buff_status = self ._read_register (tx_buff .CTRL_REG )
354
348
self ._dbg (_tx_buffer_status_decode (tx_buff_status ))
355
349
send_confirmed = (tx_buff_status & _TXB_TXREQ_M ) == 0
350
+ if send_confirmed :
351
+ return True
356
352
357
- return True
353
+ raise RuntimeError ( "Timeout occoured waiting for transmit confirmation" )
358
354
359
355
@property
360
356
def unread_message_count (self ):
@@ -429,7 +425,6 @@ def _read_from_rx_buffers(self):
429
425
if status & 0b10 :
430
426
self ._read_rx_buffer (_READ_RX1 )
431
427
432
- # pylint:disable=too-many-arguments
433
428
def _write_message (self , tx_buffer , message_obj ):
434
429
435
430
if isinstance (message_obj , RemoteTransmissionRequest ):
@@ -477,8 +472,6 @@ def _write_message(self, tx_buffer, message_obj):
477
472
# send the frame based on the current buffers
478
473
self ._start_transmit (tx_buffer )
479
474
480
- # pylint:enable=too-many-arguments
481
-
482
475
# TODO: Priority
483
476
def _start_transmit (self , tx_buffer ):
484
477
#
@@ -643,9 +636,8 @@ def _set_mode(self, mode):
643
636
raise RuntimeError ("Unable to change mode" )
644
637
645
638
def _request_new_mode (self , mode ):
646
- start_time_ns = monotonic_ns ()
647
-
648
- while True :
639
+ self ._timer .rewind_to (0.200 )
640
+ while not self ._timer .expired :
649
641
# Request new mode
650
642
# This is inside the loop as sometimes requesting the new mode once doesn't work
651
643
# (usually when attempting to sleep)
@@ -655,9 +647,7 @@ def _request_new_mode(self, mode):
655
647
if (status & _MODE_MASK ) == mode :
656
648
return True
657
649
658
- # timeout
659
- if ((monotonic_ns () - start_time_ns ) / 1000000 ) > 200 :
660
- raise RuntimeError ("Timeout setting Mode" )
650
+ raise RuntimeError ("Timeout setting Mode" )
661
651
662
652
def _mod_register (self , register_addr , mask , new_value ):
663
653
"""There appears to be an interface on the MCP2515 that allows for
0 commit comments