From 121ad398e4de6cd9c0b16b06f0fe89b3478866ca Mon Sep 17 00:00:00 2001 From: Robert Wolterman Date: Sat, 25 Feb 2017 17:06:38 -0600 Subject: [PATCH] new method for determining if the computer is a chip/chip pro, this fixes the runtimeerror's i was getting. for #32 --- source/common.c | 122 ++++++++++++++++++++------------------------ source/common.h | 1 - source/py_gpio.c | 50 ++++++++++-------- source/py_pwm.c | 13 +---- source/py_servo.c | 15 +----- source/py_softpwm.c | 15 +----- 6 files changed, 87 insertions(+), 129 deletions(-) diff --git a/source/common.c b/source/common.c index a1a7776..0e5caa1 100644 --- a/source/common.c +++ b/source/common.c @@ -45,6 +45,7 @@ SOFTWARE. #include #include #include +#include int setup_error = 0; int module_setup = 0; @@ -53,7 +54,7 @@ int module_setup = 0; int DEBUG = 0; // Is This a CHIP PRO -int ISCHIPPRO = 0; +int is_chip_pro = 0; pins_t pins_info[] = { { "GND", "GND", "U13_1", -1, BASE_METHOD_AS_IS, -1, -1, BOTH}, @@ -220,76 +221,57 @@ int get_xio_base(void) return xio_base_address; } /* get_xio_base */ -#define BOOTPATH "/boot" +#define RAMDETERMINER 380.0 +#define MEGABYTE 1024.0*1024.0 int is_this_chippro(void) { - int rtnval = -1; - DIR *dir; - struct dirent *ent; + int is_pro = 0; - // Default ISCHIPPRO to 0 for CHIP - ISCHIPPRO = 0; - - // Get the boot directory open - dir = opendir (BOOTPATH); - if (dir == NULL) { - char err[256]; - snprintf(err, sizeof(err), "is_this_chippro: could not open '%s' (%s)", BOOTPATH, strerror(errno)); - add_error_msg(err); - return -1; + struct sysinfo si; + sysinfo (&si); + + if ((si.totalram/MEGABYTE) > RAMDETERMINER) { + is_pro = 0; + } else { + is_pro = 1; } - if (DEBUG) - printf(" ** is_this_chippro: checking boot directory\n"); - // Loop through the directory files - while ((ent = readdir (dir)) != NULL) { - //printf ("%s\n", ent->d_name); - if(strstr(ent->d_name, "gr8") != NULL) { - rtnval = 1; - ISCHIPPRO = 1; - if (DEBUG) - printf(" ** is_this_chippro: this is a chip pro!\n"); - break; - } - } - closedir (dir); - - if (ISCHIPPRO == 0) - { - rtnval = 0; - if (DEBUG) - printf(" ** is_this_chippro: this is a chip!\n"); - } - - return rtnval; + return is_pro; } int gpio_allowed(int gpio) { int rtnval = -1; pins_t *p; - // Determine if we are CHIP Pro - // Running because we cannot be sure if it was previously run - int iscpro = is_this_chippro(); + int tmpgpio = -1; // If the return is good, we should be good to go, so let's check the data - if (iscpro != -1) { - // Loop through the pins - for (p = pins_info; p->key != NULL; ++p) { - if (p->gpio == gpio) { - // We have a CHIP and the pin is for CHIP/BOTH - if (((p->sbc_type == CHIP) || (p->sbc_type == BOTH)) && (ISCHIPPRO == 0)) { - rtnval = 1; - // We have a CHIP Pro and the pin is for CHIPPRO/BOTH - } else if (((p->sbc_type == CHIPPRO) || (p->sbc_type == BOTH)) && (ISCHIPPRO == 1)) { - rtnval = 1; - } else { - rtnval = 0; - } + + // Loop through the pins + for (p = pins_info; p->key != NULL; ++p) { + tmpgpio = gpio_number(p); + if (tmpgpio == gpio) { + if (DEBUG) + printf(" ** gpio_allowed: found match\n"); + // We have a CHIP and the pin is for CHIP/BOTH + if (((p->sbc_type == CHIP) || (p->sbc_type == BOTH)) && (is_this_chippro() == 0)) { + if (DEBUG) + printf(" ** gpio_allowed: pin allowed for chip or bth and we're a chip\n"); + rtnval = 1; + // We have a CHIP Pro and the pin is for CHIPPRO/BOTH + } else if (((p->sbc_type == CHIPPRO) || (p->sbc_type == BOTH)) && (is_this_chippro() == 1)) { + if (DEBUG) + printf(" ** gpio_allowed: pin allowed for chip pro or both and we're a chip pro\n"); + rtnval = 1; + } else { + if (DEBUG) + printf(" ** gpio_allowed: pin is not allowed on hardware\n"); + rtnval = 0; } } } + return rtnval; } @@ -299,24 +281,30 @@ int pwm_allowed(const char *key) pins_t *p; // Determine if we are CHIP Pro // Running because we cannot be sure if it was previously run - int iscpro = is_this_chippro(); // If the return is good, we should be good to go, so let's check the data - if (iscpro != -1) { - for (p = pins_info; p->key != NULL; ++p) { - if (strcmp(p->key, key) == 0) { - // We have a CHIP and the pin is for CHIP/BOTH - if ((p->sbc_type == BOTH) && (ISCHIPPRO == 0)) { - rtnval = 1; - // We have a CHIP Pro and the pin is for CHIPPRO/BOTH - } else if (((p->sbc_type == CHIPPRO) || (p->sbc_type == BOTH)) && (ISCHIPPRO == 1)) { - rtnval = 1; - } else { - rtnval = 0; - } + for (p = pins_info; p->key != NULL; ++p) { + if (strcmp(p->key, key) == 0) { + if (DEBUG) + printf(" ** pwm_allowed: found match\n"); + // We have a CHIP and the pin is for CHIP/BOTH + if ((p->sbc_type == BOTH) && (is_this_chippro() == 0)) { + if (DEBUG) + printf(" ** pwm_allowed: pwm allowed for chip or bth and we're a chip\n"); + rtnval = 1; + // We have a CHIP Pro and the pin is for CHIPPRO/BOTH + } else if (((p->sbc_type == CHIPPRO) || (p->sbc_type == BOTH)) && (is_this_chippro() == 1)) { + if (DEBUG) + printf(" ** pwm_allowed: pwm allowed for chip pro or both and we're a chip pro\n"); + rtnval = 1; + } else { + if (DEBUG) + printf(" ** pwm_allowed: pwm is not allowed on hardware\n"); + rtnval = 0; } } } + return rtnval; } diff --git a/source/common.h b/source/common.h index 2248cfc..1df0627 100644 --- a/source/common.h +++ b/source/common.h @@ -89,7 +89,6 @@ typedef struct dyn_int_array_s dyn_int_array_t; int setup_error; int module_setup; int DEBUG; -int ISCHIPPRO; int get_xio_base(void); int is_this_chippro(void); diff --git a/source/py_gpio.c b/source/py_gpio.c index f1d9013..677cccc 100644 --- a/source/py_gpio.c +++ b/source/py_gpio.c @@ -43,6 +43,7 @@ SOFTWARE. #include "event_gpio.h" static int gpio_warnings = 1; +static int r8_mem_setup = 0; int max_gpio = -1; dyn_int_array_t *gpio_direction = NULL; @@ -70,22 +71,6 @@ static PyObject *py_toggle_debug(PyObject *self, PyObject *args) static int init_module(void) { clear_error_msg(); - - if (map_pio_memory() < 0) { - char err[2000]; - snprintf(err, sizeof(err), "init_module error (%s)", get_error_msg()); - PyErr_SetString(PyExc_RuntimeError, err); - return 0; - } - - // figure out if we're a chip pro - if (is_this_chippro() < 1) { - char err[2000]; - snprintf(err, sizeof(err), "init_module error (%s)", get_error_msg()); - PyErr_SetString(PyExc_RuntimeError, err); - return 0; - } - // After this point, ISCHIPPRO variable should be good to go // If we make it here, we're good to go if (DEBUG) @@ -95,16 +80,34 @@ static int init_module(void) return 0; } +static int init_r8_gpio_mem(void) +{ + clear_error_msg(); + + if (DEBUG) + printf(" ** init_r8_gpio_mem: mapping memory **\n"); + + if (map_pio_memory() < 0) { + char err[2000]; + snprintf(err, sizeof(err), "init_r8_gpio_mem error (%s)", get_error_msg()); + PyErr_SetString(PyExc_RuntimeError, err); + return 0; + } + + // If we make it here, we're good to go + if (DEBUG) + printf(" ** init_r8_gpio_mem: setup complete **\n"); + r8_mem_setup = 1; + + return 0; +} + // python function value = is_chip_pro static PyObject *py_is_chip_pro(PyObject *self, PyObject *args) { PyObject *py_value; - if (!module_setup) { - init_module(); - } - - py_value = Py_BuildValue("i", ISCHIPPRO); + py_value = Py_BuildValue("i", is_this_chippro()); return py_value; } @@ -206,6 +209,11 @@ static PyObject *py_setup_channel(PyObject *self, PyObject *args, PyObject *kwar PyErr_SetString(PyExc_ValueError, err); return NULL; } + + // Only map /dev/mem if we're not an XIO + if (!r8_mem_setup && !(gpio >= lookup_gpio_by_name("XIO-P0") && gpio <= lookup_gpio_by_name("XIO-P7"))) { + init_r8_gpio_mem(); + } if (gpio_export(gpio) < 0) { char err[2000]; diff --git a/source/py_pwm.c b/source/py_pwm.c index 6019b36..2cf4277 100644 --- a/source/py_pwm.c +++ b/source/py_pwm.c @@ -55,15 +55,6 @@ static PyObject *py_toggle_debug(PyObject *self, PyObject *args) static int init_module(void) { clear_error_msg(); - - // figure out if we're a chip pro - if (is_this_chippro() < 1) { - char err[2000]; - snprintf(err, sizeof(err), "init_module error (%s)", get_error_msg()); - PyErr_SetString(PyExc_RuntimeError, err); - return 0; - } - // After this point, ISCHIPPRO variable should be good to go // If we make it here, we're good to go if (DEBUG) @@ -76,11 +67,9 @@ static int init_module(void) // python function value = is_chip_pro static PyObject *py_is_chip_pro(PyObject *self, PyObject *args) { - int is_cpro; PyObject *py_value; - is_cpro = is_this_chippro(); - py_value = Py_BuildValue("i", is_cpro); + py_value = Py_BuildValue("i", is_this_chippro()); return py_value; } diff --git a/source/py_servo.c b/source/py_servo.c index 6874521..89b96fc 100644 --- a/source/py_servo.c +++ b/source/py_servo.c @@ -60,15 +60,6 @@ static int init_module(void) { clear_error_msg(); - // figure out if we're a chip pro - if (is_this_chippro() < 1) { - char err[2000]; - snprintf(err, sizeof(err), "init_module error (%s)", get_error_msg()); - PyErr_SetString(PyExc_RuntimeError, err); - return 0; - } - // After this point, ISCHIPPRO variable should be good to go - // If we make it here, we're good to go if (DEBUG) printf(" ** init_module: setup complete **\n"); @@ -82,11 +73,7 @@ static PyObject *py_is_chip_pro(PyObject *self, PyObject *args) { PyObject *py_value; - if (!module_setup) { - init_module(); - } - - py_value = Py_BuildValue("i", ISCHIPPRO); + py_value = Py_BuildValue("i", is_this_chippro()); return py_value; } diff --git a/source/py_softpwm.c b/source/py_softpwm.c index 6913824..10aea16 100644 --- a/source/py_softpwm.c +++ b/source/py_softpwm.c @@ -55,15 +55,6 @@ static PyObject *py_cleanup(PyObject *self, PyObject *args) static int init_module(void) { clear_error_msg(); - - // figure out if we're a chip pro - if (is_this_chippro() < 1) { - char err[2000]; - snprintf(err, sizeof(err), "init_module error (%s)", get_error_msg()); - PyErr_SetString(PyExc_RuntimeError, err); - return 0; - } - // After this point, ISCHIPPRO variable should be good to go // If we make it here, we're good to go if (DEBUG) @@ -77,12 +68,8 @@ static int init_module(void) static PyObject *py_is_chip_pro(PyObject *self, PyObject *args) { PyObject *py_value; - - if (!module_setup) { - init_module(); - } - py_value = Py_BuildValue("i", ISCHIPPRO); + py_value = Py_BuildValue("i", is_this_chippro()); return py_value; }