From f4b285dc9cc1a2751ad090afa2f582349197d179 Mon Sep 17 00:00:00 2001 From: "Man, Jianting (Meco)" <920369182@qq.com> Date: Sun, 8 Jan 2023 21:33:38 -0500 Subject: [PATCH] improve sensor framework (#6784) * [stm32l475-pandora] rename sensor_port.c as drv_sensors.c * [sensor][cmd] add enter critical section protection * [sensor] add sensor reset command --- bsp/stm32/stm32l475-atk-pandora/board/SConscript | 4 ++-- .../board/ports/{sensor_port.c => drv_sensors.c} | 0 components/drivers/sensor/sensor.c | 7 +------ components/drivers/sensor/sensor_cmd.c | 14 ++++++++++++++ 4 files changed, 17 insertions(+), 8 deletions(-) rename bsp/stm32/stm32l475-atk-pandora/board/ports/{sensor_port.c => drv_sensors.c} (100%) diff --git a/bsp/stm32/stm32l475-atk-pandora/board/SConscript b/bsp/stm32/stm32l475-atk-pandora/board/SConscript index 8131ce76b7..a2d6f54677 100644 --- a/bsp/stm32/stm32l475-atk-pandora/board/SConscript +++ b/bsp/stm32/stm32l475-atk-pandora/board/SConscript @@ -27,8 +27,8 @@ if GetDepend(['BSP_USING_FS']): src += Glob('ports/fal/fal_spi_flash_sfud_port.c') path += [cwd + '/ports/fal'] -if GetDepend(['BSP_USING_ICM20608']) or GetDepend(['BSP_USING_AHT10']): - src += Glob('ports/sensor_port.c') +if GetDepend(['RT_USING_SENSOR']): + src += Glob('ports/drv_sensors.c') if GetDepend(['BSP_USING_AUDIO']): src += Glob('ports/audio/drv_es8388.c') diff --git a/bsp/stm32/stm32l475-atk-pandora/board/ports/sensor_port.c b/bsp/stm32/stm32l475-atk-pandora/board/ports/drv_sensors.c similarity index 100% rename from bsp/stm32/stm32l475-atk-pandora/board/ports/sensor_port.c rename to bsp/stm32/stm32l475-atk-pandora/board/ports/drv_sensors.c diff --git a/components/drivers/sensor/sensor.c b/components/drivers/sensor/sensor.c index 025182862b..40e4b46037 100644 --- a/components/drivers/sensor/sensor.c +++ b/components/drivers/sensor/sensor.c @@ -228,8 +228,8 @@ __exit: static rt_err_t _sensor_close(rt_device_t dev) { rt_sensor_t sensor = (rt_sensor_t)dev; - int i; rt_err_t (*local_ctrl)(rt_sensor_t sensor, int cmd, void *arg) = _local_control; + int i; RT_ASSERT(dev != RT_NULL); @@ -517,10 +517,5 @@ int rt_hw_sensor_register(rt_sensor_t sensor, LOG_I("sensor[%s] init success", device_name); rt_free(device_name); - /* set sensor accuracy and power as the hightest, and polling data as default */ - rt_device_control(device, RT_SENSOR_CTRL_SET_ACCURACY_MODE, RT_SENSOR_MODE_ACCURACY_HIGHEST); - rt_device_control(device, RT_SENSOR_CTRL_SET_POWER_MODE, RT_SENSOR_MODE_POWER_HIGHEST); - rt_device_control(device, RT_SENSOR_CTRL_SET_FETCH_MODE, RT_SENSOR_MODE_FETCH_POLLING); - return RT_EOK; } diff --git a/components/drivers/sensor/sensor_cmd.c b/components/drivers/sensor/sensor_cmd.c index 2069a1182d..db6061b675 100644 --- a/components/drivers/sensor/sensor_cmd.c +++ b/components/drivers/sensor/sensor_cmd.c @@ -542,6 +542,7 @@ static void sensor_cmd_warning_unknown(void) rt_kprintf(" power [mode] set or get power mode\n"); rt_kprintf(" accuracy [mode] set or get accuracy mode\n"); rt_kprintf(" fetch [mode] set or get fetch data mode\n"); + rt_kprintf(" reset reset sensor chip\n"); } static void sensor_cmd_warning_probe(void) @@ -649,6 +650,19 @@ static void sensor(int argc, char **argv) 2, sensor_dev->info.scale.range_min, 2, sensor_dev->info.scale.range_max, 5, sensor_get_unit_name(&sensor_dev->info)); } } + else if (!rt_strcmp(argv[1], "reset")) + { + if (dev == RT_NULL) + { + sensor_cmd_warning_probe(); + return; + } + + if (rt_device_control(dev, RT_SENSOR_CTRL_SOFT_RESET, RT_NULL) != RT_EOK) + { + LOG_E("This sensor doesn't support this command!"); + } + } else if (!rt_strcmp(argv[1], "probe")) { rt_uint8_t reg = 0xFF;