Skip to content

Commit b2eaca9

Browse files
authored
Merge pull request #32 from maxiwoj/tm/jtag-low-level
Tm/jtag
2 parents 31378f0 + c09959b commit b2eaca9

File tree

16 files changed

+1640
-7
lines changed

16 files changed

+1640
-7
lines changed

Inc/adiv5/adiv5.h

Lines changed: 157 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,157 @@
1+
/*
2+
* This file is part of the Black Magic Debug project.
3+
*
4+
* Copyright (C) 2011 Black Sphere Technologies Ltd.
5+
* Written by Gareth McMullin <[email protected]>
6+
*
7+
* This program is free software: you can redistribute it and/or modify
8+
* it under the terms of the GNU General Public License as published by
9+
* the Free Software Foundation, either version 3 of the License, or
10+
* (at your option) any later version.
11+
*
12+
* This program is distributed in the hope that it will be useful,
13+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
14+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15+
* GNU General Public License for more details.
16+
*
17+
* You should have received a copy of the GNU General Public License
18+
* along with this program. If not, see <http://www.gnu.org/licenses/>.
19+
*/
20+
21+
#ifndef __ADIV5_H
22+
#define __ADIV5_H
23+
24+
#include "stm32f4xx_it.h"
25+
/* ADIv5 DP Register addresses */
26+
#define ADIV5_DP_IDCODE 0x0
27+
#define ADIV5_DP_ABORT 0x0
28+
#define ADIV5_DP_CTRLSTAT 0x4
29+
#define ADIV5_DP_SELECT 0x8
30+
#define ADIV5_DP_RDBUFF 0xC
31+
32+
/* AP Abort Register (ABORT) */
33+
/* Bits 31:5 - Reserved */
34+
#define ADIV5_DP_ABORT_ORUNERRCLR (1 << 4)
35+
#define ADIV5_DP_ABORT_WDERRCLR (1 << 3)
36+
#define ADIV5_DP_ABORT_STKERRCLR (1 << 2)
37+
#define ADIV5_DP_ABORT_STKCMPCLR (1 << 1)
38+
/* Bits 5:1 - SW-DP only, reserved in JTAG-DP */
39+
#define ADIV5_DP_ABORT_DAPABORT (1 << 0)
40+
41+
/* Control/Status Register (CTRLSTAT) */
42+
#define ADIV5_DP_CTRLSTAT_CSYSPWRUPACK (1u << 31)
43+
#define ADIV5_DP_CTRLSTAT_CSYSPWRUPREQ (1u << 30)
44+
#define ADIV5_DP_CTRLSTAT_CDBGPWRUPACK (1u << 29)
45+
#define ADIV5_DP_CTRLSTAT_CDBGPWRUPREQ (1u << 28)
46+
#define ADIV5_DP_CTRLSTAT_CDBGRSTACK (1u << 27)
47+
#define ADIV5_DP_CTRLSTAT_CDBGRSTREQ (1u << 26)
48+
/* Bits 25:24 - Reserved */
49+
/* Bits 23:12 - TRNCNT */
50+
#define ADIV5_DP_CTRLSTAT_TRNCNT
51+
/* Bits 11:8 - MASKLANE */
52+
#define ADIV5_DP_CTRLSTAT_MASKLANE
53+
/* Bits 7:6 - Reserved in JTAG-DP */
54+
#define ADIV5_DP_CTRLSTAT_WDATAERR (1u << 7)
55+
#define ADIV5_DP_CTRLSTAT_READOK (1u << 6)
56+
#define ADIV5_DP_CTRLSTAT_STICKYERR (1u << 5)
57+
#define ADIV5_DP_CTRLSTAT_STICKYCMP (1u << 4)
58+
#define ADIV5_DP_CTRLSTAT_TRNMODE_MASK (3u << 2)
59+
#define ADIV5_DP_CTRLSTAT_STICKYORUN (1u << 1)
60+
#define ADIV5_DP_CTRLSTAT_ORUNDETECT (1u << 0)
61+
62+
63+
/* ADIv5 MEM-AP Registers */
64+
#define ADIV5_AP_CSW 0x00
65+
#define ADIV5_AP_TAR 0x04
66+
/* 0x08 - Reserved */
67+
#define ADIV5_AP_DRW 0x0C
68+
#define ADIV5_AP_DB(x) (0x10 + (4*(x)))
69+
/* 0x20:0xF0 - Reserved */
70+
#define ADIV5_AP_CFG 0xF4
71+
#define ADIV5_AP_BASE 0xF8
72+
#define ADIV5_AP_IDR 0xFC
73+
74+
/* AP Control and Status Word (CSW) */
75+
#define ADIV5_AP_CSW_DBGSWENABLE (1u << 31)
76+
/* Bits 30:24 - Prot, Implementation defined, for Cortex-M3: */
77+
#define ADIV5_AP_CSW_MASTERTYPE_DEBUG (1u << 29)
78+
#define ADIV5_AP_CSW_HPROT1 (1u << 25)
79+
#define ADIV5_AP_CSW_SPIDEN (1u << 23)
80+
/* Bits 22:12 - Reserved */
81+
/* Bits 11:8 - Mode, must be zero */
82+
#define ADIV5_AP_CSW_TRINPROG (1u << 7)
83+
#define ADIV5_AP_CSW_DEVICEEN (1u << 6)
84+
#define ADIV5_AP_CSW_ADDRINC_NONE (0u << 4)
85+
#define ADIV5_AP_CSW_ADDRINC_SINGLE (1u << 4)
86+
#define ADIV5_AP_CSW_ADDRINC_PACKED (2u << 4)
87+
#define ADIV5_AP_CSW_ADDRINC_MASK (3u << 4)
88+
/* Bit 3 - Reserved */
89+
#define ADIV5_AP_CSW_SIZE_BYTE (0u << 0)
90+
#define ADIV5_AP_CSW_SIZE_HALFWORD (1u << 0)
91+
#define ADIV5_AP_CSW_SIZE_WORD (2u << 0)
92+
#define ADIV5_AP_CSW_SIZE_MASK (7u << 0)
93+
94+
/* Constants to make RnW and APnDP parameters more clear in code */
95+
#define ADIV5_LOW_WRITE 0
96+
#define ADIV5_LOW_READ 1
97+
#define ADIV5_LOW_DP 0
98+
#define ADIV5_LOW_AP 1
99+
100+
101+
typedef struct ADIv5_DP_s ADIv5_DP_t;
102+
103+
/* low level functions to access ADIv5_DP */
104+
typedef struct ADIv5_DP_OPS_s {
105+
106+
void (*dp_write)(void *priv, uint8_t addr, uint32_t value);
107+
uint32_t (*dp_read)(void *priv, uint8_t addr);
108+
109+
uint32_t (*error)(void *priv);
110+
111+
uint32_t (*low_access)(void *priv, uint8_t APnDP, uint8_t RnW,
112+
uint8_t addr, uint32_t value);
113+
114+
void (*priv_free)(ADIv5_DP_t *this);
115+
} ADIv5_DP_OPS_t;
116+
117+
typedef struct ADIv5_DP_s {
118+
uint16_t ap_count;
119+
ADIv5_DP_OPS_t *ops;
120+
void *priv;
121+
} ADIv5_DP_t;
122+
123+
124+
/* Structure describing AP */
125+
126+
typedef struct ADIv5_AP_s ADIv5_AP_t;
127+
128+
typedef struct ADIv5_AP_PRIV_s {
129+
ADIv5_DP_t *dp;
130+
131+
uint8_t apsel;
132+
133+
uint32_t idr;
134+
uint32_t cfg;
135+
uint32_t base;
136+
uint32_t csw;
137+
138+
} ADIv5_AP_PRIV_t;
139+
140+
typedef struct ADIv5_AP_OPS_s {
141+
uint32_t (*mem_read_word)(ADIv5_AP_PRIV_t *ap, uint32_t addr);
142+
void (*mem_write_word)(ADIv5_AP_PRIV_t *ap, uint32_t addr, uint32_t value);
143+
uint32_t (*mem_read_words)(ADIv5_AP_PRIV_t *ap, uint32_t *dest, uint32_t src, uint32_t len);
144+
uint32_t (*mem_write_words)(ADIv5_AP_PRIV_t *ap, uint32_t dest, const uint32_t *src, uint32_t len);
145+
uint32_t (*error_check)(ADIv5_AP_PRIV_t *ap);
146+
147+
void (*priv_free)(ADIv5_AP_t *this);
148+
} ADIv5_AP_OPS_t;
149+
150+
typedef struct ADIv5_AP_s {
151+
ADIv5_AP_OPS_t *ops;
152+
ADIv5_AP_PRIV_t *priv;
153+
} ADIv5_AP_t;
154+
155+
uint16_t adiv5_init(ADIv5_DP_t *dp_low_level);
156+
157+
#endif //__ADIV5_H

