Merge branch 'dsiroky-master'

This commit is contained in:
Daniel Beer 2016-07-27 20:42:00 +12:00
commit bf61024ec2
4 changed files with 59 additions and 3 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)
} }
} }
if (was_rts_exported == 0)
{
gpio_unexport ( rts ); gpio_unexport ( rts );
}
if (was_dtr_exported == 0)
{
gpio_unexport ( dtr ); gpio_unexport ( dtr );
}
delay_ms(50); delay_ms(50);

View File

@ -36,6 +36,7 @@ struct flash_bsl_device {
sport_t serial_fd; sport_t serial_fd;
int long_password; int long_password;
const struct device_args *args;
const char *seq; const char *seq;
}; };
@ -554,7 +555,15 @@ static void flash_bsl_destroy(device_t dev_base)
{ {
struct flash_bsl_device *dev = (struct flash_bsl_device *)dev_base; struct flash_bsl_device *dev = (struct flash_bsl_device *)dev_base;
if ( dev->args->bsl_gpio_used )
{
bsllib_seq_do_gpio(dev->args->bsl_gpio_rts, dev->args->bsl_gpio_dtr,
bsllib_seq_next(dev->seq));
}
else
{
bsllib_seq_do(dev->serial_fd, bsllib_seq_next(dev->seq)); bsllib_seq_do(dev->serial_fd, bsllib_seq_next(dev->seq));
}
sport_close(dev->serial_fd); sport_close(dev->serial_fd);
free(dev); free(dev);
} }
@ -581,6 +590,7 @@ static device_t flash_bsl_open(const struct device_args *args)
memset(dev, 0, sizeof(*dev)); memset(dev, 0, sizeof(*dev));
dev->base.type = &device_flash_bsl; dev->base.type = &device_flash_bsl;
dev->args = args;
dev->serial_fd = sport_open(args->path, 9600, SPORT_EVEN_PARITY); dev->serial_fd = sport_open(args->path, 9600, SPORT_EVEN_PARITY);
if (SPORT_ISERR(dev->serial_fd)) { if (SPORT_ISERR(dev->serial_fd)) {

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 );