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 * 2017-10-20 ZYH add mode open drain and input pull down 10 */ 11 12 #ifndef PIN_H__ 13 #define PIN_H__ 14 15 #include <rtthread.h> 16 #include <rtdevice.h> 17 18 #ifdef __cplusplus 19 extern "C" { 20 #endif 21 22 /* pin device and operations for RT-Thread */ 23 struct rt_device_pin 24 { 25 struct rt_device parent; 26 const struct rt_pin_ops *ops; 27 }; 28 29 #define PIN_LOW 0x00 30 #define PIN_HIGH 0x01 31 32 #define PIN_MODE_OUTPUT 0x00 33 #define PIN_MODE_INPUT 0x01 34 #define PIN_MODE_INPUT_PULLUP 0x02 35 #define PIN_MODE_INPUT_PULLDOWN 0x03 36 #define PIN_MODE_OUTPUT_OD 0x04 37 38 #define PIN_IRQ_MODE_RISING 0x00 39 #define PIN_IRQ_MODE_FALLING 0x01 40 #define PIN_IRQ_MODE_RISING_FALLING 0x02 41 #define PIN_IRQ_MODE_HIGH_LEVEL 0x03 42 #define PIN_IRQ_MODE_LOW_LEVEL 0x04 43 44 #define PIN_IRQ_DISABLE 0x00 45 #define PIN_IRQ_ENABLE 0x01 46 47 #define PIN_IRQ_PIN_NONE -1 48 49 struct rt_device_pin_mode 50 { 51 rt_uint16_t pin; 52 rt_uint16_t mode; 53 }; 54 struct rt_device_pin_status 55 { 56 rt_uint16_t pin; 57 rt_uint16_t status; 58 }; 59 struct rt_pin_irq_hdr 60 { 61 rt_int16_t pin; 62 rt_uint16_t mode; 63 void (*hdr)(void *args); 64 void *args; 65 }; 66 struct rt_pin_ops 67 { 68 void (*pin_mode)(struct rt_device *device, rt_base_t pin, rt_base_t mode); 69 void (*pin_write)(struct rt_device *device, rt_base_t pin, rt_base_t value); 70 int (*pin_read)(struct rt_device *device, rt_base_t pin); 71 72 /* TODO: add GPIO interrupt */ 73 rt_err_t (*pin_attach_irq)(struct rt_device *device, rt_int32_t pin, 74 rt_uint32_t mode, void (*hdr)(void *args), void *args); 75 rt_err_t (*pin_detach_irq)(struct rt_device *device, rt_int32_t pin); 76 rt_err_t (*pin_irq_enable)(struct rt_device *device, rt_base_t pin, rt_uint32_t enabled); 77 }; 78 79 int rt_device_pin_register(const char *name, const struct rt_pin_ops *ops, void *user_data); 80 81 void rt_pin_mode(rt_base_t pin, rt_base_t mode); 82 void rt_pin_write(rt_base_t pin, rt_base_t value); 83 int rt_pin_read(rt_base_t pin); 84 rt_err_t rt_pin_attach_irq(rt_int32_t pin, rt_uint32_t mode, 85 void (*hdr)(void *args), void *args); 86 rt_err_t rt_pin_detach_irq(rt_int32_t pin); 87 rt_err_t rt_pin_irq_enable(rt_base_t pin, rt_uint32_t enabled); 88 89 #ifdef __cplusplus 90 } 91 #endif 92 93 #endif 94