PwrCAN Module

PwrCAN Module 13.2 is a multifunctional module designed for the PwrCAN bus, integrating isolated CAN communication and DC 9-24V power bus. The module also includes Pwr485 (with isolation) bus functionality and can provide isolated 5V power supply to the M5 host. The CAN communication part uses the CA-IS3050G isolated transceiver, and the RS485 part uses the CA-IS3082W isolated transceiver. The GPIOs related to CAN and RS485 communication can be selected through dip switches, and the 120-ohm terminal resistance at the CAN and RS485 outputs can also be selected through dip switches. The module’s power bus supports DC 9-24V wide voltage input, with the DC socket directly connected to the HT3.96 and XT30 power parts. The built-in isolated power module F0505S-2WR3 provides power to the M5 host. This module is suitable for fields such as robot control, protocol conversion, industrial automation, automotive communication systems, intelligent transportation, and building automation.

Supported Products:

PwrCANModule

Micropython Example:

 1# SPDX-FileCopyrightText: 2024 M5Stack Technology CO LTD
 2#
 3# SPDX-License-Identifier: MIT
 4
 5import os, sys, io
 6import M5
 7from M5 import *
 8from module import PwrCANModule
 9from module import PwrCANModuleRS485
10from unit import RS485Unit
11import time
12
13
14title0 = None
15label3 = None
16label0 = None
17label1 = None
18label2 = None
19pwrcan_0 = None
20pwrcan_1 = None
21rs485_0 = None
22
23
24def setup():
25    global title0, label3, label0, label1, label2, pwrcan_0, pwrcan_1, rs485_0
26
27    M5.begin()
28    Widgets.fillScreen(0x222222)
29    title0 = Widgets.Title(
30        "PwrCANModule CoreS3 Example", 3, 0xFFFFFF, 0x0000FF, Widgets.FONTS.DejaVu18
31    )
32    label3 = Widgets.Label("CAN Rec:", 0, 95, 1.0, 0xFFFFFF, 0x222222, Widgets.FONTS.DejaVu18)
33    label0 = Widgets.Label(
34        "CAN Message State: ", 0, 49, 1.0, 0xFFFFFF, 0x222222, Widgets.FONTS.DejaVu18
35    )
36    label1 = Widgets.Label(
37        "RS485 Message State: ", 0, 138, 1.0, 0xFFFFFF, 0x222222, Widgets.FONTS.DejaVu18
38    )
39    label2 = Widgets.Label("RS485 Rec:", 0, 179, 1.0, 0xFFFFFF, 0x222222, Widgets.FONTS.DejaVu18)
40
41    pwrcan_0 = PwrCANModule(0, 17, 18, PwrCANModule.NORMAL, baudrate=1000000)
42    pwrcan_1 = PwrCANModuleRS485(1, baudrate=115200, bits=8, parity=None, stop=1, tx=13, rx=7)
43    rs485_0 = RS485Unit(
44        2,
45        port=(1, 2),
46        baudrate=115200,
47        bits=8,
48        parity=None,
49        stop=1,
50        txbuf=256,
51        rxbuf=256,
52        timeout=0,
53        timeout_char=0,
54        invert=0,
55        flow=0,
56    )
57
58
59def loop():
60    global title0, label3, label0, label1, label2, pwrcan_0, pwrcan_1, rs485_0
61    M5.update()
62    if M5.Touch.getCount():
63        pwrcan_0.send("uiflow2", 0, timeout=0, rtr=False, extframe=False)
64        label0.setText(str("CAN Message State: Send"))
65        pwrcan_1.write("RS485_uiflow2" + "\r\n")
66        label1.setText(str("RS485 Message State: Send"))
67        time.sleep(1)
68    else:
69        label0.setText(str("CAN Message State: Not Send"))
70        label1.setText(str("RS485 Message State: Not Send"))
71    if pwrcan_0.any(0):
72        label3.setText(str((str("CAN Rec:") + str((pwrcan_0.recv(0, timeout=5000))))))
73    if rs485_0.any():
74        label2.setText(str((str("RS485 Rec:") + str((rs485_0.read())))))
75
76
77if __name__ == "__main__":
78    try:
79        setup()
80        while True:
81            loop()
82    except (Exception, KeyboardInterrupt) as e:
83        try:
84            from utility import print_error_msg
85
86            print_error_msg(e)
87        except ImportError:
88            print("please update to latest firmware")

UIFLOW2 Example:

example.png

pwrcan_cores3_example.m5f2

class PwrCANModule

Constructors

class PwrCANModule(id, mode, tx, rx, prescaler=32, sjw=3, bs1=15, bs2=4, triple_sampling=False)

Initialise the CAN bus with the given parameters:

  • id is the can bus id

  • tx is the pin to use for transmitting data

  • rx is the pin to use for receiving data

  • mode is one of: NORMAL, NO_ACKNOWLEDGE, LISTEN_ONLY

  • prescaler is the value by which the CAN input clock is divided to generate the nominal bit time quanta. The prescaler can be a value between 1 and 1024 inclusive for classic CAN.

  • sjw is the resynchronisation jump width in units of time quanta for nominal bits; it can be a value between 1 and 4 inclusive for classic CAN.

  • bs1 defines the location of the sample point in units of the time quanta for nominal bits; it can be a value between 1 and 16 inclusive for classic CAN.

  • bs2 defines the location of the transmit point in units of the time quanta for nominal bits; it can be a value between 1 and 8 inclusive for classic CAN.

  • triple_sampling is Enables triple sampling when the TWAI controller samples a bit

