From bf27e2feea855ab760f41b638869f1e102a38e42 Mon Sep 17 00:00:00 2001 From: Robert Wolterman Date: Sat, 28 Jan 2017 02:09:14 +0000 Subject: [PATCH] final updates in the initial addition of debug printing. this should close #55 --- README.rst | 21 ++++++++++++++---- source/c_pwm.c | 2 +- source/event_gpio.c | 52 +++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 70 insertions(+), 5 deletions(-) diff --git a/README.rst b/README.rst index 6a5b82a..a9d72f2 100644 --- a/README.rst +++ b/README.rst @@ -169,6 +169,13 @@ You can also refer to the bin based upon its alternate name:: GPIO.setup("GPIO1", GPIO.IN) +**GPIO Debug** + +Debug can be enabled/disabled by the following command:: + + # Enable Debug + GPIO.toggle_debug() + **GPIO Output** Setup the pin for output, and write GPIO.HIGH or GPIO.LOW. Or you can use 1 or 0.:: @@ -236,6 +243,8 @@ To clean up the GPIO when done, do the following:: Hardware PWM requires a DTB Overlay loaded on the CHIP to allow the kernel to know there is a PWM device available to use. :: import CHIP_IO.PWM as PWM + # Enable/Disable Debug + PWM.toggle_debug() #PWM.start(channel, duty, freq=2000, polarity=0) #duty values are valid 0 (off) to 100 (on) PWM.start("PWM0", 50) @@ -250,6 +259,8 @@ Hardware PWM requires a DTB Overlay loaded on the CHIP to allow the kernel to kn **SOFTPWM**:: import CHIP_IO.SOFTPWM as SPWM + # Enable/Disable Debug + SPWM.toggle_debug() #SPWM.start(channel, duty, freq=2000, polarity=0) #duty values are valid 0 (off) to 100 (on) #you can choose any pin @@ -275,8 +286,8 @@ The LRADC was enabled in the 4.4.13-ntc-mlc. This is a 6 bit ADC that is 2 Volt Sample code below details how to talk to the LRADC.:: import CHIP_IO.LRADC as ADC - # Enable Debug - ADC.enable_debug() + # Enable/Disable Debug + ADC.toggle_debug() # Check to see if the LRADC Device exists # Returns True/False ADC.get_device_exists() @@ -310,8 +321,8 @@ PWM0, SPI2, I2C1, CUST Only one of each type of overlay can be loaded at a time, but all three options can be loaded simultaneously. So you can have SPI2 and I2C1 without PWM0, but you cannot have SPI2 loaded twice. :: import CHIP_IO.OverlayManager as OM - # The enable_debug() function turns on debug printing - #OM.enable_debug() + # The toggle_debug() function turns on/off debug printing + #OM.toggle_debug() # To load an overlay, feed in the name to load() OM.load("PWM0") # To verify the overlay was properly loaded, the get_ functions return booleans @@ -340,6 +351,8 @@ CHIP_IO now supports the ability to enable and disable the 1.8V port on U13. Th To use the utilities, here is sample code:: import CHIP_IO.Utilities as UT + # Enable/Disable Debug + UT.toggle_debug() # Enable 1.8V Output UT.enable_1v8_pin() # Set 2.0V Output diff --git a/source/c_pwm.c b/source/c_pwm.c index 92ff7a2..4caa1e1 100644 --- a/source/c_pwm.c +++ b/source/c_pwm.c @@ -433,7 +433,7 @@ int pwm_disable(const char *key) if (strcmp(pwm->key, key) == 0) { if (DEBUG) { - printf(" ** pwm_disable: unexporting %s\n", key); + printf(" ** pwm_disable: freeing memory %s\n", key); } //close the fd close(pwm->enable_fd); diff --git a/source/event_gpio.c b/source/event_gpio.c index dcf447c..7fe4665 100644 --- a/source/event_gpio.c +++ b/source/event_gpio.c @@ -93,6 +93,9 @@ int gpio_export(int gpio) char str_gpio[80]; struct gpio_exp *new_gpio, *g; + if (DEBUG) + printf(" ** gpio_export **\n"); + snprintf(filename, sizeof(filename), "/sys/class/gpio/export"); BUF2SMALL(filename); if ((fd = open(filename, O_WRONLY)) < 0) @@ -115,6 +118,8 @@ int gpio_export(int gpio) } // add to list + if (DEBUG) + printf(" ** gpio_export: creating data struct **\n"); new_gpio = malloc(sizeof(struct gpio_exp)); ASSRT(new_gpio != NULL); new_gpio->gpio = gpio; @@ -217,6 +222,8 @@ int open_value_file(int gpio) // Changed this to open Read/Write to prevent a ton of file open/closes from happening when using // the GPIO for SOFTPWM + if (DEBUG) + printf(" ** open_value_file **\n"); if ((fd = open(filename, O_RDWR | O_NONBLOCK)) < 0) { char err[256]; snprintf(err, sizeof(err), "open_value_file: could not open '%s' (%s)", filename, strerror(errno)); @@ -236,6 +243,8 @@ int open_edge_file(int gpio) // create file descriptor of value file snprintf(filename, sizeof(filename), "/sys/class/gpio/gpio%d/edge", gpio); BUF2SMALL(filename); + if (DEBUG) + printf(" ** open_edge_file **\n"); if ((fd = open(filename, O_RDONLY | O_NONBLOCK)) < 0) { char err[256]; snprintf(err, sizeof(err), "open_edge_file: could not open '%s' (%s)", filename, strerror(errno)); @@ -253,6 +262,9 @@ int gpio_unexport(int gpio) char str_gpio[16]; struct gpio_exp *g, *temp, *prev_g = NULL; + if (DEBUG) + printf(" ** gpio_unexport **\n"); + close_value_fd(gpio); snprintf(filename, sizeof(filename), "/sys/class/gpio/unexport"); BUF2SMALL(filename); @@ -274,6 +286,8 @@ int gpio_unexport(int gpio) return -1; } + if (DEBUG) + printf(" ** gpio_unexport: freeing memory **\n"); // remove from list g = exported_gpios; while (g != NULL) @@ -315,6 +329,9 @@ int gpio_set_direction(int gpio, unsigned int in_flag) } else { strncpy(direction, "in", ARRAY_SIZE(direction) - 1); } + if (DEBUG) + printf(" ** gpio_set_direction: %s **\n",direction); + ssize_t s = write(fd, direction, strlen(direction)); e_no = errno; close(fd); if (s != strlen(direction)) { @@ -360,6 +377,9 @@ int gpio_get_direction(int gpio, unsigned int *value) add_error_msg(err); return -1; } + + if (DEBUG) + printf(" ** gpio_get_direction: %s **\n",direction); if (strcmp(direction, "out") == 0) *value = OUTPUT; @@ -403,6 +423,9 @@ int gpio_set_value(int gpio, unsigned int value) strncpy(vstr, "0", ARRAY_SIZE(vstr) - 1); } + if (DEBUG) + printf(" ** gpio_set_value: writing %s **\n", vstr); + ssize_t s = write(fd, vstr, strlen(vstr)); e_no = errno; if (s != strlen(vstr)) { @@ -444,6 +467,9 @@ int gpio_get_value(int gpio, unsigned int *value) return -1; } + if (DEBUG) + printf(" ** gpio_get_value: %c **\n", ch); + if (ch == '1') { *value = 1; } else if (ch == '0') { @@ -472,6 +498,9 @@ int gpio_set_edge(int gpio, unsigned int edge) return -1; } + if (DEBUG) + printf(" ** gpio_set_edge: %s **\n", stredge[edge]); + ssize_t s = write(fd, stredge[edge], strlen(stredge[edge]) + 1); if (s < 0) { char err[256]; @@ -521,6 +550,9 @@ int gpio_get_edge(int gpio) return -1; } + if (DEBUG) + printf(" ** gpio_get_edge: %s **\n", edge); + if (strcmp(edge, "rising") == 0) { rtnedge = 1; @@ -553,6 +585,8 @@ int gpio_lookup(int fd) void exports_cleanup(void) { // unexport everything + if (DEBUG) + printf(" ** exports_cleanup **\n"); while (exported_gpios != NULL) gpio_unexport(exported_gpios->gpio); } @@ -562,6 +596,8 @@ int add_edge_callback(int gpio, int edge, void (*func)(int gpio, void* data), vo struct callback *cb = callbacks; struct callback *new_cb; + if (DEBUG) + printf(" ** add_edge_callback **\n"); new_cb = malloc(sizeof(struct callback)); ASSRT(new_cb != NULL); new_cb->fde = open_edge_file(gpio); @@ -611,6 +647,8 @@ void run_callbacks(int gpio) // Only run if we are allowed if (canrun) { + if (DEBUG) + printf(" ** run_callbacks: gpio triggered: %d **\n", gpio); cb->func(cb->gpio, cb->data); } @@ -629,6 +667,8 @@ void remove_callbacks(int gpio) { if (cb->gpio == gpio) { + if (DEBUG) + printf(" ** remove_callbacks: gpio: %d **\n", gpio); close(cb->fde); if (prev == NULL) callbacks = cb->next; @@ -762,6 +802,9 @@ int add_edge_detect(int gpio, unsigned int edge) struct epoll_event ev; long t = 0; + if (DEBUG) + printf(" ** add_edge_detect: gpio: %d **\n", gpio); + // check to see if this gpio has been added already if (gpio_event_add(gpio) != 0) return 1; @@ -828,6 +871,9 @@ void remove_edge_detect(int gpio) struct epoll_event ev; int fd = fd_lookup(gpio); + if (DEBUG) + printf(" ** remove_edge_detect: gpio : %d **\n", gpio); + // delete callbacks for gpio remove_callbacks(gpio); @@ -871,6 +917,9 @@ int blocking_wait_for_edge(int gpio, unsigned int edge) struct epoll_event events, ev; char buf; + if (DEBUG) + printf(" ** blocking_wait_for_edge: gpio: %d **\n", gpio); + if ((epfd = epoll_create(1)) == -1) { char err[256]; snprintf(err, sizeof(err), "blocking_wait_for_edge: could not epoll_create GPIO %d (%s)", gpio, strerror(errno)); @@ -949,6 +998,9 @@ int blocking_wait_for_edge(int gpio, unsigned int edge) } } + if (DEBUG) + printf(" ** blocking_wait_for_edge: gpio triggered: %d **\n", gpio); + gpio_event_remove(gpio); close(epfd); return 0;