Skip to content

Commit afc79e5

Browse files
committed
loader: fixups(nano_connect): add double tap reset
if the board is reset twice within 500ms enter USB bootloader (BOOTSEL) mode Signed-off-by: Gilberto Conti <g.conti@arduino.cc>
1 parent 6446710 commit afc79e5

File tree

1 file changed

+46
-0
lines changed

1 file changed

+46
-0
lines changed

loader/fixups.c

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,52 @@ SYS_INIT(disable_bootloader_mpu, PRE_KERNEL_1, CONFIG_KERNEL_INIT_PRIORITY_DEFAU
4747
SYS_INIT(disable_mpu_rasr_xn, PRE_KERNEL_1, CONFIG_KERNEL_INIT_PRIORITY_DEFAULT);
4848
#endif
4949

50+
#if defined(CONFIG_BOARD_ARDUINO_NANO_RP2040_CONNECT)
51+
#include <zephyr/kernel.h>
52+
#include <zephyr/drivers/gpio.h>
53+
#include <pico/bootrom.h>
54+
55+
/*
56+
* Double-tap reset detection: if the board is reset twice within 500ms,
57+
* enter USB bootloader (BOOTSEL) mode. This mirrors the original
58+
* ArduinoCore-mbed NANO_RP2040_CONNECT behavior.
59+
*
60+
* A magic token is stored in uninitialized RAM (.noinit), which survives
61+
* a warm reset but is lost on power cycle. On boot:
62+
* - If the token is present: a second reset happened quickly, so we
63+
* clear the token and enter USB boot mode (never returns).
64+
* - If not: write the token, wait 500ms, then clear it and boot normally.
65+
*/
66+
static const uint32_t magic_token[] = {
67+
0xf01681de, 0xbd729b29, 0xd359be7a,
68+
};
69+
70+
static uint32_t magic_location[3] __attribute__((section(".noinit.double_tap")));
71+
72+
#define NANO_RP2040_LED_PIN 6
73+
74+
int double_tap_check(void) {
75+
if (magic_location[0] == magic_token[0] &&
76+
magic_location[1] == magic_token[1] &&
77+
magic_location[2] == magic_token[2]) {
78+
magic_location[0] = 0;
79+
reset_usb_boot(1 << NANO_RP2040_LED_PIN, 0);
80+
/* never returns */
81+
}
82+
83+
for (int i = 0; i < 3; i++) {
84+
magic_location[i] = magic_token[i];
85+
}
86+
87+
k_busy_wait(500000);
88+
89+
magic_location[0] = 0;
90+
return 0;
91+
}
92+
93+
SYS_INIT(double_tap_check, POST_KERNEL, 0);
94+
#endif
95+
5096
#if defined(CONFIG_SOC_STM32H747XX_M7)
5197
int enable_bkp_access(void) {
5298
/* Enable access to the backup domain */

0 commit comments

Comments
 (0)