Skip to content

Commit bafc6db

Browse files
committed
removed monotonic use
1 parent 66fe85a commit bafc6db

File tree

1 file changed

+11
-21
lines changed

1 file changed

+11
-21
lines changed

adafruit_mcp2515/__init__.py

Lines changed: 11 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,8 @@
2525
"""
2626

2727
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
3030
from micropython import const
3131
import adafruit_bus_device.spi_device as spi_device
3232
from .canio import *
@@ -189,10 +189,6 @@
189189
}
190190

191191

192-
def _has_elapsed(start, timeout_ms):
193-
return (monotonic_ns() - start) / 1000000 > timeout_ms
194-
195-
196192
def _tx_buffer_status_decode(status_byte):
197193
out_str = "Status: "
198194
# when CAN_H is disconnected?: 0x18
@@ -328,7 +324,7 @@ def initialize(self):
328324

329325
self._set_mode(new_mode)
330326

331-
def send(self, message_obj, wait_sent=True): # pylint:disable=too-many-arguments
327+
def send(self, message_obj, wait_sent=True):
332328
"""Send a message on the bus with the given data and id. If the message could not be sent
333329
due to a full fifo or a bus error condition, RuntimeError is raised.
334330
@@ -345,16 +341,16 @@ def send(self, message_obj, wait_sent=True): # pylint:disable=too-many-argument
345341
sleep(0.010)
346342

347343
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:
352346
# the status register address is whatever tx_buff_n is, minus one?
353347
tx_buff_status = self._read_register(tx_buff.CTRL_REG)
354348
self._dbg(_tx_buffer_status_decode(tx_buff_status))
355349
send_confirmed = (tx_buff_status & _TXB_TXREQ_M) == 0
350+
if send_confirmed:
351+
return True
356352

357-
return True
353+
raise RuntimeError("Timeout occoured waiting for transmit confirmation")
358354

359355
@property
360356
def unread_message_count(self):
@@ -429,7 +425,6 @@ def _read_from_rx_buffers(self):
429425
if status & 0b10:
430426
self._read_rx_buffer(_READ_RX1)
431427

432-
# pylint:disable=too-many-arguments
433428
def _write_message(self, tx_buffer, message_obj):
434429

435430
if isinstance(message_obj, RemoteTransmissionRequest):
@@ -477,8 +472,6 @@ def _write_message(self, tx_buffer, message_obj):
477472
# send the frame based on the current buffers
478473
self._start_transmit(tx_buffer)
479474

480-
# pylint:enable=too-many-arguments
481-
482475
# TODO: Priority
483476
def _start_transmit(self, tx_buffer):
484477
#
@@ -643,9 +636,8 @@ def _set_mode(self, mode):
643636
raise RuntimeError("Unable to change mode")
644637

645638
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:
649641
# Request new mode
650642
# This is inside the loop as sometimes requesting the new mode once doesn't work
651643
# (usually when attempting to sleep)
@@ -655,9 +647,7 @@ def _request_new_mode(self, mode):
655647
if (status & _MODE_MASK) == mode:
656648
return True
657649

658-
# timeout
659-
if ((monotonic_ns() - start_time_ns) / 1000000) > 200:
660-
raise RuntimeError("Timeout setting Mode")
650+
raise RuntimeError("Timeout setting Mode")
661651

662652
def _mod_register(self, register_addr, mask, new_value):
663653
"""There appears to be an interface on the MCP2515 that allows for

0 commit comments

Comments
 (0)