这些小活动你都参加了吗?快来围观一下吧!>>
电子产品世界 » 论坛首页 » DIY与开源设计 » 电子DIY » 【Zephyr|瑞萨RA6E2】5二氧化碳监测系统

共1条 1/1 1 跳转至

【Zephyr|瑞萨RA6E2】5二氧化碳监测系统

高工
2025-12-28 17:41:10     打赏

【目标】

体验环境监测功能,实现室内二氧化碳监测

【开发环境】

1、wsl2

2、west

【实现步骤】

1、在原有oled显示驱动的工程中添加stcc4驱动库

到github:Sensirion/embedded-i2c-stcc4中下载驱动包,放入工程中:

image.png

2、修改设备树,i2c0下添加stcc4节点

&sci0 {
    status = "okay";

    uart {
        status = "disabled";
    };

    i2c: i2c {
        compatible = "renesas,ra-i2c-sci";
        status = "okay";
        clock-frequency = <400000>;

        ssd1306: ssd1306@3c {
            compatible = "solomon,ssd1306fb";
            reg = <0x3c>;
            width = <128>;
            height = <64>;
            segment-offset = <0>;   // 列偏移(通常为 0)
            page-offset = <0>;      // ←←← 行(页)偏移(通常为 0)
            display-offset = <0>;
            multiplex-ratio = <63>;
            prechargep = <2>;
            status = "okay";
        };
        // 添加 STCC4 传感器节点
        stcc4: stcc4@62 {
            compatible = "sensirion,stcc4";
            reg = <0x64>;
            status = "okay";
        };
    };
};

3、修改hal驱动适配ra6e2的初始化

/**
 * @brief 初始化 I2C —— 由你的应用或驱动调用
 */
int sensirion_i2c_hal_init_with_device(const struct device *i2c_dev, uint8_t addr)
{
    if (!device_is_ready(i2c_dev)) {
        LOG_ERR("I2C device not ready");
        return -ENODEV;
    }

    i2c_bus_dev = i2c_dev;
    stcc4_i2c_addr = addr;
    return 0;
}

4、添加read、write代码实现驱动对接:

int8_t sensirion_i2c_hal_read(uint8_t address, uint8_t* data, uint8_t count) {
    ARG_UNUSED(address);

    if (i2c_read(i2c_bus_dev, data, count, stcc4_i2c_addr) != 0) {
        LOG_ERR("I2C read failed");
        return -1;
    }
    return 0;
}

int8_t sensirion_i2c_hal_write(uint8_t address, const uint8_t* data,
                               uint8_t count) {
    ARG_UNUSED(address);  // 我们已固定地址

    if (i2c_write(i2c_bus_dev, data, count, stcc4_i2c_addr) != 0) {
        LOG_ERR("I2C write failed");
        return -1;
    }
    return 0;
}

5、添加延时函数的对接,直接使用zephyr的延时来实现:

void sensirion_i2c_hal_sleep_usec(uint32_t useconds) {
    if (useconds < 1000) {
        k_usleep(useconds);
    } else {
        k_msleep(useconds / 1000);
    }
}

6、编译

lugl@lugl:~/zephyrproject/app/ra6e2/ra6e2_ssd1306_i2c$ west build -b fpb_ra6e2
[1/7] Building C object CMakeFiles/app.dir/src/main.c.obj
/home/lugl/zephyrproject/app/ra6e2/ra6e2_ssd1306_i2c/src/main.c:17:6: warning: return type of 'main' is not 'int' [-Wmain]
   17 | void main(void)
      |      ^~~~
[7/7] Linking C executable zephyr/zephyr.elf
Memory region         Used Size  Region Size  %age Used
           FLASH:       31340 B       256 KB     11.96%
             RAM:        6632 B        40 KB     16.19%
 OFS_OFS0_MEMORY:           4 B          4 B    100.00%
 OFS_OSIS_MEMORY:          16 B         16 B    100.00%
OFS_OFS1_SEC_MEMORY:           4 B          4 B    100.00%
OFS_BPS_SEC_MEMORY:           4 B          4 B    100.00%
OFS_PBPS_SEC_MEMORY:           4 B          4 B    100.00%
        IDT_LIST:          0 GB        32 KB      0.00%
Generating files from /home/lugl/zephyrproject/app/ra6e2/ra6e2_ssd1306_i2c/build/zephyr/zephyr.elf for board: fpb_ra6e2

7、下载

使用jlink flash下载

image.png


【效果展示】

image.png

【总结】

在zephyr生态中,对i2c设备的支持非常友好,只需要少量的代码便可以移植并驱动外设。

附源码代码

ra6e2_stcc4.zip






关键词: Zephyr     瑞萨     RA6E2     二氧化碳    

共1条 1/1 1 跳转至

回复

匿名不能发帖!请先 [ 登陆 注册 ]