File: ucanbus.py

package info (click to toggle)
python-can 4.5.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 3,372 kB
  • sloc: python: 25,840; makefile: 38; sh: 20
file content (333 lines) | stat: -rw-r--r-- 11,354 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
import logging
from threading import Event

from can import (
    BusABC,
    BusState,
    CanError,
    CanInitializationError,
    CanOperationError,
    CanProtocol,
    Message,
)

from .constants import *
from .exceptions import UcanException
from .structures import *
from .ucan import UcanServer

log = logging.getLogger("can.systec")


class Ucan(UcanServer):
    """
    Wrapper around UcanServer to read messages with timeout using events.
    """

    def __init__(self):
        super().__init__()
        self._msg_received_event = Event()

    def can_msg_received_event(self, channel):
        self._msg_received_event.set()

    def read_can_msg(self, channel, count, timeout):
        self._msg_received_event.clear()
        if self.get_msg_pending(channel, PendingFlags.PENDING_FLAG_RX_DLL) == 0:
            if not self._msg_received_event.wait(timeout):
                return None, False
        return super().read_can_msg(channel, 1)


class UcanBus(BusABC):
    """
    The CAN Bus implemented for the SYSTEC interface.
    """

    BITRATES = {
        10000: Baudrate.BAUD_10kBit,
        20000: Baudrate.BAUD_20kBit,
        50000: Baudrate.BAUD_50kBit,
        100000: Baudrate.BAUD_100kBit,
        125000: Baudrate.BAUD_125kBit,
        250000: Baudrate.BAUD_250kBit,
        500000: Baudrate.BAUD_500kBit,
        800000: Baudrate.BAUD_800kBit,
        1000000: Baudrate.BAUD_1MBit,
    }

    def __init__(self, channel, can_filters=None, **kwargs):
        """
        :param int channel:
            The Channel id to create this bus with.

        :param list can_filters:
            See :meth:`can.BusABC.set_filters`.

        Backend Configuration

        :param int bitrate:
            Channel bitrate in bit/s.
            Default is 500000.

        :param int device_number:
            The device number of the USB-CAN.
            Valid values: 0 through 254. Special value 255 is reserved to detect the first connected device (should only
            be used, in case only one module is connected to the computer).
            Default is 255.

        :param can.bus.BusState state:
            BusState of the channel.
            Default is ACTIVE.

        :param bool receive_own_messages:
            If messages transmitted should also be received back.
            Default is False.

        :param int rx_buffer_entries:
            The maximum number of entries in the receive buffer.
            Default is 4096.

        :param int tx_buffer_entries:
            The maximum number of entries in the transmit buffer.
            Default is 4096.

        :raises ValueError:
            If invalid input parameter were passed.

        :raises ~can.exceptions.CanInterfaceNotImplementedError:
            If the platform is not supported.

        :raises ~can.exceptions.CanInitializationError:
            If hardware or CAN interface initialization failed.
        """
        try:
            self._ucan = Ucan()
        except CanError as error:
            raise error
        except Exception as exception:
            raise CanInitializationError(
                "The SYSTEC ucan library has not been initialized."
            ) from exception

        self.channel = int(channel)
        self._can_protocol = CanProtocol.CAN_20

        device_number = int(kwargs.get("device_number", ANY_MODULE))

        # configuration options
        bitrate = kwargs.get("bitrate", 500000)
        if bitrate not in self.BITRATES:
            raise ValueError(f"Invalid bitrate {bitrate}")

        state = kwargs.get("state", BusState.ACTIVE)
        if state is BusState.ACTIVE or state is BusState.PASSIVE:
            self._state = state
        else:
            raise ValueError("BusState must be Active or Passive")

        # get parameters
        self._params = {
            "mode": Mode.MODE_NORMAL
            | (Mode.MODE_TX_ECHO if kwargs.get("receive_own_messages") else 0)
            | (Mode.MODE_LISTEN_ONLY if state is BusState.PASSIVE else 0),
            "BTR": self.BITRATES[bitrate],
        }
        # get extra parameters
        if kwargs.get("rx_buffer_entries"):
            self._params["rx_buffer_entries"] = int(kwargs.get("rx_buffer_entries"))
        if kwargs.get("tx_buffer_entries"):
            self._params["tx_buffer_entries"] = int(kwargs.get("tx_buffer_entries"))

        try:
            self._ucan.init_hardware(device_number=device_number)
            self._ucan.init_can(self.channel, **self._params)
            hw_info_ex, _, _ = self._ucan.get_hardware_info()
            self.channel_info = (
                f"{self._ucan.get_product_code_message(hw_info_ex.product_code)}, "
                f"S/N {hw_info_ex.serial}, "
                f"CH {self.channel}, "
                f"BTR {self._ucan.get_baudrate_message(self.BITRATES[bitrate])}"
            )
        except UcanException as exception:
            raise CanInitializationError() from exception

        self._is_filtered = False

        super().__init__(
            channel=channel,
            can_filters=can_filters,
            **kwargs,
        )

    def _recv_internal(self, timeout):
        try:
            message, _ = self._ucan.read_can_msg(self.channel, 1, timeout)
        except UcanException as exception:
            raise CanOperationError() from exception

        if not message:
            return None, False

        msg = Message(
            timestamp=float(message[0].time) / 1000.0,
            is_remote_frame=bool(message[0].frame_format & MsgFrameFormat.MSG_FF_RTR),
            is_extended_id=bool(message[0].frame_format & MsgFrameFormat.MSG_FF_EXT),
            arbitration_id=message[0].id,
            dlc=len(message[0].data),
            data=message[0].data,
        )
        return msg, self._is_filtered

    def send(self, msg, timeout=None):
        """
        Sends one CAN message.

        When a transmission timeout is set the firmware tries to send
        a message within this timeout. If it could not be sent the firmware sets
        the "auto delete" state. Within this state all transmit CAN messages for
        this channel will be deleted automatically for not blocking the other channel.

        :param can.Message msg:
            The CAN message.

        :param float timeout:
            Transmit timeout in seconds (value 0 switches off the "auto delete")

        :raises ~can.exceptions.CanOperationError:
            If the message could not be sent.
        """
        try:
            if timeout is not None and timeout >= 0:
                self._ucan.set_tx_timeout(self.channel, int(timeout * 1000))

            message = CanMsg(
                msg.arbitration_id,
                MsgFrameFormat.MSG_FF_STD
                | (MsgFrameFormat.MSG_FF_EXT if msg.is_extended_id else 0)
                | (MsgFrameFormat.MSG_FF_RTR if msg.is_remote_frame else 0),
                msg.data,
                msg.dlc,
            )
            self._ucan.write_can_msg(self.channel, [message])
        except UcanException as exception:
            raise CanOperationError() from exception

    @staticmethod
    def _detect_available_configs():
        configs = []
        try:
            # index, is_used, hw_info_ex, init_info
            for _, _, hw_info_ex, _ in Ucan.enumerate_hardware():
                configs.append(
                    {
                        "interface": "systec",
                        "channel": Channel.CHANNEL_CH0,
                        "device_number": hw_info_ex.device_number,
                    }
                )
                if Ucan.check_support_two_channel(hw_info_ex):
                    configs.append(
                        {
                            "interface": "systec",
                            "channel": Channel.CHANNEL_CH1,
                            "device_number": hw_info_ex.device_number,
                        }
                    )
        except Exception:
            log.warning("The SYSTEC ucan library has not been initialized.")
        return configs

    def _apply_filters(self, filters):
        try:
            if filters and len(filters) == 1:
                can_id = filters[0]["can_id"]
                can_mask = filters[0]["can_mask"]
                self._ucan.set_acceptance(self.channel, can_mask, can_id)
                self._is_filtered = True
                log.info("Hardware filtering on ID 0x%X, mask 0x%X", can_id, can_mask)
            else:
                self._ucan.set_acceptance(self.channel)
                self._is_filtered = False
                log.info("Hardware filtering has been disabled")
        except UcanException as exception:
            raise CanOperationError() from exception

    def flush_tx_buffer(self):
        """
        Flushes the transmit buffer.

        :raises ~can.exceptions.CanError:
            If flushing of the transmit buffer failed.
        """
        log.info("Flushing transmit buffer")
        try:
            self._ucan.reset_can(self.channel, ResetFlags.RESET_ONLY_TX_BUFF)
        except UcanException as exception:
            raise CanOperationError() from exception

    @staticmethod
    def create_filter(extended, from_id, to_id, rtr_only, rtr_too):
        """
        Calculates AMR and ACR using CAN-ID as parameter.

        :param bool extended:
            if True parameters from_id and to_id contains 29-bit CAN-ID

        :param int from_id:
            first CAN-ID which should be received

        :param int to_id:
            last CAN-ID which should be received

        :param bool rtr_only:
            if True only RTR-Messages should be received, and rtr_too will be ignored

        :param bool rtr_too:
            if True CAN data frames and RTR-Messages should be received

        :return: Returns list with one filter containing a "can_id", a "can_mask" and "extended" key.
        """
        return [
            {
                "can_id": Ucan.calculate_acr(
                    extended, from_id, to_id, rtr_only, rtr_too
                ),
                "can_mask": Ucan.calculate_amr(
                    extended, from_id, to_id, rtr_only, rtr_too
                ),
                "extended": extended,
            }
        ]

    @property
    def state(self):
        return self._state

    @state.setter
    def state(self, new_state):
        if self._state is not BusState.ERROR and (
            new_state is BusState.ACTIVE or new_state is BusState.PASSIVE
        ):
            try:
                # close the CAN channel
                self._ucan.shutdown(self.channel, False)
                # set mode
                if new_state is BusState.ACTIVE:
                    self._params["mode"] &= ~Mode.MODE_LISTEN_ONLY
                else:
                    self._params["mode"] |= Mode.MODE_LISTEN_ONLY
                # reinitialize CAN channel
                self._ucan.init_can(self.channel, **self._params)
            except UcanException as exception:
                raise CanOperationError() from exception

    def shutdown(self):
        """
        Shuts down all CAN interfaces and hardware interface.
        """
        super().shutdown()
        try:
            self._ucan.shutdown()
        except Exception as exception:
            log.error(exception)