Skip to content

Commit c339050

Browse files
committed
tweaks, fixed docs
1 parent eadb9c1 commit c339050

File tree

4 files changed

+84
-72
lines changed

4 files changed

+84
-72
lines changed

adafruit_mcp2515/__init__.py

Lines changed: 20 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -209,24 +209,7 @@ def _tx_buffer_status_decode(status_byte):
209209

210210

211211
class MCP2515: # pylint:disable=too-many-instance-attributes
212-
"""
213-
A common shared-bus protocol.
214-
215-
:param ~busio.SPI spi: The SPI bus used to communicate with the MCP2515
216-
:param ~digitalio.DigitalInOut cs_pin: SPI bus enable pin
217-
:param int baudrate: The bit rate of the bus in Hz, using a 16Mhz crystal. All devices on\
218-
the bus must agree on this value. Defaults to 250000.
219-
:param bool loopback: Receive only packets sent from this device, and send only to\
220-
this device. Requires that `silent` is also set to `False`, but only prevents\
221-
transimssion to other devices. Otherwise the send/receive behavior is normal.
222-
:param bool silent:When `True` the controller does not transmit and all messages\
223-
are received, ignoring errors and filters. This mode can be used to “sniff” a CAN\
224-
bus without interfering. Defaults to `False`.
225-
:param bool auto_restart: **Not supported by hardware. An `AttributeError`
226-
will be raised if `auto_restart` is set to `True`** If `True`, will restart\
227-
communications after entering bus-off state. Defaults to `False`.
228-
:param bool debug: If `True`, will enable printing debug information. Defaults to `False`.
229-
"""
212+
"""A common shared-bus protocol."""
230213

