rpi_gpio 0.1.5 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,50 +1,53 @@
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 PUD_OFF 0
49
- #define PUD_DOWN 1
50
- #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-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,100 +1,101 @@
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
- int revision = -1;
44
-
45
- int check_gpio_priv(void)
46
- {
47
- // check module has been imported cleanly
48
- if (setup_error)
49
- {
50
- rb_raise(rb_eRuntimeError, "Gem not imported correctly!");
51
- return 1;
52
- }
53
-
54
- // check mmap setup has worked
55
- if (!module_setup)
56
- {
57
- rb_raise(rb_eRuntimeError, "No access to /dev/mem. Try "
58
- "running as root!");
59
- return 2;
60
- }
61
- return 0;
62
- }
63
-
64
- int get_gpio_number(int channel, unsigned int *gpio)
65
- {
66
- // check setmode() has been run
67
- if (gpio_mode != BOARD && gpio_mode != BCM)
68
- {
69
- rb_raise(rb_eRuntimeError, "Please set pin numbering mode "
70
- "using RPi::GPIO.set_mode(RPi::GPIO::BOARD) or "
71
- "RPi::GPIO.set_mode(RPi::GPIO::BCM)");
72
- return 3;
73
- }
74
-
75
- // check channel number is in range
76
- if ( (gpio_mode == BCM && (channel < 0 || channel > 53))
77
- || (gpio_mode == BOARD && (channel < 1 || channel > 26) && revision != 3)
78
- || (gpio_mode == BOARD && (channel < 1 || channel > 40) && revision == 3))
79
- {
80
- rb_raise(rb_eArgError, "The channel sent is invalid on a Raspberry Pi");
81
- return 4;
82
- }
83
-
84
- // convert channel to gpio
85
- if (gpio_mode == BOARD)
86
- {
87
- if (*(*pin_to_gpio+channel) == -1)
88
- {
89
- rb_raise(rb_eArgError, "The channel sent is invalid on a Raspberry "
90
- "Pi");
91
- return 5;
92
- } else {
93
- *gpio = *(*pin_to_gpio+channel);
94
- }
95
- }
96
- else // gpio_mode == BCM
97
- *gpio = channel;
98
-
99
- return 0;
100
- }
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,46 +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
- #define MODE_UNKNOWN -1
28
- #define BOARD 10
29
- #define BCM 11
30
- #define SERIAL 40
31
- #define SPI 41
32
- #define I2C 42
33
- #define PWM 43
34
-
35
- int gpio_mode;
36
- const int pin_to_gpio_rev1[41];
37
- const int pin_to_gpio_rev2[41];
38
- const int pin_to_gpio_rev3[41];
39
- const int (*pin_to_gpio)[41];
40
- int gpio_direction[54];
41
- int revision;
42
-
43
- int check_gpio_priv(void);
44
- int get_gpio_number(int channel, unsigned int *gpio);
45
- int setup_error;
46
- int module_setup;
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,88 +1,159 @@
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 <string.h>
29
- #include "cpuinfo.h"
30
-
31
- char *get_cpuinfo_revision(char *revision)
32
- {
33
- FILE *fp;
34
- char buffer[1024];
35
- char hardware[1024];
36
- int rpi_found = 0;
37
-
38
- if ((fp = fopen("/proc/cpuinfo", "r")) == NULL)
39
- return 0;
40
-
41
- while(!feof(fp)) {
42
- fgets(buffer, sizeof(buffer) , fp);
43
- sscanf(buffer, "Hardware : %s", hardware);
44
- if (strcmp(hardware, "BCM2708") == 0)
45
- rpi_found = 1;
46
- sscanf(buffer, "Revision : %s", revision);
47
- }
48
- fclose(fp);
49
-
50
- if (!rpi_found)
51
- revision = NULL;
52
- return revision;
53
- }
54
-
55
- int get_rpi_revision(void)
56
- {
57
- char raw_revision[1024] = {'\0'};
58
- int len;
59
- char *revision;
60
-
61
- if (get_cpuinfo_revision(raw_revision) == NULL)
62
- return -1;
63
-
64
- // get last four characters (ignore preceeding 1000 for overvolt)
65
- len = strlen(raw_revision);
66
- if (len > 4)
67
- revision = (char *)&raw_revision+len-4;
68
- else
69
- revision = raw_revision;
70
-
71
- if ((strcmp(revision, "0002") == 0) ||
72
- (strcmp(revision, "0003") == 0))
73
- return 1;
74
- else if ((strcmp(revision, "0004") == 0) ||
75
- (strcmp(revision, "0005") == 0) ||
76
- (strcmp(revision, "0006") == 0) ||
77
- (strcmp(revision, "0007") == 0) ||
78
- (strcmp(revision, "0008") == 0) ||
79
- (strcmp(revision, "0009") == 0) ||
80
- (strcmp(revision, "000d") == 0) ||
81
- (strcmp(revision, "000e") == 0) ||
82
- (strcmp(revision, "000f") == 0))
83
- return 2; // revision 2
84
- else if (strcmp(revision, "0011") == 0)
85
- return 0; // compute module
86
- else // assume B+ (0010) or A+ (0012)
87
- return 3;
88
- }
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
+