Python read/write jtag via FTDI chip

Python read/write jtag via FTDI chip

2022/03/03 Computer Engineering 0

https://detail.tmall.com/item.htm?id=608821542489&ut_sk=1.YQ5qR5EunYQDAGcswWUaYJAm_21380790_1645025705879.Copy.ShareGlobalNavigation_1&sourceType=item&suid=5126BB92-305B-45E6-8276-4172EC31002C&un=8a0a0fd7954c2f6e4c6e4bed9157ce66&share_crt_v=1&un_site=0&spm=a2159r.13376460.0.0&tbSocialPopKey=shareItem&sp_tk=N1NJYzJlWjE5Qlo=&cpp=1&shareurl=true&short_name=h.flQxYcG&bxsign=scd65TMfrhTLdTfSFZEA2TIorerOMUxRkGPXq1CP3M21S-rVCWvUVwd-K7-LDjMP5iVdFbrvI-pqNh1QUjfy_74ZmHByWfWWbQkQ35VzPBYR0iP51drxemJkdDVH4TmnTfC&tk=7SIc2eZ19BZ%E3%80%8CFT232H/FT2232HL%E6%A8%A1%E5%9D%97USB%E8%BD%ACFIFO/SPI/I2C/JTAG/RS232%E4%B8%B2%E5%8F%A3%E6%A8%A1%E5%9D%97/%E9%AB%98%E9%80%9F%E3%80%8D&app=chrome#

https://ftdichip.com/wp-content/uploads/2020/08/AN_110_Programmers_Guide_for_High_Speed_FTCJTAG_DLL-1.pdf

#!/usr/bin/env python3
from os import environ
from pyftdi.jtag import JtagEngine, JtagTool
from pyftdi.bits import BitSequence
JTAG_INSTR = {
  'SAMPLE'  : BitSequence('0000000101', msb=True, length=10),
  'IDCODE'  : BitSequence('0000000110', msb=True, length=10),
  'USERCODE': BitSequence('0000000111', msb=True, length=10),
  'BYPASS'  : BitSequence('1111111111', msb=True, length=10)}
url  = environ.get('FTDI_DEVICE', 'ftdi://ftdi:2232h/1')
jtag = JtagEngine(trst=True, frequency=3E6)
jtag.configure(url)
jtag.reset()
def read_idcode(jtag):
    instr = JTAG_INSTR['IDCODE']
    jtag.reset()
    jtag.write_ir(instr)
    idcode = jtag.read_dr(32)
    jtag.go_idle()
    print("  IDCODE: 0x%08x" % int(idcode))
def read_usercode(jtag):
    instr = JTAG_INSTR['USERCODE']
    jtag.write_ir(instr)
    idcode = jtag.read_dr(32)
    jtag.go_idle()
    print("USERCODE: 0x%08x" % int(idcode))
if __name__ == '__main__':
    read_idcode(jtag)
    read_usercode(jtag)
    del jtag
# altera_ep4ce6e22:  0x020f10dd 
# altera_ep4ce15e22: 0x020f30dd

Read PWM

#!/usr/bin/env python3
from pyftdi.jtag import JtagEngine
from pyftdi.bits import BitSequence
import time

FTDI_BOARD = 'ftdi://ftdi:2232h/1'
SPEED = 5
TCK_FREQ = 1e6
MAX_WIDTH = 255

def read_pwm():
    jtag.write_ir(RD_PWM)
    pwm = jtag.read_dr(1)
    print("PWM: 0x%x" % int(pwm))

# Define Instructions
IDCODE = BitSequence('00000100', msb=True, length=8)
WR_WIDTH = BitSequence('00000101', msb=True, length=8)
RD_PWM = BitSequence('00000110', msb=True, length=8)

# Initialize
jtag = JtagEngine(frequency=TCK_FREQ)
jtag.configure(FTDI_BOARD)

jtag.reset()    # Test Logic Reset (5 times of TMS=1)

# To Shift-IR, Exit1-IR and finally move to UpdateIR
jtag.write_ir(IDCODE)

# Move to Shift-DR, repeat Shift-DR, wait 15ms, and move to Test-Logic Reset
idcode = jtag.read_dr(32)
print("IDCODE (reset): 0x%x" % int(idcode))

width = 0
dir = 1
while True:
    jtag.write_ir(WR_WIDTH)
    jtag.write_dr(BitSequence(width, msb=False, length=8))

    width += dir * SPEED
    if width >= MAX_WIDTH:
        read_pwm()
        dir = -1
        width = MAX_WIDTH
    elif width <= 0:
        read_pwm()
        dir = 1
        width = 0

    time.sleep(0.001)

Pins reference : https://ftdichip.com/wp-content/uploads/2020/07/AN_184-FTDI-Device-Input-Output-Pin-States.pdf

Leave a Reply

Your email address will not be published.