-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgpio.c
265 lines (187 loc) · 6.05 KB
/
gpio.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
////////////////////////////////////////////////
// general peripheral i/o
//
// 2020-04-02 pds initial cut
// 2022-12-02 pds bug fix gpio_out_xor()
//
#include <stdint.h> // for uint32_t
#include <stddef.h> // for size_t
#include "defs.h"
#include "gpio.h"
///////////////////////////////////////////////
//
// low level gpio routines
//
#define gpio_base 0x10012000
#define gpio_iof_en (*(volatile uint32_t *) (gpio_base + 0x38))
// p=0..31; x: 0=gpio, 1=uart/spi/i2c/pwm
#define __gpio_pin_en(p,x) ( gpio_iof_en = (( gpio_iof_en & ~(0x1 << p)) | (( (x) & 0x1) << p)) )
#define gpio_iof_sel (*(volatile uint32_t *) (gpio_base + 0x3C))
// p=0..31; x: 0=uart/spi/i2c, 1=pwm
#define __gpio_pin_sel(p,x) ( gpio_iof_sel = (( gpio_iof_sel & ~(0x1 << p)) | (( (x) & 0x1) << p)) )
//
// GPIO_INPUT_VAL
//
#define gpio_input_val (*(volatile uint32_t *) (gpio_base + 0x00))
#define __gpio_input_val(p) ( (gpio_input_val >> (p)) & 0x1 )
//
// GPIO_INPUT_EN
//
#define gpio_input_en (*(volatile uint32_t *) (gpio_base + 0x04))
#define __gpio_input_en(p,x) ( gpio_input_en = (uint32_t) (( gpio_input_en & ~(0x1 << (p))) | (( (x) & 0x1) << (p))) )
//
// GPIO_OUTPUT_EN
//
#define gpio_output_en (*(volatile uint32_t *) (gpio_base + 0x08))
#define __gpio_output_en(p,x) ( gpio_output_en = (uint32_t) (( gpio_output_en & ~(0x1 << (p))) | (( (x) & 0x1) << (p))) )
//
// GPIO_OUTPUT_VAL
//
#define gpio_output_val (*(volatile uint32_t *) (gpio_base + 0x0C))
#define __gpio_output_val(p,x) ( gpio_output_val = (uint32_t) (( gpio_output_val & ~(0x1UL << (p))) | (( (x) & 0x1UL) << (p))) )
//
// GPIO_PUE
//
#define gpio_pue (*(volatile uint32_t *) (gpio_base + 0x10))
#define __gpio_pue(p,x) ( gpio_pue = (uint32_t) (( gpio_pue & ~(0x1 << (p))) | (( (x) & 0x1) << (p))) )
//
// GPIO_DS
//
#define gpio_ds (*(volatile uint32_t *) (gpio_base + 0x14))
#define __gpio_ds(p,x) ( gpio_ds = (uint32_t) (( gpio_ds & ~(0x1 << (p))) | (( (x) & 0x1) << (p))) )
//
// GPIO_RISE_IE
//
#define gpio_rise_ie (*(volatile uint32_t *) (gpio_base + 0x18))
#define __gpio_rise_ie(p,x) ( gpio_rise_ie = (uint32_t) (( gpio_rise_ie & ~(0x1 << (p))) | (( (x) & 0x1) << (p))) )
//
// GPIO_RISE_IP
//
#define gpio_rise_ip (*(volatile uint32_t *) (gpio_base + 0x1C))
#define __gpio_rise_ip(p,x) ( gpio_rise_ip = (uint32_t) (( gpio_rise_ip & ~(0x1 << (p))) | (( (x) & 0x1) << (p))) )
//
// GPIO_FALL_IE
//
#define gpio_fall_ie (*(volatile uint32_t *) (gpio_base + 0x20))
#define __gpio_fall_ie(p,x) ( gpio_fall_ie = (uint32_t) (( gpio_fall_ie & ~(0x1 << (p))) | (( (x) & 0x1) << (p))) )
//
// GPIO_FALL_IP
//
#define gpio_fall_ip (*(volatile uint32_t *) (gpio_base + 0x24))
#define __gpio_fall_ip(p,x) ( gpio_fall_ip = (uint32_t) (( gpio_fall_ip & ~(0x1 << (p))) | (( (x) & 0x1) << (p))) )
//
// GPIO_HIGH_IE
//
#define gpio_high_ie (*(volatile uint32_t *) (gpio_base + 0x28))
#define __gpio_high_ie(p,x) ( gpio_high_ie = (uint32_t) (( gpio_high_ie & ~(0x1 << (p))) | (( (x) & 0x1) << (p))) )
//
// GPIO_HIGH_IP
//
#define gpio_high_ip (*(volatile uint32_t *) (gpio_base + 0x2C))
#define __gpio_high_ip(p,x) ( gpio_high_ip = (uint32_t) (( gpio_high_ip & ~(0x1 << (p))) | (( (x) & 0x1) << (p))) )
//
// GPIO_LOW_IE
//
#define gpio_low_ie (*(volatile uint32_t *) (gpio_base + 0x30))
#define __gpio_low_ie(p,x) ( gpio_low_ie = (uint32_t) (( gpio_low_ie & ~(0x1 << (p))) | (( (x) & 0x1) << (p))) )
//
// GPIO_LOW_IP
//
#define gpio_low_ip (*(volatile uint32_t *) (gpio_base + 0x34))
#define __gpio_low_ip(p,x) ( gpio_low_ip = (uint32_t) (( gpio_low_ip & ~(0x1 << (p))) | (( (x) & 0x1) << (p))) )
//
// GPIO_IOF_EN
//
#define gpio_iof_en (*(volatile uint32_t *) (gpio_base + 0x38))
// p=0..31; x: 0=gpio, 1=uart/spi/i2c/pwm
#define __gpio_pin_en(p,x) ( gpio_iof_en = (( gpio_iof_en & ~(0x1 << p)) | (( (x) & 0x1) << p)) )
// p=0..31; x: 0=gpio, 1=uart/spi/i2c/pwm
#define __gpio_iof_en(p,x) ( gpio_iof_en = (uint32_t) (( gpio_iof_en & ~(0x1 << (p))) | (( (x) & 0x1) << (p))) )
//
// GPIO_IOF_SEL
//
#define gpio_iof_sel (*(volatile uint32_t *) (gpio_base + 0x3C))
// p=0..31; x: 0=uart/spi/i2c, 1=pwm
#define __gpio_pin_sel(p,x) ( gpio_iof_sel = (( gpio_iof_sel & ~(0x1 << p)) | (( (x) & 0x1) << p)) )
// p=0..31; x: 0=uart/spi/i2c, 1=pwm
#define __gpio_iof_sel(p,x) ( gpio_iof_sel = (uint32_t) (( gpio_iof_sel & ~(0x1 << (p))) | (( (x) & 0x1) << (p))) )
//
// GPIO_OUT_XOR
//
#define gpio_out_xor (*(volatile uint32_t *) (gpio_base + 0x40))
// p=0..31; x: 0=normal, 1=invert
#define __gpio_out_xor(p,x) ( gpio_out_xor = (uint32_t) (( gpio_out_xor & ~(0x1 << (p))) | (( (x) & 0x1) << (p))) )
///////////////////////////////////////////////
//
// high level gpio routines
//
void gpio_init (void)
{
gpio_ds = 0;
gpio_pue = 0;
gpio_out_xor = 0;
gpio_output_en = 0;
gpio_input_en = 0;
}
uint8_t gpio_read (uint8_t g)
{
return __gpio_input_val( g );
}
void gpio_high (uint8_t g)
{
__gpio_output_val( g, 1 ); // High
}
void gpio_low (uint8_t g)
{
__gpio_output_val( g, 0 ); // Low
}
void gpio_dir (uint8_t g, enum gpio_dir_t x)
{
__gpio_ds( g, 0 ); // 1mA
// __gpio_ds( g, 1 ); // 20mA
__gpio_pue( g, 0 ); // open-drain
// __gpio_pue( g, 1 ); // push-pull
// __gpio_out_xor( g, 0 ); // Normal
switch (x)
{
case GPIO_IN:
__gpio_output_en( g, 0 ); // Disable
__gpio_input_en( g, 1 ); // Enable
break;
case GPIO_OUT:
__gpio_input_en( g, 0 ); // Disable
__gpio_output_en( g, 1 ); // Enable
break;
default:
break;
}
}
void gpio_iof (uint8_t g, enum gpio_iof_t x)
{
switch (x)
{
case GPIO_GPIO:
__gpio_iof_en( g, 0 ); // Disable
break;
case GPIO_UART:
case GPIO_SPI:
case GPIO_I2C:
__gpio_iof_sel( g, 0 ); // Uart,Spi,I2c
__gpio_iof_en( g, 1 ); // Enable
break;
case GPIO_PWM:
__gpio_iof_sel( g, 1 ); // PWM
__gpio_iof_en( g, 1 ); // Enable
break;
default:
break;
}
}
void gpio_pin_en (uint8_t g, uint8_t x)
{
__gpio_pin_en( g, x );
}
void gpio_pin_sel (uint8_t g, uint8_t x)
{
__gpio_pin_sel( g, x );
}