Skip to content

Add simple UART bootloader example #571

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 5 commits into
base: develop
Choose a base branch
from

Conversation

will-v-pi
Copy link
Contributor

Add a UART bootloader example, which reads a binary from the partition table on the main device, and uses the UART bootloader to boot that binary on a separate device. Requires a modified separate board to run, as you need access to the QSPI pins to use them as UARTs for the bootloader (see 5.8 in the datasheet) - the main board can be a normal RP2350 board.

This sample binary flashes the LED and prints "Hello, world" back over the UART interface, which demonstrates setting up QSPI GPIOs as UARTs.

@will-v-pi will-v-pi added this to the 2.0.1 milestone Nov 12, 2024
Comment on lines 10 to 11
#define UART_ID uart1
#define BAUD_RATE 1000000
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The baud-rate is hardcoded in uart_binary.c (because I assume that it can't be changed, as it's the baud-rate required by the bootrom) so should it also be hardcoded (rather than being a #define) in uart_boot.c too?

Although I guess that the uart-bootloader baud rate could theoretically be different to the post-boot application baud rate (IYSWIM); so I wonder if having them deliberately different might make this a more interesting example? 🤔

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, it should be hardcoded in both - I've fixed that

If the post-boot application used a different baud rate, then it wouldn't be able to boot the board again if it's power cycled - this way you can power cycle the separate device and watch the bootloader run again without rebooting the main device

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, I was assuming that in order to accommodate my suggestion, the code (in the main device) would need to dynamically reconfigure the baud rate at the start and end of the uart-boot sequence 🙂

@lurch
Copy link
Contributor

lurch commented Nov 14, 2024

I wonder if things like the "splash string", the "knock sequence", the "uart-bootloader baudrate", etc. ought to be defined in a header-file somewhere in pico-sdk? (As I assume they'll always be the same for every RP2350 chip?)

EDIT: Ah, perhaps https://github.com/raspberrypi/pico-sdk/blob/develop/src/rp2_common/boot_bootrom_headers/include/boot/bootrom_constants.h would be a good place for those 🙂

reset_chip();
return;
}
current_addr += 32;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Or I guess you could do += i 😉

printf("Splash found\n");
uart_boot();
} else {
ptr = memchr(buf, hello[0], sizeof(buf));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hah, I guess it's important that hello[0] doesn't appear anywhere in the splash string 😉

Is there any reason not to have this check for the full "Hello, world\n" that uart_binary transmits?

Comment on lines +190 to +191
char splash[] = "RP2350";
char hello[] = "Hello";
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

minor nitpick: I guess these two variables could be moved outside of the while (true) loop?

#include "hardware/structs/io_qspi.h"

#ifndef LED_DELAY_MS
#define LED_DELAY_MS 500
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

From what I understand, I think the "parent" RP2350 will reset the "child" RP2350 if it doesn't receive the string "Hello" once per second; so I guess that means that LED_DELAY_MS can't be larger than 500?

@peterharperuk
Copy link
Contributor

This worked well for me once I'd figured out that the uart binary had to be loaded into the "master" device first. It would be more fun if the binary could be built into a header file to avoid that step

# add partition table
pico_embed_pt_in_binary(uart_boot ${CMAKE_CURRENT_LIST_DIR}/uart-pt.json)

# create absolute UF2
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

possible worth a quick why comment

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added - it's because it's a bootloader, so shouldn't go in a partition

kilograham
kilograham previously approved these changes Mar 24, 2025
@kilograham kilograham self-requested a review March 24, 2025 23:02
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants