Running FreeRTOS on ZYNQ SoC

This project demonstrates how to get started with FreeRTOS on the Zynq SoC.
Introduction
The AMD Zynq SoC device combines the programmable logic structure of a typical FPGA with the processing power provided by an ARM processor core, serving as a platform for building various embedded system applications. Dedicated logic structures can be designed in the PL to handle compute-intensive tasks, while the PS can control the PL and provide user applications.

An example of a product that can utilize this capability is an endoscope. The video chain is implemented in the PL, processing the image signals from the camera and providing additional features such as video saving, resolution changes, and color correction. The processed video can then be sent to a display receiver.
This tutorial will demonstrate how to run applications on the FreeRTOS kernel in the Zynq PS.
FreeRTOS Real-Time Operating System
Before understanding what FreeRTOS is, let’s take a look at three ways to develop embedded systems:

The differences among the three approaches lie in how the software portion is developed. Software can be divided into user space (where applications reside) and kernel space (where drivers and libraries reside). The size of the software increases from bare metal to embedded operating systems.
FreeRTOS is an open-source operating system specifically designed for real-time applications. It provides a kernel space that can be used to build applications. Developers can customize the FreeRTOS kernel, allowing for the construction of applications with real-time constraints. An example of an application that is not suitable for RTOS is running a mature GUI.
Next, let’s dive into how to set up FreeRTOS on the Zynq PS.
Hardware Design in Vivado
Open Vivado and add Zynq PS in the newly created block design.

Customize peripherals in the PS, where we mainly focus on adding the UART peripheral. Other peripherals such as GPIO, SD0, USB0, etc., can be enabled according to your hardware.

Save and validate the design. Then create an HDL wrapper, generate output products, and generate the bitstream. Export the hardware platform information as XSA.

Vitis Software Development
Launch Vitis IDE and create a new platform project using the XSA. Select the FreeRTOS kernel as the operating system.

After creating the platform project, modify its BSP to disable the xiltimer software library.



Then build the platform. Now create a new application project using the FreeRTOS Hello World template.

Before explaining the main application code, I want to mention that the scheduler within the FreeRTOS kernel requires the timer to be configured to the default frequency of 100Hz to operate. This can be either a software or hardware timer. That’s why I chose to disable the software timer and its library in the BSP. Otherwise, the application would conflict with the hardware timer during runtime.
The main application uses the xTaskCreate() function to define two FreeRTOS tasks:

Then, it configures the hardware timer for 10 seconds, after which it will expire.

Now it is important to figure out which hardware timer the design is using. Remember, when customizing the Zynq PS in Vivado, we did not enable any such timer (TTC/SWDT). We can look at the porting code present in the platform project.

The portZynq7000.c file contains information on how to port the FreeRTOS kernel to fit devices based on the Zynq 7000 SoC. Upon opening it, we can see the following conditional definitions:

Thus, after disabling the xiltimer, the design relies on using the scutimer to generate timer interrupts. This scutimer refers to the CPU-specific timer present within each ARM-A9 core.

If you read the portZynq7000.c file in detail, you can see how to use the scutimer and scugic to set up timer interrupts.
Once the timer is started, the vTaskStartScheduler() call is made to start the scheduler of the FreeRTOS kernel. The scheduler will start the two tasks defined above, and the program will execute any code defined in those two tasks.
Final Result
After successfully building the software, run the application on the board. You will see the following text printed on the UART console:

Conclusion
That’s it! You are ready to start developing more complex programs on the FreeRTOS kernel running on the Zynq SoC.