Skip to content

Commit 6f227f4

Browse files
committed
arm: rpi_pico: Add boot mode bootloader support
Add early init function for RP2 that checks the retained boot mode and jumps to the bootloader if requested. Signed-off-by: Peter Johanson <[email protected]>
1 parent 9f66c9f commit 6f227f4

File tree

6 files changed

+55
-15
lines changed

6 files changed

+55
-15
lines changed

modules/hal_rpi_pico/CMakeLists.txt

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,6 @@ if(CONFIG_HAS_RPI_PICO)
5656
${rp2_common_dir}/hardware_xosc/xosc.c
5757
${rp2_common_dir}/hardware_watchdog/watchdog.c
5858
${rp2_common_dir}/pico_platform/platform.c
59-
${rp2_common_dir}/pico_bootrom/bootrom.c
6059
)
6160

6261
zephyr_include_directories(
@@ -69,12 +68,10 @@ if(CONFIG_HAS_RPI_PICO)
6968
${rp2_common_dir}/hardware_sync/include
7069
${rp2_common_dir}/hardware_timer/include
7170
${rp2_common_dir}/hardware_resets/include
72-
${rp2_common_dir}/pico_bootrom/include
7371
${rp2040_dir}/hardware_regs/include
7472
${rp2040_dir}/hardware_structs/include
7573
${common_dir}/pico_base/include
7674
${rp2_common_dir}/pico_platform/include
77-
${rp2_common_dir}/pico_bootrom/include
7875
${CMAKE_CURRENT_LIST_DIR}
7976
)
8077

@@ -116,6 +113,11 @@ if(CONFIG_HAS_RPI_PICO)
116113
zephyr_include_directories_ifdef(CONFIG_PICOSDK_USE_CLAIM
117114
${rp2_common_dir}/hardware_claim/include)
118115

116+
zephyr_library_sources_ifdef(CONFIG_PICOSDK_USE_PICO_BOOTROM
117+
${rp2_common_dir}/pico_bootrom/bootrom.c)
118+
zephyr_include_directories_ifdef(CONFIG_PICOSDK_USE_PICO_BOOTROM
119+
${rp2_common_dir}/pico_bootrom/include)
120+
119121
# Some flash driver functions must be executed from the RAM.
120122
# Originally pico-sdk places them in the RW data section, so this
121123
# implementation does the same.

modules/hal_rpi_pico/Kconfig

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,3 +49,8 @@ config PICOSDK_USE_TIMER
4949
bool
5050
help
5151
Use the TIMER driver from pico-sdk
52+
53+
config PICOSDK_USE_PICO_BOOTROM
54+
bool
55+
help
56+
Use the pico bootrom library from pico-sdk

soc/arm/rpi_pico/rp2/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,4 @@
44
zephyr_library()
55

66
zephyr_library_sources(soc.c)
7+
zephyr_library_sources_ifdef(CONFIG_RP2_BOOTLOADER bootloader.c)

soc/arm/rpi_pico/rp2/Kconfig.soc

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,13 @@ config RP2_REQUIRES_SECOND_STAGE_BOOT
1717
bool
1818
default y if FLASH_LOAD_OFFSET = 0x100
1919

20+
21+
config RP2_BOOTLOADER
22+
bool
23+
default y
24+
depends on RETENTION_BOOT_MODE
25+
select PICOSDK_USE_PICO_BOOTROM
26+
2027
# Flash type used by the SoC. The board should select the one used.
2128

2229
config RP2_FLASH_W25Q080

soc/arm/rpi_pico/rp2/bootloader.c

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/*
2+
* Copyright (c) 2024 Pete Johanson
3+
*
4+
* SPDX-License-Identifier: Apache-2.0
5+
*/
6+
7+
#include <soc.h>
8+
#include <zephyr/init.h>
9+
#include <zephyr/retention/retention.h>
10+
#include <zephyr/retention/bootmode.h>
11+
#include <pico/bootrom.h>
12+
13+
static FUNC_NORETURN void jump_to_bootloader(void)
14+
{
15+
uint32_t boot_led_mask = 0;
16+
17+
#if DT_HAS_CHOSEN(zephyr_rpi_pico_boot_led)
18+
boot_led_mask = BIT(DT_GPIO_PIN_BY_IDX(DT_CHOSEN(zephyr_rpi_pico_boot_led), gpios, 0));
19+
#endif
20+
21+
reset_usb_boot(boot_led_mask,0);
22+
23+
while (1) { }
24+
}
25+
26+
static int rp2_bootloader_check_boot_init(void)
27+
{
28+
if (bootmode_check(BOOT_MODE_TYPE_BOOTLOADER) > 0) {
29+
bootmode_clear();
30+
jump_to_bootloader();
31+
}
32+
33+
return 0;
34+
}
35+
36+
SYS_INIT(rp2_bootloader_check_boot_init, PRE_KERNEL_2, 0);
37+

soc/arm/rpi_pico/rp2/soc.c

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -25,21 +25,9 @@
2525
#include <hardware/regs/resets.h>
2626
#include <hardware/clocks.h>
2727
#include <hardware/resets.h>
28-
#include <pico/bootrom.h>
2928

3029
LOG_MODULE_REGISTER(soc, CONFIG_SOC_LOG_LEVEL);
3130

32-
/* Overrides the weak ARM implementation:
33-
Set general purpose retention register and reboot */
34-
void sys_arch_reboot(int type)
35-
{
36-
if (type != 0) {
37-
reset_usb_boot(0,0);
38-
} else {
39-
NVIC_SystemReset();
40-
}
41-
}
42-
4331
static int rp2040_init(void)
4432
{
4533
reset_block(~(RESETS_RESET_IO_QSPI_BITS | RESETS_RESET_PADS_QSPI_BITS |

0 commit comments

Comments
 (0)