UIFLOW2:

init.png

Methods

PwrCANModule class inherits CAN class, See hardware.CAN for more details.

class PwrCANModuleRS485

Constructors

class PwrCANModuleRS485(id, baudrate=9600, bits=8, parity=None, stop=1, *, ...)

Construct a UART object of the given id.

For more parameters, please refer to init.

UIFLOW2:

init_rs485.png

Methods

PwrCANModuleRS485.init(baudrate=9600, bits=8, parity=None, stop=1, *, ...)

Initialise the UART bus with the given parameters:

  • baudrate is the clock rate.

  • bits is the number of bits per character, 7, 8 or 9.

  • parity is the parity, None, 0 (even) or 1 (odd).

  • stop is the number of stop bits, 1 or 2.

Additional keyword-only parameters that may be supported by a port are:

  • tx specifies the TX pin to use.

  • rx specifies the RX pin to use.

  • rts specifies the RTS (output) pin to use for hardware receive flow control.

  • cts specifies the CTS (input) pin to use for hardware transmit flow control.

  • txbuf specifies the length in characters of the TX buffer.

  • rxbuf specifies the length in characters of the RX buffer.

  • timeout specifies the time to wait for the first character (in ms).

  • timeout_char specifies the time to wait between characters (in ms).

  • invert specifies which lines to invert.

    • 0 will not invert lines (idle state of both lines is logic high).

    • PwrCANModuleRS485.INV_TX will invert TX line (idle state of TX line now logic low).

    • PwrCANModuleRS485.INV_RX will invert RX line (idle state of RX line now logic low).

    • PwrCANModuleRS485.INV_TX | PwrCANModuleRS485.INV_RX will invert both lines (idle state at logic low).

  • flow specifies which hardware flow control signals to use. The value is a bitmask.

    • 0 will ignore hardware flow control signals.

    • PwrCANModuleRS485.RTS will enable receive flow control by using the RTS output pin to signal if the receive FIFO has sufficient space to accept more data.

    • PwrCANModuleRS485.CTS will enable transmit flow control by pausing transmission when the CTS input pin signals that the receiver is running low on buffer space.

    • PwrCANModuleRS485.RTS | PwrCANModuleRS485.CTS will enable both, for full hardware flow control.

Note

It is possible to call init() multiple times on the same object in order to reconfigure UART on the fly. That allows using single UART peripheral to serve different devices attached to different GPIO pins. Only one device can be served at a time in that case. Also do not call deinit() as it will prevent calling init() again.

UIFLOW2:

setup.png

PwrCANModuleRS485.deinit()

Turn off the UART bus.

Note

You will not be able to call init() on the object after deinit(). A new instance needs to be created in that case.

UIFLOW2:

deinit.png

PwrCANModuleRS485.any()

Returns an integer counting the number of characters that can be read without blocking. It will return 0 if there are no characters available and a positive number if there are characters. The method may return 1 even if there is more than one character available for reading.

For more sophisticated querying of available characters use select.poll:

poll = select.poll()
poll.register(uart, select.POLLIN)
poll.poll(timeout)

UIFLOW2:

any.png

PwrCANModuleRS485.read([nbytes])

Read characters. If nbytes is specified then read at most that many bytes, otherwise read as much data as possible. It may return sooner if a timeout is reached. The timeout is configurable in the constructor.

Return value: a bytes object containing the bytes read in. Returns None on timeout.

UIFLOW2:

read_all.png

read_bytes.png

PwrCANModuleRS485.readinto(buf[, nbytes])

Read bytes into the buf. If nbytes is specified then read at most that many bytes. Otherwise, read at most len(buf) bytes. It may return sooner if a timeout is reached. The timeout is configurable in the constructor.

Return value: number of bytes read and stored into buf or None on timeout.

UIFLOW2:

readinto.png

PwrCANModuleRS485.readline()

Read a line, ending in a newline character. It may return sooner if a timeout is reached. The timeout is configurable in the constructor.

Return value: the line read or None on timeout.

UIFLOW2:

readline.png

PwrCANModuleRS485.write(buf)

Write the buffer of bytes to the bus.

Return value: number of bytes written or None on timeout.

UIFLOW2:

write.png

write_line.png

write_list.png

PwrCANModuleRS485.sendbreak()

Send a break condition on the bus. This drives the bus low for a duration longer than required for a normal transmission of a character.

UIFLOW2:

sendbreak.png

PwrCANModuleRS485.flush()

Waits until all data has been sent. In case of a timeout, an exception is raised. The timeout duration depends on the tx buffer size and the baud rate. Unless flow control is enabled, a timeout should not occur.

Note

For the rp2, esp8266 and nrf ports the call returns while the last byte is sent. If required, a one character wait time has to be added in the calling script.

UIFLOW2:

flush.png

PwrCANModuleRS485.txdone()

Tells whether all data has been sent or no data transfer is happening. In this case, it returns True. If a data transmission is ongoing it returns False.

Note

For the rp2, esp8266 and nrf ports the call may return True even if the last byte of a transfer is still being sent. If required, a one character wait time has to be added in the calling script.

UIFLOW2:

txdone.png