-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathGPIO_prog.c
More file actions
334 lines (218 loc) · 5.39 KB
/
GPIO_prog.c
File metadata and controls
334 lines (218 loc) · 5.39 KB
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
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
#include "GPIO_Int.h"
#include "C:/Keil/EE319KwareSpring2020/inc/tm4c123gh6pm.h"
#define u8 unsigned char
#define assignBits(var,Bno,val) do{if(val==1) ((var)|= (1 << (Bno))); \
else if(val==0) ((var)&= ~(1 << (Bno))); \
}while(0)
#define getBit(PIN,Pin) (PIN=(PIN & (1<<Pin)))
void GPIO_init(u8 Port){ //enable LOCK , enable registers writing
switch(Port){
case Port_A:
GPIO_PORTA_LOCK_R = 0x4C4F434B;
GPIO_PORTA_CR_R=0xFF; //enable registers writing
GPIO_PORTA_AFSEL_R = 0; //no alternative functions
GPIO_PORTA_PCTL_R = 0; // no alternative functions
GPIO_PORTA_AMSEL_R = 0; // analog function disabled
GPIO_PORTA_DEN_R= 0xFF; // digital enable
GPIO_PORTA_PUR_R = 255;
break;
case Port_B:
GPIO_PORTB_LOCK_R = 0x4C4F434B;
GPIO_PORTB_CR_R=0xFF; //enable registers writing
GPIO_PORTB_AFSEL_R = 0; //no alternative functions
GPIO_PORTB_PCTL_R = 0; // no alternative functions
GPIO_PORTB_AMSEL_R = 0; // analog function disabled
GPIO_PORTB_DEN_R= 0xFF; // digital enable
GPIO_PORTB_PUR_R = 255;
break;
case Port_D:
GPIO_PORTD_LOCK_R = 0x4C4F434B;
GPIO_PORTD_CR_R=0xFF; //enable registers writing
GPIO_PORTD_AFSEL_R = 0; //no alternative functions
GPIO_PORTD_PCTL_R = 0; // no alternative functions
GPIO_PORTD_AMSEL_R = 0; // analog function disabled
GPIO_PORTD_DEN_R= 0xFF; // digital enable
GPIO_PORTD_PUR_R = 255;
break;
case Port_E:
GPIO_PORTE_LOCK_R = 0x4C4F434B;
GPIO_PORTE_CR_R=0xFF; //enable registers writing
GPIO_PORTE_AFSEL_R = 0; //no alternative functions
GPIO_PORTE_PCTL_R = 0; // no alternative functions
GPIO_PORTE_AMSEL_R = 0; // analog function disabled
GPIO_PORTE_DEN_R= 0xFF; // digital enable
GPIO_PORTE_PUR_R = 255;
break;
case Port_F:
GPIO_PORTF_LOCK_R = 0x4C4F434B;
GPIO_PORTF_CR_R=0xFF; //enable registers writing
GPIO_PORTF_AFSEL_R = 0; //no alternative functions
GPIO_PORTF_PCTL_R = 0; // no alternative functions
GPIO_PORTF_AMSEL_R = 0; // analog function disabled
GPIO_PORTF_DEN_R= 0xFF; // digital enable
GPIO_PORTF_PUR_R = 255;
break;
}
}
u8 GPIO_setPinValue(u8 Port , u8 Pin , u8 value){
if(Port>=0 && Port<=5 && Pin>=0 && Pin<=7){
switch(Port){
case Port_A:
assignBits(GPIO_PORTA_DATA_R,Pin,value);
break;
case Port_B:
assignBits(GPIO_PORTB_DATA_R,Pin,value);
break;
case Port_C:
assignBits(GPIO_PORTC_DATA_R,Pin,value);
break;
case Port_D:
assignBits(GPIO_PORTD_DATA_R,Pin,value);
break;
case Port_E:
assignBits(GPIO_PORTE_DATA_R,Pin,value);
break;
case Port_F:
assignBits(GPIO_PORTF_DATA_R,Pin,value);
break;
}
return 0;
}
return 1;
}
u8 GPIO_setPullUpResistor(u8 Port , u8 value){
if(Port>=0 && Port<=5){
switch(Port){
case Port_A:
GPIO_PORTA_PUR_R = value;
break;
case Port_B:
GPIO_PORTB_PUR_R = value;
break;
case Port_C:
GPIO_PORTC_PUR_R = value;
break;
case Port_D:
GPIO_PORTD_PUR_R = value;
break;
case Port_E:
GPIO_PORTE_PUR_R = value;
break;
case Port_F:
GPIO_PORTF_PUR_R = value;
break;
}
return 0;
}
return 1;
}
u8 GPIO_getPinValue(u8 Port , u8 Pin , u8* level){
if(Port>=0 && Port<=5 && Pin>=0 && Pin<=7){
switch(Port){
case Port_A:
getBit(GPIO_PORTA_DATA_R,Pin);
return GPIO_PORTA_DATA_R;
break;
case Port_B:
getBit(GPIO_PORTB_DATA_R,Pin);
*level = GPIO_PORTB_DATA_R;
break;
case Port_C:
getBit(GPIO_PORTC_DATA_R,Pin);
*level = GPIO_PORTC_DATA_R;
break;
case Port_D:
getBit(GPIO_PORTD_DATA_R,Pin);
*level = GPIO_PORTD_DATA_R;
break;
case Port_E:
getBit(GPIO_PORTE_DATA_R,Pin);
*level = GPIO_PORTE_DATA_R;
break;
case Port_F:
getBit(GPIO_PORTF_DATA_R,Pin);
*level = GPIO_PORTF_DATA_R;
break;
}
return 0;
}
return 1;
}
u8 GPIO_setPinDirection(u8 Port , u8 Pin , u8 direction){
if(Port>=0 && Port<=5 && Pin>=0 && Pin<=7){
switch(Port){
case Port_A:
assignBits(GPIO_PORTA_DIR_R,Pin,direction);
break;
case Port_B:
assignBits(GPIO_PORTB_DIR_R,Pin,direction);
break;
case Port_C:
assignBits(GPIO_PORTC_DIR_R,Pin,direction);
break;
case Port_D:
assignBits(GPIO_PORTD_DIR_R,Pin,direction);
break;
case Port_E:
assignBits(GPIO_PORTE_DIR_R,Pin,direction);
break;
case Port_F:
assignBits(GPIO_PORTF_DIR_R,Pin,direction);
break;
}
return 0;
}
return 1;
}
u8 GPIO_setPortDirection(u8 Port , u8 directions){
if(Port>=0 && Port<=5){
switch(Port){
case Port_A:
GPIO_PORTA_DIR_R = directions;
break;
case Port_B:
GPIO_PORTB_DIR_R = directions;
break;
case Port_C:
GPIO_PORTC_DIR_R = directions;
break;
case Port_D:
GPIO_PORTD_DIR_R = directions;
break;
case Port_E:
GPIO_PORTE_DIR_R = directions;
break;
case Port_F:
GPIO_PORTF_DIR_R = directions;
break;
}
return 0;
}
return 1;
}
u8 GPIO_setPortValue(u8 Port , u8 value){
if(Port>=0 && Port<=5){
switch(Port){
case Port_A:
GPIO_PORTA_DATA_R = value;
break;
case Port_B:
GPIO_PORTB_DATA_R = value;
break;
case Port_C:
GPIO_PORTC_DATA_R = value;
break;
case Port_D:
GPIO_PORTD_DATA_R = value;
break;
case Port_E:
GPIO_PORTE_DATA_R = value;
break;
case Port_F:
GPIO_PORTF_DATA_R = value;
break;
}
return 0;
}
return 1;
}