embeded/rpi pico

rpi pico pio 예제(?)

구차니 2026. 11. 25. 14:50

 

jtag.pio

;/*
; * The MIT License (MIT)
; *
; * Copyright (c) 2020-2025 Patrick Dussud
; *
; * Permission is hereby granted, free of charge, to any person obtaining a copy
; * of this software and associated documentation files (the "Software"), to deal
; * in the Software without restriction, including without limitation the rights
; * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
; * copies of the Software, and to permit persons to whom the Software is
; * furnished to do so, subject to the following conditions:
; *
; * The above copyright notice and this permission notice shall be included in
; * all copies or substantial portions of the Software.
; *
; * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
; * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
; * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
; * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
; * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
; * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
; * THE SOFTWARE.
; *
; */

;JTAG implementation

.pio_version 0 // only requires PIO version 0
.program djtag_tdo
.side_set 1 opt

; Pin assignments:
; - TCK is side-set pin 0
; - TDI is OUT pin 0
; - TDO is IN pin 0
;
; Autopush and autopull must be enabled, and the serial frame size is set by
; configuring the push/pull threshold (32 bits). Shift should be left

; data is captured on the leading edge of each TCK pulse, and
; transitions on the trailing edge, or some time before the first leading edge.
    pull                        ; get length-1 and disregard previous OSR state 
    out x, 32       side 0      ; this moves the first 32 bits into X
loop:
    out pins, 1     side 0      ; Stall here on empty (sideset proceeds even if instruction stalls, so we stall with TCK low
    nop             side 1      ; raise TCK
    in pins, 1      side 1      ; sample TDO
    jmp x-- loop    side 0
    push            side 0      ; Force the last ISR bits to be pushed to the tx fifo
% c-sdk {
#include "hardware/gpio.h"
static inline void pio_jtag_init(PIO pio, uint sm,
        uint16_t clkdiv, uint pin_tck, uint pin_tdi, uint pin_tdo) {
    uint prog_offs = pio_add_program(pio, &djtag_tdo_program);
    pio_sm_config c = djtag_tdo_program_get_default_config(prog_offs);
    sm_config_set_out_pins(&c, pin_tdi, 1);
    sm_config_set_in_pins(&c, pin_tdo);
    sm_config_set_in_pin_count(&c 1);
    sm_config_set_sideset_pins(&c, pin_tck);
    //(shift to left, auto push/pull, threshold=nbits)
    sm_config_set_out_shift(&c, false, true, 8);
    sm_config_set_in_shift(&c, false, true, 8);
    sm_config_set_clkdiv_int_frac(&c, clkdiv, 0);

    // TDI, TCK output are low, TDO is input
    pio_sm_set_pins_with_mask(pio, sm, 0, (1u << pin_tck) | (1u << pin_tdi));
    pio_sm_set_pindirs_with_mask(pio, sm, (1u << pin_tck) | (1u << pin_tdi), (1u << pin_tck) | (1u << pin_tdi) | (1u << pin_tdo));
    pio_gpio_init(pio, pin_tdi);
    //pio_gpio_init(pio, pin_tdo);
    pio_gpio_init(pio, pin_tck);

    // jtag is synchronous, so bypass input synchroniser to reduce input delay.
    hw_set_bits(&pio->input_sync_bypass, 1u << pin_tdo);
    gpio_set_pulls(pin_tdo, false, true); //TDO is pulled down
    pio_sm_init(pio, sm, prog_offs, &c);
    pio_sm_set_enabled(pio, sm, true);
}
%}

 

pio_jtag.c

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);
    jtag->pin_tdi = pin_tdi;
    jtag->pin_tdo = pin_tdo;
    jtag->pin_tck = pin_tck;
    jtag->pin_tms = pin_tms;
    #if !( BOARD_TYPE == BOARD_QMTECH_RP2040_DAUGHTERBOARD )
    jtag->pin_rst = pin_rst;
    jtag->pin_trst = pin_trst;
    #endif
    uint16_t clkdiv = 31;  // around 1 MHz @ 125MHz clk_sys
    pio_jtag_init(jtag->pio, jtag->sm,
                    clkdiv,
                    pin_tck,
                    pin_tdi,
                    pin_tdo
                 );

    jtag_set_clk_freq(jtag, freq);
}

 

[링크 : https://github.com/phdussud/pico-dirtyJtag.git]