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.
@@ -0,0 +1,49 @@
1
+ /*
2
+ Copyright (c) 2012-2013 Ben Croston
3
+
4
+ Permission is hereby granted, free of charge, to any person obtaining a copy of
5
+ this software and associated documentation files (the "Software"), to deal in
6
+ the Software without restriction, including without limitation the rights to
7
+ use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
8
+ of the Software, and to permit persons to whom the Software is furnished to do
9
+ so, subject to the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be included in all
12
+ copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20
+ SOFTWARE.
21
+ */
22
+
23
+ int setup(void);
24
+ void setup_gpio(int gpio, int direction, int pud);
25
+ int gpio_function(int gpio);
26
+ void output_gpio(int gpio, int value);
27
+ int input_gpio(int gpio);
28
+ void set_rising_event(int gpio, int enable);
29
+ void set_falling_event(int gpio, int enable);
30
+ void set_high_event(int gpio, int enable);
31
+ void set_low_event(int gpio, int enable);
32
+ int eventdetected(int gpio);
33
+ void cleanup(void);
34
+
35
+ #define SETUP_OK 0
36
+ #define SETUP_DEVMEM_FAIL 1
37
+ #define SETUP_MALLOC_FAIL 2
38
+ #define SETUP_MMAP_FAIL 3
39
+
40
+ #define INPUT 1 // is really 0 for control register!
41
+ #define OUTPUT 0 // is really 1 for control register!
42
+ #define ALT0 4
43
+
44
+ #define HIGH 1
45
+ #define LOW 0
46
+
47
+ #define PUD_OFF 0
48
+ #define PUD_DOWN 1
49
+ #define PUD_UP 2
@@ -0,0 +1,87 @@
1
+ /*
2
+ Copyright (c) 2013-2014 Ben Croston
3
+
4
+ Permission is hereby granted, free of charge, to any person obtaining a copy of
5
+ this software and associated documentation files (the "Software"), to deal in
6
+ the Software without restriction, including without limitation the rights to
7
+ use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
8
+ of the Software, and to permit persons to whom the Software is furnished to do
9
+ so, subject to the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be included in all
12
+ copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20
+ SOFTWARE.
21
+ */
22
+
23
+ #include "Python.h"
24
+ #include "c_gpio.h"
25
+ #include "common.h"
26
+
27
+ int gpio_mode = MODE_UNKNOWN;
28
+ 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 };
29
+ 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 };
30
+ 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 };
31
+ int setup_error = 0;
32
+ int module_setup = 0;
33
+
34
+ int check_gpio_priv(void)
35
+ {
36
+ // check module has been imported cleanly
37
+ if (setup_error)
38
+ {
39
+ PyErr_SetString(PyExc_RuntimeError, "Module not imported correctly!");
40
+ return 1;
41
+ }
42
+
43
+ // check mmap setup has worked
44
+ if (!module_setup)
45
+ {
46
+ PyErr_SetString(PyExc_RuntimeError, "No access to /dev/mem. Try running as root!");
47
+ return 2;
48
+ }
49
+ return 0;
50
+ }
51
+
52
+ int get_gpio_number(int channel, unsigned int *gpio)
53
+ {
54
+ // check setmode() has been run
55
+ if (gpio_mode != BOARD && gpio_mode != BCM)
56
+ {
57
+ PyErr_SetString(PyExc_RuntimeError, "Please set pin numbering mode using GPIO.setmode(GPIO.BOARD) or GPIO.setmode(GPIO.BCM)");
58
+ return 3;
59
+ }
60
+
61
+ // check channel number is in range
62
+ if ( (gpio_mode == BCM && (channel < 0 || channel > 53))
63
+ || (gpio_mode == BOARD && (channel < 1 || channel > 26) && rpiinfo.p1_revision != 3)
64
+ || (gpio_mode == BOARD && (channel < 1 || channel > 40) && rpiinfo.p1_revision == 3) )
65
+ {
66
+ PyErr_SetString(PyExc_ValueError, "The channel sent is invalid on a Raspberry Pi");
67
+ return 4;
68
+ }
69
+
70
+ // convert channel to gpio
71
+ if (gpio_mode == BOARD)
72
+ {
73
+ if (*(*pin_to_gpio+channel) == -1)
74
+ {
75
+ PyErr_SetString(PyExc_ValueError, "The channel sent is invalid on a Raspberry Pi");
76
+ return 5;
77
+ } else {
78
+ *gpio = *(*pin_to_gpio+channel);
79
+ }
80
+ }
81
+ else // gpio_mode == BCM
82
+ {
83
+ *gpio = channel;
84
+ }
85
+
86
+ return 0;
87
+ }
@@ -0,0 +1,43 @@
1
+ /*
2
+ Copyright (c) 2013-2015 Ben Croston
3
+
4
+ Permission is hereby granted, free of charge, to any person obtaining a copy of
5
+ this software and associated documentation files (the "Software"), to deal in
6
+ the Software without restriction, including without limitation the rights to
7
+ use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
8
+ of the Software, and to permit persons to whom the Software is furnished to do
9
+ so, subject to the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be included in all
12
+ copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20
+ SOFTWARE.
21
+ */
22
+
23
+ #include "cpuinfo.h"
24
+
25
+ #define MODE_UNKNOWN -1
26
+ #define BOARD 10
27
+ #define BCM 11
28
+ #define SERIAL 40
29
+ #define SPI 41
30
+ #define I2C 42
31
+ #define PWM 43
32
+
33
+ int gpio_mode;
34
+ const int pin_to_gpio_rev1[41];
35
+ const int pin_to_gpio_rev2[41];
36
+ const int pin_to_gpio_rev3[41];
37
+ const int (*pin_to_gpio)[41];
38
+ int gpio_direction[54];
39
+ rpi_info rpiinfo;
40
+ int setup_error;
41
+ int module_setup;
42
+ int check_gpio_priv(void);
43
+ int get_gpio_number(int channel, unsigned int *gpio);
@@ -0,0 +1,84 @@
1
+ /*
2
+ Copyright (c) 2013-2015 Ben Croston
3
+
4
+ Permission is hereby granted, free of charge, to any person obtaining a copy of
5
+ this software and associated documentation files (the "Software"), to deal in
6
+ the Software without restriction, including without limitation the rights to
7
+ use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
8
+ of the Software, and to permit persons to whom the Software is furnished to do
9
+ so, subject to the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be included in all
12
+ copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20
+ SOFTWARE.
21
+ */
22
+
23
+ #include "Python.h"
24
+ #include "constants.h"
25
+ #include "common.h"
26
+ #include "c_gpio.h"
27
+ #include "event_gpio.h"
28
+
29
+ void define_constants(PyObject *module)
30
+ {
31
+ high = Py_BuildValue("i", HIGH);
32
+ PyModule_AddObject(module, "HIGH", high);
33
+
34
+ low = Py_BuildValue("i", LOW);
35
+ PyModule_AddObject(module, "LOW", low);
36
+
37
+ output = Py_BuildValue("i", OUTPUT);
38
+ PyModule_AddObject(module, "OUT", output);
39
+
40
+ input = Py_BuildValue("i", INPUT);
41
+ PyModule_AddObject(module, "IN", input);
42
+
43
+ pwm = Py_BuildValue("i", PWM);
44
+ PyModule_AddObject(module, "HARD_PWM", pwm);
45
+
46
+ serial = Py_BuildValue("i", SERIAL);
47
+ PyModule_AddObject(module, "SERIAL", serial);
48
+
49
+ i2c = Py_BuildValue("i", I2C);
50
+ PyModule_AddObject(module, "I2C", i2c);
51
+
52
+ spi = Py_BuildValue("i", SPI);
53
+ PyModule_AddObject(module, "SPI", spi);
54
+
55
+ unknown = Py_BuildValue("i", MODE_UNKNOWN);
56
+ PyModule_AddObject(module, "UNKNOWN", unknown);
57
+
58
+ board = Py_BuildValue("i", BOARD);
59
+ PyModule_AddObject(module, "BOARD", board);
60
+
61
+ bcm = Py_BuildValue("i", BCM);
62
+ PyModule_AddObject(module, "BCM", bcm);
63
+
64
+ pud_off = Py_BuildValue("i", PUD_OFF + PY_PUD_CONST_OFFSET);
65
+ PyModule_AddObject(module, "PUD_OFF", pud_off);
66
+
67
+ pud_up = Py_BuildValue("i", PUD_UP + PY_PUD_CONST_OFFSET);
68
+ PyModule_AddObject(module, "PUD_UP", pud_up);
69
+
70
+ pud_down = Py_BuildValue("i", PUD_DOWN + PY_PUD_CONST_OFFSET);
71
+ PyModule_AddObject(module, "PUD_DOWN", pud_down);
72
+
73
+ rising_edge = Py_BuildValue("i", RISING_EDGE + PY_EVENT_CONST_OFFSET);
74
+ PyModule_AddObject(module, "RISING", rising_edge);
75
+
76
+ falling_edge = Py_BuildValue("i", FALLING_EDGE + PY_EVENT_CONST_OFFSET);
77
+ PyModule_AddObject(module, "FALLING", falling_edge);
78
+
79
+ both_edge = Py_BuildValue("i", BOTH_EDGE + PY_EVENT_CONST_OFFSET);
80
+ PyModule_AddObject(module, "BOTH", both_edge);
81
+
82
+ version = Py_BuildValue("s", "0.5.11");
83
+ PyModule_AddObject(module, "VERSION", version);
84
+ }
@@ -0,0 +1,45 @@
1
+ /*
2
+ Copyright (c) 2013 Ben Croston
3
+
4
+ Permission is hereby granted, free of charge, to any person obtaining a copy of
5
+ this software and associated documentation files (the "Software"), to deal in
6
+ the Software without restriction, including without limitation the rights to
7
+ use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
8
+ of the Software, and to permit persons to whom the Software is furnished to do
9
+ so, subject to the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be included in all
12
+ copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20
+ SOFTWARE.
21
+ */
22
+
23
+ #define PY_PUD_CONST_OFFSET 20
24
+ #define PY_EVENT_CONST_OFFSET 30
25
+
26
+ PyObject *high;
27
+ PyObject *low;
28
+ PyObject *input;
29
+ PyObject *output;
30
+ PyObject *pwm;
31
+ PyObject *serial;
32
+ PyObject *i2c;
33
+ PyObject *spi;
34
+ PyObject *unknown;
35
+ PyObject *board;
36
+ PyObject *bcm;
37
+ PyObject *pud_off;
38
+ PyObject *pud_up;
39
+ PyObject *pud_down;
40
+ PyObject *rising_edge;
41
+ PyObject *falling_edge;
42
+ PyObject *both_edge;
43
+ PyObject *version;
44
+
45
+ void define_constants(PyObject *module);
@@ -0,0 +1,149 @@
1
+ /*
2
+ Copyright (c) 2012-2015 Ben Croston
3
+
4
+ Permission is hereby granted, free of charge, to any person obtaining a copy of
5
+ this software and associated documentation files (the "Software"), to deal in
6
+ the Software without restriction, including without limitation the rights to
7
+ use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
8
+ of the Software, and to permit persons to whom the Software is furnished to do
9
+ so, subject to the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be included in all
12
+ copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20
+ SOFTWARE.
21
+ */
22
+
23
+ #include <stdio.h>
24
+ #include <stdlib.h>
25
+ #include <string.h>
26
+ #include "cpuinfo.h"
27
+
28
+ int get_rpi_info(rpi_info *info)
29
+ {
30
+ FILE *fp;
31
+ char buffer[1024];
32
+ char hardware[1024];
33
+ char revision[1024];
34
+ char *rev;
35
+ int found = 0;
36
+ int len;
37
+
38
+ if ((fp = fopen("/proc/cpuinfo", "r")) == NULL)
39
+ return -1;
40
+ while(!feof(fp)) {
41
+ fgets(buffer, sizeof(buffer) , fp);
42
+ sscanf(buffer, "Hardware : %s", hardware);
43
+ if (strcmp(hardware, "BCM2708") == 0 ||
44
+ strcmp(hardware, "BCM2709") == 0 ||
45
+ strcmp(hardware, "BCM2835") == 0 ||
46
+ strcmp(hardware, "BCM2836") == 0) {
47
+ found = 1;
48
+ }
49
+ sscanf(buffer, "Revision : %s", revision);
50
+ }
51
+ fclose(fp);
52
+
53
+ if (!found)
54
+ return -1;
55
+
56
+ if ((len = strlen(revision)) == 0)
57
+ return -1;
58
+
59
+ if (len >= 6 && strtol((char[]){revision[len-6],0}, NULL, 16) & 8) {
60
+ // new scheme
61
+ //info->rev = revision[len-1]-'0';
62
+ strcpy(info->revision, revision);
63
+ switch (revision[len-2]) {
64
+ case '0': info->type = "Model A"; info->p1_revision = 2; break;
65
+ case '1': info->type = "Model B"; info->p1_revision = 2; break;
66
+ case '2': info->type = "Model A+"; info->p1_revision = 3; break;
67
+ case '3': info->type = "Model B+"; info->p1_revision = 3; break;
68
+ case '4': info->type = "Pi2 Model B"; info->p1_revision = 3; break;
69
+ case '5': info->type = "Alpha"; info->p1_revision = 3; break;
70
+ case '6': info->type = "Compute"; info->p1_revision = 0; break;
71
+ default : info->type = "Unknown"; info->p1_revision = 3; break;
72
+ }
73
+ switch (revision[len-4]) {
74
+ case '0': info->processor = "BCM2835"; break;
75
+ case '1': info->processor = "BCM2836"; break;
76
+ default : info->processor = "Unknown"; break;
77
+ }
78
+ switch (revision[len-5]) {
79
+ case '0': info->manufacturer = "Sony"; break;
80
+ case '1': info->manufacturer = "Egoman"; break;
81
+ case '2': info->manufacturer = "Embest"; break;
82
+ case '4': info->manufacturer = "Embest"; break;
83
+ default : info->manufacturer = "Unknown"; break;
84
+ }
85
+ switch (strtol((char[]){revision[len-6],0}, NULL, 16) & 7) {
86
+ case 0: info->ram = "256M"; break;
87
+ case 1: info->ram = "512M"; break;
88
+ case 2: info->ram = "1024M"; break;
89
+ default: info->ram = "Unknown"; break;
90
+ }
91
+ } else {
92
+ // old scheme
93
+ info->ram = "Unknown";
94
+ info->manufacturer = "Unknown";
95
+ info->processor = "Unknown";
96
+ info->type = "Unknown";
97
+ strcpy(info->revision, revision);
98
+
99
+ // get last four characters (ignore preceeding 1000 for overvolt)
100
+ if (len > 4)
101
+ rev = (char *)&revision+len-4;
102
+ else
103
+ rev = revision;
104
+
105
+ if ((strcmp(rev, "0002") == 0) ||
106
+ (strcmp(rev, "0003") == 0))
107
+ info->p1_revision = 1;
108
+ else if ((strcmp(rev, "0004") == 0) ||
109
+ (strcmp(rev, "0005") == 0) ||
110
+ (strcmp(rev, "0006") == 0) ||
111
+ (strcmp(rev, "0007") == 0) ||
112
+ (strcmp(rev, "0008") == 0) ||
113
+ (strcmp(rev, "0009") == 0) ||
114
+ (strcmp(rev, "000d") == 0) ||
115
+ (strcmp(rev, "000e") == 0) ||
116
+ (strcmp(rev, "000f") == 0))
117
+ info->p1_revision = 2;
118
+ else if (strcmp(rev, "0011") == 0)
119
+ info->p1_revision = 0; // compute module
120
+ else // assume B+ (0010) or A+ (0012) or RPi2
121
+ info->p1_revision = 3;
122
+ }
123
+ return 0;
124
+ }
125
+
126
+ /*
127
+
128
+ 32 bits
129
+ NEW 23: will be 1 for the new scheme, 0 for the old scheme
130
+ MEMSIZE 20: 0=256M 1=512M 2=1G
131
+ MANUFACTURER 16: 0=SONY 1=EGOMAN
132
+ PROCESSOR 12: 0=2835 1=2836
133
+ TYPE 04: 0=MODELA 1=MODELB 2=MODELA+ 3=MODELB+ 4=Pi2 MODEL B 5=ALPHA 6=CM
134
+ REV 00: 0=REV0 1=REV1 2=REV2
135
+
136
+ pi2 = 1<<23 | 2<<20 | 1<<12 | 4<<4 = 0xa01040
137
+
138
+ --------------------
139
+
140
+ SRRR MMMM PPPP TTTT TTTT VVVV
141
+
142
+ S scheme (0=old, 1=new)
143
+ R RAM (0=256, 1=512, 2=1024)
144
+ M manufacturer (0='SONY',1='EGOMAN',2='EMBEST',3='UNKNOWN',4='EMBEST')
145
+ P processor (0=2835, 1=2836)
146
+ T type (0='A', 1='B', 2='A+', 3='B+', 4='Pi 2 B', 5='Alpha', 6='Compute Module')
147
+ V revision (0-15)
148
+
149
+ */