-
Notifications
You must be signed in to change notification settings - Fork 2
/
gpio.c
115 lines (80 loc) · 1.89 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
#include "gpio.h"
#include "mmio.h"
#include <stddef.h>
#include <stdint.h>
void SetPinFunction(gpio_pin gpio, gpio_function function){
switch(gpio / 10){
case 0:
mmio_write(GPIO_BASE + GPFSEL0, ((function) << ((gpio%10)*3)));
break;
case 1:
mmio_write(GPIO_BASE + GPFSEL1, ((function) << ((gpio%10)*3)));
break;
case 2:
mmio_write(GPIO_BASE + GPFSEL2, ((function) << ((gpio%10)*3)));
break;
case 3:
mmio_write(GPIO_BASE + GPFSEL3, ((function) << ((gpio%10)*3)));
break;
case 4:
mmio_write(GPIO_BASE + GPFSEL4, ((function) << ((gpio%10)*3)));
break;
case 5:
mmio_write(GPIO_BASE + GPFSEL5, ((function) << ((gpio%10)*3)));
break;
default:
break;
}
}
void SetGpioOutPut(gpio_pin gpio){
SetPinFunction(gpio, GPIO_OUTPUT);
}
void SetGpioInput(gpio_pin gpio){
SetPinFunction(gpio, GPIO_INPUT);
}
uint32_t getPinValue(gpio_pin gpio){
uint32_t command;
gpio_value result = UNKOWN;
switch(gpio/32){
case 0:
command = GPLEV0;
break;
case 1:
command = GPLEV1;
break;
default:
break;
}
result = mmio_read(GPIO_BASE + command) & (1 << (gpio%32));
if(result != UNKOWN){
if(result){
result = HI;
}
}
return result;
}
void PinToggle(gpio_pin gpio){
if(getPinValue(gpio)){
setPinOff(gpio);
}else{
setPinOn(gpio);
}
}
void setPinOn(gpio_pin gpio){
uint32_t command;
if(gpio/32){
command = GPFSET1;
}else{
command = GPFSET0;
}
mmio_write(GPIO_BASE + command, (1 << (gpio % 32)));
}
void setPinOff(gpio_pin gpio){
uint32_t command;
if(gpio/32){
command = GPCLR1;
}else{
command = GPCLR0;
}
mmio_write(GPIO_BASE + command, (1 << (gpio % 32)));
}