Setting Up FreeRTOS From Scratch on STM32F407 Discovery Kit

Introduction: Setting Up FreeRTOS From Scratch on STM32F407 Discovery Kit

About: Embedded Systems Engineer

Choosing FreeRTOS as a Real-Time Operating System for your embedded project is a great choice. The FreeRTOS is truly free and provides many simple and effective RTOS features. But setting up freeRTOS from scratch might be difficult or I can say a bit confusing as it requires some customization such as adding Microcontroller specific files, setting header file paths, etc. In this Instructable, I will guide you on how to set up FreeRTOS on your STM32F407 Discovery kit in detail using Kiel uVision IDE.

Supplies:

Step 1: Open Keil UVision IDE

Open Keil uVision IDE. Click on a project the select New uVision Project...Then select your working directory and give your preferred project name.

Step 2: Select the Device

Once you have given name to the project, in the next step you need to add device. Here we are adding STM32F407VG Micronconroller from STMicroelectronics. Select the STM32F407VG, then Click OK.

Step 3: Manage Run-Time Environment

The next step is to select the library/driver component in the Manage Run-Time Environment Tab. Here select all components as shown in the above picture. Once you check all appropriate field Click Resolve then Click OK.

Step 4: Copy FreeRTOS Into Your Project Folder

Now you need to Copy the entire FreeRTOS folder into your project folder.

Step 5: Add FreeRTOS Files to Project

Once you have copied the FreeRTOS folder inside your project folder, you have to add all the necessary FreeRTOS files to your Project.

  1. In Keil, Select Target1, right-click then select Add new group. Rename this group as FreeRTOS.
  2. Now Click on the FreeRTOS group, right-click the select Add Existing files to Group "FreeRTOS..."
  3. Add all the FreeRTOS files as shown in the above picture.

The path to finding these files in FreeRTOS folder are :

  • Files: croutine, event_groups, list, queue, stream_buffer, tasks and timers. Path: (....\FreeRTOSv10.2.1\FreeRTOS\Source)
  • Files:heap_4(There are 4 memory management files add anyone). Path: (....\FreeRTOSv10.2.1\FreeRTOS\Source\portable\MemMang)
  • Files: port.c (This is an MCU specific file). Path: (...\FreeRTOSv10.2.1\FreeRTOS\Source\portable\RVDS\ARM_CM4F)

Note : FreeRTOS version may change. Just use the latest version available.

Step 6: Configure the Path of the FreeRTOS Header Files

Once you have added the FreeRTOS source files, you need to tell the compiler where respective header files are located. Hence we need to configure the compiler option.

Right Click on Target1--> Option for Target "Target1.." --> C/C++--> Include path. Make sure you include these paths :

  1. Include folder in FreeRTOS ( ...\FreeRTOSv10.2.1\FreeRTOS\Source\include)
  2. RVDS directory (...\FreeRTOSv10.2.1\FreeRTOS\Source\portable\RVDS\ARM_CM4F)

Note: If you have any header files, make sure you include the path of these header files as explained above.

Step 7: Add "FreeRTOSConfig.h" File to Project

The FreeRTOS has one important header file called FreeRTOSConfig.h. This file contains the application-specific(in our case-specific to Cortex M4F MCU) customization. For simplicity, I have copied our MCU specific FreeRTOSConfig.h file in the RVDS directory. And also in step 6, we have already added the RVDS path. If you're adding it yourself then You have to add this file in your project and also make sure you include the path of this file as explained in step 6.

Incase if you want to add FreeRTOSConfig.h file by your self in your preferred directory, I have included this file below.

For more information Click here FreeRTOSConfig.h

Step 8: Add the "main.c" File With the Basic Template

  • Now Create a New User group( I have renamed it to "User application").
  • Add a new C-file to this Group( I have added a file called main.c).
  • This is the file where the main() function exists. I have included all the minimum required functions and headers into this file so that the project successfully compiles.