Inc/adiv5/adiv5_jtag.h

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
#ifndef __ADIV5_JTAG_H
2+
#define __ADIV5_JTAG_H
3+
4+
#include "adiv5/adiv5.h"
5+
6+
typedef struct ADIv5_DP_JTAG_s {
7+
int jtag_dev_num;
8+
} ADIv5_DP_JTAG_t;
9+
10+
int adiv5_jtag_handler(int dev_num);
11+
12+
#endif //__ADIV5_JTAG_H

Inc/cortexm/cortexm.h

Lines changed: 172 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,172 @@
1+
#ifndef __CORTEXM_H
2+
#define __CORTEXM_H
3+
4+
#include "stm32f4xx_it.h"
5+
6+
/* target options recognised by the Cortex-M target */
7+
#define TOPT_FLAVOUR_V6M (1<<0) /* if not set, target is assumed to be v7m */
8+
#define TOPT_FLAVOUR_V7MF (1<<1) /* if set, floating-point enabled. */
9+
10+
/* Private peripheral bus base address */
11+
#define CORTEXM_PPB_BASE 0xE0000000
12+
13+
#define CORTEXM_SCS_BASE (CORTEXM_PPB_BASE + 0xE000)
14+
15+
#define CORTEXM_AIRCR (CORTEXM_SCS_BASE + 0xD0C)
16+
#define CORTEXM_CFSR (CORTEXM_SCS_BASE + 0xD28)
17+
#define CORTEXM_HFSR (CORTEXM_SCS_BASE + 0xD2C)
18+
#define CORTEXM_DFSR (CORTEXM_SCS_BASE + 0xD30)
19+
#define CORTEXM_CPACR (CORTEXM_SCS_BASE + 0xD88)
20+
#define CORTEXM_DHCSR (CORTEXM_SCS_BASE + 0xDF0)
21+
#define CORTEXM_DCRSR (CORTEXM_SCS_BASE + 0xDF4)
22+
#define CORTEXM_DCRDR (CORTEXM_SCS_BASE + 0xDF8)
23+
#define CORTEXM_DEMCR (CORTEXM_SCS_BASE + 0xDFC)
24+
25+
#define CORTEXM_FPB_BASE (CORTEXM_PPB_BASE + 0x2000)
26+
27+
/* ARM Literature uses FP_*, we use CORTEXM_FPB_* consistently */
28+
#define CORTEXM_FPB_CTRL (CORTEXM_FPB_BASE + 0x000)
29+
#define CORTEXM_FPB_REMAP (CORTEXM_FPB_BASE + 0x004)
30+
#define CORTEXM_FPB_COMP(i) (CORTEXM_FPB_BASE + 0x008 + (4*(i)))
31+
32+
#define CORTEXM_DWT_BASE (CORTEXM_PPB_BASE + 0x1000)
33+
34+
#define CORTEXM_DWT_CTRL (CORTEXM_DWT_BASE + 0x000)
35+
#define CORTEXM_DWT_COMP(i) (CORTEXM_DWT_BASE + 0x020 + (0x10*(i)))
36+
#define CORTEXM_DWT_MASK(i) (CORTEXM_DWT_BASE + 0x024 + (0x10*(i)))
37+
#define CORTEXM_DWT_FUNC(i) (CORTEXM_DWT_BASE + 0x028 + (0x10*(i)))
38+
39+
/* Application Interrupt and Reset Control Register (AIRCR) */
40+
#define CORTEXM_AIRCR_VECTKEY (0x05FA << 16)
41+
/* Bits 31:16 - Read as VECTKETSTAT, 0xFA05 */
42+
#define CORTEXM_AIRCR_ENDIANESS (1 << 15)
43+
/* Bits 15:11 - Unused, reserved */
44+
#define CORTEXM_AIRCR_PRIGROUP (7 << 8)
45+
/* Bits 7:3 - Unused, reserved */
46+
#define CORTEXM_AIRCR_SYSRESETREQ (1 << 2)
47+
#define CORTEXM_AIRCR_VECTCLRACTIVE (1 << 1)
48+
#define CORTEXM_AIRCR_VECTRESET (1 << 0)
49+
50+
/* HardFault Status Register (HFSR) */
51+
#define CORTEXM_HFSR_DEBUGEVT (1 << 31)
52+
#define CORTEXM_HFSR_FORCED (1 << 30)
53+
/* Bits 29:2 - Not specified */
54+
#define CORTEXM_HFSR_VECTTBL (1 << 1)
55+
/* Bits 0 - Reserved */
56+
57+
/* Debug Fault Status Register (DFSR) */
58+
/* Bits 31:5 - Reserved */
59+
#define CORTEXM_DFSR_RESETALL 0x1F
60+
#define CORTEXM_DFSR_EXTERNAL (1 << 4)
61+
#define CORTEXM_DFSR_VCATCH (1 << 3)
62+
#define CORTEXM_DFSR_DWTTRAP (1 << 2)
63+
#define CORTEXM_DFSR_BKPT (1 << 1)
64+
#define CORTEXM_DFSR_HALTED (1 << 0)
65+
66+
/* Debug Halting Control and Status Register (DHCSR) */
67+
/* This key must be written to bits 31:16 for write to take effect */
68+
#define CORTEXM_DHCSR_DBGKEY 0xA05F0000
69+
/* Bits 31:26 - Reserved */
70+
#define CORTEXM_DHCSR_S_RESET_ST (1 << 25)
71+
#define CORTEXM_DHCSR_S_RETIRE_ST (1 << 24)
72+
/* Bits 23:20 - Reserved */
73+
#define CORTEXM_DHCSR_S_LOCKUP (1 << 19)
74+
#define CORTEXM_DHCSR_S_SLEEP (1 << 18)
75+
#define CORTEXM_DHCSR_S_HALT (1 << 17)
76+
#define CORTEXM_DHCSR_S_REGRDY (1 << 16)
77+
/* Bits 15:6 - Reserved */
78+
#define CORTEXM_DHCSR_C_SNAPSTALL (1 << 5) /* v7m only */
79+
/* Bit 4 - Reserved */
80+
#define CORTEXM_DHCSR_C_MASKINTS (1 << 3)
81+
#define CORTEXM_DHCSR_C_STEP (1 << 2)
82+
#define CORTEXM_DHCSR_C_HALT (1 << 1)
83+
#define CORTEXM_DHCSR_C_DEBUGEN (1 << 0)
84+
85+
/* Debug Core Register Selector Register (DCRSR) */
86+
#define CORTEXM_DCRSR_REGWnR 0x00010000
87+
#define CORTEXM_DCRSR_REGSEL_MASK 0x0000001F
88+
#define CORTEXM_DCRSR_REGSEL_XPSR 0x00000010
89+
#define CORTEXM_DCRSR_REGSEL_MSP 0x00000011
90+
#define CORTEXM_DCRSR_REGSEL_PSP 0x00000012
91+
92+
/* Debug Exception and Monitor Control Register (DEMCR) */
93+
/* Bits 31:25 - Reserved */
94+
#define CORTEXM_DEMCR_TRCENA (1 << 24)
95+
/* Bits 23:20 - Reserved */
96+
#define CORTEXM_DEMCR_MON_REQ (1 << 19) /* v7m only */
97+
#define CORTEXM_DEMCR_MON_STEP (1 << 18) /* v7m only */
98+
#define CORTEXM_DEMCR_VC_MON_PEND (1 << 17) /* v7m only */
99+
#define CORTEXM_DEMCR_VC_MON_EN (1 << 16) /* v7m only */
100+
/* Bits 15:11 - Reserved */
101+
#define CORTEXM_DEMCR_VC_HARDERR (1 << 10)
102+
#define CORTEXM_DEMCR_VC_INTERR (1 << 9) /* v7m only */
103+
#define CORTEXM_DEMCR_VC_BUSERR (1 << 8) /* v7m only */
104+
#define CORTEXM_DEMCR_VC_STATERR (1 << 7) /* v7m only */
105+
#define CORTEXM_DEMCR_VC_CHKERR (1 << 6) /* v7m only */
106+
#define CORTEXM_DEMCR_VC_NOCPERR (1 << 5) /* v7m only */
107+
#define CORTEXM_DEMCR_VC_MMERR (1 << 4) /* v7m only */
108+
/* Bits 3:1 - Reserved */
109+
#define CORTEXM_DEMCR_VC_CORERESET (1 << 0)
110+
111+
/* Flash Patch and Breakpoint Control Register (FP_CTRL) */
112+
/* Bits 32:15 - Reserved */
113+
/* Bits 14:12 - NUM_CODE2 */ /* v7m only */
114+
/* Bits 11:8 - NUM_LIT */ /* v7m only */
115+
/* Bits 7:4 - NUM_CODE1 */
116+
/* Bits 3:2 - Unspecified */
117+
#define CORTEXM_FPB_CTRL_KEY (1 << 1)
118+
#define CORTEXM_FPB_CTRL_ENABLE (1 << 0)
119+
120+
/* Data Watchpoint and Trace Mask Register (DWT_MASKx) */
121+
#define CORTEXM_DWT_MASK_BYTE (0 << 0)
122+
#define CORTEXM_DWT_MASK_HALFWORD (1 << 0)
123+
#define CORTEXM_DWT_MASK_WORD (3 << 0)
124+
125+
/* Data Watchpoint and Trace Function Register (DWT_FUNCTIONx) */
126+
#define CORTEXM_DWT_FUNC_MATCHED (1 << 24)
127+
#define CORTEXM_DWT_FUNC_DATAVSIZE_WORD (2 << 10) /* v7m only */
128+
#define CORTEXM_DWT_FUNC_FUNC_READ (5 << 0)
129+
#define CORTEXM_DWT_FUNC_FUNC_WRITE (6 << 0)
130+
#define CORTEXM_DWT_FUNC_FUNC_ACCESS (7 << 0)
131+
132+
/* Signals returned by cortexm_halt_wait() */
133+
#define SIGINT 2
134+
#define SIGTRAP 5
135+
#define SIGSEGV 11
136+
137+
138+
typedef struct ADIv5_AP_s ADIv5_AP_t;
139+
typedef struct CORTEXM_s CORTEXM_t;
140+
141+
typedef struct CORTEXM_PRIV_s
142+
{
143+
ADIv5_AP_t *ap;
144+
} CORTEXM_PRIV_t;
145+
146+
typedef struct CORTEXM_OPS_s
147+
{
148+
uint32_t (*read_word)(CORTEXM_PRIV_t *priv, uint32_t addr);
149+
void (*write_word)(CORTEXM_PRIV_t *priv, uint32_t addr, uint32_t value);
150+
uint32_t (*read_words)(CORTEXM_PRIV_t *priv, uint32_t *dest, uint32_t src, uint32_t len);
151+
uint32_t (*write_words)(CORTEXM_PRIV_t *priv, uint32_t dest, const uint32_t *src, uint32_t len);
152+
uint32_t (*pc_read)(CORTEXM_PRIV_t *priv);
153+
void (*pc_write)(CORTEXM_PRIV_t *priv, uint32_t val);
154+
void (*halt_request)(CORTEXM_PRIV_t *priv);
155+
uint32_t (*halt_wait)(CORTEXM_PRIV_t *priv);
156+
void (*halt_resume)(CORTEXM_PRIV_t *priv);
157+
uint32_t (*check_error)(CORTEXM_PRIV_t *priv);
158+
void (*restart)(CORTEXM_PRIV_t *priv);
159+
160+
void (*free)(CORTEXM_t *cortexm);
161+
} CORTEXM_OPS_t;
162+
163+
164+
typedef struct CORTEXM_s
165+
{
166+
CORTEXM_OPS_t *ops;
167+
CORTEXM_PRIV_t *priv;
168+
} CORTEXM_t;
169+
170+
int probe_cortexm(ADIv5_AP_t *ap);
171+
172+
#endif //__CORTEXM_H

0 commit comments

Comments
 (0)