Page 1 of 3 There are big advantages to using FreeRTOS with the Pico, but getting started isn't easy - unless you read on. This is an extract from our latest book on the Raspberry Pi Pico 2W in C.
Master the Raspberry Pi Pico in C: WiFiwith lwIP, mbedTLS & FreeRTOS Second Edition
By Harry Fairhead & Mike James

Buy from Amazon.
Contents
Preface
- The Pico WiFi Stack
- Introduction To TCP
Extract: Simplest HTTP Client *
- More Advanced TCP
- SSL/TLS and HTTPS
Extract: Simplest HTTPS Client *
- Details of Cryptography
Extract: Random Numbers*
- Servers
Extract: HTTP Server *
- UDP For Speed
Extract: Basic UDP *
- SNTP For Time-Keeping
- SMTP For Email
- MQTT For The IoT
- FreeRTOS
Extract: Installing FreeRTOS NEW!!
- Client Sockets
- Socket Server
- Secure Sockets
Appendix 1 Getting Started In C
* Extracts from the first edition not yet updated.
<ASIN:B0FDYDPQ54>
FreeRTOS
When it comes to WiFi and lwIP, there is a considerable advantage in using FreeRTOS with the Pico. It is the only reasonable way to make use of the lwIP Sockets API. Sockets are the standard way to make connections over a network, and it is the only easy way to use blocking network calls while keeping the rest of the application running. As well as being a significant improvement when it comes to WiFi and networking in general, it is also regarded by many as the best way to organize an application as a collection of asynchronous tasks. This is not always a good fit for applications that need guaranteed response times, but for applications that it does work for, it is a big simplification. That said, it is worth remembering that you need to control access to shared objects to avoid race conditions. In this chapter, we look at using FreeRTOS with the Pico. In the next chapter, we look at how to use lwIP and mbedTLS under FreeRTOS. This chapter is based on Chapter 18 from Programming The Raspberry Pi Pico/W In C, Third Edition, ISBN: 9781871962963 and is included here for completeness. If you know how to get started and use FreeRTOS, skip to the sections that interest you.
Install FreeRTOS
FreeRTOS is an open source project to make a real-time operating system available on a wide range of processors. The basic FreeRTOS is a single-core operating system aimed at making running multiple tasks easier. The version of FreeRTOS used by the Pico has been extended to work with two cores to utilize Symmetric Multi-Processing (SMP), a technique where multiple processors work together to execute tasks simultaneously. This is the version of FreeRTOS that is available from the Raspberry Pi website. It is a complete port of FreeRTOS, but it lacks documentation which means that getting started can be difficult if you don’t know much about FreeRTOS and, even if you do, getting it to work with VS Code and the Pico SDK is tough.
To get FreeRTOS installed in a project, first start a new project or navigate to the root folder of an existing project. If you are creating a new project, select the libraries that you are sure you want to use – you can add more in the CMakeLists.txt file later. Compile and run the program just to make sure that the project works before you add FreeRTOS to it.
Next make sure you are in the top-level directory of the project, start a terminal window and enter the command:
git clone https://github.com/raspberrypi/ FreeRTOS-Kernel.git
The copy takes a few minutes:
Cloning into 'FreeRTOS-Kernel'...
remote: Enumerating objects: 163154, done.
remote: Counting objects: 100% (1092/1092), done.
remote: Compressing objects: 100% (893/893), done.
remote: Total 163154 (delta 209), reused 199 (delta 199), pack-reused 162062 (from 1)
Receiving objects: 100% (163154/163154), 109.84 MiB | 8.54 MiB/s, done.
Resolving deltas: 100% (117064/117064), done.
When the command has copied all of the files, you will find a new FreeRTOS-Kernel directory:
The FreeRTOS kernel that you need to compile and use is stored in FreeRTOS-Kernel/portable/ThirdParty/GCC/ where you will find a folder for each possible configuration of the Pico, RP2040, RP2350_ARM_NTZ and RP2350_RISC-V:
To include the correct directory in you project you need to copy the file
FreeRTOS_Kernel_import.cmake
into the top-level project folder. You will find this file in each of the Pico folders and you can use any of these copies as they are identical. This file contains the CMake instruction needed to work out which version of the Pico you are using and to compile the contents of the appropriate folder.
The next step is to modify the CMakeLists.txt file to include this file and setup the compile of FreeRTOS. Add two lines after the line that includes the pico_sdk_import.cmake file:
# Pull in Raspberry Pi Pico SDK (must be before project)
include(pico_sdk_import.cmake)
set(FREERTOS_KERNEL_PATH ${CMAKE_CURRENT_LIST_DIR}/ FreeRTOS-Kernel)
# Adjust path if different
include(FreeRTOS_Kernel_import.cmake)
If you have installed FreeRTOS in another location change the set to the correct path.
Lower down in the file change the target_include_directories and target_link_libraries to read:
# Add the standard include files to the build
target_include_directories(hellofreertos PRIVATE
${CMAKE_CURRENT_LIST_DIR}
include/
)
# Add any user requested libraries
target_link_libraries( hellofreertos
pico_multicore
FreeRTOS-Kernel-Heap4)
)
The link_libraries can include additional libraries according to the nature of the project.
To complete the set up of FreeRTOS we need to include a configuration header FreeRTOSConfig.h in the project. You can find a default header in the Pico examples on Github:
https://github.com/raspberrypi/pico-examples/blob/master/
freertos/FreeRTOSConfig_examples_common.h
You can also find the file in this book’s Github repo.
It should be copied to the project folder:
FreeRTOS-Kernel/include/FreeRTOSConfig.h
Note the change in filename. There are other FreeRTOSConfig.h files in examples, but they usually don’t work with the latest version of FreeRTOS.
Now we have a Pico project that has FreeRTOS installed and ready to use.
As a simple test program try the following, hellofreertos.c:
#include <stdio.h>
#include "pico/stdlib.h"
#include "pico/cyw43_arch.h"
#include <FreeRTOS.h>
#include <task.h>
void task(void *arg){
printf("Hello FreeRTOS World \n");
}
int main()
{
stdio_init_all();
xTaskCreate(task, "task", 2048,NULL, 1, NULL);
vTaskStartScheduler();
}
This program creates a new FreeRTOS task and then starts the system running. If everything is working, vTaskStartScheduler never returns and spends its time running any tasks you have created. In general tasks don’t ever come to an end, but in this case it is simpler to just return.
|