The UCAN Protocol¶
UCAN is the protocol used by the microcontroller-based USB-CANadapter that is integrated on System-on-Modules from Theobroma Systemsand that is also available as a standalone USB stick.
The UCAN protocol has been designed to be hardware-independent.It is modeled closely after how Linux represents CAN devicesinternally. All multi-byte integers are encoded as Little Endian.
All structures mentioned in this document are defined indrivers/net/can/usb/ucan.c.
USB Endpoints¶
UCAN devices use three USB endpoints:
- CONTROL endpoint
The driver sends device management commands on this endpoint
- IN endpoint
The device sends CAN data frames and CAN error frames
- OUT endpoint
The driver sends CAN data frames on the out endpoint
CONTROL Messages¶
UCAN devices are configured using vendor requests on the control pipe.
To support multiple CAN interfaces in a single USB device allconfiguration commands target the corresponding interface in the USBdescriptor.
The driver usesucan_ctrl_command_in/out anducan_device_request_in to deliver commands to the device.
Setup Packet¶
| Direction | Vendor | (Interface or Device) |
| Command Number |
| Subcommand Number (16 Bit) or 0 if not used |
| USB Interface Index (0 for device commands) |
|
|
Error Handling¶
The device indicates failed control commands by stalling thepipe.
Device Commands¶
UCAN_DEVICE_GET_FW_STRING¶
Dev2Host; optional
Request the device firmware string.
Interface Commands¶
UCAN_COMMAND_START¶
Host2Dev; mandatory
Bring the CAN interface up.
- Payload Format
ucan_ctl_payload_t.cmd_start
mode | or mask of |
UCAN_COMMAND_STOP¶
Host2Dev; mandatory
Stop the CAN interface
- Payload Format
empty
UCAN_COMMAND_RESET¶
Host2Dev; mandatory
Reset the CAN controller (including error counters)
- Payload Format
empty
UCAN_COMMAND_GET¶
Host2Dev; mandatory
Get Information from the Device
Subcommands¶
- UCAN_COMMAND_GET_INFO
Request the device information structure
ucan_ctl_payload_t.device_info.See the
device_infofield for details, anduapi/linux/can/netlink.hfor an explanation of thecan_bittimingfields.- Payload Format
ucan_ctl_payload_t.device_info
UCAN_COMMAND_GET_PROTOCOL_VERSION
Request the device protocol version
ucan_ctl_payload_t.protocol_version. The current protocol version is 3.
- Payload Format
ucan_ctl_payload_t.protocol_version
Note
Devices that do not implement this command use the oldprotocol version 1
UCAN_COMMAND_SET_BITTIMING¶
Host2Dev; mandatory
Setup bittiming by sending the structureucan_ctl_payload_t.cmd_set_bittiming (seestructbittiming fordetails)
- Payload Format
ucan_ctl_payload_t.cmd_set_bittiming.
UCAN_SLEEP/WAKE¶
Host2Dev; optional
Configure sleep and wake modes. Not yet supported by the driver.
UCAN_FILTER¶
Host2Dev; optional
Setup hardware CAN filters. Not yet supported by the driver.
Allowed interface commands¶
Legal Device State | Command | New Device State |
|---|---|---|
stopped | SET_BITTIMING | stopped |
stopped | START | started |
started | STOP or RESET | stopped |
stopped | STOP or RESET | stopped |
started | RESTART | started |
any | GET | no change |
IN Message Format¶
A data packet on the USB IN endpoint contains one or moreucan_message_in values. If multiple messages are batched in a USBdata packet, thelen field can be used to jump to the nextucan_message_in value (take care to sanity-check thelen valueagainst the actual data size).
len field¶
Eachucan_message_in must be aligned to a 4-byte boundary (relativeto the start of the start of the data buffer). That means that theremay be padding bytes between multipleucan_message_in values:
+----------------------------+ < 0| || struct ucan_message_in || |+----------------------------+ < len [padding]+----------------------------+ < round_up(len, 4)| || struct ucan_message_in || |+----------------------------+ [...]
type field¶
Thetype field specifies the type of the message.
UCAN_IN_RX¶
subtypezero
Data received from the CAN bus (ID + payload).
UCAN_IN_TX_COMPLETE¶
subtypezero
The CAN device has sent a message to the CAN bus. It answers with alist of tuples <echo-ids, flags>.
The echo-id identifies the frame from (echos the id from a previousUCAN_OUT_TX message). The flag indicates the result of thetransmission. Whereas a set Bit 0 indicates success. All other bitsare reserved and set to zero.
Flow Control¶
When receiving CAN messages there is no flow control on the USBbuffer. The driver has to handle inbound message quickly enough toavoid drops. I case the device buffer overflow the condition isreported by sending corresponding error frames (seeCAN Error Handling)
OUT Message Format¶
A data packet on the USB OUT endpoint contains one or morestructucan_message_out values. If multiple messages are batched into onedata packet, the device uses thelen field to jump to the nextucan_message_out value. Each ucan_message_out must be aligned to 4bytes (relative to the start of the data buffer). The mechanism issame as described inlen field.
+----------------------------+ < 0| || struct ucan_message_out || |+----------------------------+ < len [padding]+----------------------------+ < round_up(len, 4)| || struct ucan_message_out || |+----------------------------+ [...]
type field¶
In protocol version 3 onlyUCAN_OUT_TX is defined, others are usedonly by legacy devices (protocol version 1).
UCAN_OUT_TX¶
subtypeecho id to be replied within a CAN_IN_TX_COMPLETE message
Transmit a CAN frame. (parameters:id,data)
Flow Control¶
When the device outbound buffers are full it starts sendingNAKs ontheOUT pipe until more buffers are available. The driver stops thequeue when a certain threshold of out packets are incomplete.
CAN Error Handling¶
If error reporting is turned on the device encodes errors into CANerror frames (seeuapi/linux/can/error.h) and sends it using theIN endpoint. The driver updates its error statistics and forwardsit.
Although UCAN devices can suppress error frames completely, in Linuxthe driver is always interested. Hence, the device is always started withtheUCAN_MODE_BERR_REPORT set. Filtering those messages for theuser space is done by the driver.
Bus OFF¶
The device does not recover from bus of automatically.
Bus OFF is indicated by an error frame (see
uapi/linux/can/error.h)Bus OFF recovery is started by
UCAN_COMMAND_RESTARTOnce Bus OFF recover is completed the device sends an error frameindicating that it is on ERROR-ACTIVE state.
During Bus OFF no frames are sent by the device.
During Bus OFF transmission requests from the host are completedimmediately with the success bit left unset.
Example Conversation¶
Device is connected to USB
Host sends command
UCAN_COMMAND_RESET, subcmd 0Host sends command
UCAN_COMMAND_GET, subcmdUCAN_COMMAND_GET_INFODevice sends
UCAN_IN_DEVICE_INFOHost sends command
UCAN_OUT_SET_BITTIMINGHost sends command
UCAN_COMMAND_START, subcmd 0, modeUCAN_MODE_BERR_REPORT