From 5e8624dae33fb2a08d80eda30311c7bdca36a45c Mon Sep 17 00:00:00 2001
From: Peter Lawrence <12226419+majbthrd@users.noreply.github.com>
Date: Sat, 6 Feb 2021 19:30:11 -0600
Subject: [PATCH] two variants; debugger now runs on Core1
---
DAP_config.h | 22 +++----
README.md | 11 +++-
pico-debug.hzp | 33 ++++++-----
picodebug_MemoryMap.xml | 2 +-
picodebug_Startup.s | 20 ++++---
picodebug_sram_placement.xml | 15 +++++
...acement.xml => picodebug_xip_placement.xml | 13 +++--
spawn.c | 58 +++++++++++++++++++
usb_descriptors.c | 2 +-
9 files changed, 132 insertions(+), 44 deletions(-)
create mode 100644 picodebug_sram_placement.xml
rename picodebug_placement.xml => picodebug_xip_placement.xml (69%)
create mode 100644 spawn.c
diff --git a/DAP_config.h b/DAP_config.h
index f04b40b..0f07ea8 100644
--- a/DAP_config.h
+++ b/DAP_config.h
@@ -225,7 +225,7 @@ __STATIC_INLINE void PORT_SWD_SETUP (void) {
/* enable the peripheral and enable local control of core1's SWD interface */
resets_hw->reset &= ~RESETS_RESET_SYSCFG_BITS;
- syscfg_hw->dbgforce = SYSCFG_DBGFORCE_PROC1_ATTACH_BITS;
+ syscfg_hw->dbgforce = SYSCFG_DBGFORCE_PROC0_ATTACH_BITS;
#if 1
/* 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) {
SWJ_Sequence(8*sizeof(sequence_alert), sequence_alert);
/* it is possible to do this with SWJ_Sequence on the rp2040 since data input and output are distinct */
- static const uint8_t write_targetsel[] = { 0x99, 0xff, 0x24, 0x05, 0x20, 0x22, 0x00, };
+ static const uint8_t write_targetsel[] = { 0x99, 0xff, 0x24, 0x05, 0x20, 0x00, 0x00, };
SWJ_Sequence(8*sizeof(write_targetsel), write_targetsel);
#endif
/* set to default high level */
- syscfg_hw->dbgforce |= SYSCFG_DBGFORCE_PROC1_SWCLK_BITS | SYSCFG_DBGFORCE_PROC1_SWDI_BITS;
+ syscfg_hw->dbgforce |= SYSCFG_DBGFORCE_PROC0_SWCLK_BITS | SYSCFG_DBGFORCE_PROC0_SWDI_BITS;
}
/** Disable JTAG/SWD I/O Pins.
@@ -268,14 +268,14 @@ __STATIC_FORCEINLINE uint32_t PIN_SWCLK_TCK_IN (void) {
Set the SWCLK/TCK DAP hardware I/O pin to high level.
*/
__STATIC_FORCEINLINE void PIN_SWCLK_TCK_SET (void) {
- syscfg_hw->dbgforce |= SYSCFG_DBGFORCE_PROC1_SWCLK_BITS;
+ syscfg_hw->dbgforce |= SYSCFG_DBGFORCE_PROC0_SWCLK_BITS;
}
/** SWCLK/TCK I/O pin: Set Output to Low.
Set the SWCLK/TCK DAP hardware I/O pin to low level.
*/
__STATIC_FORCEINLINE void PIN_SWCLK_TCK_CLR (void) {
- syscfg_hw->dbgforce &= ~SYSCFG_DBGFORCE_PROC1_SWCLK_BITS;
+ syscfg_hw->dbgforce &= ~SYSCFG_DBGFORCE_PROC0_SWCLK_BITS;
}
@@ -294,21 +294,21 @@ __STATIC_FORCEINLINE uint32_t PIN_SWDIO_TMS_IN (void) {
Set the SWDIO/TMS DAP hardware I/O pin to high level.
*/
__STATIC_FORCEINLINE void PIN_SWDIO_TMS_SET (void) {
- syscfg_hw->dbgforce |= SYSCFG_DBGFORCE_PROC1_SWDI_BITS;
+ syscfg_hw->dbgforce |= SYSCFG_DBGFORCE_PROC0_SWDI_BITS;
}
/** SWDIO/TMS I/O pin: Set Output to Low.
Set the SWDIO/TMS DAP hardware I/O pin to low level.
*/
__STATIC_FORCEINLINE void PIN_SWDIO_TMS_CLR (void) {
- syscfg_hw->dbgforce &= ~SYSCFG_DBGFORCE_PROC1_SWDI_BITS;
+ syscfg_hw->dbgforce &= ~SYSCFG_DBGFORCE_PROC0_SWDI_BITS;
}
/** SWDIO I/O pin: Get Input (used in SWD mode only).
\return Current status of the SWDIO DAP hardware I/O pin.
*/
__STATIC_FORCEINLINE uint32_t PIN_SWDIO_IN (void) {
- return (syscfg_hw->dbgforce & SYSCFG_DBGFORCE_PROC1_SWDO_BITS) ? 1U : 0U;
+ return (syscfg_hw->dbgforce & SYSCFG_DBGFORCE_PROC0_SWDO_BITS) ? 1U : 0U;
}
/** SWDIO I/O pin: Set Output (used in SWD mode only).
@@ -316,9 +316,9 @@ __STATIC_FORCEINLINE uint32_t PIN_SWDIO_IN (void) {
*/
__STATIC_FORCEINLINE void PIN_SWDIO_OUT (uint32_t bit) {
if (bit & 1)
- syscfg_hw->dbgforce |= SYSCFG_DBGFORCE_PROC1_SWDI_BITS;
+ syscfg_hw->dbgforce |= SYSCFG_DBGFORCE_PROC0_SWDI_BITS;
else
- syscfg_hw->dbgforce &= ~SYSCFG_DBGFORCE_PROC1_SWDI_BITS;
+ syscfg_hw->dbgforce &= ~SYSCFG_DBGFORCE_PROC0_SWDI_BITS;
}
/** 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
called prior \ref PIN_SWDIO_IN function calls.
*/
__STATIC_FORCEINLINE void PIN_SWDIO_OUT_DISABLE (void) {
- syscfg_hw->dbgforce |= SYSCFG_DBGFORCE_PROC1_SWDI_BITS;
+ syscfg_hw->dbgforce |= SYSCFG_DBGFORCE_PROC0_SWDI_BITS;
}
diff --git a/README.md b/README.md
index d2ee0e0..289b3ce 100644
--- a/README.md
+++ b/README.md
@@ -6,13 +6,18 @@ pico-debug runs on one core in a RP2040 and provides a USB CMSIS-DAP interface t
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.
-*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).
+To cater to different user situations, there are two versions of pico-debug: **MAXRAM** and **GIMMECACHE**
-If viewing this on github, a pre-built binary is available for download on the right under "Releases".
+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).
+
+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.
+
+If viewing this on github, pre-built binaries are available for download on the right under "Releases".
## Caveats whilst using pico-debug
-- the flash cache cannot be used by the user code, as pico-debug is using this memory
+- MAXRAM only: the flash cache cannot be used by the user code, as pico-debug is using this memory
+- GIMMECACHE only: SRAM 0x2003C000 to 0x2003FFFF must not be used by user code
- user code cannot reconfigure the PLL and clocks, as the USB peripheral needs this
- the USB peripheral is used to provide the debugger, so the user code cannot use it as well
diff --git a/pico-debug.hzp b/pico-debug.hzp
index 950ef4c..7a4428e 100644
--- a/pico-debug.hzp
+++ b/pico-debug.hzp
@@ -3,18 +3,20 @@
+ gcc_entry_point="boot_entry"
+ package_dependencies="CMSIS;Cortex_M_Generic" />
+
@@ -44,9 +46,7 @@
-
-
-
+ Name="THUMB Release - XIP"
+ inherited_configurations="THUMB;Release;XIP" />
+ Name="THUMB Release - SRAM"
+ inherited_configurations="THUMB;Release;SRAM" />
+ hidden="Yes"
+ link_time_optimization="Yes" />
+
+
diff --git a/picodebug_MemoryMap.xml b/picodebug_MemoryMap.xml
index 0bd2cd1..7c0bdfb 100644
--- a/picodebug_MemoryMap.xml
+++ b/picodebug_MemoryMap.xml
@@ -1,5 +1,5 @@
-
+
diff --git a/picodebug_Startup.s b/picodebug_Startup.s
index 987f2f0..63f4276 100644
--- a/picodebug_Startup.s
+++ b/picodebug_Startup.s
@@ -42,6 +42,7 @@
.syntax unified
.global boot_entry
.extern main
+ .extern multicore_launch_core1_raw
.global exit
.weak exit
@@ -49,6 +50,7 @@
.thumb_func
boot_entry:
+#ifndef STAY_IN_SRAM
/* disable flash cache (allowing XIP_SRAM access) */
ldr r0, =0x14000000
ldr r1, =0
@@ -70,15 +72,17 @@ copy_loop:
subs r2, r2, #1
bne copy_loop
copy_finished:
+#endif
- ldr r1, =__vectors_start__ /* origin of where vector table now resides */
- ldr r0, =0xE000ED08 /* VTOR register */
- str r1, [r0] /* point VTOR to user app */
- ldr r0, [r1] /* load stack pointer from user app */
- msr msp, r0
- msr psp, r0
- ldr r0, [r1, #4] /* load reset address from user app */
- mov pc, r0
+ ldr r2, =__vectors_start__ /* origin of where vector table now resides */
+ ldr r1, [r2] /* load stack pointer from user app */
+ ldr r0, [r2, #4] /* load reset address from user app */
+ ldr r3, =multicore_launch_core1_raw
+ blx r3
+
+sleep:
+ wfi
+ b sleep
.section .vectors, "ax"
.code 16
diff --git a/picodebug_sram_placement.xml b/picodebug_sram_placement.xml
new file mode 100644
index 0000000..593e5f3
--- /dev/null
+++ b/picodebug_sram_placement.xml
@@ -0,0 +1,15 @@
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
diff --git a/picodebug_placement.xml b/picodebug_xip_placement.xml
similarity index 69%
rename from picodebug_placement.xml
rename to picodebug_xip_placement.xml
index 03d20ec..c52f7c3 100644
--- a/picodebug_placement.xml
+++ b/picodebug_xip_placement.xml
@@ -2,12 +2,13 @@
-
-
-
-
-
-
+
+
+
+
+
+
+
diff --git a/spawn.c b/spawn.c
new file mode 100644
index 0000000..d70a534
--- /dev/null
+++ b/spawn.c
@@ -0,0 +1,58 @@
+/*
+ * Copyright (c) 2020 Raspberry Pi (Trading) Ltd.
+ *
+ * SPDX-License-Identifier: BSD-3-Clause
+ */
+
+#include
+
+static inline bool multicore_fifo_rvalid() {
+ return !!(sio_hw->fifo_st & SIO_FIFO_ST_VLD_BITS);
+}
+
+static inline void multicore_fifo_drain() {
+ while (multicore_fifo_rvalid())
+ (void) sio_hw->fifo_rd;
+}
+
+static inline bool multicore_fifo_wready() {
+ return !!(sio_hw->fifo_st & SIO_FIFO_ST_RDY_BITS);
+}
+
+static inline void multicore_fifo_push_blocking(uint32_t data) {
+ // We wait for the fifo to have some space
+ while (!multicore_fifo_wready())
+ tight_loop_contents();
+
+ sio_hw->fifo_wr = data;
+
+ // Fire off an event to the other core
+ __SEV();
+}
+
+static inline uint32_t multicore_fifo_pop_blocking(void) {
+ // If nothing there yet, we wait for an event first,
+ // to try and avoid too much busy waiting
+ while (!multicore_fifo_rvalid())
+ __WFE();
+
+ return sio_hw->fifo_rd;
+}
+
+__attribute__ (( section(".bootc") )) void multicore_launch_core1_raw(void (*entry)(void), uint32_t *sp, uint32_t vector_table) {
+ uint32_t cmd_sequence[] = {0, 0, 1, (uintptr_t) vector_table, (uintptr_t) sp, (uintptr_t) entry};
+
+ uint seq = 0;
+ do {
+ uint cmd = cmd_sequence[seq];
+ // we drain before sending a 0
+ if (!cmd) {
+ multicore_fifo_drain();
+ __SEV(); // core 1 may be waiting for fifo space
+ }
+ multicore_fifo_push_blocking(cmd);
+ uint32_t response = multicore_fifo_pop_blocking();
+ // move to next state on correct response otherwise start over
+ seq = cmd == response ? seq + 1 : 0;
+ } while (seq < (sizeof(cmd_sequence) / sizeof(*cmd_sequence)));
+}
diff --git a/usb_descriptors.c b/usb_descriptors.c
index e769d5c..19e6b47 100644
--- a/usb_descriptors.c
+++ b/usb_descriptors.c
@@ -23,7 +23,7 @@ tusb_desc_device_t const desc_device =
/* using Dapper Miser CMSIS-DAP VID:PID */
.idVendor = 0x1209,
.idProduct = 0x2488,
- .bcdDevice = 0x1000,
+ .bcdDevice = 0x1001,
.iManufacturer = 0,
.iProduct = STRID_PRODUCT,