Skip to content
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

Add simple UART bootloader example #571

Open
wants to merge 4 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.

The sample binary flashes the LED and prints Hello, world back over the UART interface
@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 🙂

bootloaders/uart/uart_boot.c Outdated Show resolved Hide resolved
@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 🙂

bootloaders/uart/uart_boot.c Outdated Show resolved Hide resolved
bootloaders/uart/uart_boot.c Outdated Show resolved Hide resolved
bootloaders/uart/uart_boot.c Outdated Show resolved Hide resolved
const int buf_words = (16 * 4) + 1; // maximum of 16 partitions, each with maximum of 4 words returned, plus 1
uint32_t* buffer = malloc(buf_words * 4);

int ret = rom_get_partition_table_info(buffer, buf_words, PT_INFO_PARTITION_LOCATION_AND_FLAGS | PT_INFO_SINGLE_PARTITION | (0 << 24));
Copy link
Contributor

@lurch lurch Nov 14, 2024

Choose a reason for hiding this comment

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

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I'm not sure what's gone wrong, but it looks like none of the RP2350 functions are included in that documentation - the doxygen is all there, but maybe the #if !PICO_RP2040 halfway down the src/rp2_common/pico_bootrom/include/pico/bootrom.h file has something to do with it?

Copy link
Contributor

Choose a reason for hiding this comment

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

I'll have a fiddle later... rom_get_partition_table_info isn't appearing in https://datasheets.raspberrypi.com/pico/raspberry-pi-pico-c-sdk.pdf either! 😱


int ret = rom_get_partition_table_info(buffer, buf_words, PT_INFO_PARTITION_LOCATION_AND_FLAGS | PT_INFO_SINGLE_PARTITION | (0 << 24));
assert(buffer[0] == (PT_INFO_PARTITION_LOCATION_AND_FLAGS | PT_INFO_SINGLE_PARTITION));
assert(ret == 3);
Copy link
Contributor

Choose a reason for hiding this comment

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

What does the magic value 3 indicate? 😉

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This one isn't actually a magic value - it's just the number of words written in the buffer (which should be 3 here - 1 word for the returned flags, and 2 words for the partition_location_and_flags)

Copy link
Contributor

Choose a reason for hiding this comment

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

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Copy link
Contributor

Choose a reason for hiding this comment

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

Oh, I missed that 😆 My eyes just jumped straight to the bottom of the doxygen description looking for the \return ... line 😜

bootloaders/uart/uart_boot.c Outdated Show resolved Hide resolved
"partitions": [
{
"start": "128K",
"size": "32K",
Copy link
Contributor

Choose a reason for hiding this comment

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

If this is intended as a "generic UART bootloader", should we increase the size of this partition to allow for bigger UF2s?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The issue is that this simple bootloader just loads the whole partition onto the other device, so the larger the partition the longer it takes. From a quick play it takes 0.35s for this 32kiB partition, vs 5.6s for a full 512kiB partition (which would be the maximum size, as that fills the RP2350 SRAM)

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.

2 participants