3/08/2020

FTDI기반의 OpenOCD 설정 (FT2232)

1. FT2232H 의 기능 및 기본구성 

기본적으로 FT232x or CP2102x 관련 칩들은 USB To Serial 기능을 가지고 있다. 
하지만, FT22x의 경우 USB To Serial의 기능도 제공하지만, 부가적인 기능을 제공해준다. 
예를들면, FT2232H의 경우 FTDI의 MPSSE의 기능을 이용하여 SPI/I2C 비롯하여 JTAG기능까지 지원 가능하다.

우선 FT2232H Mini Module의 기본구조를 살펴보면, 2개의 Channel을 제공하며, 원하는대로 변경사용가능하다. 






상위 Connect PIN Map을 보면 Channel은 CN2 와 CN3로 구분되며, 각각의 Channel의 핀구성을 살펴보자. 

  • FTDI의 FT2232H Miniboard Manual (상위보드)
좀 더 상위 FT2232H Mini Board의 동작구성을 알고 싶다면, 아래의 Module Manual 참조 
  https://www.ftdichip.com/Support/Documents/DataSheets/Modules/DS_FT2232H_Mini_Module.pdf


  • FDDI Device Driver
  https://www.ftdichip.com/Drivers/D2XX.htm


1.1 FT2232H Board 설정 및 기본이해 



나의 경우는 상위보드를 구입 후 USB Host의 전원기반으로 사용할 것이므로 USB Bus Power로 설정로 설정하여 구성할 것이다. 
만약 전원이 부족하다면, 어쩔수 없이 USB Self-Powered부분을 참조해서 구성하자. 

상위 Board의 Connect Pin Map 보고 아래와 같이 연결해주자. 

1) Connect VBUS to VCC
  1. CN3-1 (VBUS USB) 와 CN3-3 (VCC 5V) 연결 
* USB VBUS -> 5V 전원공급하여 3.3V전원 공급가능 
2) Connect V3V3 to VIO
  1. CN2-1 (V3V3 OUT) 와 CN2-11 (VIO), CN2-21 (VIO) 연결  ( 3.3V )
  2. CN2-3 (V3V3 OUT) 와 CN3-12 (VIO) 연결 (3.3V)
  3. CN2-5 (V3V3 OUT) 와 CN3-22 (VIO) 연결 (3.3V)

상위 Board Manul 에서 Table 참조



1.2  FT2232 지원기능 및 동작 

기본적으로 USB의 Dual channel로 USB Device 2개로 연결이 되어 아래와 같이 다양하게 동작가능
UART일 경우 VCP( Virtual COM Port)설정이 가능
  1. Single chip USB to dual channel UART (RS232,RS422 or RS485).
  2. Single chip USB to dual channel FIFO.
  3. Single chip USB to dual channel JTAG.
  4. Single chip USB to dual channel SPI.
  5. Single chip USB to dual channel I2C.
  6. Single chip USB to dual channel Bit-Bang.
  7. Single chip USB to dual combination of any of above interfaces.
  8. Single chip USB to Fast Serial Optic Interface.
  9. Single chip USB to CPU target interface (as memory), double and independent.
  10. Single chip USB to Host Bus Emulation (as CPU).
  11. PDA to USB data transfer
  12. USB Smart Card Readers
  13. USB Instrumentation
  14. USB Industrial Control
  15. USB MP3 Player Interface
  16. USB FLASH Card Reader / Writers
  17. Set Top Box PC - USB interface
  18. USB Digital Camera Interface
  19. USB Bar Code Readers
아래의 DataSheet를 보면, 상당히 많은 기능을 제공을 해주는데, 이 많은 기능중에 몇개밖에 사용하지 않는다는게, 너무 안타깝다. 

FT2232H Datasheet 
  https://www.ftdichip.com/Support/Documents/DataSheets/ICs/DS_FT2232H.pdf




  • MPSSE Mode (JTAG/SPI/I2C)
JTAG을 사용하기위해서는 MPSSE Mode 설정 및 아래의 문서의 Example 반드시확인
  https://www.ftdichip.com/Support/Documents/AppNotes/AN_135_MPSSE_Basics.pdf



  • MPSSE Mode (JTAG/SPI/I2C)의 설정 Flow 
사용법은 아래의 D2XX Programmer GUIDE참조



  • D2XX Programmer GUIDE (FD_xxx API)
상위 MPSSE Programing을 이해를 위해 각 FD_xxx API 관련이해
  https://www.ftdichip.com/Support/Documents/ProgramGuides/D2XX_Programmer's_Guide(FT_000071).pdf


  • JTAG을 Control 하기위해서 Opcode 관련부분
  https://www.ftdichip.com/Support/Documents/AppNotes/AN_108_Command_Processor_for_MPSSE_and_MCU_Host_Bus_Emulation_Modes.pdf


  • JTAG 관련문서
  https://www.ftdichip.com/Support/Documents/AppNotes/AN_129_FTDI_Hi_Speed_USB_To_JTAG_Example.pdf
  https://www.ftdichip.com/Support/Documents/AppNotes/AN_110_Programmers_Guide_for_High_Speed_FTCJTAG_DLL.pdf


1.3 Window에서 FT2232 Device USB 연결 

상위 FT2232 Board를 Self Power로 설정 후 USB로 Window에 연결 후 각 설정을 살펴보자

  • 장치관리자->USB Controller
  1. USB Composite Device 
  2. USB Serial Converter A  (Vendor ID: 0x0403 Product ID: 0x6010)
  3. USB Serial Converter B
    
  • 장치관리자->Port
  1. Serial Port로 2개로 기본인식됨 (FTDI CDM Driver로 연결)



