From d01b2ca84b8a6f7950cc63d27ba5093b0624c6aa Mon Sep 17 00:00:00 2001 From: Bernard Xiong Date: Mon, 19 Jan 2015 06:18:16 +0000 Subject: [DeviceDrivers] Add pin io driver framework --- components/drivers/include/drivers/pin.h | 79 ++++++++++++++++++++++++++++++++ 1 file changed, 79 insertions(+) create mode 100644 components/drivers/include/drivers/pin.h (limited to 'components/drivers/include') diff --git a/components/drivers/include/drivers/pin.h b/components/drivers/include/drivers/pin.h new file mode 100644 index 0000000000..7e1fd4e825 --- /dev/null +++ b/components/drivers/include/drivers/pin.h @@ -0,0 +1,79 @@ +/* + * File : pin.h + * This file is part of RT-Thread RTOS + * COPYRIGHT (C) 2015, RT-Thread Development Team + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along + * with this program; if not, write to the Free Software Foundation, Inc., + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + * + * Change Logs: + * Date Author Notes + * 2015-01-20 Bernard the first version + */ + +#ifndef PIN_H__ +#define PIN_H__ + +#include +#include + +#ifdef __cplusplus +extern "C" { +#endif + +/* pin device and operations for RT-Thread */ +struct rt_device_pin +{ + struct rt_device parent; + const struct rt_pin_ops *ops; +}; + +#define PIN_LOW 0x00 +#define PIN_HIGH 0x01 + +#define PIN_MODE_OUTPUT 0x00 +#define PIN_MODE_INPUT 0x01 +#define PIN_MODE_INPUT_PULLUP 0x02 + +struct rt_device_pin_mode +{ + rt_uint16_t pin; + rt_uint16_t mode; +}; +struct rt_device_pin_status +{ + rt_uint16_t pin; + rt_uint16_t status; +}; + +struct rt_pin_ops +{ + void (*pin_mode) (struct rt_device* device, rt_base_t pin, rt_base_t mode); + void (*pin_write)(struct rt_device* device, rt_base_t pin, rt_base_t value); + int (*pin_read) (struct rt_device* device, rt_base_t pin); + + /* TODO: add GPIO interrupt */ +}; + +int rt_device_pin_register(const char* name, const struct rt_pin_ops* ops, void* user_data); + +void rt_pin_mode(rt_base_t pin, rt_base_t mode); +void rt_pin_write(rt_base_t pin, rt_base_t value); +int rt_pin_read (rt_base_t pin); + +#ifdef __cplusplus +} +#endif + +#endif -- cgit v1.3.1 From 1bb425e839fc78fe18accd1e9ec65ff9a7631812 Mon Sep 17 00:00:00 2001 From: Bernard Xiong Date: Mon, 19 Jan 2015 08:56:43 +0000 Subject: [DeviceDrivers] code cleanup for hardware pin --- components/drivers/include/drivers/pin.h | 10 +++---- components/drivers/misc/pin.c | 50 ++++++++++++++++---------------- 2 files changed, 30 insertions(+), 30 deletions(-) (limited to 'components/drivers/include') diff --git a/components/drivers/include/drivers/pin.h b/components/drivers/include/drivers/pin.h index 7e1fd4e825..704114cf99 100644 --- a/components/drivers/include/drivers/pin.h +++ b/components/drivers/include/drivers/pin.h @@ -59,18 +59,18 @@ struct rt_device_pin_status struct rt_pin_ops { - void (*pin_mode) (struct rt_device* device, rt_base_t pin, rt_base_t mode); - void (*pin_write)(struct rt_device* device, rt_base_t pin, rt_base_t value); - int (*pin_read) (struct rt_device* device, rt_base_t pin); + void (*pin_mode)(struct rt_device *device, rt_base_t pin, rt_base_t mode); + void (*pin_write)(struct rt_device *device, rt_base_t pin, rt_base_t value); + int (*pin_read)(struct rt_device *device, rt_base_t pin); /* TODO: add GPIO interrupt */ }; -int rt_device_pin_register(const char* name, const struct rt_pin_ops* ops, void* user_data); +int rt_device_pin_register(const char *name, const struct rt_pin_ops *ops, void *user_data); void rt_pin_mode(rt_base_t pin, rt_base_t mode); void rt_pin_write(rt_base_t pin, rt_base_t value); -int rt_pin_read (rt_base_t pin); +int rt_pin_read(rt_base_t pin); #ifdef __cplusplus } diff --git a/components/drivers/misc/pin.c b/components/drivers/misc/pin.c index ef244ccdc7..d83c3d2f03 100644 --- a/components/drivers/misc/pin.c +++ b/components/drivers/misc/pin.c @@ -29,30 +29,30 @@ #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) +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; + 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; + 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) +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; + 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; + 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); @@ -62,13 +62,13 @@ static rt_size_t _pin_write (rt_device_t dev, rt_off_t pos, const void *buffer, static rt_err_t _pin_control(rt_device_t dev, rt_uint8_t cmd, void *args) { - struct rt_device_pin_mode* mode; - struct rt_device_pin* pin = (struct rt_device_pin*)dev; + 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; + 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); @@ -76,24 +76,24 @@ static rt_err_t _pin_control(rt_device_t dev, rt_uint8_t cmd, void *args) return 0; } -int rt_device_pin_register(const char* name, const struct rt_pin_ops* ops, void* user_data) +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; + _hw_pin.parent.type = RT_Device_Class_Miscellaneous; + _hw_pin.parent.rx_indicate = RT_NULL; + _hw_pin.parent.tx_complete = RT_NULL; - _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; + _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; _hw_pin.ops = ops; - _hw_pin.parent.user_data = user_data; + _hw_pin.parent.user_data = user_data; - /* register a character device */ - rt_device_register(&_hw_pin.parent, "pin", RT_DEVICE_FLAG_RDWR); + /* register a character device */ + rt_device_register(&_hw_pin.parent, "pin", RT_DEVICE_FLAG_RDWR); return 0; } @@ -111,7 +111,7 @@ void rt_pin_write(rt_base_t pin, rt_base_t value) } FINSH_FUNCTION_EXPORT_ALIAS(rt_pin_write, pinWrite, write value to hardware pin); -int rt_pin_read (rt_base_t pin) +int rt_pin_read(rt_base_t pin) { return _hw_pin.ops->pin_read(&_hw_pin.parent, pin); } -- cgit v1.3.1 From 73d5acecba80316e717f8ecdc43526575dcfbe0c Mon Sep 17 00:00:00 2001 From: Bernard Xiong Date: Tue, 20 Jan 2015 07:24:49 +0000 Subject: [DeviceDrivers] Add pin.h in the rtdevice.h --- components/drivers/include/rtdevice.h | 4 ++++ 1 file changed, 4 insertions(+) (limited to 'components/drivers/include') diff --git a/components/drivers/include/rtdevice.h b/components/drivers/include/rtdevice.h index dc9761032e..bb01da2d12 100644 --- a/components/drivers/include/rtdevice.h +++ b/components/drivers/include/rtdevice.h @@ -358,5 +358,9 @@ rt_inline void rt_work_init(struct rt_work* work, void (*work_func)(struct rt_wo #include "drivers/watchdog.h" #endif +#ifdef RT_USING_PIN +#include "drivers/pin.h" +#endif + #endif /* __RT_DEVICE_H__ */ -- cgit v1.3.1