-
Notifications
You must be signed in to change notification settings - Fork 6
/
gpio_lib.c
159 lines (124 loc) · 3.69 KB
/
gpio_lib.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
/*
* gpio_lib.c
*
* Copyright 2013 Stefan Mavrodiev <support@olimex.com>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
* MA 02110-1301, USA.
*/
#include <ctype.h>
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <math.h>
#include <time.h>
#include <signal.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <sys/mman.h>
#include <sys/select.h>
#include <pthread.h>
#include <unistd.h>
#include <sched.h>
#include "gpio_lib.h"
unsigned int SUNXI_PIO_BASE = 0;
static volatile long int *gpio_map = NULL;
int sunxi_gpio_init(void) {
int fd;
unsigned int addr_start, addr_offset;
unsigned int PageSize, PageMask;
fd = open("/dev/mem", O_RDWR);
if(fd < 0) {
return SETUP_DEVMEM_FAIL;
}
PageSize = sysconf(_SC_PAGESIZE);
PageMask = (~(PageSize-1));
addr_start = SW_PORTC_IO_BASE & PageMask;
addr_offset = SW_PORTC_IO_BASE & ~PageMask;
gpio_map = (void *)mmap(0, PageSize*2, PROT_READ|PROT_WRITE, MAP_SHARED, fd, addr_start);
if(gpio_map == MAP_FAILED) {
return SETUP_MMAP_FAIL;
}
SUNXI_PIO_BASE = (unsigned int)gpio_map;
SUNXI_PIO_BASE += addr_offset;
close(fd);
return SETUP_OK;
}
int sunxi_gpio_set_cfgpin(unsigned int pin, unsigned int val) {
unsigned int cfg;
unsigned int bank = GPIO_BANK(pin);
unsigned int index = GPIO_CFG_INDEX(pin);
unsigned int offset = GPIO_CFG_OFFSET(pin);
if(SUNXI_PIO_BASE == 0) {
return -1;
}
struct sunxi_gpio *pio =
&((struct sunxi_gpio_reg *)SUNXI_PIO_BASE)->gpio_bank[bank];
cfg = *(&pio->cfg[0] + index);
cfg &= ~(0xf << offset);
cfg |= val << offset;
*(&pio->cfg[0] + index) = cfg;
return 0;
}
int sunxi_gpio_get_cfgpin(unsigned int pin) {
unsigned int cfg;
unsigned int bank = GPIO_BANK(pin);
unsigned int index = GPIO_CFG_INDEX(pin);
unsigned int offset = GPIO_CFG_OFFSET(pin);
if(SUNXI_PIO_BASE == 0)
{
return -1;
}
struct sunxi_gpio *pio = &((struct sunxi_gpio_reg *)SUNXI_PIO_BASE)->gpio_bank[bank];
cfg = *(&pio->cfg[0] + index);
cfg >>= offset;
return (cfg & 0xf);
}
int sunxi_gpio_output(unsigned int pin, unsigned int val) {
unsigned int bank = GPIO_BANK(pin);
unsigned int num = GPIO_NUM(pin);
if(SUNXI_PIO_BASE == 0)
{
return -1;
}
struct sunxi_gpio *pio =&((struct sunxi_gpio_reg *)SUNXI_PIO_BASE)->gpio_bank[bank];
if(val)
*(&pio->dat) |= 1 << num;
else
*(&pio->dat) &= ~(1 << num);
return 0;
}
int sunxi_gpio_input(unsigned int pin) {
unsigned int dat;
unsigned int bank = GPIO_BANK(pin);
unsigned int num = GPIO_NUM(pin);
if(SUNXI_PIO_BASE == 0)
{
return -1;
}
struct sunxi_gpio *pio =&((struct sunxi_gpio_reg *)SUNXI_PIO_BASE)->gpio_bank[bank];
dat = *(&pio->dat);
dat >>= num;
return (dat & 0x1);
}
void sunxi_gpio_cleanup(void)
{
unsigned int PageSize;
if (gpio_map == NULL)
return;
PageSize = sysconf(_SC_PAGESIZE);
munmap((void*)gpio_map, PageSize*2);
}