1 /*
2  * Copyright (c) 2006-2023, RT-Thread Development Team
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  *
6  * Change Logs:
7  * Date           Author         Notes
8  * 2020-04-16     bigmagic       first version
9  */
10 
11 #ifndef __DRV_GPIO_H__
12 #define __DRV_GPIO_H__
13 
14 #include <rtthread.h>
15 #include <rtdevice.h>
16 
17 #include "board.h"
18 #include "interrupt.h"
19 
20 struct gpio_irq_def
21 {
22     void  *irq_arg[32];
23     void (*irq_cb[32])(void *param);
24     rt_uint8_t irq_type[32];
25     rt_uint8_t state[32];
26 };
27 
28 #define GPIO_REG_GPFSEL0(BASE)             HWREG32(BASE + 0x00)
29 #define GPIO_REG_GPFSEL1(BASE)             HWREG32(BASE + 0x04)
30 #define GPIO_REG_GPFSEL2(BASE)             HWREG32(BASE + 0x08)
31 #define GPIO_REG_GPFSEL3(BASE)             HWREG32(BASE + 0x0C)
32 #define GPIO_REG_GPFSEL4(BASE)             HWREG32(BASE + 0x10)
33 #define GPIO_REG_GPFSEL5(BASE)             HWREG32(BASE + 0x14)
34 #define GPIO_REG_REV0(BASE)                HWREG32(BASE + 0x18)
35 #define GPIO_REG_GPSET0(BASE)              HWREG32(BASE + 0x1C)
36 #define GPIO_REG_GPSET1(BASE)              HWREG32(BASE + 0x20)
37 #define GPIO_REG_REV1(BASE)                HWREG32(BASE + 0x24)
38 #define GPIO_REG_GPCLR0(BASE)              HWREG32(BASE + 0x28)
39 #define GPIO_REG_GPCLR1(BASE)              HWREG32(BASE + 0x2C)
40 #define GPIO_REG_REV2(BASE)                HWREG32(BASE + 0x30)
41 #define GPIO_REG_GPLEV0(BASE)              HWREG32(BASE + 0x34)
42 #define GPIO_REG_GPLEV1(BASE)              HWREG32(BASE + 0x38)
43 #define GPIO_REG_REV3(BASE)                HWREG32(BASE + 0x3C)
44 #define GPIO_REG_GPEDS0(BASE)              HWREG32(BASE + 0x40)
45 #define GPIO_REG_GPEDS1(BASE)              HWREG32(BASE + 0x44)
46 #define GPIO_REG_REV4(BASE)                HWREG32(BASE + 0x48)
47 #define GPIO_REG_GPREN0(BASE)              HWREG32(BASE + 0x4C)
48 #define GPIO_REG_GPREN1(BASE)              HWREG32(BASE + 0x50)
49 #define GPIO_REG_REV5(BASE)                HWREG32(BASE + 0x54)
50 #define GPIO_REG_GPFEN0(BASE)              HWREG32(BASE + 0x58)
51 #define GPIO_REG_GPFEN1(BASE)              HWREG32(BASE + 0x5C)
52 #define GPIO_REG_REV6(BASE)                HWREG32(BASE + 0x60)
53 #define GPIO_REG_GPHEN0(BASE)              HWREG32(BASE + 0x64)
54 #define GPIO_REG_GPHEN1(BASE)              HWREG32(BASE + 0x68)
55 #define GPIO_REG_REV7(BASE)                HWREG32(BASE + 0x6C)
56 #define GPIO_REG_GPLEN0(BASE)              HWREG32(BASE + 0x70)
57 #define GPIO_REG_GPLEN1(BASE)              HWREG32(BASE + 0x74)
58 #define GPIO_REG_REV8(BASE)                HWREG32(BASE + 0x78)
59 #define GPIO_REG_GPAREN0(BASE)             HWREG32(BASE + 0x7C)
60 #define GPIO_REG_GPAREN1(BASE)             HWREG32(BASE + 0x80)
61 #define GPIO_REG_REV11(BASE)               HWREG32(BASE + 0x84)
62 #define GPIO_REG_GPAFEN0(BASE)             HWREG32(BASE + 0x88)
63 #define GPIO_REG_GPAFEN1(BASE)             HWREG32(BASE + 0x8C)
64 #define GPIO_REG_REV10(BASE)               HWREG32(BASE + 0x90)
65 #define GPIO_REG_GPPUD(BASE)               HWREG32(BASE + 0x94)
66 #define GPIO_REG_GPPUDCLK0(BASE)           HWREG32(BASE + 0x98)
67 #define GPIO_REG_GPPUDCLK1(BASE)           HWREG32(BASE + 0x9C)
68 #define GPIO_REG_REV9(BASE)                HWREG32(BASE + 0xA0)
69 #define GPIO_REG_TEST(BASE)                HWREG32(BASE + 0xA4)
70 #define GPIO_PUP_PDN_CNTRL_REG0(BASE)      HWREG32(BASE + 0xE4)
71 #define GPIO_PUP_PDN_CNTRL_REG1(BASE)      HWREG32(BASE + 0xE8)
72 #define GPIO_PUP_PDN_CNTRL_REG2(BASE)      HWREG32(BASE + 0xEC)
73 #define GPIO_PUP_PDN_CNTRL_REG3(BASE)      HWREG32(BASE + 0xF0)
74 
75 typedef enum {
76     GPIO_PIN_0,
77     GPIO_PIN_1,
78     GPIO_PIN_2,
79     GPIO_PIN_3,
80     GPIO_PIN_4,
81     GPIO_PIN_5,
82     GPIO_PIN_6,
83     GPIO_PIN_7,
84     GPIO_PIN_8,
85     GPIO_PIN_9,
86     GPIO_PIN_10,
87     GPIO_PIN_11,
88     GPIO_PIN_12,
89     GPIO_PIN_13,
90     GPIO_PIN_14,
91     GPIO_PIN_15,
92     GPIO_PIN_16,
93     GPIO_PIN_17,
94     GPIO_PIN_18,
95     GPIO_PIN_19,
96     GPIO_PIN_20,
97     GPIO_PIN_21,
98     GPIO_PIN_22,
99     GPIO_PIN_23,
100     GPIO_PIN_24,
101     GPIO_PIN_25,
102     GPIO_PIN_26,
103     GPIO_PIN_27,
104     GPIO_PIN_28,
105     GPIO_PIN_29,
106     GPIO_PIN_30,
107     GPIO_PIN_31,
108     GPIO_PIN_32,
109     GPIO_PIN_33,
110     GPIO_PIN_34,
111     GPIO_PIN_35,
112     GPIO_PIN_36,
113     GPIO_PIN_37,
114     GPIO_PIN_38,
115     GPIO_PIN_39,
116     GPIO_PIN_40,
117 } GPIO_PIN;
118 
119 typedef enum {
120     INPUT = 0b000,
121     OUTPUT = 0b001,
122     ALT0 = 0b100,
123     ALT1 = 0b101,
124     ALT2 = 0b110,
125     ALT3 = 0b111,
126     ALT4 = 0b011,
127     ALT5 = 0b010
128 } GPIO_FUNC;
129 
130 typedef enum {
131     RASPI_NO_RESISTOR = 0x00,
132     RASPI_PULL_UP = 0x01,
133     RASPI_PULL_DOWN = 0x10
134 } GPIO_PUPD_FUNC;
135 
136 void prev_raspi_pin_mode(GPIO_PIN pin, GPIO_FUNC mode);
137 void prev_raspi_pin_write(GPIO_PIN pin, int pin_value);
138 int rt_hw_gpio_init(void);
139 
140 #endif /* __DRV_GPIO_H__ */
141