sdio+fatfs创建

This commit is contained in:
sc committed 2025-10-19 00:26:13 +08:00
commit f762d5d7f8
2473 files changed
+1192014

No files matched your search

@@ -0,0 +1,29 @@
from building import *
cwd = GetCurrentDir()
src = []
CPPPATH = [cwd + '/../include']
group = []
if GetDepend(['RT_USING_PIN']):
src = src + ['pin.c']
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']
if GetDepend(['RT_USING_PULSE_ENCODER']):
src = src + ['pulse_encoder.c']
if GetDepend(['RT_USING_INPUT_CAPTURE']):
src = src + ['rt_inputcapture.c']
if len(src):
group = DefineGroup('DeviceDrivers', src, depend = [''], CPPPATH = CPPPATH)
Return('group')
+235
View File
@@ -0,0 +1,235 @@
/*
* Copyright (c) 2006-2021, RT-Thread Development Team
*
* 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
*/
#include <rtthread.h>
#include <rtdevice.h>
#include <string.h>
#include <stdlib.h>
#define DBG_TAG "adc"
#define DBG_LVL DBG_INFO
#include <rtdbg.h>
static rt_size_t _adc_read(rt_device_t dev, rt_off_t pos, void *buffer, rt_size_t size)
{
rt_err_t result = RT_EOK;
rt_size_t i;
struct rt_adc_device *adc = (struct rt_adc_device *)dev;
rt_uint32_t *value = (rt_uint32_t *)buffer;
for (i = 0; i < size; i += sizeof(int))
{
result = adc->ops->convert(adc, pos + i, value);
if (result != RT_EOK)
{
return 0;
}
value++;
}
return i;
}
static rt_err_t _adc_control(rt_device_t dev, int cmd, void *args)
{
rt_err_t result = RT_EOK;
rt_adc_device_t adc = (struct rt_adc_device *)dev;
if (adc->ops->enabled == RT_NULL)
{
return -RT_ENOSYS;
}
if (cmd == RT_ADC_CMD_ENABLE)
{
result = adc->ops->enabled(adc, (rt_uint32_t)args, RT_TRUE);
}
else if (cmd == RT_ADC_CMD_DISABLE)
{
result = adc->ops->enabled(adc, (rt_uint32_t)args, RT_FALSE);
}
return result;
}
#ifdef RT_USING_DEVICE_OPS
const static struct rt_device_ops adc_ops =
{
RT_NULL,
RT_NULL,
RT_NULL,
_adc_read,
RT_NULL,
_adc_control,
};
#endif
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_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 = &adc_ops;
#else
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;
#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_adc_read(rt_adc_device_t dev, rt_uint32_t channel)
{
rt_uint32_t value;
RT_ASSERT(dev);
dev->ops->convert(dev, channel, &value);
return value;
}
rt_err_t rt_adc_enable(rt_adc_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, RT_TRUE);
}
else
{
result = -RT_ENOSYS;
}
return result;
}
rt_err_t rt_adc_disable(rt_adc_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, RT_FALSE);
}
else
{
result = -RT_ENOSYS;
}
return result;
}
#ifdef RT_USING_FINSH
static int adc(int argc, char **argv)
{
int value = 0;
int result = RT_EOK;
static rt_adc_device_t adc_device = RT_NULL;
char *result_str;
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
{
rt_kprintf("adc probe <adc_name> - probe adc by name\n");
}
}
else
{
if (adc_device == RT_NULL)
{
rt_kprintf("Please using 'adc probe <adc_name>' first\n");
return -RT_ERROR;
}
if (!strcmp(argv[1], "enable"))
{
if (argc == 3)
{
result = rt_adc_enable(adc_device, atoi(argv[2]));
result_str = (result == RT_EOK) ? "success" : "failure";
rt_kprintf("%s channel %d enables %s \n", adc_device->parent.parent.name, atoi(argv[2]), result_str);
}
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]));
rt_kprintf("%s channel %d read value is 0x%08X \n", adc_device->parent.parent.name, atoi(argv[2]), value);
}
else
{
rt_kprintf("adc read <channel> - read adc value on the channel\n");
}
}
else if (!strcmp(argv[1], "disable"))
{
if (argc == 3)
{
result = rt_adc_disable(adc_device, atoi(argv[2]));
result_str = (result == RT_EOK) ? "success" : "failure";
rt_kprintf("%s channel %d disable %s \n", adc_device->parent.parent.name, atoi(argv[2]), result_str);
}
else
{
rt_kprintf("adc disable <channel> - disable adc channel\n");
}
}
else
{
rt_kprintf("Unknown command. Please enter 'adc' for help\n");
}
}
}
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");
result = -RT_ERROR;
}
return RT_EOK;
}
MSH_CMD_EXPORT(adc, adc function);
#endif /* RT_USING_FINSH */
+231
View File
@@ -0,0 +1,231 @@
/*
* Copyright (c) 2006-2021, 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_disable(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 RT_USING_FINSH
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_disable(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 /* RT_USING_FINSH */
+169
View File
@@ -0,0 +1,169 @@
/*
* Copyright (c) 2006-2021, RT-Thread Development Team
*
* SPDX-License-Identifier: Apache-2.0
*
* 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>
#ifdef RT_USING_FINSH
#include <finsh.h>
#endif
static struct rt_device_pin _hw_pin;
static rt_size_t _pin_read(rt_device_t dev, rt_off_t pos, void *buffer, rt_size_t size)
{
struct rt_device_pin_status *status;
struct rt_device_pin *pin = (struct rt_device_pin *)dev;
/* check parameters */
RT_ASSERT(pin != RT_NULL);
status = (struct rt_device_pin_status *) buffer;
if (status == RT_NULL || size != sizeof(*status)) return 0;
status->status = pin->ops->pin_read(dev, status->pin);
return size;
}
static rt_size_t _pin_write(rt_device_t dev, rt_off_t pos, const void *buffer, rt_size_t size)
{
struct rt_device_pin_status *status;
struct rt_device_pin *pin = (struct rt_device_pin *)dev;
/* check parameters */
RT_ASSERT(pin != RT_NULL);
status = (struct rt_device_pin_status *) buffer;
if (status == RT_NULL || size != sizeof(*status)) return 0;
pin->ops->pin_write(dev, (rt_base_t)status->pin, (rt_base_t)status->status);
return size;
}
static rt_err_t _pin_control(rt_device_t dev, int cmd, void *args)
{
struct rt_device_pin_mode *mode;
struct rt_device_pin *pin = (struct rt_device_pin *)dev;
/* check parameters */
RT_ASSERT(pin != RT_NULL);
mode = (struct rt_device_pin_mode *) args;
if (mode == RT_NULL) return -RT_ERROR;
pin->ops->pin_mode(dev, (rt_base_t)mode->pin, (rt_base_t)mode->mode);
return 0;
}
#ifdef RT_USING_DEVICE_OPS
const static struct rt_device_ops pin_ops =
{
RT_NULL,
RT_NULL,
RT_NULL,
_pin_read,
_pin_write,
_pin_control
};
#endif
int rt_device_pin_register(const char *name, const struct rt_pin_ops *ops, void *user_data)
{
_hw_pin.parent.type = RT_Device_Class_Miscellaneous;
_hw_pin.parent.rx_indicate = RT_NULL;
_hw_pin.parent.tx_complete = RT_NULL;
#ifdef RT_USING_DEVICE_OPS
_hw_pin.parent.ops = &pin_ops;
#else
_hw_pin.parent.init = RT_NULL;
_hw_pin.parent.open = RT_NULL;
_hw_pin.parent.close = RT_NULL;
_hw_pin.parent.read = _pin_read;
_hw_pin.parent.write = _pin_write;
_hw_pin.parent.control = _pin_control;
#endif
_hw_pin.ops = ops;
_hw_pin.parent.user_data = user_data;
/* register a character device */
rt_device_register(&_hw_pin.parent, name, RT_DEVICE_FLAG_RDWR);
return 0;
}
rt_err_t rt_pin_attach_irq(rt_int32_t pin, rt_uint32_t mode,
void (*hdr)(void *args), void *args)
{
RT_ASSERT(_hw_pin.ops != RT_NULL);
if(_hw_pin.ops->pin_attach_irq)
{
return _hw_pin.ops->pin_attach_irq(&_hw_pin.parent, pin, mode, hdr, args);
}
return -RT_ENOSYS;
}
rt_err_t rt_pin_detach_irq(rt_int32_t pin)
{
RT_ASSERT(_hw_pin.ops != RT_NULL);
if(_hw_pin.ops->pin_detach_irq)
{
return _hw_pin.ops->pin_detach_irq(&_hw_pin.parent, pin);
}
return -RT_ENOSYS;
}
rt_err_t rt_pin_irq_enable(rt_base_t pin, rt_uint32_t enabled)
{
RT_ASSERT(_hw_pin.ops != RT_NULL);
if(_hw_pin.ops->pin_irq_enable)
{
return _hw_pin.ops->pin_irq_enable(&_hw_pin.parent, pin, enabled);
}
return -RT_ENOSYS;
}
/* RT-Thread Hardware PIN APIs */
void rt_pin_mode(rt_base_t pin, rt_base_t mode)
{
RT_ASSERT(_hw_pin.ops != RT_NULL);
_hw_pin.ops->pin_mode(&_hw_pin.parent, pin, mode);
}
FINSH_FUNCTION_EXPORT_ALIAS(rt_pin_mode, pinMode, set hardware pin mode);
void rt_pin_write(rt_base_t pin, rt_base_t value)
{
RT_ASSERT(_hw_pin.ops != RT_NULL);
_hw_pin.ops->pin_write(&_hw_pin.parent, pin, value);
}
FINSH_FUNCTION_EXPORT_ALIAS(rt_pin_write, pinWrite, write value to hardware pin);
int rt_pin_read(rt_base_t pin)
{
RT_ASSERT(_hw_pin.ops != RT_NULL);
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);
@@ -0,0 +1,136 @@
/*
* Copyright (c) 2006-2021, RT-Thread Development Team
*
* SPDX-License-Identifier: Apache-2.0
*
* Change Logs:
* Date Author Notes
* 2019-08-08 balanceTWK the first version
*/
#include <rtthread.h>
#include <rtdevice.h>
static rt_err_t rt_pulse_encoder_init(struct rt_device *dev)
{
struct rt_pulse_encoder_device *pulse_encoder;
pulse_encoder = (struct rt_pulse_encoder_device *)dev;
if (pulse_encoder->ops->init)
{
return pulse_encoder->ops->init(pulse_encoder);
}
else
{
return -RT_ENOSYS;
}
}
static rt_err_t rt_pulse_encoder_open(struct rt_device *dev, rt_uint16_t oflag)
{
struct rt_pulse_encoder_device *pulse_encoder;
pulse_encoder = (struct rt_pulse_encoder_device *)dev;
if (pulse_encoder->ops->control)
{
return pulse_encoder->ops->control(pulse_encoder, PULSE_ENCODER_CMD_ENABLE, RT_NULL);
}
else
{
return -RT_ENOSYS;
}
}
static rt_err_t rt_pulse_encoder_close(struct rt_device *dev)
{
struct rt_pulse_encoder_device *pulse_encoder;
pulse_encoder = (struct rt_pulse_encoder_device *)dev;
if (pulse_encoder->ops->control)
{
return pulse_encoder->ops->control(pulse_encoder, PULSE_ENCODER_CMD_DISABLE, RT_NULL);
}
else
{
return -RT_ENOSYS;
}
}
static rt_size_t rt_pulse_encoder_read(struct rt_device *dev, rt_off_t pos, void *buffer, rt_size_t size)
{
struct rt_pulse_encoder_device *pulse_encoder;
pulse_encoder = (struct rt_pulse_encoder_device *)dev;
if (pulse_encoder->ops->get_count)
{
*(rt_int32_t *)buffer = pulse_encoder->ops->get_count(pulse_encoder);
}
return 1;
}
static rt_err_t rt_pulse_encoder_control(struct rt_device *dev, int cmd, void *args)
{
rt_err_t result;
struct rt_pulse_encoder_device *pulse_encoder;
result = RT_EOK;
pulse_encoder = (struct rt_pulse_encoder_device *)dev;
switch (cmd)
{
case PULSE_ENCODER_CMD_CLEAR_COUNT:
result = pulse_encoder->ops->clear_count(pulse_encoder);
break;
case PULSE_ENCODER_CMD_GET_TYPE:
*(enum rt_pulse_encoder_type *)args = pulse_encoder->type;
break;
case PULSE_ENCODER_CMD_ENABLE:
case PULSE_ENCODER_CMD_DISABLE:
result = pulse_encoder->ops->control(pulse_encoder, cmd, args);
break;
default:
result = -RT_ENOSYS;
break;
}
return result;
}
#ifdef RT_USING_DEVICE_OPS
const static struct rt_device_ops pulse_encoder_ops =
{
rt_pulse_encoder_init,
rt_pulse_encoder_open,
rt_pulse_encoder_close,
rt_pulse_encoder_read,
RT_NULL,
rt_pulse_encoder_control
};
#endif
rt_err_t rt_device_pulse_encoder_register(struct rt_pulse_encoder_device *pulse_encoder, const char *name, void *user_data)
{
struct rt_device *device;
RT_ASSERT(pulse_encoder != RT_NULL);
RT_ASSERT(pulse_encoder->ops != RT_NULL);
device = &(pulse_encoder->parent);
device->type = RT_Device_Class_Miscellaneous;
device->rx_indicate = RT_NULL;
device->tx_complete = RT_NULL;
#ifdef RT_USING_DEVICE_OPS
device->ops = &pulse_encoder_ops;
#else
device->init = rt_pulse_encoder_init;
device->open = rt_pulse_encoder_open;
device->close = rt_pulse_encoder_close;
device->read = rt_pulse_encoder_read;
device->write = RT_NULL;
device->control = rt_pulse_encoder_control;
#endif
device->user_data = user_data;
return rt_device_register(device, name, RT_DEVICE_FLAG_RDONLY | RT_DEVICE_FLAG_STANDALONE);
}
@@ -0,0 +1,328 @@
/*
* Copyright (c) 2006-2021, RT-Thread Development Team
*
* SPDX-License-Identifier: Apache-2.0
*
* Change Logs:
* Date Author Notes
* 2018-05-07 aozima the first version
*/
#include <string.h>
#include <stdlib.h>
#include <drivers/rt_drv_pwm.h>
static rt_err_t _pwm_control(rt_device_t dev, int cmd, void *args)
{
rt_err_t result = RT_EOK;
struct rt_device_pwm *pwm = (struct rt_device_pwm *)dev;
if (pwm->ops->control)
{
result = pwm->ops->control(pwm, cmd, args);
}
return result;
}
/*
pos: channel
void *buffer: rt_uint32_t pulse[size]
size : number of pulse, only set to sizeof(rt_uint32_t).
*/
static rt_size_t _pwm_read(rt_device_t dev, rt_off_t pos, void *buffer, rt_size_t size)
{
rt_err_t result = RT_EOK;
struct rt_device_pwm *pwm = (struct rt_device_pwm *)dev;
rt_uint32_t *pulse = (rt_uint32_t *)buffer;
struct rt_pwm_configuration configuration = {0};
configuration.channel = pos;
if (pwm->ops->control)
{
result = pwm->ops->control(pwm, PWM_CMD_GET, &configuration);
if (result != RT_EOK)
{
return 0;
}
*pulse = configuration.pulse;
}
return size;
}
/*
pos: channel
void *buffer: rt_uint32_t pulse[size]
size : number of pulse, only set to sizeof(rt_uint32_t).
*/
static rt_size_t _pwm_write(rt_device_t dev, rt_off_t pos, const void *buffer, rt_size_t size)
{
rt_err_t result = RT_EOK;
struct rt_device_pwm *pwm = (struct rt_device_pwm *)dev;
rt_uint32_t *pulse = (rt_uint32_t *)buffer;
struct rt_pwm_configuration configuration = {0};
configuration.channel = pos;
if (pwm->ops->control)
{
result = pwm->ops->control(pwm, PWM_CMD_GET, &configuration);
if (result != RT_EOK)
{
return 0;
}
configuration.pulse = *pulse;
result = pwm->ops->control(pwm, PWM_CMD_SET, &configuration);
if (result != RT_EOK)
{
return 0;
}
}
return size;
}
#ifdef RT_USING_DEVICE_OPS
static const struct rt_device_ops pwm_device_ops =
{
RT_NULL,
RT_NULL,
RT_NULL,
_pwm_read,
_pwm_write,
_pwm_control
};
#endif /* RT_USING_DEVICE_OPS */
rt_err_t rt_device_pwm_register(struct rt_device_pwm *device, const char *name, const struct rt_pwm_ops *ops, const void *user_data)
{
rt_err_t result = RT_EOK;
rt_memset(device, 0, sizeof(struct rt_device_pwm));
#ifdef RT_USING_DEVICE_OPS
device->parent.ops = &pwm_device_ops;
#else
device->parent.init = RT_NULL;
device->parent.open = RT_NULL;
device->parent.close = RT_NULL;
device->parent.read = _pwm_read;
device->parent.write = _pwm_write;
device->parent.control = _pwm_control;
#endif /* RT_USING_DEVICE_OPS */
device->parent.type = RT_Device_Class_Miscellaneous;
device->ops = ops;
device->parent.user_data = (void *)user_data;
result = rt_device_register(&device->parent, name, RT_DEVICE_FLAG_RDWR);
return result;
}
rt_err_t rt_pwm_enable(struct rt_device_pwm *device, int channel)
{
rt_err_t result = RT_EOK;
struct rt_pwm_configuration configuration = {0};
if (!device)
{
return -RT_EIO;
}
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;
}
rt_err_t rt_pwm_disable(struct rt_device_pwm *device, int channel)
{
rt_err_t result = RT_EOK;
struct rt_pwm_configuration configuration = {0};
if (!device)
{
return -RT_EIO;
}
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;
}
rt_err_t rt_pwm_set(struct rt_device_pwm *device, int channel, rt_uint32_t period, rt_uint32_t pulse)
{
rt_err_t result = RT_EOK;
struct rt_pwm_configuration configuration = {0};
if (!device)
{
return -RT_EIO;
}
configuration.channel = channel;
configuration.period = period;
configuration.pulse = pulse;
result = rt_device_control(&device->parent, PWM_CMD_SET, &configuration);
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>
FINSH_FUNCTION_EXPORT_ALIAS(rt_pwm_enable, pwm_enable, enable pwm by channel.);
FINSH_FUNCTION_EXPORT_ALIAS(rt_pwm_set, pwm_set, set pwm.);
#ifdef RT_USING_FINSH
static int pwm_enable(int argc, char **argv)
{
int result = 0;
struct rt_device_pwm *device = RT_NULL;
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;
}
device = (struct rt_device_pwm *)rt_device_find(argv[1]);
if (!device)
{
result = -RT_EIO;
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 <pwm_dev> <channel/-channel>);
static int pwm_disable(int argc, char **argv)
{
int result = 0;
struct rt_device_pwm *device = RT_NULL;
if (argc != 3)
{
rt_kprintf("Usage: pwm_disable pwm1 1\n");
rt_kprintf(" pwm_disable <pwm_dev> <channel/-channel> \n");
result = -RT_ERROR;
goto _exit;
}
device = (struct rt_device_pwm *)rt_device_find(argv[1]);
if (!device)
{
result = -RT_EIO;
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 <pwm_dev> <channel/-channel>);
static int pwm_set(int argc, char **argv)
{
int result = 0;
struct rt_device_pwm *device = RT_NULL;
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;
}
device = (struct rt_device_pwm *)rt_device_find(argv[1]);
if (!device)
{
result = -RT_EIO;
goto _exit;
}
result = rt_pwm_set(device, atoi(argv[2]), atoi(argv[3]), atoi(argv[4]));
_exit:
return result;
}
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 /* RT_USING_FINSH */
#endif /* RT_USING_FINSH */
@@ -0,0 +1,195 @@
/*
* Copyright (c) 2006-2021, RT-Thread Development Team
*
* SPDX-License-Identifier: Apache-2.0
*
* Change Logs:
* Date Author Notes
* 2019-08-13 balanceTWK the first version
*/
#include <rtthread.h>
#include <rtdevice.h>
#include <rtdbg.h>
static rt_err_t rt_inputcapture_init(struct rt_device *dev)
{
rt_err_t ret;
struct rt_inputcapture_device *inputcapture;
RT_ASSERT(dev != RT_NULL);
ret = RT_EOK;
inputcapture = (struct rt_inputcapture_device *)dev;
inputcapture->watermark = RT_INPUT_CAPTURE_RB_SIZE / 2;
if (inputcapture->ops->init)
{
ret = inputcapture->ops->init(inputcapture);
}
return ret;
}
static rt_err_t rt_inputcapture_open(struct rt_device *dev, rt_uint16_t oflag)
{
rt_err_t ret;
struct rt_inputcapture_device *inputcapture;
RT_ASSERT(dev != RT_NULL);
ret = RT_EOK;
inputcapture = (struct rt_inputcapture_device *)dev;
if (inputcapture->ringbuff == RT_NULL)
{
inputcapture->ringbuff = rt_ringbuffer_create(sizeof(struct rt_inputcapture_data) * RT_INPUT_CAPTURE_RB_SIZE);
}
if (inputcapture->ops->open)
{
ret = inputcapture->ops->open(inputcapture);
}
return ret;
}
static rt_err_t rt_inputcapture_close(struct rt_device *dev)
{
rt_err_t ret;
struct rt_inputcapture_device *inputcapture;
RT_ASSERT(dev != RT_NULL);
ret = -RT_ERROR;
inputcapture = (struct rt_inputcapture_device *)dev;
if (inputcapture->ops->close)
{
ret = inputcapture->ops->close(inputcapture);
}
if (ret != RT_EOK)
{
return ret;
}
if (inputcapture->ringbuff)
{
rt_ringbuffer_destroy(inputcapture->ringbuff);
inputcapture->ringbuff = RT_NULL;
}
return ret;
}
static rt_size_t rt_inputcapture_read(struct rt_device *dev,
rt_off_t pos,
void *buffer,
rt_size_t size)
{
rt_size_t receive_size;
struct rt_inputcapture_device *inputcapture;
RT_ASSERT(dev != RT_NULL);
inputcapture = (struct rt_inputcapture_device *)dev;
receive_size = rt_ringbuffer_get(inputcapture->ringbuff, (rt_uint8_t *)buffer, sizeof(struct rt_inputcapture_data) * size);
return receive_size / sizeof(struct rt_inputcapture_data);
}
static rt_err_t rt_inputcapture_control(struct rt_device *dev, int cmd, void *args)
{
rt_err_t result;
struct rt_inputcapture_device *inputcapture;
RT_ASSERT(dev != RT_NULL);
result = RT_EOK;
inputcapture = (struct rt_inputcapture_device *)dev;
switch (cmd)
{
case INPUTCAPTURE_CMD_CLEAR_BUF:
if (inputcapture->ringbuff)
{
rt_ringbuffer_reset(inputcapture->ringbuff);
}
break;
case INPUTCAPTURE_CMD_SET_WATERMARK:
inputcapture->watermark = *(rt_size_t *)args;
break;
default:
result = -RT_ENOSYS;
break;
}
return result;
}
#ifdef RT_USING_DEVICE_OPS
const static struct rt_device_ops inputcapture_ops =
{
rt_inputcapture_init,
rt_inputcapture_open,
rt_inputcapture_close,
rt_inputcapture_read,
RT_NULL,
rt_inputcapture_control
};
#endif
rt_err_t rt_device_inputcapture_register(struct rt_inputcapture_device *inputcapture, const char *name, void *user_data)
{
struct rt_device *device;
RT_ASSERT(inputcapture != RT_NULL);
RT_ASSERT(inputcapture->ops != RT_NULL);
RT_ASSERT(inputcapture->ops->get_pulsewidth != RT_NULL);
device = &(inputcapture->parent);
device->type = RT_Device_Class_Miscellaneous;
device->rx_indicate = RT_NULL;
device->tx_complete = RT_NULL;
inputcapture->ringbuff = RT_NULL;
#ifdef RT_USING_DEVICE_OPS
device->ops = &inputcapture_ops;
#else
device->init = rt_inputcapture_init;
device->open = rt_inputcapture_open;
device->close = rt_inputcapture_close;
device->read = rt_inputcapture_read;
device->write = RT_NULL;
device->control = rt_inputcapture_control;
#endif
device->user_data = user_data;
return rt_device_register(device, name, RT_DEVICE_FLAG_RDONLY | RT_DEVICE_FLAG_STANDALONE);
}
/**
* This function is ISR for inputcapture interrupt.
* level: RT_TRUE denotes high level pulse, and RT_FALSE denotes low level pulse.
*/
void rt_hw_inputcapture_isr(struct rt_inputcapture_device *inputcapture, rt_bool_t level)
{
struct rt_inputcapture_data data;
rt_size_t receive_size;
if (inputcapture->ops->get_pulsewidth(inputcapture, &data.pulsewidth_us) != RT_EOK)
{
return;
}
data.is_high = level;
if (rt_ringbuffer_put(inputcapture->ringbuff, (rt_uint8_t *)&data, sizeof(struct rt_inputcapture_data)) == 0)
{
LOG_W("inputcapture ringbuffer doesn't have enough space.");
}
receive_size = rt_ringbuffer_data_len(inputcapture->ringbuff) / sizeof(struct rt_inputcapture_data);
if (receive_size >= inputcapture->watermark)
{
/* indicate to upper layer application */
if (inputcapture->parent.rx_indicate != RT_NULL)
inputcapture->parent.rx_indicate(&inputcapture->parent, receive_size);
}
}