rpi_gpio 0.2.0 → 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,53 +1,55 @@
1
- /*
2
- Original code by Ben Croston modified for Ruby by Nick Lowery
3
- (github.com/clockvapor)
4
- Copyright (c) 2014-2015 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
- 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
-
44
- #define INPUT 1 // is really 0 for control register!
45
- #define OUTPUT 0 // is really 1 for control register!
46
- #define ALT0 4
47
-
48
- #define HIGH 1
49
- #define LOW 0
50
-
51
- #define PUD_OFF 0
52
- #define PUD_DOWN 1
53
- #define PUD_UP 2
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,101 +1,97 @@
1
- /*
2
- Original code by Ben Croston modified for Ruby by Nick Lowery
3
- (github.com/clockvapor)
4
- Copyright (c) 2014-2015 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,
33
- 18, 21, -1, 22, 23, -1, 24, 10, -1, 9, 25, 11, 8, -1, 7, -1, -1, -1, -1, -1,
34
- -1, -1, -1, -1, -1, -1, -1, -1, -1 };
35
- const int pin_to_gpio_rev2[41] = {-1, -1, -1, 2, -1, 3, -1, 4, 14, -1, 15, 17,
36
- 18, 27, -1, 22, 23, -1, 24, 10, -1, 9, 25, 11, 8, -1, 7, -1, -1, -1, -1, -1,
37
- -1, -1, -1, -1, -1, -1, -1, -1, -1 };
38
- const int pin_to_gpio_rev3[41] = {-1, -1, -1, 2, -1, 3, -1, 4, 14, -1, 15, 17,
39
- 18, 27, -1, 22, 23, -1, 24, 10, -1, 9, 25, 11, 8, -1, 7, -1, -1, 5, -1, 6, 12,
40
- 13, -1, 19, 16, 26, 20, -1, 21 };
41
- int setup_error = 0;
42
- int module_setup = 0;
43
-
44
- int check_gpio_priv(void)
45
- {
46
- // check module has been imported cleanly
47
- if (setup_error)
48
- {
49
- rb_raise(rb_eRuntimeError, "gem not imported correctly!");
50
- return 1;
51
- }
52
-
53
- // check mmap setup has worked
54
- if (!module_setup)
55
- {
56
- rb_raise(rb_eRuntimeError, "no access to /dev/mem. Try "
57
- "running as root!");
58
- return 2;
59
- }
60
- return 0;
61
- }
62
-
63
- int get_gpio_number(int channel, unsigned int *gpio)
64
- {
65
- // check setmode() has been run
66
- if (gpio_mode != BOARD && gpio_mode != BCM)
67
- {
68
- rb_raise(rb_eRuntimeError, "please set pin numbering mode "
69
- "using RPi::GPIO.set_numbering :board or "
70
- "RPi::GPIO.set_numbering :bcm");
71
- return 3;
72
- }
73
-
74
- // check channel number is in range
75
- if ( (gpio_mode == BCM && (channel < 0 || channel > 53))
76
- || (gpio_mode == BOARD && (channel < 1 || channel > 26) &&
77
- rpiinfo.p1_revision != 3)
78
- || (gpio_mode == BOARD && (channel < 1 || channel > 40) &&
79
- rpiinfo.p1_revision == 3))
80
- {
81
- rb_raise(rb_eArgError, "the channel sent is invalid on a Raspberry Pi");
82
- return 4;
83
- }
84
-
85
- // convert channel to gpio
86
- if (gpio_mode == BOARD)
87
- {
88
- if (*(*pin_to_gpio+channel) == -1)
89
- {
90
- rb_raise(rb_eArgError, "the channel sent is invalid on a Raspberry "
91
- "Pi");
92
- return 5;
93
- } else {
94
- *gpio = *(*pin_to_gpio+channel);
95
- }
96
- }
97
- else // gpio_mode == BCM
98
- *gpio = channel;
99
-
100
- return 0;
101
- }
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 +1,47 @@
1
- /*
2
- Original code by Ben Croston modified for Ruby by Nick Lowery
3
- (github.com/clockvapor)
4
- Copyright (c) 2014-2015 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 "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
+ /*
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,159 +1,220 @@
1
- /*
2
- Original code by Ben Croston modified for Ruby by Nick Lowery
3
- (github.com/clockvapor)
4
- Copyright (c) 2014-2015 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 <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
- }
45
- while(!feof(fp)) {
46
- fgets(buffer, sizeof(buffer) , fp);
47
- sscanf(buffer, "Hardware : %s", hardware);
48
- if (strcmp(hardware, "BCM2708") == 0 ||
49
- strcmp(hardware, "BCM2709") == 0 ||
50
- strcmp(hardware, "BCM2835") == 0 ||
51
- strcmp(hardware, "BCM2836") == 0) {
52
- found = 1;
53
- }
54
- sscanf(buffer, "Revision : %s", revision);
55
- }
56
- fclose(fp);
57
-
58
- if (!found) {
59
- return -1;
60
- }
61
-
62
- if ((len = strlen(revision)) == 0) {
63
- return -1;
64
- }
65
-
66
- if (len >= 6 && strtol((char[]){revision[len-6],0}, NULL, 16) & 8) {
67
- // new scheme
68
- //info->rev = revision[len-1]-'0';
69
- strcpy(info->revision, revision);
70
- switch (revision[len-2]) {
71
- case '0': info->type = "Model A"; info->p1_revision = 2; break;
72
- case '1': info->type = "Model B"; info->p1_revision = 2; break;
73
- case '2': info->type = "Model A+"; info->p1_revision = 3; break;
74
- case '3': info->type = "Model B+"; info->p1_revision = 3; break;
75
- case '4': info->type = "Pi2 Model B"; info->p1_revision = 3; break;
76
- case '5': info->type = "Alpha"; info->p1_revision = 3; break;
77
- case '6': info->type = "Compute"; info->p1_revision = 0; 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
- default : info->processor = "Unknown"; break;
84
- }
85
- switch (revision[len-5]) {
86
- case '0': info->manufacturer = "Sony"; break;
87
- case '1': info->manufacturer = "Egoman"; break;
88
- case '2': info->manufacturer = "Embest"; break;
89
- case '4': info->manufacturer = "Embest"; break;
90
- default : info->manufacturer = "Unknown"; break;
91
- }
92
- switch (strtol((char[]){revision[len-6],0}, NULL, 16) & 7) {
93
- case 0: info->ram = "256M"; break;
94
- case 1: info->ram = "512M"; break;
95
- case 2: info->ram = "1024M"; break;
96
- default: info->ram = "Unknown"; break;
97
- }
98
- } else {
99
- // old scheme
100
- info->ram = "Unknown";
101
- info->manufacturer = "Unknown";
102
- info->processor = "Unknown";
103
- info->type = "Unknown";
104
- strcpy(info->revision, revision);
105
-
106
- // get last four characters (ignore preceeding 1000 for overvolt)
107
- if (len > 4) {
108
- rev = (char *)&revision+len-4;
109
- } else {
110
- rev = revision;
111
- }
112
-
113
- if ((strcmp(rev, "0002") == 0) ||
114
- (strcmp(rev, "0003") == 0)) {
115
- info->p1_revision = 1;
116
- } else if ((strcmp(rev, "0004") == 0) ||
117
- (strcmp(rev, "0005") == 0) ||
118
- (strcmp(rev, "0006") == 0) ||
119
- (strcmp(rev, "0007") == 0) ||
120
- (strcmp(rev, "0008") == 0) ||
121
- (strcmp(rev, "0009") == 0) ||
122
- (strcmp(rev, "000d") == 0) ||
123
- (strcmp(rev, "000e") == 0) ||
124
- (strcmp(rev, "000f") == 0)) {
125
- info->p1_revision = 2;
126
- } else if (strcmp(rev, "0011") == 0) {
127
- info->p1_revision = 0; // compute module
128
- } else { // assume B+ (0010) or A+ (0012) or RPi2
129
- info->p1_revision = 3;
130
- }
131
- }
132
- return 0;
133
- }
134
-
135
- /*
136
-
137
- 32 bits
138
- NEW 23: will be 1 for the new scheme, 0 for the old scheme
139
- MEMSIZE 20: 0=256M 1=512M 2=1G
140
- MANUFACTURER 16: 0=SONY 1=EGOMAN
141
- PROCESSOR 12: 0=2835 1=2836
142
- TYPE 04: 0=MODELA 1=MODELB 2=MODELA+ 3=MODELB+ 4=Pi2 MODEL B 5=ALPHA 6=CM
143
- REV 00: 0=REV0 1=REV1 2=REV2
144
-
145
- pi2 = 1<<23 | 2<<20 | 1<<12 | 4<<4 = 0xa01040
146
-
147
- --------------------
148
-
149
- SRRR MMMM PPPP TTTT TTTT VVVV
150
-
151
- S scheme (0=old, 1=new)
152
- R RAM (0=256, 1=512, 2=1024)
153
- M manufacturer (0='SONY',1='EGOMAN',2='EMBEST',3='UNKNOWN',4='EMBEST')
154
- P processor (0=2835, 1=2836)
155
- T type (0='A', 1='B', 2='A+', 3='B+', 4='Pi 2 B', 5='Alpha', 6='Compute Module')
156
- V revision (0-15)
157
-
158
- */
159
-
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
+ */