Thursday, December 8, 2022

World No 1 Tech News Website

Programming Raspberry Pi Pico using C | Getting Started with C SDK

Must read

Aizaz khan
Aizaz khanhttps://myelectricsparks.com/aizaz-khan/
Aizaz was the first person to get a byline on his blog on technology from his home in Bannu in 2017. Then, he went on to a career in breaking things professionally at my electric sparks which is where he eventually took over the kit as a hardware editor. Today, as the senior editor of hardware for my electric sparks, he spends time reporting about the most recent developments in the hardware industry and technology. If he's not reporting on hardware or electronics, you'll see him trying to be as remote from the world of technology as possible through camping in the wild.

This article is all about the Programing of Raspberry Pi Pico using C.We have explained all steps of ” How to program Raspberry Pi Pico using C”, how to download programing language (for example Raspberry Pi Pico’s C SDK) and its example is given from Github.

Further sub-topics are also explained for knowledge such as installation of all necessary tools, creating the Blinky C Program, and uploading UF2 file to Pico and its working Examples.

Have You Read These?

For best results, you should go through two other Articles named” Getting Started with Raspberry Pi Pico “and “Programming Raspberry Pi Pico with MicroPython”.
We strongly prefer you to read the above-given articles for better understanding and to know more about Raspberry Pi Pico C SDK.

Getting Started with Raspberry Pi Pico C SDK

The writing of C programming language will be easier for those who had graduated from old school hardware engineer and also gained experience in writing firmware. These students are more experts in programming C language than Python or MicroPython firmware writings✍.  For information purposes ” MicroPython is written in C”.
The Raspberry Pi Foundation promotes Python as the initial programming language.  But things get complicated in the case of MicroPython for all Raspberry Pi SBCs.
In the list of high programming languages “C “is considered low among them. For complete grip over hardware gain knowledge about all programming languages then C language will be the easiest option for you. C language is also considered for the programming of microcontrollers and embedded systems.
In promoting and investing in MicroPython the Raspberry Pi Foundation invented SDK (Software Development Kit) for C / C++ programming languages to create applications for RP2040 based boards. The RP2040 Microcontroller-based Raspberry Pi Pico is no exception for it.
Now let’s come toward Programming Raspberry Pi Pico using C language.

Which Computer is used?

In the “Programming Pico with MicroPython” article it is already explained in detail but for the reminder, Any Computer based on Linux, Window,s or Mac system can be used but the best option is Raspberry Pi Computers are the Raspberry Pi Pico focuses on the Raspberry Pi OS as the main host.

If the latest Raspberry Pi 4 SBC is not available then you can use the previous version of Raspberry Pi 3 Model B. Now getting started with C SDK use Raspberry Pi to create applications for Pico.

NOTE:

In the future, we will try our best to provide you with another article based on working with others systems like Windows, Visual Studio Code, etc, and their environments.

Get Pico’s C SDK and Examples

Consider that you are using a Raspberry Pi Computer. Now follow the steps;
  1. Log in to Pi using SSH or VNC. For best results connect Pi to a monitor.
  2. Use the VNC viewer for opening the desktop of Raspberry Pi.
  3. Now Open the terminal and create a directory called ‘pico’ at /home/pi/pico.
  4. then Add the given commands step by step.
cd ~/ mkdir pico cd pico

Download C SDK

The official repository for Pico’s C SDK is https://github.com/raspberrypi/pico-sdk. Copy this repo to your fresh-made ‘pico’ directory using the given commands step by step. git clone -b master https://github.com/raspberrypi/pico-sdk.git

This will develop a directory called ‘pico-SDK inside the ‘pico’ directory.

Before moving forward toward Downloading step, first, initialize the ‘tinyusb’ submodule in the ‘pico-SDK for USB functions like USB CDC Serial to work.

Now open the ‘Pico- SDK’ directory and initializing the USB submodule using the given commands.

cd pico-SDK
git submodule update – -init

Download Examples

Raspberry Pi Foundation had introduced different peripheral like GPIO, ADC, PWM, I²C, SPI., etc. Now to make C Program use these examples to get knowledge about different functions and APIs. The main Repo of Pico Examples are:

https://github.com/raspberrypi/pico-examples

Now copy this repo into the ‘pico’ directory and then go back to the ‘pico’ directory and copy the git by witting the given commands.

cd. git clone -b master https://github.com/raspberrypi/pico-examples.git

Installing the Toolchain

Just downloaded the C source files for Pico. To compile and build these files,  additional tools like ARM GCC Compiler, ARM Cortex libraries, CMake build tool, etc are needed. All these tools can be installed using ‘apt’. Now update the package index using the ‘update’ command.

sudo apt update

