2015-01-19 14:18:16 +08:00
|
|
|
/*
|
2018-10-14 19:37:18 +08:00
|
|
|
* Copyright (c) 2006-2018, RT-Thread Development Team
|
2015-01-19 14:18:16 +08:00
|
|
|
*
|
2018-10-14 19:37:18 +08:00
|
|
|
* SPDX-License-Identifier: Apache-2.0
|
2015-01-19 14:18:16 +08:00
|
|
|
*
|
|
|
|
* Change Logs:
|
|
|
|
* Date Author Notes
|
|
|
|
* 2015-01-20 Bernard the first version
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <drivers/pin.h>
|
|
|
|
|
|
|
|
#ifdef RT_USING_FINSH
|
|
|
|
#include <finsh.h>
|
|
|
|
#endif
|
|
|
|
|
|
|
|
static struct rt_device_pin _hw_pin;
|
2015-01-19 16:56:43 +08:00
|
|
|
static rt_size_t _pin_read(rt_device_t dev, rt_off_t pos, void *buffer, rt_size_t size)
|
2015-01-19 14:18:16 +08:00
|
|
|
{
|
2015-01-19 16:56:43 +08:00
|
|
|
struct rt_device_pin_status *status;
|
|
|
|
struct rt_device_pin *pin = (struct rt_device_pin *)dev;
|
2015-01-19 14:18:16 +08:00
|
|
|
|
|
|
|
/* check parameters */
|
|
|
|
RT_ASSERT(pin != RT_NULL);
|
|
|
|
|
2015-01-19 16:56:43 +08:00
|
|
|
status = (struct rt_device_pin_status *) buffer;
|
2015-01-19 14:18:16 +08:00
|
|
|
if (status == RT_NULL || size != sizeof(*status)) return 0;
|
|
|
|
|
|
|
|
status->status = pin->ops->pin_read(dev, status->pin);
|
|
|
|
return size;
|
|
|
|
}
|
|
|
|
|
2015-01-19 16:56:43 +08:00
|
|
|
static rt_size_t _pin_write(rt_device_t dev, rt_off_t pos, const void *buffer, rt_size_t size)
|
2015-01-19 14:18:16 +08:00
|
|
|
{
|
2015-01-19 16:56:43 +08:00
|
|
|
struct rt_device_pin_status *status;
|
|
|
|
struct rt_device_pin *pin = (struct rt_device_pin *)dev;
|
2015-01-19 14:18:16 +08:00
|
|
|
|
|
|
|
/* check parameters */
|
|
|
|
RT_ASSERT(pin != RT_NULL);
|
|
|
|
|
2015-01-19 16:56:43 +08:00
|
|
|
status = (struct rt_device_pin_status *) buffer;
|
2015-01-19 14:18:16 +08:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2017-10-15 22:56:46 +08:00
|
|
|
static rt_err_t _pin_control(rt_device_t dev, int cmd, void *args)
|
2015-01-19 14:18:16 +08:00
|
|
|
{
|
2015-01-19 16:56:43 +08:00
|
|
|
struct rt_device_pin_mode *mode;
|
|
|
|
struct rt_device_pin *pin = (struct rt_device_pin *)dev;
|
2015-01-19 14:18:16 +08:00
|
|
|
|
|
|
|
/* check parameters */
|
|
|
|
RT_ASSERT(pin != RT_NULL);
|
|
|
|
|
2015-01-19 16:56:43 +08:00
|
|
|
mode = (struct rt_device_pin_mode *) args;
|
2015-01-19 14:18:16 +08:00
|
|
|
if (mode == RT_NULL) return -RT_ERROR;
|
|
|
|
|
|
|
|
pin->ops->pin_mode(dev, (rt_base_t)mode->pin, (rt_base_t)mode->mode);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2018-06-10 17:59:17 +08:00
|
|
|
#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
|
|
|
|
|
2015-01-19 16:56:43 +08:00
|
|
|
int rt_device_pin_register(const char *name, const struct rt_pin_ops *ops, void *user_data)
|
2015-01-19 14:18:16 +08:00
|
|
|
{
|
2015-01-19 16:56:43 +08:00
|
|
|
_hw_pin.parent.type = RT_Device_Class_Miscellaneous;
|
|
|
|
_hw_pin.parent.rx_indicate = RT_NULL;
|
|
|
|
_hw_pin.parent.tx_complete = RT_NULL;
|
2015-01-19 14:18:16 +08:00
|
|
|
|
2018-06-10 17:59:17 +08:00
|
|
|
#ifdef RT_USING_DEVICE_OPS
|
|
|
|
_hw_pin.parent.ops = &pin_ops;
|
|
|
|
#else
|
2015-01-19 16:56:43 +08:00
|
|
|
_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;
|
2018-06-10 17:59:17 +08:00
|
|
|
#endif
|
2015-01-19 14:18:16 +08:00
|
|
|
|
|
|
|
_hw_pin.ops = ops;
|
2015-01-19 16:56:43 +08:00
|
|
|
_hw_pin.parent.user_data = user_data;
|
2015-01-19 14:18:16 +08:00
|
|
|
|
2015-01-19 16:56:43 +08:00
|
|
|
/* register a character device */
|
2015-03-23 09:38:09 +08:00
|
|
|
rt_device_register(&_hw_pin.parent, name, RT_DEVICE_FLAG_RDWR);
|
2015-01-19 14:18:16 +08:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2017-03-10 15:07:28 +08:00
|
|
|
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;
|
|
|
|
}
|
2018-06-26 22:18:58 +08:00
|
|
|
rt_err_t rt_pin_detach_irq(rt_int32_t pin)
|
2017-03-10 15:07:28 +08:00
|
|
|
{
|
|
|
|
RT_ASSERT(_hw_pin.ops != RT_NULL);
|
2018-06-26 22:18:58 +08:00
|
|
|
if(_hw_pin.ops->pin_detach_irq)
|
2017-03-10 15:07:28 +08:00
|
|
|
{
|
2018-06-26 22:18:58 +08:00
|
|
|
return _hw_pin.ops->pin_detach_irq(&_hw_pin.parent, pin);
|
2017-03-10 15:07:28 +08:00
|
|
|
}
|
|
|
|
return RT_ENOSYS;
|
|
|
|
}
|
2018-06-10 17:59:17 +08:00
|
|
|
|
2017-11-07 20:29:32 +08:00
|
|
|
rt_err_t rt_pin_irq_enable(rt_base_t pin, rt_uint32_t enabled)
|
2017-03-10 15:07:28 +08:00
|
|
|
{
|
|
|
|
RT_ASSERT(_hw_pin.ops != RT_NULL);
|
|
|
|
if(_hw_pin.ops->pin_irq_enable)
|
|
|
|
{
|
2017-03-14 10:26:49 +08:00
|
|
|
return _hw_pin.ops->pin_irq_enable(&_hw_pin.parent, pin, enabled);
|
2017-03-10 15:07:28 +08:00
|
|
|
}
|
|
|
|
return RT_ENOSYS;
|
|
|
|
}
|
2018-06-10 17:59:17 +08:00
|
|
|
|
2015-01-19 14:18:16 +08:00
|
|
|
/* RT-Thread Hardware PIN APIs */
|
|
|
|
void rt_pin_mode(rt_base_t pin, rt_base_t mode)
|
|
|
|
{
|
2017-03-10 15:07:28 +08:00
|
|
|
RT_ASSERT(_hw_pin.ops != RT_NULL);
|
2015-01-19 14:18:16 +08:00
|
|
|
_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)
|
|
|
|
{
|
2017-03-10 15:07:28 +08:00
|
|
|
RT_ASSERT(_hw_pin.ops != RT_NULL);
|
2015-01-19 14:18:16 +08:00
|
|
|
_hw_pin.ops->pin_write(&_hw_pin.parent, pin, value);
|
|
|
|
}
|
|
|
|
FINSH_FUNCTION_EXPORT_ALIAS(rt_pin_write, pinWrite, write value to hardware pin);
|
|
|
|
|
2015-01-19 16:56:43 +08:00
|
|
|
int rt_pin_read(rt_base_t pin)
|
2015-01-19 14:18:16 +08:00
|
|
|
{
|
2017-03-10 15:07:28 +08:00
|
|
|
RT_ASSERT(_hw_pin.ops != RT_NULL);
|
2015-01-19 14:18:16 +08:00
|
|
|
return _hw_pin.ops->pin_read(&_hw_pin.parent, pin);
|
|
|
|
}
|
|
|
|
FINSH_FUNCTION_EXPORT_ALIAS(rt_pin_read, pinRead, read status from hardware pin);
|
2020-09-11 11:14:53 +08:00
|
|
|
|
|
|
|
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);
|
|
|
|
|