韌體工程師的前置工作:把開發板架起來、裝好工具鏈、寫第一支程式
Raspberry Pi OS(Bookworm 之後,建議 64-bit)。映像選擇與燒錄方式見「開發者環境」頁(Windows/macOS/Linux 皆有)。
| 項目 | 本平台 |
|---|---|
| 映像 | Raspberry Pi OS Desktop / Lite(用 Raspberry Pi Imager 燒錄) |
| 預設使用者 | pi(首次開機設定,或預燒 ssh 檔) |
| 首次開機 | 接螢幕完成設定;headless 可預先在 boot 分割放 ssh 空檔 + userconf.txt |
| 更新 | sudo apt update && sudo apt upgrade -y |
sudo raspi-config → Interface Options → I2C / SSH / Serial → Enable
Camera 0 對應 i2c-22;Camera 1 對應 i2c-10。啟用 I2C 後需重啟。
sudo apt update && sudo apt install -y build-essential cmake git \
i2c-tools v4l-utils python3-pip| 工具 | 用途 |
|---|---|
build-essential | gcc / make,編譯 C |
cmake | 專案建置系統 |
git | 版本控制 |
i2c-tools | i2cdetect / i2ctransfer |
v4l-utils | v4l2-ctl / media-ctl |
python3-pip | Python 工具 |
sudo i2cdetect -y 22 # 預期看到 0x36(OV5647)
韌體工程師的核心技能——用 userspace I2C 直接跟感測器溝通。以下是讀 OV 感測器 ID 的最小程式:
#include#include #include #include #include #include /* 用 userspace I2C 讀感測器 ID(16-bit register,big-endian) */ int main(int argc, char **argv) { int bus = (argc > 1) ? atoi(argv[1]) : 22; int addr = 0x36; /* OV5647 7-bit 位址 */ char path[32]; snprintf(path, sizeof(path), "/dev/i2c-%d", bus); int fd = open(path, O_RDWR); if (fd < 0) { perror("open /dev/i2c"); return 1; } if (ioctl(fd, I2C_SLAVE, addr) < 0) { perror("I2C_SLAVE"); return 1; } uint8_t reg[2] = {0x30, 0x0A}; /* ID 高位 register */ if (write(fd, reg, 2) != 2) { perror("write"); return 1; } uint8_t hi = 0, lo = 0; if (read(fd, &hi, 1) != 1) { perror("read"); return 1; } reg[1] = 0x0B; /* ID 低位 register */ if (write(fd, reg, 2) != 2) return 1; if (read(fd, &lo, 1) != 1) return 1; printf("Sensor ID: 0x%02X%02X\n", hi, lo); close(fd); return 0; }
gcc -o read_sensor_id read_sensor_id.c && sudo ./read_sensor_id 22
aarch64-linux-gnu-gcc 編 ARM 二進位。sudo apt install -y gcc-aarch64-linux-gnu aarch64-linux-gnu-gcc -o read_sensor_id read_sensor_id.c scp read_sensor_id pi(首次開機設定,或預燒 ssh 檔)@IP:/home/pi(首次開機設定,或預燒 ssh 檔)/
libcamera / ov5647 驅動從原始碼建置:git clone + meson/ninja(進階,正式開發才需要)。
| 工具 | 用途 |
|---|---|
dmesg | 內核訊息(probe/CSI 錯誤) |
strace | 追蹤 syscall(I2C 讀寫行為) |
i2cdump | dump 感測器 register |
perf | 效能分析 |