ARM cortex-M4 jtag 读取 dp 和 ap 寄存器 实例 解读

  • 实验资料
https://www.linmingjie.cn/index.php/archives/253/
DDI0413C_cortexm1_r0p1_trm.pdf // https://developer.arm.com/documentation/ddi0413/c/Babeagge
stm32f407zg 开发板 // 随意一个 这样的卡发版即可
dsview 套装(逻辑分析仪)
jlink for windows 软件
jlink (usb转jtag)


因为 没有合适的资料,所以选取了 m1 的 trm 
	DDI0413C_cortexm1_r0p1_trm 中 有详细的 DAP 描述

读者可以 以 cortex-M1 开发板 为基准做资料


  • 实验重点
在 Jlink.exe 命令行中 运行 https://www.linmingjie.cn/index.php/archives/253/ 中的 三条命令
writedp 2 0 
writeap 1 20000000
readap 3  
并 抓 jtag 时序(非swd),并加以解析


# 写DP,选AP[0] Bank[0]                                           
J-Link>writedp 2 0                                              
Writing DP register 2 = 0x00000000 (0 write repetitions needed) 
# 写AP[0] BANK[0] Address寄存器
J-Link>writeap 1 20000000                                       
Writing AP register 1 = 0x20000000 (0 write repetitions needed) 
# 读操作代表读memory, 可以看到结果已经变化
J-Link>readap 3                                                 
Reading AP register 3 = 0x12345678 (0 read repetitions needed)  

1. 准备工作 环境搭建 和 建立软件链接

  • 安装 jlink for windows
  • 连接物理设备
  • 建立软件链接
    在这里插入图片描述
SEGGER J-Link Commander V7.22b (Compiled Jun 17 2021 17:23:58)
DLL version V7.22b, compiled Jun 17 2021 17:22:49

Connecting to J-Link via USB...O.K.
Firmware: J-Link V9 compiled May  7 2021 16:26:12
Hardware version: V9.20
S/N: -1
VTref=3.285V


Type "connect" to establish a target connection, '?' for help
J-Link>connect
Please specify device / core. <Default>: STM32F407VG
Type '?' for selection dialog
Device>
Please specify target interface:
  J) JTAG (Default)
  S) SWD
  T) cJTAG
TIF>
Device position in JTAG chain (IRPre,DRPre) <Default>: -1,-1 => Auto-detect
JTAGConf>
Specify target interface speed [kHz]. <Default>: 4000 kHz
Speed>
Device "STM32F407VG" selected.


Connecting to target via JTAG
InitTarget() start
Can not attach to CPU. Trying connect under reset.
InitTarget() end
TotalIRLen = 9, IRPrint = 0x0011
JTAG chain detection found 2 devices:
 #0 Id: 0x4BA00477, IRLen: 04, CoreSight JTAG-DP
 #1 Id: 0x06413041, IRLen: 05, STM32 Boundary Scan
DPv0 detected
Scanning AP map to find all available APs
AP[1]: Stopped AP scan as end of AP map has been reached
AP[0]: AHB-AP (IDR: 0x24770011)
Iterating through AP map to find AHB-AP to use
AP[0]: Core found
AP[0]: AHB-AP ROM base: 0xE00FF000
CPUID register: 0x410FC241. Implementer code: 0x41 (ARM)
Found Cortex-M4 r0p1, Little endian.
FPUnit: 6 code (BP) slots and 2 literal slots
CoreSight components:
ROMTbl[0] @ E00FF000
ROMTbl[0][0]: E000E000, CID: B105E00D, PID: 000BB00C SCS-M7
ROMTbl[0][1]: E0001000, CID: B105E00D, PID: 003BB002 DWT
ROMTbl[0][2]: E0002000, CID: B105E00D, PID: 002BB003 FPB
ROMTbl[0][3]: E0000000, CID: B105E00D, PID: 003BB001 ITM
ROMTbl[0][4]: E0040000, CID: B105900D, PID: 000BB9A1 TPIU
ROMTbl[0][5]: E0041000, CID: B105900D, PID: 000BB925 ETM
Cortex-M4 identified.
J-Link>
  • 写地址 20000000 为 12345678
    在这里插入图片描述
  • 读地址 20000000 , 以验证写入正确
    在这里插入图片描述

