Skip to content

add I2C and SPI tools for hardware interaction#140

Merged
mengzhuo merged 2 commits intosipeed:mainfrom
thtskaran:main
Feb 14, 2026
Merged

add I2C and SPI tools for hardware interaction#140
mengzhuo merged 2 commits intosipeed:mainfrom
thtskaran:main

Conversation

@thtskaran
Copy link
Contributor

Summary

Adds I2C and SPI tools that let the agent interact with hardware peripherals — not as a dumb pipe to i2cget, but as an agent that understands device protocols and can turn "what's
the temperature?" into a multi-step hardware interaction.

Why an LLM for I2C/SPI?

The agent isn't replacing CLI tools. It's replacing the human knowledge between raw bytes and useful answers:

User: "what's the temperature?"

Agent internally:

  1. i2c detect → finds bus 1
  2. i2c scan(bus: "1") → finds device at 0x38
  3. Recognizes 0x38 = AHT20 (from skill reference docs)
  4. i2c write(0x38, [0xAC, 0x33, 0x00]) → trigger measurement
  5. waits 80ms
  6. i2c read(0x38, length: 6) → [0x1C, 0x54, 0xA3, 0x48, 0x6B, 0x2F]
  7. Applies datasheet formula to parse raw bytes

Agent: "Temperature is 23.5°C, humidity is 41.2%"

Seven steps — bus discovery, device identification by address, protocol knowledge, multi-step init/trigger/read, raw byte parsing with datasheet formulas — from one natural language
request.

The skill system makes this work. The hardware skill ships with reference docs covering register maps, init sequences, and parsing formulas for common devices (AHT20, BME280,
SSD1306, MPU6050, DS3231, INA219, PCA9685, MCP3008, and more), plus board-specific pinmux setup for Sipeed boards.

What's included

Tools (pkg/tools/):

  • i2c — detect buses, scan for devices, read/write with register addressing
  • spi — list devices, full-duplex transfer, read

Implementation details:

  • Platform-split via build tags: _linux.go (real syscall/ioctl) and _other.go (stubs)
  • I2C scan uses the same hybrid probe strategy as i2cdetect MODE_AUTO — SMBus Quick Write for most addresses, SMBus Read Byte only for EEPROM ranges (0x30-0x37, 0x50-0x5F) to avoid
    corrupting AT24RF08 chips or locking write-only devices
  • SPI uses kernel spi_ioc_transfer struct with verified ioctl constants, runtime.KeepAlive for GC safety on buffer pointers
  • Write operations require confirm: true safety guard
  • Input validation: bus IDs, 7-bit address range, register 0x00-0xFF, path injection prevention
  • Reports EBUSY devices (claimed by kernel drivers) during scan

Hardware skill (skills/hardware/):

  • SKILL.md — quick start, safety notes, troubleshooting
  • references/board-pinout.md — pinmux setup for LicheeRV Nano, MaixCAM, MaixCAM2, NanoKVM (verified against Sipeed wiki)
  • references/common-devices.md — register maps and formulas for 10 common I2C/SPI devices (verified against datasheets)

Test plan

  • make fmt + git diff --exit-code
  • go vet ./...
  • go test ./...
  • Cross-compile: linux/amd64, linux/arm64, linux/riscv64, darwin/arm64, windows/amd64
  • I2C ioctl constants verified against Linux kernel headers
  • SPI ioctl constants verified via _IOW macro calculation
  • spiTransfer struct layout verified field-by-field against kernel spi_ioc_transfer
  • All device register maps verified against official datasheets
  • Board pinout/pinmux info verified against Sipeed wiki
  • On-hardware testing with real I2C/SPI devices (needs board access)

Closes #107

- Implemented I2CTool for I2C bus interaction, including device scanning, reading, and writing.
- Implemented SPITool for SPI bus communication, supporting device listing, data transfer, and reading.
- Added platform-specific implementations for Linux and stubs for non-Linux platforms.
- Updated agent loop to register new I2C and SPI tools.
- Created documentation for hardware skills, including usage examples and pinmux setup instructions.
Copy link
Collaborator

@mengzhuo mengzhuo left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for your contributions

@mengzhuo mengzhuo merged commit 2720fa7 into sipeed:main Feb 14, 2026
3 checks passed
emadomedher pushed a commit to emadomedher/picoclaw that referenced this pull request Feb 17, 2026
* add I2C and SPI tools for hardware interaction

- Implemented I2CTool for I2C bus interaction, including device scanning, reading, and writing.
- Implemented SPITool for SPI bus communication, supporting device listing, data transfer, and reading.
- Added platform-specific implementations for Linux and stubs for non-Linux platforms.
- Updated agent loop to register new I2C and SPI tools.
- Created documentation for hardware skills, including usage examples and pinmux setup instructions.

* Remove build constraints for Linux from I2C and SPI tool files.
StarWindv referenced this pull request in StarWindv/PicoClaw-shou Feb 19, 2026
* add I2C and SPI tools for hardware interaction

- Implemented I2CTool for I2C bus interaction, including device scanning, reading, and writing.
- Implemented SPITool for SPI bus communication, supporting device listing, data transfer, and reading.
- Added platform-specific implementations for Linux and stubs for non-Linux platforms.
- Updated agent loop to register new I2C and SPI tools.
- Created documentation for hardware skills, including usage examples and pinmux setup instructions.

* Remove build constraints for Linux from I2C and SPI tool files.
@Orgmar
Copy link
Contributor

Orgmar commented Feb 19, 2026

@thtskaran I2C and SPI tool support is awesome, this really opens up PicoClaw for hardware interaction use cases. Thanks for putting this together!

We're growing the PicoClaw Dev Group on Discord for contributors to share ideas and collaborate. If you want in, send an email to support@sipeed.com with the subject [Join PicoClaw Dev Group] + Your GitHub account and we'll get you the invite link.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

skills for direct I2C or SPI control

3 participants