[adc.c] add finsh voltage conversion command (#5937)

* Update adc.c

add finsh voltage conversion command
This commit is contained in:
Stanley Lwin 2022-05-18 19:36:50 -07:00 committed by GitHub
parent c147ff417c
commit f92c26a60b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 44 additions and 9 deletions

View File

@ -7,6 +7,7 @@
* Date Author Notes * Date Author Notes
* 2018-05-07 aozima the first version * 2018-05-07 aozima the first version
* 2018-11-16 Ernest Chen add finsh command and update adc function * 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__ #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_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_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_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__ */ #endif /* __ADC_H__ */

View File

@ -7,6 +7,7 @@
* Date Author Notes * Date Author Notes
* 2018-05-07 aozima the first version * 2018-05-07 aozima the first version
* 2018-11-16 Ernest Chen add finsh command and update adc function * 2018-11-16 Ernest Chen add finsh command and update adc function
* 2022-05-11 Stanley Lwin add finsh voltage conversion command
*/ */
#include <rtthread.h> #include <rtthread.h>
@ -16,6 +17,7 @@
#include <stdlib.h> #include <stdlib.h>
#define DBG_TAG "adc" #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 #define DBG_LVL DBG_INFO
#include <rtdbg.h> #include <rtdbg.h>
@ -152,12 +154,29 @@ rt_err_t rt_adc_disable(rt_adc_device_t dev, rt_uint32_t channel)
return result; 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 #ifdef RT_USING_FINSH
static int adc(int argc, char **argv) static int adc(int argc, char **argv)
{ {
int value = 0; int value = 0, voltage = 0;
int result = RT_EOK; rt_err_t result = -RT_ERROR;
static rt_adc_device_t adc_device = RT_NULL; static rt_adc_device_t adc_device = RT_NULL;
char *result_str; char *result_str;
@ -173,14 +192,14 @@ static int adc(int argc, char **argv)
} }
else else
{ {
rt_kprintf("adc probe <adc_name> - probe adc by name\n"); rt_kprintf("adc probe <device name> - probe adc by name\n");
} }
} }
else else
{ {
if (adc_device == RT_NULL) if (adc_device == RT_NULL)
{ {
rt_kprintf("Please using 'adc probe <adc_name>' first\n"); rt_kprintf("Please using 'adc probe <device name>' first\n");
return -RT_ERROR; return -RT_ERROR;
} }
if (!strcmp(argv[1], "enable")) if (!strcmp(argv[1], "enable"))
@ -221,6 +240,19 @@ static int adc(int argc, char **argv)
rt_kprintf("adc disable <channel> - disable adc channel\n"); rt_kprintf("adc disable <channel> - 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 <channel> \n");
}
}
else else
{ {
rt_kprintf("Unknown command. Please enter 'adc' for help\n"); rt_kprintf("Unknown command. Please enter 'adc' for help\n");
@ -230,14 +262,15 @@ static int adc(int argc, char **argv)
else else
{ {
rt_kprintf("Usage: \n"); rt_kprintf("Usage: \n");
rt_kprintf("adc probe <adc_name> - probe adc by name\n"); rt_kprintf("adc probe <device name> - probe adc by name\n");
rt_kprintf("adc read <channel> - read adc value on the channel\n"); rt_kprintf("adc read <channel> - read adc value on the channel\n");
rt_kprintf("adc disable <channel> - disable adc channel\n"); rt_kprintf("adc disable <channel> - disable adc channel\n");
rt_kprintf("adc enable <channel> - enable adc channel\n"); rt_kprintf("adc enable <channel> - enable adc channel\n");
result = -RT_ERROR; result = -RT_ERROR;
} }
return RT_EOK; return RT_EOK;
} }
MSH_CMD_EXPORT(adc, adc function); MSH_CMD_EXPORT(adc, adc <device name> <option> <channel>);
#endif /* RT_USING_FINSH */ #endif /* RT_USING_FINSH */