sllin is a TTY line discipline that allows a Linux system to act as a LIN master
and, to some extent, as a LIN slave.
Communication to userspace is exposed as a CAN netdevice (sllin0, sllin1, ...), so you can
use standard tools like ip, candump, and cangen.
Features:
- Slave Mode Answer to RTR Frames (via Cache response)
- Slave Mode Listen only
- Master Mode Send RTR Frames (Break,Sync + ID) and wait for slave answer (Data + CRC)
- Master Mode Send Complete LIN Frames
- Linux with matching kernel headers/sources for the currently running kernel
- UART interface (for example
/dev/ttyS0,/dev/ttyUSB0) - LIN level shifter/transceiver (UART ↔ LIN)
can-utils(for examplecandump,cangen)- Root privileges for
insmod,ip link,ldattach
Optional:
slcan_attachor project-specific attach tools/scripts
Inside the sllin directory:
makeThis builds the kernel module sllin.ko.
sudo insmod ./sllin.koWith parameters (example):
sudo insmod ./sllin.ko master=1 baudrate=19200 maxdev=10Important parameters:
master:1= master,0= slavebaudrate: LIN baud rate (driver default:LIN_DEFAULT_BAUDRATE, usually19200)maxdev: maximum number of dynamic interfaces (currently defaults to10in this driver)break_by_baud: generate break via temporary baud-rate switch (0/1)
Check status:
dmesg | tail -n 20sudo ldattach <ldisc_number> /dev/ttyS0Note: historically, 28 (master) and 29 (slave) were often used.
On modern kernels, these numbers are already assigned (N_MCTP,
N_DEVELOPMENT). For out-of-tree drivers, the line discipline number
registered by the driver must match the running kernel setup.
The kernel reports assignemnt of the line discipline numbers
by proc entry /proc/tty/ldiscs. The number assigned to the sllin
master can be obtained by
sed -n -e 's/^[n_]*sllin[ \t]*\([0-9]*\)$/\1/p' /proc/tty/ldiscsfor the slave
sed -n -e 's/^[n_]*sllin-slave[ \t]*\([0-9]*\)$/\1/p' /proc/tty/ldiscsThe line discipline numbers can be adjusted in the sllin.c source file
#define N_SLLIN 28
#define N_SLLIN_SLAVE 29
If a compatible helper tool is available (need to be patched first with patch under slin/canutils-patches) :
sudo slcan_attach -w /dev/ttyS0ip link show dev sllin0
sudo ip link set sllin0 up
ip link show dev sllin0state UNKNOWN is expected for this interface type.
- LIN ID is always in the lower 6 bits (
can_id & 0x3f) - EFF (
CAN_EFF_FLAG) marks a control/configuration frame forsllinframe cache - Important control bits (above LIN ID):
LIN_CACHE_RESPONSE(1 << 6=0x40): enable cached response for this LIN IDLIN_CHECKSUM_EXTENDED(1 << 7=0x80): use enhanced checksumLIN_SINGLE_RESPONSE(1 << 8=0x100): one-shot cached response
One-shot cache response (LIN ID 0x10):
# Configure one-shot cache response for LIN ID 0x10
# can_id = 0x10 (ID) + 0x40 (CACHE_RESPONSE) + 0x100 (SINGLE_RESPONSE) = 0x150
cangen sllin0 -e -I 0x150 -n 1 -L 2 -D a1b2
# Trigger with RTR
cangen sllin0 -R -I 0x10 -n 1 -L 0cache response (LIN ID 0x10):
# Configure cache response for LIN ID 0x10
# can_id = 0x10 (ID) + 0x40 (CACHE_RESPONSE) = 0x50
cangen sllin0 -e -I 0x50 -n 1 -L 2 -D a1b2
# Trigger with RTR
cangen sllin0 -R -I 0x10 -n 3 -L 0
# clear cache response
# can_id = 0x10 (ID) + 0x40 (CACHE_RESPONSE) = 0x50
# BUT DLC = 0 -> Disable cache response
cangen sllin0 -e -I 0x50 -n 1 -L 0cangen sllin0 -R -I 1 -n 1 -L 0Monitor in parallel:
candump sllin0cangen sllin0 -R -I 8 -n 1 -L 0
ip -s link show dev sllin0If no slave responds, RX error counters increase.
cangen sllin0 -e -I 0x848 -n 1 -L 2 -D beef
cangen sllin0 -R -I 8 -n 1 -L 0cangen sllin0 -I 7 -n 1 -L 2 -D f00f