From 9ac22c32cd2dca8d0249d4fe40b0a7278492156e Mon Sep 17 00:00:00 2001 From: "Brady L. Hurlburt" Date: Thu, 7 Apr 2016 20:19:38 +0000 Subject: [PATCH 01/13] Adds softpwm files --- setup.py | 3 +- source/c_pwm.c | 2 +- source/c_softpwm.c | 344 ++++++++++++++++++++++++++++++++++++++++++++ source/c_softpwm.h | 37 +++++ source/common.c | 47 +++++- source/common.h | 1 + source/py_softpwm.c | 214 +++++++++++++++++++++++++++ 7 files changed, 640 insertions(+), 8 deletions(-) create mode 100644 source/c_softpwm.c create mode 100644 source/c_softpwm.h create mode 100644 source/py_softpwm.c diff --git a/setup.py b/setup.py index 54b441d..0703fb6 100644 --- a/setup.py +++ b/setup.py @@ -31,5 +31,6 @@ setup(name = 'CHIP_IO', classifiers = classifiers, packages = find_packages(), ext_modules = [Extension('CHIP_IO.GPIO', ['source/py_gpio.c', 'source/event_gpio.c', 'source/constants.c', 'source/common.c'], extra_compile_args=['-Wno-format-security']), - Extension('CHIP_IO.PWM', ['source/py_pwm.c', 'source/c_pwm.c', 'source/constants.c', 'source/common.c'], extra_compile_args=['-Wno-format-security'])]) #, + Extension('CHIP_IO.PWM', ['source/py_pwm.c', 'source/c_pwm.c', 'source/constants.c', 'source/common.c'], extra_compile_args=['-Wno-format-security']), + Extension('CHIP_IO.SOFTPWM', ['source/py_softpwm.c', 'source/c_softpwm.c', 'source/constants.c', 'source/common.c'], extra_compile_args=['-Wno-format-security'])]) #, # Extension('CHIP_IO.ADC', ['source/py_adc.c', 'source/c_adc.c', 'source/constants.c', 'source/common.c'], extra_compile_args=['-Wno-format-security']), diff --git a/source/c_pwm.c b/source/c_pwm.c index c38657c..b4aa3e0 100644 --- a/source/c_pwm.c +++ b/source/c_pwm.c @@ -147,7 +147,7 @@ int pwm_set_polarity(const char *key, int polarity) { else { len = snprintf(buffer, sizeof(buffer), "%s", "inverted"); - } + } write(pwm->polarity_fd, buffer, len); return 0; diff --git a/source/c_softpwm.c b/source/c_softpwm.c new file mode 100644 index 0000000..81b0fba --- /dev/null +++ b/source/c_softpwm.c @@ -0,0 +1,344 @@ +/* +Copyright (c) 2016 Robert Wolterman + +Original BBIO Author Justin Cooper +Modified for CHIP_IO Author Robert Wolterman + +This file incorporates work covered by the following copyright and +permission notice, all modified code adopts the original license: + +Copyright (c) 2013 Adafruit +Author: Justin Cooper + +Permission is hereby granted, free of charge, to any person obtaining a copy of +this software and associated documentation files (the "Software"), to deal in +the Software without restriction, including without limitation the rights to +use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies +of the Software, and to permit persons to whom the Software is furnished to do +so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. +*/ + +#include +#include +#include +#include +#include +#include +#include "c_pwm.h" +#include "common.h" + +#define KEYLEN 7 + +#define PERIOD 0 +#define DUTY 1 + +int pwm_initialized = 0; + +// pwm exports +struct pwm_exp +{ + char key[KEYLEN+1]; /* leave room for terminating NUL byte */ + int period_fd; + int duty_fd; + int polarity_fd; + int enable_fd; + int enable; + unsigned long duty; + unsigned long period_ns; + struct pwm_exp *next; +}; +struct pwm_exp *exported_pwms = NULL; + +struct pwm_exp *lookup_exported_pwm(const char *key) +{ + struct pwm_exp *pwm = exported_pwms; + + while (pwm != NULL) + { + if (strcmp(pwm->key, key) == 0) { + return pwm; + } + pwm = pwm->next; + } + + return NULL; /* standard for pointers */ +} + +int initialize_pwm(void) +{ + if (!pwm_initialized) { + int fd, len; + char str_gpio[2]; + // Per https://github.com/NextThingCo/CHIP-linux/pull/4 + // we need to export 0 here to enable pwm0 + int gpio = 0; + + if ((fd = open("/sys/class/pwm/pwmchip0/export", O_WRONLY)) < 0) + { + return -1; + } + len = snprintf(str_gpio, sizeof(str_gpio), "%d", gpio); + write(fd, str_gpio, len); + close(fd); + + pwm_initialized = 1; + return 1; + } + + return 0; +} + +int softpwm_set_frequency(const char *key, float freq) { + int len; + char buffer[20]; + unsigned long period_ns; + struct pwm_exp *pwm; + + if (freq <= 0.0) + return -1; + + pwm = lookup_exported_pwm(key); + + if (pwm == NULL) { + return -1; + } + + period_ns = (unsigned long)(1e9 / freq); + + if (period_ns != pwm->period_ns) { + pwm->period_ns = period_ns; + + len = snprintf(buffer, sizeof(buffer), "%lu", period_ns); + write(pwm->period_fd, buffer, len); + } + + return 1; +} + +int softpwm_set_polarity(const char *key, int polarity) { + int len; + char buffer[9]; /* allow room for trailing NUL byte */ + struct pwm_exp *pwm; + + pwm = lookup_exported_pwm(key); + + if (pwm == NULL) { + return -1; + } + + if (polarity < 0 || polarity > 1) { + return -1; + } + + if (polarity == 0) { + len = snprintf(buffer, sizeof(buffer), "%s", "normal"); + } + else + { + len = snprintf(buffer, sizeof(buffer), "%s", "inverted"); + } + write(pwm->polarity_fd, buffer, len); + + return 0; +} + +int softpwm_set_duty_cycle(const char *key, float duty) { + int len; + char buffer[20]; + struct pwm_exp *pwm; + + if (duty < 0.0 || duty > 100.0) + return -1; + + pwm = lookup_exported_pwm(key); + + if (pwm == NULL) { + return -1; + } + + pwm->duty = (unsigned long)(pwm->period_ns * (duty / 100.0)); + + len = snprintf(buffer, sizeof(buffer), "%lu", pwm->duty); + write(pwm->duty_fd, buffer, len); + + return 0; +} + +int softpwm_set_enable(const char *key, int enable) +{ + int len; + char buffer[20]; + struct pwm_exp *pwm; + + if (enable != 0 || enable != 1) + return -1; + + pwm = lookup_exported_pwm(key); + + if (pwm == NULL) { + return -1; + } + + pwm->enable = enable; + + len = snprintf(buffer, sizeof(buffer), "%d", pwm->enable); + write(pwm->enable_fd, buffer, len); + + return 0; +} + +int softpwm_start(const char *key, float duty, float freq, int polarity) +{ + char pwm_base_path[45]; + char period_path[50]; + char duty_path[50]; + char enable_path[50]; + char polarity_path[55]; + int period_fd, duty_fd, polarity_fd, enable_fd; + struct pwm_exp *new_pwm, *pwm; + + if(!pwm_initialized) { + initialize_pwm(); + } + + //setup the pwm base path, the chip only has one pwm + snprintf(pwm_base_path, sizeof(pwm_base_path), "/sys/class/pwm/pwmchip0/%d", "pwm0"); + + //create the path for the period and duty + snprintf(enable_path, sizeof(enable_path), "%s/enable", pwm_base_path); + snprintf(period_path, sizeof(period_path), "%s/period", pwm_base_path); + snprintf(duty_path, sizeof(duty_path), "%s/duty_cycle", pwm_base_path); + snprintf(polarity_path, sizeof(polarity_path), "%s/polarity", pwm_base_path); + + //add period and duty fd to pwm list + if ((enable_fd = open(enable_path, O_RDWR)) < 0) + return -1; + + if ((period_fd = open(period_path, O_RDWR)) < 0) { + close(enable_fd); + return -1; + } + + + if ((duty_fd = open(duty_path, O_RDWR)) < 0) { + //error, close already opened period_fd. + close(enable_fd); + close(period_fd); + return -1; + } + + if ((polarity_fd = open(polarity_path, O_RDWR)) < 0) { + //error, close already opened period_fd and duty_fd. + close(enable_fd); + close(period_fd); + close(duty_fd); + return -1; + } + + // add to list + new_pwm = malloc(sizeof(struct pwm_exp)); + if (new_pwm == 0) { + return -1; // out of memory + } + + strncpy(new_pwm->key, key, KEYLEN); /* can leave string unterminated */ + new_pwm->key[KEYLEN] = '\0'; /* terminate string */ + new_pwm->period_fd = period_fd; + new_pwm->duty_fd = duty_fd; + new_pwm->polarity_fd = polarity_fd; + new_pwm->enable_fd = enable_fd; + new_pwm->next = NULL; + + if (exported_pwms == NULL) + { + // create new list + exported_pwms = new_pwm; + } else { + // add to end of existing list + pwm = exported_pwms; + while (pwm->next != NULL) + pwm = pwm->next; + pwm->next = new_pwm; + } + + softpwm_set_frequency(key, freq); + softpwm_set_polarity(key, polarity); + softpwm_set_enable(key, 1); + softpwm_set_duty_cycle(key, duty); + + return 1; +} + +int softpwm_disable(const char *key) +{ + struct pwm_exp *pwm, *temp, *prev_pwm = NULL; + char fragment[18]; + + int fd, len; + char str_gpio[2]; + // Per https://github.com/NextThingCo/CHIP-linux/pull/4 + // we need to export 0 here to enable pwm0 + int gpio = 0; + + // Disable the PWM + softpwm_set_frequency(key, 0); + softpwm_set_polarity(key, 0); + softpwm_set_enable(key, 0); + softpwm_set_duty_cycle(key, 0); + + if ((fd = open("/sys/class/pwm/pwmchip0/unexport", O_WRONLY)) < 0) + { + return -1; + } + len = snprintf(str_gpio, sizeof(str_gpio), "%d", gpio); + write(fd, str_gpio, len); + close(fd); + + // remove from list + pwm = exported_pwms; + while (pwm != NULL) + { + if (strcmp(pwm->key, key) == 0) + { + //close the fd + close(pwm->enable_fd); + close(pwm->period_fd); + close(pwm->duty_fd); + close(pwm->polarity_fd); + + if (prev_pwm == NULL) + { + exported_pwms = pwm->next; + prev_pwm = pwm; + } else { + prev_pwm->next = pwm->next; + } + + temp = pwm; + pwm = pwm->next; + free(temp); + } else { + prev_pwm = pwm; + pwm = pwm->next; + } + } + return 0; +} + +void softpwm_cleanup(void) +{ + while (exported_pwms != NULL) { + softpwm_disable(exported_pwms->key); + } +} diff --git a/source/c_softpwm.h b/source/c_softpwm.h new file mode 100644 index 0000000..62fa737 --- /dev/null +++ b/source/c_softpwm.h @@ -0,0 +1,37 @@ +/* +Copyright (c) 2016 Robert Wolterman + +Original BBIO Author Justin Cooper +Modified for CHIP_IO Author Robert Wolterman + +This file incorporates work covered by the following copyright and +permission notice, all modified code adopts the original license: + +Copyright (c) 2013 Adafruit +Author: Justin Cooper + +Permission is hereby granted, free of charge, to any person obtaining a copy of +this software and associated documentation files (the "Software"), to deal in +the Software without restriction, including without limitation the rights to +use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies +of the Software, and to permit persons to whom the Software is furnished to do +so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. +*/ + +int softpwm_start(const char *key, float duty, float freq, int polarity); +int softpwm_disable(const char *key); +int softpwm_set_frequency(const char *key, float freq); +int softpwm_set_duty_cycle(const char *key, float duty); +int softpwm_set_enable(const char *key, int enable); +void softpwm_cleanup(void); diff --git a/source/common.c b/source/common.c index 2f3e7dd..91f8d58 100644 --- a/source/common.c +++ b/source/common.c @@ -187,7 +187,20 @@ int lookup_ain_by_name(const char *name) } } return -1; -} +} + +int copy_key_by_key(const char *input_key, char *key) +{ + pins_t *p; + for (p = table; p->key != NULL; ++p) { + if (strcmp(p->key, input_key) == 0) { + strncpy(key, p->key, 7); + key[7] = '\0'; + return 1; + } + } + return 0; +} int copy_pwm_key_by_key(const char *input_key, char *key) { @@ -204,9 +217,22 @@ int copy_pwm_key_by_key(const char *input_key, char *key) } } return 0; -} +} + +int get_key_by_name(const char *name, char *key) +{ + pins_t *p; + for (p = table; p->name != NULL; ++p) { + if (strcmp(p->name, name) == 0) { + strncpy(key, p->key, 7); + key[7] = '\0'; + return 1; + } + } + return 0; +} -int get_pwm_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) { @@ -232,7 +258,16 @@ int get_gpio_number(const char *key, unsigned int *gpio) } return 0; -} +} + +int get_key(const char *input, char *key) +{ + if (!copy_key_by_key(input, key)) { + return get_key_by_name(input, key); + } + + return 1; +} int get_pwm_key(const char *input, char *key) { @@ -241,8 +276,8 @@ int get_pwm_key(const char *input, char *key) } return 1; -} - +} + int get_adc_ain(const char *key, unsigned int *ain) { *ain = lookup_ain_by_key(key); diff --git a/source/common.h b/source/common.h index b6f0755..9e97d21 100644 --- a/source/common.h +++ b/source/common.h @@ -52,6 +52,7 @@ char ocp_dir[25]; int get_gpio_number(const char *key, unsigned int *gpio); int get_pwm_key(const char *input, char *key); +int get_key(const char *input, char *key); int get_adc_ain(const char *key, unsigned int *ain); int build_path(const char *partial_path, const char *prefix, char *full_path, size_t full_path_len); int get_spi_bus_path_number(unsigned int spi); diff --git a/source/py_softpwm.c b/source/py_softpwm.c new file mode 100644 index 0000000..f25ef69 --- /dev/null +++ b/source/py_softpwm.c @@ -0,0 +1,214 @@ +/* +Copyright (c) 2016 Robert Wolterman + +Original BBIO Author Justin Cooper +Modified for CHIP_IO Author Robert Wolterman + +This file incorporates work covered by the following copyright and +permission notice, all modified code adopts the original license: + +Copyright (c) 2013 Adafruit +Author: Justin Cooper + +Permission is hereby granted, free of charge, to any person obtaining a copy of +this software and associated documentation files (the "Software"), to deal in +the Software without restriction, including without limitation the rights to +use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies +of the Software, and to permit persons to whom the Software is furnished to do +so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. +*/ + +#include "Python.h" +#include "constants.h" +#include "common.h" +#include "c_softpwm.h" + +// python function cleanup() +static PyObject *py_cleanup(PyObject *self, PyObject *args) +{ + // unexport the PWM + softpwm_cleanup(); + + Py_RETURN_NONE; +} + +// python function start(channel, duty_cycle, freq) +static PyObject *py_start_channel(PyObject *self, PyObject *args, PyObject *kwargs) +{ + char key[8]; + char *channel; + float frequency = 2000.0; + float duty_cycle = 0.0; + int polarity = 0; + static char *kwlist[] = {"channel", "duty_cycle", "frequency", "polarity", NULL}; + + if (!PyArg_ParseTupleAndKeywords(args, kwargs, "s|ffi", kwlist, &channel, &duty_cycle, &frequency, &polarity)) { + return NULL; + } + + if (!get_key(channel, key)) { + PyErr_SetString(PyExc_ValueError, "Invalid PWM key or name."); + return NULL; + } + + if (duty_cycle < 0.0 || duty_cycle > 100.0) + { + PyErr_SetString(PyExc_ValueError, "duty_cycle must have a value from 0.0 to 100.0"); + return NULL; + } + + if (frequency <= 0.0) + { + PyErr_SetString(PyExc_ValueError, "frequency must be greater than 0.0"); + return NULL; + } + + if (polarity < 0 || polarity > 1) { + PyErr_SetString(PyExc_ValueError, "polarity must be either 0 or 1"); + return NULL; + } + + if (!softpwm_start(key, duty_cycle, frequency, polarity)) + return NULL; + + Py_RETURN_NONE; +} + +// python function stop(channel) +static PyObject *py_stop_channel(PyObject *self, PyObject *args, PyObject *kwargs) +{ + char key[8]; + char *channel; + + if (!PyArg_ParseTuple(args, "s", &channel)) + return NULL; + + if (!get_key(channel, key)) { + PyErr_SetString(PyExc_ValueError, "Invalid PWM key or name."); + return NULL; + } + + softpwm_disable(key); + + Py_RETURN_NONE; +} + +// python method PWM.set_duty_cycle(channel, duty_cycle) +static PyObject *py_set_duty_cycle(PyObject *self, PyObject *args, PyObject *kwargs) +{ + char key[8]; + char *channel; + float duty_cycle = 0.0; + static char *kwlist[] = {"channel", "duty_cycle", NULL}; + + if (!PyArg_ParseTupleAndKeywords(args, kwargs, "s|f", kwlist, &channel, &duty_cycle)) + return NULL; + + if (duty_cycle < 0.0 || duty_cycle > 100.0) + { + PyErr_SetString(PyExc_ValueError, "duty_cycle must have a value from 0.0 to 100.0"); + return NULL; + } + + if (!get_key(channel, key)) { + PyErr_SetString(PyExc_ValueError, "Invalid PWM key or name."); + return NULL; + } + + if (softpwm_set_duty_cycle(key, duty_cycle) == -1) { + PyErr_SetString(PyExc_RuntimeError, "You must start() the PWM channel first"); + return NULL; + } + + Py_RETURN_NONE; +} + +// python method PWM.set_frequency(channel, frequency) +static PyObject *py_set_frequency(PyObject *self, PyObject *args, PyObject *kwargs) +{ + char key[8]; + char *channel; + float frequency = 1.0; + static char *kwlist[] = {"channel", "frequency", NULL}; + + if (!PyArg_ParseTupleAndKeywords(args, kwargs, "s|f", kwlist, &channel, &frequency)) + return NULL; + + if ((frequency <= 0.0) || (frequency > 10000.0)) + { + PyErr_SetString(PyExc_ValueError, "frequency must be greater than 0.0 and less than 10000.0"); + return NULL; + } + + if (!get_key(channel, key)) { + PyErr_SetString(PyExc_ValueError, "Invalid PWM key or name."); + return NULL; + } + + if (pwm_set_frequency(key, frequency) == -1) { + PyErr_SetString(PyExc_RuntimeError, "You must start() the PWM channel first"); + return NULL; + } + + Py_RETURN_NONE; +} + + +static const char moduledocstring[] = "Software PWM functionality of a CHIP using Python"; + +PyMethodDef pwm_methods[] = { + {"start", (PyCFunction)py_start_channel, METH_VARARGS | METH_KEYWORDS, "Set up and start the PWM channel. channel can be in the form of 'XIO-P0', or 'U14_13'"}, + {"stop", (PyCFunction)py_stop_channel, METH_VARARGS | METH_KEYWORDS, "Stop the PWM channel. channel can be in the form of 'XIO-P0', or 'U14_13'"}, + { "set_duty_cycle", (PyCFunction)py_set_duty_cycle, METH_VARARGS, "Change the duty cycle\ndutycycle - between 0.0 and 100.0" }, + { "set_frequency", (PyCFunction)py_set_frequency, METH_VARARGS, "Change the frequency\nfrequency - frequency in Hz (freq > 0.0)" }, + {"cleanup", py_cleanup, METH_VARARGS, "Clean up by resetting all GPIO channels that have been used by this program to INPUT with no pullup/pulldown and no event detection"}, + //{"setwarnings", py_setwarnings, METH_VARARGS, "Enable or disable warning messages"}, + {NULL, NULL, 0, NULL} +}; + +#if PY_MAJOR_VERSION > 2 +static struct PyModuleDef chippwmmodule = { + PyModuleDef_HEAD_INIT, + "SOFTPWM", // name of module + moduledocstring, // module documentation, may be NULL + -1, // size of per-interpreter state of the module, or -1 if the module keeps state in global variables. + pwm_methods +}; +#endif + +#if PY_MAJOR_VERSION > 2 +PyMODINIT_FUNC PyInit_SOFTPWM(void) +#else +PyMODINIT_FUNC initSOFTPWM(void) +#endif +{ + PyObject *module = NULL; + +#if PY_MAJOR_VERSION > 2 + if ((module = PyModule_Create(&chippwmmodule)) == NULL) + return NULL; +#else + if ((module = Py_InitModule3("SOFTPWM", pwm_methods, moduledocstring)) == NULL) + return; +#endif + + define_constants(module); + + +#if PY_MAJOR_VERSION > 2 + return module; +#else + return; +#endif +} From 9645d2aae352e7dbbecf853c1d59611add7fa67a Mon Sep 17 00:00:00 2001 From: "Brady L. Hurlburt" Date: Sun, 10 Apr 2016 16:45:13 -0400 Subject: [PATCH 02/13] Sets up softpwm struct and thread --- source/c_softpwm.c | 157 ++++++++++++++++++--------------------------- 1 file changed, 64 insertions(+), 93 deletions(-) diff --git a/source/c_softpwm.c b/source/c_softpwm.c index 81b0fba..54347dc 100644 --- a/source/c_softpwm.c +++ b/source/c_softpwm.c @@ -35,8 +35,11 @@ SOFTWARE. #include #include #include +#include +#include #include "c_pwm.h" #include "common.h" +#include "event_gpio.h" #define KEYLEN 7 @@ -46,23 +49,19 @@ SOFTWARE. int pwm_initialized = 0; // pwm exports -struct pwm_exp +struct softpwm { char key[KEYLEN+1]; /* leave room for terminating NUL byte */ - int period_fd; - int duty_fd; - int polarity_fd; - int enable_fd; - int enable; - unsigned long duty; - unsigned long period_ns; - struct pwm_exp *next; + unsigned long on_ns; + unsigned long off_ns; + pthread_t thread; + struct softpwm *next; }; -struct pwm_exp *exported_pwms = NULL; +struct softpwm *exported_pwms = NULL; -struct pwm_exp *lookup_exported_pwm(const char *key) +struct softpwm *lookup_exported_pwm(const char *key) { - struct pwm_exp *pwm = exported_pwms; + struct softpwm *pwm = exported_pwms; while (pwm != NULL) { @@ -75,35 +74,11 @@ struct pwm_exp *lookup_exported_pwm(const char *key) return NULL; /* standard for pointers */ } -int initialize_pwm(void) -{ - if (!pwm_initialized) { - int fd, len; - char str_gpio[2]; - // Per https://github.com/NextThingCo/CHIP-linux/pull/4 - // we need to export 0 here to enable pwm0 - int gpio = 0; - - if ((fd = open("/sys/class/pwm/pwmchip0/export", O_WRONLY)) < 0) - { - return -1; - } - len = snprintf(str_gpio, sizeof(str_gpio), "%d", gpio); - write(fd, str_gpio, len); - close(fd); - - pwm_initialized = 1; - return 1; - } - - return 0; -} - int softpwm_set_frequency(const char *key, float freq) { int len; char buffer[20]; unsigned long period_ns; - struct pwm_exp *pwm; + struct softpwm *pwm; if (freq <= 0.0) return -1; @@ -129,7 +104,7 @@ int softpwm_set_frequency(const char *key, float freq) { int softpwm_set_polarity(const char *key, int polarity) { int len; char buffer[9]; /* allow room for trailing NUL byte */ - struct pwm_exp *pwm; + struct softpwm *pwm; pwm = lookup_exported_pwm(key); @@ -156,7 +131,7 @@ int softpwm_set_polarity(const char *key, int polarity) { int softpwm_set_duty_cycle(const char *key, float duty) { int len; char buffer[20]; - struct pwm_exp *pwm; + struct softpwm *pwm; if (duty < 0.0 || duty > 100.0) return -1; @@ -179,7 +154,7 @@ int softpwm_set_enable(const char *key, int enable) { int len; char buffer[20]; - struct pwm_exp *pwm; + struct softpwm *pwm; if (enable != 0 || enable != 1) return -1; @@ -198,66 +173,65 @@ int softpwm_set_enable(const char *key, int enable) return 0; } +void* softpwm_toggle(void *key) +{ + struct softpwm *pwm; + unsigned int gpio; + struct timespec tim; + unsigned int sec; + + get_gpio_number(key, &gpio); + pwm = lookup_exported_pwm((char*)key); + + while (1) { + sec = (unsigned int)(pwm->on_ns/1e9); + tim.tv_sec = sec; + tim.tv_nsec = pwm->on_ns - (sec*1e9); + gpio_set_value(gpio, HIGH); + nanosleep(&tim, NULL); + sec = (unsigned int)(pwm->off_ns/1e9); + tim.tv_sec = sec; + tim.tv_nsec = pwm->off_ns - (sec*1e9); + gpio_set_value(gpio, LOW); + nanosleep(&tim, NULL); + } +} + int softpwm_start(const char *key, float duty, float freq, int polarity) { - char pwm_base_path[45]; - char period_path[50]; - char duty_path[50]; - char enable_path[50]; - char polarity_path[55]; - int period_fd, duty_fd, polarity_fd, enable_fd; - struct pwm_exp *new_pwm, *pwm; + struct softpwm *new_pwm, *pwm; + pthread_t *new_thread; + unsigned int gpio; + unsigned long period_ns; + unsigned long on_ns; + int ret; - if(!pwm_initialized) { - initialize_pwm(); - } - - //setup the pwm base path, the chip only has one pwm - snprintf(pwm_base_path, sizeof(pwm_base_path), "/sys/class/pwm/pwmchip0/%d", "pwm0"); - - //create the path for the period and duty - snprintf(enable_path, sizeof(enable_path), "%s/enable", pwm_base_path); - snprintf(period_path, sizeof(period_path), "%s/period", pwm_base_path); - snprintf(duty_path, sizeof(duty_path), "%s/duty_cycle", pwm_base_path); - snprintf(polarity_path, sizeof(polarity_path), "%s/polarity", pwm_base_path); - - //add period and duty fd to pwm list - if ((enable_fd = open(enable_path, O_RDWR)) < 0) - return -1; - - if ((period_fd = open(period_path, O_RDWR)) < 0) { - close(enable_fd); - return -1; - } - - - if ((duty_fd = open(duty_path, O_RDWR)) < 0) { - //error, close already opened period_fd. - close(enable_fd); - close(period_fd); - return -1; - } - - if ((polarity_fd = open(polarity_path, O_RDWR)) < 0) { - //error, close already opened period_fd and duty_fd. - close(enable_fd); - close(period_fd); - close(duty_fd); - return -1; - } + get_gpio_number(key, &gpio); + gpio_export(gpio); + gpio_set_direction(gpio, OUTPUT); // add to list - new_pwm = malloc(sizeof(struct pwm_exp)); + new_pwm = malloc(sizeof(struct softpwm)); if (new_pwm == 0) { return -1; // out of memory } + // calculate period and on time from freq + period_ns = (unsigned long)(1e9 / freq); + on_ns = (unsigned long)(period_ns * (duty/100)); + + // create thread for pwm + ret = pthread_create(new_thread, NULL, softpwm_toggle, (void *)key); + if (ret){ + printf("ERROR; return code from pthread_create() is %d\n", ret); + exit(-1); + } + strncpy(new_pwm->key, key, KEYLEN); /* can leave string unterminated */ new_pwm->key[KEYLEN] = '\0'; /* terminate string */ - new_pwm->period_fd = period_fd; - new_pwm->duty_fd = duty_fd; - new_pwm->polarity_fd = polarity_fd; - new_pwm->enable_fd = enable_fd; + new_pwm->on_ns = on_ns; + new_pwm->off_ns = period_ns - on_ns; + new_pwm->thread = *new_thread; new_pwm->next = NULL; if (exported_pwms == NULL) @@ -272,17 +246,14 @@ int softpwm_start(const char *key, float duty, float freq, int polarity) pwm->next = new_pwm; } - softpwm_set_frequency(key, freq); softpwm_set_polarity(key, polarity); - softpwm_set_enable(key, 1); - softpwm_set_duty_cycle(key, duty); return 1; } int softpwm_disable(const char *key) { - struct pwm_exp *pwm, *temp, *prev_pwm = NULL; + struct softpwm *pwm, *temp, *prev_pwm = NULL; char fragment[18]; int fd, len; From d7bf982b11e692f8ca4394a9520bcb605b4b2fb5 Mon Sep 17 00:00:00 2001 From: "Brady L. Hurlburt" Date: Sat, 23 Apr 2016 19:17:56 -0400 Subject: [PATCH 03/13] Adds softpwm disabling and restructures thread --- source/c_softpwm.c | 210 ++++++++++++++++++++++---------------------- source/py_softpwm.c | 4 +- 2 files changed, 110 insertions(+), 104 deletions(-) diff --git a/source/c_softpwm.c b/source/c_softpwm.c index 54347dc..b6915a5 100644 --- a/source/c_softpwm.c +++ b/source/c_softpwm.c @@ -31,6 +31,7 @@ SOFTWARE. #include #include +#include #include #include #include @@ -43,6 +44,8 @@ SOFTWARE. #define KEYLEN 7 +#define NOTCHIPHW + #define PERIOD 0 #define DUTY 1 @@ -52,10 +55,14 @@ int pwm_initialized = 0; struct softpwm { char key[KEYLEN+1]; /* leave room for terminating NUL byte */ - unsigned long on_ns; - unsigned long off_ns; + + float duty; + float freq; pthread_t thread; struct softpwm *next; + bool enabled; + bool stop_flag; + int polarity; }; struct softpwm *exported_pwms = NULL; @@ -75,9 +82,6 @@ struct softpwm *lookup_exported_pwm(const char *key) } int softpwm_set_frequency(const char *key, float freq) { - int len; - char buffer[20]; - unsigned long period_ns; struct softpwm *pwm; if (freq <= 0.0) @@ -89,21 +93,12 @@ int softpwm_set_frequency(const char *key, float freq) { return -1; } - period_ns = (unsigned long)(1e9 / freq); - - if (period_ns != pwm->period_ns) { - pwm->period_ns = period_ns; - - len = snprintf(buffer, sizeof(buffer), "%lu", period_ns); - write(pwm->period_fd, buffer, len); - } + pwm->freq = freq; return 1; } int softpwm_set_polarity(const char *key, int polarity) { - int len; - char buffer[9]; /* allow room for trailing NUL byte */ struct softpwm *pwm; pwm = lookup_exported_pwm(key); @@ -116,21 +111,12 @@ int softpwm_set_polarity(const char *key, int polarity) { return -1; } - if (polarity == 0) { - len = snprintf(buffer, sizeof(buffer), "%s", "normal"); - } - else - { - len = snprintf(buffer, sizeof(buffer), "%s", "inverted"); - } - write(pwm->polarity_fd, buffer, len); + pwm->polarity = polarity; return 0; } -int softpwm_set_duty_cycle(const char *key, float duty) { - int len; - char buffer[20]; +int softpwm_set_duty_cycle(const char *key, float duty) {; struct softpwm *pwm; if (duty < 0.0 || duty > 100.0) @@ -142,59 +128,93 @@ int softpwm_set_duty_cycle(const char *key, float duty) { return -1; } - pwm->duty = (unsigned long)(pwm->period_ns * (duty / 100.0)); - - len = snprintf(buffer, sizeof(buffer), "%lu", pwm->duty); - write(pwm->duty_fd, buffer, len); + pwm->duty = duty; return 0; } -int softpwm_set_enable(const char *key, int enable) -{ - int len; - char buffer[20]; - struct softpwm *pwm; - - if (enable != 0 || enable != 1) - return -1; - - pwm = lookup_exported_pwm(key); - - if (pwm == NULL) { - return -1; - } - - pwm->enable = enable; - - len = snprintf(buffer, sizeof(buffer), "%d", pwm->enable); - write(pwm->enable_fd, buffer, len); - - return 0; -} - -void* softpwm_toggle(void *key) +void* softpwm_thread_toggle(void *key) { struct softpwm *pwm; unsigned int gpio; - struct timespec tim; + struct timespec tim_on; + struct timespec tim_off; unsigned int sec; + unsigned int period_ns; + unsigned int on_ns; + unsigned int off_ns; + + /* Used to determine if something has + * has changed + */ + unsigned int freq_local = 0; + unsigned int duty_local = 0; get_gpio_number(key, &gpio); pwm = lookup_exported_pwm((char*)key); - while (1) { - sec = (unsigned int)(pwm->on_ns/1e9); - tim.tv_sec = sec; - tim.tv_nsec = pwm->on_ns - (sec*1e9); - gpio_set_value(gpio, HIGH); - nanosleep(&tim, NULL); - sec = (unsigned int)(pwm->off_ns/1e9); - tim.tv_sec = sec; - tim.tv_nsec = pwm->off_ns - (sec*1e9); - gpio_set_value(gpio, LOW); - nanosleep(&tim, NULL); + while (pwm->enabled) { + /* If freq or duty has been changed, update the + * sleep times + */ + if ((freq_local != pwm->freq) || (duty_local != pwm->duty)) { + period_ns = (unsigned long)(1e9 / pwm->freq); + on_ns = (unsigned long)(period_ns * (pwm->duty/100)); + off_ns = period_ns - on_ns; + sec = (unsigned int)(on_ns/1e9); /* Intentional truncation */ + tim_on.tv_sec = sec; + tim_on.tv_nsec = on_ns - (sec*1e9); + sec = (unsigned int)(off_ns/1e9); /* Intentional truncation */ + tim_off.tv_sec = sec; + tim_off.tv_nsec = off_ns - (sec*1e9); + freq_local = pwm->freq; + duty_local = pwm->duty; + } + + /* Set gpio */ + if (pwm->polarity) + { +#ifdef NOTCHIPHW + printf("Setting gpio high"); +#else + gpio_set_value(gpio, HIGH); +#endif + + } + else + { +#ifdef NOTCHIPHW + printf("Setting gpio low"); +#else + //gpio_set_value(gpio, LOW); +#endif + } + + nanosleep(&tim_on, NULL); + + /* Unset gpio */ + if (pwm->polarity) + { +#ifdef NOTCHIPHW + printf("Setting gpio low"); +#else + gpio_set_value(gpio, LOW); +#endif + } + else + { +#ifdef NOTCHIPHW + printf("Setting gpio high"); +#else + gpio_set_value(gpio, HIGH); +#endif + } + + nanosleep(&tim_off, NULL); } + + /* This pwm has been disabled */ + pthread_exit(NULL); } int softpwm_start(const char *key, float duty, float freq, int polarity) @@ -202,13 +222,13 @@ int softpwm_start(const char *key, float duty, float freq, int polarity) struct softpwm *new_pwm, *pwm; pthread_t *new_thread; unsigned int gpio; - unsigned long period_ns; - unsigned long on_ns; int ret; get_gpio_number(key, &gpio); +#ifndef NOTCHIPHW gpio_export(gpio); gpio_set_direction(gpio, OUTPUT); +#endif // add to list new_pwm = malloc(sizeof(struct softpwm)); @@ -216,22 +236,10 @@ int softpwm_start(const char *key, float duty, float freq, int polarity) return -1; // out of memory } - // calculate period and on time from freq - period_ns = (unsigned long)(1e9 / freq); - on_ns = (unsigned long)(period_ns * (duty/100)); - - // create thread for pwm - ret = pthread_create(new_thread, NULL, softpwm_toggle, (void *)key); - if (ret){ - printf("ERROR; return code from pthread_create() is %d\n", ret); - exit(-1); - } - strncpy(new_pwm->key, key, KEYLEN); /* can leave string unterminated */ new_pwm->key[KEYLEN] = '\0'; /* terminate string */ - new_pwm->on_ns = on_ns; - new_pwm->off_ns = period_ns - on_ns; - new_pwm->thread = *new_thread; + new_pwm->enabled = false; + new_pwm->stop_flag = false; new_pwm->next = NULL; if (exported_pwms == NULL) @@ -246,47 +254,43 @@ int softpwm_start(const char *key, float duty, float freq, int polarity) pwm->next = new_pwm; } + softpwm_set_duty_cycle(key, duty); + softpwm_set_frequency(key, freq); softpwm_set_polarity(key, polarity); + // create thread for pwm + ret = pthread_create(new_thread, NULL, softpwm_thread_toggle, (void *)key); + if (ret) { + printf("ERROR; return code from pthread_create() is %d\n", ret); + exit(-1); + } + new_pwm->thread = *new_thread; + new_pwm->enabled = true; + return 1; } int softpwm_disable(const char *key) { struct softpwm *pwm, *temp, *prev_pwm = NULL; - char fragment[18]; - - int fd, len; - char str_gpio[2]; - // Per https://github.com/NextThingCo/CHIP-linux/pull/4 - // we need to export 0 here to enable pwm0 - int gpio = 0; + unsigned int gpio = 0; // Disable the PWM softpwm_set_frequency(key, 0); softpwm_set_polarity(key, 0); - softpwm_set_enable(key, 0); softpwm_set_duty_cycle(key, 0); - if ((fd = open("/sys/class/pwm/pwmchip0/unexport", O_WRONLY)) < 0) - { - return -1; - } - len = snprintf(str_gpio, sizeof(str_gpio), "%d", gpio); - write(fd, str_gpio, len); - close(fd); - // remove from list pwm = exported_pwms; while (pwm != NULL) { if (strcmp(pwm->key, key) == 0) { - //close the fd - close(pwm->enable_fd); - close(pwm->period_fd); - close(pwm->duty_fd); - close(pwm->polarity_fd); + pwm->stop_flag = true; + get_gpio_number(key, &gpio); +#ifndef NOTCHIPHW + gpio_unexport(gpio); +#endif if (prev_pwm == NULL) { diff --git a/source/py_softpwm.c b/source/py_softpwm.c index f25ef69..f99e343 100644 --- a/source/py_softpwm.c +++ b/source/py_softpwm.c @@ -53,6 +53,8 @@ static PyObject *py_start_channel(PyObject *self, PyObject *args, PyObject *kwar int polarity = 0; static char *kwlist[] = {"channel", "duty_cycle", "frequency", "polarity", NULL}; + printf("Made it to CHIPIO softpwm start"); + if (!PyArg_ParseTupleAndKeywords(args, kwargs, "s|ffi", kwlist, &channel, &duty_cycle, &frequency, &polarity)) { return NULL; } @@ -156,7 +158,7 @@ static PyObject *py_set_frequency(PyObject *self, PyObject *args, PyObject *kwar return NULL; } - if (pwm_set_frequency(key, frequency) == -1) { + if (softpwm_set_frequency(key, frequency) == -1) { PyErr_SetString(PyExc_RuntimeError, "You must start() the PWM channel first"); return NULL; } From 41203a46a6dc630c5e08d4bee38ab1facf1e0014 Mon Sep 17 00:00:00 2001 From: "Brady L. Hurlburt" Date: Sun, 24 Apr 2016 13:09:36 -0400 Subject: [PATCH 04/13] Adds mutex protection --- source/c_softpwm.c | 130 ++++++++++++++++++++++++--------------------- 1 file changed, 69 insertions(+), 61 deletions(-) diff --git a/source/c_softpwm.c b/source/c_softpwm.c index b6915a5..afa758f 100644 --- a/source/c_softpwm.c +++ b/source/c_softpwm.c @@ -44,25 +44,27 @@ SOFTWARE. #define KEYLEN 7 -#define NOTCHIPHW - #define PERIOD 0 #define DUTY 1 int pwm_initialized = 0; -// pwm exports +struct pwm_params +{ + float duty; + float freq; + bool enabled; + bool stop_flag; + int polarity; +}; + struct softpwm { char key[KEYLEN+1]; /* leave room for terminating NUL byte */ - - float duty; - float freq; + struct pwm_params params; + pthread_mutex_t* params_lock; pthread_t thread; struct softpwm *next; - bool enabled; - bool stop_flag; - int polarity; }; struct softpwm *exported_pwms = NULL; @@ -93,7 +95,9 @@ int softpwm_set_frequency(const char *key, float freq) { return -1; } - pwm->freq = freq; + pthread_mutex_lock(pwm->params_lock); + pwm->params.freq = freq; + pthread_mutex_unlock(pwm->params_lock); return 1; } @@ -111,7 +115,9 @@ int softpwm_set_polarity(const char *key, int polarity) { return -1; } - pwm->polarity = polarity; + pthread_mutex_lock(pwm->params_lock); + pwm->params.polarity = polarity; + pthread_mutex_unlock(pwm->params_lock); return 0; } @@ -128,7 +134,9 @@ int softpwm_set_duty_cycle(const char *key, float duty) {; return -1; } - pwm->duty = duty; + pthread_mutex_lock(pwm->params_lock); + pwm->params.duty = duty; + pthread_mutex_unlock(pwm->params_lock); return 0; } @@ -149,17 +157,31 @@ void* softpwm_thread_toggle(void *key) */ unsigned int freq_local = 0; unsigned int duty_local = 0; + unsigned int polarity_local = 0; + bool stop_flag_local = false; + bool enabled_local = false; + bool recalculate_timing = false; get_gpio_number(key, &gpio); pwm = lookup_exported_pwm((char*)key); - while (pwm->enabled) { + while (!stop_flag_local) { + pthread_mutex_lock(pwm->params_lock); + if ((freq_local != pwm->params.freq) || (duty_local != pwm->params.duty)) { + recalculate_timing = true; + } + freq_local = pwm->params.freq; + duty_local = pwm->params.duty; + enabled_local = pwm->params.enabled; + stop_flag_local = pwm->params.stop_flag; + polarity_local = pwm->params.polarity; + pthread_mutex_unlock(pwm->params_lock); /* If freq or duty has been changed, update the * sleep times */ - if ((freq_local != pwm->freq) || (duty_local != pwm->duty)) { - period_ns = (unsigned long)(1e9 / pwm->freq); - on_ns = (unsigned long)(period_ns * (pwm->duty/100)); + if (recalculate_timing) { + period_ns = (unsigned long)(1e9 / freq_local); + on_ns = (unsigned long)(period_ns * (duty_local/100)); off_ns = period_ns - on_ns; sec = (unsigned int)(on_ns/1e9); /* Intentional truncation */ tim_on.tv_sec = sec; @@ -167,50 +189,27 @@ void* softpwm_thread_toggle(void *key) sec = (unsigned int)(off_ns/1e9); /* Intentional truncation */ tim_off.tv_sec = sec; tim_off.tv_nsec = off_ns - (sec*1e9); - freq_local = pwm->freq; - duty_local = pwm->duty; + recalculate_timing = false; } - /* Set gpio */ - if (pwm->polarity) + if (enabled_local) { -#ifdef NOTCHIPHW - printf("Setting gpio high"); -#else - gpio_set_value(gpio, HIGH); -#endif + /* Set gpio */ + if (polarity_local) + gpio_set_value(gpio, HIGH); + else + gpio_set_value(gpio, LOW); - } - else - { -#ifdef NOTCHIPHW - printf("Setting gpio low"); -#else - //gpio_set_value(gpio, LOW); -#endif - } + nanosleep(&tim_on, NULL); - nanosleep(&tim_on, NULL); + /* Unset gpio */ + if (polarity_local) + gpio_set_value(gpio, LOW); + else + gpio_set_value(gpio, HIGH); - /* Unset gpio */ - if (pwm->polarity) - { -#ifdef NOTCHIPHW - printf("Setting gpio low"); -#else - gpio_set_value(gpio, LOW); -#endif + nanosleep(&tim_off, NULL); } - else - { -#ifdef NOTCHIPHW - printf("Setting gpio high"); -#else - gpio_set_value(gpio, HIGH); -#endif - } - - nanosleep(&tim_off, NULL); } /* This pwm has been disabled */ @@ -221,14 +220,13 @@ int softpwm_start(const char *key, float duty, float freq, int polarity) { struct softpwm *new_pwm, *pwm; pthread_t *new_thread; + pthread_mutex_t *new_params_lock; unsigned int gpio; int ret; get_gpio_number(key, &gpio); -#ifndef NOTCHIPHW gpio_export(gpio); gpio_set_direction(gpio, OUTPUT); -#endif // add to list new_pwm = malloc(sizeof(struct softpwm)); @@ -236,10 +234,17 @@ int softpwm_start(const char *key, float duty, float freq, int polarity) return -1; // out of memory } + new_params_lock = (pthread_mutex_t *)malloc(sizeof(pthread_mutex_t)); + if (new_pwm == 0) { + return -1; // out of memory + } + pthread_mutex_init(new_params_lock, NULL); + strncpy(new_pwm->key, key, KEYLEN); /* can leave string unterminated */ new_pwm->key[KEYLEN] = '\0'; /* terminate string */ - new_pwm->enabled = false; - new_pwm->stop_flag = false; + new_pwm->params.enabled = false; + new_pwm->params.stop_flag = false; + new_pwm->params_lock = new_params_lock; new_pwm->next = NULL; if (exported_pwms == NULL) @@ -265,7 +270,9 @@ int softpwm_start(const char *key, float duty, float freq, int polarity) exit(-1); } new_pwm->thread = *new_thread; - new_pwm->enabled = true; + pthread_mutex_lock(new_params_lock); + new_pwm->params.enabled = true; + pthread_mutex_unlock(new_params_lock); return 1; } @@ -286,11 +293,11 @@ int softpwm_disable(const char *key) { if (strcmp(pwm->key, key) == 0) { - pwm->stop_flag = true; + pthread_mutex_lock(pwm->params_lock); + pwm->params.stop_flag = true; + pthread_mutex_unlock(pwm->params_lock); get_gpio_number(key, &gpio); -#ifndef NOTCHIPHW gpio_unexport(gpio); -#endif if (prev_pwm == NULL) { @@ -302,6 +309,7 @@ int softpwm_disable(const char *key) temp = pwm; pwm = pwm->next; + free(temp->params_lock); free(temp); } else { prev_pwm = pwm; From 9538a4f52b7f5aa25424f1643046352a288bf55f Mon Sep 17 00:00:00 2001 From: "Brady L. Hurlburt" Date: Fri, 6 May 2016 14:47:05 +0000 Subject: [PATCH 05/13] Adds softpwm dependency on event_gpio --- setup.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/setup.py b/setup.py index 0703fb6..3c6296e 100644 --- a/setup.py +++ b/setup.py @@ -32,5 +32,5 @@ setup(name = 'CHIP_IO', packages = find_packages(), ext_modules = [Extension('CHIP_IO.GPIO', ['source/py_gpio.c', 'source/event_gpio.c', 'source/constants.c', 'source/common.c'], extra_compile_args=['-Wno-format-security']), Extension('CHIP_IO.PWM', ['source/py_pwm.c', 'source/c_pwm.c', 'source/constants.c', 'source/common.c'], extra_compile_args=['-Wno-format-security']), - Extension('CHIP_IO.SOFTPWM', ['source/py_softpwm.c', 'source/c_softpwm.c', 'source/constants.c', 'source/common.c'], extra_compile_args=['-Wno-format-security'])]) #, + Extension('CHIP_IO.SOFTPWM', ['source/py_softpwm.c', 'source/c_softpwm.c', 'source/constants.c', 'source/common.c', 'source/event_gpio.c'], extra_compile_args=['-Wno-format-security'])]) #, # Extension('CHIP_IO.ADC', ['source/py_adc.c', 'source/c_adc.c', 'source/constants.c', 'source/common.c'], extra_compile_args=['-Wno-format-security']), From 2f3172568a9f1a0db328e5f3ed19415aacffc258 Mon Sep 17 00:00:00 2001 From: "Brady L. Hurlburt" Date: Fri, 6 May 2016 15:13:48 +0000 Subject: [PATCH 06/13] Fixes bugs with creating thread. Forces 0 and 100 duty cycles --- source/c_softpwm.c | 64 +++++++++++++++++++++++++++------------------- 1 file changed, 38 insertions(+), 26 deletions(-) diff --git a/source/c_softpwm.c b/source/c_softpwm.c index afa758f..ee710f3 100644 --- a/source/c_softpwm.c +++ b/source/c_softpwm.c @@ -41,6 +41,7 @@ SOFTWARE. #include "c_pwm.h" #include "common.h" #include "event_gpio.h" +#include "Python.h" #define KEYLEN 7 @@ -141,7 +142,7 @@ int softpwm_set_duty_cycle(const char *key, float duty) {; return 0; } -void* softpwm_thread_toggle(void *key) +void *softpwm_thread_toggle(void *key) { struct softpwm *pwm; unsigned int gpio; @@ -155,13 +156,14 @@ void* softpwm_thread_toggle(void *key) /* Used to determine if something has * has changed */ - unsigned int freq_local = 0; - unsigned int duty_local = 0; + float freq_local = 0; + float duty_local = 0; unsigned int polarity_local = 0; bool stop_flag_local = false; bool enabled_local = false; bool recalculate_timing = false; + get_gpio_number(key, &gpio); pwm = lookup_exported_pwm((char*)key); @@ -194,24 +196,38 @@ void* softpwm_thread_toggle(void *key) if (enabled_local) { - /* Set gpio */ - if (polarity_local) - gpio_set_value(gpio, HIGH); - else - gpio_set_value(gpio, LOW); + + /* Force 0 duty cycle to be 0 */ + if (duty_local != 0) + { + /* Set gpio */ + if (polarity_local) + gpio_set_value(gpio, HIGH); + else + gpio_set_value(gpio, LOW); + } nanosleep(&tim_on, NULL); - /* Unset gpio */ - if (polarity_local) - gpio_set_value(gpio, LOW); - else - gpio_set_value(gpio, HIGH); + /* Force 100 duty cycle to be 100 */ + if (duty_local != 100) + { + /* Unset gpio */ + if (polarity_local) + gpio_set_value(gpio, LOW); + else + gpio_set_value(gpio, HIGH); + } nanosleep(&tim_off, NULL); } } + if (polarity_local) + gpio_set_value(gpio, LOW); + else + gpio_set_value(gpio, HIGH); + /* This pwm has been disabled */ pthread_exit(NULL); } @@ -219,7 +235,7 @@ void* softpwm_thread_toggle(void *key) int softpwm_start(const char *key, float duty, float freq, int polarity) { struct softpwm *new_pwm, *pwm; - pthread_t *new_thread; + pthread_t new_thread; pthread_mutex_t *new_params_lock; unsigned int gpio; int ret; @@ -233,7 +249,6 @@ int softpwm_start(const char *key, float duty, float freq, int polarity) if (new_pwm == 0) { return -1; // out of memory } - new_params_lock = (pthread_mutex_t *)malloc(sizeof(pthread_mutex_t)); if (new_pwm == 0) { return -1; // out of memory @@ -259,17 +274,18 @@ int softpwm_start(const char *key, float duty, float freq, int polarity) pwm->next = new_pwm; } - softpwm_set_duty_cycle(key, duty); - softpwm_set_frequency(key, freq); - softpwm_set_polarity(key, polarity); + softpwm_set_duty_cycle(new_pwm->key, duty); + softpwm_set_frequency(new_pwm->key, freq); + softpwm_set_polarity(new_pwm->key, polarity); // create thread for pwm - ret = pthread_create(new_thread, NULL, softpwm_thread_toggle, (void *)key); + ret = pthread_create(&new_thread, NULL, softpwm_thread_toggle, (void *)new_pwm->key); if (ret) { - printf("ERROR; return code from pthread_create() is %d\n", ret); + PySys_WriteStderr("DEBUG; soft_pwm ERROR IN pthread_create\n"); exit(-1); } - new_pwm->thread = *new_thread; + + new_pwm->thread = new_thread; pthread_mutex_lock(new_params_lock); new_pwm->params.enabled = true; pthread_mutex_unlock(new_params_lock); @@ -282,11 +298,6 @@ int softpwm_disable(const char *key) struct softpwm *pwm, *temp, *prev_pwm = NULL; unsigned int gpio = 0; - // Disable the PWM - softpwm_set_frequency(key, 0); - softpwm_set_polarity(key, 0); - softpwm_set_duty_cycle(key, 0); - // remove from list pwm = exported_pwms; while (pwm != NULL) @@ -297,6 +308,7 @@ int softpwm_disable(const char *key) pwm->params.stop_flag = true; pthread_mutex_unlock(pwm->params_lock); get_gpio_number(key, &gpio); + gpio_set_value(gpio, LOW); gpio_unexport(gpio); if (prev_pwm == NULL) From 9ef55ede59a219b3188285a41b30552944ad8ee1 Mon Sep 17 00:00:00 2001 From: "Brady L. Hurlburt" Date: Fri, 6 May 2016 15:14:14 +0000 Subject: [PATCH 07/13] Makes default polarity 1 --- source/py_softpwm.c | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/source/py_softpwm.c b/source/py_softpwm.c index f99e343..701cff0 100644 --- a/source/py_softpwm.c +++ b/source/py_softpwm.c @@ -50,17 +50,15 @@ static PyObject *py_start_channel(PyObject *self, PyObject *args, PyObject *kwar char *channel; float frequency = 2000.0; float duty_cycle = 0.0; - int polarity = 0; + int polarity = 1; static char *kwlist[] = {"channel", "duty_cycle", "frequency", "polarity", NULL}; - printf("Made it to CHIPIO softpwm start"); - if (!PyArg_ParseTupleAndKeywords(args, kwargs, "s|ffi", kwlist, &channel, &duty_cycle, &frequency, &polarity)) { return NULL; } if (!get_key(channel, key)) { - PyErr_SetString(PyExc_ValueError, "Invalid PWM key or name."); + PyErr_SetString(PyExc_ValueError, "Invalid SOFTPWM key or name."); return NULL; } From 67dd919fedd951338508cc781937e3ad95bf688b Mon Sep 17 00:00:00 2001 From: "Brady L. Hurlburt" Date: Fri, 6 May 2016 17:07:01 +0000 Subject: [PATCH 08/13] Adds softpwm test --- test/test_softpwm_setup.py | 104 +++++++++++++++++++++++++++++++++++++ 1 file changed, 104 insertions(+) create mode 100644 test/test_softpwm_setup.py diff --git a/test/test_softpwm_setup.py b/test/test_softpwm_setup.py new file mode 100644 index 0000000..e2a9284 --- /dev/null +++ b/test/test_softpwm_setup.py @@ -0,0 +1,104 @@ +import pytest +import os + +import CHIP_IO.SOFTPWM as PWM + +def teardown_module(module): + PWM.cleanup() + +class TestSoftpwmSetup: + def test_start_pwm(self): + PWM.start("XIO-P7", 50, 10) + assert os.path.exists('/sys/class/gpio/gpio415') + direction = open('/sys/class/gpio/gpio415/direction').read() + assert direction == 'out\n' + PWM.cleanup() + + def test_pwm_start_invalid_pwm_key(self): + with pytest.raises(ValueError): + PWM.start("P8_25", -1) + + def test_pwm_start_invalid_duty_cycle_negative(self): + with pytest.raises(ValueError): + PWM.start("XIO-P7", -1) + + def test_pwm_start_valid_duty_cycle_min(self): + #testing an exception isn't thrown + PWM.start("XIO-P7", 0) + PWM.cleanup() + + def test_pwm_start_valid_duty_cycle_max(self): + #testing an exception isn't thrown + PWM.start("XIO-P7", 100) + PWM.cleanup() + + def test_pwm_start_invalid_duty_cycle_high(self): + with pytest.raises(ValueError): + PWM.start("XIO-P7", 101) + + def test_pwm_start_invalid_duty_cycle_string(self): + with pytest.raises(TypeError): + PWM.start("XIO-P7", "1") + + def test_pwm_start_invalid_frequency_negative(self): + with pytest.raises(ValueError): + PWM.start("XIO-P7", 0, -1) + + def test_pwm_start_invalid_frequency_string(self): + with pytest.raises(TypeError): + PWM.start("XIO-P7", 0, "1") + + def test_pwm_start_negative_polarity(self): + with pytest.raises(ValueError): + PWM.start("XIO-P7", 0, 100, -1) + + def test_pwm_start_invalid_positive_polarity(self): + with pytest.raises(ValueError): + PWM.start("XIO-P7", 0, 100, 2) + + def test_pwm_start_invalid_polarity_type(self): + with pytest.raises(TypeError): + PWM.start("XIO-P7", 0, 100, "1") + + def test_pwm_duty_cycle_non_setup_key(self): + with pytest.raises(RuntimeError): + PWM.set_duty_cycle("XIO-P7", 100) + PWM.cleanup() + + def test_pwm_duty_cycle_invalid_key(self): + with pytest.raises(ValueError): + PWM.set_duty_cycle("P9_15", 100) + PWM.cleanup() + + def test_pwm_duty_cycle_invalid_value_high(self): + PWM.start("XIO-P7", 0) + with pytest.raises(ValueError): + PWM.set_duty_cycle("XIO-P7", 101) + PWM.cleanup() + + def test_pwm_duty_cycle_invalid_value_negative(self): + PWM.start("XIO-P7", 0) + with pytest.raises(ValueError): + PWM.set_duty_cycle("XIO-P7", -1) + PWM.cleanup() + + def test_pwm_duty_cycle_invalid_value_string(self): + PWM.start("XIO-P7", 0) + with pytest.raises(TypeError): + PWM.set_duty_cycle("XIO-P7", "a") + PWM.cleanup() + + def test_pwm_frequency_invalid_value_negative(self): + PWM.start("XIO-P7", 0) + with pytest.raises(ValueError): + PWM.set_frequency("XIO-P7", -1) + PWM.cleanup() + + def test_pwm_frequency_invalid_value_string(self): + PWM.start("XIO-P7", 0) + with pytest.raises(TypeError): + PWM.set_frequency("XIO-P7", "11") + PWM.cleanup() + + def test_stop_pwm(self): + pass From 0cae62be43a28c26f4d6b3bb60f3cc18f3cd3ea0 Mon Sep 17 00:00:00 2001 From: Brady Hurlburt Date: Fri, 6 May 2016 13:13:12 -0400 Subject: [PATCH 09/13] Update README.rst --- README.rst | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/README.rst b/README.rst index 4a5b220..24ea3c7 100644 --- a/README.rst +++ b/README.rst @@ -79,6 +79,24 @@ Detecting events:: #set polarity to 1 on start: PWM.start("PWM0", 50, 2000, 1) + +**SOFTPWM**:: + + import CHIP_IO.SOFTPWM as PWM + #PWM.start(channel, duty, freq=2000, polarity=0) + #duty values are valid 0 (off) to 100 (on) + #you can choose any pin + PWM.start("XIO-P7", 50) + PWM.set_duty_cycle("PWM0", 25.5) + PWM.set_frequency("PWM0", 10) + + PWM.stop("PWM0") + PWM.cleanup() + + #set polarity to 1 on start: + PWM.start("PWM0", 50, 2000, 1) + +Use SOFTPWM at low speeds (hundreds of Hz) for the best results. Do not use for anything that needs high precision or reliability. **ADC**:: From f5063c199c5be9f51935e7774ebb7fdbe5a958aa Mon Sep 17 00:00:00 2001 From: "Brady L. Hurlburt" Date: Fri, 6 May 2016 17:26:02 +0000 Subject: [PATCH 10/13] Corrects backwards polarity --- source/c_softpwm.c | 6 +++--- source/py_softpwm.c | 2 +- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/source/c_softpwm.c b/source/c_softpwm.c index ee710f3..bd81d86 100644 --- a/source/c_softpwm.c +++ b/source/c_softpwm.c @@ -201,7 +201,7 @@ void *softpwm_thread_toggle(void *key) if (duty_local != 0) { /* Set gpio */ - if (polarity_local) + if (!polarity_local) gpio_set_value(gpio, HIGH); else gpio_set_value(gpio, LOW); @@ -213,7 +213,7 @@ void *softpwm_thread_toggle(void *key) if (duty_local != 100) { /* Unset gpio */ - if (polarity_local) + if (!polarity_local) gpio_set_value(gpio, LOW); else gpio_set_value(gpio, HIGH); @@ -223,7 +223,7 @@ void *softpwm_thread_toggle(void *key) } } - if (polarity_local) + if (!polarity_local) gpio_set_value(gpio, LOW); else gpio_set_value(gpio, HIGH); diff --git a/source/py_softpwm.c b/source/py_softpwm.c index 701cff0..acb3296 100644 --- a/source/py_softpwm.c +++ b/source/py_softpwm.c @@ -50,7 +50,7 @@ static PyObject *py_start_channel(PyObject *self, PyObject *args, PyObject *kwar char *channel; float frequency = 2000.0; float duty_cycle = 0.0; - int polarity = 1; + int polarity = 0; static char *kwlist[] = {"channel", "duty_cycle", "frequency", "polarity", NULL}; if (!PyArg_ParseTupleAndKeywords(args, kwargs, "s|ffi", kwlist, &channel, &duty_cycle, &frequency, &polarity)) { From 3c912ecb726661a075dc1a3403adbc7cc49f634e Mon Sep 17 00:00:00 2001 From: "Brady L. Hurlburt" Date: Fri, 6 May 2016 17:38:51 +0000 Subject: [PATCH 11/13] Reverts accidental change to c_pwm --- source/c_pwm.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/source/c_pwm.c b/source/c_pwm.c index b4aa3e0..c38657c 100644 --- a/source/c_pwm.c +++ b/source/c_pwm.c @@ -147,7 +147,7 @@ int pwm_set_polarity(const char *key, int polarity) { else { len = snprintf(buffer, sizeof(buffer), "%s", "inverted"); - } + } write(pwm->polarity_fd, buffer, len); return 0; From bfb660e3712014ea5042057a945fcad166ac4c79 Mon Sep 17 00:00:00 2001 From: "Brady L. Hurlburt" Date: Mon, 9 May 2016 18:33:48 +0000 Subject: [PATCH 12/13] Fixes gpio name in SOFTPWM examples --- README.rst | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/README.rst b/README.rst index 24ea3c7..699675f 100644 --- a/README.rst +++ b/README.rst @@ -79,7 +79,7 @@ Detecting events:: #set polarity to 1 on start: PWM.start("PWM0", 50, 2000, 1) - + **SOFTPWM**:: import CHIP_IO.SOFTPWM as PWM @@ -87,15 +87,15 @@ Detecting events:: #duty values are valid 0 (off) to 100 (on) #you can choose any pin PWM.start("XIO-P7", 50) - PWM.set_duty_cycle("PWM0", 25.5) - PWM.set_frequency("PWM0", 10) + PWM.set_duty_cycle("XIO-P7", 25.5) + PWM.set_frequency("XIO-P7", 10) - PWM.stop("PWM0") + PWM.stop("XIO-P7") PWM.cleanup() #set polarity to 1 on start: - PWM.start("PWM0", 50, 2000, 1) - + PWM.start("XIO-P7", 50, 2000, 1) + Use SOFTPWM at low speeds (hundreds of Hz) for the best results. Do not use for anything that needs high precision or reliability. **ADC**:: From 2ebbffd137ee352df8680dec53e4850b8763a176 Mon Sep 17 00:00:00 2001 From: "Brady L. Hurlburt" Date: Mon, 9 May 2016 18:42:36 +0000 Subject: [PATCH 13/13] Edits copyright name on SOFTPWM files --- source/c_softpwm.c | 4 ++-- source/c_softpwm.h | 4 ++-- source/py_softpwm.c | 4 ++-- 3 files changed, 6 insertions(+), 6 deletions(-) diff --git a/source/c_softpwm.c b/source/c_softpwm.c index bd81d86..6bc073e 100644 --- a/source/c_softpwm.c +++ b/source/c_softpwm.c @@ -1,8 +1,8 @@ /* -Copyright (c) 2016 Robert Wolterman +Copyright (c) 2016 Brady Hurlburt Original BBIO Author Justin Cooper -Modified for CHIP_IO Author Robert Wolterman +Modified for CHIP_IO Author Brady Hurlburt This file incorporates work covered by the following copyright and permission notice, all modified code adopts the original license: diff --git a/source/c_softpwm.h b/source/c_softpwm.h index 62fa737..9be8eee 100644 --- a/source/c_softpwm.h +++ b/source/c_softpwm.h @@ -1,8 +1,8 @@ /* -Copyright (c) 2016 Robert Wolterman +Copyright (c) 2016 Brady Hurlburt Original BBIO Author Justin Cooper -Modified for CHIP_IO Author Robert Wolterman +Modified for CHIP_IO Author Brady Hurlburt This file incorporates work covered by the following copyright and permission notice, all modified code adopts the original license: diff --git a/source/py_softpwm.c b/source/py_softpwm.c index acb3296..8be3037 100644 --- a/source/py_softpwm.c +++ b/source/py_softpwm.c @@ -1,8 +1,8 @@ /* -Copyright (c) 2016 Robert Wolterman +Copyright (c) 2016 Brady Hurlburt Original BBIO Author Justin Cooper -Modified for CHIP_IO Author Robert Wolterman +Modified for CHIP_IO Author Brady Hurlburt This file incorporates work covered by the following copyright and permission notice, all modified code adopts the original license: