1 /* 2 * Copyright (c) 2006-2018, RT-Thread Development Team 3 * 4 * SPDX-License-Identifier: Apache-2.0 5 * 6 * Change Logs: 7 * Date Author Notes 8 * 2015-01-20 Bernard the first version 9 */ 10 11 #include <drivers/pin.h> 12 13 #ifdef RT_USING_FINSH 14 #include <finsh.h> 15 #endif 16 17 static struct rt_device_pin _hw_pin; 18 static rt_size_t _pin_read(rt_device_t dev, rt_off_t pos, void *buffer, rt_size_t size) 19 { 20 struct rt_device_pin_status *status; 21 struct rt_device_pin *pin = (struct rt_device_pin *)dev; 22 23 /* check parameters */ 24 RT_ASSERT(pin != RT_NULL); 25 26 status = (struct rt_device_pin_status *) buffer; 27 if (status == RT_NULL || size != sizeof(*status)) return 0; 28 29 status->status = pin->ops->pin_read(dev, status->pin); 30 return size; 31 } 32 33 static rt_size_t _pin_write(rt_device_t dev, rt_off_t pos, const void *buffer, rt_size_t size) 34 { 35 struct rt_device_pin_status *status; 36 struct rt_device_pin *pin = (struct rt_device_pin *)dev; 37 38 /* check parameters */ 39 RT_ASSERT(pin != RT_NULL); 40 41 status = (struct rt_device_pin_status *) buffer; 42 if (status == RT_NULL || size != sizeof(*status)) return 0; 43 44 pin->ops->pin_write(dev, (rt_base_t)status->pin, (rt_base_t)status->status); 45 46 return size; 47 } 48 49 static rt_err_t _pin_control(rt_device_t dev, int cmd, void *args) 50 { 51 struct rt_device_pin_mode *mode; 52 struct rt_device_pin *pin = (struct rt_device_pin *)dev; 53 54 /* check parameters */ 55 RT_ASSERT(pin != RT_NULL); 56 57 mode = (struct rt_device_pin_mode *) args; 58 if (mode == RT_NULL) return -RT_ERROR; 59 60 pin->ops->pin_mode(dev, (rt_base_t)mode->pin, (rt_base_t)mode->mode); 61 62 return 0; 63 } 64 65 #ifdef RT_USING_DEVICE_OPS 66 const static struct rt_device_ops pin_ops = 67 { 68 RT_NULL, 69 RT_NULL, 70 RT_NULL, 71 _pin_read, 72 _pin_write, 73 _pin_control 74 }; 75 #endif 76 77 int rt_device_pin_register(const char *name, const struct rt_pin_ops *ops, void *user_data) 78 { 79 _hw_pin.parent.type = RT_Device_Class_Miscellaneous; 80 _hw_pin.parent.rx_indicate = RT_NULL; 81 _hw_pin.parent.tx_complete = RT_NULL; 82 83 #ifdef RT_USING_DEVICE_OPS 84 _hw_pin.parent.ops = &pin_ops; 85 #else 86 _hw_pin.parent.init = RT_NULL; 87 _hw_pin.parent.open = RT_NULL; 88 _hw_pin.parent.close = RT_NULL; 89 _hw_pin.parent.read = _pin_read; 90 _hw_pin.parent.write = _pin_write; 91 _hw_pin.parent.control = _pin_control; 92 #endif 93 94 _hw_pin.ops = ops; 95 _hw_pin.parent.user_data = user_data; 96 97 /* register a character device */ 98 rt_device_register(&_hw_pin.parent, name, RT_DEVICE_FLAG_RDWR); 99 100 return 0; 101 } 102 103 rt_err_t rt_pin_attach_irq(rt_int32_t pin, rt_uint32_t mode, 104 void (*hdr)(void *args), void *args) 105 { 106 RT_ASSERT(_hw_pin.ops != RT_NULL); 107 if(_hw_pin.ops->pin_attach_irq) 108 { 109 return _hw_pin.ops->pin_attach_irq(&_hw_pin.parent, pin, mode, hdr, args); 110 } 111 return RT_ENOSYS; 112 } 113 rt_err_t rt_pin_detach_irq(rt_int32_t pin) 114 { 115 RT_ASSERT(_hw_pin.ops != RT_NULL); 116 if(_hw_pin.ops->pin_detach_irq) 117 { 118 return _hw_pin.ops->pin_detach_irq(&_hw_pin.parent, pin); 119 } 120 return RT_ENOSYS; 121 } 122 123 rt_err_t rt_pin_irq_enable(rt_base_t pin, rt_uint32_t enabled) 124 { 125 RT_ASSERT(_hw_pin.ops != RT_NULL); 126 if(_hw_pin.ops->pin_irq_enable) 127 { 128 return _hw_pin.ops->pin_irq_enable(&_hw_pin.parent, pin, enabled); 129 } 130 return RT_ENOSYS; 131 } 132 133 /* RT-Thread Hardware PIN APIs */ 134 void rt_pin_mode(rt_base_t pin, rt_base_t mode) 135 { 136 RT_ASSERT(_hw_pin.ops != RT_NULL); 137 _hw_pin.ops->pin_mode(&_hw_pin.parent, pin, mode); 138 } 139 FINSH_FUNCTION_EXPORT_ALIAS(rt_pin_mode, pinMode, set hardware pin mode); 140 141 void rt_pin_write(rt_base_t pin, rt_base_t value) 142 { 143 RT_ASSERT(_hw_pin.ops != RT_NULL); 144 _hw_pin.ops->pin_write(&_hw_pin.parent, pin, value); 145 } 146 FINSH_FUNCTION_EXPORT_ALIAS(rt_pin_write, pinWrite, write value to hardware pin); 147 148 int rt_pin_read(rt_base_t pin) 149 { 150 RT_ASSERT(_hw_pin.ops != RT_NULL); 151 return _hw_pin.ops->pin_read(&_hw_pin.parent, pin); 152 } 153 FINSH_FUNCTION_EXPORT_ALIAS(rt_pin_read, pinRead, read status from hardware pin); 154