Now, install the necessary tools to write the given commands. If any of these tools are installed, ‘apt’ will not install them again.

sudo apt install cmake gcc-arm-none-eabi libnewlib-arm-none-eabi build-essential

WARNING: The installation of tools occupies approximately 1800 MB of disk space. Make sure you have enough space on the SD Card in which the Raspberry Pi OS is installed. It will be best to use a 16 GB card.

Also, A bit of time for is needed completion of installation so Grab a cup of coffee and relax.

Exploring Blink Example

Let’s see the first C example program, which is known as the ‘Blink’ program’. Now click on the ‘pico’ directory, open the ‘pico-examples’ directory. This directory consists of different examples of different peripherals and modules. Now Open the‘ blink’ directory.

cd pico-examples/blink

Now the ‘blink.c source code for blinking the onboard LED. Open it, use an editor like ‘vim’. If ‘vim is not installed then install it using Sudo apt install vim command.

vim blink.c

We had created a copy of the code and added comments for easy understanding.

view raw raspberry-Pi-Pico-C-Blink.c hosted with ❤ by GitHub

 

Now build this source code and generate a binary file.

Build the Blink

Now return to ‘pico-examples’ directory and create a new directory called ‘build’. This directory consists of all the CMake build files for all the example projects. Now Open the newly developed ‘build’ directory.

cd…
mkdir build
cd build

Export the path for pico-SDK by writing the given commands.

export PICO_SDK_PATH=../../pico-SDK

It’s now ready to run command CMake to prepare the build directory.

CMake..

The ‘build’ directory consists of all the build-related files for all the examples. But we want only the blink example, let’s create the example by writing the blink command. Now open the ‘blink’ directory in the ‘build’ directory and make the files.

cd blink
make -j4

NOTE:

The ‘-j’ option work is to specify the numbers to do the job perfectly. As the Raspberry Pi 3 had a quad-core CPU, so select 4 important jobs.

The command ‘make’ will combine all the important files and will create a group of binary files.

Make a list of the contents for the ‘blink’ directory in the ‘build’ directory, then the bunch o files will be generated.

As the most important file is‘.uf2’  named ‘blink.uf2’. In the MicroPython Article, this is file is formatted from the MicroPython binary and it can be easily uploaded to Raspberry Pi Pico. The easiest method is just by dragging and dropping (set the Pico in bootloader mode afterward).

Load the Blink and Run it

The easiest way to use Raspberry Pi Pico for uploading the file is;

Just the file to a Bootloader mode, it will mount the Pico as a USB Mass Storage Device. Then easily drag-and-drop the blink.uf2 file.

For setting the Pico in Bootloader mode follow the steps:

First, attach the micro USB cable to Raspberry Pi Pico and hold the ‘BOOTSEL’ button while attaching the cable to Raspberry Pi (or the host computer). The Pico will automatically mount as a mass storage device.

Open the file manages, then search pico/pico-examples/build/blink directory. Just drag and drop the ‘blink.uf2’ file onto Pico. The Pico will start resetting and will get detached from the computer and the LED will start blinking.

Bonus: Alternative to Unplugging and Plugging USB Cable

If you are fed up with restarting Raspberry Pi Pico each time after uploading it to the Bootloader mode?
If your answer is yes, then here is a method by following it, you don’t have to do Plugging and Unplugging each time after uploading a program to Pico.
The process of Plugging and Unplugging is actually resetting Pico. If there’s another method to reset Pico while keeping the Pico Plugged into the computer then use it.
Given below is one of the simplest and easiest ways to reset Pico.
If you had studied the Pinout of  Raspberry Pi Pico. There is a small pin number 30 known as ‘RUN’

That pin is actually the global reset pin of the RP2040 Microcontroller. If this pin is pulled down,  the RP2040 microcontroller will start resetting.

So, At first, attach a push-button between the RUN pin and GND. Whenever the button is pushed, the microcontroller will start resetting.  Let’s named this button as “Reset Button”.

Now for converting the Raspberry Pi Pico to Bootloader Mode, follow the given steps:

1: At first push and then hold the resetting button and BOOTSEL Button. Now slowly release the Reset button and then release the BOOTSEL button after a second.

That’s all, now the Pico is in Bootloader Mode. It can also keep the USB Cable and the host computer connected to Pico.

As you have learned, now you can easily reset and also convert Pico to Bootloader mode. For Resetting Pico only just press the Reset Button once and then release it.

Conclusion

It’s a complete guide for beginners. After reading you will be able to understand what is Raspberry Pi Pico C Programming language and how it’s downloaded.
For any difficulty feel free to comment down below and also give us feedback about our article through comments ????.
FTC: We use income earning auto affiliate links For More info Click Here.

More articles

2 COMMENTS

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Latest article