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