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

Realized that the AP-EINT1/3 test code wasn't actually testing callbacks, they do work properly in my testing. Reverting gptest.py to what it was. Revision Update. Issue #9 is resolved.

This commit is contained in:
Robert Wolterman
2016-07-10 19:23:45 -05:00
parent 3d1ecb6b5e
commit a4dd1558f0
5 changed files with 52 additions and 81 deletions

View File

@ -1,3 +1,7 @@
0.1.0
----
* Fixed edge detection code, will trigger proper for callbacks now
0.0.9 0.0.9
---- ----
* Fixed SoftPWM segfault * Fixed SoftPWM segfault

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.9', version = '0.1.0',
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',

View File

@ -76,6 +76,6 @@ void define_constants(PyObject *module)
both_edge = Py_BuildValue("i", BOTH_EDGE); both_edge = Py_BuildValue("i", BOTH_EDGE);
PyModule_AddObject(module, "BOTH", both_edge); PyModule_AddObject(module, "BOTH", both_edge);
version = Py_BuildValue("s", "0.0.9"); version = Py_BuildValue("s", "0.1.0");
PyModule_AddObject(module, "VERSION", version); PyModule_AddObject(module, "VERSION", version);
} }

View File

@ -426,9 +426,6 @@ int gpio_set_edge(int gpio, unsigned int edge)
int fd; int fd;
char filename[MAX_FILENAME]; char filename[MAX_FILENAME];
// DEBUG
printf("DEBUG: gpio_set_edge(%d, %d (%s))\n", gpio, edge, stredge[edge]);
snprintf(filename, sizeof(filename), "/sys/class/gpio/gpio%d/edge", gpio); BUF2SMALL(filename); snprintf(filename, sizeof(filename), "/sys/class/gpio/gpio%d/edge", gpio); BUF2SMALL(filename);
if ((fd = open(filename, O_WRONLY)) < 0) { if ((fd = open(filename, O_WRONLY)) < 0) {
@ -470,19 +467,15 @@ int open_edge_file(int gpio)
int gpio_get_edge(int gpio) int gpio_get_edge(int gpio)
{ {
int e_no; int fd;
int fd; // = fd_lookup(gpio);
int rtnedge = -1; int rtnedge = -1;
//if (!fd)
//{
if ((fd = open_edge_file(gpio)) == -1) { if ((fd = open_edge_file(gpio)) == -1) {
char err[256]; char err[256];
snprintf(err, sizeof(err), "gpio_get_value: could not open GPIO %d edge file", gpio); snprintf(err, sizeof(err), "gpio_get_value: could not open GPIO %d edge file", gpio);
add_error_msg(err); add_error_msg(err);
return -1; return -1;
} }
//}
if (lseek(fd, 0, SEEK_SET) < 0) { if (lseek(fd, 0, SEEK_SET) < 0) {
char err[256]; char err[256];
@ -492,7 +485,7 @@ int gpio_get_edge(int gpio)
} }
char edge[16] = { 0 }; /* make sure read is null-terminated */ char edge[16] = { 0 }; /* make sure read is null-terminated */
ssize_t s = read(fd, &edge, sizeof(edge) - 1); e_no = errno; ssize_t s = read(fd, &edge, sizeof(edge) - 1);
close(fd); close(fd);
while (s > 0 && edge[s-1] == '\n') { /* strip trailing newlines */ while (s > 0 && edge[s-1] == '\n') { /* strip trailing newlines */
edge[s-1] = '\0'; edge[s-1] = '\0';
@ -519,9 +512,6 @@ int gpio_get_edge(int gpio)
rtnedge = 3; rtnedge = 3;
} }
// DEBUG
printf("DEBUG gpio_get_edge(%d -> %d)\n", gpio, rtnedge);
return rtnedge; return rtnedge;
} }
@ -577,11 +567,8 @@ void run_callbacks(int gpio)
if (cb->gpio == gpio) if (cb->gpio == gpio)
{ {
int canrun = 0; int canrun = 0;
int edge = gpio_get_edge(gpio);
unsigned int value = 0; unsigned int value = 0;
gpio_get_value(gpio, &value); gpio_get_value(gpio, &value);
// DEBUG
printf("DEBUG run_callbacks(GPIO: %d, EDGE: %d, VALUE: %d)\n", gpio, cb->edge, value);
// Both Edge // Both Edge
if (cb->edge == 3) if (cb->edge == 3)
{ {
@ -681,17 +668,10 @@ void *poll_thread(void *threadarg)
} }
// The return value represents the ending level after the edge. // The return value represents the ending level after the edge.
gpio = gpio_lookup(events.data.fd); gpio = gpio_lookup(events.data.fd);
// DEBUG
printf("DEBUG *poll_thread -> GPIO: %d\n", gpio);
if (gpio_initial(gpio)) { // ignore first epoll trigger if (gpio_initial(gpio)) { // ignore first epoll trigger
printf("DEBUG *poll_thread -> GPIO INITIAL HIT\n");
set_initial_false(gpio); set_initial_false(gpio);
} else { } else {
printf("DEBUG *poll_thread -> GPIO RUNNING CALLBACK\n");
dyn_int_array_set(&event_occurred, gpio, 1, 0); dyn_int_array_set(&event_occurred, gpio, 1, 0);
int value = 0;
gpio_get_value(gpio, &value);
printf("DEBUG *poll_thread -> GPIO VALUE: %d\n", value);
run_callbacks(gpio); run_callbacks(gpio);
} }
} }

View File

@ -4,10 +4,6 @@ import CHIP_IO.GPIO as GPIO
import time import time
import threading import threading
DO_APEINT1_TEST = False
DO_APEINT3_TEST = False
DO_XIOP2_TEST = True
num_callbacks = 0 num_callbacks = 0
def myfuncallback(channel): def myfuncallback(channel):
@ -19,7 +15,7 @@ def myfuncallback(channel):
loopfunction_exit = False loopfunction_exit = False
def loopfunction(): def loopfunction():
print("LOOP FUNCTION") print("LOOP FUNCTION START")
for i in xrange(6): for i in xrange(6):
if loopfunction_exit: if loopfunction_exit:
break break
@ -31,12 +27,16 @@ def loopfunction():
mystr = "SETTING CSID0 HIGH (i=%d)" % i mystr = "SETTING CSID0 HIGH (i=%d)" % i
print(mystr) print(mystr)
GPIO.output("CSID0", GPIO.HIGH) GPIO.output("CSID0", GPIO.HIGH)
print("SLEEPING") print(" LOOP FUNCTION SLEEPING")
time.sleep(1) time.sleep(1)
num_errs = 0 num_errs = 0
print("RUNNING GPIO SELF TEST") print("GETTING CHIP_IO VERSION")
mystr = "CHIP_IO VERSION: %s" % GPIO.VERSION
print(mystr)
print("\nRUNNING GPIO SELF TEST")
GPIO.selftest(0) GPIO.selftest(0)
print("\nVERIFYING SIMPLE FUNCTIONALITY") print("\nVERIFYING SIMPLE FUNCTIONALITY")
@ -71,70 +71,57 @@ GPIO.output("CSID0", GPIO.LOW)
print "LOW", GPIO.input("GPIO1") print "LOW", GPIO.input("GPIO1")
assert(GPIO.input("GPIO1") == GPIO.LOW) assert(GPIO.input("GPIO1") == GPIO.LOW)
print("SWAP GPIO WIRES FOR EDGE DETECTION TESTS AS NEEDED")
raw_input("PRESS ENTER WHEN READY TO START EDGE DETECTION TESTS")
# ============================================== # ==============================================
# EDGE DETECTION - AP-EINT1 # EDGE DETECTION - AP-EINT1
if DO_APEINT1_TEST: print("\nSETTING UP RISING EDGE DETECTION ON AP-EINT1")
print("\nSETTING UP RISING EDGE DETECTION ON AP-EINT1") GPIO.setup("AP-EINT1", GPIO.IN)
GPIO.setup("AP-EINT1", GPIO.IN) GPIO.add_event_detect("AP-EINT1", GPIO.RISING, myfuncallback)
GPIO.add_event_detect("AP-EINT1", GPIO.RISING) print("VERIFYING EDGE DETECT WAS SET PROPERLY")
f = open("/sys/class/gpio/gpio193/edge", "r")
print("VERIFYING EDGE DETECT") edge = f.read()
f = open("/sys/class/gpio/gpio193/edge", "r") f.close()
edge = f.read() assert(edge == "rising\n")
f.close() GPIO.remove_event_detect("AP-EINT1")
assert(edge == "rising\n")
GPIO.remove_event_detect("AP-EINT1")
# ============================================== # ==============================================
# EDGE DETECTION - AP-EINT3 # EDGE DETECTION - AP-EINT3
if DO_APEINT3_TEST: print("\nSETTING UP BOTH EDGE DETECTION ON AP-EINT3")
print("\nSETTING UP BOTH EDGE DETECTION ON AP-EINT3") GPIO.setup("AP-EINT3", GPIO.IN)
GPIO.setup("AP-EINT3", GPIO.IN) GPIO.add_event_detect("AP-EINT3", GPIO.BOTH, myfuncallback)
GPIO.add_event_detect("AP-EINT3", GPIO.BOTH) print("VERIFYING EDGE DETECT WAS SET PROPERLY")
f = open("/sys/class/gpio/gpio35/edge", "r")
print("VERIFYING EDGE DETECT") edge = f.read()
f = open("/sys/class/gpio/gpio35/edge", "r") f.close()
edge = f.read() assert(edge == "both\n")
f.close() GPIO.remove_event_detect("AP-EINT3")
assert(edge == "both\n")
GPIO.remove_event_detect("AP-EINT3")
# ============================================== # ==============================================
# EDGE DETECTION - EXPANDED GPIO # EDGE DETECTION - EXPANDED GPIO
if DO_XIOP2_TEST: print("\nSETTING UP FALLING EDGE DETECTION ON XIO-P2")
print("\nSETTING UP FALLING EDGE DETECTION ON XIO-P2") GPIO.add_event_detect("XIO-P2", GPIO.FALLING, myfuncallback)
GPIO.add_event_detect("XIO-P2", GPIO.FALLING, myfuncallback)
print("VERIFYING EDGE DETECT") print("VERIFYING EDGE DETECT")
base = GPIO.get_gpio_base() base = GPIO.get_gpio_base()
gfile = "/sys/class/gpio/gpio%d/edge" % (base + 2) gfile = "/sys/class/gpio/gpio%d/edge" % (base + 2)
f = open(gfile, "r") f = open(gfile, "r")
edge = f.read() edge = f.read()
f.close() f.close()
assert(edge == "falling\n") assert(edge == "falling\n")
# LOOP WRITING ON CSID0 TO HOPEFULLY GET CALLBACK TO WORK # LOOP WRITING ON CSID0 TO HOPEFULLY GET CALLBACK TO WORK
print("WAITING FOR CALLBACKS") print("WAITING FOR CALLBACKS ON XIO-P2")
loopfunction() loopfunction()
mystr = " num_callbacks = %d" % num_callbacks mystr = " num_callbacks = %d" % num_callbacks
print(mystr) print(mystr)
GPIO.remove_event_detect("XIO-P2")
print("\nWAIT FOR EDGE TESTING") print("\nWAIT FOR EDGE TESTING")
print("PRESS CONTROL-C TO EXIT IF SCRIPT GETS STUCK") print("PRESS CONTROL-C TO EXIT IF SCRIPT GETS STUCK")
GPIO.remove_event_detect("XIO-P2")
try: try:
# WAIT FOR EDGE # WAIT FOR EDGE
t = threading.Thread(target=loopfunction) t = threading.Thread(target=loopfunction)
t.start() t.start()
print("WAITING FOR EDGE") print("WAITING FOR EDGE ON XIO-P2")
if DO_APEINT1_TEST:
GPIO.wait_for_edge("AP-EINT1", GPIO.FALLING)
if DO_APEINT3_TEST:
GPIO.wait_for_edge("AP-EINT3", GPIO.FALLING)
if DO_XIOP2_TEST:
GPIO.wait_for_edge("XIO-P2", GPIO.FALLING) GPIO.wait_for_edge("XIO-P2", GPIO.FALLING)
# THIS SHOULD ONLY PRINT IF WE'VE HIT THE EDGE DETECT # THIS SHOULD ONLY PRINT IF WE'VE HIT THE EDGE DETECT
print("WE'VE FALLEN LIKE COOLIO'S CAREER") print("WE'VE FALLEN LIKE COOLIO'S CAREER")