OpenOCD
sysfsgpio.c File Reference

This driver implements a bitbang jtag interface using gpio lines via sysfs. More...

Include dependency graph for sysfsgpio.c:

Go to the source code of this file.

Functions

static void cleanup_all_fds (void)
 
static void cleanup_fd (int fd, int gpio)
 
 COMMAND_HANDLER (sysfsgpio_handle_jtag_gpionum_srst)
 
 COMMAND_HANDLER (sysfsgpio_handle_jtag_gpionum_tck)
 
 COMMAND_HANDLER (sysfsgpio_handle_jtag_gpionum_tdi)
 
 COMMAND_HANDLER (sysfsgpio_handle_jtag_gpionum_tdo)
 
 COMMAND_HANDLER (sysfsgpio_handle_jtag_gpionum_tms)
 
 COMMAND_HANDLER (sysfsgpio_handle_jtag_gpionum_trst)
 
 COMMAND_HANDLER (sysfsgpio_handle_jtag_gpionums)
 
 COMMAND_HANDLER (sysfsgpio_handle_swd_gpionum_swclk)
 
 COMMAND_HANDLER (sysfsgpio_handle_swd_gpionum_swdio)
 
 COMMAND_HANDLER (sysfsgpio_handle_swd_gpionums)
 
static bool is_gpio_valid (int gpio)
 
static int open_write_close (const char *name, const char *valstr)
 
static int setup_sysfs_gpio (int gpio, int is_output, int init_high)
 
static int sysfsgpio_init (void)
 
static bool sysfsgpio_jtag_mode_possible (void)
 
static int sysfsgpio_quit (void)
 
static bb_value_t sysfsgpio_read (void)
 
static int sysfsgpio_reset (int trst, int srst)
 
static bool sysfsgpio_swd_mode_possible (void)
 
static int sysfsgpio_swd_write (int swclk, int swdio)
 
static void sysfsgpio_swdio_drive (bool is_output)
 
static int sysfsgpio_swdio_read (void)
 
static int sysfsgpio_write (int tck, int tms, int tdi)
 
static void unexport_sysfs_gpio (int gpio)
 

Variables

static bool last_stored
 
static int last_swclk
 
static int last_swdio
 
static int srst_fd = -1
 
static int srst_gpio = -1
 
static int swclk_fd = -1
 
static int swclk_gpio = -1
 
static int swdio_fd = -1
 
static int swdio_gpio = -1
 
static bool swdio_input
 
struct adapter_driver sysfsgpio_adapter_driver
 
static struct bitbang_interface sysfsgpio_bitbang
 
static const struct command_registration sysfsgpio_command_handlers []
 
static struct jtag_interface sysfsgpio_interface
 
static const struct command_registration sysfsgpio_subcommand_handlers []
 
static const char *const sysfsgpio_transports [] = { "jtag", "swd", NULL }
 
static int tck_fd = -1
 
static int tck_gpio = -1
 
static int tdi_fd = -1
 
static int tdi_gpio = -1
 
static int tdo_fd = -1
 
static int tdo_gpio = -1
 
static int tms_fd = -1
 
static int tms_gpio = -1
 
static int trst_fd = -1
 
static int trst_gpio = -1
 

Detailed Description

This driver implements a bitbang jtag interface using gpio lines via sysfs.

