Skip to content

Documentation

MicroPython command reference

Complete native LIN API, master, multiple slave IDs, passive sniffer, examples and firmware updates.

Complete MicroPython command reference

The STM32G0B0 provides independent LIN1 and LIN2 channels. Time-critical LIN functionality and LEDs are native C; MicroPython is the scripting interface. These features are available on firmware main.

All commands

Command Description
lin.init(ch, baud) 1/2; 1000–20000 baud
lin.send(ch, id, data, enhanced=True) 0–8 bytes; master publish
lin.request(ch, id, length, enhanced=True) 0–8 bytes; returns bytes, checks checksum
lin.request_raw(ch, id, length) 1–9 raw bytes including checksum, no validation
lin.slave_set(ch, id, data, enhanced=True) Up to 64 slave IDs per channel
lin.slave_clear(ch, id) Remove one configured slave ID
lin.slave_clear(ch) Remove all slave IDs
lin.sniffer_set(ch, id, length, enhanced=True) Monitor selected ID, fixed data length 0–8
lin.sniffer_clear(ch) Clear monitoring rules
lin.sniffer(ch, True/False) Enable/disable passive sniffer
lin.recv(ch) Pop oldest frame dict or None
lin.tic() Current 64-bit microsecond tick

ch is 1 or 2, and id is 0x00–0x3F. The default checksum mode is Enhanced; enhanced=False selects Classic. Exceptions include OSError and ValueError. The C implementation prevents a channel from combining passive sniffing with active master or slave functionality.

Example: Multiple slave IDs

import lin
lin.init(1, 19200)
lin.init(2, 19200)
lin.slave_clear(2)
lin.slave_set(2, 0x12, b'\x11\x22\x33\x44')
lin.slave_set(2, 0x23, b'\x55\x66')
print(lin.request(1, 0x12, 4))
print(lin.request(1, 0x23, 2))
print(lin.recv(1))
print(lin.recv(2))
lin.slave_clear(2, 0x23)

Example: Passive filtered monitor

import lin
lin.init(1, 19200)
lin.init(2, 19200)
lin.slave_clear(2)
lin.sniffer_clear(2)
lin.sniffer_set(2, 0x12, 4)
lin.sniffer_set(2, 0x23, 2, False)
lin.sniffer(2, True)
lin.send(1, 0x12, b'\x11\x22\x33\x44')
lin.send(1, 0x35, b'\xAA\xBB')  # ignored
while True:
    frame = lin.recv(2)
    if frame is None:
        break
    print(frame)
lin.sniffer(2, False)

The sniffer never transmits; it only captures configured IDs with specified lengths and checksums. Ensure correct LIN bus supply/pull-up when using the two-channel loopback example.

Example: TIC and frame timing

import lin, time
a = lin.tic()
time.sleep_ms(150)
print('us:', lin.tic()-a)
f = lin.recv(1)
if f:
    print(f['tic_start'], f['tic_duration'], f['status'])

Frames contain id, len, data, 64-bit tic_start, 32-bit tic_duration (microseconds), and status (0 valid, 1 sniffer checksum error). FIFOs hold 15 usable frames per channel, dropping the oldest on overflow. TIC is not wall-clock time: see the dedicated TIC page.

Raspberry Pi CLI and flashing

cd ~/be-iis-lin
./.venv/bin/beiis-lin --direct info
./.venv/bin/beiis-lin --direct exec "import lin; print(lin.tic())"
git switch main
git pull --ff-only
make host
make firmware-all
make test
make flash-i2c

I²C address is 0x42 on bus 1 by default. The Raw-REPL result timeout is 5 seconds by default; longer scripts need a larger timeout in host/beiis_lin/raw_repl.py. flash-i2c enters mboot via the running MicroPython application, writes/verifies the application and restarts it. For SWD recovery see Firmware & Flashing. Avoid programming the bootloader during routine updates.

Native LEDs and known limits

MASTER/TX/RX pulses remain visible for at least 50 ms; SLAVE stays lit while slave IDs are configured. All eight active-low LEDs run in native C; no MicroPython LED command exists. Unknown-length monitoring, incomplete-frame capture, diagnostic transport, scheduling and sleep/wakeup are not complete.

LIN overview · TIC · Source