From f92c26a60b8a1e83d3cb7974f7af799674435a69 Mon Sep 17 00:00:00 2001 From: Stanley Lwin <98496195+byte-me-stan@users.noreply.github.com> Date: Wed, 18 May 2022 19:36:50 -0700 Subject: [PATCH] [adc.c] add finsh voltage conversion command (#5937) * Update adc.c add finsh voltage conversion command --- components/drivers/include/drivers/adc.h | 2 + components/drivers/misc/adc.c | 51 +++++++++++++++++++----- 2 files changed, 44 insertions(+), 9 deletions(-) diff --git a/components/drivers/include/drivers/adc.h b/components/drivers/include/drivers/adc.h index d251a33a53..2a4bdceb46 100644 --- a/components/drivers/include/drivers/adc.h +++ b/components/drivers/include/drivers/adc.h @@ -7,6 +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 */ #ifndef __ADC_H__ @@ -40,5 +41,6 @@ rt_err_t rt_hw_adc_register(rt_adc_device_t adc,const char *name, const struct r rt_uint32_t rt_adc_read(rt_adc_device_t dev, rt_uint32_t channel); rt_err_t rt_adc_enable(rt_adc_device_t dev, rt_uint32_t channel); 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); #endif /* __ADC_H__ */ diff --git a/components/drivers/misc/adc.c b/components/drivers/misc/adc.c index 9c87919793..f652bf1f48 100644 --- a/components/drivers/misc/adc.c +++ b/components/drivers/misc/adc.c @@ -7,6 +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 */ #include @@ -16,6 +17,7 @@ #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 @@ -152,12 +154,29 @@ rt_err_t rt_adc_disable(rt_adc_device_t dev, rt_uint32_t channel) return result; } +rt_uint32_t rt_adc_voltage(rt_adc_device_t dev, rt_uint32_t channel) +{ + rt_uint32_t value = 0, voltage = 0; + + RT_ASSERT(dev); + + /*read the value and convert to voltage*/ + if (dev->ops->get_resolution != RT_NULL && dev->ops->convert != 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); + } + + return voltage; +} #ifdef RT_USING_FINSH static int adc(int argc, char **argv) { - int value = 0; - int result = RT_EOK; + int value = 0, voltage = 0; + rt_err_t result = -RT_ERROR; static rt_adc_device_t adc_device = RT_NULL; char *result_str; @@ -173,14 +192,14 @@ static int adc(int argc, char **argv) } else { - rt_kprintf("adc probe - probe adc by name\n"); + rt_kprintf("adc probe - probe adc by name\n"); } } else { if (adc_device == RT_NULL) { - rt_kprintf("Please using 'adc probe ' first\n"); + rt_kprintf("Please using 'adc probe ' first\n"); return -RT_ERROR; } if (!strcmp(argv[1], "enable")) @@ -221,6 +240,19 @@ static int adc(int argc, char **argv) rt_kprintf("adc disable - disable adc channel\n"); } } + else if (!strcmp(argv[1], "voltage")) + { + if(argc == 3) + { + 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); + } + else + { + rt_kprintf("adc convert voltage \n"); + } + } else { rt_kprintf("Unknown command. Please enter 'adc' for help\n"); @@ -230,14 +262,15 @@ static int adc(int argc, char **argv) else { rt_kprintf("Usage: \n"); - rt_kprintf("adc probe - probe adc by name\n"); - rt_kprintf("adc read - read adc value on the channel\n"); - rt_kprintf("adc disable - disable adc channel\n"); - rt_kprintf("adc enable - enable adc channel\n"); + rt_kprintf("adc probe - probe adc by name\n"); + rt_kprintf("adc read - read adc value on the channel\n"); + rt_kprintf("adc disable - disable adc channel\n"); + rt_kprintf("adc enable - enable adc channel\n"); + result = -RT_ERROR; } return RT_EOK; } -MSH_CMD_EXPORT(adc, adc function); +MSH_CMD_EXPORT(adc, adc