-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmy_gpio.c
271 lines (237 loc) · 4.84 KB
/
my_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
#include "my_gpio.h"
#include <stm32f10x.h>
void enPORT(char port)
{
//enable port
RCC->APB2ENR|=(0x01<<(2+port));
}
GPIO_TypeDef* getGPIO(char port)
{
if(port==0)
return GPIOA;
else if(port==1)
return GPIOB;
else if(port==2)
return GPIOC;
else if(port==3)
return GPIOD;
else if(port==4)
return GPIOE;
else if(port==5)
return GPIOF;
else
return GPIOG;
}
void inCONFIG(char portpin,char mode) //portpin input config
{
char port=portpin/16;
enPORT(port);
GPIO_TypeDef* gpio=getGPIO(port);
char pin=portpin%16;
char shift=(pin%8)*4;
if(pin<8) //first 8 pins
{
gpio->CRL&=~(0x0f<<shift);
gpio->CRL|=(mode<<shift);
/*if(mode==ANA)
{
//already reset which is analog mode
}
else if(mode==FLT)
{
gpio->CRL|=(0x04<<shift);
}
else if(mode==PUD)
{
gpio->CRL|=(0x08<<shift);
}*/
}
else { //last 8 pins
gpio->CRH&=~(0x0f<<shift);
gpio->CRH|=(mode<<shift);
/*if(mode==ANA)
{
//already reset which is analog mode
}
else if(mode==FLT)
{
gpio->CRH|=(FLT<<shift);
}
else if(mode==PUD)
{
gpio->CRH|=(PUD<<shift);
}*/
}
}
void inMODE(char portpin)
{
inCONFIG(portpin,PUD);
}
void inANA(char portpin)
{
inCONFIG(portpin,ANA);
}
void inFLT(char portpin)
{
inCONFIG(portpin,FLT);
}
void outCONFIG(char portpin, char mode) //portpin output config
{
char port=portpin/16;
enPORT(port);
GPIO_TypeDef* gpio=getGPIO(port);
char pin=portpin%16;
char shift=(pin%8)*4;
if(pin<8) //first 8 pins
{
gpio->CRL&=~(0x0f<<shift);
gpio->CRL|=(mode<<shift);
/*if(mode==PP)
{
gpio->CRL|=(PP<<shift);
}
else if(mode==OD)
{
gpio->CRL|=(OD<<shift);
}
else if(mode==APP)
{
gpio->CRL|=(APP<<shift);
}
else if(mode==AOD)
{
gpio->CRL|=(AOD<<shift);
}*/
}
else {
//last 8 pins
gpio->CRH&=~(0x0f<<shift);
gpio->CRH|=(mode<<shift);
/*if(mode==PP)
{
gpio->CRH|=(PP<<shift);
}
else if(mode==OD)
{
gpio->CRH|=(OD<<shift);
}
else if(mode==APP)
{
gpio->CRH|=(APP<<shift);
}
else if(mode==AOD)
{
gpio->CRH|=(AOD<<shift);
}*/
}
if(mode==APP||mode==AOD)
{
RCC->APB2ENR|=0x1; //enable Af clock
}
}
void outMODE(char portpin)
{
outCONFIG(portpin,PP);
}
void outMODE2(char portpin)
{
outCONFIG(portpin,OD);
}
bool pinR(char portpin)
{
char port=portpin/16;
char pin=portpin%16;
GPIO_TypeDef* gpio=getGPIO(port);
return (gpio->IDR&(0x01<<pin));
}
void pinW(char portpin,char mode) //portpin write func
{
char port=portpin/16;
char pin=portpin%16;
GPIO_TypeDef* gpio=getGPIO(port);
if(mode==OFF)
{
gpio->ODR&=~(ON<<pin);
}
else if(mode==ON)
{
gpio->ODR|=(ON<<pin);
}
}
void pinON(char portpin)
{
pinW(portpin,ON);
}
void pinOFF(char portpin)
{
pinW(portpin,OFF);
}
void pinTOGGLE(char portpin)
{
if(pinR(portpin))
{
pinOFF(portpin);
}
else {
pinON(portpin);
}
}
void intCONFIG(char portpin,char trig) //portpin interrupt config
{
inMODE(portpin);
RCC->APB2ENR|=(0x01);
uint16_t port=portpin/16;
uint16_t pin=portpin%16;
__disable_irq();
AFIO->EXTICR[pin/4]&=~( 0x0f<<((pin%4)*4) );
AFIO->EXTICR[pin/4]|=( port<<((pin%4)*4) ); //Enable select source input for EXTI interupt
EXTI->IMR|=(0x01<<pin);
if(trig==RIS)
{
EXTI->RTSR|=(0x01<<pin);
}
else if(trig==FAL)
{
EXTI->FTSR|=(0x01<<pin);
}
if(pin==0)
{
NVIC_EnableIRQ(EXTI0_IRQn);
}
else if (pin==1) {
NVIC_EnableIRQ(EXTI1_IRQn);
}
else if(pin==2)
{
NVIC_EnableIRQ(EXTI2_IRQn);
}
else if (pin==3) {
NVIC_EnableIRQ(EXTI3_IRQn);
}
else if (pin==4) {
NVIC_EnableIRQ(EXTI4_IRQn);
}
else if ((pin>=5) && (pin<=9)) {
NVIC_EnableIRQ(EXTI9_5_IRQn);
}
else if ((pin>=10)&&(pin<=15)) {
NVIC_EnableIRQ(EXTI15_10_IRQn);
}
__enable_irq();
}
void risINT(char portpin)
{
intCONFIG(portpin,RIS);
}
void falINT(char portpin)
{
intCONFIG(portpin,FAL);
}
void outAF(char portpin) //alternate func output push pull mode
{
outCONFIG(portpin,APP);
}
void outOAF(char portpin)
{
outCONFIG(portpin,AOD);
}