pin.h 3.3 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
/*
 * 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
23
 * 2017-10-20      ZYH          add mode open drain and input pull down
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49
 */

#ifndef PIN_H__
#define PIN_H__

#include <rtthread.h>
#include <rtdevice.h>

#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

50 51 52
#define PIN_IRQ_MODE_RISING             0x00
#define PIN_IRQ_MODE_FALLING            0x01
#define PIN_IRQ_MODE_RISING_FALLING     0x02
53 54
#define PIN_MODE_INPUT_PULLDOWN 0x03
#define PIN_MODE_OUTPUT_OD      0x04
lymzzyh's avatar
lymzzyh 已提交
55

56 57 58
#define PIN_IRQ_DISABLE                 0x00
#define PIN_IRQ_ENABLE                  0x01

59 60
#define PIN_IRQ_PIN_NONE                -1

61 62 63 64 65 66 67 68 69 70
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;
};
71 72
struct rt_pin_irq_hdr
{
73 74
    rt_int16_t        pin;
    rt_uint16_t       mode;
75 76 77
    void (*hdr)(void *args);
    void             *args;
};
78 79
struct rt_pin_ops
{
80 81 82
    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);
83 84

    /* TODO: add GPIO interrupt */
85 86 87
    rt_err_t (*pin_attach_irq)(struct rt_device *device, rt_int32_t pin,
                      rt_uint32_t mode, void (*hdr)(void *args), void *args);
    rt_err_t (*pin_dettach_irq)(struct rt_device *device, rt_int32_t pin);
88
    rt_err_t (*pin_irq_enable)(struct rt_device *device, rt_base_t pin, rt_uint32_t enabled);
89 90
};

91
int rt_device_pin_register(const char *name, const struct rt_pin_ops *ops, void *user_data);
92 93 94

void rt_pin_mode(rt_base_t pin, rt_base_t mode);
void rt_pin_write(rt_base_t pin, rt_base_t value);
95
int  rt_pin_read(rt_base_t pin);
96 97 98
rt_err_t rt_pin_attach_irq(rt_int32_t pin, rt_uint32_t mode,
                             void (*hdr)(void *args), void  *args);
rt_err_t rt_pin_dettach_irq(rt_int32_t pin);
99
rt_err_t pin_irq_enable(rt_base_t pin, rt_uint32_t enabled);
100

101 102
int rt_device_pin_irq_register(const char *name, const struct rt_pin_ops *ops,
                                                              void *user_data);
103 104 105 106 107
#ifdef __cplusplus
}
#endif

#endif