实验开始

第1步
J-Link>writedp 2 0
Writing DP register 2 = 0x00000000 (0 write repetitions needed)
// 作用 : 选中 第0个AP中的第0个bank// 从0开始计数
// 参数分析
// writedp : 会产生 IR ,0b1010 , 选中 DPACC
// 2 : 选中 Select Register  // P151 In the case of a DPACC access, to read a debug port register, by the value shifted into A[3:2].
// 0 : 写入 Select Register 中的值(包括选择哪个AP和选择哪个bank)

jtag 时序

以 DDI0413C_cortexm1_r0p1_trm.pdf 分析 , 之后的 Pxx ,都是该书中的页码
分析
1. IR TDI 传入 0x1FA
	P145 : On debug logic reset, IDCODE becomes the current instruction
	所以,此时 必须 走 IR 链 选中 DPACC
	传入 0b1010 (同时TDO 0b0001) 选中 DPACC // 但是 写入的是 9bit(0x1FA),高5位无效
2. DR TDI 传入  0x04
	P149 : DPACC
	P151 : RnW为0 ,表示写DATAIN[31:0]到某个寄存器 //If RnW is shifted in as 0, the request is to write the value in DATAIN[31:0] to the addressed register.
	P151 : A[3:2]即TDI[2:1](值为0b10)选中寄存器P185 "AP Select Register", In the case of a DPACC access, to read a debug port register, by the value shifted into A[3:2].See JTAG-DP register map on page 9-53 for the addressing details
		// 注意 : DP 只有一个,AP可以有多个
	P185 : AP Select Register 的 Address[3:2]0b10
	
	此时标志要 写 DATAIN[31:0]即 TDI[34:3]进入 AP Select Register. // 注意 : 此次TDI传输了36bit, 高1位无效
	根据 P195 "AP Select Register" 的定义 , APSEL 为0 ,APBANKSEL为0 , 即第0个AP,0个bank
	注意 : M0 中只有一个 AP(即AHB-AP) , M4 也只有一个 AP(即AHB-AP) , APSEL 域 只能填入0
第2步
J-Link>writeap 1 20000000
Writing AP register 1 = 0x20000000 (0 write repetitions needed)
// 作用 : 之前已经选中"某AP的某bank",此时写入 20000000 到该bank 中的 第1(从0开始计数)个寄存器
// 参数分析
// writeap : 会产生 IR ,0b1011 , 选中 APACC
// 1 : 选中 Transfer Address Register  // P151 In the case of a APACC access, to read an access port register, by the combination of: the value shifted into A[3:2]
// 0 : 写入 Transfer Address Register 中的值(要读的地址)

jtag 时序

以 DDI0413C_cortexm1_r0p1_trm.pdf 分析 , 之后的 Pxx ,都是该书中的页码
分析
1. IR TDI 传入 0x1FB
	之前已经选中APACC,而现在要操作 APACC 组的寄存器
	所以,此时 必须 走 IR 链 选中 APACC
	传入 0b1011 (同时TDO 0b0001) 选中 APACC // 但是 写入的是 9bit(0x1FB),高5位无效
