From cc565da5a178d598d35cc9b7d4a1a36be5fb17f5 Mon Sep 17 00:00:00 2001 From: tangweikang Date: Fri, 9 Aug 2019 18:37:16 +0800 Subject: [PATCH 1/3] [components][drivers] add encoder driven framework --- components/drivers/Kconfig | 4 + components/drivers/include/drivers/encoder.h | 65 +++++++++ components/drivers/include/rtdevice.h | 4 + components/drivers/misc/SConscript | 3 + components/drivers/misc/encoder.c | 135 +++++++++++++++++++ 5 files changed, 211 insertions(+) create mode 100644 components/drivers/include/drivers/encoder.h create mode 100644 components/drivers/misc/encoder.c diff --git a/components/drivers/Kconfig b/components/drivers/Kconfig index 250d661e9f..ff2639b8fa 100755 --- a/components/drivers/Kconfig +++ b/components/drivers/Kconfig @@ -434,6 +434,10 @@ menuconfig RT_USING_HWCRYPTO endif endif +config RT_USING_ENCODER + bool "Using ENCODER device drivers" + default n + menuconfig RT_USING_WIFI bool "Using Wi-Fi framework" default n diff --git a/components/drivers/include/drivers/encoder.h b/components/drivers/include/drivers/encoder.h new file mode 100644 index 0000000000..c6c3e4d708 --- /dev/null +++ b/components/drivers/include/drivers/encoder.h @@ -0,0 +1,65 @@ +/* + * Copyright (c) 2006-2018, RT-Thread Development Team + * + * SPDX-License-Identifier: Apache-2.0 + * + * Change Logs: + * Date Author Notes + * 2019-08-08 balanceTWK the first version + */ + +#ifndef __ENCODER_H__ +#define __ENCODER_H__ + +#include +#include + +#ifdef __cplusplus +extern "C" { +#endif + +/* encoder control command */ +typedef enum +{ + ENCODER_INFO_GET = 0x01, /* get a encoder feature information */ + ENCODER_SWITCH_ON, /* switch on encoder */ + ENCODER_SWITCH_OFF, /* switch off encoder */ + ENCODER_COUNT_CLEAR, /* clear encoder count */ +} rt_encoder_ctrl_t; + +/* encoder type */ +typedef enum +{ + SINGLE_PHASE_ENCODER = 0x01, /* single phase encoder */ + AB_PHASE_ENCODER /* two phase encoder */ +} rt_encoder_type_t; + +struct rt_encoder_device; + +struct rt_encoder_ops +{ + rt_err_t (*init)(struct rt_encoder_device *encoder); + rt_int32_t (*get_count)(struct rt_encoder_device *encoder); + rt_err_t (*control)(struct rt_encoder_device *encoder, rt_uint32_t cmd, void *args); +}; + +/* Encoder feature information */ +struct rt_encoder_info +{ + rt_encoder_type_t type; /* the type of encoder */ +}; + +typedef struct rt_encoder_device +{ + struct rt_device parent; + const struct rt_encoder_ops *ops; + const struct rt_encoder_info *info; +} rt_encoder_t; + +rt_err_t rt_device_encoder_register(rt_encoder_t *encoder, const char *name, void *user_data); + +#ifdef __cplusplus +} +#endif + +#endif /* __ENCODER_H__ */ diff --git a/components/drivers/include/rtdevice.h b/components/drivers/include/rtdevice.h index f764d30c24..0c009e9580 100644 --- a/components/drivers/include/rtdevice.h +++ b/components/drivers/include/rtdevice.h @@ -126,6 +126,10 @@ extern "C" { #include "drivers/crypto.h" #endif +#ifdef RT_USING_ENCODER +#include "drivers/encoder.h" +#endif + #ifdef __cplusplus } #endif diff --git a/components/drivers/misc/SConscript b/components/drivers/misc/SConscript index c45ba524c5..3bfb73fea0 100644 --- a/components/drivers/misc/SConscript +++ b/components/drivers/misc/SConscript @@ -13,6 +13,9 @@ if GetDepend(['RT_USING_ADC']): if GetDepend(['RT_USING_PWM']): src = src + ['rt_drv_pwm.c'] + +if GetDepend(['RT_USING_ENCODER']): + src = src + ['encoder.c'] if len(src): group = DefineGroup('DeviceDrivers', src, depend = [''], CPPPATH = CPPPATH) diff --git a/components/drivers/misc/encoder.c b/components/drivers/misc/encoder.c new file mode 100644 index 0000000000..d0b2ef9522 --- /dev/null +++ b/components/drivers/misc/encoder.c @@ -0,0 +1,135 @@ +/* + * Copyright (c) 2006-2018, RT-Thread Development Team + * + * SPDX-License-Identifier: Apache-2.0 + * + * Change Logs: + * Date Author Notes + * 2019-08-08 balanceTWK the first version + */ + +#include +#include + +static rt_err_t rt_encoder_init(struct rt_device *dev) +{ + rt_encoder_t *encoder; + + encoder = (rt_encoder_t *)dev; + if (encoder->ops->init) + { + return encoder->ops->init(encoder); + } + else + { + return -RT_ENOSYS; + } +} + +static rt_err_t rt_encoder_open(struct rt_device *dev, rt_uint16_t oflag) +{ + rt_encoder_t *encoder; + + encoder = (rt_encoder_t *)dev; + if (encoder->ops->control) + { + return encoder->ops->control(encoder, ENCODER_SWITCH_ON, RT_NULL); + } + else + { + return -RT_ENOSYS; + } +} + +static rt_err_t rt_encoder_close(struct rt_device *dev) +{ + rt_encoder_t *encoder; + + encoder = (rt_encoder_t *)dev; + if (encoder->ops->control) + { + return encoder->ops->control(encoder, ENCODER_SWITCH_OFF, RT_NULL); + } + else + { + return -RT_ENOSYS; + } +} + +static rt_size_t rt_encoder_read(struct rt_device *dev, rt_off_t pos, void *buffer, rt_size_t size) +{ + rt_encoder_t *encoder; + + encoder = (rt_encoder_t *)dev; + if (encoder->ops->get_count) + { + *(rt_int32_t *)buffer = encoder->ops->get_count(encoder); + } + return 1; +} + +static rt_err_t rt_encoder_control(struct rt_device *dev, int cmd, void *args) +{ + rt_err_t result; + rt_encoder_t *encoder; + + result = RT_EOK; + encoder = (rt_encoder_t *)dev; + switch (cmd) + { + case ENCODER_INFO_GET: + *(struct rt_encoder_info *)args = *encoder->info; + break; + case ENCODER_SWITCH_ON: + case ENCODER_SWITCH_OFF: + case ENCODER_COUNT_CLEAR: + result = encoder->ops->control(encoder, cmd, args); + break; + default: + result = -RT_ENOSYS; + break; + } + + return result; +} + +#ifdef RT_USING_DEVICE_OPS +const static struct rt_device_ops encoder_ops = +{ + rt_encoder_init, + rt_encoder_open, + rt_encoder_close, + rt_encoder_read, + RT_NULL, + rt_encoder_control +}; +#endif + +rt_err_t rt_device_encoder_register(rt_encoder_t *encoder, const char *name, void *user_data) +{ + struct rt_device *device; + + RT_ASSERT(encoder != RT_NULL); + RT_ASSERT(encoder->ops != RT_NULL); + RT_ASSERT(encoder->info != RT_NULL); + + device = &(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 = &encoder_ops; +#else + device->init = rt_encoder_init; + device->open = rt_encoder_open; + device->close = rt_encoder_close; + device->read = rt_encoder_read; + device->write = RT_NULL; + device->control = rt_encoder_control; +#endif + device->user_data = user_data; + + return rt_device_register(device, name, RT_DEVICE_FLAG_RDONLY | RT_DEVICE_FLAG_STANDALONE); +} From e5dc60e3cfc5551c18308dd4d2dad1069c6e5f2f Mon Sep 17 00:00:00 2001 From: tangweikang Date: Sun, 11 Aug 2019 15:34:51 +0800 Subject: [PATCH 2/3] [components][drivers] update encoder driven framework --- components/drivers/include/drivers/encoder.h | 32 +++++++---------- components/drivers/misc/encoder.c | 37 ++++++++++---------- 2 files changed, 30 insertions(+), 39 deletions(-) diff --git a/components/drivers/include/drivers/encoder.h b/components/drivers/include/drivers/encoder.h index c6c3e4d708..57cb47e6fb 100644 --- a/components/drivers/include/drivers/encoder.h +++ b/components/drivers/include/drivers/encoder.h @@ -19,20 +19,18 @@ extern "C" { #endif /* encoder control command */ -typedef enum -{ - ENCODER_INFO_GET = 0x01, /* get a encoder feature information */ - ENCODER_SWITCH_ON, /* switch on encoder */ - ENCODER_SWITCH_OFF, /* switch off encoder */ - ENCODER_COUNT_CLEAR, /* clear encoder count */ -} rt_encoder_ctrl_t; +#define ENCODER_CMD_GET_TYPE (128 + 0) /* get a encoder type information */ +#define ENCODER_CMD_ENABLE (128 + 1) /* enable encoder */ +#define ENCODER_CMD_DISABLE (128 + 2) /* disable encoder */ +#define ENCODER_CMD_CLEAR_COUNT (128 + 3) /* clear encoder count */ /* encoder type */ -typedef enum +enum rt_encoder_type { - SINGLE_PHASE_ENCODER = 0x01, /* single phase encoder */ + UNKNOWN_ENCODER_TYPE = 0x00, /* Unknown encoder type */ + SINGLE_PHASE_ENCODER, /* single phase encoder */ AB_PHASE_ENCODER /* two phase encoder */ -} rt_encoder_type_t; +}; struct rt_encoder_device; @@ -43,20 +41,14 @@ struct rt_encoder_ops rt_err_t (*control)(struct rt_encoder_device *encoder, rt_uint32_t cmd, void *args); }; -/* Encoder feature information */ -struct rt_encoder_info -{ - rt_encoder_type_t type; /* the type of encoder */ -}; - -typedef struct rt_encoder_device +struct rt_encoder_device { struct rt_device parent; const struct rt_encoder_ops *ops; - const struct rt_encoder_info *info; -} rt_encoder_t; + enum rt_encoder_type type; +}; -rt_err_t rt_device_encoder_register(rt_encoder_t *encoder, const char *name, void *user_data); +rt_err_t rt_device_encoder_register(struct rt_encoder_device *encoder, const char *name, void *user_data); #ifdef __cplusplus } diff --git a/components/drivers/misc/encoder.c b/components/drivers/misc/encoder.c index d0b2ef9522..ec799bd5fe 100644 --- a/components/drivers/misc/encoder.c +++ b/components/drivers/misc/encoder.c @@ -13,9 +13,9 @@ static rt_err_t rt_encoder_init(struct rt_device *dev) { - rt_encoder_t *encoder; + struct rt_encoder_device *encoder; - encoder = (rt_encoder_t *)dev; + encoder = (struct rt_encoder_device *)dev; if (encoder->ops->init) { return encoder->ops->init(encoder); @@ -28,12 +28,12 @@ static rt_err_t rt_encoder_init(struct rt_device *dev) static rt_err_t rt_encoder_open(struct rt_device *dev, rt_uint16_t oflag) { - rt_encoder_t *encoder; + struct rt_encoder_device *encoder; - encoder = (rt_encoder_t *)dev; + encoder = (struct rt_encoder_device *)dev; if (encoder->ops->control) { - return encoder->ops->control(encoder, ENCODER_SWITCH_ON, RT_NULL); + return encoder->ops->control(encoder, ENCODER_CMD_ENABLE, RT_NULL); } else { @@ -43,12 +43,12 @@ static rt_err_t rt_encoder_open(struct rt_device *dev, rt_uint16_t oflag) static rt_err_t rt_encoder_close(struct rt_device *dev) { - rt_encoder_t *encoder; + struct rt_encoder_device *encoder; - encoder = (rt_encoder_t *)dev; + encoder = (struct rt_encoder_device *)dev; if (encoder->ops->control) { - return encoder->ops->control(encoder, ENCODER_SWITCH_OFF, RT_NULL); + return encoder->ops->control(encoder, ENCODER_CMD_DISABLE, RT_NULL); } else { @@ -58,9 +58,9 @@ static rt_err_t rt_encoder_close(struct rt_device *dev) static rt_size_t rt_encoder_read(struct rt_device *dev, rt_off_t pos, void *buffer, rt_size_t size) { - rt_encoder_t *encoder; + struct rt_encoder_device *encoder; - encoder = (rt_encoder_t *)dev; + encoder = (struct rt_encoder_device *)dev; if (encoder->ops->get_count) { *(rt_int32_t *)buffer = encoder->ops->get_count(encoder); @@ -71,18 +71,18 @@ static rt_size_t rt_encoder_read(struct rt_device *dev, rt_off_t pos, void *buff static rt_err_t rt_encoder_control(struct rt_device *dev, int cmd, void *args) { rt_err_t result; - rt_encoder_t *encoder; + struct rt_encoder_device *encoder; result = RT_EOK; - encoder = (rt_encoder_t *)dev; + encoder = (struct rt_encoder_device *)dev; switch (cmd) { - case ENCODER_INFO_GET: - *(struct rt_encoder_info *)args = *encoder->info; + case ENCODER_CMD_GET_TYPE: + *(enum rt_encoder_type *)args = encoder->type; break; - case ENCODER_SWITCH_ON: - case ENCODER_SWITCH_OFF: - case ENCODER_COUNT_CLEAR: + case ENCODER_CMD_ENABLE: + case ENCODER_CMD_DISABLE: + case ENCODER_CMD_CLEAR_COUNT: result = encoder->ops->control(encoder, cmd, args); break; default: @@ -105,13 +105,12 @@ const static struct rt_device_ops encoder_ops = }; #endif -rt_err_t rt_device_encoder_register(rt_encoder_t *encoder, const char *name, void *user_data) +rt_err_t rt_device_encoder_register(struct rt_encoder_device *encoder, const char *name, void *user_data) { struct rt_device *device; RT_ASSERT(encoder != RT_NULL); RT_ASSERT(encoder->ops != RT_NULL); - RT_ASSERT(encoder->info != RT_NULL); device = &(encoder->parent); From 2b9a9e0163a833ca426109738507442d31a089fb Mon Sep 17 00:00:00 2001 From: tangweikang Date: Mon, 12 Aug 2019 13:47:03 +0800 Subject: [PATCH 3/3] [components][drivers] update : Move clear_count() function from ops->control() to ops->clear_count() --- components/drivers/include/drivers/encoder.h | 1 + components/drivers/misc/encoder.c | 4 +++- 2 files changed, 4 insertions(+), 1 deletion(-) diff --git a/components/drivers/include/drivers/encoder.h b/components/drivers/include/drivers/encoder.h index 57cb47e6fb..f338f9e845 100644 --- a/components/drivers/include/drivers/encoder.h +++ b/components/drivers/include/drivers/encoder.h @@ -38,6 +38,7 @@ struct rt_encoder_ops { rt_err_t (*init)(struct rt_encoder_device *encoder); rt_int32_t (*get_count)(struct rt_encoder_device *encoder); + rt_err_t (*clear_count)(struct rt_encoder_device *encoder); rt_err_t (*control)(struct rt_encoder_device *encoder, rt_uint32_t cmd, void *args); }; diff --git a/components/drivers/misc/encoder.c b/components/drivers/misc/encoder.c index ec799bd5fe..e9e1da0f40 100644 --- a/components/drivers/misc/encoder.c +++ b/components/drivers/misc/encoder.c @@ -77,12 +77,14 @@ static rt_err_t rt_encoder_control(struct rt_device *dev, int cmd, void *args) encoder = (struct rt_encoder_device *)dev; switch (cmd) { + case ENCODER_CMD_CLEAR_COUNT: + result = encoder->ops->clear_count(encoder); + break; case ENCODER_CMD_GET_TYPE: *(enum rt_encoder_type *)args = encoder->type; break; case ENCODER_CMD_ENABLE: case ENCODER_CMD_DISABLE: - case ENCODER_CMD_CLEAR_COUNT: result = encoder->ops->control(encoder, cmd, args); break; default: