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);
111 snprintf(gpiostr,
sizeof(gpiostr),
"%d", gpio);
114 if (errno == EBUSY) {
117 LOG_ERROR(
"Couldn't export gpio %d", gpio);
118 LOG_ERROR(
"sysfsgpio: %s", strerror(errno));
126 snprintf(buf,
sizeof(buf),
"/sys/class/gpio/gpio%d/direction", gpio);
128 ret =
open_write_close(buf, is_output ? (init_high ?
"high" :
"low") :
"in");
129 if (ret >= 0 || errno != EACCES)
136 LOG_ERROR(
"Couldn't set direction for gpio %d", gpio);
137 LOG_ERROR(
"sysfsgpio: %s", strerror(errno));
142 snprintf(buf,
sizeof(buf),
"/sys/class/gpio/gpio%d/value", gpio);
144 ret = open(buf, O_RDWR | O_NONBLOCK | O_SYNC);
145 if (ret >= 0 || errno != EACCES)
152 LOG_ERROR(
"Couldn't open value for gpio %d", gpio);
153 LOG_ERROR(
"sysfsgpio: %s", strerror(errno));
193 snprintf(buf,
sizeof(buf),
"/sys/class/gpio/gpio%d/direction",
swdio_gpio);
197 LOG_ERROR(
"sysfsgpio: %s", strerror(errno));
210 int ret = read(
swdio_fd, &buf,
sizeof(buf));
217 return buf[0] !=
'0';
222 const char one[] =
"1";
223 const char zero[] =
"0";
225 size_t bytes_written;
229 bytes_written = write(
swdio_fd, swdio ? &one : &
zero, 1);
230 if (bytes_written != 1)
237 bytes_written = write(
swclk_fd, swclk ? &one : &
zero, 1);
238 if (bytes_written != 1)
260 lseek(
tdo_fd, 0, SEEK_SET);
261 int ret = read(
tdo_fd, &buf,
sizeof(buf));
279 const char one[] =
"1";
280 const char zero[] =
"0";
286 static int first_time;
287 size_t bytes_written;
296 if (tdi != last_tdi) {
297 bytes_written = write(
tdi_fd, tdi ? &one : &
zero, 1);
298 if (bytes_written != 1)
303 bytes_written = write(
tms_fd, tms ? &one : &
zero, 1);
304 if (bytes_written != 1)
309 if (tck != last_tck) {
310 bytes_written = write(
tck_fd, tck ? &one : &
zero, 1);
311 if (bytes_written != 1)
330 const char one[] =
"1";
331 const char zero[] =
"0";
332 size_t bytes_written;
336 bytes_written = write(
srst_fd, srst ? &
zero : &one, 1);
337 if (bytes_written != 1)
343 bytes_written = write(
trst_fd, trst ? &
zero : &one, 1);
344 if (bytes_written != 1)
363 "SysfsGPIO nums: tck = %d, tms = %d, tdi = %d, tdo = %d",
433 "SysfsGPIO nums: swclk = %d, swdio = %d",
460 .handler = &sysfsgpio_handle_jtag_gpionums,
462 .help =
"gpio numbers for tck, tms, tdi, tdo. (in that order)",
463 .usage =
"[tck tms tdi tdo]",
467 .handler = &sysfsgpio_handle_jtag_gpionum_tck,
469 .help =
"gpio number for tck.",
474 .handler = &sysfsgpio_handle_jtag_gpionum_tms,
476 .help =
"gpio number for tms.",
481 .handler = &sysfsgpio_handle_jtag_gpionum_tdo,
483 .help =
"gpio number for tdo.",
488 .handler = &sysfsgpio_handle_jtag_gpionum_tdi,
490 .help =
"gpio number for tdi.",
495 .handler = &sysfsgpio_handle_jtag_gpionum_srst,
497 .help =
"gpio number for srst.",
502 .handler = &sysfsgpio_handle_jtag_gpionum_trst,
504 .help =
"gpio number for trst.",
509 .handler = &sysfsgpio_handle_swd_gpionums,
511 .help =
"gpio numbers for swclk, swdio. (in that order)",
512 .usage =
"[swclk swdio]",
516 .handler = &sysfsgpio_handle_swd_gpionum_swclk,
518 .help =
"gpio number for swclk.",
523 .handler = &sysfsgpio_handle_swd_gpionum_swdio,
525 .help =
"gpio number for swdio.",
535 .help =
"perform sysfsgpio management",
626 LOG_INFO(
"SysfsGPIO JTAG/SWD bitbang driver");
636 LOG_ERROR(
"Require tck, tms, tdi and tdo gpios for JTAG mode");
666 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(struct jtag_command *cmd_queue)
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 ...)
Represents a driver for a debugging interface.
const char *const name
The name of the interface driver.
Low level callbacks (for bitbang).
enum bb_value(* 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 int 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 int sysfsgpio_init(void)
static int setup_sysfs_gpio(int gpio, int is_output, int init_high)
static int sysfsgpio_swdio_read(void)
static enum bb_value sysfsgpio_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)
static const struct bitbang_interface sysfsgpio_bitbang
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)