From 34167a957497868200794c4ce5d215ac893933a7 Mon Sep 17 00:00:00 2001 From: fordsfords Date: Tue, 21 Jun 2016 17:40:00 -0500 Subject: [PATCH] Some (not all) of the changes for the new XIO base addr --- source/common.c | 237 +++++++++++++++++++++++++---------------------- source/common.h | 6 -- source/py_gpio.c | 20 +++- 3 files changed, 142 insertions(+), 121 deletions(-) diff --git a/source/common.c b/source/common.c index 1149ed0..993c073 100644 --- a/source/common.c +++ b/source/common.c @@ -49,97 +49,104 @@ SOFTWARE. int setup_error = 0; int module_setup = 0; +int xio_base_address = -1; /* not yet initialized */ + +// In the pins_t structure, the "base_method" field tells how +// the "gpio" field should be interpreted. +#define BASE_METHOD_AS_IS 1 /* use the gpio value directly */ +#define BASE_METHOD_XIO 2 /* add the gpio value to the XIO base */ typedef struct pins_t { const char *name; const char *key; - int gpio; + int gpio; /* port number to use under /sys/class/gpio */ + int base_method; /* modifier for port number; see BASE_METHOD_... */ int pwm_mux_mode; int ain; } pins_t; // I have no idea if this table is correct, we shall see - Robert Wolterman -pins_t table[] = { - { "GND", "U13_1", 0, -1, -1}, - { "CHG-IN", "U13_2", 0, -1, -1}, - { "VCC-5V", "U13_3", 0, -1, -1}, - { "GND", "U13_4", 0, -1, -1}, - { "VCC-3V3", "U13_5", 0, -1, -1}, - { "TS", "U13_6", 0, -1, -1}, - { "VCC-1V8", "U13_7", 0, -1, -1}, - { "BAT", "U13_8", 0, -1, -1}, - { "TWI1-SDA", "U13_9", 48, -1, -1}, - { "PWRON", "U13_10", 0, -1, -1}, - { "TWI1-SCK", "U13_11", 47, -1, -1}, - { "GND", "U13_12", 0, -1, -1}, - { "X1", "U13_13", 0, -1, -1}, - { "X2", "U13_14", 0, -1, -1}, - { "Y1", "U13_15", 0, -1, -1}, - { "Y2", "U13_16", 0, -1, -1}, - { "LCD-D2", "U13_17", 98, -1, -1}, - { "PWM0", "U13_18", 34, 0, -1}, - { "LCD-D4", "U13_19", 100, -1, -1}, - { "LCD-D3", "U13_20", 99, -1, -1}, - { "LCD-D6", "U13_21", 102, -1, -1}, - { "LCD-D5", "U13_22", 101, -1, -1}, - { "LCD-D10", "U13_23", 106, -1, -1}, - { "LCD-D7", "U13_24", 103, -1, -1}, - { "LCD-D12", "U13_25", 108, -1, -1}, - { "LCD-D11", "U13_26", 107, -1, -1}, - { "LCD-D14", "U13_27", 110, -1, -1}, - { "LCD-D13", "U13_28", 109, -1, -1}, - { "LCD-D18", "U13_29", 114, -1, -1}, - { "LCD-D15", "U13_30", 111, -1, -1}, - { "LCD-D20", "U13_31", 116, -1, -1}, - { "LCD-D19", "U13_32", 115, -1, -1}, - { "LCD-D22", "U13_33", 118, -1, -1}, - { "LCD-D21", "U13_34", 117, -1, -1}, - { "LCD-CLK", "U13_35", 120, -1, -1}, - { "LCD-D23", "U13_36", 119, -1, -1}, - { "LCD-VSYNC", "U13_37", 123, -1, -1}, - { "LCD-HSYNC", "U13_38", 122, -1, -1}, - { "GND", "U13_39", 0, -1, -1}, - { "LCD-DE", "U13_40", 121, -1, -1}, - { "GND", "U14_1", 0, -1, -1}, - { "VCC-5V", "U14_2", 0, -1, -1}, - { "UART1-TX", "U14_3", 195, -1, -1}, - { "HPL", "U14_4", 0, -1, -1}, - { "UART1-RX", "U14_5", 196, -1, -1}, - { "HPCOM", "U14_6", 0, -1, -1}, - { "FEL", "U14_7", 0, -1, -1}, - { "HPR", "U14_8", 0, -1, -1}, - { "VCC-3V3", "U14_9", 0, -1, -1}, - { "MICM", "U14_10", 0, -1, -1}, - { "LRADC", "U14_11", 0, -1, 0}, - { "MICIN1", "U14_12", 0, -1, -1}, - { "XIO-P0", "U14_13", 408, -1, -1}, - { "XIO-P1", "U14_14", 409, -1, -1}, - { "XIO-P2", "U14_15", 410, -1, -1}, - { "XIO-P3", "U14_16", 411, -1, -1}, - { "XIO-P4", "U14_17", 412, -1, -1}, - { "XIO-P5", "U14_18", 413, -1, -1}, - { "XIO-P6", "U14_19", 414, -1, -1}, - { "XIO-P7", "U14_20", 415, -1, -1}, - { "GND", "U14_21", 0, -1, -1}, - { "GND", "U14_22", 0, -1, -1}, - { "AP-EINT1", "U14_23", 193, -1, -1}, - { "AP-EINT3", "U14_24", 35, -1, -1}, - { "TWI2-SDA", "U14_25", 50, -1, -1}, - { "TWI2-SCK", "U14_26", 49, -1, -1}, - { "CSIPCK", "U14_27", 128, -1, -1}, - { "CSICK", "U14_28", 129, -1, -1}, - { "CSIHSYNC", "U14_29", 130, 1, -1}, - { "CSIVSYNC", "U14_30", 131, -1, -1}, - { "CSID0", "U14_31", 132, 1, -1}, - { "CSID1", "U14_32", 133, -1, -1}, - { "CSID2", "U14_33", 134, -1, -1}, - { "CSID3", "U14_34", 135, -1, -1}, - { "CSID4", "U14_35", 136, -1, -1}, - { "CSID5", "U14_36", 137, -1, -1}, - { "CSID6", "U14_37", 138, -1, -1}, - { "CSID7", "U14_38", 139, -1, -1}, - { "GND", "U14_39", 0, -1, -1}, - { "GND", "U14_40", 0, -1, -1}, +pins_t pins_info[] = { + { "GND", "U13_1", 0, BASE_METHOD_AS_IS, -1, -1}, + { "CHG-IN", "U13_2", 0, BASE_METHOD_AS_IS, -1, -1}, + { "VCC-5V", "U13_3", 0, BASE_METHOD_AS_IS, -1, -1}, + { "GND", "U13_4", 0, BASE_METHOD_AS_IS, -1, -1}, + { "VCC-3V3", "U13_5", 0, BASE_METHOD_AS_IS, -1, -1}, + { "TS", "U13_6", 0, BASE_METHOD_AS_IS, -1, -1}, + { "VCC-1V8", "U13_7", 0, BASE_METHOD_AS_IS, -1, -1}, + { "BAT", "U13_8", 0, BASE_METHOD_AS_IS, -1, -1}, + { "TWI1-SDA", "U13_9", 48, BASE_METHOD_AS_IS, -1, -1}, + { "PWRON", "U13_10", 0, BASE_METHOD_AS_IS, -1, -1}, + { "TWI1-SCK", "U13_11", 47, BASE_METHOD_AS_IS, -1, -1}, + { "GND", "U13_12", 0, BASE_METHOD_AS_IS, -1, -1}, + { "X1", "U13_13", 0, BASE_METHOD_AS_IS, -1, -1}, + { "X2", "U13_14", 0, BASE_METHOD_AS_IS, -1, -1}, + { "Y1", "U13_15", 0, BASE_METHOD_AS_IS, -1, -1}, + { "Y2", "U13_16", 0, BASE_METHOD_AS_IS, -1, -1}, + { "LCD-D2", "U13_17", 98, BASE_METHOD_AS_IS, -1, -1}, + { "PWM0", "U13_18", 34, BASE_METHOD_AS_IS, 0, -1}, + { "LCD-D4", "U13_19", 100, BASE_METHOD_AS_IS, -1, -1}, + { "LCD-D3", "U13_20", 99, BASE_METHOD_AS_IS, -1, -1}, + { "LCD-D6", "U13_21", 102, BASE_METHOD_AS_IS, -1, -1}, + { "LCD-D5", "U13_22", 101, BASE_METHOD_AS_IS, -1, -1}, + { "LCD-D10", "U13_23", 106, BASE_METHOD_AS_IS, -1, -1}, + { "LCD-D7", "U13_24", 103, BASE_METHOD_AS_IS, -1, -1}, + { "LCD-D12", "U13_25", 108, BASE_METHOD_AS_IS, -1, -1}, + { "LCD-D11", "U13_26", 107, BASE_METHOD_AS_IS, -1, -1}, + { "LCD-D14", "U13_27", 110, BASE_METHOD_AS_IS, -1, -1}, + { "LCD-D13", "U13_28", 109, BASE_METHOD_AS_IS, -1, -1}, + { "LCD-D18", "U13_29", 114, BASE_METHOD_AS_IS, -1, -1}, + { "LCD-D15", "U13_30", 111, BASE_METHOD_AS_IS, -1, -1}, + { "LCD-D20", "U13_31", 116, BASE_METHOD_AS_IS, -1, -1}, + { "LCD-D19", "U13_32", 115, BASE_METHOD_AS_IS, -1, -1}, + { "LCD-D22", "U13_33", 118, BASE_METHOD_AS_IS, -1, -1}, + { "LCD-D21", "U13_34", 117, BASE_METHOD_AS_IS, -1, -1}, + { "LCD-CLK", "U13_35", 120, BASE_METHOD_AS_IS, -1, -1}, + { "LCD-D23", "U13_36", 119, BASE_METHOD_AS_IS, -1, -1}, + { "LCD-VSYNC", "U13_37", 123, BASE_METHOD_AS_IS, -1, -1}, + { "LCD-HSYNC", "U13_38", 122, BASE_METHOD_AS_IS, -1, -1}, + { "GND", "U13_39", 0, BASE_METHOD_AS_IS, -1, -1}, + { "LCD-DE", "U13_40", 121, BASE_METHOD_AS_IS, -1, -1}, + { "GND", "U14_1", 0, BASE_METHOD_AS_IS, -1, -1}, + { "VCC-5V", "U14_2", 0, BASE_METHOD_AS_IS, -1, -1}, + { "UART1-TX", "U14_3", 195, BASE_METHOD_AS_IS, -1, -1}, + { "HPL", "U14_4", 0, BASE_METHOD_AS_IS, -1, -1}, + { "UART1-RX", "U14_5", 196, BASE_METHOD_AS_IS, -1, -1}, + { "HPCOM", "U14_6", 0, BASE_METHOD_AS_IS, -1, -1}, + { "FEL", "U14_7", 0, BASE_METHOD_AS_IS, -1, -1}, + { "HPR", "U14_8", 0, BASE_METHOD_AS_IS, -1, -1}, + { "VCC-3V3", "U14_9", 0, BASE_METHOD_AS_IS, -1, -1}, + { "MICM", "U14_10", 0, BASE_METHOD_AS_IS, -1, -1}, + { "LRADC", "U14_11", 0, BASE_METHOD_AS_IS, -1, 0}, + { "MICIN1", "U14_12", 0, BASE_METHOD_AS_IS, -1, -1}, + { "XIO-P0", "U14_13", 0, BASE_METHOD_XIO, -1, -1}, + { "XIO-P1", "U14_14", 1, BASE_METHOD_XIO, -1, -1}, + { "XIO-P2", "U14_15", 2, BASE_METHOD_XIO, -1, -1}, + { "XIO-P3", "U14_16", 3, BASE_METHOD_XIO, -1, -1}, + { "XIO-P4", "U14_17", 4, BASE_METHOD_XIO, -1, -1}, + { "XIO-P5", "U14_18", 5, BASE_METHOD_XIO, -1, -1}, + { "XIO-P6", "U14_19", 6, BASE_METHOD_XIO, -1, -1}, + { "XIO-P7", "U14_20", 7, BASE_METHOD_XIO, -1, -1}, + { "GND", "U14_21", 0, BASE_METHOD_AS_IS, -1, -1}, + { "GND", "U14_22", 0, BASE_METHOD_AS_IS, -1, -1}, + { "AP-EINT1", "U14_23", 193, BASE_METHOD_AS_IS, -1, -1}, + { "AP-EINT3", "U14_24", 35, BASE_METHOD_AS_IS, -1, -1}, + { "TWI2-SDA", "U14_25", 50, BASE_METHOD_AS_IS, -1, -1}, + { "TWI2-SCK", "U14_26", 49, BASE_METHOD_AS_IS, -1, -1}, + { "CSIPCK", "U14_27", 128, BASE_METHOD_AS_IS, -1, -1}, + { "CSICK", "U14_28", 129, BASE_METHOD_AS_IS, -1, -1}, + { "CSIHSYNC", "U14_29", 130, BASE_METHOD_AS_IS, 1, -1}, + { "CSIVSYNC", "U14_30", 131, BASE_METHOD_AS_IS, -1, -1}, + { "CSID0", "U14_31", 132, BASE_METHOD_AS_IS, 1, -1}, + { "CSID1", "U14_32", 133, BASE_METHOD_AS_IS, -1, -1}, + { "CSID2", "U14_33", 134, BASE_METHOD_AS_IS, -1, -1}, + { "CSID3", "U14_34", 135, BASE_METHOD_AS_IS, -1, -1}, + { "CSID4", "U14_35", 136, BASE_METHOD_AS_IS, -1, -1}, + { "CSID5", "U14_36", 137, BASE_METHOD_AS_IS, -1, -1}, + { "CSID6", "U14_37", 138, BASE_METHOD_AS_IS, -1, -1}, + { "CSID7", "U14_38", 139, BASE_METHOD_AS_IS, -1, -1}, + { "GND", "U14_39", 0, BASE_METHOD_AS_IS, -1, -1}, + { "GND", "U14_40", 0, BASE_METHOD_AS_IS, -1, -1}, { NULL, NULL, 0 } }; @@ -198,22 +205,35 @@ int get_xio_base() return xiobase; } + +int gpio_number(pins_t *pin) +{ + int gpio_num = -1; + + switch (pin->base_method) { + case BASE_METHOD_AS_IS: + gpio_num = pin->gpio; + break; + + case BASE_METHOD_XIO: + if (xio_base_address == -1) { + xio_base_address = get_xio_base(); /* only call this the first time */ + } + gpio_num = pin->gpio + xio_base_address; + break; + } + + assert(gpio_num != -1); + return gpio_num; +} + + int lookup_gpio_by_key(const char *key) { pins_t *p; - for (p = table; p->key != NULL; ++p) { + for (p = pins_info; p->key != NULL; ++p) { if (strcmp(p->key, key) == 0) { - // FIGURE OUT IF WE'RE AN XIO PIN USING THE DEFAULT PINS - if (p->gpio >= OLDXIOBASE && p->gpio < OLDXIOBASE+8) { - int gbase = get_xio_base(); - if (gbase != -1) { - return (gbase + (p->gpio - OLDXIOBASE)); - } else { - return 0; - } - } else { - return p->gpio; - } + return gpio_number(p); } } return 0; @@ -222,19 +242,9 @@ int lookup_gpio_by_key(const char *key) int lookup_gpio_by_name(const char *name) { pins_t *p; - for (p = table; p->name != NULL; ++p) { + for (p = pins_info; p->name != NULL; ++p) { if (strcmp(p->name, name) == 0) { - // FIGURE OUT IF WE'RE AN XIO PIN USING THE DEFAULT PINS - if (p->gpio >= OLDXIOBASE && p->gpio < OLDXIOBASE+8) { - int gbase = get_xio_base(); - if (gbase != -1) { - return (gbase + (p->gpio - OLDXIOBASE)); - } else { - return 0; - } - } else { - return p->gpio; - } + return gpio_number(p); } } return 0; @@ -243,7 +253,7 @@ int lookup_gpio_by_name(const char *name) int lookup_ain_by_key(const char *key) { pins_t *p; - for (p = table; p->key != NULL; ++p) { + for (p = pins_info; p->key != NULL; ++p) { if (strcmp(p->key, key) == 0) { if (p->ain == -1) { return -1; @@ -258,7 +268,7 @@ int lookup_ain_by_key(const char *key) int lookup_ain_by_name(const char *name) { pins_t *p; - for (p = table; p->name != NULL; ++p) { + for (p = pins_info; p->name != NULL; ++p) { if (strcmp(p->name, name) == 0) { if (p->ain == -1) { return -1; @@ -273,7 +283,7 @@ int lookup_ain_by_name(const char *name) int copy_key_by_key(const char *input_key, char *key) { pins_t *p; - for (p = table; p->key != NULL; ++p) { + for (p = pins_info; p->key != NULL; ++p) { if (strcmp(p->key, input_key) == 0) { strncpy(key, p->key, 7); key[7] = '\0'; @@ -286,7 +296,7 @@ int copy_key_by_key(const char *input_key, char *key) int copy_pwm_key_by_key(const char *input_key, char *key) { pins_t *p; - for (p = table; p->key != NULL; ++p) { + for (p = pins_info; p->key != NULL; ++p) { if (strcmp(p->key, input_key) == 0) { //validate it's a valid pwm pin if (p->pwm_mux_mode == -1) @@ -303,7 +313,7 @@ int copy_pwm_key_by_key(const char *input_key, char *key) int get_key_by_name(const char *name, char *key) { pins_t *p; - for (p = table; p->name != NULL; ++p) { + for (p = pins_info; p->name != NULL; ++p) { if (strcmp(p->name, name) == 0) { strncpy(key, p->key, 7); key[7] = '\0'; @@ -316,7 +326,7 @@ int get_key_by_name(const char *name, char *key) int get_pwm_key_by_name(const char *name, char *key) { pins_t *p; - for (p = table; p->name != NULL; ++p) { + for (p = pins_info; p->name != NULL; ++p) { if (strcmp(p->name, name) == 0) { //validate it's a valid pwm pin if (p->pwm_mux_mode == -1) @@ -401,7 +411,8 @@ int build_path(const char *partial_path, const char *prefix, char *full_path, si int get_spi_bus_path_number(unsigned int spi) { - char path[50]; + char path[80]; + char ocp_dir[30]; build_path("/sys/devices", "ocp", ocp_dir, sizeof(ocp_dir)); diff --git a/source/common.h b/source/common.h index d012856..716482c 100644 --- a/source/common.h +++ b/source/common.h @@ -44,12 +44,6 @@ SOFTWARE. #define FILENAME_BUFFER_SIZE 128 -int gpio_mode; -int gpio_direction[120]; - -char ctrl_dir[35]; -char ocp_dir[25]; - int get_xio_base(); int get_gpio_number(const char *key, unsigned int *gpio); int get_pwm_key(const char *input, char *key); diff --git a/source/py_gpio.c b/source/py_gpio.c index 2d0676f..2f3f71d 100644 --- a/source/py_gpio.c +++ b/source/py_gpio.c @@ -36,6 +36,7 @@ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ +#include "stdlib.h" #include "Python.h" #include "constants.h" #include "common.h" @@ -43,6 +44,9 @@ SOFTWARE. static int gpio_warnings = 1; +int max_gpio = -1; +int *gpio_direction = NULL; + struct py_callback { char channel[32]; @@ -58,7 +62,9 @@ static int init_module(void) { int i; - for (i=0; i<430; i++) + max_gpio = 1024; + gpio_direction = (int *)malloc(max_gpio * sizeof(int)); + for (i=0; i= max_gpio) { /* Does gpio_direction need to be expanded? */ + max_gpio = gpio + (gpio / 2); + gpio_direction = (int *)realloc(gpio_direction, max_gpio * sizeof(int)); + } + gpio_direction[gpio] = direction; +} + // python function cleanup() static PyObject *py_cleanup(PyObject *self, PyObject *args) { @@ -121,7 +137,7 @@ static PyObject *py_setup_channel(PyObject *self, PyObject *args, PyObject *kwar } } - gpio_direction[gpio] = direction; + set_gpio_direction(gpio, direction); Py_RETURN_NONE; }