Skip to content
This repository was archived by the owner on Dec 11, 2022. It is now read-only.

Commit 5e8624d

Browse files
committed
two variants; debugger now runs on Core1
1 parent d604948 commit 5e8624d

9 files changed

+132
-44
lines changed

DAP_config.h

+11-11
Original file line numberDiff line numberDiff line change
@@ -225,7 +225,7 @@ __STATIC_INLINE void PORT_SWD_SETUP (void) {
225225

226226
/* enable the peripheral and enable local control of core1's SWD interface */
227227
resets_hw->reset &= ~RESETS_RESET_SYSCFG_BITS;
228-
syscfg_hw->dbgforce = SYSCFG_DBGFORCE_PROC1_ATTACH_BITS;
228+
syscfg_hw->dbgforce = SYSCFG_DBGFORCE_PROC0_ATTACH_BITS;
229229

230230
#if 1
231231
/* this #if block is a temporary measure to perform target selection even if the host IDE doesn't know how */
@@ -238,12 +238,12 @@ __STATIC_INLINE void PORT_SWD_SETUP (void) {
238238
SWJ_Sequence(8*sizeof(sequence_alert), sequence_alert);
239239

240240
/* it is possible to do this with SWJ_Sequence on the rp2040 since data input and output are distinct */
241-
static const uint8_t write_targetsel[] = { 0x99, 0xff, 0x24, 0x05, 0x20, 0x22, 0x00, };
241+
static const uint8_t write_targetsel[] = { 0x99, 0xff, 0x24, 0x05, 0x20, 0x00, 0x00, };
242242
SWJ_Sequence(8*sizeof(write_targetsel), write_targetsel);
243243
#endif
244244

245245
/* set to default high level */
246-
syscfg_hw->dbgforce |= SYSCFG_DBGFORCE_PROC1_SWCLK_BITS | SYSCFG_DBGFORCE_PROC1_SWDI_BITS;
246+
syscfg_hw->dbgforce |= SYSCFG_DBGFORCE_PROC0_SWCLK_BITS | SYSCFG_DBGFORCE_PROC0_SWDI_BITS;
247247
}
248248

249249
/** Disable JTAG/SWD I/O Pins.
@@ -268,14 +268,14 @@ __STATIC_FORCEINLINE uint32_t PIN_SWCLK_TCK_IN (void) {
268268
Set the SWCLK/TCK DAP hardware I/O pin to high level.
269269
*/
270270
__STATIC_FORCEINLINE void PIN_SWCLK_TCK_SET (void) {
271-
syscfg_hw->dbgforce |= SYSCFG_DBGFORCE_PROC1_SWCLK_BITS;
271+
syscfg_hw->dbgforce |= SYSCFG_DBGFORCE_PROC0_SWCLK_BITS;
272272
}
273273

274274
/** SWCLK/TCK I/O pin: Set Output to Low.
275275
Set the SWCLK/TCK DAP hardware I/O pin to low level.
276276
*/
277277
__STATIC_FORCEINLINE void PIN_SWCLK_TCK_CLR (void) {
278-
syscfg_hw->dbgforce &= ~SYSCFG_DBGFORCE_PROC1_SWCLK_BITS;
278+
syscfg_hw->dbgforce &= ~SYSCFG_DBGFORCE_PROC0_SWCLK_BITS;
279279
}
280280

281281

@@ -294,31 +294,31 @@ __STATIC_FORCEINLINE uint32_t PIN_SWDIO_TMS_IN (void) {
294294
Set the SWDIO/TMS DAP hardware I/O pin to high level.
295295
*/
296296
__STATIC_FORCEINLINE void PIN_SWDIO_TMS_SET (void) {
297-
syscfg_hw->dbgforce |= SYSCFG_DBGFORCE_PROC1_SWDI_BITS;
297+
syscfg_hw->dbgforce |= SYSCFG_DBGFORCE_PROC0_SWDI_BITS;
298298
}
299299

300300
/** SWDIO/TMS I/O pin: Set Output to Low.
301301
Set the SWDIO/TMS DAP hardware I/O pin to low level.
302302
*/
303303
__STATIC_FORCEINLINE void PIN_SWDIO_TMS_CLR (void) {
304-
syscfg_hw->dbgforce &= ~SYSCFG_DBGFORCE_PROC1_SWDI_BITS;
304+
syscfg_hw->dbgforce &= ~SYSCFG_DBGFORCE_PROC0_SWDI_BITS;
305305
}
306306

307307
/** SWDIO I/O pin: Get Input (used in SWD mode only).
308308
\return Current status of the SWDIO DAP hardware I/O pin.
309309
*/
310310
__STATIC_FORCEINLINE uint32_t PIN_SWDIO_IN (void) {
311-
return (syscfg_hw->dbgforce & SYSCFG_DBGFORCE_PROC1_SWDO_BITS) ? 1U : 0U;
311+
return (syscfg_hw->dbgforce & SYSCFG_DBGFORCE_PROC0_SWDO_BITS) ? 1U : 0U;
312312
}
313313

314314
/** SWDIO I/O pin: Set Output (used in SWD mode only).
315315
\param bit Output value for the SWDIO DAP hardware I/O pin.
316316
*/
317317
__STATIC_FORCEINLINE void PIN_SWDIO_OUT (uint32_t bit) {
318318
if (bit & 1)
319-
syscfg_hw->dbgforce |= SYSCFG_DBGFORCE_PROC1_SWDI_BITS;
319+
syscfg_hw->dbgforce |= SYSCFG_DBGFORCE_PROC0_SWDI_BITS;
320320
else
321-
syscfg_hw->dbgforce &= ~SYSCFG_DBGFORCE_PROC1_SWDI_BITS;
321+
syscfg_hw->dbgforce &= ~SYSCFG_DBGFORCE_PROC0_SWDI_BITS;
322322
}
323323

324324
/** SWDIO I/O pin: Switch to Output mode (used in SWD mode only).
@@ -334,7 +334,7 @@ Configure the SWDIO DAP hardware I/O pin to input mode. This function is
334334
called prior \ref PIN_SWDIO_IN function calls.
335335
*/
336336
__STATIC_FORCEINLINE void PIN_SWDIO_OUT_DISABLE (void) {
337-
syscfg_hw->dbgforce |= SYSCFG_DBGFORCE_PROC1_SWDI_BITS;
337+
syscfg_hw->dbgforce |= SYSCFG_DBGFORCE_PROC0_SWDI_BITS;
338338
}
339339

340340

README.md

+8-3
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,18 @@ pico-debug runs on one core in a RP2040 and provides a USB CMSIS-DAP interface t
66

77
Boot the RP2040 with the BOOTSEL button pressed, copy over pico-debug.uf2, and it immediately reboots as a CMSIS-DAP adapter. pico-debug loads as a RAM only .uf2 image, meaning that it is never written to flash and doesn't replace existing user code.
88

9-
*All* 264kBytes of SRAM on the RP2040 is available for running user code; pico-debug shoehorns itself entirely into the 16kBytes of XIP_SRAM (aka flash cache).
9+
To cater to different user situations, there are two versions of pico-debug: **MAXRAM** and **GIMMECACHE**
1010

11-
If viewing this on github, a pre-built binary is available for download on the right under "Releases".
11+
With **pico-debug-maxram**, *all* 264kBytes of SRAM on the RP2040 is available for running user code; pico-debug shoehorns itself entirely into the 16kBytes of XIP_SRAM (aka flash cache).
12+
13+
With **pico-debug-gimmecache**, 248kBytes (94% of total) of SRAM is available for running user code; pico-debug gives plenty of elbow room by occupying only 6% near the very top of SRAM, and unlike MAXRAM, leaves the flash cache operational.
14+
15+
If viewing this on github, pre-built binaries are available for download on the right under "Releases".
1216

1317
## Caveats whilst using pico-debug
1418

15-
- the flash cache cannot be used by the user code, as pico-debug is using this memory
19+
- MAXRAM only: the flash cache cannot be used by the user code, as pico-debug is using this memory
20+
- GIMMECACHE only: SRAM 0x2003C000 to 0x2003FFFF must not be used by user code
1621
- user code cannot reconfigure the PLL and clocks, as the USB peripheral needs this
1722
- the USB peripheral is used to provide the debugger, so the user code cannot use it as well
1823

pico-debug.hzp

+19-14
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,20 @@
33
<project Name="pico-debug">
44
<configuration
55
Name="Common"
6+
batch_build_configurations="THUMB Release - SRAM;THUMB Release - XIP"
67
build_intermediate_directory="./_build/$(ProjectName) $(Configuration)"
78
build_output_directory="./_build/$(ProjectName) $(Configuration)"
89
c_preprocessor_definitions=""
9-
package_dependencies="CMSIS;Cortex_M_Generic"
1010
c_user_include_directories=".;./rp2040/;$(PackagesDir)/CMSIS_5/CMSIS/Core/Include;./CMSIS_5/CMSIS/DAP/Firmware/Include"
11-
gcc_entry_point="boot_entry" />
11+
gcc_entry_point="boot_entry"
12+
package_dependencies="CMSIS;Cortex_M_Generic" />
1213
<folder Name="Source Files">
1314
<configuration Name="Common" filter="c;cpp;cxx;cc;h;s;asm;inc" />
1415
<file file_name="./main.c" />
1516
<file file_name="./usb_descriptors.c" />
1617
<file file_name="myboard.c" />
1718
<file file_name="clock_setup.c" />
19+
<file file_name="spawn.c" />
1820
</folder>
1921
<folder Name="hal">
2022
<file file_name="$(TOP)/hw/bsp/board.c" />
@@ -44,9 +46,7 @@
4446
</folder>
4547
<folder Name="System Files">
4648
<file file_name="picodebug_Startup.s" />
47-
<file file_name="picodebug_placement.xml" />
4849
</folder>
49-
<configuration Name="Release" link_time_optimization="Yes" />
5050
</project>
5151
<configuration
5252
Name="Common"
@@ -72,7 +72,6 @@
7272
property_groups_file_path="$(TargetsDir)/Cortex_M/propertyGroups.xml"
7373
target_reset_script="Reset()"
7474
target_script_file="$(TargetsDir)/Cortex_M/Cortex_M_Target.js" />
75-
<configuration Name="THUMB Debug" inherited_configurations="THUMB;Debug" />
7675
<configuration
7776
Name="THUMB"
7877
Platform="ARM"
@@ -81,20 +80,26 @@
8180
c_preprocessor_definitions="__THUMB"
8281
hidden="Yes" />
8382
<configuration
84-
Name="Debug"
85-
c_preprocessor_definitions="DEBUG"
86-
gcc_debugging_level="Level 3"
87-
gcc_omit_frame_pointer="Yes"
88-
gcc_optimization_level="None"
89-
hidden="Yes" />
83+
Name="THUMB Release - XIP"
84+
inherited_configurations="THUMB;Release;XIP" />
9085
<configuration
91-
Name="THUMB Release"
92-
inherited_configurations="THUMB;Release" />
86+
Name="THUMB Release - SRAM"
87+
inherited_configurations="THUMB;Release;SRAM" />
9388
<configuration
9489
Name="Release"
9590
c_preprocessor_definitions="NDEBUG"
9691
gcc_debugging_level="Level 3"
9792
gcc_omit_frame_pointer="Yes"
9893
gcc_optimization_level="Level 1"
99-
hidden="Yes" />
94+
hidden="Yes"
95+
link_time_optimization="Yes" />
96+
<configuration
97+
Name="XIP"
98+
hidden="Yes"
99+
linker_section_placement_file="picodebug_xip_placement.xml" />
100+
<configuration
101+
Name="SRAM"
102+
c_preprocessor_definitions="STAY_IN_SRAM"
103+
hidden="Yes"
104+
linker_section_placement_file="picodebug_sram_placement.xml" />
100105
</solution>

picodebug_MemoryMap.xml

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<!DOCTYPE Board_Memory_Definition_File>
22
<Root name="Cortex-M0">
3-
<MemorySegment start="0x20030000" size="0x4100" access="Read/Write" name="SRAM" />
3+
<MemorySegment start="0x2003BF00" size="0x4100" access="Read/Write" name="SRAM" />
44
<MemorySegment start="0x15000000" size="0x4000" access="Read/Write" name="XIP_SRAM" />
55
</Root>

picodebug_Startup.s

+12-8
Original file line numberDiff line numberDiff line change
@@ -42,13 +42,15 @@
4242
.syntax unified
4343
.global boot_entry
4444
.extern main
45+
.extern multicore_launch_core1_raw
4546
.global exit
4647
.weak exit
4748

4849
.section .boot, "ax"
4950
.thumb_func
5051

5152
boot_entry:
53+
#ifndef STAY_IN_SRAM
5254
/* disable flash cache (allowing XIP_SRAM access) */
5355
ldr r0, =0x14000000
5456
ldr r1, =0
@@ -70,15 +72,17 @@ copy_loop:
7072
subs r2, r2, #1
7173
bne copy_loop
7274
copy_finished:
75+
#endif
7376

74-
ldr r1, =__vectors_start__ /* origin of where vector table now resides */
75-
ldr r0, =0xE000ED08 /* VTOR register */
76-
str r1, [r0] /* point VTOR to user app */
77-
ldr r0, [r1] /* load stack pointer from user app */
78-
msr msp, r0
79-
msr psp, r0
80-
ldr r0, [r1, #4] /* load reset address from user app */
81-
mov pc, r0
77+
ldr r2, =__vectors_start__ /* origin of where vector table now resides */
78+
ldr r1, [r2] /* load stack pointer from user app */
79+
ldr r0, [r2, #4] /* load reset address from user app */
80+
ldr r3, =multicore_launch_core1_raw
81+
blx r3
82+
83+
sleep:
84+
wfi
85+
b sleep
8286

8387
.section .vectors, "ax"
8488
.code 16

picodebug_sram_placement.xml

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<!DOCTYPE Linker_Placement_File>
2+
<Root name="picodebug Section Placement">
3+
<MemorySegment name="SRAM">
4+
<ProgramSection alignment="4" load="Yes" name=".boot" />
5+
<ProgramSection alignment="4" load="Yes" name=".bootc" />
6+
<ProgramSection alignment="0x100" load="Yes" name=".vectors" />
7+
<ProgramSection alignment="4" load="Yes" name=".text" />
8+
<ProgramSection alignment="4" load="Yes" name=".data" />
9+
<ProgramSection alignment="4" load="Yes" name=".rodata" />
10+
<ProgramSection alignment="4" load="No" name=".bss" />
11+
<ProgramSection alignment="8" size="__STACKSIZE__" load="No" name=".stack" />
12+
</MemorySegment>
13+
<MemorySegment name="XIP_SRAM">
14+
</MemorySegment>
15+
</Root>

picodebug_placement.xml picodebug_xip_placement.xml

+7-6
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,13 @@
22
<Root name="picodebug Section Placement">
33
<MemorySegment name="SRAM">
44
<ProgramSection alignment="4" load="Yes" name=".boot" />
5-
<ProgramSection alignment="0x100" load="Yes" runoffset="0x15000000-0x20030100" name=".vectors" />
6-
<ProgramSection alignment="4" load="Yes" runoffset="0x15000000-0x20030100" name=".text" />
7-
<ProgramSection alignment="4" load="Yes" runoffset="0x15000000-0x20030100" name=".data" />
8-
<ProgramSection alignment="4" load="Yes" runoffset="0x15000000-0x20030100" name=".rodata" />
9-
<ProgramSection alignment="4" load="No" runoffset="0x15000000-0x20030100" name=".bss" />
10-
<ProgramSection alignment="8" size="__STACKSIZE__" load="No" runoffset="0x15000000-0x20030100" name=".stack" />
5+
<ProgramSection alignment="4" load="Yes" name=".bootc" />
6+
<ProgramSection alignment="0x100" load="Yes" runoffset="0x15000000-0x2003C000" name=".vectors" />
7+
<ProgramSection alignment="4" load="Yes" runoffset="0x15000000-0x2003C000" name=".text" />
8+
<ProgramSection alignment="4" load="Yes" runoffset="0x15000000-0x2003C000" name=".data" />
9+
<ProgramSection alignment="4" load="Yes" runoffset="0x15000000-0x2003C000" name=".rodata" />
10+
<ProgramSection alignment="4" load="No" runoffset="0x15000000-0x2003C000" name=".bss" />
11+
<ProgramSection alignment="8" size="__STACKSIZE__" load="No" runoffset="0x15000000-0x2003C000" name=".stack" />
1112
</MemorySegment>
1213
<MemorySegment name="XIP_SRAM">
1314
</MemorySegment>

spawn.c

+58
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
/*
2+
* Copyright (c) 2020 Raspberry Pi (Trading) Ltd.
3+
*
4+
* SPDX-License-Identifier: BSD-3-Clause
5+
*/
6+
7+
#include <rp2040.h>
8+
9+
static inline bool multicore_fifo_rvalid() {
10+
return !!(sio_hw->fifo_st & SIO_FIFO_ST_VLD_BITS);
11+
}
12+
13+
static inline void multicore_fifo_drain() {
14+
while (multicore_fifo_rvalid())
15+
(void) sio_hw->fifo_rd;
16+
}
17+
18+
static inline bool multicore_fifo_wready() {
19+
return !!(sio_hw->fifo_st & SIO_FIFO_ST_RDY_BITS);
20+
}
21+
22+
static inline void multicore_fifo_push_blocking(uint32_t data) {
23+
// We wait for the fifo to have some space
24+
while (!multicore_fifo_wready())
25+
tight_loop_contents();
26+
27+
sio_hw->fifo_wr = data;
28+
29+
// Fire off an event to the other core
30+
__SEV();
31+
}
32+
33+
static inline uint32_t multicore_fifo_pop_blocking(void) {
34+
// If nothing there yet, we wait for an event first,
35+
// to try and avoid too much busy waiting
36+
while (!multicore_fifo_rvalid())
37+
__WFE();
38+
39+
return sio_hw->fifo_rd;
40+
}
41+
42+
__attribute__ (( section(".bootc") )) void multicore_launch_core1_raw(void (*entry)(void), uint32_t *sp, uint32_t vector_table) {
43+
uint32_t cmd_sequence[] = {0, 0, 1, (uintptr_t) vector_table, (uintptr_t) sp, (uintptr_t) entry};
44+
45+
uint seq = 0;
46+
do {
47+
uint cmd = cmd_sequence[seq];
48+
// we drain before sending a 0
49+
if (!cmd) {
50+
multicore_fifo_drain();
51+
__SEV(); // core 1 may be waiting for fifo space
52+
}
53+
multicore_fifo_push_blocking(cmd);
54+
uint32_t response = multicore_fifo_pop_blocking();
55+
// move to next state on correct response otherwise start over
56+
seq = cmd == response ? seq + 1 : 0;
57+
} while (seq < (sizeof(cmd_sequence) / sizeof(*cmd_sequence)));
58+
}

usb_descriptors.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ tusb_desc_device_t const desc_device =
2323
/* using Dapper Miser CMSIS-DAP VID:PID */
2424
.idVendor = 0x1209,
2525
.idProduct = 0x2488,
26-
.bcdDevice = 0x1000,
26+
.bcdDevice = 0x1001,
2727

2828
.iManufacturer = 0,
2929
.iProduct = STRID_PRODUCT,

0 commit comments

Comments
 (0)