[components][drivers] change name: capture -> inputcapture
This commit is contained in:
parent
e1587de6f7
commit
f9bad31d57
|
@ -438,13 +438,13 @@ config RT_USING_ENCODER
|
|||
bool "Using ENCODER device drivers"
|
||||
default n
|
||||
|
||||
config RT_USING_CAPTURE
|
||||
bool "Using CAPTURE device drivers"
|
||||
config RT_USING_INPUT_CAPTURE
|
||||
bool "Using INPUT CAPTURE device drivers"
|
||||
default n
|
||||
|
||||
if RT_USING_CAPTURE
|
||||
config RT_CAPTURE_RB_SIZE
|
||||
int "Set capture ringbuffer size"
|
||||
if RT_USING_INPUT_CAPTURE
|
||||
config RT_INPUT_CAPTURE_RB_SIZE
|
||||
int "Set input capture ringbuffer size"
|
||||
default 100
|
||||
endif
|
||||
|
||||
|
|
|
@ -1,60 +0,0 @@
|
|||
/*
|
||||
* Copyright (c) 2006-2018, RT-Thread Development Team
|
||||
*
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*
|
||||
* Change Logs:
|
||||
* Date Author Notes
|
||||
* 2019-08-13 balanceTWK first version.
|
||||
*/
|
||||
|
||||
#ifndef __RT_CAPTURE_H__
|
||||
#define __RT_CAPTURE_H__
|
||||
|
||||
#include <rtthread.h>
|
||||
#include <rtdevice.h>
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
/* capture control command */
|
||||
#define CAPTURE_CMD_CLEAR_BUF (128 + 0) /* clear capture buf */
|
||||
#define CAPTURE_CMD_SET_WATERMARK (128 + 1) /* Set the callback threshold */
|
||||
|
||||
struct capture_data
|
||||
{
|
||||
rt_uint32_t pulsewidth_us;
|
||||
rt_bool_t is_high;
|
||||
};
|
||||
|
||||
struct rt_capture_device
|
||||
{
|
||||
struct rt_device parent;
|
||||
|
||||
const struct rt_capture_ops *ops;
|
||||
struct rt_ringbuffer *ringbuff;
|
||||
rt_size_t watermark;
|
||||
};
|
||||
|
||||
/**
|
||||
* capture operators
|
||||
*/
|
||||
struct rt_capture_ops
|
||||
{
|
||||
rt_err_t (*init)(struct rt_capture_device *capture);
|
||||
rt_err_t (*open)(struct rt_capture_device *capture);
|
||||
rt_err_t (*close)(struct rt_capture_device *capture);
|
||||
rt_err_t (*get_pulsewidth)(struct rt_capture_device *capture, rt_uint32_t *pulsewidth_us);
|
||||
};
|
||||
|
||||
void rt_hw_capture_isr(struct rt_capture_device *capture, rt_bool_t level);
|
||||
|
||||
rt_err_t rt_device_capture_register(struct rt_capture_device *capture,
|
||||
const char *name,
|
||||
void *data);
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif /* __RT_CAPTURE_H__ */
|
|
@ -0,0 +1,60 @@
|
|||
/*
|
||||
* Copyright (c) 2006-2018, RT-Thread Development Team
|
||||
*
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*
|
||||
* Change Logs:
|
||||
* Date Author Notes
|
||||
* 2019-08-13 balanceTWK first version.
|
||||
*/
|
||||
|
||||
#ifndef __RT_INPUT_CAPTURE_H__
|
||||
#define __RT_INPUT_CAPTURE_H__
|
||||
|
||||
#include <rtthread.h>
|
||||
#include <rtdevice.h>
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
/* capture control command */
|
||||
#define INPUTCAPTURE_CMD_CLEAR_BUF (128 + 0) /* clear capture buf */
|
||||
#define INPUTCAPTURE_CMD_SET_WATERMARK (128 + 1) /* Set the callback threshold */
|
||||
|
||||
struct rt_inputcapture_data
|
||||
{
|
||||
rt_uint32_t pulsewidth_us;
|
||||
rt_bool_t is_high;
|
||||
};
|
||||
|
||||
struct rt_inputcapture_device
|
||||
{
|
||||
struct rt_device parent;
|
||||
|
||||
const struct rt_inputcapture_ops *ops;
|
||||
struct rt_ringbuffer *ringbuff;
|
||||
rt_size_t watermark;
|
||||
};
|
||||
|
||||
/**
|
||||
* capture operators
|
||||
*/
|
||||
struct rt_inputcapture_ops
|
||||
{
|
||||
rt_err_t (*init)(struct rt_inputcapture_device *inputcapture);
|
||||
rt_err_t (*open)(struct rt_inputcapture_device *inputcapture);
|
||||
rt_err_t (*close)(struct rt_inputcapture_device *inputcapture);
|
||||
rt_err_t (*get_pulsewidth)(struct rt_inputcapture_device *inputcapture, rt_uint32_t *pulsewidth_us);
|
||||
};
|
||||
|
||||
void rt_hw_capture_isr(struct rt_inputcapture_device *inputcapture, rt_bool_t level);
|
||||
|
||||
rt_err_t rt_device_inputcapture_register(struct rt_inputcapture_device *inputcapture,
|
||||
const char *name,
|
||||
void *data);
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif /* __RT_INPUT_CAPTURE_H__ */
|
|
@ -130,8 +130,8 @@ extern "C" {
|
|||
#include "drivers/encoder.h"
|
||||
#endif
|
||||
|
||||
#ifdef RT_USING_CAPTURE
|
||||
#include "drivers/rt_capture.h"
|
||||
#ifdef RT_USING_INPUT_CAPTURE
|
||||
#include "drivers/rt_inputcapture.h"
|
||||
#endif
|
||||
|
||||
#ifdef __cplusplus
|
||||
|
|
|
@ -17,8 +17,8 @@ if GetDepend(['RT_USING_PWM']):
|
|||
if GetDepend(['RT_USING_ENCODER']):
|
||||
src = src + ['encoder.c']
|
||||
|
||||
if GetDepend(['RT_USING_CAPTURE']):
|
||||
src = src + ['rt_capture.c']
|
||||
if GetDepend(['RT_USING_INPUT_CAPTURE']):
|
||||
src = src + ['rt_inputcapture.c']
|
||||
|
||||
if len(src):
|
||||
group = DefineGroup('DeviceDrivers', src, depend = [''], CPPPATH = CPPPATH)
|
||||
|
|
|
@ -1,195 +0,0 @@
|
|||
/*
|
||||
* Copyright (c) 2006-2018, 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_capture_init(struct rt_device *dev)
|
||||
{
|
||||
rt_err_t ret;
|
||||
struct rt_capture_device *capture;
|
||||
|
||||
RT_ASSERT(dev != RT_NULL);
|
||||
|
||||
ret = RT_EOK;
|
||||
capture = (struct rt_capture_device *)dev;
|
||||
capture->watermark = RT_CAPTURE_RB_SIZE / 2;
|
||||
if (capture->ops->init)
|
||||
{
|
||||
ret = capture->ops->init(capture);
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
static rt_err_t rt_capture_open(struct rt_device *dev, rt_uint16_t oflag)
|
||||
{
|
||||
rt_err_t ret;
|
||||
struct rt_capture_device *capture;
|
||||
|
||||
RT_ASSERT(dev != RT_NULL);
|
||||
|
||||
ret = RT_EOK;
|
||||
capture = (struct rt_capture_device *)dev;
|
||||
if (capture->ringbuff == RT_NULL)
|
||||
{
|
||||
capture->ringbuff = rt_ringbuffer_create(sizeof(struct capture_data) * RT_CAPTURE_RB_SIZE);
|
||||
}
|
||||
if (capture->ops->open)
|
||||
{
|
||||
ret = capture->ops->open(capture);
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
static rt_err_t rt_capture_close(struct rt_device *dev)
|
||||
{
|
||||
rt_err_t ret;
|
||||
struct rt_capture_device *capture;
|
||||
|
||||
RT_ASSERT(dev != RT_NULL);
|
||||
|
||||
ret = -RT_ERROR;
|
||||
capture = (struct rt_capture_device *)dev;
|
||||
|
||||
if (capture->ops->close)
|
||||
{
|
||||
ret = capture->ops->close(capture);
|
||||
}
|
||||
|
||||
if (ret != RT_EOK)
|
||||
{
|
||||
return ret;
|
||||
}
|
||||
|
||||
if (capture->ringbuff)
|
||||
{
|
||||
rt_ringbuffer_destroy(capture->ringbuff);
|
||||
capture->ringbuff = RT_NULL;
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
static rt_size_t rt_capture_read(struct rt_device *dev,
|
||||
rt_off_t pos,
|
||||
void *buffer,
|
||||
rt_size_t size)
|
||||
{
|
||||
rt_size_t receive_size;
|
||||
struct rt_capture_device *capture;
|
||||
|
||||
RT_ASSERT(dev != RT_NULL);
|
||||
|
||||
capture = (struct rt_capture_device *)dev;
|
||||
receive_size = rt_ringbuffer_get(capture->ringbuff, (rt_uint8_t *)buffer, sizeof(struct capture_data) * size);
|
||||
|
||||
return receive_size / sizeof(struct capture_data);
|
||||
}
|
||||
|
||||
static rt_err_t rt_capture_control(struct rt_device *dev, int cmd, void *args)
|
||||
{
|
||||
rt_err_t result;
|
||||
struct rt_capture_device *capture;
|
||||
|
||||
RT_ASSERT(dev != RT_NULL);
|
||||
|
||||
result = RT_EOK;
|
||||
capture = (struct rt_capture_device *)dev;
|
||||
switch (cmd)
|
||||
{
|
||||
case CAPTURE_CMD_CLEAR_BUF:
|
||||
if (capture->ringbuff)
|
||||
{
|
||||
rt_ringbuffer_reset(capture->ringbuff);
|
||||
}
|
||||
break;
|
||||
case CAPTURE_CMD_SET_WATERMARK:
|
||||
capture->watermark = *(rt_size_t *)args;
|
||||
break;
|
||||
default:
|
||||
result = -RT_ENOSYS;
|
||||
break;
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
#ifdef RT_USING_DEVICE_OPS
|
||||
const static struct rt_device_ops capture_ops =
|
||||
{
|
||||
rt_capture_init,
|
||||
rt_capture_open,
|
||||
rt_capture_close,
|
||||
rt_capture_read,
|
||||
RT_NULL,
|
||||
rt_capture_control
|
||||
};
|
||||
#endif
|
||||
|
||||
rt_err_t rt_device_capture_register(struct rt_capture_device *capture, const char *name, void *user_data)
|
||||
{
|
||||
struct rt_device *device;
|
||||
|
||||
RT_ASSERT(capture != RT_NULL);
|
||||
RT_ASSERT(capture->ops != RT_NULL);
|
||||
RT_ASSERT(capture->ops->get_pulsewidth != RT_NULL);
|
||||
|
||||
device = &(capture->parent);
|
||||
|
||||
device->type = RT_Device_Class_Miscellaneous;
|
||||
device->rx_indicate = RT_NULL;
|
||||
device->tx_complete = RT_NULL;
|
||||
capture->ringbuff = RT_NULL;
|
||||
|
||||
#ifdef RT_USING_DEVICE_OPS
|
||||
device->ops = &capture_ops;
|
||||
#else
|
||||
device->init = rt_capture_init;
|
||||
device->open = rt_capture_open;
|
||||
device->close = rt_capture_close;
|
||||
device->read = rt_capture_read;
|
||||
device->write = RT_NULL;
|
||||
device->control = rt_capture_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 capture interrupt.
|
||||
* level: RT_TRUE denotes high level pulse, and RT_FALSE denotes low level pulse.
|
||||
*/
|
||||
void rt_hw_capture_isr(struct rt_capture_device *capture, rt_bool_t level)
|
||||
{
|
||||
struct capture_data data;
|
||||
rt_size_t receive_size;
|
||||
if (capture->ops->get_pulsewidth(capture, &data.pulsewidth_us) != RT_EOK)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
data.is_high = level;
|
||||
if (rt_ringbuffer_put(capture->ringbuff, (rt_uint8_t *)&data, sizeof(struct capture_data)) == 0)
|
||||
{
|
||||
LOG_W("capture ringbuffer doesn't have enough space.");
|
||||
}
|
||||
|
||||
receive_size = rt_ringbuffer_data_len(capture->ringbuff) / sizeof(struct capture_data);
|
||||
|
||||
if (receive_size >= capture->watermark)
|
||||
{
|
||||
/* indicate to upper layer application */
|
||||
if (capture->parent.rx_indicate != RT_NULL)
|
||||
capture->parent.rx_indicate(&capture->parent, receive_size);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,195 @@
|
|||
/*
|
||||
* Copyright (c) 2006-2018, 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);
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue