From ac7c57c92a02d1c849ab051c25824e48ee167d27 Mon Sep 17 00:00:00 2001 From: phdussud Date: Mon, 7 Nov 2022 10:22:53 -0800 Subject: [PATCH] Change RST pin to emulate an open drain behavior --- pio_jtag.c | 33 ++++++++++++++++++++++++++++++--- pio_jtag.h | 3 ++- 2 files changed, 32 insertions(+), 4 deletions(-) diff --git a/pio_jtag.c b/pio_jtag.c index 63cd54b..70e753d 100644 --- a/pio_jtag.c +++ b/pio_jtag.c @@ -7,6 +7,32 @@ void jtag_task();//to process USB OUT packets while waiting for DMA to finish #define DMA +#if 0 +static bool pins_source = false; //false: PIO, true: GPIO + +static void switch_pins_source(const pio_jtag_inst_t *jtag, bool gpio) +{ + if (pins_source != gpio) + { + if (gpio) + { + gpio_put(jtag->pin_tdi, gpio_get(jtag->pin_tdi)); + gpio_set_function(jtag->pin_tdi, GPIO_FUNC_SIO); + gpio_put(jtag->pin_tck, gpio_get(jtag->pin_tck)); + gpio_set_function(jtag->pin_tck, GPIO_FUNC_SIO); + gpio_set_dir_out_masked((1 << jtag->pin_tdi) | (1 << jtag->pin_tck)); + } + else + { + gpio_set_function(jtag->pin_tdi, GPIO_FUNC_PIO0); + gpio_set_function(jtag->pin_tck, GPIO_FUNC_PIO0); + } + pins_source = gpio; + } +} +#endif + + #ifdef DMA @@ -215,13 +241,14 @@ uint8_t __time_critical_func(pio_jtag_write_tms_blocking)(const pio_jtag_inst_t static void init_pins(uint pin_tck, uint pin_tdi, uint pin_tdo, uint pin_tms, uint pin_rst, uint pin_trst) { + // emulate open drain with pull up and direction + gpio_pull_up(pin_rst); gpio_clr_mask((1u << pin_tms) | (1u << pin_rst) | (1u << pin_trst)); gpio_init_mask((1u << pin_tms) | (1u << pin_rst) | (1u << pin_trst)); - gpio_set_dir_masked( (1u << pin_tms) | (1u << pin_rst) | (1u << pin_trst), 0xffffffffu); + gpio_set_dir_masked( (1u << pin_tms) | (1u << pin_trst), 0xffffffffu); + gpio_set_dir(pin_rst, false); } - - void init_jtag(pio_jtag_inst_t* jtag, uint freq, uint pin_tck, uint pin_tdi, uint pin_tdo, uint pin_tms, uint pin_rst, uint pin_trst) { init_pins(pin_tck, pin_tdi, pin_tdo, pin_tms, pin_rst, pin_trst); diff --git a/pio_jtag.h b/pio_jtag.h index bbf7b5b..afd1550 100644 --- a/pio_jtag.h +++ b/pio_jtag.h @@ -37,7 +37,8 @@ static inline void jtag_set_tms(const pio_jtag_inst_t *jtag, bool value) } static inline void jtag_set_rst(const pio_jtag_inst_t *jtag, bool value) { - gpio_put(jtag->pin_rst, value); + /* Change the direction to out to drive pin to 0 or to in to emulate open drain */ + gpio_set_dir(jtag->pin_rst, !value); } static inline void jtag_set_trst(const pio_jtag_inst_t *jtag, bool value) {