{"id":11152,"date":"2023-10-30T09:30:35","date_gmt":"2023-10-30T07:30:35","guid":{"rendered":"https:\/\/deepbluembedded.com\/?p=11152"},"modified":"2023-11-02T09:01:58","modified_gmt":"2023-11-02T07:01:58","slug":"raspberry-pi-pico-w-sdk-c-programming-rp2040","status":"publish","type":"post","link":"https:\/\/deepbluembedded.com\/raspberry-pi-pico-w-sdk-c-programming-rp2040\/","title":{"rendered":"Raspberry Pi Pico SDK C\/C++ Programming (Pico W | RP2040)"},"content":{"rendered":"\n

This tutorial is a Getting-Started Guide For The Raspberry Pi Pico C\/C++ SDK Programming (And Pico W)<\/strong>. The RP2040-based Raspberry Pi Pico boards can also be programmed using (Arduino C++, MicroPython, CircuitPython, and Rust). You can find navigation buttons on the left sidebar to go to the Raspberry Pi Pico tutorials series using the programming language you prefer.<\/p>\n\n\n\n

In this tutorial, we’ll set up the Raspberry Pi Pico C\/C++ SDK Toolchain<\/strong> to use VS Code <\/strong>as an IDE<\/strong> for the development of embedded projects with RP2040-based Pi Pico boards. Without further ado, let’s get right into it!<\/p>\n\n\n

Table of Contents<\/h2>\n
    \n
  1. Raspberry Pi Pico C\/C++ SDK Programming<\/a>\n\n\n<\/li>\n\n<\/li>\n\n<\/li>\n\n
  2. Installing Pi Pico C\/C++ SDK Toolchain<\/a>\n\n<\/li>\n
  3. Flashing Firmware To Raspberry Pi Pico (C SDK)<\/a>\n\n<\/li>\n
  4. Standalone Raspberry Pi Pico C\/C++ Project<\/a>\n\n<\/li>\n
  5. Raspberry Pi Pico C\/C++ SDK LED Blinking Example<\/a>\n\n<\/li>\n
  6. Raspberry Pi Pico W C\/C++ SDK LED Blinking Example<\/a>\n\n<\/li>\n
  7. Raspberry Pi Pico C\/C++ SDK Project Simulation (Wokwi)<\/a>\n\n<\/li>\n
  8. Concluding Remarks<\/a>\n<\/li><\/ol>\n\n\n
    \n\n\n

    Raspberry Pi Pico C\/C++ SDK Programming<\/strong><\/h2>\n\n\n

    As stated in the previous Getting Started With Raspberry Pi Pico (MicroPython)<\/a><\/strong> tutorial, the Pi Pico C\/C++ SDK programming is the most conservative way of programming the RP2040-based microcontrollers. Embedded-C programming gives you the highest level of control over your embedded software behavior and performance.<\/p>\n\n\n\n

    \"\"<\/figure>\n\n\n\n

    <\/p>\n\n\n\n

    We can deterministically build reliable embedded systems using the C programming language as we’ve always been doing over the past decades. It’s still the standard way of developing such systems across multiple industries. Therefore, in this tutorial, we’ll prepare the required setup to develop and build C projects for the Raspberry Pi Pico RP2040 microcontroller.<\/p>\n\n\n

    RP2040 Hardware Architecture<\/strong><\/h3>\n\n\n

    The RP2040 microcontroller, like most microcontrollers, has: internal RAM, ROM, GPIOs, Peripherals, and DMA. Additionally, it’s got dual-core ARM Cortex-M0+ processors and a PIO which is a very interesting feature that we’ll address in future tutorials. The diagram below shows you the internal architecture and interconnection of the RP2040 hardware.<\/p>\n\n\n\n

    \"Raspberry-Pi-Pico-RP2040-Hardware-Architecture-Diagram\"<\/figure>\n\n\n\n

    <\/p>\n\n\n

    What is an SDK? Why To Use Pico C\/C++ SDK?<\/strong><\/h3>\n\n\n

    All the peripherals and hardware features that you’ve seen in the RP2040’s architecture diagram above are configured and controlled with special function registers (SFRs) in the microcontroller’s memory. You can refer to the RP2040’s datasheet for details of how each peripheral operates and which registers are responsible for which configuration or operation.<\/p>\n\n\n\n

    To build embedded applications using the RP2040, or any other microcontroller, we need to build a memory map for the hardware registers that define the address of each SFR register in the memory and give it a name that we can refer to in the code. This is, of course, a tedious process that we have to do unless <\/strong>the manufacturer provides<\/strong> their own SDK<\/strong> (S<\/strong>oftware D<\/strong>evelopment K<\/strong>it) that has, at least, memory-mapped low-level register definitions.<\/p>\n\n\n\n

    The Raspberry Pi Pico C\/C++ SDK not only will give us low-level memory-mapped register definitions, but also all the necessary low-level drivers for most of the hardware peripherals in the microcontroller. It’s got also some other features as we’ll see in the next section.<\/p>\n\n\n

    Raspberry Pi Pico C\/C++ SDK Overview<\/strong><\/h3>\n\n\n

    The Raspberry Pi Pico C\/C++ SDK consists of multiple low-level device drivers and hardware abstraction drivers that are built on top of the low-level drivers. There are also a handful of high-level APIs that provide some services to support your target application (things like time, sleep, utilities, etc).<\/p>\n\n\n\n

    The Raspberry Pi Pico C\/C++ SDK software architecture can be viewed as shown in the figure below. The Pico SDK will abstract the interfacing between the applications we’ll be developing in this series of tutorials and the target RP2040 microcontroller’s low-level hardware.<\/p>\n\n\n\n

    \"Raspberry-Pi-Pico-SDK-Software-Architecture-Layers-Diagram\"<\/figure>\n\n\n\n

    <\/p>\n\n\n

    CMake & Pico C\/C++ SDK Build Process<\/strong><\/h3>\n\n\n

    As of writing this tutorial, there is no “proper” IDE for developing embedded-C projects based on the RP2040 microcontroller. This means that we’ll have to install individual tools to create a convenient development environment and we’ll have to deal with compilers at a lower level and use things like CMake, compiler flags, build tools, etc.<\/p>\n\n\n\n

    The maker space has always been dominated by Arduino, AVR, PIC, STM32, and other mainstream microcontrollers. Each of which has an excellent development environment where IDEs handle all low-level stuff and no one has to deal with compiler flags, CMake, and such. Just don’t get frustrated by this matter and look at it as a chance to learn new things and get closer to the compiler and the C build process.<\/p>\n\n\n\n

    The Raspberry Pi Pico SDK uses CMake<\/strong> to manage project builds. Each project should have a file called CMakeLists.txt<\/code>, which specifies the header files to which the project should link, any source files that it should include, and other configurations for the build process.<\/p>\n\n\n\n

    For each project we’ll create, we’ll use a CMakeLists.txt file template that will be modified to suit each project’s needs. We’ll demonstrate this more later on in this tutorial as we’ll create and build a project from scratch after installing the required toolchain.<\/p>\n\n\n

    \n
    What is CMake?<\/strong><\/div>\n\n\n

    CMake is an open-source cross-platform build system that provides a unified, platform-independent way to manage the build process for software projects. It allows developers to describe the build process using a simple scripting language (CMake language) and it generates native build files for various platforms and build environments.<\/p>\n\n<\/div><\/div>\n\n\n


    \n\n\n

    Installing Pi Pico C\/C++ SDK Toolchain<\/strong><\/h2>\n\n\n

    Now, we need to install the Raspberry Pi Pico C\/C++ SDK toolchain to start developing our first embedded project for the RP2040 microcontroller. Here is a list of the software tools that we need to install:<\/p>\n\n\n\n