2. DR TDI 传入 0x1 0000 0002 // LSB
	P149 : APACC
	P151 : RnW为0 ,表示写DATAIN[31:0]到某个寄存器 //If RnW is shifted in as 0, the request is to write the value in DATAIN[31:0] to the addressed register.
	P151 : 
		1. P151 : In the case of a APACC access, to read an access port register, by the combination of:the current value of the SELECT register in the DP, see AP Select Register,SELECT on page 9-63.
		  知道了当前选中的 AP 那哪个(上一条命令决定了是 AHB-AP)
		2. 根据 AP是哪个 A[3:2]即TDI[2:1](值为0b01) 选中寄存器P201 "Transfer Address, TAR",In the case of a APACC access, to read an access port register, by the combination of: the value shifted into A[3:2]
	P201 : Transfer Address, TAR 的 offset[3:2]0b01
	
	此时标志要 写 DATAIN[31:0]即 TDI[34:3]进入 "Transfer Address, TAR" // 注意 : 此次TDI传输了36bit, 高1位无效
	根据 P203 "Transfer Address, TAR" 的定义 ,Address of the current transfer 为填充为 0x2000 0000 // 注意 (0x1 0000 0002 是 LSB)
	
	写入后,会产生一次transter ,"Transfer Address, TAR" 中的值为地址, 读该地址中的内容到 该AP的 "Data Read/Write, DRW"寄存器
第3步
J-Link>readap 3
Reading AP register 3 = 0x12345678 (0 read repetitions needed)

// 作用 : 之前已经选中"某AP的某bank",此时读 该bank 中的 第3(从0开始计数)个寄存器
// 参数分析
// writeap : 会产生 IR ,0b1011 , 选中 APACC
// 3 : 选中 "Data Read/Write, DRW"

jtag 时序

以 DDI0413C_cortexm1_r0p1_trm.pdf 分析 , 之后的 Pxx ,都是该书中的页码
分析
1. IR TDI 传入 0x1FB
	之前已经选中APACC,而现在要操作 APACC 组的寄存器
	所以,此时 必须 走 IR 链 选中 APACC
	传入 0b1011 (同时TDO 0b0001) 选中 APACC // 但是 写入的是 9bit(0x1FB),高5位无效
2. DR TDI 传入 0x7 // LSB
	P149 : APACC
	P151 : RnW为1 ,表示读某个寄存器 
		//If RnW is shifted in as 1, the request is to read the value of the addressed register. 
		// The value in DATAIN[31:0] is ignored. 
		// You must read the scan chain again to obtain the value read from the register.
	P151 : 
		1. P151 : In the case of a APACC access, to read an access port register, by the combination of:the current value of the SELECT register in the DP, see AP Select Register,SELECT on page 9-63.
		  知道了当前选中的 AP 那哪个(上一条命令决定了是 AHB-AP)
		2. 根据 AP是哪个 A[3:2]即TDI[2:1](值为0b11) 选中寄存器P201 "Data Read/Write, DRW",In the case of a APACC access, to read an access port register, by the combination of: the value shifted into A[3:2]
	P201 : Data Read/Write, DRW 的 offset[3:2]0b11
	
	此时标志要 读 P204 "Data Read/Write, DRW"
		注意 : 之前讲述的
			1. The value in DATAIN[31:0] is ignored.
			2. You must read the scan chain again to obtain the value read from the register.
			TODO : 要读哪个寄存器? 猜测是 DPACC 中的 "Read Buffer"

3. DR TDI 传入 0x1FA // LSB
	选中 DPACC
4. DR TDI 传入 0x7 , TDO 传出 0x91A2B3C2 // 实际传送了 36bit , 最高为无效
	RnW 为1 ,表示读
	TDI[2:1]0b11 , 对应 P185 中的 "Read Buffer"
	TDI[34:3] 被忽略
	
	TDO的[34:3] 是 value
	TDO[3] 是 第1个字节的最低位
	0x91A2B3C2:000010010001101000101011001111000010
			  :{
    
    0} 0001 0010 0011 0100 0101 0110 0111 100[0] 010 // [0] 为 TDO[3] , {0} 为无效位
			  :    1    2    3    4    5    6    7    8

Supongo que te gusta

Origin blog.csdn.net/u011011827/article/details/121190906
Recomendado
Clasificación