The aim of this driver implementation is use system GPIOs but avoid the need for a additional kernel driver. (Note memory mapped IO is another option, however it doesn't mix well with the kernel gpiolib driver - which makes sense I guess.)

A gpio is required for tck, tms, tdi and tdo. One or both of srst and trst must be also be specified. The required jtag gpios are specified via the sysfsgpio_jtag_nums command or the relevant sysfsgpio_XXX_num commands. The srst and trst gpios are set via the sysfsgpio_srst_num and sysfsgpio_trst_num respectively. GPIO numbering follows the kernel convention of starting from 0.

The gpios should not be in use by another entity, and must not be requested by a kernel driver without also being exported by it (otherwise they can't be exported by sysfs).

The sysfs gpio interface can only manipulate one gpio at a time, so the bitbang write handler remembers the last state for tck, tms, tdi to avoid superfluous writes. For speed the sysfs "value" entry is opened at init and held open. This results in considerable gains over open-write-close (45s vs 900s)

Further work could address: -srst and trst open drain/ push pull -configurable active high/low for srst & trst

Definition in file sysfsgpio.c.

Function Documentation

◆ cleanup_all_fds()

static void cleanup_all_fds ( void  )
static

◆ cleanup_fd()

static void cleanup_fd ( int  fd,
int  gpio 
)
static

Definition at line 578 of file sysfsgpio.c.

References unexport_sysfs_gpio().

Referenced by cleanup_all_fds().

◆ COMMAND_HANDLER() [1/10]

COMMAND_HANDLER ( sysfsgpio_handle_jtag_gpionum_srst  )

Definition at line 408 of file sysfsgpio.c.

References CMD, CMD_ARGC, CMD_ARGV, COMMAND_PARSE_NUMBER, command_print(), ERROR_OK, and srst_gpio.

◆ COMMAND_HANDLER() [2/10]

COMMAND_HANDLER ( sysfsgpio_handle_jtag_gpionum_tck  )

Definition at line 372 of file sysfsgpio.c.

References CMD, CMD_ARGC, CMD_ARGV, COMMAND_PARSE_NUMBER, command_print(), ERROR_OK, and tck_gpio.

◆ COMMAND_HANDLER() [3/10]

COMMAND_HANDLER ( sysfsgpio_handle_jtag_gpionum_tdi  )

Definition at line 399 of file sysfsgpio.c.

References CMD, CMD_ARGC, CMD_ARGV, COMMAND_PARSE_NUMBER, command_print(), ERROR_OK, and tdi_gpio.

◆ COMMAND_HANDLER() [4/10]

COMMAND_HANDLER ( sysfsgpio_handle_jtag_gpionum_tdo  )

Definition at line 390 of file sysfsgpio.c.

References CMD, CMD_ARGC, CMD_ARGV, COMMAND_PARSE_NUMBER, command_print(), ERROR_OK, and tdo_gpio.

◆ COMMAND_HANDLER() [5/10]

COMMAND_HANDLER ( sysfsgpio_handle_jtag_gpionum_tms  )

Definition at line 381 of file sysfsgpio.c.

References CMD, CMD_ARGC, CMD_ARGV, COMMAND_PARSE_NUMBER, command_print(), ERROR_OK, and tms_gpio.

◆ COMMAND_HANDLER() [6/10]

COMMAND_HANDLER ( sysfsgpio_handle_jtag_gpionum_trst  )

Definition at line 417 of file sysfsgpio.c.

References CMD, CMD_ARGC, CMD_ARGV, COMMAND_PARSE_NUMBER, command_print(), ERROR_OK, and trst_gpio.

◆ COMMAND_HANDLER() [7/10]

COMMAND_HANDLER ( sysfsgpio_handle_jtag_gpionums  )

◆ COMMAND_HANDLER() [8/10]

COMMAND_HANDLER ( sysfsgpio_handle_swd_gpionum_swclk  )

Definition at line 442 of file sysfsgpio.c.

References CMD, CMD_ARGC, CMD_ARGV, COMMAND_PARSE_NUMBER, command_print(), ERROR_OK, and swclk_gpio.

◆ COMMAND_HANDLER() [9/10]

COMMAND_HANDLER ( sysfsgpio_handle_swd_gpionum_swdio  )

Definition at line 451 of file sysfsgpio.c.

References CMD, CMD_ARGC, CMD_ARGV, COMMAND_PARSE_NUMBER, command_print(), ERROR_OK, and swdio_gpio.

◆ COMMAND_HANDLER() [10/10]

COMMAND_HANDLER ( sysfsgpio_handle_swd_gpionums  )

◆ is_gpio_valid()

static bool is_gpio_valid ( int  gpio)
static

◆ open_write_close()

static int open_write_close ( const char *  name,
const char *  valstr 
)
static

Definition at line 65 of file sysfsgpio.c.

References name.

Referenced by setup_sysfs_gpio(), sysfsgpio_swdio_drive(), and unexport_sysfs_gpio().

◆ setup_sysfs_gpio()

static int setup_sysfs_gpio ( int  gpio,
int  is_output,
int  init_high 
)
static

◆ sysfsgpio_init()

◆ sysfsgpio_jtag_mode_possible()

static bool sysfsgpio_jtag_mode_possible ( void  )
static

Definition at line 604 of file sysfsgpio.c.

References is_gpio_valid(), tck_gpio, tdi_gpio, tdo_gpio, and tms_gpio.

Referenced by sysfsgpio_init().

◆ sysfsgpio_quit()

static int sysfsgpio_quit ( void  )
static

Definition at line 697 of file sysfsgpio.c.

References cleanup_all_fds(), and ERROR_OK.

◆ sysfsgpio_read()

static bb_value_t sysfsgpio_read ( void  )
static

Definition at line 258 of file sysfsgpio.c.

References BB_HIGH, BB_LOW, LOG_WARNING, and tdo_fd.

◆ sysfsgpio_reset()

static int sysfsgpio_reset ( int  trst,
int  srst 
)
static

Definition at line 330 of file sysfsgpio.c.

References ERROR_OK, LOG_DEBUG, LOG_WARNING, srst_fd, trst_fd, and zero.

◆ sysfsgpio_swd_mode_possible()

static bool sysfsgpio_swd_mode_possible ( void  )
static

Definition at line 617 of file sysfsgpio.c.

References is_gpio_valid(), swclk_gpio, and swdio_gpio.

Referenced by sysfsgpio_init().

◆ sysfsgpio_swd_write()

static int sysfsgpio_swd_write ( int  swclk,
int  swdio 
)
static

Definition at line 223 of file sysfsgpio.c.

References ERROR_OK, last_stored, last_swclk, last_swdio, LOG_WARNING, swclk_fd, swdio_fd, swdio_input, and zero.

◆ sysfsgpio_swdio_drive()

static void sysfsgpio_swdio_drive ( bool  is_output)
static

Definition at line 191 of file sysfsgpio.c.

References last_stored, LOG_ERROR, open_write_close(), swdio_gpio, and swdio_input.

◆ sysfsgpio_swdio_read()

static int sysfsgpio_swdio_read ( void  )
static

Definition at line 207 of file sysfsgpio.c.

References LOG_WARNING, and swdio_fd.

◆ sysfsgpio_write()

static int sysfsgpio_write ( int  tck,
int  tms,
int  tdi 
)
static

Definition at line 280 of file sysfsgpio.c.

References ERROR_OK, last_tms, LOG_WARNING, tck_fd, tdi_fd, tms_fd, and zero.

◆ unexport_sysfs_gpio()

static void unexport_sysfs_gpio ( int  gpio)
static

Definition at line 81 of file sysfsgpio.c.

References is_gpio_valid(), LOG_ERROR, and open_write_close().

Referenced by cleanup_fd(), and setup_sysfs_gpio().

Variable Documentation

◆ last_stored

bool last_stored
static

Definition at line 188 of file sysfsgpio.c.

Referenced by sysfsgpio_swd_write(), and sysfsgpio_swdio_drive().

◆ last_swclk

int last_swclk
static

Definition at line 186 of file sysfsgpio.c.

Referenced by sysfsgpio_swd_write().

◆ last_swdio

int last_swdio
static

Definition at line 187 of file sysfsgpio.c.

Referenced by sysfsgpio_swd_write().

◆ srst_fd

int srst_fd = -1
static

Definition at line 182 of file sysfsgpio.c.

Referenced by cleanup_all_fds(), sysfsgpio_init(), and sysfsgpio_reset().

◆ srst_gpio

int srst_gpio = -1
static

Definition at line 169 of file sysfsgpio.c.

Referenced by cleanup_all_fds(), COMMAND_HANDLER(), and sysfsgpio_init().

◆ swclk_fd

int swclk_fd = -1
static

Definition at line 183 of file sysfsgpio.c.

Referenced by cleanup_all_fds(), sysfsgpio_init(), and sysfsgpio_swd_write().

◆ swclk_gpio

int swclk_gpio = -1
static

◆ swdio_fd

int swdio_fd = -1
static

◆ swdio_gpio

int swdio_gpio = -1
static

◆ swdio_input

bool swdio_input
static

Definition at line 189 of file sysfsgpio.c.

Referenced by sysfsgpio_swd_write(), and sysfsgpio_swdio_drive().

◆ sysfsgpio_adapter_driver

struct adapter_driver sysfsgpio_adapter_driver
Initial value:
= {
.name = "sysfsgpio",
.transports = sysfsgpio_transports,
.init = sysfsgpio_init,
.quit = sysfsgpio_quit,
.reset = sysfsgpio_reset,
.jtag_ops = &sysfsgpio_interface,
.swd_ops = &bitbang_swd,
}
const struct swd_driver bitbang_swd
Definition: bitbang.c:614
static struct jtag_interface sysfsgpio_interface
Definition: sysfsgpio.c:550
static const struct command_registration sysfsgpio_command_handlers[]
Definition: sysfsgpio.c:534
static int sysfsgpio_reset(int trst, int srst)
Definition: sysfsgpio.c:330
static const char *const sysfsgpio_transports[]
Definition: sysfsgpio.c:548
static int sysfsgpio_init(void)
Definition: sysfsgpio.c:626
static int sysfsgpio_quit(void)
Definition: sysfsgpio.c:697

Definition at line 548 of file sysfsgpio.c.

◆ sysfsgpio_bitbang

struct bitbang_interface sysfsgpio_bitbang
static
Initial value:
= {
.read = sysfsgpio_read,
.write = sysfsgpio_write,
.swdio_read = sysfsgpio_swdio_read,
.swdio_drive = sysfsgpio_swdio_drive,
.swd_write = sysfsgpio_swd_write,
.blink = NULL,
}
static int sysfsgpio_swdio_read(void)
Definition: sysfsgpio.c:207
static void sysfsgpio_swdio_drive(bool is_output)
Definition: sysfsgpio.c:191
static int sysfsgpio_write(int tck, int tms, int tdi)
Definition: sysfsgpio.c:280
static int sysfsgpio_swd_write(int swclk, int swdio)
Definition: sysfsgpio.c:223
static bb_value_t sysfsgpio_read(void)
Definition: sysfsgpio.c:258
#define NULL
Definition: usb.h:16

Definition at line 548 of file sysfsgpio.c.

Referenced by sysfsgpio_init().

◆ sysfsgpio_command_handlers

const struct command_registration sysfsgpio_command_handlers[]
static
Initial value:
= {
{
.name = "sysfsgpio",
.mode = COMMAND_ANY,
.help = "perform sysfsgpio management",
.usage = "",
},
}
#define COMMAND_REGISTRATION_DONE
Use this as the last entry in an array of command_registration records.
Definition: command.h:253
@ COMMAND_ANY
Definition: command.h:42
const char * usage
a string listing the options and arguments, required or optional
Definition: command.h:241
static const struct command_registration sysfsgpio_subcommand_handlers[]
Definition: sysfsgpio.c:460

Definition at line 451 of file sysfsgpio.c.

◆ sysfsgpio_interface

struct jtag_interface sysfsgpio_interface
static
Initial value:
= {
.supported = DEBUG_CAP_TMS_SEQ,
.execute_queue = bitbang_execute_queue,
}
int bitbang_execute_queue(struct jtag_command *cmd_queue)
Definition: bitbang.c:293
#define DEBUG_CAP_TMS_SEQ
Definition: interface.h:187

Definition at line 548 of file sysfsgpio.c.

◆ sysfsgpio_subcommand_handlers

const struct command_registration sysfsgpio_subcommand_handlers[]
static

Definition at line 451 of file sysfsgpio.c.

◆ sysfsgpio_transports

const char* const sysfsgpio_transports[] = { "jtag", "swd", NULL }
static

Definition at line 548 of file sysfsgpio.c.

◆ tck_fd

int tck_fd = -1
static

Definition at line 177 of file sysfsgpio.c.

Referenced by cleanup_all_fds(), sysfsgpio_init(), and sysfsgpio_write().

◆ tck_gpio

int tck_gpio = -1
static

◆ tdi_fd

int tdi_fd = -1
static

Definition at line 179 of file sysfsgpio.c.

Referenced by cleanup_all_fds(), sysfsgpio_init(), and sysfsgpio_write().

◆ tdi_gpio

int tdi_gpio = -1
static

◆ tdo_fd

int tdo_fd = -1
static

Definition at line 180 of file sysfsgpio.c.

Referenced by cleanup_all_fds(), sysfsgpio_init(), and sysfsgpio_read().

◆ tdo_gpio

int tdo_gpio = -1
static

◆ tms_fd

int tms_fd = -1
static

Definition at line 178 of file sysfsgpio.c.

Referenced by cleanup_all_fds(), sysfsgpio_init(), and sysfsgpio_write().

◆ tms_gpio

int tms_gpio = -1
static

◆ trst_fd

int trst_fd = -1
static

Definition at line 181 of file sysfsgpio.c.

Referenced by cleanup_all_fds(), sysfsgpio_init(), and sysfsgpio_reset().

◆ trst_gpio

int trst_gpio = -1
static

Definition at line 168 of file sysfsgpio.c.

Referenced by cleanup_all_fds(), COMMAND_HANDLER(), and sysfsgpio_init().