最近买了个 LSM6DSR + LIS3MDLTR 模块准备玩玩九轴IMU,店家提供了个简单的库文件以及英飞凌的例程,因为他的库文件写得不怎么样,以及需要移植到 ESP32,所以决定试试让 AI 根据数据手册编以及官方范例写库文件以及测试例程。其中有个难点就是 LIS3MDLTR 是通过 LSM6DSR 的 Sensor Hub 功能接入。

测试方法:使用统一的项目模板,统一的提示词,让 AI Agent 完成代码。如果出错则把错误信息反馈到 AI,让 AI 尝试修复。

硬件

硬件比较简单,ESP32-C3 通过 SPI 连接 LSM6DSR,而模块上 LSM6DSR 通过 I2C 连接 LIS3MDLTR,运行在 SensorHub 模式。

ESP32-C3 <-> LSM6DSR (SPI2):

  • SCLK: GPIO8
  • MISO (SDO): GPIO9
  • MOSI (SDA/SDI): GPIO10
  • CS: GPIO4
  • VDD / VDDIO: 3.3V
  • GND: GND

项目模板

template 项目,包含几个从 idf sdk 拷贝的范例项目,ST 官方提供的 LSM6DSR 驱动库,以及预处理过的两个 Markdown 格式的数据手册。

├── CMakeLists.txt
├── datasheet
│   ├── LIS3MDLTR
│   │   ├── 00_Introduction.md
│   │   ├── 01_Features.md
│   │   ├── 02_Applications.md
│   │   ├── 03_Description.md
│   │   ├── 04_Contents.md
│   │   ├── 05_List_of_tables.md
│   │   ├── 06_List_of_figures.md
│   │   ├── 07_Block_diagram_and_pin_description.md
│   │   ├── 08_Magnetic_and_electrical_specifications.md
│   │   ├── 09_Terminology_and_functionality.md
│   │   ├── 10_Application_hints.md
│   │   ├── 11_Digital_interfaces.md
│   │   ├── 12_Register_mapping.md
│   │   ├── 13_Registers_description.md
│   │   ├── 14_Package_information.md
│   │   ├── 15_Revision_history.md
│   │   ├── 16_Important_notice.md
│   │   └── datasheet
│   └── LSM6DSR
│       ├── 00_Features.md
│       ├── 00_Introduction.md
│       ├── 01_Applications.md
│       ├── 02_Description.md
│       ├── 03_Contents.md
│       ├── 04_List_of_tables.md
│       ├── 05_List_of_figures.md
│       ├── 06_Overview.md
│       ├── 07_Embedded_low_power_features.md
│       ├── 08_Pin_description.md
│       ├── 09_Module_specifications.md
│       ├── 10_Digital_interfaces.md
│       ├── 11_Functionality.md
│       ├── 12_Application_hints.md
│       ├── 13_Register_mapping.md
│       ├── 14_Register_description.md
│       ├── 15_Embedded_functions_register_mapping.md
│       ├── 16_Embedded_functions_register_description.md
│       ├── 17_Embedded_advanced_features_pages.md
│       ├── 18_Embedded_advanced_features_register_description.md
│       └── datasheet
├── driver_example
│   ├── CONTRIBUTING.md
│   ├── LICENSE
│   ├── lsm6dsr_reg.c
│   ├── lsm6dsr_reg.h
│   ├── README.md
│   ├── Release_Notes.html
│   └── Release_Notes.md
├── examples
│   ├── get-started
│   │   ├── blink
│   │   ├── hello_world
│   │   └── README.md
│   └── peripherals
│       ├── spi_master
│       └── usb_serial_jtag
└── main
    ├── CMakeLists.txt
    └── main.c

提示词

You are an expert embedded systems engineer specializing in sensor integration with the ESP-IDF framework.

Your goal is to write a complete and robust driver library for the LSM6DSR IMU sensor for the ESP32-C3, using the ESP-IDF framework. The library must include both basic IMU functionality and the advanced sensor hub feature to read from an external LIS3MDLTR magnetometer. You will also create a comprehensive example application to demonstrate the library's usage.

Communication Interfaces & Pinout

a) ESP32-C3 to LSM6DSR (SPI)

The primary communication with the LSM6DSR will be via SPI on the HSPI (SPI2) host.
*   **SCLK:** `GPIO8`
*   **MISO (SDO):** `GPIO9`
*   **MOSI (SDA/SDI):** `GPIO10`
*   **CS (Chip Select):** `GPIO4`

b) LSM6DSR to LIS3MDLTR (I2C)

The LSM6DSR will act as an I2C master to communicate with the LIS3MDLTR in sensor hub mode. 

the example application should output sensor values using usb_serial_jtag, and you should disable the default log output of esp-idf. data output in the follow format with configurable frequency:

acc_x_value acc_y_value acc_z_value,gyro_x_value gyro_y_value gyro_z_value,mag_x_value mag_y_value mag_z_value\n
...

I will provide you with a basic application template. You need to complete the missing parts.

You can find datasheets in datasheet folder, some basic esp-idf examples in examples folder, and some basic driver code in driver_example folder.

Claude Code - Sonnet Sonnet 4

能使用 esp-idf 组件的形式实现 lsm6dsr_driver 组件,并实现 esp32 相关的 SPI 接入代码。

usb_serial_jtag 功能接入失败,后来改成 printf 输出;

LSM6DSR 加速度,角速度功能能读到数据,初步校验应该正确;LIS3MDLTR 初始化失败,无法读到 ID。

LIS3MDLTR 初始化失败问题经过多轮交互与调试依然不成功,最后通过提供商家的范例代码,成功定位到是 Sensor hub 寄存器初始化顺序问题,并成功修正。

Codex - gpt-5-codex-high

能使用 esp-idf 组件的形式实现 lsm6dsr_driver 组件,并实现 esp32 相关的 SPI 接入代码。

usb_serial_jtag 功能接入失败,后来改成 printf 输出;

LSM6DSR 加速度,角速度功能能读到数据,但数据明显错误;LIS3MDLTR 初始化失败,无法读到 ID。

提供商家的范例代码后依然无法读出正确数据,也无法读到 LIS3MDLTR ID。

Gemini - gemini-2.5-pro

Claude Code 与 Codex 都是把官方驱动库拷贝到自己的组件内作为库文件,而 Gemini 通过 N 轮修改后,把官方驱动改为一个 driver_example 组件,并作为 lsm6dsr_driver 库的依赖。

usb_serial_jtag 改成 printf 这个指令竟然都操作错误。

spi 初始化多轮对话后依然失败。