2018-11-19 19:04:44 +08:00
|
|
|
/*
|
2023-04-18 10:26:23 +08:00
|
|
|
* Copyright (c) 2006-2023, RT-Thread Development Team
|
2018-11-19 19:04:44 +08:00
|
|
|
*
|
|
|
|
* SPDX-License-Identifier: Apache-2.0
|
|
|
|
*
|
|
|
|
* Change Logs:
|
|
|
|
* Date Author Notes
|
|
|
|
* 2018-05-07 aozima the first version
|
|
|
|
* 2018-11-16 Ernest Chen add finsh command and update adc function
|
2022-05-21 13:46:52 +08:00
|
|
|
* 2022-05-11 Stanley Lwin add finsh voltage conversion command
|
2018-11-19 19:04:44 +08:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <rtthread.h>
|
|
|
|
#include <rtdevice.h>
|
|
|
|
|
|
|
|
#include <string.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
|
2019-04-12 10:18:57 +08:00
|
|
|
#define DBG_TAG "adc"
|
|
|
|
#define DBG_LVL DBG_INFO
|
2018-11-19 19:04:44 +08:00
|
|
|
#include <rtdbg.h>
|
|
|
|
|
2023-02-06 07:35:33 +08:00
|
|
|
static rt_ssize_t _adc_read(rt_device_t dev, rt_off_t pos, void *buffer, rt_size_t size)
|
2018-11-19 19:04:44 +08:00
|
|
|
{
|
2024-06-26 20:26:52 +08:00
|
|
|
rt_err_t result;
|
|
|
|
rt_size_t i;
|
|
|
|
struct rt_adc_device *adc = (struct rt_adc_device *)dev;
|
|
|
|
rt_uint32_t *value = (rt_uint32_t *)buffer;
|
2018-11-19 19:04:44 +08:00
|
|
|
|
2024-06-26 20:26:52 +08:00
|
|
|
for (i = 0; i < size; i++)
|
2018-11-19 19:04:44 +08:00
|
|
|
{
|
2024-06-26 20:26:52 +08:00
|
|
|
result = adc->ops->convert(adc, pos, value);
|
2018-11-19 19:04:44 +08:00
|
|
|
if (result != RT_EOK)
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
value++;
|
|
|
|
}
|
|
|
|
|
|
|
|
return i;
|
|
|
|
}
|
|
|
|
|
|
|
|
static rt_err_t _adc_control(rt_device_t dev, int cmd, void *args)
|
|
|
|
{
|
|
|
|
rt_adc_device_t adc = (struct rt_adc_device *)dev;
|
2024-06-26 20:26:52 +08:00
|
|
|
rt_err_t result;
|
|
|
|
|
2018-11-19 19:04:44 +08:00
|
|
|
|
2022-04-22 22:26:41 +08:00
|
|
|
if (cmd == RT_ADC_CMD_ENABLE && adc->ops->enabled)
|
2018-11-19 19:04:44 +08:00
|
|
|
{
|
|
|
|
result = adc->ops->enabled(adc, (rt_uint32_t)args, RT_TRUE);
|
|
|
|
}
|
2022-04-22 22:26:41 +08:00
|
|
|
else if (cmd == RT_ADC_CMD_DISABLE && adc->ops->enabled)
|
2018-11-19 19:04:44 +08:00
|
|
|
{
|
|
|
|
result = adc->ops->enabled(adc, (rt_uint32_t)args, RT_FALSE);
|
|
|
|
}
|
2022-05-29 10:22:33 +08:00
|
|
|
else if (cmd == RT_ADC_CMD_GET_RESOLUTION && adc->ops->get_resolution && args)
|
2022-04-22 22:26:41 +08:00
|
|
|
{
|
|
|
|
rt_uint8_t resolution = adc->ops->get_resolution(adc);
|
2024-06-26 20:26:52 +08:00
|
|
|
if (resolution != 0)
|
2022-04-22 22:26:41 +08:00
|
|
|
{
|
2024-06-26 20:26:52 +08:00
|
|
|
*((rt_uint8_t *)args) = resolution;
|
2022-04-23 06:59:27 +08:00
|
|
|
LOG_D("resolution: %d bits", resolution);
|
2022-04-22 22:26:41 +08:00
|
|
|
result = RT_EOK;
|
|
|
|
}
|
|
|
|
}
|
2022-05-29 10:22:33 +08:00
|
|
|
else if (cmd == RT_ADC_CMD_GET_VREF && adc->ops->get_vref && args)
|
|
|
|
{
|
|
|
|
rt_int16_t value = adc->ops->get_vref(adc);
|
2024-06-26 20:26:52 +08:00
|
|
|
if (value != 0)
|
2022-05-29 10:22:33 +08:00
|
|
|
{
|
2024-06-26 20:26:52 +08:00
|
|
|
*((rt_int16_t *)args) = value;
|
|
|
|
result = RT_EOK;
|
2022-05-29 10:22:33 +08:00
|
|
|
}
|
|
|
|
}
|
2018-11-19 19:04:44 +08:00
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2019-03-23 09:47:45 +08:00
|
|
|
#ifdef RT_USING_DEVICE_OPS
|
|
|
|
const static struct rt_device_ops adc_ops =
|
2024-06-26 20:26:52 +08:00
|
|
|
{
|
|
|
|
RT_NULL,
|
|
|
|
RT_NULL,
|
|
|
|
RT_NULL,
|
|
|
|
_adc_read,
|
|
|
|
RT_NULL,
|
|
|
|
_adc_control,
|
2019-03-23 09:47:45 +08:00
|
|
|
};
|
|
|
|
#endif
|
|
|
|
|
2018-11-19 19:04:44 +08:00
|
|
|
rt_err_t rt_hw_adc_register(rt_adc_device_t device, const char *name, const struct rt_adc_ops *ops, const void *user_data)
|
|
|
|
{
|
|
|
|
RT_ASSERT(ops != RT_NULL && ops->convert != RT_NULL);
|
2024-06-26 20:26:52 +08:00
|
|
|
rt_err_t result;
|
2018-11-19 19:04:44 +08:00
|
|
|
|
2024-06-26 20:26:52 +08:00
|
|
|
device->parent.type = RT_Device_Class_ADC;
|
2019-03-23 09:47:45 +08:00
|
|
|
device->parent.rx_indicate = RT_NULL;
|
|
|
|
device->parent.tx_complete = RT_NULL;
|
|
|
|
|
|
|
|
#ifdef RT_USING_DEVICE_OPS
|
2024-06-26 20:26:52 +08:00
|
|
|
device->parent.ops = &adc_ops;
|
2019-03-23 09:47:45 +08:00
|
|
|
#else
|
2024-06-26 20:26:52 +08:00
|
|
|
device->parent.init = RT_NULL;
|
|
|
|
device->parent.open = RT_NULL;
|
|
|
|
device->parent.close = RT_NULL;
|
|
|
|
device->parent.read = _adc_read;
|
|
|
|
device->parent.write = RT_NULL;
|
|
|
|
device->parent.control = _adc_control;
|
2019-03-23 09:47:45 +08:00
|
|
|
#endif
|
2024-06-26 20:26:52 +08:00
|
|
|
device->ops = ops;
|
2018-11-19 19:04:44 +08:00
|
|
|
device->parent.user_data = (void *)user_data;
|
|
|
|
|
|
|
|
result = rt_device_register(&device->parent, name, RT_DEVICE_FLAG_RDWR);
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2023-04-09 10:24:19 +08:00
|
|
|
rt_uint32_t rt_adc_read(rt_adc_device_t dev, rt_int8_t channel)
|
2018-11-19 19:04:44 +08:00
|
|
|
{
|
|
|
|
RT_ASSERT(dev);
|
2024-06-26 20:26:52 +08:00
|
|
|
rt_uint32_t value;
|
|
|
|
rt_err_t result;
|
2018-11-19 19:04:44 +08:00
|
|
|
|
2024-06-26 20:26:52 +08:00
|
|
|
result = dev->ops->convert(dev, channel, &value);
|
|
|
|
if (result != RT_EOK)
|
|
|
|
return 0;
|
2018-11-19 19:04:44 +08:00
|
|
|
|
|
|
|
return value;
|
|
|
|
}
|
|
|
|
|
2023-04-09 10:24:19 +08:00
|
|
|
rt_err_t rt_adc_enable(rt_adc_device_t dev, rt_int8_t channel)
|
2018-11-19 19:04:44 +08:00
|
|
|
{
|
|
|
|
RT_ASSERT(dev);
|
2024-06-26 20:26:52 +08:00
|
|
|
rt_err_t result;
|
2022-04-22 22:26:41 +08:00
|
|
|
|
2018-11-19 19:04:44 +08:00
|
|
|
if (dev->ops->enabled != RT_NULL)
|
|
|
|
{
|
|
|
|
result = dev->ops->enabled(dev, channel, RT_TRUE);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
result = -RT_ENOSYS;
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2023-04-09 10:24:19 +08:00
|
|
|
rt_err_t rt_adc_disable(rt_adc_device_t dev, rt_int8_t channel)
|
2018-11-19 19:04:44 +08:00
|
|
|
{
|
|
|
|
RT_ASSERT(dev);
|
2024-06-26 20:26:52 +08:00
|
|
|
rt_err_t result;
|
2022-04-22 22:26:41 +08:00
|
|
|
|
2018-11-19 19:04:44 +08:00
|
|
|
if (dev->ops->enabled != RT_NULL)
|
|
|
|
{
|
|
|
|
result = dev->ops->enabled(dev, channel, RT_FALSE);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
result = -RT_ENOSYS;
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2023-04-09 10:24:19 +08:00
|
|
|
rt_int16_t rt_adc_voltage(rt_adc_device_t dev, rt_int8_t channel)
|
2022-05-19 10:36:50 +08:00
|
|
|
{
|
|
|
|
RT_ASSERT(dev);
|
|
|
|
|
2024-06-26 20:26:52 +08:00
|
|
|
rt_uint32_t value;
|
|
|
|
rt_int16_t vref, voltage;
|
|
|
|
rt_uint8_t resolution;
|
|
|
|
rt_err_t result;
|
2022-05-19 10:36:50 +08:00
|
|
|
|
2024-06-26 20:26:52 +08:00
|
|
|
/*get the resolution in bits*/
|
|
|
|
resolution = dev->ops->get_resolution(dev);
|
2022-05-29 10:22:33 +08:00
|
|
|
/*get the reference voltage*/
|
2024-06-26 20:26:52 +08:00
|
|
|
vref = dev->ops->get_vref(dev);
|
|
|
|
if (vref == 0)
|
2022-05-29 10:22:33 +08:00
|
|
|
goto _voltage_exit;
|
|
|
|
|
|
|
|
/*read the value and convert to voltage*/
|
2024-06-26 20:26:52 +08:00
|
|
|
result = dev->ops->enabled(dev, channel, RT_TRUE);
|
|
|
|
if (result != RT_EOK)
|
|
|
|
goto _voltage_exit;
|
|
|
|
result = dev->ops->convert(dev, channel, &value);
|
|
|
|
if (result != RT_EOK)
|
|
|
|
goto _voltage_exit;
|
|
|
|
result = dev->ops->enabled(dev, channel, RT_FALSE);
|
|
|
|
if (result != RT_EOK)
|
|
|
|
goto _voltage_exit;
|
2024-06-01 13:27:12 +08:00
|
|
|
voltage = value * vref / ((1 << resolution) - 1);
|
2022-05-29 10:22:33 +08:00
|
|
|
|
|
|
|
_voltage_exit:
|
2022-05-19 10:36:50 +08:00
|
|
|
return voltage;
|
|
|
|
}
|
2022-05-29 10:22:33 +08:00
|
|
|
|
2021-08-23 17:30:05 +08:00
|
|
|
#ifdef RT_USING_FINSH
|
2018-11-19 19:04:44 +08:00
|
|
|
|
|
|
|
static int adc(int argc, char **argv)
|
|
|
|
{
|
2024-06-26 20:26:52 +08:00
|
|
|
int value = 0;
|
|
|
|
rt_int16_t voltage = 0;
|
|
|
|
rt_err_t result = -RT_ERROR;
|
2018-11-19 19:04:44 +08:00
|
|
|
static rt_adc_device_t adc_device = RT_NULL;
|
2024-06-26 20:26:52 +08:00
|
|
|
char *result_str;
|
2018-11-19 19:04:44 +08:00
|
|
|
|
|
|
|
if (argc > 1)
|
|
|
|
{
|
|
|
|
if (!strcmp(argv[1], "probe"))
|
|
|
|
{
|
|
|
|
if (argc == 3)
|
|
|
|
{
|
|
|
|
adc_device = (rt_adc_device_t)rt_device_find(argv[2]);
|
|
|
|
result_str = (adc_device == RT_NULL) ? "failure" : "success";
|
|
|
|
rt_kprintf("probe %s %s \n", argv[2], result_str);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2022-05-19 10:36:50 +08:00
|
|
|
rt_kprintf("adc probe <device name> - probe adc by name\n");
|
2018-11-19 19:04:44 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (adc_device == RT_NULL)
|
|
|
|
{
|
2022-05-19 10:36:50 +08:00
|
|
|
rt_kprintf("Please using 'adc probe <device name>' first\n");
|
2018-11-19 19:04:44 +08:00
|
|
|
return -RT_ERROR;
|
|
|
|
}
|
|
|
|
if (!strcmp(argv[1], "enable"))
|
|
|
|
{
|
|
|
|
if (argc == 3)
|
|
|
|
{
|
2024-06-26 20:26:52 +08:00
|
|
|
result = rt_adc_enable(adc_device, atoi(argv[2]));
|
2018-11-19 19:04:44 +08:00
|
|
|
result_str = (result == RT_EOK) ? "success" : "failure";
|
2022-10-15 12:56:00 +08:00
|
|
|
rt_kprintf("%s channel %d enables %s \n", adc_device->parent.parent.name, (rt_base_t)atoi(argv[2]), result_str);
|
2018-11-19 19:04:44 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
rt_kprintf("adc enable <channel> - enable adc channel\n");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (!strcmp(argv[1], "read"))
|
|
|
|
{
|
|
|
|
if (argc == 3)
|
|
|
|
{
|
|
|
|
value = rt_adc_read(adc_device, atoi(argv[2]));
|
2022-10-15 12:56:00 +08:00
|
|
|
rt_kprintf("%s channel %d read value is 0x%08X \n", adc_device->parent.parent.name, (rt_base_t)atoi(argv[2]), value);
|
2018-11-19 19:04:44 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
rt_kprintf("adc read <channel> - read adc value on the channel\n");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (!strcmp(argv[1], "disable"))
|
|
|
|
{
|
|
|
|
if (argc == 3)
|
|
|
|
{
|
2024-06-26 20:26:52 +08:00
|
|
|
result = rt_adc_disable(adc_device, atoi(argv[2]));
|
2018-11-19 19:04:44 +08:00
|
|
|
result_str = (result == RT_EOK) ? "success" : "failure";
|
2022-10-15 12:56:00 +08:00
|
|
|
rt_kprintf("%s channel %d disable %s \n", adc_device->parent.parent.name, (rt_base_t)atoi(argv[2]), result_str);
|
2018-11-19 19:04:44 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
rt_kprintf("adc disable <channel> - disable adc channel\n");
|
|
|
|
}
|
|
|
|
}
|
2022-05-19 10:36:50 +08:00
|
|
|
else if (!strcmp(argv[1], "voltage"))
|
|
|
|
{
|
2024-06-26 20:26:52 +08:00
|
|
|
if (argc == 3)
|
2022-05-19 10:36:50 +08:00
|
|
|
{
|
|
|
|
voltage = rt_adc_voltage(adc_device, atoi(argv[2]));
|
2022-10-15 12:56:00 +08:00
|
|
|
rt_kprintf("%s channel %d voltage is %d.%03dV \n", adc_device->parent.parent.name, (rt_base_t)atoi(argv[2]), voltage / 1000, voltage % 1000);
|
2022-05-19 10:36:50 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
rt_kprintf("adc convert voltage <channel> \n");
|
|
|
|
}
|
|
|
|
}
|
2018-11-19 19:04:44 +08:00
|
|
|
else
|
|
|
|
{
|
|
|
|
rt_kprintf("Unknown command. Please enter 'adc' for help\n");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
rt_kprintf("Usage: \n");
|
2022-05-19 10:36:50 +08:00
|
|
|
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");
|
2024-06-01 13:27:12 +08:00
|
|
|
rt_kprintf("adc voltage <channel> - read voltage on the channel\n");
|
2022-05-19 10:36:50 +08:00
|
|
|
|
2018-11-19 19:04:44 +08:00
|
|
|
result = -RT_ERROR;
|
|
|
|
}
|
|
|
|
return RT_EOK;
|
|
|
|
}
|
2022-05-21 13:46:52 +08:00
|
|
|
MSH_CMD_EXPORT(adc, adc [option]);
|
2018-11-19 19:04:44 +08:00
|
|
|
|
2021-08-23 17:30:05 +08:00
|
|
|
#endif /* RT_USING_FINSH */
|