sunxi_gpio 0.0.3

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 6c50611c24ebb118b677a76ef041ec5e26a65faf
4
+ data.tar.gz: 7743b9dce6dcabc6ce586d3734c832caa745ce53
5
+ SHA512:
6
+ metadata.gz: 7cda901080750937c67c20de0155891e4694ed3928485bfe168f8030ce798e3a4359dc6ec97809463a1cea422fd49b0c7ccc1e3d3915ae6122221ca0d4d5b955
7
+ data.tar.gz: 9bdc783d08856e7f83ce8ed4c1dd3f4060d8a93bdfe700c4fc239ec19e533019af4772e52a328f538e7dad54077a2fd56375697998865f64593a0e64a14ef44f
data/README.md ADDED
@@ -0,0 +1,45 @@
1
+ sunxi_gpio gem
2
+ ===============
3
+
4
+ Native Ruby Extension to work with Sunxi GPIO. This gem is currently **beta**. It supports writing, reading, watching.
5
+
6
+
7
+
8
+ ## Installtion
9
+
10
+ ```
11
+ gem install sunxi_gpio
12
+ ```
13
+
14
+
15
+ ## Usage
16
+
17
+ ### Simple writing
18
+
19
+ ```ruby
20
+ require 'sunxi_gpio/pin'
21
+
22
+ pin = SunxiGPIO::Pin.new(pin: :PB2, direction: :out)
23
+ pin.on
24
+ sleep 1
25
+ pin.off
26
+ ```
27
+
28
+
29
+ ### Watching
30
+
31
+ ```ruby
32
+ require 'sunxi_gpio/pin'
33
+
34
+ pin = SunxiGPIO::Pin.new(pin: :PB2, direction: :out)
35
+
36
+ pin.watch do
37
+ puts "Pin changed from #{last_value} to #{value}"
38
+ end
39
+ ```
40
+
41
+
42
+ ## Contributors
43
+
44
+ * [phortx](https://github.com/phortx)
45
+ * [happychriss](https://github.com/happychriss)
@@ -0,0 +1,3 @@
1
+ require 'mkmf'
2
+
3
+ create_makefile 'sunxi_gpio'
@@ -0,0 +1,177 @@
1
+ /*
2
+ * gpio_lib.c
3
+ *
4
+ * Copyright 2013 Stefan Mavrodiev <support@olimex.com>
5
+ *
6
+ * This program is free software; you can redistribute it and/or modify
7
+ * it under the terms of the GNU General Public License as published by
8
+ * the Free Software Foundation; either version 2 of the License, or
9
+ * (at your option) any later version.
10
+ *
11
+ * This program is distributed in the hope that it will be useful,
12
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14
+ * GNU General Public License for more details.
15
+ *
16
+ * You should have received a copy of the GNU General Public License
17
+ * along with this program; if not, write to the Free Software
18
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
19
+ * MA 02110-1301, USA.
20
+ */
21
+
22
+
23
+ #include <ctype.h>
24
+ #include <string.h>
25
+ #include <stdlib.h>
26
+ #include <stdio.h>
27
+ #include <math.h>
28
+ #include <time.h>
29
+ #include <signal.h>
30
+ #include <sys/types.h>
31
+ #include <sys/stat.h>
32
+ #include <fcntl.h>
33
+ #include <sys/mman.h>
34
+ #include <sys/select.h>
35
+ #include <pthread.h>
36
+ #include <unistd.h>
37
+ #include <sched.h>
38
+
39
+ #include "gpio_lib.h"
40
+
41
+
42
+ unsigned int SUNXI_PIO_BASE = 0;
43
+ static volatile long int *gpio_map = NULL;
44
+
45
+ int sunxi_gpio_init(void) {
46
+ int fd;
47
+ unsigned int addr_start, addr_offset;
48
+ unsigned int PageSize, PageMask;
49
+
50
+
51
+ fd = open("/dev/mem", O_RDWR);
52
+ if(fd < 0) {
53
+ return SETUP_DEVMEM_FAIL;
54
+ }
55
+
56
+ PageSize = sysconf(_SC_PAGESIZE);
57
+ PageMask = (~(PageSize-1));
58
+
59
+ addr_start = SW_PORTC_IO_BASE & PageMask;
60
+ addr_offset = SW_PORTC_IO_BASE & ~PageMask;
61
+
62
+ gpio_map = (void *)mmap(0, PageSize*2, PROT_READ|PROT_WRITE, MAP_SHARED, fd, addr_start);
63
+ if(gpio_map == MAP_FAILED) {
64
+ return SETUP_MMAP_FAIL;
65
+ }
66
+
67
+ SUNXI_PIO_BASE = (unsigned int)gpio_map;
68
+ SUNXI_PIO_BASE += addr_offset;
69
+
70
+ close(fd);
71
+ return SETUP_OK;
72
+ }
73
+
74
+ int sunxi_gpio_set_cfgpin(unsigned int pin, unsigned int val) {
75
+
76
+ unsigned int cfg;
77
+ unsigned int bank = GPIO_BANK(pin);
78
+ unsigned int index = GPIO_CFG_INDEX(pin);
79
+ unsigned int offset = GPIO_CFG_OFFSET(pin);
80
+
81
+ if(SUNXI_PIO_BASE == 0) {
82
+ return -1;
83
+ }
84
+
85
+ struct sunxi_gpio *pio =
86
+ &((struct sunxi_gpio_reg *)SUNXI_PIO_BASE)->gpio_bank[bank];
87
+
88
+
89
+ cfg = *(&pio->cfg[0] + index);
90
+ cfg &= ~(0xf << offset);
91
+ cfg |= val << offset;
92
+
93
+ *(&pio->cfg[0] + index) = cfg;
94
+
95
+ return 0;
96
+ }
97
+
98
+ int sunxi_gpio_get_cfgpin(unsigned int pin) {
99
+
100
+ unsigned int cfg;
101
+ unsigned int bank = GPIO_BANK(pin);
102
+ unsigned int index = GPIO_CFG_INDEX(pin);
103
+ unsigned int offset = GPIO_CFG_OFFSET(pin);
104
+ if(SUNXI_PIO_BASE == 0)
105
+ {
106
+ return -1;
107
+ }
108
+ struct sunxi_gpio *pio = &((struct sunxi_gpio_reg *)SUNXI_PIO_BASE)->gpio_bank[bank];
109
+ cfg = *(&pio->cfg[0] + index);
110
+ cfg >>= offset;
111
+ return (cfg & 0xf);
112
+ }
113
+ int sunxi_gpio_output(unsigned int pin, unsigned int val) {
114
+
115
+ unsigned int bank = GPIO_BANK(pin);
116
+ unsigned int num = GPIO_NUM(pin);
117
+
118
+ if(SUNXI_PIO_BASE == 0)
119
+ {
120
+ return -1;
121
+ }
122
+ struct sunxi_gpio *pio =&((struct sunxi_gpio_reg *)SUNXI_PIO_BASE)->gpio_bank[bank];
123
+
124
+ if(val)
125
+ *(&pio->dat) |= 1 << num;
126
+ else
127
+ *(&pio->dat) &= ~(1 << num);
128
+
129
+ return 0;
130
+ }
131
+
132
+ int sunxi_gpio_input(unsigned int pin) {
133
+
134
+ unsigned int dat;
135
+ unsigned int bank = GPIO_BANK(pin);
136
+ unsigned int num = GPIO_NUM(pin);
137
+
138
+ if(SUNXI_PIO_BASE == 0)
139
+ {
140
+ return -1;
141
+ }
142
+
143
+ struct sunxi_gpio *pio =&((struct sunxi_gpio_reg *)SUNXI_PIO_BASE)->gpio_bank[bank];
144
+
145
+ dat = *(&pio->dat);
146
+ dat >>= num;
147
+
148
+ return (dat & 0x1);
149
+ }
150
+
151
+ // http://www.cubieforums.com/index.php?topic=2881.15
152
+ int sunxi_gpio_set_pull(unsigned int pin, unsigned int val) {
153
+ unsigned int pull;
154
+ unsigned int bank = GPIO_BANK(pin);
155
+ unsigned int index = GPIO_PULL_INDEX(pin);
156
+ unsigned int offset = GPIO_PULL_OFFSET(pin);
157
+ struct sunxi_gpio *pio = &((struct sunxi_gpio_reg *)SUNXI_PIO_BASE)->gpio_bank[bank];
158
+
159
+ pull = *(&pio->pull[0] + index);
160
+ pull &= ~(0x3 << offset);
161
+ pull |= val << offset;
162
+
163
+ *(&pio->pull[0] + index) = pull;
164
+
165
+ return 0;
166
+ }
167
+
168
+ void sunxi_gpio_cleanup(void)
169
+ {
170
+ unsigned int PageSize;
171
+ if (gpio_map == NULL)
172
+ return;
173
+
174
+ PageSize = sysconf(_SC_PAGESIZE);
175
+ munmap((void*)gpio_map, PageSize*2);
176
+ }
177
+
@@ -0,0 +1,160 @@
1
+ #ifndef _GPIO_LIB_H_
2
+ #define _GPIO_LIB_H_
3
+
4
+
5
+ #define SW_PORTC_IO_BASE 0x01c20800
6
+
7
+
8
+ #define SUNXI_GPIO_A 0
9
+ #define SUNXI_GPIO_B 1
10
+ #define SUNXI_GPIO_C 2
11
+ #define SUNXI_GPIO_D 3
12
+ #define SUNXI_GPIO_E 4
13
+ #define SUNXI_GPIO_F 5
14
+ #define SUNXI_GPIO_G 6
15
+ #define SUNXI_GPIO_H 7
16
+ #define SUNXI_GPIO_I 8
17
+
18
+ #define SETUP_OK 0
19
+ #define SETUP_DEVMEM_FAIL 1
20
+ #define SETUP_MALLOC_FAIL 2
21
+ #define SETUP_MMAP_FAIL 3
22
+
23
+ #define HIGH 1
24
+ #define LOW 0
25
+
26
+ #define INPUT 0
27
+ #define OUTPUT 1
28
+ #define PER 2
29
+
30
+
31
+ struct sunxi_gpio {
32
+ unsigned int cfg[4];
33
+ unsigned int dat;
34
+ unsigned int drv[2];
35
+ unsigned int pull[2];
36
+ };
37
+
38
+ /* gpio interrupt control */
39
+ struct sunxi_gpio_int {
40
+ unsigned int cfg[3];
41
+ unsigned int ctl;
42
+ unsigned int sta;
43
+ unsigned int deb;
44
+ };
45
+
46
+ struct sunxi_gpio_reg {
47
+ struct sunxi_gpio gpio_bank[9];
48
+ unsigned char res[0xbc];
49
+ struct sunxi_gpio_int gpio_int;
50
+ };
51
+
52
+ #define GPIO_BANK(pin) ((pin) >> 5)
53
+ #define GPIO_NUM(pin) ((pin) & 0x1F)
54
+
55
+ #define GPIO_CFG_INDEX(pin) (((pin) & 0x1F) >> 3)
56
+ #define GPIO_CFG_OFFSET(pin) ((((pin) & 0x1F) & 0x7) << 2)
57
+
58
+ #define GPIO_PULL_INDEX(pin) (((pin) & 0x1F) >> 4)
59
+ #define GPIO_PULL_OFFSET(pin) ((((pin) & 0x1F) & 0xf) << 1)
60
+
61
+
62
+ /* GPIO bank sizes */
63
+ #define SUNXI_GPIO_A_NR (32)
64
+ #define SUNXI_GPIO_B_NR (32)
65
+ #define SUNXI_GPIO_C_NR (32)
66
+ #define SUNXI_GPIO_D_NR (32)
67
+ #define SUNXI_GPIO_E_NR (32)
68
+ #define SUNXI_GPIO_F_NR (32)
69
+ #define SUNXI_GPIO_G_NR (32)
70
+ #define SUNXI_GPIO_H_NR (32)
71
+ #define SUNXI_GPIO_I_NR (32)
72
+
73
+ #define SUNXI_GPIO_NEXT(__gpio) ((__gpio##_START)+(__gpio##_NR)+0)
74
+
75
+ enum sunxi_gpio_number {
76
+ SUNXI_GPIO_A_START = 0,
77
+ SUNXI_GPIO_B_START = SUNXI_GPIO_NEXT(SUNXI_GPIO_A), //32
78
+ SUNXI_GPIO_C_START = SUNXI_GPIO_NEXT(SUNXI_GPIO_B), //64
79
+ SUNXI_GPIO_D_START = SUNXI_GPIO_NEXT(SUNXI_GPIO_C), //96
80
+ SUNXI_GPIO_E_START = SUNXI_GPIO_NEXT(SUNXI_GPIO_D), //128
81
+ SUNXI_GPIO_F_START = SUNXI_GPIO_NEXT(SUNXI_GPIO_E), //160
82
+ SUNXI_GPIO_G_START = SUNXI_GPIO_NEXT(SUNXI_GPIO_F), //192
83
+ SUNXI_GPIO_H_START = SUNXI_GPIO_NEXT(SUNXI_GPIO_G), //224
84
+ SUNXI_GPIO_I_START = SUNXI_GPIO_NEXT(SUNXI_GPIO_H) //256
85
+ };
86
+
87
+ /* SUNXI GPIO number definitions */
88
+ #define SUNXI_GPA(_nr) (SUNXI_GPIO_A_START + (_nr))
89
+ #define SUNXI_GPB(_nr) (SUNXI_GPIO_B_START + (_nr))
90
+ #define SUNXI_GPC(_nr) (SUNXI_GPIO_C_START + (_nr))
91
+ #define SUNXI_GPD(_nr) (SUNXI_GPIO_D_START + (_nr))
92
+ #define SUNXI_GPE(_nr) (SUNXI_GPIO_E_START + (_nr))
93
+ #define SUNXI_GPF(_nr) (SUNXI_GPIO_F_START + (_nr))
94
+ #define SUNXI_GPG(_nr) (SUNXI_GPIO_G_START + (_nr))
95
+ #define SUNXI_GPH(_nr) (SUNXI_GPIO_H_START + (_nr))
96
+ #define SUNXI_GPI(_nr) (SUNXI_GPIO_I_START + (_nr))
97
+
98
+ /* GPIO pin function config */
99
+
100
+ #define SUNXI_GPIO_INPUT (0)
101
+ #define SUNXI_GPIO_OUTPUT (1)
102
+ #define SUNXI_GPIO_PER (2)
103
+
104
+ #define SUNXI_GPA0_ERXD3 (2)
105
+ #define SUNXI_GPA0_SPI1_CS0 (3)
106
+ #define SUNXI_GPA0_UART2_RTS (4)
107
+
108
+ #define SUNXI_GPA1_ERXD2 (2)
109
+ #define SUNXI_GPA1_SPI1_CLK (3)
110
+ #define SUNXI_GPA1_UART2_CTS (4)
111
+
112
+ #define SUNXI_GPA2_ERXD1 (2)
113
+ #define SUNXI_GPA2_SPI1_MOSI (3)
114
+ #define SUNXI_GPA2_UART2_TX (4)
115
+
116
+ #define SUNXI_GPA10_UART1_TX (4)
117
+ #define SUNXI_GPA11_UART1_RX (4)
118
+
119
+ #define SUN4I_GPB22_UART0_TX (2)
120
+ #define SUN4I_GPB23_UART0_RX (2)
121
+
122
+ #define SUN5I_GPG3_UART0_TX (4)
123
+ #define SUN5I_GPG4_UART0_RX (4)
124
+
125
+ #define SUNXI_GPC2_NCLE (2)
126
+ #define SUNXI_GPC2_SPI0_CLK (3)
127
+
128
+ #define SUNXI_GPC6_NRB0 (2)
129
+ #define SUNXI_GPC6_SDC2_CMD (3)
130
+
131
+ #define SUNXI_GPC7_NRB1 (2)
132
+ #define SUNXI_GPC7_SDC2_CLK (3)
133
+
134
+ #define SUNXI_GPC8_NDQ0 (2)
135
+ #define SUNXI_GPC8_SDC2_D0 (3)
136
+
137
+ #define SUNXI_GPC9_NDQ1 (2)
138
+ #define SUNXI_GPC9_SDC2_D1 (3)
139
+
140
+ #define SUNXI_GPC10_NDQ2 (2)
141
+ #define SUNXI_GPC10_SDC2_D2 (3)
142
+
143
+ #define SUNXI_GPC11_NDQ3 (2)
144
+ #define SUNXI_GPC11_SDC2_D3 (3)
145
+
146
+ #define SUNXI_GPF2_SDC0_CLK (2)
147
+ #define SUNXI_GPF2_UART0_TX (4)
148
+
149
+ #define SUNXI_GPF4_SDC0_D3 (2)
150
+ #define SUNXI_GPF4_UART0_RX (4)
151
+
152
+ extern int sunxi_gpio_input(unsigned int pin);
153
+ extern int sunxi_gpio_init(void);
154
+ extern int sunxi_gpio_set_cfgpin(unsigned int pin, unsigned int val);
155
+ extern int sunxi_gpio_get_cfgpin(unsigned int pin);
156
+ extern int sunxi_gpio_output(unsigned int pin, unsigned int val);
157
+ extern void sunxi_gpio_cleanup(void);
158
+ extern int sunxi_gpio_set_pull(unsigned int pin, unsigned int val);
159
+ extern unsigned int SUNXI_PIO_BASE;
160
+ #endif