can

This module controls the CAN bus peripheral.

Add rx id

Adds a CAN ID to listen on.

can.add_rx_id(c, ID, [mask, remote])

Input Parameters:

  • c [int] = The CAN channel to use. Either: can.CAN0, can.CAN1.

  • ID [int] = CAN ID to listen on

  • mask [int] = bits the controller takes into account when matching the ID (default 0x7ff); use 0 to listen on all IDs

  • remote [bool] = set ID as a remote ID

Returns the message object number the controller will use to listen on the given ID [int].

Configure

Sets CAN configuration parameters.

can.configure(ctrlr, cfg)

Input Parameters:

  • ctrlr[int] = Specifies the bus to configure.

  • cfg[var] = Can be one of many things depending on the desired config:

    • None which disables the CAN controller for the specified bus. Returns None.

    • 5-tuple which resets the bus with specified bit timing as div, brp, tseg1, tseg2 & sjw. Returns a 2-tuple of baud rate and sample point.

    • A valid dictionary object with parameters as shown in the table below. Unsupported keys are ignored. The return value depends on the key.

      • {GetFlags : None} = Gets value of CANx_flags as integer.

      • {SetFlags : Integer} = Sets value of CANx_flags.

Returns a message based on config. See cfg description above for details.

Delete rx

Stop listening on an ID (given by a message object number).

can.del_rx(c, msgnum)

Input Parameters:

  • c [int] = The CAN channel to use. Either: can.CAN0, can.CAN1.

  • msgnum [int] = message object number on which we want to stop listening

Delete rx id

Stop listening on an ID.

can.del_rx_id(c, id)

Input Parameters:

  • c [int] = The CAN channel to use. Either: can.CAN0, can.CAN1.

  • id [int] = ID on which we want to stop listening

In the current firmware this function may not reliably stop listening on the given ID.

Get message

Get the message from the channel message queue.

can.get_msg(c)

Input Parameters:

  • c [int] = The CAN channel to use. Either: can.CAN0, can.CAN1.

Returns the first message in the queue if there was one, otherwise None.

The message is returned as an attribute tuple with the following elements:

  • id [int] = can id of the message

  • msgnum [int] = message object number

  • msglost_ctrlr [bool] = did this message overwrite a previous unrecovered message in the CAN controller?

  • msglost_buffer [bool] = did this message overwrite a previous unrecovered message in the message queue?

  • data [list of ints] = data from the can message

Get state

Gets the state of the CAN bus.

can.get_state(ctrlr)

Input Parameters:

  • ctrlr[int] = 1 or 0 depending on which CAN bus on the VBOX Touch you are referring to.

Returns:

  • stat[int] = Current value of the CAN status register

  • tec[int] = Transmit error counter

  • rec[int] = Receive error counter

  • rxcnt[int] = total number of messages received

  • prcnt[int] = total number of messages processed

Get timing

Gets the timing of the CAN bus.

can.get_timing(ctrlr)

Input Parameters:

  • ctrlr[int] = 1 or 0 depending on which CAN bus on the VBOX Touch you are referring to.

Returns:

  • rate[float] = CAN bit rate

  • sample_pt[float] = CAN sample point calculated from the register values.

  • clock[int] = Clock Speed

  • bt[int] = Value of the CAN bit timing register

  • div[int] = Clock Divider

  • brp[int] = Baud Rate Prescaler

  • seg1[int] = The value of the time segment before the sample point (includes propagation time segment).

  • seg2[int] = The value of the time segment after the sample point.

  • sjw[int] = synchronisation jump width

Reset

Resets the CAN controller.

reset(c, [bitrate])

Input Parameters:

  • c [int] = The CAN channel to use. Either: can.CAN0, can.CAN1.

  • bitrate [int] = channel bitrate in bits per second

This resets the given channel. As it’s a hardware reset at the controller level, this will:

  • delete all message objects;

  • returns the channel to test mode.

  • The default bitrate is 500000 (500 kb/s). Once a bitrate is specified, that bitrate will be used in subsequent channel resets (no need to specify it again).

Send message

Send a message.

send_msg(c, msg)

Input Parameters:

  • c [int] = The CAN channel to use. Either: can.CAN0, can.CAN1.

  • msg [list] = message to be sent

  • The message is a list with 2 or 3 elements:

    • id [int] = message ID

    • remote [bool] = set remote flag

    • data [list of ints] = optional list with 1 to 8 data elements to be sent

Returns the message object number used. Raises RuntimeError if there are no free message objects on the controller. Raises TimeoutError if the message could not be sent in less than 10 ms.

Set callback

Set/unset new message callback functions.

set_callback(c, cb)

Input Parameters:

  • c [int] = The CAN channel to use. Either: can.CAN0, can.CAN1.

  • cb [callable] = callback function to be executed when a new message is inserted into the given channel’s queue (use None to disable)

Termination

Turn resistive termination on/off.

termination(c, on)

Input Parameters:

  • c [int] = The CAN channel to use. Either: can.CAN0, can.CAN1.

  • on [bool] = turn termination resistors on or off for the given channel