You can find the main.c file with basic template below.

Attachments

Step 9: Connect Your STM32F407 Discovery Kit to Your PC/Laptop

Step 10: Select ST-Link Debugger in Compiler Configuration

Right Click on Target1, then click on Option for Target "Target1..", then navigate to Debug Tab andSelect ST-Link-Debugger as shown in the above picture

Step 11: Configure ST-Link Debugger

After selecting the ST-Link Debugger in step 10, click on Settings then select Trace and check all the fields as shown in the above picture.

Step 12: Build and Upload the Code

After completing all the steps build the project and make sure there are no errors in the code. After successful compilation, upload the code to your Discovery Kit.

Step 13: Goto to Debug Window and Open Serial Monitor

After uploading go to debug window-->view-->Serial Windows-->Debug(printf) Viewer as shown in the above picture.

Step 14: Run the Code to See the Ouput on Debug Printf Window

The Final step is to run the code as shown in the picture to see the output in the printf window. Here in main.c I have implemented 2 simple tasks called task1 and task2. Both the task have the same priority and they just print the name of the task. Due to the same priorities you can see both of them running and printing the name.

Be the First to Share

    Recommendations

    • Make it Glow Contest

      Make it Glow Contest
    • First Time Author Contest

      First Time Author Contest
    • PCB Challenge

      PCB Challenge

    6 Discussions

    0
    j.samuelsclarke
    j.samuelsclarke

    20 days ago

    Thank you so much for this! I don't know why but trying to learn FreeRTOS is making me pull my non-existence hair out. I don't mean it's hard, it's getting the basic examples working that's been the problem.

    0
    baojason
    baojason

    5 months ago

    Sharath_N25, Thanks for such a nice document about the FreeRTOS starting on STM32. I follows your steps and have the project successfully built. However, I cannot see the running output (with printf) on the debugger viewer. The debugger viewer window is empty.
    I think step13 indicates to redirect the output to debugger viewer, but I don't know why it does not work in my case?
    I tried to setup breakpoints and I can step into the code and see the running procedures.

    I am using a STM32F4VE board as below and the processor is STM32407VET6. Not sure if it is because of different on the hardware?

    Annotation 2020-08-08 075208.png
    0
    Sharath_N25
    Sharath_N25

    Reply 5 months ago

    Hi baojason, Try printing some text using printf() without RTOS. There should some documentation on how to do this for your board. First figure out that then implement the same with RTOS.
    Note : on Step 3 please make sure you check mark "STDOUT --> ITM" which actually enables you to print on debug window..

    0
    matteo.concas59
    matteo.concas59

    Question 6 months ago on Step 12

    Hi, after following all the steps when I try to build the compiler returns that the build failed with 91 errors and 200+ warnings. However when building directly from your projet everything compiles. The two projects seem identical but the IDE apparently doesn't see it that way. I'm using FreeRTOS v10.3.1 and the STM32f4.


    EDIT : So after looking into it, what caused all those issues was that the newer versions of the Keil IDE default to ARM compiler 6. After changing it to the ARM Compiler 5 in "Options for Target 'Target 1' " / Target / ARM Compiler, the project built with 0 error and 0 warning.

    Annotation 2020-07-27 123504.png
    0
    Ken Garner
    Ken Garner

    Question 6 months ago on Step 1

    In attempting to Compile FreeRTOS for STM32F407VGT6 Receive a number or compiler errors.
    From Port.h and PortMacro.h: Port.h from ..portable/RVDS/ARM_CM4F
    error __ASM expecting ( and not {
    Am I using the wrong compiler.
    Thanks in anticipation
    Ken Garner

    0
    Sharath_N25
    Sharath_N25

    Answer 6 months ago

    Hi Ken, I am not sure what is the problem, Please follow all the steps given in the Instructables and verify, Definitely you can compile FreeRTOS successfully. The error say expecting "(" , please check the error messages in detail(if it's pointing to a specific line of the file?) try fixing it.