Part 2 - Hello World

Today we are going to cover the basic setup for creating our own projects on the Raspberry Pi Pico.

Inside of our pico folder lets create a 0x02_pico_hello_world folder alongside of the pico-sdk and pico-example folders.

  1. mkdir 0x02_pico_hello_world
  2. cd 0x02_pico_hello_world

Let’s create our vim 0x02_hello_world.c file.

  1. vim 0x02_hello_world.c

Let’s add the following.

  1. #include <stdio.h>
  2. #include "pico/stdlib.h"
  3. int main()
  4. {
  5. stdio_init_all();
  6. while(1)
  7. {
  8. printf("Hello world!\n");
  9. sleep_ms(1000);
  10. }
  11. return 0;
  12. }

We first handle the logic to init all standard input and output.

  1. stdio_init_all();

Finally we print “Hello world!” every 1 second to the standard output in an infinite loop.

  1. while(1)
  2. {
  3. printf("Hello world!\n");
  4. sleep_ms(1000);
  5. }

We then upon success return 0 to indicate success as our main function is an int. It is not technically required but good practice.

  1. return 0;

Working with cmake significantly helps in the build process for our projects. We first need to make a CMakeLists.txt file.

  1. cmake_minimum_required(VERSION 3.13)
  2. include(pico_sdk_import.cmake)
  3. project(test_project C CXX ASM)
  4. set(CMAKE_C_STANDARD 11)
  5. set(CMAKE_CXX_STANDARD 17)
  6. pico_sdk_init()
  7. add_executable(0x02_hello_world
  8. 0x02_hello_world.c
  9. )
  10. pico_enable_stdio_usb(0x02_hello_world 1)
  11. pico_add_extra_outputs(0x02_hello_world)
  12. target_link_libraries(0x02_hello_world pico_stdlib)

Next we need to copy the pico_sdk_import.cmake file from the external folder in the pico-sdk installation to the 0x02_hello_world project folder.

  1. cp ../pico-sdk/external/pico_sdk_import.cmake .

Finally we are ready to build.

  1. mkdir build
  2. cd build
  3. export PICO_SDK_PATH=../../pico-sdk
  4. cmake ..
  5. make

This will produce a number of files and the ones we are going to focus on are the .elf file when it comes to debugging and hacking which is the full program output, possibly including debug information and the .uf2 file which is the program code and data in a UF2 form that you can drag-and-drop on to the RP2040 board when it is mounted as a USB drive.

I took the time to wire up a reset button on the Pico so that I do not have to keep unplugging in the USB and pressing the BOOTSEL every time I need to re-flash so here is the schematic of such.

Part 2 - Hello World - 图1

To flash press the external button and while it is still pressed, press the BOOTSEL on the board, then release the BOOTSEL and finally release the external button.

Then simply copy the .uf2 file to the drive.

  1. cp 0x02_hello_world.uf2 /Volumes/RPI-RP2

Then we need to locate the USB drive so you can do the following.

  1. ls /dev/tty.

Press tab to find the drive and then in my case I will use screen to connect.

  1. screen /dev/tty.usbmodem0000000000001

Hooray! You should see, “Hello world!” to the standard output every second.

In our next lesson we will debug the .elf binary in Radare2.