1.4 FT2232H Module의 최종 연결상태 

  1. USB Self Power로 연결 
  2. JTAG 연결 





2. Raspberry Pi3 의 JTAG PIN 구성 및 설정 


Raspberry Pi3 의 Pinmux ALT4/ALT5로 변경사용
** ALT4 와 ALT5의 JTAG의 설정차이는 TRST의 사용여부
** 기본적으로 JTAG은 TCLK/TDI/TDO/TMS는 반드시 필요하며, RTCK 옵션 

https://sysprogs.com/VisualKernel/tutorials/raspberry/jtagsetup/



Raspberry Pi3 B JTAG 관련정보 
  https://metebalci.com/blog/bare-metal-raspberry-pi-3b-jtag/
  https://www.suse.com/c/debugging-raspberry-pi-3-with-jtag/
  https://lb.raspberrypi.org/forums/viewtopic.php?t=143938
  https://medium.com/@burstaccessing/debugging-raspberry-pi-3-b-475452974f21
  https://wiki.aalto.fi/download/attachments/84747235/presentation.pdf?version=1&modificationDate=1386936615719&api=v2

Raspberry Pi3에서 JTAG사용 
  https://github.com/synthetos/PiOCD/wiki/Using-a-Raspberry-Pi-as-a-JTAG-Dongle



  • Raspberry Pi3 B 의 ALT4 사용 (GPIO22/23 미사용)

Pin #Function
GPIO22ARM_TRST
GPIO23ARM_RTCK
GPIO24ARM_TDO
GPIO25ARM_TCK
GPIO26ARM_TDI
GPIO27ARM_TMS

RTCLK을 필요없으며, 이 부분은 JTAG관련부분 참조
  1. Output Pin : TDI/TMS/TCK/TRST 
  2. Input Pin: TDO 

  • Raspberry Pi3 B  와 FT2232 Channel A or B 연결  
  1. Pin 15: GPIO 22 : TRST        AD4/BD4
  2. Pin 18: GPIO 24 : TDO         AD2/BD2
  3. Pin 22: GPIO 25 : TCLK        AD0/BD0  
  4. Pin 37: GPIO 26 : TDI          AD1/BD1
  5. Pin 13: GPIO 27 : TMS         AD3/BD3
  6. Pin 06: GROUND               CN2-2/GND

Raspberry Pi JTAG
TRST은 옵션이지만, Raspberry Pi에서 없으면 동작이 안됨 주의



Raspberry Pi JTAG Pin 및 ALT4/5 확인 
  https://www.raspberrypi.org/documentation/hardware/computemodule/datasheets/rpi_DATA_CM_1p0.pdf


Raspberry Pi3 의 PIN 구성
https://www.raspberrypi-spy.co.uk/2012/06/simple-guide-to-the-rpi-gpio-header-and-pins/

https://learn.adafruit.com/adafruits-raspberry-pi-lesson-4-gpio-setup/the-gpio-connector



  • Raspberry Pi3 B 의 JTAG ALT4 설정방법 
ALT4로 설정시 TRST도 같이 설정됨
pi@raspberrypi:~ $ cat /boot/config.txt  // enable_jtag_gpio 추가 (ALT4 자동설정) sudo vi 이용 
....
enable_jtag_gpio=1
.....

pi@raspberrypi:~ $ vcgencmd get_config arm_freq
arm_freq=1200

pi@raspberrypi:~ $ vcgencmd get_config enable_jtag_gpio   // 설정된 값 확인
enable_jtag_gpio=1

pi@raspberrypi:~ $ raspi-gpio get 22         // PIN 확인 
GPIO 22: level=0 fsel=3 alt=4 func=ARM_TRST
pi@raspberrypi:~ $ raspi-gpio get 23
GPIO 23: level=0 fsel=3 alt=4 func=ARM_RTCK
pi@raspberrypi:~ $ raspi-gpio get 24
GPIO 24: level=1 fsel=3 alt=4 func=ARM_TDO
pi@raspberrypi:~ $ raspi-gpio get 25
GPIO 25: level=0 fsel=3 alt=4 func=ARM_TCK
pi@raspberrypi:~ $ raspi-gpio get 26
GPIO 26: level=0 fsel=3 alt=4 func=ARM_TDI
pi@raspberrypi:~ $ raspi-gpio get 27
GPIO 27: level=0 fsel=3 alt=4 func=ARM_TMS

 //만약 PIN을 직접 변경하고 싶다면 gpio=0-27=a2 (ALT2) 이런식으로 설정하면됨 세부내용은 Manual 참조 

pi@raspberrypi:~ $ ls /lib/firmware


  • Raspberry Pi3 B 의 JTAG ALT5 설정방법 
pi@raspberrypi:~ $ cat /boot/config.txt  // JTAG ALT5 수동설정 
....
gpio=4-6=a5
gpio=12,13=a5
.....

pi@raspberrypi:~ $ raspi-gpio get 4
GPIO 4: level=1 fsel=2 alt=5 func=ARM_TDI
pi@raspberrypi:~ $ raspi-gpio get 5
GPIO 5: level=0 fsel=2 alt=5 func=ARM_TDO
pi@raspberrypi:~ $ raspi-gpio get 6
GPIO 6: level=0 fsel=2 alt=5 func=ARM_RTCK
pi@raspberrypi:~ $ raspi-gpio get 12
GPIO 12: level=0 fsel=2 alt=5 func=ARM_TMS
pi@raspberrypi:~ $ raspi-gpio get 13
GPIO 13: level=0 fsel=2 alt=5 func=ARM_TCK


