本文へ移動
cccskills
無料GitHub で公開

lego-protocol

LEGO Powered Up protocol context for the Megahub project. Use when working on LEGO device communication, lpfuart library, UART parsing, motor control, sensor modes, device initialization, or debugging UART noise/corruption issues.

インストール方法を見る

含まれるファイル(1)

  • SKILL.md3.9 KB

SKILL.md(原文)

インストールする前に、エージェントに与えられる指示の中身を確認できます。

LEGO Powered Up Protocol — Megahub ESP32

Hardware Setup

  • UART chip: SC16IS752 (I²C-to-dual-UART bridge)
  • Ports: 0–3 (physical LEGO connectors)
  • Baud rate: negotiated per device during handshake (typically starts at low speed, switches to 115200)
  • Library: lib/lpfuart/ — legodevice.cpp, lumpparser.cpp, SC16IS752serialadapter.cpp

Device Initialization Sequence

Devices send identification messages on connect. Never skip any step.

  1. LUMP_CMD_TYPE — device reports its type ID and name
  2. LUMP_CMD_MODES — device reports number of supported modes
  3. LUMP_CMD_SPEED — device requests baud rate to switch to
  4. LUMP_CMD_VERSION — device reports firmware + hardware versions
  5. LUMP_INFO_NAME/RAW/PCT/SI/UNITS/MAPPING/FORMAT — per-mode metadata (repeats for each mode)
  6. LUMP_INFO_MODE_COMBOS — supported mode combinations
  7. finishHandshake() — switch to negotiated baud rate (e.g., 115200)
  8. selectMode() — set default operating mode

Example (BOOST Interactive Motor, device ID 0x26):

  • 4 modes: POWER (0), SPEED (1), POS (2), TEST (3)
  • Default mode: 2 (POS — position feedback)
  • Handshake takes ~3 seconds from connect to ready

Protocol Rules

  • Command rate limit: ~20ms minimum between commands for most devices
  • Device timeout: some devices sleep if no command within ~1 second — send keepalives
  • Byte order: LEGO uses little-endian in most messages
  • Port cleanup: on disconnect, cancel all pending commands for that port
  • Mode selection: always use device type to validate supported modes before CMD_SELECT

UART Corruption from Motor Noise

This is a known hardware issue. When motors run, EMI corrupts the UART RX line.

Symptoms

  • [WARN] LUMP sync lost after N good frames (checksum expected=0xXX actual=0xYY)
  • Repeated 0xD2 bytes in discarded data (spurious detections of LEGO header byte)
  • uartOvr=1 in parser stats — SC16IS752 FIFO overflow

Root Cause

Motor control via M1/M2 generates high-frequency switching noise → current spikes → EMI coupling into UART RX trace → SC16IS752 FIFO fills faster than ESP32 reads via I²C.

Software Mitigations (already implemented)

  • UART read batch size increased to 64 bytes per call (reduces FIFO overrun probability)
  • delayMicroseconds(100) after motor speed changes (allows transients to settle)
  • FIFO overrun counter tracked and logged as uartOvr

Hardware Fixes (if software mitigations are insufficient)

PriorityFixExpected improvement
1 (best)68–100pF NP0/C0G ceramic cap on UART RX pin to GND50–70% error reduction
2Clip-on ferrite bead on RX wire near PCB40–60% error reduction
3100Ω + 100pF RC filter on UART TX10–20% additional
4100nF decoupling cap on SC16IS752 VCC pinstandard practice

Target: < 5 sync recoveries per minute with motor at 100% speed.

Parser Recovery

The parser (lumpparser.cpp) handles corruption gracefully:

  • Detects checksum mismatch
  • Scans forward to find next valid frame header
  • Logs discarded bytes
  • Recovers without device reset

Good state: Parser: ok=XXX csErr=0 discarded=0 recoveries=0 uartOvr=0


Debugging LEGO

Watch parser stats in serial log (logged every ~5 seconds):

[INFO] logParserStats() - Parser: ok=138 csErr=0 discarded=0 recoveries=0 overflow=0 unknownSys=0 invalidSize=0 uartOvr=1
FieldMeaning
okGood frames parsed
csErrChecksum errors
discardedBytes thrown away during resync
recoveriesNumber of sync-loss recovery events
uartOvrSC16IS752 FIFO overflow count

レビュー

まだレビューはありません。使ってみた感想をお寄せください。

同じリポジトリのスキル

概要と使いどころ

ble

無料

Bluetooth context for the Megahub project. Use when working on BLE GATT server, Classic Bluetooth HID host, Web Bluetooth API integration, btremote library, connection handling, or any Bluetooth-related firmware code.

日本語の概要は準備中です。原文の説明を表示しています。

mirkosertic/Megahub72026年9月29日 更新

blockly

無料

Blockly visual programming context for the Megahub project. Use when adding, modifying, or debugging custom Blockly blocks, the Lua code generator, the toolbox, block colors, or the BLOCKS.md documentation generation script.

日本語の概要は準備中です。原文の説明を表示しています。

mirkosertic/Megahub72026年9月29日 更新

Documentation structure and maintenance rules for the Megahub project. Use whenever making user-facing changes — adding features, changing behavior, fixing hardware compatibility, or modifying configuration. Describes which files to update, the README structure, and writing style.

日本語の概要は準備中です。原文の説明を表示しています。

mirkosertic/Megahub72026年9月29日 更新

Core firmware context for the Megahub ESP32 project. Use when working on PlatformIO configuration, build scripts, library structure, main.cpp, or general ESP32 firmware development. Covers build system, project structure, common pitfalls, and debugging.

日本語の概要は準備中です。原文の説明を表示しています。

mirkosertic/Megahub72026年9月29日 更新

frontend

無料

Frontend development context for the Megahub project. Use when working on the web-based IDE, Vite build system, Blockly visual programming, Web Bluetooth API integration, JavaScript frontend code, CSS/styles, UI components, or anything in the /frontend/ directory.

日本語の概要は準備中です。原文の説明を表示しています。

mirkosertic/Megahub72026年9月29日 更新

linting

無料

Code formatting and linting context for the Megahub project. Use when writing or reviewing code in C++ or JavaScript, or when running format/lint checks. Describes tool commands, style conventions, and when to apply them.

日本語の概要は準備中です。原文の説明を表示しています。

mirkosertic/Megahub72026年9月29日 更新

mirkosertic のスキルをすべて見る

このスキルの問題を報告する