| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 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 |
const int (*pin_to_gpio)[41]; |
| 32 |
int gpio_direction[54]; |
| 33 |
rpi_info rpiinfo; |
| 34 |
int setup_error = 0; |
| 35 |
int module_setup = 0; |
| 36 |
|
| 37 |
int check_gpio_priv(void) |
| 38 |
{ |
| 39 |
|
| 40 |
if (setup_error) |
| 41 |
{ |
| 42 |
PyErr_SetString(PyExc_RuntimeError, "Module not imported correctly!"); |
| 43 |
return 1; |
| 44 |
} |
| 45 |
|
| 46 |
|
| 47 |
if (!module_setup) |
| 48 |
{ |
| 49 |
PyErr_SetString(PyExc_RuntimeError, "No access to /dev/mem. Try running as root!"); |
| 50 |
return 2; |
| 51 |
} |
| 52 |
return 0; |
| 53 |
} |
| 54 |
|
| 55 |
int get_gpio_number(int channel, unsigned int *gpio) |
| 56 |
{ |
| 57 |
|
| 58 |
if (gpio_mode != BOARD && gpio_mode != BCM) |
| 59 |
{ |
| 60 |
PyErr_SetString(PyExc_RuntimeError, "Please set pin numbering mode using GPIO.setmode(GPIO.BOARD) or GPIO.setmode(GPIO.BCM)"); |
| 61 |
return 3; |
| 62 |
} |
| 63 |
|
| 64 |
|
| 65 |
if ( (gpio_mode == BCM && (channel < 0 || channel > 53)) |
| 66 |
|| (gpio_mode == BOARD && (channel < 1 || channel > 26) && rpiinfo.p1_revision != 3) |
| 67 |
|| (gpio_mode == BOARD && (channel < 1 || channel > 40) && rpiinfo.p1_revision == 3) ) |
| 68 |
{ |
| 69 |
PyErr_SetString(PyExc_ValueError, "The channel sent is invalid on a Raspberry Pi"); |
| 70 |
return 4; |
| 71 |
} |
| 72 |
|
| 73 |
|
| 74 |
if (gpio_mode == BOARD) |
| 75 |
{ |
| 76 |
if (*(*pin_to_gpio+channel) == -1) |
| 77 |
{ |
| 78 |
PyErr_SetString(PyExc_ValueError, "The channel sent is invalid on a Raspberry Pi"); |
| 79 |
return 5; |
| 80 |
} else { |
| 81 |
*gpio = *(*pin_to_gpio+channel); |
| 82 |
} |
| 83 |
} |
| 84 |
else |
| 85 |
{ |
| 86 |
*gpio = channel; |
| 87 |
} |
| 88 |
|
| 89 |
return 0; |
| 90 |
} |