From 4b9567e7f072507642d21552d8dc448bca1ad467 Mon Sep 17 00:00:00 2001 From: Stanley Lwin Date: Mon, 23 May 2022 12:34:24 -0700 Subject: [PATCH 01/10] add adc_get_vref add stm32_adc_get_vref --- bsp/stm32/libraries/HAL_Drivers/drv_adc.c | 10 ++++++++-- components/drivers/include/drivers/adc.h | 6 ++++-- components/drivers/misc/adc.c | 18 ++++++++++++------ 3 files changed, 24 insertions(+), 10 deletions(-) diff --git a/bsp/stm32/libraries/HAL_Drivers/drv_adc.c b/bsp/stm32/libraries/HAL_Drivers/drv_adc.c index dcff684461e..aad04b82233 100644 --- a/bsp/stm32/libraries/HAL_Drivers/drv_adc.c +++ b/bsp/stm32/libraries/HAL_Drivers/drv_adc.c @@ -1,5 +1,5 @@ /* - * Copyright (c) 2006-2021, RT-Thread Development Team + * Copyright (c) 2006-2022, RT-Thread Development Team * * SPDX-License-Identifier: Apache-2.0 * @@ -10,6 +10,7 @@ * 2019-02-01 yuneizhilin fix the stm32_adc_init function initialization issue * 2020-06-17 thread-liu Porting for stm32mp1xx * 2020-10-14 Dozingfiretruck Porting for stm32wbxx + * 2022-05-22 Stanley Lwin Add stm32_adc_get_vref */ #include @@ -169,7 +170,11 @@ static rt_uint32_t stm32_adc_get_channel(rt_uint32_t channel) return stm32_channel; } - +static rt_uint32_t stm32_adc_get_vref (struct rt_adc_device *device) +{ + RT_ASSERT(device); + return 3300; +} static rt_err_t stm32_adc_get_value(struct rt_adc_device *device, rt_uint32_t channel, rt_uint32_t *value) { ADC_ChannelConfTypeDef ADC_ChanConf; @@ -285,6 +290,7 @@ static const struct rt_adc_ops stm_adc_ops = .enabled = stm32_adc_enabled, .convert = stm32_adc_get_value, .get_resolution = stm32_adc_get_resolution, + .get_vref = stm32_adc_get_vref, }; static int stm32_adc_init(void) diff --git a/components/drivers/include/drivers/adc.h b/components/drivers/include/drivers/adc.h index 2a4bdceb46d..74e891cdb62 100644 --- a/components/drivers/include/drivers/adc.h +++ b/components/drivers/include/drivers/adc.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2006-2021, RT-Thread Development Team + * Copyright (c) 2006-2022, RT-Thread Development Team * * SPDX-License-Identifier: Apache-2.0 * @@ -7,7 +7,7 @@ * Date Author Notes * 2018-05-07 aozima the first version * 2018-11-16 Ernest Chen add finsh command and update adc function - * 2022-05-11 Stanley Lwin add finsh voltage conversion command + * 2022-05-11 Stanley Lwin add finsh voltage conversion command */ #ifndef __ADC_H__ @@ -20,6 +20,7 @@ struct rt_adc_ops rt_err_t (*enabled)(struct rt_adc_device *device, rt_uint32_t channel, rt_bool_t enabled); rt_err_t (*convert)(struct rt_adc_device *device, rt_uint32_t channel, rt_uint32_t *value); rt_uint8_t (*get_resolution)(struct rt_adc_device *device); + rt_uint32_t (*get_vref) (struct rt_adc_device *device); }; struct rt_adc_device @@ -34,6 +35,7 @@ typedef enum RT_ADC_CMD_ENABLE = RT_DEVICE_CTRL_BASE(ADC) + 1, RT_ADC_CMD_DISABLE = RT_DEVICE_CTRL_BASE(ADC) + 2, RT_ADC_CMD_GET_RESOLUTION = RT_DEVICE_CTRL_BASE(ADC) + 3, + RT_ADC_CMD_GET_VREF = RT_DEVICE_CTRL_BASE(ADC) + 4, } rt_adc_cmd_t; rt_err_t rt_hw_adc_register(rt_adc_device_t adc,const char *name, const struct rt_adc_ops *ops, const void *user_data); diff --git a/components/drivers/misc/adc.c b/components/drivers/misc/adc.c index f652bf1f480..1b3706e6294 100644 --- a/components/drivers/misc/adc.c +++ b/components/drivers/misc/adc.c @@ -1,5 +1,5 @@ /* - * Copyright (c) 2006-2021, RT-Thread Development Team + * Copyright (c) 2006-2022, RT-Thread Development Team * * SPDX-License-Identifier: Apache-2.0 * @@ -7,7 +7,7 @@ * Date Author Notes * 2018-05-07 aozima the first version * 2018-11-16 Ernest Chen add finsh command and update adc function - * 2022-05-11 Stanley Lwin add finsh voltage conversion command + * 2022-05-11 Stanley Lwin add finsh voltage conversion command */ #include @@ -17,7 +17,6 @@ #include #define DBG_TAG "adc" -#define REFER_VOLTAGE 330 /*reference voltage, multiplied by 100 and reserve 2 decimal places for data accuracy*/ #define DBG_LVL DBG_INFO #include @@ -64,6 +63,10 @@ static rt_err_t _adc_control(rt_device_t dev, int cmd, void *args) result = RT_EOK; } } + else if (cmd == RT_ADC_CMD_GET_VREF && adc->ops->get_resolution) + { + result = adc->ops->get_vref(adc); + } return result; } @@ -156,17 +159,20 @@ rt_err_t rt_adc_disable(rt_adc_device_t dev, rt_uint32_t channel) rt_uint32_t rt_adc_voltage(rt_adc_device_t dev, rt_uint32_t channel) { - rt_uint32_t value = 0, voltage = 0; + rt_uint32_t value = 0, voltage = 0, vref = 0; RT_ASSERT(dev); /*read the value and convert to voltage*/ if (dev->ops->get_resolution != RT_NULL && dev->ops->convert != RT_NULL) { + /*get reference voltage*/ + vref = _adc_control((rt_device_t) dev, RT_ADC_CMD_GET_VREF, RT_NULL ); + /*get the convert bits*/ rt_uint8_t resolution = dev->ops->get_resolution(dev); dev->ops->convert(dev, channel, &value); - voltage = value * REFER_VOLTAGE / (1 << resolution); + voltage = value * vref / (1 << resolution); } return voltage; @@ -246,7 +252,7 @@ static int adc(int argc, char **argv) { voltage = rt_adc_voltage(adc_device, atoi(argv[2])); result_str = (result == RT_EOK) ? "success" : "failure"; - rt_kprintf("%s channel %d voltage is %d.%02d \n", adc_device->parent.parent.name, atoi(argv[2]), voltage / 100, voltage % 100); + rt_kprintf("%s channel %d voltage is %d.%03dV \n", adc_device->parent.parent.name, atoi(argv[2]), voltage / 1000, voltage % 1000); } else { From 5f17a31c411ea5ad47b0fadccb0b8cda6a80ac76 Mon Sep 17 00:00:00 2001 From: Stanley Lwin Date: Mon, 23 May 2022 12:37:48 -0700 Subject: [PATCH 02/10] Update adc.c fixed formatting issue --- components/drivers/misc/adc.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/components/drivers/misc/adc.c b/components/drivers/misc/adc.c index 1b3706e6294..9145ef3d9f7 100644 --- a/components/drivers/misc/adc.c +++ b/components/drivers/misc/adc.c @@ -159,7 +159,7 @@ rt_err_t rt_adc_disable(rt_adc_device_t dev, rt_uint32_t channel) rt_uint32_t rt_adc_voltage(rt_adc_device_t dev, rt_uint32_t channel) { - rt_uint32_t value = 0, voltage = 0, vref = 0; + rt_uint32_t value = 0, voltage = 0, vref = 0; RT_ASSERT(dev); From a5287099a6b05cef90a4cdb00b3ca3a69c6038f2 Mon Sep 17 00:00:00 2001 From: Stanley Lwin Date: Fri, 27 May 2022 02:56:28 -0700 Subject: [PATCH 03/10] resolved feedbacks --- bsp/stm32/libraries/HAL_Drivers/drv_adc.c | 4 ++- components/drivers/include/drivers/adc.h | 6 ++--- components/drivers/misc/adc.c | 30 +++++++++++++++++------ 3 files changed, 29 insertions(+), 11 deletions(-) diff --git a/bsp/stm32/libraries/HAL_Drivers/drv_adc.c b/bsp/stm32/libraries/HAL_Drivers/drv_adc.c index aad04b82233..2ed648321ee 100644 --- a/bsp/stm32/libraries/HAL_Drivers/drv_adc.c +++ b/bsp/stm32/libraries/HAL_Drivers/drv_adc.c @@ -170,11 +170,13 @@ static rt_uint32_t stm32_adc_get_channel(rt_uint32_t channel) return stm32_channel; } -static rt_uint32_t stm32_adc_get_vref (struct rt_adc_device *device) + +static rt_uint16_t stm32_adc_get_vref (struct rt_adc_device *device) { RT_ASSERT(device); return 3300; } + static rt_err_t stm32_adc_get_value(struct rt_adc_device *device, rt_uint32_t channel, rt_uint32_t *value) { ADC_ChannelConfTypeDef ADC_ChanConf; diff --git a/components/drivers/include/drivers/adc.h b/components/drivers/include/drivers/adc.h index 74e891cdb62..6d2bd883539 100644 --- a/components/drivers/include/drivers/adc.h +++ b/components/drivers/include/drivers/adc.h @@ -20,7 +20,7 @@ struct rt_adc_ops rt_err_t (*enabled)(struct rt_adc_device *device, rt_uint32_t channel, rt_bool_t enabled); rt_err_t (*convert)(struct rt_adc_device *device, rt_uint32_t channel, rt_uint32_t *value); rt_uint8_t (*get_resolution)(struct rt_adc_device *device); - rt_uint32_t (*get_vref) (struct rt_adc_device *device); + rt_uint16_t (*get_vref) (const struct rt_adc_device *device); }; struct rt_adc_device @@ -34,8 +34,8 @@ typedef enum { RT_ADC_CMD_ENABLE = RT_DEVICE_CTRL_BASE(ADC) + 1, RT_ADC_CMD_DISABLE = RT_DEVICE_CTRL_BASE(ADC) + 2, - RT_ADC_CMD_GET_RESOLUTION = RT_DEVICE_CTRL_BASE(ADC) + 3, - RT_ADC_CMD_GET_VREF = RT_DEVICE_CTRL_BASE(ADC) + 4, + RT_ADC_CMD_GET_RESOLUTION = RT_DEVICE_CTRL_BASE(ADC) + 3, /* get the resolution in bits */ + RT_ADC_CMD_GET_VREF = RT_DEVICE_CTRL_BASE(ADC) + 4, /* get reference voltage */ } rt_adc_cmd_t; rt_err_t rt_hw_adc_register(rt_adc_device_t adc,const char *name, const struct rt_adc_ops *ops, const void *user_data); diff --git a/components/drivers/misc/adc.c b/components/drivers/misc/adc.c index 9145ef3d9f7..56c27d609e6 100644 --- a/components/drivers/misc/adc.c +++ b/components/drivers/misc/adc.c @@ -53,7 +53,7 @@ static rt_err_t _adc_control(rt_device_t dev, int cmd, void *args) { result = adc->ops->enabled(adc, (rt_uint32_t)args, RT_FALSE); } - else if (cmd == RT_ADC_CMD_GET_RESOLUTION && adc->ops->get_resolution) + else if (cmd == RT_ADC_CMD_GET_RESOLUTION && adc->ops->get_resolution && args) { rt_uint8_t resolution = adc->ops->get_resolution(adc); if(resolution != 0) @@ -63,9 +63,21 @@ static rt_err_t _adc_control(rt_device_t dev, int cmd, void *args) result = RT_EOK; } } - else if (cmd == RT_ADC_CMD_GET_VREF && adc->ops->get_resolution) + else if (cmd == RT_ADC_CMD_GET_VREF && adc->ops->get_vref && args) { - result = adc->ops->get_vref(adc); + rt_int16_t value; + + value = adc->ops->get_vref(adc); + *((rt_int16_t *) args) = value; + + if(value == 0) + { + result = -RT_ENOSYS; + } + else + { + result = RT_EOK; + } } return result; @@ -159,15 +171,19 @@ rt_err_t rt_adc_disable(rt_adc_device_t dev, rt_uint32_t channel) rt_uint32_t rt_adc_voltage(rt_adc_device_t dev, rt_uint32_t channel) { - rt_uint32_t value = 0, voltage = 0, vref = 0; + rt_uint32_t value = 0, voltage = 0; + rt_int16_t vref = 0; RT_ASSERT(dev); /*read the value and convert to voltage*/ - if (dev->ops->get_resolution != RT_NULL && dev->ops->convert != RT_NULL) + if(_adc_control((rt_device_t) dev, RT_ADC_CMD_GET_RESOLUTION, RT_NULL ) != RT_NULL) { /*get reference voltage*/ - vref = _adc_control((rt_device_t) dev, RT_ADC_CMD_GET_VREF, RT_NULL ); + if(_adc_control((rt_device_t) dev, RT_ADC_CMD_GET_VREF, &vref) != RT_EOK ) + { + return 0; + } /*get the convert bits*/ rt_uint8_t resolution = dev->ops->get_resolution(dev); @@ -279,4 +295,4 @@ static int adc(int argc, char **argv) } MSH_CMD_EXPORT(adc, adc