MAE 3185 - Introduction to Mechatronics

Logo

View the Project on GitHub Abhiricky1992/UTA-MAE3185-Notes

Code Development Process

You created a folder in section HelloWorld! of Software Installation chapter with three files. Such a folder with a C/C++ file, the pico_sdk_import.cmake file and a CMakeLists.txt will be called a ‘Project Folder’. Following is the directory structure of a project folder,

project_folder
├── build
│   └── Files compiled by cmake
├── CMakeLists.txt
├── *.c
└── pico_sdk_import.cmake

Each of these files contain following information,

Out of these three files, you’ll have to worry about the C/C++ file and the CMakeLists.txt files only. Let’s take an example to learn how the compilation process works. Create a new folder anywhere you like. You can copy the pico_sdk_import.cmake file from section HelloWorld! into this folder.

Don't keep your 'Project Folder' in the 'Downloads' folder if you are using a **Mac** device.

Each project folder can have multiple C/C++ files. However, that situation will not arise throughout this class. So, create a C/C++ file with whatever name you like, don’t keep any Space in the name.

Analyzing the C/C++ file

Copy following content into the C/C++ file that you have just created.

#include <stdio.h>
#include <pico/stdlib.h>

int main()
{
    stdio_init_all();

    while (true)
        printf("Hello World!\r\n");
}

You might have noticed that this is a stripped down version of the code that you compiled in HelloWorld! section. Moreover, you might also understand the majority of the code from your C/C++ programming days. What might be new to you is,

Analyzing the CMakeLists.txt

Now, copy following content into a CMakeLists.txt file and then take a look at the comments, the contents after # symbol.

cmake_minimum_required(VERSION 3.13)            # Specify the minimum required version of cmake
set(ENV{PICO_SDK_PATH} "~/pico/pico-sdk/")      # Specify the path to Raspberry Pi Pico C/C++ SDK in your system
include(pico_sdk_import.cmake)                  # Include the contents of the pico_sdk_import.cmake file that exists in the same folder
project(myProject C CXX ASM)                    # Specify the name of the project and the type of source files it may contain

set(CMAKE_C_STANDARD 11)                        # Specify what C standard to follow
set(CMAKE_CXX_STANDARD 17)                      # Specify what C++ standard to follow
pico_sdk_init()                                 # Initialize necessary components of the SDK

add_executable(myProject                        # This is where the name of the C/C++ file will go.
*.c
)
pico_add_extra_outputs(myProject)               # Tell cmake to create a UF2 file

pico_enable_stdio_usb(myProject 1)              # Configures USB as the 'Standard Input Output'
pico_enable_stdio_uart(myProject 0)             # Don't configure UART as the 'Standard Input Output'

target_link_libraries(myProject pico_stdlib)    # Link libraries that are used in this project

Most of the content in CMakeLists.txt will stay the same for all the project folders you may create in this course. Following are the lines that may change,

Starting next chapter, we’ll discuss different peripherals available in the μC. Thus, you’ll have to add the libraries for the specific peripheral you might be working on in the C/C++ file and the CMakeLists.txt file, and you’ll be all set to use the functions available in that library. These notes will discuss some important functions for each peripheral. However, if you want to know more about the functions available in a specific library, then you can always look at the SDK documentation.

Copy the Code to the μC

You can build/compile this code into a .uf2 file by clicking on the ‘Build’ button in VSCode. We have to transfer this binary, the .uf2 file, to the μC for it to run. This is where the BOOTSEL button on the μC comes into picture. Connect the μC to your laptop through the USB cable while keeping the BOOTSEL button pressed down. This will cause the μC to boot into the ‘USB Storage’ mode. You’ll see a USB drive detected in your laptop. Now you can copy the .uf2 file into the USB drive. As soon as the copying completes, the μC reboots and starts running the code that you have just copied.

Next

Microcontroller Laptop Communication

Back

Raspberry Pi Pico