56 return gpio >= 0 && gpio < 10000;
68 int fd = open(
name, O_WRONLY);
72 ret = write(fd, valstr, strlen(valstr));
88 snprintf(gpiostr,
sizeof(gpiostr),
"%d", gpio);
90 LOG_ERROR(
"Couldn't unexport gpio %d", gpio);
112 snprintf(gpiostr,
sizeof(gpiostr),
"%d", gpio);
115 if (errno == EBUSY) {
118 LOG_ERROR(
"Couldn't export gpio %d", gpio);
119 LOG_ERROR(
"sysfsgpio: %s", strerror(errno));
127 snprintf(buf,
sizeof(buf),
"/sys/class/gpio/gpio%d/direction", gpio);
129 ret =
open_write_close(buf, is_output ? (init_high ?
"high" :
"low") :
"in");
130 if (ret >= 0 || errno != EACCES)
138 LOG_ERROR(
"Couldn't set direction for gpio %d", gpio);
139 LOG_ERROR(
"sysfsgpio: %s", strerror(errno));
144 snprintf(buf,
sizeof(buf),
"/sys/class/gpio/gpio%d/value", gpio);
146 ret = open(buf, O_RDWR | O_NONBLOCK | O_SYNC);
147 if (ret >= 0 || errno != EACCES)
155 LOG_ERROR(
"Couldn't open value for gpio %d", gpio);
156 LOG_ERROR(
"sysfsgpio: %s", strerror(errno));
196 snprintf(buf,
sizeof(buf),
"/sys/class/gpio/gpio%d/direction",
swdio_gpio);
200 LOG_ERROR(
"sysfsgpio: %s", strerror(errno));
213 int ret = read(
swdio_fd, &buf,
sizeof(buf));
220 return buf[0] !=
'0';
225 const char one[] =
"1";
226 const char zero[] =
"0";
228 size_t bytes_written;
232 bytes_written = write(
swdio_fd, swdio ? &one : &zero, 1);
233 if (bytes_written != 1)
240 bytes_written = write(
swclk_fd, swclk ? &one : &zero, 1);
241 if (bytes_written != 1)
263 lseek(
tdo_fd, 0, SEEK_SET);
264 int ret = read(
tdo_fd, &buf,
sizeof(buf));
282 const char one[] =
"1";
283 const char zero[] =
"0";
289 static int first_time;
290 size_t bytes_written;
299 if (tdi != last_tdi) {
300 bytes_written = write(
tdi_fd, tdi ? &one : &zero, 1);
301 if (bytes_written != 1)
306 bytes_written = write(
tms_fd, tms ? &one : &zero, 1);
307 if (bytes_written != 1)
312 if (tck != last_tck) {
313 bytes_written = write(
tck_fd, tck ? &one : &zero, 1);
314 if (bytes_written != 1)
333 const char one[] =
"1";
334 const char zero[] =
"0";
335 size_t bytes_written;
339 bytes_written = write(
srst_fd, srst ? &zero : &one, 1);
340 if (bytes_written != 1)
346 bytes_written = write(
trst_fd, trst ? &zero : &one, 1);
347 if (bytes_written != 1)
366 "SysfsGPIO nums: tck = %d, tms = %d, tdi = %d, tdo = %d",
436 "SysfsGPIO nums: swclk = %d, swdio = %d",
463 .handler = &sysfsgpio_handle_jtag_gpionums,
465 .help =
"gpio numbers for tck, tms, tdi, tdo. (in that order)",
466 .usage =
"[tck tms tdi tdo]",
470 .handler = &sysfsgpio_handle_jtag_gpionum_tck,
472 .help =
"gpio number for tck.",
477 .handler = &sysfsgpio_handle_jtag_gpionum_tms,
479 .help =
"gpio number for tms.",
484 .handler = &sysfsgpio_handle_jtag_gpionum_tdo,
486 .help =
"gpio number for tdo.",
491 .handler = &sysfsgpio_handle_jtag_gpionum_tdi,
493 .help =
"gpio number for tdi.",
498 .handler = &sysfsgpio_handle_jtag_gpionum_srst,
500 .help =
"gpio number for srst.",
505 .handler = &sysfsgpio_handle_jtag_gpionum_trst,
507 .help =
"gpio number for trst.",
512 .handler = &sysfsgpio_handle_swd_gpionums,
514 .help =
"gpio numbers for swclk, swdio. (in that order)",
515 .usage =
"[swclk swdio]",
519 .handler = &sysfsgpio_handle_swd_gpionum_swclk,
521 .help =
"gpio number for swclk.",
526 .handler = &sysfsgpio_handle_swd_gpionum_swdio,
528 .help =
"gpio number for swdio.",
538 .help =
"perform sysfsgpio management",
630 LOG_INFO(
"SysfsGPIO JTAG/SWD bitbang driver");
640 LOG_ERROR(
"Require tck, tms, tdi and tdo gpios for JTAG mode");
670 LOG_ERROR(
"Require swclk and swdio gpio for SWD mode");
bool transport_is_swd(void)
Returns true if the current debug session is using SWD as its transport.
int bitbang_execute_queue(void)
const struct swd_driver bitbang_swd
void command_print(struct command_invocation *cmd, const char *format,...)
#define CMD
Use this macro to access the command being handled, rather than accessing the variable directly.
#define CMD_ARGV
Use this macro to access the arguments for the command being handled, rather than accessing the varia...
#define ERROR_COMMAND_SYNTAX_ERROR
#define CMD_ARGC
Use this macro to access the number of arguments for the command being handled, rather than accessing...
#define COMMAND_PARSE_NUMBER(type, in, out)
parses the string in into out as a type, or prints a command error and passes the error code to the c...
#define COMMAND_REGISTRATION_DONE
Use this as the last entry in an array of command_registration records.
#define DEBUG_CAP_TMS_SEQ
bool transport_is_jtag(void)
Returns true if the current debug session is using JTAG as its transport.
void jtag_sleep(uint32_t us)
#define ERROR_JTAG_INIT_FAILED
#define LOG_WARNING(expr ...)
#define LOG_ERROR(expr ...)
#define LOG_INFO(expr ...)
#define LOG_DEBUG(expr ...)
int gettimeofday(struct timeval *tv, struct timezone *tz)
Represents a driver for a debugging interface.
const char *const name
The name of the interface driver.
Low level callbacks (for bitbang).
bb_value_t(* read)(void)
Sample TDO and return the value.
const char * usage
a string listing the options and arguments, required or optional
Represents a driver for a debugging interface.
unsigned supported
Bit vector listing capabilities exposed by this driver.
static struct jtag_interface sysfsgpio_interface
static void cleanup_fd(int fd, int gpio)
static const struct command_registration sysfsgpio_command_handlers[]
static int sysfsgpio_reset(int trst, int srst)
static struct bitbang_interface sysfsgpio_bitbang
static const char *const sysfsgpio_transports[]
static int sysfsgpio_init(void)
static int setup_sysfs_gpio(int gpio, int is_output, int init_high)
static int sysfsgpio_swdio_read(void)
static void sysfsgpio_swdio_drive(bool is_output)
static int open_write_close(const char *name, const char *valstr)
static void cleanup_all_fds(void)
static void unexport_sysfs_gpio(int gpio)
static int sysfsgpio_write(int tck, int tms, int tdi)
struct adapter_driver sysfsgpio_adapter_driver
static int sysfsgpio_quit(void)
static bool sysfsgpio_jtag_mode_possible(void)
static int sysfsgpio_swd_write(int swclk, int swdio)
static bool is_gpio_valid(int gpio)
static const struct command_registration sysfsgpio_subcommand_handlers[]
COMMAND_HANDLER(sysfsgpio_handle_jtag_gpionums)
static bool sysfsgpio_swd_mode_possible(void)
static bb_value_t sysfsgpio_read(void)
int timeval_compare(const struct timeval *x, const struct timeval *y)
int timeval_add_time(struct timeval *result, long sec, long usec)