1
0
mirror of https://github.com/xtacocorex/CHIP_IO synced 2025-07-20 04:43:21 +00:00

Initial PWM support for the CHIP even though the base .dtb does not support, this is untested

This commit is contained in:
Robert Wolterman
2016-02-25 22:38:58 -06:00
parent 91918f8f36
commit 9d593828ae
6 changed files with 546 additions and 41 deletions

View File

@ -20,7 +20,7 @@ classifiers = ['Development Status :: 3 - Alpha',
'Topic :: System :: Hardware'] 'Topic :: System :: Hardware']
setup(name = 'CHIP_IO', setup(name = 'CHIP_IO',
version = '0.0.2', version = '0.0.5',
author = 'Robert Wolterman', author = 'Robert Wolterman',
author_email = 'robert.wolterman@gmail.com', author_email = 'robert.wolterman@gmail.com',
description = 'A module to control CHIP IO channels', description = 'A module to control CHIP IO channels',
@ -30,7 +30,7 @@ setup(name = 'CHIP_IO',
url = 'https://github.com/xtacocorex/CHIP_IO/', url = 'https://github.com/xtacocorex/CHIP_IO/',
classifiers = classifiers, classifiers = classifiers,
packages = find_packages(), 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'])]) #, 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.ADC', ['source/py_adc.c', 'source/c_adc.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']),
# Extension('CHIP_IO.SPI', ['source/spimodule.c', 'source/constants.c', 'source/common.c'], extra_compile_args=['-Wno-format-security'])]) # Extension('CHIP_IO.SPI', ['source/spimodule.c', 'source/constants.c', 'source/common.c'], extra_compile_args=['-Wno-format-security'])])

291
source/c_pwm.c Normal file
View File

@ -0,0 +1,291 @@
/*
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 <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <string.h>
#include <fcntl.h>
#include <unistd.h>
#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;
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 pwm_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 pwm_set_polarity(const char *key, int polarity) {
int len;
char buffer[7]; /* allow room for trailing NUL byte */
struct pwm_exp *pwm;
pwm = lookup_exported_pwm(key);
if (pwm == NULL) {
return -1;
}
len = snprintf(buffer, sizeof(buffer), "%d", polarity);
write(pwm->polarity_fd, buffer, len);
return 0;
}
int pwm_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 pwm_start(const char *key, float duty, float freq, int polarity)
{
char pwm_base_path[45];
char period_path[50];
char duty_path[50];
char polarity_path[55];
int period_fd, duty_fd, polarity_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(period_path, sizeof(period_path), "%s/period", pwm_base_path);
snprintf(duty_path, sizeof(duty_path), "%s/duty", pwm_base_path);
snprintf(polarity_path, sizeof(polarity_path), "%s/polarity", pwm_base_path);
//add period and duty fd to pwm list
if ((period_fd = open(period_path, O_RDWR)) < 0)
return -1;
if ((duty_fd = open(duty_path, O_RDWR)) < 0) {
//error, close already opened period_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(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->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;
}
pwm_set_frequency(key, freq);
pwm_set_polarity(key, polarity);
pwm_set_duty_cycle(key, duty);
return 1;
}
int pwm_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;
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->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 pwm_cleanup(void)
{
while (exported_pwms != NULL) {
pwm_disable(exported_pwms->key);
}
}

36
source/c_pwm.h Normal file
View File

@ -0,0 +1,36 @@
/*
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 pwm_start(const char *key, float duty, float freq, int polarity);
int pwm_disable(const char *key);
int pwm_set_frequency(const char *key, float freq);
int pwm_set_duty_cycle(const char *key, float duty);
void pwm_cleanup(void);

View File

@ -71,7 +71,7 @@ pins_t table[] = {
{ "Y1", "U13_15", 0, -1, -1}, { "Y1", "U13_15", 0, -1, -1},
{ "Y2", "U13_16", 0, -1, -1}, { "Y2", "U13_16", 0, -1, -1},
{ "LCD-D2", "U13_17", 98, -1, -1}, { "LCD-D2", "U13_17", 98, -1, -1},
{ "PWM0", "U13_18", 34, -1, -1}, { "PWM0", "U13_18", 34, 0, -1},
{ "LCD-D4", "U13_19", 100, -1, -1}, { "LCD-D4", "U13_19", 100, -1, -1},
{ "LCD-D3", "U13_20", 99, -1, -1}, { "LCD-D3", "U13_20", 99, -1, -1},
{ "LCD-D6", "U13_21", 102, -1, -1}, { "LCD-D6", "U13_21", 102, -1, -1},
@ -104,7 +104,7 @@ pins_t table[] = {
{ "HPR", "U14_8", 0, -1, -1}, { "HPR", "U14_8", 0, -1, -1},
{ "VCC-3V3", "U14_9", 0, -1, -1}, { "VCC-3V3", "U14_9", 0, -1, -1},
{ "MICM", "U14_10", 0, -1, -1}, { "MICM", "U14_10", 0, -1, -1},
{ "LRADC", "U14_11", 0, -1, -1}, { "LRADC", "U14_11", 0, -1, 0},
{ "MICIN1", "U14_12", 0, -1, -1}, { "MICIN1", "U14_12", 0, -1, -1},
{ "XIO-P0", "U14_13", 408, -1, -1}, { "XIO-P0", "U14_13", 408, -1, -1},
{ "XIO-P1", "U14_14", 409, -1, -1}, { "XIO-P1", "U14_14", 409, -1, -1},

214
source/py_pwm.c Normal file
View File

@ -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_pwm.h"
// python function cleanup()
static PyObject *py_cleanup(PyObject *self, PyObject *args)
{
// unexport the PWM
pwm_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_pwm_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 (!pwm_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_pwm_key(channel, key)) {
PyErr_SetString(PyExc_ValueError, "Invalid PWM key or name.");
return NULL;
}
pwm_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_pwm_key(channel, key)) {
PyErr_SetString(PyExc_ValueError, "Invalid PWM key or name.");
return NULL;
}
if (pwm_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)
{
PyErr_SetString(PyExc_ValueError, "frequency must be greater than 0.0");
return NULL;
}
if (!get_pwm_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[] = "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 'PWM0', or 'U13_18'"},
{"stop", (PyCFunction)py_stop_channel, METH_VARARGS | METH_KEYWORDS, "Stop the PWM channel. channel can be in the form of 'PWM0', or 'U13_18'"},
{ "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 bbpwmmodule = {
PyModuleDef_HEAD_INIT,
"PWM", // 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_PWM(void)
#else
PyMODINIT_FUNC initPWM(void)
#endif
{
PyObject *module = NULL;
#if PY_MAJOR_VERSION > 2
if ((module = PyModule_Create(&bbpwmmodule)) == NULL)
return NULL;
#else
if ((module = Py_InitModule3("PWM", pwm_methods, moduledocstring)) == NULL)
return;
#endif
define_constants(module);
#if PY_MAJOR_VERSION > 2
return module;
#else
return;
#endif
}

View File

@ -1,36 +0,0 @@
try:
from overlays import builder
builder.compile()
builder.copy()
except:
pass
import distribute_setup
distribute_setup.use_setuptools()
from setuptools import setup, Extension, find_packages
classifiers = ['Development Status :: 3 - Alpha',
'Operating System :: POSIX :: Linux',
'License :: OSI Approved :: MIT License',
'Intended Audience :: Developers',
'Programming Language :: Python :: 2.7',
'Programming Language :: Python :: 3',
'Topic :: Software Development',
'Topic :: Home Automation',
'Topic :: System :: Hardware']
setup(name = 'CHIP_IO',
version = '0.0.4',
author = 'Robert Wolterman',
author_email = 'robert.wolterman@gmail.com',
description = 'A module to control CHIP IO channels',
long_description = open('README.md').read() + open('CHANGELOG.rst').read(),
license = 'MIT',
keywords = 'CHIP NextThingCo IO GPIO PWM ADC',
url = 'https://github.com/xtacocorex/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.ADC', ['source/py_adc.c', 'source/c_adc.c', 'source/constants.c', 'source/common.c'], extra_compile_args=['-Wno-format-security']),
# Extension('CHIP_IO.SPI', ['source/spimodule.c', 'source/constants.c', 'source/common.c'], extra_compile_args=['-Wno-format-security'])])