rpi_gpio 0.3.0 → 0.5.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,55 +0,0 @@
1
- /*
2
- Original code by Ben Croston modified for Ruby by Nick Lowery
3
- (github.com/clockvapor)
4
- Copyright (c) 2014-2016 Nick Lowery
5
-
6
- Copyright (c) 2013-2015 Ben Croston
7
-
8
- Permission is hereby granted, free of charge, to any person obtaining a copy of
9
- this software and associated documentation files (the "Software"), to deal in
10
- the Software without restriction, including without limitation the rights to
11
- use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
12
- of the Software, and to permit persons to whom the Software is furnished to do
13
- so, subject to the following conditions:
14
-
15
- The above copyright notice and this permission notice shall be included in all
16
- copies or substantial portions of the Software.
17
-
18
- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19
- IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20
- FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21
- AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22
- LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23
- OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
24
- SOFTWARE.
25
- */
26
-
27
- int setup(void);
28
- void setup_gpio(int gpio, int direction, int pud);
29
- int gpio_function(int gpio);
30
- void output_gpio(int gpio, int value);
31
- int input_gpio(int gpio);
32
- void set_rising_event(int gpio, int enable);
33
- void set_falling_event(int gpio, int enable);
34
- void set_high_event(int gpio, int enable);
35
- void set_low_event(int gpio, int enable);
36
- int eventdetected(int gpio);
37
- void cleanup(void);
38
-
39
- #define SETUP_OK 0
40
- #define SETUP_DEVMEM_FAIL 1
41
- #define SETUP_MALLOC_FAIL 2
42
- #define SETUP_MMAP_FAIL 3
43
- #define SETUP_CPUINFO_FAIL 4
44
- #define SETUP_NOT_RPI_FAIL 5
45
-
46
- #define INPUT 1 // is really 0 for control register!
47
- #define OUTPUT 0 // is really 1 for control register!
48
- #define ALT0 4
49
-
50
- #define HIGH 1
51
- #define LOW 0
52
-
53
- #define PUD_OFF 0
54
- #define PUD_DOWN 1
55
- #define PUD_UP 2
@@ -1,97 +0,0 @@
1
- /*
2
- Original code by Ben Croston modified for Ruby by Nick Lowery
3
- (github.com/clockvapor)
4
- Copyright (c) 2014-2016 Nick Lowery
5
-
6
- Copyright (c) 2013-2014 Ben Croston
7
-
8
- Permission is hereby granted, free of charge, to any person obtaining a copy of
9
- this software and associated documentation files (the "Software"), to deal in
10
- the Software without restriction, including without limitation the rights to
11
- use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
12
- of the Software, and to permit persons to whom the Software is furnished to do
13
- so, subject to the following conditions:
14
-
15
- The above copyright notice and this permission notice shall be included in all
16
- copies or substantial portions of the Software.
17
-
18
- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19
- IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20
- FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21
- AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22
- LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23
- OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
24
- SOFTWARE.
25
- */
26
-
27
- #include "ruby.h"
28
- #include "c_gpio.h"
29
- #include "common.h"
30
-
31
- int gpio_mode = MODE_UNKNOWN;
32
- const int pin_to_gpio_rev1[41] = {-1, -1, -1, 0, -1, 1, -1, 4, 14, -1, 15, 17, 18, 21, -1, 22, 23, -1, 24, 10, -1, 9, 25, 11, 8, -1, 7, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 };
33
- const int pin_to_gpio_rev2[41] = {-1, -1, -1, 2, -1, 3, -1, 4, 14, -1, 15, 17, 18, 27, -1, 22, 23, -1, 24, 10, -1, 9, 25, 11, 8, -1, 7, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 };
34
- const int pin_to_gpio_rev3[41] = {-1, -1, -1, 2, -1, 3, -1, 4, 14, -1, 15, 17, 18, 27, -1, 22, 23, -1, 24, 10, -1, 9, 25, 11, 8, -1, 7, -1, -1, 5, -1, 6, 12, 13, -1, 19, 16, 26, 20, -1, 21 };
35
- int setup_error = 0;
36
- int module_setup = 0;
37
-
38
- int check_gpio_priv(void)
39
- {
40
- // check module has been imported cleanly
41
- if (setup_error)
42
- {
43
- rb_raise(rb_eRuntimeError, "gem not imported correctly!");
44
- return 1;
45
- }
46
-
47
- // check mmap setup has worked
48
- if (!module_setup)
49
- {
50
- rb_raise(rb_eRuntimeError, "no access to /dev/mem. Try "
51
- "running as root!");
52
- return 2;
53
- }
54
- return 0;
55
- }
56
-
57
- int get_gpio_number(int channel, unsigned int *gpio)
58
- {
59
- // check setmode() has been run
60
- if (gpio_mode != BOARD && gpio_mode != BCM)
61
- {
62
- rb_raise(rb_eRuntimeError, "please set pin numbering mode "
63
- "using RPi::GPIO.set_numbering :board or "
64
- "RPi::GPIO.set_numbering :bcm");
65
- return 3;
66
- }
67
-
68
- // check channel number is in range
69
- if ( (gpio_mode == BCM && (channel < 0 || channel > 53))
70
- || (gpio_mode == BOARD && (channel < 1 || channel > 26) &&
71
- rpiinfo.p1_revision != 3)
72
- || (gpio_mode == BOARD && (channel < 1 || channel > 40) &&
73
- rpiinfo.p1_revision == 3))
74
- {
75
- rb_raise(rb_eArgError, "the channel sent is invalid on a Raspberry Pi");
76
- return 4;
77
- }
78
-
79
- // convert channel to gpio
80
- if (gpio_mode == BOARD)
81
- {
82
- if (*(*pin_to_gpio+channel) == -1)
83
- {
84
- rb_raise(rb_eArgError, "the channel sent is invalid on a Raspberry "
85
- "Pi");
86
- return 5;
87
- } else {
88
- *gpio = *(*pin_to_gpio+channel);
89
- }
90
- }
91
- else // gpio_mode == BCM
92
- {
93
- *gpio = channel;
94
- }
95
-
96
- return 0;
97
- }
@@ -1,47 +0,0 @@
1
- /*
2
- Original code by Ben Croston modified for Ruby by Nick Lowery
3
- (github.com/clockvapor)
4
- Copyright (c) 2014-2016 Nick Lowery
5
-
6
- Copyright (c) 2013-2015 Ben Croston
7
-
8
- Permission is hereby granted, free of charge, to any person obtaining a copy of
9
- this software and associated documentation files (the "Software"), to deal in
10
- the Software without restriction, including without limitation the rights to
11
- use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
12
- of the Software, and to permit persons to whom the Software is furnished to do
13
- so, subject to the following conditions:
14
-
15
- The above copyright notice and this permission notice shall be included in all
16
- copies or substantial portions of the Software.
17
-
18
- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19
- IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20
- FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21
- AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22
- LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23
- OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
24
- SOFTWARE.
25
- */
26
-
27
- #include "cpuinfo.h"
28
-
29
- #define MODE_UNKNOWN -1
30
- #define BOARD 10
31
- #define BCM 11
32
- #define SERIAL 40
33
- #define SPI 41
34
- #define I2C 42
35
- #define PWM 43
36
-
37
- int gpio_mode;
38
- const int pin_to_gpio_rev1[41];
39
- const int pin_to_gpio_rev2[41];
40
- const int pin_to_gpio_rev3[41];
41
- const int (*pin_to_gpio)[41];
42
- int gpio_direction[54];
43
- rpi_info rpiinfo;
44
- int setup_error;
45
- int module_setup;
46
- int check_gpio_priv(void);
47
- int get_gpio_number(int channel, unsigned int *gpio);
@@ -1,220 +0,0 @@
1
- /*
2
- Original code by Ben Croston modified for Ruby by Nick Lowery
3
- (github.com/clockvapor)
4
- Copyright (c) 2014-2016 Nick Lowery
5
-
6
- Copyright (c) 2013-2016 Ben Croston
7
-
8
- Permission is hereby granted, free of charge, to any person obtaining a copy of
9
- this software and associated documentation files (the "Software"), to deal in
10
- the Software without restriction, including without limitation the rights to
11
- use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
12
- of the Software, and to permit persons to whom the Software is furnished to do
13
- so, subject to the following conditions:
14
-
15
- The above copyright notice and this permission notice shall be included in all
16
- copies or substantial portions of the Software.
17
-
18
- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19
- IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20
- FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21
- AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22
- LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23
- OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
24
- SOFTWARE.
25
- */
26
-
27
- #include <stdio.h>
28
- #include <stdlib.h>
29
- #include <string.h>
30
- #include "cpuinfo.h"
31
-
32
- int get_rpi_info(rpi_info *info)
33
- {
34
- FILE *fp;
35
- char buffer[1024];
36
- char hardware[1024];
37
- char revision[1024];
38
- char *rev;
39
- int found = 0;
40
- int len;
41
-
42
- if ((fp = fopen("/proc/cpuinfo", "r")) == NULL)
43
- return -1;
44
- while(!feof(fp)) {
45
- fgets(buffer, sizeof(buffer), fp);
46
- sscanf(buffer, "Hardware : %s", hardware);
47
- if (strcmp(hardware, "BCM2708") == 0 ||
48
- strcmp(hardware, "BCM2709") == 0 ||
49
- strcmp(hardware, "BCM2835") == 0 ||
50
- strcmp(hardware, "BCM2836") == 0 ||
51
- strcmp(hardware, "BCM2837") == 0 ) {
52
- found = 1;
53
- }
54
- sscanf(buffer, "Revision : %s", revision);
55
- }
56
- fclose(fp);
57
-
58
- if (!found)
59
- return -1;
60
-
61
- if ((len = strlen(revision)) == 0)
62
- return -1;
63
-
64
- if (len >= 6 && strtol((char[]){revision[len-6],0}, NULL, 16) & 8) {
65
- // new scheme
66
- //info->rev = revision[len-1]-'0';
67
- strcpy(info->revision, revision);
68
- switch (revision[len-2]) {
69
- case '0': info->type = "Model A"; info->p1_revision = 2; break;
70
- case '1': info->type = "Model B"; info->p1_revision = 2; break;
71
- case '2': info->type = "Model A+"; info->p1_revision = 3; break;
72
- case '3': info->type = "Model B+"; info->p1_revision = 3; break;
73
- case '4': info->type = "Pi 2 Model B"; info->p1_revision = 3; break;
74
- case '5': info->type = "Alpha"; info->p1_revision = 3; break;
75
- case '6': info->type = "Compute"; info->p1_revision = 0; break;
76
- case '8': info->type = "Pi 3 Model B"; info->p1_revision = 3; break;
77
- case '9': info->type = "Zero"; info->p1_revision = 3; break;
78
- default : info->type = "Unknown"; info->p1_revision = 3; break;
79
- }
80
- switch (revision[len-4]) {
81
- case '0': info->processor = "BCM2835"; break;
82
- case '1': info->processor = "BCM2836"; break;
83
- case '2': info->processor = "BCM2837"; break;
84
- default : info->processor = "Unknown"; break;
85
- }
86
- switch (revision[len-5]) {
87
- case '0': info->manufacturer = "Sony"; break;
88
- case '1': info->manufacturer = "Egoman"; break;
89
- case '2': info->manufacturer = "Embest"; break;
90
- case '4': info->manufacturer = "Embest"; break;
91
- default : info->manufacturer = "Unknown"; break;
92
- }
93
- switch (strtol((char[]){revision[len-6],0}, NULL, 16) & 7) {
94
- case 0: info->ram = "256M"; break;
95
- case 1: info->ram = "512M"; break;
96
- case 2: info->ram = "1024M"; break;
97
- default: info->ram = "Unknown"; break;
98
- }
99
- } else {
100
- // old scheme
101
- info->ram = "Unknown";
102
- info->manufacturer = "Unknown";
103
- info->processor = "Unknown";
104
- info->type = "Unknown";
105
- strcpy(info->revision, revision);
106
-
107
- // get last four characters (ignore preceeding 1000 for overvolt)
108
- if (len > 4)
109
- rev = (char *)&revision+len-4;
110
- else
111
- rev = revision;
112
-
113
- if ((strcmp(rev, "0002") == 0) ||
114
- (strcmp(rev, "0003") == 0)) {
115
- info->type = "Model B";
116
- info->p1_revision = 1;
117
- info->ram = "256M";
118
- info->processor = "BCM2835";
119
- } else if (strcmp(rev, "0004") == 0) {
120
- info->type = "Model B";
121
- info->p1_revision = 2;
122
- info->ram = "256M";
123
- info->manufacturer = "Sony";
124
- info->processor = "BCM2835";
125
- } else if (strcmp(rev, "0005") == 0) {
126
- info->type = "Model B";
127
- info->p1_revision = 2;
128
- info->ram = "256M";
129
- info->manufacturer = "Qisda";
130
- info->processor = "BCM2835";
131
- } else if (strcmp(rev, "0006") == 0) {
132
- info->type = "Model B";
133
- info->p1_revision = 2;
134
- info->ram = "256M";
135
- info->manufacturer = "Egoman";
136
- info->processor = "BCM2835";
137
- } else if (strcmp(rev, "0007") == 0) {
138
- info->type = "Model A";
139
- info->p1_revision = 2;
140
- info->ram = "256M";
141
- info->manufacturer = "Egoman";
142
- info->processor = "BCM2835";
143
- } else if (strcmp(rev, "0008") == 0) {
144
- info->type = "Model A";
145
- info->p1_revision = 2;
146
- info->ram = "256M";
147
- info->manufacturer = "Sony";
148
- info->processor = "BCM2835";
149
- } else if (strcmp(rev, "0009") == 0) {
150
- info->type = "Model A";
151
- info->p1_revision = 2;
152
- info->ram = "256M";
153
- info->manufacturer = "Qisda";
154
- info->processor = "BCM2835";
155
- } else if (strcmp(rev, "000d") == 0) {
156
- info->type = "Model B";
157
- info->p1_revision = 2;
158
- info->ram = "512M";
159
- info->manufacturer = "Egoman";
160
- info->processor = "BCM2835";
161
- } else if (strcmp(rev, "000e") == 0) {
162
- info->type = "Model B";
163
- info->p1_revision = 2;
164
- info->ram = "512M";
165
- info->manufacturer = "Sony";
166
- info->processor = "BCM2835";
167
- } else if (strcmp(rev, "000f") == 0) {
168
- info->type = "Model B";
169
- info->p1_revision = 2;
170
- info->ram = "512M";
171
- info->manufacturer = "Qisda";
172
- info->processor = "BCM2835";
173
- } else if ((strcmp(rev, "0011") == 0) ||
174
- (strcmp(rev, "0014") == 0)) {
175
- info->type = "Compute Module";
176
- info->p1_revision = 0;
177
- info->ram = "512M";
178
- info->processor = "BCM2835";
179
- } else if (strcmp(rev, "0012") == 0) {
180
- info->type = "Model A+";
181
- info->p1_revision = 3;
182
- info->ram = "256M";
183
- info->processor = "BCM2835";
184
- } else if ((strcmp(rev, "0010") == 0) ||
185
- (strcmp(rev, "0013") == 0)) {
186
- info->type = "Model B+";
187
- info->p1_revision = 3;
188
- info->ram = "512M";
189
- info->processor = "BCM2835";
190
- } else { // don't know - assume revision 3 p1 connector
191
- info->p1_revision = 3;
192
- }
193
- }
194
- return 0;
195
- }
196
-
197
- /*
198
-
199
- 32 bits
200
- NEW 23: will be 1 for the new scheme, 0 for the old scheme
201
- MEMSIZE 20: 0=256M 1=512M 2=1G
202
- MANUFACTURER 16: 0=SONY 1=EGOMAN
203
- PROCESSOR 12: 0=2835 1=2836
204
- TYPE 04: 0=MODELA 1=MODELB 2=MODELA+ 3=MODELB+ 4=Pi2 MODEL B 5=ALPHA 6=CM
205
- REV 00: 0=REV0 1=REV1 2=REV2
206
-
207
- pi2 = 1<<23 | 2<<20 | 1<<12 | 4<<4 = 0xa01040
208
-
209
- --------------------
210
-
211
- SRRR MMMM PPPP TTTT TTTT VVVV
212
-
213
- S scheme (0=old, 1=new)
214
- R RAM (0=256, 1=512, 2=1024)
215
- M manufacturer (0='SONY',1='EGOMAN',2='EMBEST',3='UNKNOWN',4='EMBEST')
216
- P processor (0=2835, 1=2836 2=2837)
217
- T type (0='A', 1='B', 2='A+', 3='B+', 4='Pi 2 B', 5='Alpha', 6='Compute Module')
218
- V revision (0-15)
219
-
220
- */
@@ -1,40 +0,0 @@
1
- /*
2
- Original code by Ben Croston modified for Ruby by Nick Lowery
3
- (github.com/clockvapor)
4
- Copyright (c) 2014-2016 Nick Lowery
5
-
6
- Copyright (c) 2013-2016 Ben Croston
7
-
8
- Permission is hereby granted, free of charge, to any person obtaining a copy of
9
- this software and associated documentation files (the "Software"), to deal in
10
- the Software without restriction, including without limitation the rights to
11
- use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
12
- of the Software, and to permit persons to whom the Software is furnished to do
13
- so, subject to the following conditions:
14
-
15
- The above copyright notice and this permission notice shall be included in all
16
- copies or substantial portions of the Software.
17
-
18
- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19
- IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20
- FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21
- AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22
- LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23
- OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
24
- SOFTWARE.
25
- */
26
-
27
- #ifndef CPUINFO_H
28
- #define CPUINFO_H
29
- typedef struct
30
- {
31
- int p1_revision;
32
- char *ram;
33
- char *manufacturer;
34
- char *processor;
35
- char *type;
36
- char revision[1024];
37
- } rpi_info;
38
- #endif /* CPUINFO_H */
39
-
40
- int get_rpi_info(rpi_info *info);