“Barebones” (no SDK) blinky sketch for the #RP2040

This post is a follow-up to yesterday’s attempt to get a blinky sketch running for the Raspberry Pico, without using the SDK.

If you haven’t read the previous post, then do so now.

What has changed is the linker script, the bootup assembly and the “pad” setup in the main file. In the end, I don’t think it was the linker or assembly that was the problem, but a lack of proper peripheral setup.

I managed to get things working because I cribbed from Jeremy Grosser’s pico_examples repo. He was writing for Ada, though.

I took his linker script and his crt0.s file. They are now “fuller fat” implementations. The linker now includes proper subsections for zero’d data, the heap, etc.. the crt0.s file now includes all the vectors for the interrupts, together with stuff for ensuring that the cores are set up properly; and more besides.

I’m not inclined to delve too deeply into it at the moment, though. The file seems to have been obtained by using the C preprocessor on the SDK. It’s fairly compact.

I did extract the boot2 code into a separate file: boot2.s. I used “word” layout instead of “byte”, which makes it easier to compare to dumps from other builds. As I said in the previous post, it is likely that the boot code is specific to the Pico. If you’re using a variant, then you should dive into Jeremy’s repo to find something suitable.

I did notice that there was a Rust repo elsewhere that actually coded a bootloader for themselves. I assume it works. The problem with bootloaders is that they have to be checksummed, so there’s some fiddling around getting that sorted.

In the end, I decided I’d just use the one generated by the SDK. I’ve used the generated bytes. I’m contented enough to leave it as “mystery meat”, because it makes things simpler. I don’t think there’s any great mileage to be had rolling your own. Maybe it’s worth having an overview of what’s going on. Maybe.

The real fix to yesterday’s code is setting up the banks. I added the following code to the start of main.c:

        // inspired by Ada. Appears to be necessary, too.
        RESETS_RESET &= ~(1ul << 5); // clear IO_BANK0
        RESETS_RESET &= ~(1ul << 8); // clear PAD_BANK0
        while(1) {
                int io_bank0_done = (RESETS_RESET_DONE & (1ul<<5)) >0;
                int pad_bank0_done = (RESETS_RESET_DONE & (1ul<<8)) >0;
                if(io_bank0_done && pad_bank0_done) break;
        PADS_BANK0_GPIO25 &= ~(1<<7); // clear output disable 
        PADS_BANK0_GPIO25 &= ~(1<<6); // clear input enable

This resets the IO and the PAD, and waits for them to come back up again. It seems to be a detail that I missed last time. Hmmm, I’m a little sceptical that it’s necessary, but it appears to be.

Rust seems to have similar code in their bootloader, and I thought that this would have already been set up by the SDK bootloader. Apparently not. I can’t help but get the sneaking suspicion that I’m still doing something wrong. It’s probably better to reset all (or most) of the banks and pad at the beginning of main(), but I’ll leave it as-is for now. Sometimes one is just grateful for getting the wretched thing working in the first place.

The good news is that not only does it seem to work in debug mode (fingers crossed), but it also works when I transfer the UF2 over to the Pico. Just type “make app.uf2” to generate the uf2 file. You’re going to need elf2uf2 for that. Fortunately, it is build by the SDK. If you’ve set your SDK up in a standard way, the Makefile should be able to find it.

Type “make flash” to flash the uf2 file over to the mcu, after you’ve put it in USB boot mode, of course. I’m using Debian Stable, which adds the Pico as /media/$(USER)/RPI-RP2. You may need to tweak it to suit your system.

So, good stuff. It was a lot of work and, as always, I’ve done a lot of deconstructing of other people’s work. Without them, I’d be rather lost. Hopefully what I’ve given you is a simple skeletal structure from which you can build on. It’s rather compact and written in C. These are huge advantages, I think, as it allows one to see what’s going on without a huge amount of fuss.


About mcturra2000

Computer programmer living in Scotland.
This entry was posted in Uncategorized. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s