don't unexport GPIO DTR/RTS if exported before

This commit is contained in:
David Siroky 2016-07-27 10:17:08 +02:00
parent 88b37e5007
commit 3eefaaf3e7
3 changed files with 48 additions and 2 deletions

View File

@ -61,6 +61,9 @@ int bsllib_seq_do(sport_t fd, const char *seq)
int bsllib_seq_do_gpio(int rts, int dtr, const char *seq) int bsllib_seq_do_gpio(int rts, int dtr, const char *seq)
{ {
int was_rts_exported = gpio_is_exported(rts);
int was_dtr_exported = gpio_is_exported(dtr);
gpio_export ( rts ); gpio_export ( rts );
gpio_set_dir ( rts, 1 ); gpio_set_dir ( rts, 1 );
gpio_export ( dtr ); gpio_export ( dtr );
@ -93,8 +96,14 @@ int bsllib_seq_do_gpio(int rts, int dtr, const char *seq)
} }
} }
gpio_unexport ( rts ); if (was_rts_exported == 0)
gpio_unexport ( dtr ); {
gpio_unexport ( rts );
}
if (was_dtr_exported == 0)
{
gpio_unexport ( dtr );
}
delay_ms(50); delay_ms(50);

View File

@ -18,6 +18,12 @@
* *
* Daniel Beer <dlbeer@gmail.com>, 3 Mar 2015 * Daniel Beer <dlbeer@gmail.com>, 3 Mar 2015
*/ */
int gpio_is_exported ( unsigned int gpio )
{
printc_err("gpio: GPIO interface not supported on Windows\n");
return -1;
}
int gpio_export ( unsigned int gpio ) int gpio_export ( unsigned int gpio )
{ {
printc_err("gpio: GPIO interface not supported on Windows\n"); printc_err("gpio: GPIO interface not supported on Windows\n");
@ -78,6 +84,36 @@ int gpio_open_fd (unsigned int gpio)
#define SYSFS_GPIO_DIR "/sys/class/gpio" #define SYSFS_GPIO_DIR "/sys/class/gpio"
#define MAX_BUF 64 #define MAX_BUF 64
/**
* @return 1 if the gpio is already exported, 0 otherwise, -1 on error
*/
int gpio_is_exported ( unsigned int gpio )
{
char dir_name[100] = {};
snprintf(dir_name, sizeof(dir_name) - 1, SYSFS_GPIO_DIR "/gpio%d", gpio);
struct stat s;
int err = stat(dir_name, &s);
if(-1 == err)
{
if(errno == ENOENT)
{
return 0;
} else {
return -1;
}
} else {
if(S_ISDIR(s.st_mode))
{
return 1;
} else {
return -1;
}
}
return -1;
}
/** /**
* Before a Linux application can configure and use a GPIO, the GPIO first has to be exported to user. * Before a Linux application can configure and use a GPIO, the GPIO first has to be exported to user.
* Each GPIO is not accessible from user space until the GPIO has been exported. * Each GPIO is not accessible from user space until the GPIO has been exported.

View File

@ -13,6 +13,7 @@
#ifndef _GPIO_H #ifndef _GPIO_H
#define _GPIO_H #define _GPIO_H
int gpio_is_exported ( unsigned int gpio );
int gpio_export ( unsigned int gpio ); int gpio_export ( unsigned int gpio );
int gpio_unexport ( unsigned int gpio ); int gpio_unexport ( unsigned int gpio );
int gpio_set_dir ( unsigned int gpio, unsigned int out_flag ); int gpio_set_dir ( unsigned int gpio, unsigned int out_flag );