-
Notifications
You must be signed in to change notification settings - Fork 0
/
GPIO.c
278 lines (214 loc) · 5.63 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
265
266
267
268
269
270
271
272
273
274
275
276
277
#include "GPIO.h"
static void (*EXTI0_Handle)(void);
static void (*EXTI1_Handle)(void);
static void (*EXTI2_Handle)(void);
static void (*EXTI3_Handle)(void);
static void (*EXTI4_Handle)(void);
void gpio_enable_clock(GPIO_TypeDef* port){
uint32_t val;
if(port == GPIOA){
val = RCC_APB2ENR_IOPAEN;
}
else if(port == GPIOB){
val = RCC_APB2ENR_IOPBEN;
}
else if(port == GPIOC){
val = RCC_APB2ENR_IOPCEN;
}
else if(port == GPIOD){
val = RCC_APB2ENR_IOPDEN;
}
else if(port == GPIOE){
val = RCC_APB2ENR_IOPEEN;
}
else if(port == GPIOF){
val = RCC_APB2ENR_IOPFEN;
}
else{
val = RCC_APB2ENR_IOPGEN;
}
RCC->APB2ENR |= val;
}
void gpio_configure_pin_output(GPIO_Object* gpio, uint8_t mode, uint8_t speed){
gpio_enable_clock(gpio->port);
volatile uint32_t* correct_config_register;
uint32_t correct_pin;
if(gpio->pin > 7){
correct_config_register = &gpio->port->CRH;
correct_pin = gpio->pin - 8;
}
else{
correct_config_register = &gpio->port->CRL;
correct_pin = gpio->pin;
}
uint32_t port_start = correct_pin * 4;
if(mode == OUTPUT_GENERAL_PURPOSE_PUSH_PULL){
*correct_config_register &= ~(1 << (port_start + CNF1));
*correct_config_register &= ~(1 << (port_start + CNF0));
}
else if(mode == OUTPUT_GENERAL_PURPOSE_OPEN_DRAIN){
*correct_config_register &= ~(1 << (port_start + CNF1));
*correct_config_register |= (1 << (port_start + CNF0));
}
else if(mode == OUTPUT_ALTERNATE_FUNCTION_PUSH_PULL){
*correct_config_register |= (1 << (port_start + CNF1));
*correct_config_register &= ~(1 << (port_start + CNF0));
}
else if(mode == OUTPUT_ALTERNATE_FUNCTION_OPEN_DRAIN){
*correct_config_register |= (1 << (port_start + CNF1));
*correct_config_register |= (1 << (port_start + CNF0));
}
if(speed == OUTPUT_SPEED_10MHz){
*correct_config_register &= ~(1 << (port_start + MODE1));
*correct_config_register |= (1 << (port_start + MODE0));
}
else if(speed == OUTPUT_SPEED_2MHz){
*correct_config_register |= (1 << (port_start + MODE1));
*correct_config_register &= ~(1 << (port_start + MODE0));
}
else if(speed == OUTPUT_SPEED_50MHz){
*correct_config_register |= (1 << (port_start + MODE1));
*correct_config_register |= (1 << (port_start + MODE0));
}
}
void gpio_write_pin(GPIO_Object* gpio, uint8_t state){
if(state){
gpio->port->BSRR = 1 << gpio->pin;
}
else{
gpio->port->BSRR = 1 << (gpio->pin + 16);
}
}
void gpio_toggle_pin(GPIO_Object* gpio){
gpio->port->ODR ^= 1 << gpio->pin;
}
void gpio_configure_pin_input(GPIO_Object* gpio, uint8_t mode){
gpio_enable_clock(gpio->port);
volatile uint32_t* correct_config_register;
uint32_t correct_pin;
if(gpio->pin > 7){
correct_config_register = &(gpio->port)->CRH;
correct_pin = gpio->pin - 8;
}
else{
correct_config_register = &gpio->port->CRL;
correct_pin = gpio->pin;
}
uint32_t port_start = correct_pin * 4;
if(mode == INPUT_ANALOG){
*correct_config_register &= ~(1 << (port_start + CNF1));
*correct_config_register &= ~(1 << (port_start + CNF0));
}
else if(mode == INPUT_FLOATING){
*correct_config_register &= ~(1 << (port_start + CNF1));
*correct_config_register |= (1 << (port_start + CNF0));
}
else if(mode == INPUT_PULLUP || mode == INPUT_PULLDOWN){
*correct_config_register |= (1 << (port_start + CNF1));
*correct_config_register &= ~(1 << (port_start + CNF0));
if(mode == INPUT_PULLUP){
gpio_write_pin(gpio, 1);
}
else{
gpio_write_pin(gpio, 0);
}
}
*correct_config_register &= ~(1 << (port_start + MODE1));
*correct_config_register &= ~(1 << (port_start + MODE0));
}
uint32_t gpio_read_pin(GPIO_Object* gpio){
return ((gpio->port->IDR & (1 << gpio->pin)) >> gpio->pin);
}
uint32_t gpio_read_port(GPIO_TypeDef* port){
return (port->IDR);
}
void gpio_configure_interrupt(GPIO_Object* gpio, uint32_t edge, void(*handle)(void)){
gpio_enable_clock(gpio->port);
RCC->APB2ENR |= RCC_APB2ENR_AFIOEN;
uint32_t port_value;
if(gpio->port == GPIOA){
port_value = PORTA_VALUE;
}
else if(gpio->port == GPIOB){
port_value = PORTB_VALUE;
}
else if(gpio->port == GPIOC){
port_value = PORTC_VALUE;
}
else if(gpio->port == GPIOD){
port_value = PORTD_VALUE;
}
else if(gpio->port == GPIOE){
port_value = PORTE_VALUE;
}
else if(gpio->port == GPIOF){
port_value = PORTF_VALUE;
}
else {
port_value = PORTG_VALUE;
}
AFIO->EXTICR[gpio->pin/4] |= (port_value << ((gpio->pin%4)*4));
if(edge == RISING_AND_FALLING_EDGES || edge == RISING_EDGE){
EXTI->RTSR |= 1 << gpio->pin;
}
if(edge == RISING_AND_FALLING_EDGES || edge == FALLING_EDGE){
EXTI->FTSR |= 1 << gpio->pin;
}
uint32_t irqn;
if(gpio->pin == 0){
irqn = EXTI0_IRQn;
EXTI0_Handle = handle;
}
else if(gpio->pin == 1){
irqn = EXTI1_IRQn;
EXTI1_Handle = handle;
}
else if(gpio->pin == 2){
irqn = EXTI2_IRQn;
EXTI2_Handle = handle;
}
else if(gpio->pin == 3){
irqn = EXTI3_IRQn;
EXTI3_Handle = handle;
}
else if(gpio->pin == 4){
irqn = EXTI4_IRQn;
EXTI4_Handle = handle;
}
else if(gpio->pin <= 9 && gpio->pin >= 5){
irqn = EXTI9_5_IRQn;
}
else {
irqn = EXTI15_10_IRQn;
}
NVIC_EnableIRQ(irqn);
}
void gpio_enable_interrupt(uint32_t pin){
EXTI->IMR |= 1 << pin;
}
void gpio_clear_interrupt(uint32_t pin){
EXTI->PR |= 1 << pin;
}
void gpio_generate_software_interrupt(uint32_t pin){
EXTI->SWIER |= (1 << pin);
}
void EXTI0_IRQHandler(void){
gpio_clear_interrupt(0);
EXTI0_Handle();
}
void EXTI1_IRQHandler(void){
gpio_clear_interrupt(1);
EXTI1_Handle();
}
void EXTI2_IRQHandler(void){
gpio_clear_interrupt(2);
EXTI2_Handle();
}
void EXTI3_IRQHandler(void){
gpio_clear_interrupt(3);
EXTI3_Handle();
}
void EXTI4_IRQHandler(void){
gpio_clear_interrupt(4);
EXTI4_Handle();
}