-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgpio_old.c
127 lines (102 loc) · 4.5 KB
/
gpio_old.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
/*
* gpio.c
*
* Created on: 31 okt. 2017
* Author: Kenneth
*/
#include <fcntl.h>
#include <errno.h>
#include <stdint.h>
#include <stdio.h>
#include <sys/mman.h>
#include "gpio_defines.h"
static volatile unsigned int *gpio;
// gpio register handling
// Input handling does not work,
#define GPIO_SET_FUNC(pin, func) *(gpio + (pin/10)) = ( *(gpio + (pin/10)) & ~(7 << ((pin%10)*3)) ) | (func << ((pin%10)*3))
#define GPIO_SET(pin) *(gpio + REG_OFFSET_GPIO_SET + (pin/32)) |= (1 << (pin%31))
#define GPIO_CLEAR(pin) *(gpio + REG_OFFSET_GPIO_CLEAR + (pin/32)) |= (1 << (pin%31))
#define GPIO_READ(pin) (*(gpio + REG_OFFSET_GPIO_READ + (pin/32)) &= (1 << (pin%31))) >> (pin%31)
#define GPIO_EVENT(pin) (*(gpio + REG_OFFSET_GPIO_EVENT + (pin/32)) &= (1 << (pin%31))) >> (pin%31)
#define GPIO_REDGE_EN(pin, enable) *(gpio + REG_OFFSET_GPIO_REDGE_EN + (pin/32)) &= ( ~(1 << (pin%31)) | (enable << ((pin%31))) )
#define GPIO_FEDGE_EN(pin, enable) *(gpio + REG_OFFSET_GPIO_FEDGE_EN + (pin/32)) &= ( ~(1 << (pin%31)) | (enable << ((pin%31))) )
#define GPIO_HLEV_EN(pin, enable) *(gpio + REG_OFFSET_GPIO_HLEV_EN + (pin/32)) &= ( ~(1 << (pin%31)) | (enable << ((pin%31))) )
#define GPIO_LLEV_EN(pin, enable) *(gpio + REG_OFFSET_GPIO_LLEV_EN + (pin/32)) &= ( ~(1 << (pin%31)) | (enable << ((pin%31))) )
#define GPIO_AREDGE_EN(pin, enable) *(gpio + REG_OFFSET_GPIO_AREDGE_EN + (pin/32)) &= ( ~(1 << (pin%31)) | (enable << ((pin%31))) )
#define GPIO_AFEDGE_EN(pin, enable) *(gpio + REG_OFFSET_GPIO_AFEDGE_EN + (pin/32)) &= ( ~(1 << (pin%31)) | (enable << ((pin%31))) )
// see BCM2835 spec to see how to use this, it's probably better to set multiple pins in one command --> TODO
#define GPIO_PULL_CNTRL(pull) *(gpio + REG_OFFSET_GPIO_PULL_CNTRL) = pull
#define GPIO_PULL_CLK(pin) *(gpio + REG_OFFSET_GPIO_EN_CLK + (pin/32)) |= (1 << pin%31)
int main(int argc, char **argv)
{
int fd, i;
static unsigned char k = 0;
// obtain handle to physical memory
if ( (fd = open("/dev/mem", O_RDWR | O_SYNC)) < 0 )
{
printf("Unable to open /dev/mem: %s\n", strerror(errno));
return -1;
}
// map physical memory to virtual memory
printf("The pagesize is: %x\n", getpagesize());
gpio = (uint32_t*)mmap(0, getpagesize(), PROT_READ | PROT_WRITE, MAP_SHARED, fd, GPIO_BASE_ADDRESS);
if ( ((int32_t)gpio) < 0)
{
printf("mmap failed: %s\n", strerror(errno));
return -1;
}
// Initialize io registers to outputs
for (i = 0; i < 30; i++)
{
GPIO_SET_FUNC(i, FSEL_INPUT);
}
//maybe wait
sleep(5);
// Set pull downs
GPIO_PULL_CNTRL(PULL_DOWN);
printf("Set pull cntrl reg, have to wait 150 cycles\n");
usleep(200);
*(gpio + REG_OFFSET_GPIO_EN_CLK) = (0xFFFFFFFF);
printf("Set pull clock reg, have to wait 150 cycles\n");
usleep(200);
GPIO_PULL_CNTRL(PULL_NONE);
*(gpio + REG_OFFSET_GPIO_EN_CLK) = (0);
// print function register
printf("gpio function select 0 address: %x\n", gpio); // 76FF3000
printf("gpio function select 0 reg: %x\n", *gpio);
printf("gpio function select 1 address: %x\n", gpio+1); // 76FF3000
printf("gpio function select 1 reg: %x\n", *(gpio+1));
printf("gpio function select 2 address: %x\n", gpio+2); // 76FF3000
printf("gpio function select 2 reg: %x\n", *(gpio+2));
printf("gpio function select 3 address: %x\n", gpio+3); // 76FF3000
printf("gpio function select 3 reg: %x\n", *(gpio+3));
printf("gpio function select 4 address: %x\n", gpio+4); // 76FF3000
printf("gpio function select 4 reg: %x\n", *(gpio+4));
printf("gpio function select 5 address: %x\n", gpio+5); // 76FF3000
printf("gpio function select 5 reg: %x\n", *(gpio+5));
// print set register
printf("gpio set address: %x\n", (gpio+REG_OFFSET_GPIO_SET)); // 76FF301C
printf("gpio set reg: %x\n", *(gpio+REG_OFFSET_GPIO_SET));
// print clear register
printf("gpio clear address: %x\n", (gpio+REG_OFFSET_GPIO_CLEAR)); // 76FF3028
printf("gpio clear reg: %x\n", *(gpio+REG_OFFSET_GPIO_CLEAR));
// print read register
printf("gpio read address: %x\n", (gpio+REG_OFFSET_GPIO_READ)); // 76FF3034
printf("gpio read reg: %x\n", *(gpio+REG_OFFSET_GPIO_READ));
// print event register
printf("gpio event address: %x\n", (gpio+REG_OFFSET_GPIO_EVENT)); // 76FF3040
printf("pgio event reg: %x\n", *(gpio+REG_OFFSET_GPIO_EVENT));
fflush(stdout);
// do something forever
while(1)
{
static int k = 1;
//for(i=0; i<30; i++)
{
printf("GPIO pin %d: %s\n", 4, (GPIO_READ(4))?"ON":"OFF");
}
// Check the entire IO reg
printf("The whole read reg: %x\n", ((uint32_t)*(gpio + REG_OFFSET_GPIO_READ))); //2ffaebef 10051410
sleep(1);
}
}