summaryrefslogtreecommitdiff
path: root/components/drivers/misc
diff options
context:
space:
mode:
authorcliff-cmc <[email protected]>2021-02-26 11:09:27 +0800
committerCliff Chen <[email protected]>2021-02-26 11:45:19 +0800
commiteea0c3d74a40f73235da45c6f77e8b411ac94cf2 (patch)
treea6906f1aed7118ab53dbe18f2ee4727a754583af /components/drivers/misc
parent1a9ecf27f31277750d92c921b62503f75275e122 (diff)
parentfae0fa8eea4c0e797940bf1eddc8ee6c1711734a (diff)
Add memtrace command for memheap.c
Diffstat (limited to 'components/drivers/misc')
-rw-r--r--components/drivers/misc/SConscript3
-rw-r--r--components/drivers/misc/dac.c231
-rw-r--r--components/drivers/misc/pin.c22
-rw-r--r--components/drivers/misc/rt_drv_pwm.c74
4 files changed, 321 insertions, 9 deletions
diff --git a/components/drivers/misc/SConscript b/components/drivers/misc/SConscript
index 8a9fdb5eb9..72058eeeca 100644
--- a/components/drivers/misc/SConscript
+++ b/components/drivers/misc/SConscript
@@ -11,6 +11,9 @@ if GetDepend(['RT_USING_PIN']):
if GetDepend(['RT_USING_ADC']):
src = src + ['adc.c']
+if GetDepend(['RT_USING_DAC']):
+ src = src + ['dac.c']
+
if GetDepend(['RT_USING_PWM']):
src = src + ['rt_drv_pwm.c']
diff --git a/components/drivers/misc/dac.c b/components/drivers/misc/dac.c
new file mode 100644
index 0000000000..336c78d018
--- /dev/null
+++ b/components/drivers/misc/dac.c
@@ -0,0 +1,231 @@
+/*
+ * Copyright (c) 2006-2018, RT-Thread Development Team
+ *
+ * SPDX-License-Identifier: Apache-2.0
+ *
+ * Change Logs:
+ * Date Author Notes
+ * 2020-06-19 thread-liu the first version
+ */
+
+#include <rtthread.h>
+#include <rtdevice.h>
+
+#include <string.h>
+#include <stdlib.h>
+
+#define DBG_TAG "dac"
+#define DBG_LVL DBG_INFO
+#include <rtdbg.h>
+
+static rt_size_t _dac_write(rt_device_t dev, rt_off_t pos, const void *buffer, rt_size_t size)
+{
+ rt_err_t result = RT_EOK;
+ rt_size_t i;
+ struct rt_dac_device *dac = (struct rt_dac_device *)dev;
+ rt_uint32_t *value = (rt_uint32_t *)buffer;
+
+ for (i = 0; i < size; i += sizeof(int))
+ {
+ result = dac->ops->convert(dac, pos + i, value);
+ if (result != RT_EOK)
+ {
+ return 0;
+ }
+ value++;
+ }
+
+ return i;
+}
+
+static rt_err_t _dac_control(rt_device_t dev, int cmd, void *args)
+{
+ rt_err_t result = RT_EOK;
+ rt_dac_device_t dac = (struct rt_dac_device *)dev;
+
+ if (dac->ops->enabled == RT_NULL)
+ {
+ return -RT_ENOSYS;
+ }
+ if (cmd == RT_DAC_CMD_ENABLE)
+ {
+ result = dac->ops->enabled(dac, (rt_uint32_t)args);
+ }
+ else if (cmd == RT_DAC_CMD_DISABLE)
+ {
+ result = dac->ops->disabled(dac, (rt_uint32_t)args);
+ }
+
+ return result;
+}
+
+#ifdef RT_USING_DEVICE_OPS
+const static struct rt_device_ops dac_ops =
+{
+ RT_NULL,
+ RT_NULL,
+ RT_NULL,
+ RT_NULL,
+ _dac_write,
+ _dac_control,
+};
+#endif
+
+rt_err_t rt_hw_dac_register(rt_dac_device_t device, const char *name, const struct rt_dac_ops *ops, const void *user_data)
+{
+ rt_err_t result = RT_EOK;
+ RT_ASSERT(ops != RT_NULL && ops->convert != RT_NULL);
+
+ device->parent.type = RT_Device_Class_Miscellaneous;
+ device->parent.rx_indicate = RT_NULL;
+ device->parent.tx_complete = RT_NULL;
+
+#ifdef RT_USING_DEVICE_OPS
+ device->parent.ops = &dac_ops;
+#else
+ device->parent.init = RT_NULL;
+ device->parent.open = RT_NULL;
+ device->parent.close = RT_NULL;
+ device->parent.read = RT_NULL;
+ device->parent.write = _dac_write;
+ device->parent.control = _dac_control;
+#endif
+ device->ops = ops;
+ device->parent.user_data = (void *)user_data;
+
+ result = rt_device_register(&device->parent, name, RT_DEVICE_FLAG_RDWR);
+
+ return result;
+}
+
+rt_uint32_t rt_dac_write(rt_dac_device_t dev, rt_uint32_t channel, rt_uint32_t value)
+{
+ RT_ASSERT(dev);
+
+ dev->ops->convert(dev, channel, &value);
+
+ return RT_EOK;
+}
+
+rt_err_t rt_dac_enable(rt_dac_device_t dev, rt_uint32_t channel)
+{
+ rt_err_t result = RT_EOK;
+
+ RT_ASSERT(dev);
+ if (dev->ops->enabled != RT_NULL)
+ {
+ result = dev->ops->enabled(dev, channel);
+ }
+ else
+ {
+ result = -RT_ENOSYS;
+ }
+
+ return result;
+}
+
+rt_err_t rt_dac_disabled(rt_dac_device_t dev, rt_uint32_t channel)
+{
+ rt_err_t result = RT_EOK;
+
+ RT_ASSERT(dev);
+ if (dev->ops->disabled != RT_NULL)
+ {
+ result = dev->ops->disabled(dev, channel);
+ }
+ else
+ {
+ result = -RT_ENOSYS;
+ }
+
+ return result;
+}
+
+#ifdef FINSH_USING_MSH
+
+static int dac(int argc, char **argv)
+{
+ int result = RT_EOK;
+ static rt_dac_device_t dac_device = RT_NULL;
+ char *result_str;
+
+ if (argc > 1)
+ {
+ if (!strcmp(argv[1], "probe"))
+ {
+ if (argc == 3)
+ {
+ dac_device = (rt_dac_device_t)rt_device_find(argv[2]);
+ result_str = (dac_device == RT_NULL) ? "failure" : "success";
+ rt_kprintf("probe %s %s \n", argv[2], result_str);
+ }
+ else
+ {
+ rt_kprintf("dac probe <dac_name> - probe dac by name\n");
+ }
+ }
+ else
+ {
+ if (dac_device == RT_NULL)
+ {
+ rt_kprintf("Please using 'dac probe <dac_name>' first\n");
+ return -RT_ERROR;
+ }
+ if (!strcmp(argv[1], "enable"))
+ {
+ if (argc == 3)
+ {
+ result = rt_dac_enable(dac_device, atoi(argv[2]));
+ result_str = (result == RT_EOK) ? "success" : "failure";
+ rt_kprintf("%s channel %d enables %s \n", dac_device->parent.parent.name, atoi(argv[2]), result_str);
+ }
+ else
+ {
+ rt_kprintf("dac enable <channel> - enable dac channel\n");
+ }
+ }
+ else if (!strcmp(argv[1], "write"))
+ {
+ if (argc == 4)
+ {
+ rt_dac_write(dac_device, atoi(argv[2]), atoi(argv[3]));
+ rt_kprintf("%s channel %d write value is %d \n", dac_device->parent.parent.name, atoi(argv[2]), atoi(argv[3]));
+ }
+ else
+ {
+ rt_kprintf("dac write <channel> <value> - write dac value on the channel\n");
+ }
+ }
+ else if (!strcmp(argv[1], "disable"))
+ {
+ if (argc == 3)
+ {
+ result = rt_dac_disabled(dac_device, atoi(argv[2]));
+ result_str = (result == RT_EOK) ? "success" : "failure";
+ rt_kprintf("%s channel %d disable %s \n", dac_device->parent.parent.name, atoi(argv[2]), result_str);
+ }
+ else
+ {
+ rt_kprintf("dac disable <channel> - disable dac channel\n");
+ }
+ }
+ else
+ {
+ rt_kprintf("Unknown command. Please enter 'dac' for help\n");
+ }
+ }
+ }
+ else
+ {
+ rt_kprintf("Usage: \n");
+ rt_kprintf("dac probe <dac_name> - probe dac by name\n");
+ rt_kprintf("dac write <channel> <value> - write dac value on the channel\n");
+ rt_kprintf("dac disable <channel> - disable dac channel\n");
+ rt_kprintf("dac enable <channel> - enable dac channel\n");
+ result = -RT_ERROR;
+ }
+ return RT_EOK;
+}
+MSH_CMD_EXPORT(dac, dac function);
+
+#endif /* FINSH_USING_MSH */
diff --git a/components/drivers/misc/pin.c b/components/drivers/misc/pin.c
index aca58ee21b..6f27fe516d 100644
--- a/components/drivers/misc/pin.c
+++ b/components/drivers/misc/pin.c
@@ -6,6 +6,7 @@
* Change Logs:
* Date Author Notes
* 2015-01-20 Bernard the first version
+ * 2021-02-06 Meco Man fix RT_ENOSYS code in negative
*/
#include <drivers/pin.h>
@@ -108,7 +109,7 @@ rt_err_t rt_pin_attach_irq(rt_int32_t pin, rt_uint32_t mode,
{
return _hw_pin.ops->pin_attach_irq(&_hw_pin.parent, pin, mode, hdr, args);
}
- return RT_ENOSYS;
+ return -RT_ENOSYS;
}
rt_err_t rt_pin_detach_irq(rt_int32_t pin)
{
@@ -117,7 +118,7 @@ rt_err_t rt_pin_detach_irq(rt_int32_t pin)
{
return _hw_pin.ops->pin_detach_irq(&_hw_pin.parent, pin);
}
- return RT_ENOSYS;
+ return -RT_ENOSYS;
}
rt_err_t rt_pin_irq_enable(rt_base_t pin, rt_uint32_t enabled)
@@ -127,7 +128,7 @@ rt_err_t rt_pin_irq_enable(rt_base_t pin, rt_uint32_t enabled)
{
return _hw_pin.ops->pin_irq_enable(&_hw_pin.parent, pin, enabled);
}
- return RT_ENOSYS;
+ return -RT_ENOSYS;
}
/* RT-Thread Hardware PIN APIs */
@@ -151,3 +152,18 @@ int rt_pin_read(rt_base_t pin)
return _hw_pin.ops->pin_read(&_hw_pin.parent, pin);
}
FINSH_FUNCTION_EXPORT_ALIAS(rt_pin_read, pinRead, read status from hardware pin);
+
+rt_base_t rt_pin_get(const char *name)
+{
+ RT_ASSERT(_hw_pin.ops != RT_NULL);
+ RT_ASSERT(name[0] == 'P');
+
+ if(_hw_pin.ops->pin_get == RT_NULL)
+ {
+ return -RT_ENOSYS;
+ }
+
+ return _hw_pin.ops->pin_get(name);
+}
+FINSH_FUNCTION_EXPORT_ALIAS(rt_pin_get, pinGet, get pin number from hardware pin);
+
diff --git a/components/drivers/misc/rt_drv_pwm.c b/components/drivers/misc/rt_drv_pwm.c
index 97d38d51b3..572580c45c 100644
--- a/components/drivers/misc/rt_drv_pwm.c
+++ b/components/drivers/misc/rt_drv_pwm.c
@@ -136,7 +136,8 @@ rt_err_t rt_pwm_enable(struct rt_device_pwm *device, int channel)
return -RT_EIO;
}
- configuration.channel = channel;
+ configuration.channel = (channel > 0) ? (channel) : (-channel); /* Make it is positive num forever */
+ configuration.complementary = (channel > 0) ? (RT_FALSE) : (RT_TRUE); /* If nagetive, it's complementary */
result = rt_device_control(&device->parent, PWM_CMD_ENABLE, &configuration);
return result;
@@ -152,7 +153,8 @@ rt_err_t rt_pwm_disable(struct rt_device_pwm *device, int channel)
return -RT_EIO;
}
- configuration.channel = channel;
+ configuration.channel = (channel > 0) ? (channel) : (-channel); /* Make it is positive num forever */
+ configuration.complementary = (channel > 0) ? (RT_FALSE) : (RT_TRUE); /* If nagetive, it's complementary */
result = rt_device_control(&device->parent, PWM_CMD_DISABLE, &configuration);
return result;
@@ -176,6 +178,20 @@ rt_err_t rt_pwm_set(struct rt_device_pwm *device, int channel, rt_uint32_t perio
return result;
}
+rt_err_t rt_pwm_get(struct rt_device_pwm *device, struct rt_pwm_configuration *cfg)
+{
+ rt_err_t result = RT_EOK;
+
+ if (!device)
+ {
+ return -RT_EIO;
+ }
+
+ result = rt_device_control(&device->parent, PWM_CMD_GET, cfg);
+
+ return result;
+}
+
#ifdef RT_USING_FINSH
#include <finsh.h>
@@ -191,6 +207,7 @@ static int pwm_enable(int argc, char **argv)
if (argc != 3)
{
rt_kprintf("Usage: pwm_enable pwm1 1\n");
+ rt_kprintf(" pwm_enable <pwm_dev> <channel/-channel>\n");
result = -RT_ERROR;
goto _exit;
}
@@ -202,12 +219,13 @@ static int pwm_enable(int argc, char **argv)
goto _exit;
}
+ /* If channel is complementary(1), make the channel number to nagetive */
result = rt_pwm_enable(device, atoi(argv[2]));
_exit:
return result;
}
-MSH_CMD_EXPORT(pwm_enable, pwm_enable pwm1 1);
+MSH_CMD_EXPORT(pwm_enable, pwm_enable <pwm_dev> <channel/-channel>);
static int pwm_disable(int argc, char **argv)
{
@@ -216,7 +234,8 @@ static int pwm_disable(int argc, char **argv)
if (argc != 3)
{
- rt_kprintf("Usage: pwm_enable pwm1 1\n");
+ rt_kprintf("Usage: pwm_disable pwm1 1\n");
+ rt_kprintf(" pwm_disable <pwm_dev> <channel/-channel> \n");
result = -RT_ERROR;
goto _exit;
}
@@ -228,12 +247,13 @@ static int pwm_disable(int argc, char **argv)
goto _exit;
}
+ /* If channel is complementary(1), make the channel number to nagetive */
result = rt_pwm_disable(device, atoi(argv[2]));
_exit:
return result;
}
-MSH_CMD_EXPORT(pwm_disable, pwm_disable pwm1 1);
+MSH_CMD_EXPORT(pwm_disable, pwm_disable <pwm_dev> <channel/-channel>);
static int pwm_set(int argc, char **argv)
{
@@ -243,6 +263,7 @@ static int pwm_set(int argc, char **argv)
if (argc != 5)
{
rt_kprintf("Usage: pwm_set pwm1 1 100 50\n");
+ rt_kprintf("Usage: pwm_set <pwm_dev> <channel> <period> <pulse>\n");
result = -RT_ERROR;
goto _exit;
}
@@ -259,7 +280,48 @@ static int pwm_set(int argc, char **argv)
_exit:
return result;
}
-MSH_CMD_EXPORT(pwm_set, pwm_set pwm1 1 100 50);
+MSH_CMD_EXPORT(pwm_set, pwm_set <pwm_dev> <channel> <period> <pulse>);
+
+
+static int pwm_get(int argc, char **argv)
+{
+ int result = 0;
+ struct rt_device_pwm *device = RT_NULL;
+ struct rt_pwm_configuration cfg = {0};
+
+ if (argc != 3)
+ {
+ rt_kprintf("Usage: pwm_get pwm1 1\n");
+ rt_kprintf(" pwm_get <pwm_dev> <channel>\n");
+ result = -RT_ERROR;
+ goto _exit;
+ }
+
+ device = (struct rt_device_pwm *)rt_device_find(argv[1]);
+ if (!device)
+ {
+ result = -RT_EIO;
+ goto _exit;
+ }
+
+ cfg.channel = atoi(argv[2]);
+ result = rt_pwm_get(device, &cfg);
+ if (result != RT_EOK)
+ {
+ rt_kprintf("Get info of device: [%s] error.\n", argv[1]);
+ }
+ else
+ {
+ rt_kprintf("Get info of device: [%s]:\n", argv[1]);
+ rt_kprintf("period : %d\n", cfg.period);
+ rt_kprintf("pulse : %d\n", cfg.pulse);
+ rt_kprintf("Duty cycle : %d%%\n", (int)(((double)(cfg.pulse)/(cfg.period)) * 100));
+ }
+
+_exit:
+ return result;
+}
+MSH_CMD_EXPORT(pwm_get, pwm_get <pwm_dev> <channel>);
#endif /* FINSH_USING_MSH */
#endif /* RT_USING_FINSH */