231214
def __init__(
232215
self,
@@ -239,6 +222,18 @@ def __init__(
239222
auto_restart: bool = False,
240223
debug: bool = False
241224
):
225+
"""A common shared-bus protocol.
226+
:param ~busio.SPI spi: The SPI bus used to communicate with the MCP2515
227+
:param ~digitalio.DigitalInOut cs_pin: SPI bus enable pin
228+
:param int baudrate: The bit rate of the bus in Hz, using a 16Mhz crystal. All devices on the bus must agree on this value. Defaults to 250000.
229+
:param bool loopback: Receive only packets sent from this device, and send only to this device. Requires that `silent` is also set to `False`, but only prevents transmission to other devices. Otherwise the send/receive behavior is normal.
230+
:param bool silent: When `True` the controller does not transmit and all messages are received, ignoring errors and filters. This mode can be used to “sniff” a CAN bus without interfering. Defaults to `False`.
231+
:param bool auto_restart: **Not supported by hardware. An `AttributeError` will be raised if `auto_restart` is set to `True`**
232+
233+
If `True`, will restart\ communications after entering bus-off state. Defaults to `False`.
234+
235+
:param bool debug: If `True`, will enable printing debug information. Defaults to `False`.
236+
"""
242237

243238
if loopback and not silent:
244239
raise AttributeError("Loopback mode requires silent to be set")
@@ -329,7 +324,7 @@ def initialize(self):
329324

330325
self._set_mode(new_mode)
331326

332-
def send(self, message_obj, wait_sent=True):
327+
def send(self, message_obj):
333328
"""Send a message on the bus with the given data and id. If the message could not be sent
334329
due to a full fifo or a bus error condition, RuntimeError is raised.
335330
@@ -339,24 +334,10 @@ def send(self, message_obj, wait_sent=True):
339334

340335
# TODO: Timeout
341336
tx_buff = self._get_tx_buffer() # info = addr.
337+
if tx_buff is None:
338+
raise RuntimeError("No transmit buffer available to send")
342339

343-
# TODO: set buffer priority
344-
self._write_message(tx_buff, message_obj)
345-
if not wait_sent:
346-
return True
347-
sleep(0.010)
348-
349-
send_confirmed = False
350-
self._timer.rewind_to(0.005)
351-
while not self._timer.expired:
352-
# the status register address is whatever tx_buff_n is, minus one?
353-
tx_buff_status = self._read_register(tx_buff.CTRL_REG)
354-
self._dbg(_tx_buffer_status_decode(tx_buff_status))
355-
send_confirmed = (tx_buff_status & _TXB_TXREQ_M) == 0
356-
if send_confirmed:
357-
return True
358-
359-
raise RuntimeError("Timeout occurred waiting for transmit confirmation")
340+
return self._write_message(tx_buff, message_obj)
360341

361342
@property
362343
def unread_message_count(self):
@@ -433,6 +414,8 @@ def _read_from_rx_buffers(self):
433414

434415
def _write_message(self, tx_buffer, message_obj):
435416

417+
if tx_buffer is None:
418+
raise RuntimeError("No transmit buffer available to send")
436419
if isinstance(message_obj, RemoteTransmissionRequest):
437420
dlc = message_obj.length
438421
else:
@@ -477,6 +460,7 @@ def _write_message(self, tx_buffer, message_obj):
477460

478461
# send the frame based on the current buffers
479462
self._start_transmit(tx_buffer)
463+
return True
480464

481465
# TODO: Priority
482466
def _start_transmit(self, tx_buffer):

examples/mcp2515_receive.py

Lines changed: 23 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -2,48 +2,39 @@
22
#
33
# SPDX-License-Identifier: MIT
44

5-
# import board
6-
# import busio
7-
# import digitalio
8-
# import adafruit_mcp2515
9-
# from adafruit_mcp2515.canio import Timer
10-
11-
12-
from digitalio import DigitalInOut
13-
from board import D5 as CS_PIN, SPI
5+
import digitalio
6+
import board
7+
import busio
148
from adafruit_mcp2515.canio import Timer
9+
from adafruit_mcp2515.canio import RemoteTransmissionRequest, Message
1510
from adafruit_mcp2515 import MCP2515 as CAN
1611

17-
# from board import CAN_RX, CAN_TX
18-
19-
20-
def bus():
21-
cs = DigitalInOut(CS_PIN)
22-
cs.switch_to_output()
23-
return CAN(SPI(), cs)
24-
25-
26-
# cs = digitalio.DigitalInOut(board.D5)
27-
# cs.direction = digitalio.Direction.OUTPUT
28-
# spi = busio.SPI(board.SCK, board.MOSI, board.MISO)
29-
# can = adafruit_mcp2515.MCP2515(spi, cs)
12+
cs = digitalio.DigitalInOut(board.D5)
13+
cs.switch_to_output()
14+
spi = busio.SPI(board.SCK, board.MOSI, board.MISO)
15+
mcp = CAN(spi, cs)
3016

3117
t = Timer(timeout=5)
18+
3219
while True:
33-
with bus() as can, can.listen(timeout=1.0) as listener:
20+
# print occationally to show we're alive
21+
if t.expired:
22+
print(".", end="")
23+
t.rewind_to(1)
24+
with mcp.listen(timeout=1.0) as listener:
3425
message_count = listener.in_waiting()
35-
print(message_count, "messages available")
26+
3627
if message_count == 0:
3728
continue
3829
for _i in range(message_count):
30+
print(message_count, "messages available")
3931
msg = listener.receive()
4032
print("Message received from ", hex(msg.id))
41-
print("message data:", msg.data)
42-
message_str = "::".join(["0x{:02X}".format(i) for i in msg.data])
43-
print(message_str)
44-
print("RTR:", msg.rtr)
33+
if isinstance(msg, Message):
34+
print("Message data:", msg.data)
35+
message_str = "::".join(["0x{:02X}".format(i) for i in msg.data])
36+
print(message_str)
37+
38+
if isinstance(msg, RemoteTransmissionRequest):
39+
print("RTR length:", msg.length)
4540
print("")
46-
# instead of sleeping, pool for messages to fill queue
47-
t.rewind_to(5)
48-
while not t.expired:
49-
message_count = listener.in_waiting()

examples/mcp2515_send_and_receive.py

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
# SPDX-FileCopyrightText: Copyright (c) 2020 Bryan Siepert for Adafruit Industries
2+
#
3+
# SPDX-License-Identifier: MIT
4+
import board
5+
import busio
6+
from digitalio import DigitalInOut
7+
from adafruit_mcp2515.canio import Message, RemoteTransmissionRequest
8+
from adafruit_mcp2515 import MCP2515 as CAN
9+
10+
NODE_ID = 0x1234ABCD
11+
12+
cs = DigitalInOut(board.D5)
13+
cs.switch_to_output()
14+
spi = busio.SPI(board.SCK, board.MOSI, board.MISO)
15+
16+
can_bus = CAN(spi, cs)
17+
while True:
18+
with can_bus.listen(timeout=1.0) as listener:
19+
20+
message = Message(id=NODE_ID, data=b"adafruit", extended=True)
21+
send_success = can_bus.send(message)
22+
print("Send success:", send_success)
23+
message_count = listener.in_waiting()
24+
if message_count == 0:
25+
continue
26+
print(message_count, "messages available")
27+
for _i in range(message_count):
28+
msg = listener.receive()
29+
print("Message from ", hex(msg.id), "extended:", msg.extended)
30+
if isinstance(msg, Message):
31+
print("message data:", msg.data)
32+
if isinstance(msg, RemoteTransmissionRequest):
33+
print("RTR length:", msg.length)
34+
print("")

examples/mcp2515_simpletest.py

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
import board
66
import busio
77
from digitalio import DigitalInOut
8-
from adafruit_mcp2515.canio import Message
8+
from adafruit_mcp2515.canio import Message, RemoteTransmissionRequest
99
from adafruit_mcp2515 import MCP2515 as CAN
1010

1111

@@ -20,12 +20,15 @@
2020
with can_bus.listen(timeout=1.0) as listener:
2121

2222
message = Message(id=0x1234ABCD, data=b"adafruit", extended=True)
23-
can_bus.send(message)
23+
send_success = can_bus.send(message)
24+
print("Send success:", send_success)
2425
message_count = listener.in_waiting()
2526
print(message_count, "messages available")
2627
for _i in range(message_count):
2728
msg = listener.receive()
2829
print("Message from ", hex(msg.id))
29-
print("message data:", msg.data)
30-
print("")
30+
if isinstance(msg, Message):
31+
print("message data:", msg.data)
32+
if isinstance(msg, RemoteTransmissionRequest):
33+
print("RTR length:", msg.length)
3134
sleep(1)

0 commit comments

Comments
 (0)