查看: 316|回复: 0

[评测分享] 【瑞萨RRH62000-EVK套件】剩余RRH62000传感器驱动添加

[复制链接]
  • TA的每日心情
    开心
    前天 09:35
  • 签到天数: 35 天

    连续签到: 1 天

    [LV.5]常住居民I

    发表于 2024-12-4 23:29:36 | 显示全部楼层 |阅读模式
    分享到:
    本帖最后由 oxlm 于 2024-12-4 23:42 编辑

       RRH62000支持的传感器驱动很多,上一次仅仅是添加完了温度传感器,这次将剩余的rrh62000传感器接口添加上去。下面是传感器支持的功能列表。
    2.jpg

      相对应的,rtthread目前支持的传感器列表如下:
    1. #define RT_SENSOR_CLASS_GYRO           (2)  /* Gyroscope         */
    2. #define RT_SENSOR_CLASS_MAG            (3)  /* Magnetometer      */
    3. #define RT_SENSOR_CLASS_TEMP           (4)  /* Temperature       */
    4. #define RT_SENSOR_CLASS_HUMI           (5)  /* Relative Humidity */
    5. #define RT_SENSOR_CLASS_BARO           (6)  /* Barometer         */
    6. #define RT_SENSOR_CLASS_LIGHT          (7)  /* Ambient light     */
    7. #define RT_SENSOR_CLASS_PROXIMITY      (8)  /* Proximity         */
    8. #define RT_SENSOR_CLASS_HR             (9)  /* Heart Rate        */
    9. #define RT_SENSOR_CLASS_TVOC           (10) /* TVOC Level        */
    10. #define RT_SENSOR_CLASS_NOISE          (11) /* Noise Loudness    */
    11. #define RT_SENSOR_CLASS_STEP           (12) /* Step sensor       */
    12. #define RT_SENSOR_CLASS_FORCE          (13) /* Force sensor      */
    13. #define RT_SENSOR_CLASS_DUST           (14) /* Dust sensor       */
    14. #define RT_SENSOR_CLASS_ECO2           (15) /* eCO2 sensor       */
    15. #define RT_SENSOR_CLASS_GNSS           (16) /* GPS/GNSS sensor   */
    16. #define RT_SENSOR_CLASS_TOF            (17) /* TOF sensor        */
    17. #define RT_SENSOR_CLASS_SPO2           (18) /* SpO2 sensor       */
    18. #define RT_SENSOR_CLASS_IAQ            (19) /* IAQ sensor.       */
    19. #define RT_SENSOR_CLASS_ETOH           (20) /* EtOH sensor.      */
    20. #define RT_SENSOR_CLASS_BP             (21) /* Blood Pressure    */
    复制代码
      从这里面看,如果不动RTT的传感器支持列表(不往里面新增),在不考虑功能重复的情况下,RRH62000至少可以添加6种传感器。除去之前添加的温度传感器,还能添加的有,灰尘传感器(规格书中提到的PMxxxNCxxx总共9种),湿度传感器,总挥发性有机化合物传感器,二氧化碳传感器,空气质量传感器。
      由于开源项目部分暂时不适合动中间框架层的代码,因此灰尘传感器部分就只上报一个参数上去,若有需求,内部项目在框架层添加传感器种类并在驱动层将剩余功能添加上。
    代码修改
      前面已经贴过所有的部分了,这里仅仅贴修改的部分。
    drv_rrh62000.c
      代码修改其实就一个套路,增加一种传感器的注册,并在读数据接口中根据传感器种类上报对应数据。由于传感器框架存在我一直认为不合理的地方,就是如果传感器是以模组形式存在,那目前的传感器框架分别读传感器内容时,就会出现每次读都要跑去运行一遍总线的情况。这极大地增加了系统地开销。
    1. static rt_ssize_t _get_data(rt_sensor_t sensor, struct rt_sensor_data *data)
    2. {
    3.     struct sensor_device *dev = sensor->parent.user_data;
    4.     rt_bool_t isDataReady = RT_FALSE;

    5.     switch(sensor->info.type)
    6.     {
    7.     case RT_SENSOR_CLASS_TEMP:
    8.     case RT_SENSOR_CLASS_HUMI:
    9.     case RT_SENSOR_CLASS_TVOC:
    10.     case RT_SENSOR_CLASS_ECO2:
    11.     case RT_SENSOR_CLASS_IAQ:
    12.     case RT_SENSOR_CLASS_DUST:
    13.         if((rrh62000_read_data_status(dev->dev, &isDataReady) == RT_EOK) && (isDataReady))
    14.         {
    15.             rrh62000_read_measured_value(dev->dev, &dev->data);
    16.         }
    17.         break;
    18.     default:
    19.         return 0;
    20.     }

    21.     switch(sensor->info.type)
    22.     {
    23.     case RT_SENSOR_CLASS_TEMP:
    24.         data->data.temp = (dev->data.temperature.integer_part << 16) + dev->data.temperature.decimal_part;
    25.         break;
    26.         
    27.     case RT_SENSOR_CLASS_HUMI:
    28.         data->data.temp = (dev->data.humidity.integer_part << 16) + dev->data.humidity.decimal_part;
    29.         break;

    30.     case RT_SENSOR_CLASS_TVOC:
    31.         data->data.temp = (dev->data.tvoc.integer_part << 16) + dev->data.tvoc.decimal_part;
    32.         break;

    33.     case RT_SENSOR_CLASS_ECO2:
    34.         data->data.temp = (dev->data.eco2.integer_part << 16) + dev->data.eco2.decimal_part;
    35.         break;

    36.     case RT_SENSOR_CLASS_IAQ:
    37.         data->data.temp = (dev->data.iaq.integer_part << 16) + dev->data.iaq.decimal_part;
    38.         break;

    39.     case RT_SENSOR_CLASS_DUST:
    40.         data->data.temp = (dev->data.nc_2p5.integer_part << 16) + dev->data.nc_2p5.decimal_part;
    41.         break;

    42.     default:
    43.         goto RET;
    44.         break;
    45.     }

    46.     data->timestamp = rt_sensor_get_ts();
    47.     return 1;

    48. RET:
    49.     return 0;
    50. }

    51. #ifdef BSP_USING_RRH62000_TEMP
    52. rt_err_t rrh62000_register_temperature(const char *name, struct rt_sensor_config *cfg, struct sensor_device *dev)
    53. {
    54.     rt_sensor_t sensor = RT_NULL;
    55.     rt_int8_t result;

    56.     /* sensor register */
    57.     sensor = rt_calloc(1, sizeof(struct rt_sensor_device));
    58.     if (sensor == RT_NULL)
    59.         goto __exit;
    60.    
    61.     sensor->info.type       = RT_SENSOR_CLASS_TEMP; // Set real type
    62.     sensor->info.vendor     = RT_SENSOR_VENDOR_UNKNOWN; // Set real vendor
    63.     sensor->info.model      = name;  // set real model name
    64.     sensor->info.unit       = RT_SENSOR_UNIT_DCELSIUS; // set to real unit flag
    65.     sensor->info.intf_type  = RT_SENSOR_INTF_I2C; // Set interface type
    66.     sensor->info.range_max  = 0xFFFF; // Set to range max
    67.     sensor->info.range_min  = 0x0000; // Set to range min
    68.     sensor->info.period_min = 50; // Set frequency

    69.     rt_memcpy(&sensor->config, cfg, sizeof(struct rt_sensor_config));
    70.     sensor->ops = &sensor_ops;

    71.     result = rt_hw_sensor_register(sensor, name, RT_DEVICE_FLAG_RDONLY, dev);
    72.     if (result != RT_EOK)
    73.     {
    74.         goto __exit;
    75.     }

    76.     return RT_EOK;

    77. __exit:
    78.     if (sensor)
    79.         rt_free(sensor);

    80.     return -RT_ERROR;
    81. }
    82. #endif

    83. #ifdef BSP_USING_RRH62000_HUMI
    84. rt_err_t rrh62000_register_humidity(const char *name, struct rt_sensor_config *cfg, struct sensor_device *dev)
    85. {
    86.     rt_sensor_t sensor = RT_NULL;
    87.     rt_int8_t result;

    88.     /* sensor register */
    89.     sensor = rt_calloc(1, sizeof(struct rt_sensor_device));
    90.     if (sensor == RT_NULL)
    91.         goto __exit;
    92.    
    93.     sensor->info.type       = RT_SENSOR_CLASS_HUMI; // Set real type
    94.     sensor->info.vendor     = RT_SENSOR_VENDOR_UNKNOWN; // Set real vendor
    95.     sensor->info.model      = name;  // set real model name
    96.     sensor->info.unit       = RT_SENSOR_UNIT_PERMILLAGE; // set to real unit flag
    97.     sensor->info.intf_type  = RT_SENSOR_INTF_I2C; // Set interface type
    98.     sensor->info.range_max  = 0xFFFF; // Set to range max
    99.     sensor->info.range_min  = 0x0000; // Set to range min
    100.     sensor->info.period_min = 50; // Set frequency

    101.     rt_memcpy(&sensor->config, cfg, sizeof(struct rt_sensor_config));
    102.     sensor->ops = &sensor_ops;

    103.     result = rt_hw_sensor_register(sensor, name, RT_DEVICE_FLAG_RDONLY, dev);
    104.     if (result != RT_EOK)
    105.     {
    106.         goto __exit;
    107.     }

    108.     return RT_EOK;

    109. __exit:
    110.     if (sensor)
    111.         rt_free(sensor);

    112.     return -RT_ERROR;
    113. }
    114. #endif

    115. #ifdef BSP_USING_RRH62000_TVOC
    116. rt_err_t rrh62000_register_TVOC(const char *name, struct rt_sensor_config *cfg, struct sensor_device *dev)
    117. {
    118.     rt_sensor_t sensor = RT_NULL;
    119.     rt_int8_t result;

    120.     /* sensor register */
    121.     sensor = rt_calloc(1, sizeof(struct rt_sensor_device));
    122.     if (sensor == RT_NULL)
    123.         goto __exit;
    124.    
    125.     sensor->info.type       = RT_SENSOR_CLASS_TVOC; // Set real type
    126.     sensor->info.vendor     = RT_SENSOR_VENDOR_UNKNOWN; // Set real vendor
    127.     sensor->info.model      = name;  // set real model name
    128.     sensor->info.unit       = RT_SENSOR_UNIT_MGM3; // set to real unit flag
    129.     sensor->info.intf_type  = RT_SENSOR_INTF_I2C; // Set interface type
    130.     sensor->info.range_max  = 0xFFFF; // Set to range max
    131.     sensor->info.range_min  = 0x0000; // Set to range min
    132.     sensor->info.period_min = 50; // Set frequency

    133.     rt_memcpy(&sensor->config, cfg, sizeof(struct rt_sensor_config));
    134.     sensor->ops = &sensor_ops;

    135.     result = rt_hw_sensor_register(sensor, name, RT_DEVICE_FLAG_RDONLY, dev);
    136.     if (result != RT_EOK)
    137.     {
    138.         goto __exit;
    139.     }

    140.     return RT_EOK;

    141. __exit:
    142.     if (sensor)
    143.         rt_free(sensor);

    144.     return -RT_ERROR;
    145. }
    146. #endif

    147. #ifdef BSP_USING_RRH62000_DUST
    148. rt_err_t rrh62000_register_Dust(const char *name, struct rt_sensor_config *cfg, struct sensor_device *dev)
    149. {
    150.     rt_sensor_t sensor = RT_NULL;
    151.     rt_int8_t result;

    152.     /* sensor register */
    153.     sensor = rt_calloc(1, sizeof(struct rt_sensor_device));
    154.     if (sensor == RT_NULL)
    155.         goto __exit;
    156.    
    157.     sensor->info.type       = RT_SENSOR_CLASS_DUST; // Set real type
    158.     sensor->info.vendor     = RT_SENSOR_VENDOR_UNKNOWN; // Set real vendor
    159.     sensor->info.model      = name;  // set real model name
    160.     sensor->info.unit       = RT_SENSOR_UNIT_MGM3; // set to real unit flag
    161.     sensor->info.intf_type  = RT_SENSOR_INTF_I2C; // Set interface type
    162.     sensor->info.range_max  = 0xFFFF; // Set to range max
    163.     sensor->info.range_min  = 0x0000; // Set to range min
    164.     sensor->info.period_min = 50; // Set frequency

    165.     rt_memcpy(&sensor->config, cfg, sizeof(struct rt_sensor_config));
    166.     sensor->ops = &sensor_ops;

    167.     result = rt_hw_sensor_register(sensor, name, RT_DEVICE_FLAG_RDONLY, dev);
    168.     if (result != RT_EOK)
    169.     {
    170.         goto __exit;
    171.     }

    172.     return RT_EOK;

    173. __exit:
    174.     if (sensor)
    175.         rt_free(sensor);

    176.     return -RT_ERROR;
    177. }
    178. #endif

    179. #ifdef BSP_USING_RRH62000_ECO2
    180. rt_err_t rrh62000_register_ECO2(const char *name, struct rt_sensor_config *cfg, struct sensor_device *dev)
    181. {
    182.     rt_sensor_t sensor = RT_NULL;
    183.     rt_int8_t result;

    184.     /* sensor register */
    185.     sensor = rt_calloc(1, sizeof(struct rt_sensor_device));
    186.     if (sensor == RT_NULL)
    187.         goto __exit;
    188.    
    189.     sensor->info.type       = RT_SENSOR_CLASS_ECO2; // Set real type
    190.     sensor->info.vendor     = RT_SENSOR_VENDOR_UNKNOWN; // Set real vendor
    191.     sensor->info.model      = name;  // set real model name
    192.     sensor->info.unit       = RT_SENSOR_UNIT_PPM; // set to real unit flag
    193.     sensor->info.intf_type  = RT_SENSOR_INTF_I2C; // Set interface type
    194.     sensor->info.range_max  = 0xFFFF; // Set to range max
    195.     sensor->info.range_min  = 0x0000; // Set to range min
    196.     sensor->info.period_min = 50; // Set frequency

    197.     rt_memcpy(&sensor->config, cfg, sizeof(struct rt_sensor_config));
    198.     sensor->ops = &sensor_ops;

    199.     result = rt_hw_sensor_register(sensor, name, RT_DEVICE_FLAG_RDONLY, dev);
    200.     if (result != RT_EOK)
    201.     {
    202.         goto __exit;
    203.     }

    204.     return RT_EOK;

    205. __exit:
    206.     if (sensor)
    207.         rt_free(sensor);

    208.     return -RT_ERROR;
    209. }
    210. #endif

    211. #ifdef BSP_USING_RRH62000_IAQ
    212. rt_err_t rrh62000_register_IAQ(const char *name, struct rt_sensor_config *cfg, struct sensor_device *dev)
    213. {
    214.     rt_sensor_t sensor = RT_NULL;
    215.     rt_int8_t result;

    216.     /* sensor register */
    217.     sensor = rt_calloc(1, sizeof(struct rt_sensor_device));
    218.     if (sensor == RT_NULL)
    219.         goto __exit;
    220.    
    221.     sensor->info.type       = RT_SENSOR_CLASS_IAQ; // Set real type
    222.     sensor->info.vendor     = RT_SENSOR_VENDOR_UNKNOWN; // Set real vendor
    223.     sensor->info.model      = name;  // set real model name
    224.     sensor->info.unit       = RT_SENSOR_UNIT_NONE; // set to real unit flag
    225.     sensor->info.intf_type  = RT_SENSOR_INTF_I2C; // Set interface type
    226.     sensor->info.range_max  = 0xFFFF; // Set to range max
    227.     sensor->info.range_min  = 0x0000; // Set to range min
    228.     sensor->info.period_min = 50; // Set frequency

    229.     rt_memcpy(&sensor->config, cfg, sizeof(struct rt_sensor_config));
    230.     sensor->ops = &sensor_ops;

    231.     result = rt_hw_sensor_register(sensor, name, RT_DEVICE_FLAG_RDONLY, dev);
    232.     if (result != RT_EOK)
    233.     {
    234.         goto __exit;
    235.     }

    236.     return RT_EOK;

    237. __exit:
    238.     if (sensor)
    239.         rt_free(sensor);

    240.     return -RT_ERROR;
    241. }
    242. #endif

    243. static int rrh62000_device_register(void)
    244. {
    245.     rt_int8_t result;
    246.     struct sensor_device *dev;
    247.     const char *name = "rrh62000";
    248.     struct rt_sensor_config      cfg = {
    249.         .mode = RT_SENSOR_MODE_POLLING,
    250.         .power = RT_SENSOR_POWER_DOWN,
    251.     };

    252.     dev = rrh62000_param_init();
    253.     if(dev == RT_NULL)
    254.     {
    255.         goto __exit;
    256.     }

    257. #ifdef BSP_USING_RRH62000_TEMP
    258.     result = rrh62000_register_temperature(name, &cfg, dev);
    259.     if (result != RT_EOK)
    260.     {
    261.         goto __exit;
    262.     }
    263. #endif

    264. #ifdef BSP_USING_RRH62000_HUMI
    265.     result = rrh62000_register_humidity(name, &cfg, dev);
    266.     if (result != RT_EOK)
    267.     {
    268.         goto __exit;
    269.     }
    270. #endif

    271. #ifdef BSP_USING_RRH62000_TVOC
    272.     result = rrh62000_register_TVOC(name, &cfg, dev);
    273.     if (result != RT_EOK)
    274.     {
    275.         goto __exit;
    276.     }
    277. #endif

    278. #ifdef BSP_USING_RRH62000_DUST
    279.     result = rrh62000_register_Dust(name, &cfg, dev);
    280.     if (result != RT_EOK)
    281.     {
    282.         goto __exit;
    283.     }
    284. #endif

    285. #ifdef BSP_USING_RRH62000_ECO2
    286.     result = rrh62000_register_ECO2(name, &cfg, dev);
    287.     if (result != RT_EOK)
    288.     {
    289.         goto __exit;
    290.     }
    291. #endif

    292. #ifdef BSP_USING_RRH62000_IAQ
    293.     result = rrh62000_register_IAQ(name, &cfg, dev);
    294.     if (result != RT_EOK)
    295.     {
    296.         goto __exit;
    297.     }
    298. #endif

    299.     return RT_EOK;

    300. __exit:
    301.     if(dev)
    302.         rt_free(dev);
    303.     return -RT_ERROR;
    304. }
    305. INIT_DEVICE_EXPORT(rrh62000_device_register);

    306. #ifdef BSP_USING_RRH62000_TEMP
    307. #define RRH_TEMP_DEVICE_NAME "temp_rrh"
    308. static void rrh6200_temp_read(void)
    309. {
    310.     rt_device_t dev = rt_device_find(RRH_TEMP_DEVICE_NAME);
    311.     rt_err_t result;
    312.     rt_uint32_t len;
    313.     struct rt_sensor_data data;

    314.     if(!dev)
    315.     {
    316.         rt_kprintf("No device name %s\n\r", RRH_TEMP_DEVICE_NAME);
    317.         return;
    318.     }
    319.     result = rt_device_open(dev,RT_DEVICE_FLAG_RDONLY);
    320.     if(result != RT_EOK)
    321.     {
    322.         rt_kprintf("Open %s Fail\n\r", RRH_TEMP_DEVICE_NAME);
    323.         return;
    324.     }
    325.    
    326.     len = rt_device_read(dev, 0 ,&data,1);
    327.     if(len)
    328.     {
    329.         rt_kprintf("Temp %d.%d \n\r", data.data.temp >> 16, data.data.temp & 0x0000FFFF);
    330.     }

    331.     result = rt_device_close(dev);
    332.     if(result != RT_EOK)
    333.     {
    334.         rt_kprintf("Close %s Fail\n\r", RRH_TEMP_DEVICE_NAME);
    335.         return;
    336.     }
    337. }
    338. MSH_CMD_EXPORT(rrh6200_temp_read, rrh62000 sample);
    339. #endif


    340. #ifdef BSP_USING_RRH62000_HUMI
    341. #define RRH_HUMI_DEVICE_NAME "humi_rrh"
    342. static void rrh6200_humi_read(void)
    343. {
    344.     rt_device_t dev = rt_device_find(RRH_HUMI_DEVICE_NAME);
    345.     rt_err_t result;
    346.     rt_uint32_t len;
    347.     struct rt_sensor_data data;

    348.     if(!dev)
    349.     {
    350.         rt_kprintf("No device name %s\n\r", RRH_HUMI_DEVICE_NAME);
    351.         return;
    352.     }
    353.     result = rt_device_open(dev,RT_DEVICE_FLAG_RDONLY);
    354.     if(result != RT_EOK)
    355.     {
    356.         rt_kprintf("Open %s Fail\n\r", RRH_HUMI_DEVICE_NAME);
    357.         return;
    358.     }
    359.    
    360.     len = rt_device_read(dev, 0 ,&data,1);
    361.     if(len)
    362.     {
    363.         rt_kprintf("Humidity %d.%d \n\r", data.data.humi >> 16, data.data.humi & 0x0000FFFF);
    364.     }

    365.     result = rt_device_close(dev);
    366.     if(result != RT_EOK)
    367.     {
    368.         rt_kprintf("Close %s Fail\n\r", RRH_HUMI_DEVICE_NAME);
    369.         return;
    370.     }
    371. }
    372. MSH_CMD_EXPORT(rrh6200_humi_read, rrh62000 sample);
    373. #endif

    374. #ifdef BSP_USING_RRH62000_TVOC
    375. #define RRH_TVOC_DEVICE_NAME "tvoc_rrh"
    376. static void rrh6200_tvoc_read(void)
    377. {
    378.     rt_device_t dev = rt_device_find(RRH_TVOC_DEVICE_NAME);
    379.     rt_err_t result;
    380.     rt_uint32_t len;
    381.     struct rt_sensor_data data;

    382.     if(!dev)
    383.     {
    384.         rt_kprintf("No device name %s\n\r", RRH_TVOC_DEVICE_NAME);
    385.         return;
    386.     }
    387.     result = rt_device_open(dev,RT_DEVICE_FLAG_RDONLY);
    388.     if(result != RT_EOK)
    389.     {
    390.         rt_kprintf("Open %s Fail\n\r", RRH_TVOC_DEVICE_NAME);
    391.         return;
    392.     }
    393.    
    394.     len = rt_device_read(dev, 0 ,&data,1);
    395.     if(len)
    396.     {
    397.         rt_kprintf("TVOC %d.%d \n\r", data.data.tvoc >> 16, data.data.tvoc & 0x0000FFFF);
    398.     }

    399.     result = rt_device_close(dev);
    400.     if(result != RT_EOK)
    401.     {
    402.         rt_kprintf("Close %s Fail\n\r", RRH_TVOC_DEVICE_NAME);
    403.         return;
    404.     }
    405. }
    406. MSH_CMD_EXPORT(rrh6200_tvoc_read, rrh62000 sample);
    407. #endif

    408. #ifdef BSP_USING_RRH62000_DUST
    409. #define RRH_DUST_DEVICE_NAME "dust_rrh"
    410. static void rrh6200_dust_read(void)
    411. {
    412.     rt_device_t dev = rt_device_find(RRH_DUST_DEVICE_NAME);
    413.     rt_err_t result;
    414.     rt_uint32_t len;
    415.     struct rt_sensor_data data;

    416.     if(!dev)
    417.     {
    418.         rt_kprintf("No device name %s\n\r", RRH_DUST_DEVICE_NAME);
    419.         return;
    420.     }
    421.     result = rt_device_open(dev,RT_DEVICE_FLAG_RDONLY);
    422.     if(result != RT_EOK)
    423.     {
    424.         rt_kprintf("Open %s Fail\n\r", RRH_DUST_DEVICE_NAME);
    425.         return;
    426.     }
    427.    
    428.     len = rt_device_read(dev, 0 ,&data,1);
    429.     if(len)
    430.     {
    431.         rt_kprintf("dust %d.%d \n\r", data.data.dust >> 16, data.data.dust & 0x0000FFFF);
    432.     }

    433.     result = rt_device_close(dev);
    434.     if(result != RT_EOK)
    435.     {
    436.         rt_kprintf("Close %s Fail\n\r", RRH_DUST_DEVICE_NAME);
    437.         return;
    438.     }
    439. }
    440. MSH_CMD_EXPORT(rrh6200_dust_read, rrh62000 sample);
    441. #endif

    442. #ifdef BSP_USING_RRH62000_ECO2
    443. #define RRH_ECO2_DEVICE_NAME "eco2_rrh"
    444. static void rrh6200_eco2_read(void)
    445. {
    446.     rt_device_t dev = rt_device_find(RRH_ECO2_DEVICE_NAME);
    447.     rt_err_t result;
    448.     rt_uint32_t len;
    449.     struct rt_sensor_data data;

    450.     if(!dev)
    451.     {
    452.         rt_kprintf("No device name %s\n\r", RRH_ECO2_DEVICE_NAME);
    453.         return;
    454.     }
    455.     result = rt_device_open(dev,RT_DEVICE_FLAG_RDONLY);
    456.     if(result != RT_EOK)
    457.     {
    458.         rt_kprintf("Open %s Fail\n\r", RRH_ECO2_DEVICE_NAME);
    459.         return;
    460.     }
    461.    
    462.     len = rt_device_read(dev, 0 ,&data,1);
    463.     if(len)
    464.     {
    465.         rt_kprintf("ECO2 %d.%d \n\r", data.data.eco2 >> 16, data.data.eco2 & 0x0000FFFF);
    466.     }

    467.     result = rt_device_close(dev);
    468.     if(result != RT_EOK)
    469.     {
    470.         rt_kprintf("Close %s Fail\n\r", RRH_ECO2_DEVICE_NAME);
    471.         return;
    472.     }
    473. }
    474. MSH_CMD_EXPORT(rrh6200_eco2_read, rrh62000 sample);
    475. #endif


    476. #ifdef BSP_USING_RRH62000_IAQ
    477. #define RRH_IAQ_DEVICE_NAME "iaq_rrh6"
    478. static void rrh6200_iaq_read(void)
    479. {
    480.     rt_device_t dev = rt_device_find(RRH_IAQ_DEVICE_NAME);
    481.     rt_err_t result;
    482.     rt_uint32_t len;
    483.     struct rt_sensor_data data;

    484.     if(!dev)
    485.     {
    486.         rt_kprintf("No device name %s\n\r", RRH_IAQ_DEVICE_NAME);
    487.         return;
    488.     }
    489.     result = rt_device_open(dev,RT_DEVICE_FLAG_RDONLY);
    490.     if(result != RT_EOK)
    491.     {
    492.         rt_kprintf("Open %s Fail\n\r", RRH_IAQ_DEVICE_NAME);
    493.         return;
    494.     }
    495.    
    496.     len = rt_device_read(dev, 0 ,&data,1);
    497.     if(len)
    498.     {
    499.         rt_kprintf("IAQ %d.%d \n\r", data.data.iaq >> 16, data.data.iaq & 0x0000FFFF);
    500.     }

    501.     result = rt_device_close(dev);
    502.     if(result != RT_EOK)
    503.     {
    504.         rt_kprintf("Close %s Fail\n\r", RRH_IAQ_DEVICE_NAME);
    505.         return;
    506.     }
    507. }
    508. MSH_CMD_EXPORT(rrh6200_iaq_read, rrh62000 sample);
    509. #endif
    复制代码

    KConfig
    1.     menu "Board extended module Drivers"
    2.         menuconfig BSP_USING_RRH62000
    3.         bool "Enable RRH62000"
    4.         default n
    5.         if BSP_USING_RRH62000
    6.             config BSP_USING_RRH62000_TEMP
    7.                 bool "Enable RRH62000 Temperature"
    8.                     default n

    9.             config BSP_USING_RRH62000_HUMI
    10.                 bool "Enable RRH62000 Humidity"
    11.                     default n

    12.             config BSP_USING_RRH62000_TVOC
    13.                 bool "Enable RRH62000 TVOC"
    14.                     default n

    15.             config BSP_USING_RRH62000_ECO2
    16.                 bool "Enable RRH62000 ECO2"
    17.                     default n

    18.             config BSP_USING_RRH62000_DUST
    19.                 bool "Enable RRH62000 Dust"
    20.                     default n

    21.             config BSP_USING_RRH62000_IAQ
    22.                 bool "Enable RRH62000 IAQ"
    23.                     default n
    24.         endif

    25.     endmenu
    复制代码
      以上部分修改完后,需运行menuconfig选配好对应的传感器功能并运行scons --target=mdk5生成新工程。我的测试环境是将所有传感器功能都启用。
    运行结果
    1. \ | /
    2. - RT -     Thread Operating System
    3. / | \     5.2.0 build Nov 30 2024 20:07:57
    4. 2006 - 2024 Copyright by RT-Thread team
    5. [I/I2C] I2C bus [i2c1] registered
    6. rrh62000 firmware version 1.0
    7. rrh62000 algoritm version 3.2.0
    8. [I/sensor] rt_sensor[temp_rrh62000] init success
    9. [I/sensor] rt_sensor[humi_rrh62000] init success
    10. [I/sensor] rt_sensor[tvoc_rrh62000] init success
    11. [I/sensor] rt_sensor[dust_rrh62000] init success
    12. [I/sensor] rt_sensor[eco2_rrh62000] init success
    13. [I/sensor] rt_sensor[iaq_rrh62000] init success

    14. Hello RT-Thread!
    15. msh >rrh6200_iaq_read
    16. IAQ 0.0
    17. msh >rrh6200_iaq_read
    18. IAQ 1.0
    19. msh >rrh6200_eco2_read
    20. ECO2 400.0
    21. msh >rrh6200_dust_read
    22. dust 0.1
    23. msh >rrh6200_tvoc_read
    24. TVOC 0.2
    25. msh >rrh6200_temp_read
    26. Temp 21.85
    27. msh >rrh6200_humi_read
    28. Humidity 41.75
    29. msh >
    复制代码



    回复

    使用道具 举报

    您需要登录后才可以回帖 注册/登录

    本版积分规则

    关闭

    站长推荐上一条 /5 下一条

    手机版|小黑屋|与非网

    GMT+8, 2024-12-23 21:56 , Processed in 0.115114 second(s), 16 queries , MemCache On.

    ICP经营许可证 苏B2-20140176  苏ICP备14012660号-2   苏州灵动帧格网络科技有限公司 版权所有.

    苏公网安备 32059002001037号

    Powered by Discuz! X3.4

    Copyright © 2001-2024, Tencent Cloud.