/boot/config 관련내용
      https://www.raspberrypi.org/documentation/configuration/config-txt/gpio.md
      https://www.raspberrypi.org/documentation/configuration/config-txt/


    2.1 Raspberry Pi의 DTS 설정변경 

    JTAG 설정확인 및 이외에 다른 기능을 직접수정하여, 테스트하고 싶다면, 아래와 같이 Device Tree 부분을 직접 수정하여 진행

    • Device Tree 확인 및 수정방법
    pi@raspberrypi:~ $ cat /proc/device-tree/soc/gpio@7e200000/jtag_gpio22/name   //Device Tree 확인 (ALT4)
    jtag_gpio22
    
    pi@raspberrypi:~ $ cat /proc/device-tree/soc/gpio@7e200000/jtag_gpio4/name   //Device Tree 확인 (ALT5)
    jtag_gpio4
    
    pi@raspberrypi:~ $ dtc -I fs /proc/device-tree  //Device Tree Syntax 확인
    or
    pi@raspberrypi:~ $ dtc -I fs /proc/device-tree > current.dts   //Device Tree Syntax 저장
    
    //DTB 직접 수정 후 Merge 한 후 diff 하는방법 
    
    pi@raspberrypi:~ $ dtc -I fs -O dtb -o base.dtb /proc/device-tree   // /proc/device-tree DTB 저장
    
    pi@raspberrypi:~ $ dtmerge base.dtb merged.dtb - sd_overclock=62   // base.dtb 와 overlay 변경사항 합쳐서 merged.dtb 저장 
    
    pi@raspberrypi:~ $ dtdiff base.dtb merged.dtb // base.dtb 와 merged.dtb 차이비교 
    --- /dev/fd/63  2020-08-24 16:55:41.175892749 +0900
    +++ /dev/fd/62  2020-08-24 16:55:41.175892749 +0900
    @@ -910,7 +910,7 @@
                    };
    
                    mmc@7e202000 {
    -                       brcm,overclock-50 = <0x0>;
    +                       brcm,overclock-50 = <0x3e>;
                            brcm,pio-limit = <0x1>;
                            bus-width = <0x4>;
                            clocks = <0x8 0x14="">;
    
    

    Raspberry Pi3 Device Tree
    Device Tree기반으로 Pin Configuration 구성확인
      https://www.raspberrypi.org/documentation/configuration/pin-configuration.md
      https://www.raspberrypi.org/documentation/configuration/device-tree.md


    3. OpenOCD 기본동작과 이해 

    Window 혹은 Linux에서 GDB에서 OpenOCD Server로 연결되어 JTAG을 Control하기 위해서 libusb or libWinUSB를 이용하여
    직접 User Interface에서 USB에서 Control하여 JTAG을 제어를 한다.

    https://www.allaboutcircuits.com/technical-articles/getting-started-with-openocd-using-ft2232h-adapter-for-swd-debugging/

    OpenOCD 와 FT2232
      https://www.allaboutcircuits.com/technical-articles/getting-started-with-openocd-using-ft2232h-adapter-for-swd-debugging/
      https://dzone.com/articles/jtag-debugging-the-esp32-with-ft2232-and-openocd
      http://openocd.org/doc/html/Debug-Adapter-Hardware.html

    GDB 연결
      http://openocd.org/doc/html/GDB-and-OpenOCD.html

    Telnet Server Port 설정
    GDB Server Port 설정
    TCL Server Port 설정
      http://openocd.org/doc/html/General-Commands.html

    Raspberry PI 기반의 OpenOCD전체사용법
      https://sysprogs.com/VisualKernel/tutorials/raspberry/jtagsetup/

    FT2232H 관련사항
      http://openocd.org/doc/html/Debug-Adapter-Hardware.html


    3.1 Window 용 OpenOCD Download

    • Download Window for OpenOCD (필수)
    Window용 Pre-built OpenOCD file
      https://gnutoolchains.com/arm-eabi/openocd/

    • Ecplise OpenOCD for Window (옵션)
      https://gnu-mcu-eclipse.github.io/openocd/install/


    3.2 FTDI (FT2232) Libusb 설정 

    Window에서 Libusb관련부분 설정을 변경하기 위해서 장치관리자에서 USB Driver를 변경해야하는데 관련 Tool을 Download하여 쉽게 변경하자 

    • Libusb/WinUSB (USBDriverTool) 설정 Tool Download 
    이미 상위 Window OpenOCD 내부에 존재하므로 별도의 설치를 할 필요 없지만 없다면 별도 download
    libusb에러가 발생시 FTDI의 CDM의 Driver가 설치되어있는데, libusb설치진행
      https://visualgdb.com/UsbDriverTool/

    • Libusb/WinUSB (Zadig) 설정 Tool Download
    USBDriverTool 과 동일한 기능으로 Window에서 libusb를 사용하기위해서 사용하는 Tool
    아래의 OpenOCD 내부의  drivers/USBdriverTool.exe 와 동일기능 
    만약 Device 가 나오지 않는다면, Option->List All Devices 를 체크 검색가능 
      https://zadig.akeo.ie/


    상위 두 개의 Tools 중 하나를 Download하여 아래와 같이 Window USB Driver를 변경진행


    FT2232 Board의 Channel A만 설정하여 진행할 것이며,
    상위 Window용 OpenOCD 내부에 drivers/USBdriverTool.exe 가 존재하며 이를 실행

    • Install Libusb -WinUSB 설정 (USB Serial Converter A)
    libusb의 제어를 위해서 상위에서 Download 한 USBdriverTool을 사용하여 libusb 설치진행
    장치관리자에서 범용직렬버스 컨트롤러 에서 libusb 로 USB Serial Converter A 이동
    기존의 Serial에서 libusb기반 Driver로변경 (아래 둘 중하나 선택)
    1. libus -WinUSB
    2. WinUSB 




    FT2232 OpenOCD Example
      https://mcuoneclipse.com/2019/10/20/jtag-debugging-the-esp32-with-ft2232-and-openocd/

    • 장치관리자
    libusb(WinUSB) devices ->USB Serial Converter A (Libusb -WinUSB) 변경확인



    • FTDI CDM
    Window의 경우 FTDI의 경우 처음 CDM Serial Driver로 설치가 되어있는데, 이부분을 libusb를 통하여 JTAG으로 변경해야함
      https://www.ftdichip.com/Support/Documents/InstallGuides/AN_396%20FTDI%20Drivers%20Installation%20Guide%20for%20Windows%2010.pdf
      https://www.ftdichip.com/Support/Utilities/CDM_Uninst_GUI_Readme.html


    3.3 OpenOCD 실행

    OpenOCD의 Interface의 설정은 기본적으로 JTAG Pin의 초기화로 libusb를 통해 모든 Pin을 설정이 가능하며, 각 Interface 와 Version에 따라 사용법이 다르다.
    Linux 와 Window도 다르기 때문에 주의를 해야함

    Config 파일은 하나로 만들어도 상관없지만,보통 두개로 나누어 사용한다 (3개도 가능)
    1. Interface Config :  JTAG 에 관련된 설정 
    2. Target config : Target Board에 관련된 설정 

    • Window에서 Bat 파일 (OpenOCD.bat)
    아래와 같이 Simple하게 실행하도록 본인이 각각 Batch 파일을 만들어서 실행하자

    @ECHO OFF
    TITLE OpenOCD TEST
    rem Author: Jeonghun Lee
    rem TEST OpenOCD
    ECHO ------------------------------------------------------------------------------------------------------
    ECHO Start to exec OpenOCD 
    ECHO.
    ECHO.
    bin\openocd.exe -f share\openocd\scripts\interface\ftdi\test.cfg  -f share\openocd\scripts\target\raspberrypi3.cfg
    ECHO.  
    ECHO.   
    ECHO ------------------------------------------------------------------------------------------------------
    PAUSE
    


    @ECHO OFF
    TITLE OpenOCD TEST
    rem Author: Jeonghun Lee
    rem TEST OpenOCD
    ECHO ------------------------------------------------------------------------------------------------------
    ECHO Start to exec OpenOCD 
    ECHO.
    ECHO.
    bin\openocd.exe -f test.cfg  -f raspberrypi3.cfg
    ECHO.  
    ECHO.   
    ECHO ------------------------------------------------------------------------------------------------------
    PAUSE
    


    3.4 OpenOCD Interface/target 설정

    위에서 설명했듯이 보통 Interface 와 Target으로 구분하여 설정하지만, 귀찮다면 하나로 설정을 해도 상관없다

    • USB Vendor ID: FTDI 0x0403
    1. Product ID (2232C/D/H):  0x6010
    2. Product ID (4232H) : 0x6011

    FT2232D USB to JTAG Hardware (Sample Config)
      http://www.baresystemdesign.com/index.php?section=hardware&hw=jtag

    • Interface 기본설정 (Raspberry Pi3)
    share\openocd\scripts\interface\ftdi\test.cfg
    ### ftdi/test.cfg 
    ###
    
    #
    # GDB/Telnet Port 설정
    #
    telnet_port 4444
    gdb_port 3333
    
    #adapter_khz     1000
    
    #
    # MPSSE (Multi-Protocol Synchronous Serial Engine) 
    # src/jtag/drivers/mpsse.c 
    # support TYPE_FT2232C/TYPE_FT2232H/TYPE_FT4232H/TYPE_FT232H
    # 
    #interface ftdi
    adapter driver ftdi
    adapter speed 500
    
    #
    # select JTAG mode (JTAG/SWD) 중 선택 
    #
    # transport select swd
    
    transport select jtag
    
    #
    # FT2232H USB VID PID
    # lsusb 로 확인가능 
    ftdi_vid_pid 0x0403 0x6010  
    
    #
    # FT2232H Channel A or B (상위 Board 참조)
    # ftdi_channel 0
    # ftdi_channel 1
    #
    ftdi_channel 0
    
    #
    # FT2232H Channel A or B not used
    #
    #ftdi_device_desc "FT2232H_CH_A"
    
    #
    # GPIO Init Setting 
    # ftdi_layout_init data (high:1, low:0)  direction (out:1,in:0)
    #
    # TRST 0x0010 0x0010   Output Pull up 설정
    # TRST은 반드시 연결하며, Pin 정의는 별도 필요없으며, 사용안함  
    #  
    #
    ftdi_layout_init 0x0018 0x001b
    
    ## 상위 설정값 확인 (나의경우 아래와 같이 사용)
    ##                          0x001b    0x0018
    ## Channe A/B   Pin      In/Out  High/Low (상위 16bit/2Byte 구성)
    # ADBUS0  Bit0  JTAG_TCK     Out 
    # ADBUS1  Bit1  JTAG_TDI     Out 
    # ADBUS2  Bit2  JTAG_TDO     In       
    # ADBUS3  Bit3  JTAG_TMS     Out     (Pull Up)
    # ADBUS4  Bit4  JTAG_TRST_L  Out     (Pull Up)
    # ADBUS5  Bit5  NC
    # ADBUS6  Bit6  NC
    # ADBUS7  Bit7  NC
    
    # ACBUS0  Bit8  NC
    # ACBUS1  Bit9  NC  
    # ACBUS2  Bit10 NC
    # ACBUS3  Bit11 NC
    # ACBUS4  Bit12 NC
    # ACBUS5  Bit13 NC
    # ACBUS6  Bit14 NC
    # ACBUS7  Bit15 NC
    #


    • Interface의 Pin 설정이해 와 Pin 정의  
    상위 ftdi_channel 0 or 1 로 channel A or B로 설정 후 각 Pin의 Direction 및 Pull up/down 설정을 하자

    #
    #
    # FT2232H Channel A (현재 Channel A)
    # ftdi_channel 0  
    # FT2232H Channel B
    # ftdi_channel 1
    #
    ftdi_channel 0
    
    # Channel A or B
    # GPIO Init Setting 
    # ftdi_layout_init data (high:1, low:0)  direction (out:1,in:0)
    #
    #                0x8= 0b1000  0xB = 0b1011
    #ftdi_layout_init 0x0008 0x000b
    
    ftdi_layout_init 0x0018 0x001b
    ftdi_layout_signal nTRST -data 0x0010 -oe 0x0010
    
    
    # various examples (다양한 예제를 위해 참고)
    # ftdi_layout_signal GIO1 -data 0x0100
    # ftdi_layout_signal GIO2 -data 0x0200
    # ftdi_layout_signal GIO3 -data 0x0400
    # ftdi_layout_signal GIO4 -data 0x0400
    
    ###### Channel A/B
    # ADBUS0  Bit0  JTAG_TCK     Out 
    # ADBUS1  Bit1  JTAG_TDI     Out 
    # ADBUS2  Bit2  JTAG_TDO     In
    # ADBUS3  Bit3  JTAG_TMS     Out  (Pull Up)
    # ADBUS4  Bit4  JTAG_TRST_L  Out  (Pull Up)
    # ADBUS5  Bit5  NC
    # ADBUS6  Bit6  NC
    # ADBUS7  Bit7  NC
    
    # ACBUS0  Bit8  NC
    # ACBUS1  Bit9  NC  
    # ACBUS2  Bit10 NC
    # ACBUS3  Bit11 NC
    # ACBUS4  Bit12 NC
    # ACBUS5  Bit13 NC
    # ACBUS6  Bit14 NC
    # ACBUS7  Bit15 NC

    • Interface의 TRST/SRST Pin 정의
    TRST/SRST은 옵션이며, 필수사항이 아니며 필요할 경우 사용하자
    1. TRST은 TRST에 연결하여 사용하며, GPIO Control
    2. SRST의 TARGET Board의 HW RESET 부분에 연결설정 GPIO Control
    #
    #
    # General JTAG Setting 
    # ftdi_layout_init 0x0008 0x000b
    #
    # General JTAG + nTRST/nSRST 추가 
    ftdi_layout_init 0x0288 0x028b
    
    #
    # Creat a signal with the name 
    # ftdi_layout_signal name [-data|-ndata data_mask] [-input|-ninput input_mask] [-oe|-noe oe_mask] [-alias|-nalias name]
    # 이름과 같이 Signal을 정의
    #
    # 내부적으로 input 과 output 을 연결하는 방식을 설정가능 
    #
    # Data가 나가는 방식결정 (Output) 
    # -ndata: inverting data inputs
    # -data:  non-inverting inputs. 
    #  data_mask 
    #
    # 상위에서 결정된 Data input이 어디에 있는 output으로 연결할 것인지 결정하며, output/ not output 
    # -oe : output enable input to output butter 연결        
    # -noe: not output enable input to output butter 연결
    #  oe_mask
    #
    #  The options -input and -ninput specify the bitmask for pins to be read with the method ftdi_get_signal.
    
    ftdi_layout_signal nTRST -data 0x0200 -oe 0x0200
    ftdi_layout_signal nSRST -data 0x0080 -oe 0x0080
    

    아직 -noe 과 -ndata를 실험해보지 못했음
    #
    # -noe 0x0040 : JTAG_TDO Input 설정 
    #
    #ftdi_layout_signal nTRST -ndata 0x0200 -noe 0x0040
    #ftdi_layout_signal nSRST -ndata 0x0080 -noe 0x0040
    


    Interface config 관련설정 Adapter관련부분 

    상위 설정을 이해하려면 아래의 Manual을 이해해야 하지만, Version 과 OS를 주의
      http://openocd.org/doc/html/Debug-Adapter-Configuration.html

    OpenOCD는 다양한 JTAG 및 다양한 Device를 JTAG기능으로 사용가능하므로 체크
      http://openocd.org/doc/html/Debug-Adapter-Hardware.html

    • Target config
    기본이해
    1. reset_config:  none or trst_and_srst // 나의 경우 미사용 none
    2. adapter_khz:  상위 adapter speed 와 동일한 기능이며 속도
    3. jtag_ntrst_delay:  TRST을 사용할 경우
    세부설정 
    1. _CHIPNAME: CPU Name (본인이 정의)
    2. _DAP_TAPID: Test Access Ports ID로 이미 정해져 있으며, 이 값으로 생각하고 찾음 
    3. jtag newtap : 상위 Test Access Ports의 ID와 찾아서 비교하고 생성 
    4. dap create  : Debug Access Port 정의 

    세부사용법은 아래사이트 참조
      http://openocd.org/doc/html/TAP-Declaration.html

    ARM CORE 4개의 CTI 와 DEBUG 주소설정
      https://developer.arm.com/documentation/ddi0464/f/debug/external-debug-interface/memory-map

    share\openocd\scripts\target\rpi3.cfg (참고 version)

    # Credits to Petr Tesarik from SUSE who provided this updated configuration,
    # source: https://www.suse.com/c/debugging-raspberry-pi-3-with-jtag/
    transport select jtag
    # we need to enable srst even though we don't connect it
    reset_config trst_and_srst
    #adapter_khz 1000  
    jtag_ntrst_delay 500
    if { [info exists CHIPNAME] } {
      set _CHIPNAME $CHIPNAME
    } else {
      set _CHIPNAME rpi3
    }
    
    #
    # Main DAP
    #
    if { [info exists DAP_TAPID] } {
       set _DAP_TAPID $DAP_TAPID
    } else {
       set _DAP_TAPID 0x4ba00477
    }
    
    jtag newtap $_CHIPNAME tap -irlen 4 -ircapture 0x1 -irmask 0xf -expected-id $_DAP_TAPID -enable
    dap create $_CHIPNAME.dap -chain-position $_CHIPNAME.tap
    
    set _TARGETNAME $_CHIPNAME.a53
    set _CTINAME $_CHIPNAME.cti
    set DBGBASE {0x80010000 0x80012000 0x80014000 0x80016000}
    set CTIBASE {0x80018000 0x80019000 0x8001a000 0x8001b000}
    set _cores 4
    for { set _core 0 } { $_core < $_cores } { incr _core } {
    cti create $_CTINAME.$_core -dap $_CHIPNAME.dap -ap-num 0 \
            -ctibase [lindex $CTIBASE $_core]
    target create $_TARGETNAME.$_core aarch64 \
            -dap $_CHIPNAME.dap -coreid $_core \
            -dbgbase [lindex $DBGBASE $_core] -cti $_CTINAME.$_core
    $_TARGETNAME.$_core configure -event reset-assert-post "aarch64 dbginit"
        $_TARGETNAME.$_core configure -event gdb-attach { halt }
    } 
    

    본인이 원하는 곳을 상위 것을 참조하여, 아래와 같이 수정하여 사용하도록 하자
    share\openocd\scripts\target\raspberrypi3.cfg

    # ref: http://www.raspberrypi.org/forums/viewtopic.php?f=72&t=100268
    #    : http://infocenter.arm.com/help/index.jsp?topic=/com.arm.doc.ddi0464f/ch10s06s01.html
    
    #adapter speed 1000
    #adapter srst delay 400
    ## 현재 trst 과 srst를 사용하지 않으며, TRST은 Pull up 만 직접설정 
    reset_config none
    
    if { [info exists CHIPNAME] } {
    set _CHIPNAME $CHIPNAME
    } else {
    set _CHIPNAME bcm2837
    }
    
    #
    # Main DAP(Debug  Access Port), TAP(Test Access Port)
    #
    if { [info exists DAP_TAPID] } {
       set _DAP_TAPID $DAP_TAPID
    } else {
       set _DAP_TAPID 0x4ba00477
    }
    
    #
    # TAP의 Scan Chain/Autoprobing 작업으로 IR capture 와 TAPID를 확인가능 
    #
    jtag newtap $_CHIPNAME cpu -irlen 4 -ircapture 0x1 -irmask 0xf -expected-id $_DAP_TAPID -enable
    
    #
    # DAP는 ARM에서 사용되는 Debug Access Point로 관련부분을 설정 
    #
    dap create $_CHIPNAME.dap -chain-position $_CHIPNAME.cpu
    
    proc bcm2837_create_core {_CHIPNAME corenum corebase ctibase} {
     set _TARGETNAME $_CHIPNAME.cpu.$corenum
     cti create $_CHIPNAME.cti.$corenum -dap $_CHIPNAME.dap -ctibase $ctibase
     target create $_TARGETNAME aarch64 -dap $_CHIPNAME.dap -coreid $corenum -dbgbase $corebase -rtos hwthread -cti $_CHIPNAME.cti.$corenum
    
     $_TARGETNAME configure -event reset-assert-post "aarch64 dbginit"
     $_TARGETNAME configure -event gdb-attach { halt }
    }
    
    bcm2837_create_core $_CHIPNAME 0 0x80010000 0x80018000
    bcm2837_create_core $_CHIPNAME 1 0x80012000 0x80019000
    bcm2837_create_core $_CHIPNAME 2 0x80014000 0x8001A000
    bcm2837_create_core $_CHIPNAME 3 0x80016000 0x8001B000
    target smp $_CHIPNAME.cpu.0 $_CHIPNAME.cpu.1 $_CHIPNAME.cpu.2 $_CHIPNAME.cpu.3
    

    상위 동작을 이해하고 싶다면, 우선 JTAG의 동작에 대해서 알아두도록하자.

    Scan Chain/Autoprobing 과 DAP 정의 
      http://openocd.org/doc/html/TAP-Declaration.html

    JTAG Reset
      http://openocd.org/doc/html/Reset-Configuration.html

    Open OCD Config example
      https://www.allaboutcircuits.com/technical-articles/getting-started-with-openocd-using-ft2232h-adapter-for-swd-debugging/
      https://rtime.felk.cvut.cz/hw/index.php/FTDI2232_JTAG
      https://gnu-mcu-eclipse.github.io/


    3.5 OpenOCD 실행시 점검사항


    • HW 관련사항 점검사항 
    TMS/TCLK/TDI 는 FT2232에서 나가는 OUTPUT이므로 3.3V로 정확하게 나오며, 각 Clock이 존재하는 지 확인
    TDO 는 Target Board에서 오는 결과 값이므로 이것이 제대로 동작이 안되면, Target Board의 Pin 연결 및 설정을 비롯하여 Board 손상까지 생각해야한다


    • FT2232 HW 문제사항 
    나의 경우 Channel A가 망가져서 TCLK의 전압이 3.3V가 나오지 않고 2.3V로 나와서 오동작하는 경우가 생겼으며, 이유는 Pin을 잘못 꽂은 후 사용하여 손상된것 같다.
    그래서 Channel B로 다시 연결하여 진행하였더니, 잘 동작함


    • OpenOCD 실행시 문제사항 (문제없음)
    Info : Listening on port 6666 for tcl connections
    Info : Listening on port 4444 for telnet connections
    Error: libusb_open() failed with LIBUSB_ERROR_NOT_SUPPORTED  //처음 VCP Open하기 때문에 생기는 에러라고 하며 이후부터 문제없음 
    ....
    


    open_matching_device 함수참조
      http://openocd.zylin.com/gitweb?p=openocd.git;a=blob;f=src/jtag/drivers/mpsse.c;h=7488d9dd877c65a97a8b88057a3299d25001e3de;hb=d214cadfef7ba75c7c52849c7946d32f0ad8b668
      https://mcuoneclipse.com/2019/10/20/jtag-debugging-the-esp32-with-ft2232-and-openocd/

    • OpenOCD 실행시 문제사항-B (Raspberry Pi3 Board 손상) 
    Raspberry Pi의 JTAG 부분의 문제로 생각했으나, Raspberry Pi3 의 손상까지도 추측했으나,
    TRST과 GND 연결 후 아래 문제가해결됨

    //HW의 증상은 TDI/TMS/TCLK을 보면 다 동작을 하지만, TDO의 값은 Level 1만 유지하며 변화가 없음
    Open On-Chip Debugger 0.10.0 (2020-03-10) [https://github.com/sysprogs/openocd]
    Licensed under GNU GPL v2
    libusb1 09e75e98b4d9ea7909e8837b7a3f00dda4589dc3
    For bug reports, read
            http://openocd.org/doc/doxygen/bugs.html
    Info : Hardware thread awareness created
    Info : Hardware thread awareness created
    Info : Hardware thread awareness created
    Info : Hardware thread awareness created
    Info : Listening on port 6666 for tcl connections
    Info : Listening on port 4444 for telnet connections
    Error: libusb_open() failed with LIBUSB_ERROR_NOT_SUPPORTED
    Info : clock speed 500 kHz
    Error: JTAG scan chain interrogation failed: all ones    //Scan chain/Autoprobing 진행하며, TDO가 항상 1인 상태 
    Error: Check JTAG interface, timings, target power, etc.
    Error: Trying to use configured scan chain anyway...
    Error: bcm2837.cpu: IR capture error; saw 0x0f not 0x01   //상위 configu의 jtag newtap의 -ircapture 0x1 와 irmask 참조 
    Warn : Bypassing JTAG setup events due to errors
    Error: Invalid ACK (7) in DAP response
    Error: JTAG-DP STICKY ERROR
    Error: Invalid ACK (7) in DAP response
    Error: JTAG-DP STICKY ERROR
    Error: Invalid ACK (7) in DAP response
    Error: JTAG-DP STICKY ERROR
    Error: Invalid ACK (7) in DAP response
    Error: JTAG-DP STICKY ERROR
    Error: Invalid ACK (7) in DAP response
    Error: JTAG-DP STICKY ERROR
    Error: Invalid ACK (7) in DAP response
    Error: JTAG-DP STICKY ERROR
    Error: Invalid ACK (7) in DAP response
    Error: JTAG-DP STICKY ERROR
    Error: Invalid ACK (7) in DAP response
    Error: JTAG-DP STICKY ERROR
    .......
    

    동일한 증상
    TDO Pin을 GND로 연결하면 all zero 변경되어 나의 JTAG의 올바르게 동작됨을 확인
    1. Error: JTAG scan chain interrogation failed: all ones
    2. Error: JTAG scan chain interrogation failed: all zeros
    다른 PIN들도 동작확인 완료 
      https://github.com/syncsrc/jtagsploitation/issues/3


    • OpenOCD가 Raspberry Pi 연결하여 동작할 경우 
    TRST/TCLK/TMS/TDI/TDO/GND 를 모두 연결 후 동작하면 GDB에서 멈춤

    Open On-Chip Debugger 0.10.0 (2020-03-10) [https://github.com/sysprogs/openocd]
    Licensed under GNU GPL v2
    libusb1 09e75e98b4d9ea7909e8837b7a3f00dda4589dc3
    For bug reports, read
            http://openocd.org/doc/doxygen/bugs.html
    Info : Hardware thread awareness created
    Info : Hardware thread awareness created
    Info : Hardware thread awareness created
    Info : Hardware thread awareness created
    Info : Listening on port 6666 for tcl connections
    Info : Listening on port 4444 for telnet connections
    Error: libusb_open() failed with LIBUSB_ERROR_NOT_SUPPORTED
    Info : clock speed 500 kHz
    Info : JTAG tap: bcm2837.cpu tap/device found: 0x4ba00477 (mfg: 0x23b (ARM Ltd.), part: 0xba00, ver: 0x4)
    Info : bcm2837.cpu.0: hardware has 6 breakpoints, 4 watchpoints
    Info : bcm2837.cpu.1: hardware has 6 breakpoints, 4 watchpoints
    Info : bcm2837.cpu.2: hardware has 6 breakpoints, 4 watchpoints
    Info : bcm2837.cpu.3: hardware has 6 breakpoints, 4 watchpoints
    Info : Listening on port 3333 for gdb connections  
    

    설치된 VS의 arm-none-eabi-gdb 실행 target remote localhost:3333 테스트가능
    C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\Linux\gcc_arm\bin


    3.6 FT232 계의 OpenOCD 사용 

    FT232H도 지원하고 있으며, 더 싼 가격으로 이용하자
      https://www.raspberrypi.org/forums/viewtopic.php?t=243625
      https://github.com/metebalci/baremetal-rpi/blob/master/rpi3.cfg
      https://mobaxterm.mobatek.net/download-home-edition.html

    현재 생각으로 Raspberry Pi3에 FT2232H를이용하여 JTAG으로 연결하고, OpenOCD를 이용할 방법을 찾아봐야겠다.


    4. Visual Studio 와 GDB 연결방법 

    FH2232의 JTAG을 이용하여 OpenOCD와 GDB를 연결 한 후, 최종적으로 GDB기반으로 JTAG을 이용 Debugging 하고 싶어서,아래와 같이 설치를 진행을 해본다.

    • Download Visual Studio Community Version (for Free)
    Visual Studio Community Version(무료 Version) 설치진행
      https://visualstudio.microsoft.com/ko/vs/community/?rr=https%3A%2F%2Fvisualgdb.com%2Fdownload%2F

    상위  Visual Studio Community를 설치를 진행을 하면, 손쉽게 Raspberry Pi를 SSH를 통해 연결이 가능하며,
    내부 Compiler로 빌드 후 gdbserver를 이용하여 손쉽게 debug 실행화면을 볼 수 있다.


    Visual Studio에서 SSH 설정 및 Project Property에서 gdbserver로 변경




    $ sudo apt install gdbserver 
    
    $ ps -aux | grep gdb   //gdbserver 동작확인 
    pi       25709  0.1  0.1   2824  1708 pts/0    S    09:28   0:00 gdbserver :62249 /home/pi/projects/Blink1/bin/ARM/Debug/Blink1.out
    pi       25714  0.0  0.0   4120   568 pts/2    S+   09:30   0:00 grep --color=auto gdb
    


    4.1 Visual Studio 기반의 GDB의 확장기능 


    Window10에서 Visual Studio

    • Download VisualGDB 
    Visual Studio기반으로 GDB확장으로 유료 Package (30일만 무료)
      https://visualgdb.com/
      https://visualgdb.com/tutorials/arm/openocd/build/
      https://visualgdb.com/tutorials/arm/stm32/
      https://visualgdb.com/tutorials/arm/openocd/



    • VisualKernel 사용
      https://sysprogs.com/VisualKernel/tutorials/raspberry/basicmodule/
      https://sysprogs.com/VisualKernel/download/


    5. VS Code 와 OpenOCD

    VS Code는 Visual Studio Code로 크로스 개발환경에서 많이 이용되어지며, 본인도 최근에서 사용하게된 Tool 이다.
    Visual Studio 와 마찬가지로 Microsoft사가 개발했으며, Window or Linux or MacOS 에서 다양 OS를  지원을 해주며, 다양한 확장프로그램을 지원을 해준다.
    미리 설치된 ToolChain 이 존재한다면, Run에 연결하여 하는 OpenOCD와 같이 사용을 하는 것으로 보인다.

    • Window의 VS Code 설치 진행전 해야할 것 
    1. STM의 ToolChain진행 
    2. STM의 LinkV2 Utiliyt 진행 
    3. Window OpenOCD 진행

    • VS Code의 Run의 launch.json 작성 

    launch.json // RUN 실행  아래의 workspaceRoot변경될 것이며, 아래의 Port 3333 OpenOCD연결 
    {
        "name": "GDB",
        "type": "gdb",
        "request": "launch",
        "cwd": "${workspaceRoot}",
        "target": "${workspaceRoot}/.pioenvs/nucleo_f303k8/firmware.elf",
        "gdbpath" : "C:/STM32Toolchain/gcc-arm/5.4 2016q3/bin/arm-none-eabi-gdb.exe",
        "autorun": [
            "target remote localhost:3333",
            "symbol-file ./.pioenvs/nucleo_f303k8/firmware.elf",
            "monitor reset"
            ]
    }





    • VS Code 와 OpenOCD+GDB
     https://www.justinmklam.com/posts/2017/10/vscode-debugger-setup/


    • VS launch.json example
      https://code.visualstudio.com/docs/cpp/launch-json-reference
      https://code.visualstudio.com/docs/editor/debugging

    댓글 없음 :