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

Adds softpwm disabling and restructures thread

This commit is contained in:
Brady L. Hurlburt
2016-04-23 19:17:56 -04:00
parent 9645d2aae3
commit d7bf982b11
2 changed files with 110 additions and 104 deletions

View File

@ -31,6 +31,7 @@ SOFTWARE.
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <stdbool.h>
#include <sys/types.h> #include <sys/types.h>
#include <string.h> #include <string.h>
#include <fcntl.h> #include <fcntl.h>
@ -43,6 +44,8 @@ SOFTWARE.
#define KEYLEN 7 #define KEYLEN 7
#define NOTCHIPHW
#define PERIOD 0 #define PERIOD 0
#define DUTY 1 #define DUTY 1
@ -52,10 +55,14 @@ int pwm_initialized = 0;
struct softpwm struct softpwm
{ {
char key[KEYLEN+1]; /* leave room for terminating NUL byte */ 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; pthread_t thread;
struct softpwm *next; struct softpwm *next;
bool enabled;
bool stop_flag;
int polarity;
}; };
struct softpwm *exported_pwms = NULL; 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 softpwm_set_frequency(const char *key, float freq) {
int len;
char buffer[20];
unsigned long period_ns;
struct softpwm *pwm; struct softpwm *pwm;
if (freq <= 0.0) if (freq <= 0.0)
@ -89,21 +93,12 @@ int softpwm_set_frequency(const char *key, float freq) {
return -1; return -1;
} }
period_ns = (unsigned long)(1e9 / freq); pwm->freq = 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; return 1;
} }
int softpwm_set_polarity(const char *key, int polarity) { int softpwm_set_polarity(const char *key, int polarity) {
int len;
char buffer[9]; /* allow room for trailing NUL byte */
struct softpwm *pwm; struct softpwm *pwm;
pwm = lookup_exported_pwm(key); pwm = lookup_exported_pwm(key);
@ -116,21 +111,12 @@ int softpwm_set_polarity(const char *key, int polarity) {
return -1; return -1;
} }
if (polarity == 0) { pwm->polarity = polarity;
len = snprintf(buffer, sizeof(buffer), "%s", "normal");
}
else
{
len = snprintf(buffer, sizeof(buffer), "%s", "inverted");
}
write(pwm->polarity_fd, buffer, len);
return 0; return 0;
} }
int softpwm_set_duty_cycle(const char *key, float duty) { int softpwm_set_duty_cycle(const char *key, float duty) {;
int len;
char buffer[20];
struct softpwm *pwm; struct softpwm *pwm;
if (duty < 0.0 || duty > 100.0) if (duty < 0.0 || duty > 100.0)
@ -142,59 +128,93 @@ int softpwm_set_duty_cycle(const char *key, float duty) {
return -1; return -1;
} }
pwm->duty = (unsigned long)(pwm->period_ns * (duty / 100.0)); pwm->duty = duty;
len = snprintf(buffer, sizeof(buffer), "%lu", pwm->duty);
write(pwm->duty_fd, buffer, len);
return 0; return 0;
} }
int softpwm_set_enable(const char *key, int enable) void* softpwm_thread_toggle(void *key)
{
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)
{ {
struct softpwm *pwm; struct softpwm *pwm;
unsigned int gpio; unsigned int gpio;
struct timespec tim; struct timespec tim_on;
struct timespec tim_off;
unsigned int sec; 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); get_gpio_number(key, &gpio);
pwm = lookup_exported_pwm((char*)key); pwm = lookup_exported_pwm((char*)key);
while (1) { while (pwm->enabled) {
sec = (unsigned int)(pwm->on_ns/1e9); /* If freq or duty has been changed, update the
tim.tv_sec = sec; * sleep times
tim.tv_nsec = pwm->on_ns - (sec*1e9); */
gpio_set_value(gpio, HIGH); if ((freq_local != pwm->freq) || (duty_local != pwm->duty)) {
nanosleep(&tim, NULL); period_ns = (unsigned long)(1e9 / pwm->freq);
sec = (unsigned int)(pwm->off_ns/1e9); on_ns = (unsigned long)(period_ns * (pwm->duty/100));
tim.tv_sec = sec; off_ns = period_ns - on_ns;
tim.tv_nsec = pwm->off_ns - (sec*1e9); sec = (unsigned int)(on_ns/1e9); /* Intentional truncation */
gpio_set_value(gpio, LOW); tim_on.tv_sec = sec;
nanosleep(&tim, NULL); 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) 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; struct softpwm *new_pwm, *pwm;
pthread_t *new_thread; pthread_t *new_thread;
unsigned int gpio; unsigned int gpio;
unsigned long period_ns;
unsigned long on_ns;
int ret; int ret;
get_gpio_number(key, &gpio); get_gpio_number(key, &gpio);
#ifndef NOTCHIPHW
gpio_export(gpio); gpio_export(gpio);
gpio_set_direction(gpio, OUTPUT); gpio_set_direction(gpio, OUTPUT);
#endif
// add to list // add to list
new_pwm = malloc(sizeof(struct softpwm)); 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 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 */ strncpy(new_pwm->key, key, KEYLEN); /* can leave string unterminated */
new_pwm->key[KEYLEN] = '\0'; /* terminate string */ new_pwm->key[KEYLEN] = '\0'; /* terminate string */
new_pwm->on_ns = on_ns; new_pwm->enabled = false;
new_pwm->off_ns = period_ns - on_ns; new_pwm->stop_flag = false;
new_pwm->thread = *new_thread;
new_pwm->next = NULL; new_pwm->next = NULL;
if (exported_pwms == 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; pwm->next = new_pwm;
} }
softpwm_set_duty_cycle(key, duty);
softpwm_set_frequency(key, freq);
softpwm_set_polarity(key, polarity); 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; return 1;
} }
int softpwm_disable(const char *key) int softpwm_disable(const char *key)
{ {
struct softpwm *pwm, *temp, *prev_pwm = NULL; struct softpwm *pwm, *temp, *prev_pwm = NULL;
char fragment[18]; unsigned int gpio = 0;
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 // Disable the PWM
softpwm_set_frequency(key, 0); softpwm_set_frequency(key, 0);
softpwm_set_polarity(key, 0); softpwm_set_polarity(key, 0);
softpwm_set_enable(key, 0);
softpwm_set_duty_cycle(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 // remove from list
pwm = exported_pwms; pwm = exported_pwms;
while (pwm != NULL) while (pwm != NULL)
{ {
if (strcmp(pwm->key, key) == 0) if (strcmp(pwm->key, key) == 0)
{ {
//close the fd pwm->stop_flag = true;
close(pwm->enable_fd); get_gpio_number(key, &gpio);
close(pwm->period_fd); #ifndef NOTCHIPHW
close(pwm->duty_fd); gpio_unexport(gpio);
close(pwm->polarity_fd); #endif
if (prev_pwm == NULL) if (prev_pwm == NULL)
{ {

View File

@ -53,6 +53,8 @@ static PyObject *py_start_channel(PyObject *self, PyObject *args, PyObject *kwar
int polarity = 0; int polarity = 0;
static char *kwlist[] = {"channel", "duty_cycle", "frequency", "polarity", NULL}; 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)) { if (!PyArg_ParseTupleAndKeywords(args, kwargs, "s|ffi", kwlist, &channel, &duty_cycle, &frequency, &polarity)) {
return NULL; return NULL;
} }
@ -156,7 +158,7 @@ static PyObject *py_set_frequency(PyObject *self, PyObject *args, PyObject *kwar
return NULL; 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"); PyErr_SetString(PyExc_RuntimeError, "You must start() the PWM channel first");
return NULL; return NULL;
} }