開發板架設與工具鏈

韌體工程師的前置工作:把開發板架起來、裝好工具鏈、寫第一支程式

給韌體工程師:rpi5-sensor-isp-zh-tw 是 Linux 開發板,但在碰感測器之前,你得先完成「板子架設 → 工具鏈 → 第一支程式」這條前置線。本頁是Raspberry Pi 5特有的完整流程——每步給指令與驗證方式。

0. 前置流程總覽

燒錄 OS首次開機/網路啟用 SSH/I2C裝工具鏈驗證環境第一支 C 程式

1. 作業系統與首次開機

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

2. 啟用必要介面(SSH / I2C / SPI / Serial)

啟用與驗證
sudo raspi-config → Interface Options → I2C / SSH / Serial → Enable

Camera 0 對應 i2c-22;Camera 1 對應 i2c-10。啟用 I2C 後需重啟。

3. 安裝工具鏈(韌體必備)

安裝
sudo apt update && sudo apt install -y build-essential cmake git \
    i2c-tools v4l-utils python3-pip
工具用途
build-essentialgcc / make,編譯 C
cmake專案建置系統
git版本控制
i2c-toolsi2cdetect / i2ctransfer
v4l-utilsv4l2-ctl / media-ctl
python3-pipPython 工具

4. 驗證環境就緒

五秒驗證
sudo i2cdetect -y 22    # 預期看到 0x36(OV5647)
通過標準:工具鏈裝好、i2cdetect 看得到感測器位址 → 前置完成,可進單元 3。

5. 第一支 C 程式:讀感測器 ID(韌體核心)

韌體工程師的核心技能——用 userspace I2C 直接跟感測器溝通。以下是讀 OV 感測器 ID 的最小程式:

read_sensor_id.c
#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

6. 交叉編譯與遠端建置

交叉編譯範例
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 檔)/

7. 從原始碼建置(進階)

libcamera / ov5647 驅動從原始碼建置:git clone + meson/ninja(進階,正式開發才需要)。

8. 常用除錯工具

工具用途
dmesg內核訊息(probe/CSI 錯誤)
strace追蹤 syscall(I2C 讀寫行為)
i2cdumpdump 感測器 register
perf效能分析
看完這頁你應該能說出:
  • 本平台的 OS 映像、使用者與首次開機流程。
  • 啟用 SSH/I2C 的方式與 I2C 匯流排編號。
  • 韌體必備工具鏈清單。
  • 用 userspace C 程式讀感測器 register。
  • 直接編譯 vs 交叉編譯的取捨。

延伸閱讀