未验证 提交 f92c26a6 编写于 作者: S Stanley Lwin 提交者: GitHub

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

* Update adc.c

add finsh voltage conversion command
上级 c147ff41
......@@ -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__ */
......@@ -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 <rtthread.h>
......@@ -16,6 +17,7 @@
#include <stdlib.h>
#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 <rtdbg.h>
......@@ -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 <adc_name> - probe adc by name\n");
rt_kprintf("adc probe <device name> - probe adc by name\n");
}
}
else
{
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;
}
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");
}
}
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
{
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 <adc_name> - probe adc by name\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 enable <channel> - enable adc channel\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 disable <channel> - disable adc channel\n");
rt_kprintf("adc enable <channel> - enable adc channel\n");
result = -RT_ERROR;
}
return RT_EOK;
}
MSH_CMD_EXPORT(adc, adc function);
MSH_CMD_EXPORT(adc, adc <device name> <option> <channel>);
#endif /* RT_USING_FINSH */
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册