DVFS operation in FreeRTOS

Hello Team,
I would like to access FreeRTOS Kernel power management drivers. I need to do Dynamic Voltage Frequency scaling in FreeRTOS. That’s done in Power drivers of FreeRTOS or CPU Frequency related drivers. But I am not able to find these drivers in FreeRTOS source code available for downloads so please help me with CPU frequency drivers or power related drivers of FreeRTOS. If these driver source is available in any of the downloads for any controllers Please do inform me,please do the needful soon.

Thanks and Regards
Abhijith N.M.

The FreeRTOS core doesn’t do anything like that, as the core is designed to be platform independent. The Port layer has all the processor specific stuff that is needed to support the core, which will include sample code to initialize some counter to be used as the system tick, and that code tends to assume a constant frequency into the counter.

If you want to change the processor frequency, that would need to be in your application code, likely with help from routines provide by the chip manufacturer. If you do change the clock frequency, you probably want to adjust things so that your tick frequency stays constant, either by adjusting the parameters to it, or setting up the tick to run off of some constant clock other than the processor clock (maybe a real time clock).

You also may need to worry about any peripherals (like serial ports) that use clocks to make sure they keep running at the right frequency.

One BIG piece of information that anyone trying to help you will need is what processor you are planning on using, as what you are talking about is very processor specific.

Hello Richard,

That’s a great Response thanks for the information, I am planning to work on BeagleBone Black latest version processor : AM335x 1GHz ARM® Cortex-A8,
Raspberry PI 4 processor : Broadcom BCM2711, Quad core Cortex-A72 (ARM v8) 64-bit SoC @ 1.5GHz

In Linux we have the flexibility to change the operating frequency for a particular task running on board, I am looking to control the tasks in FreeRTOS the same way. So that it results in Less energy consumption.I believe System Clock needs to be changed to handle the tasks which results in less energy consumption for the task. On selecting Beagle Bone Black latest version.
Please let me know whether we change the System Clock and Tick Interrupt clock by changing configCPU_CLOCK_HZ and configPERIPHERAL_CLOCK_HZ present in FreeRTOSConfig.h and, by default, map to the ICLK_FREQUENCY and PCLK_FREQUENCY.

If I can specify values to configCPU_CLOCK_HZ then I should be able to modify System Clock as per my understanding, correct me if am wrong.
I will make sure configPERIPHERAL_CLOCK_HZ is set to some other clock so that its frequency remains constant.
But if I need different instances of Clock can I have multiple instances of configCPU_CLOCK_HZ with different names ?

In our project we specify different Algorithms which sets the operating frequency to different Clocks for a particular task and hence power consumption reduction is achieved in LINUX I would like to do the same here in FreeRTOS. So please do help me how I could achieve it in FreeRTOS.

Thanks and Regards
Abhijith N.M.

configCPU_CLOCK_HZ and configPERIPHERAL_CLOCK_HZ are used by the provided startup code to initialize the system to your specifications (when supported by the particular port layer). They are also sometimes used by the application to interrogate the system configuration. If you plan to dynamically change the numbers, then these macros should probably point to global variables with the current value (or functions that return them). Just changing them will not magically change the processor configuration, but you will need to provide some routine (perhaps using code from the processor manufacturer) to change the settings.

Generally, changing the speed of the processor clock isn’t an instantaneous operation, taking a bit of time, (especially if changing a PLL), and also the scheduler will be running at the speed of the task being switched out, so you generally don’t want to drop the processor speed just because you switched to a ‘slow’ task for a short while. Generally things like Linux will check if a important task hasn’t run for a while (like seconds) and drop the clock when the usage seems low, and because it doesn’t have ‘Real Time’ requirements, if it is slow to switch back that is ok.

When I have done this sort of thing in FreeRTOS, this sort of clock changing is very much an APPLICATION side decision, it knows when it is starting up some operation that needs the higher processor speed, and it then does the clock change and then starts the operation that needs the speed (and generally the real time requirements aren’t in STARTING that operation, but in performing that operation). If the tick timer is running off the peripheral clock, FreeRTOS basically doesn’t care about the CPU clock.

As was said, I would highly try to avoid changing the speed of the peripheral clocks, as that often says that many devices need to be reconfigured, and often any transfers in progress get mangled. I have built a system where when putting the processor into a reasonably deep sleep required changing the peripheral clock, and that required having tentacles into all the I/O systems to confirm that nothing was being transferred so it was ok to change the clock, and then everything got reconfigured after the clock.

Hello Richard,
Thanks for the detailed explanation , I am thankful for your time,My main aim is to use DVFS operation in FreeRTOS, DVFS operation mainly involves switching the operating Frequency depending on the Period of execution of task and Frequency of occurrence of the task. Depending on these two parameters calculation is done and the frequency is selected for a particular task. Now I am trying to Implement the same DVFS operation for FreeRTOS. So please do inform me whether I can achieve the DVFS operation (the frequency scaling operation) in FreeRTOS. I will get back with queries in your previous explanation but I would like to know whether we achieve this DVFS operation in FreeRTOS.

If So please provide me the source of FreeRTOS using DVFS technique or an example code Illustrating the DVFS technique used in FreeRTOS.

Thanks and Regards
Abhijith N.M.

As I have said, FreeRTOS itself won’t handle this. This sort of operation is VERY machine dependent, and most of FreeRTOS is designed to be machine independent, with a small machine dependent piece pulled in from the appropriate directory in the portable tree. These files will have the code for do things like saving/restoring the registers for task switching, disabling interrupts for critical sections, the default initial setup of the machine (the timers) and sometimes a default implementation of tickles idle.

The concept of adjusting the machine operating frequency based on load is an advanced topic, and is very machine and application dependent. One big thing to remember is that FreeRTOS is designed to make it easier to meet ‘Real Time’ requirements, and meeting these requirements gets trickier when you need to factor in possible changes in the machine speed, so changes of this sort need to fall upon the application, not the operating system. Note that Linux is a very different sort of system, one that generally DOESN’T deal with hard real-time requirements, but is more apt to be measured at being ‘responsive’, so is more able to monitor how the machine is working and try to optimize machine speed to meet the work load.

There is nothing built into FreeRTOS to do anything like dynamically altering the speed of the processor based on load. There is also nothing in FreeRTOS that prevents YOU from adjusting the processor speed based on your known load (or measuring the load), provided you keep the tick rate approximately constant (and actually, since the FreeRTOS Core does everything in terms of ticks, it doesn’t really need the tick rate to be constant, it just makes programs easier to control there timing).

I have done some programs doing a bit of this, certain tasks that only occasionally needed to be active, would communicate with a CPU clock sub-task that would adjust the clock based on the highest speed currently needed. Something like that is not that hard to do, though these were fairly course steps.

Hello Richard,
I am trying to setup the FreeRTOS windows Simulator to execute my project.
Its showing below attached errors I have tried everything on Internet to fix the problem. But its not getting resolved.

On Building:
Cannot create temporary file in C:\Users\abijn\AppData\Local\Temp;C:\MinGW\bin\gcc: Invalid argument

On Run:
Launch Failed Binary not created.

Please suggest me possible solutions for the snapshots attached in this request.
Please do the needful.


Thanks and Regards
Abhijith N.M.

The ‘On Run’ error is simple, the build didn’t complete so there isn’t anything TO run.
The ‘On Building’ error looks like a tool setup issue. The path C:\Users\abijn\AppData\Local\Temp;C:\MinGW\bin\gcc isn’t anything like a proper path, especially for a temporary file. It seems like somehow it is appending the path to GCC to the temp file path (could your temporary file environment variable have gotten messed up?). This really isn’t a FreeRTOS error, but a tool setup issue.

Hello Richard,
I am looking for Dynamic Volatge Frequency Scaling implementation for FreeRTOS, please provide me any source which helps me in the code implementation of DVFS for free RTOS.
Or any data which exactly refers to switching the CPU frequency with respect to tasks.
Please do the needful.

Thanks and Regards
Abhijith N.M.

Many aspects of variable clock speed have already been handle in this interesting post.
What I miss is to mention tickless idle configUSE_TICKLESS_IDLE. That is another perfect way of saving energy and to avoid heath production.
Tickless idle means that the clock-tick while idle will be stretched out so that the MPU will get into a sleep state.

I had a TCP application running on a LPC part. It would make sleeps of e.g. 30 seconds.
Now if your part needs to be active constantly, tickless idle is not for you.

One minor remark about variable CPU speeds: I was once using an SD card under Linux, and for unknown reasons the transfer could stall at any moment. This bug costed me many days to solve.
It turned out that DMA would stall as soon as frequency scaling became active. Make sure that won’t happen in your part.

Hello Richard,
You mentioned in the last post that you have built a system where certain tasks occasionally need to be active with a CPU clock SUB-task that would adjust the clock based on the highest speed currently needed. Please provide me inputs on how you built this system. I would like to know whether I can apply same techniques to my project requirement.
My email id: abimsstudy@gmail.com , You can mail me as well to provide me any inputs.
Please do the needful.

Thanks and Regards
Abhijith N.M.

I am not able to provide the code I used to do this, as it was used for projects at work, and is considered company proprietary. I can describe some of the general principles.

First, let me point out that everything in FreeRTOS doesn’t need to be a ‘Task’, my codebases have many modules that aren’t designed as being a task, but as a library function that many tasks might call, this is very true of most I/O device drivers. (Some drivers may have a task to handle the communications processing like an ethernet or USB driver). The code in that driver handles any mutual exclusion needs for requests from multiple tasks, and tries to present a ‘clean’ interface to the calling task.

My clock speed adjustment routine works on a similar basis. It is a module which various tasks might call to indicate what speed they need to run at. If a task asks for a speed higher than the machine is currently running at, then the system will adjust the clock speed to that new speed and then return. A task can drop its requested speed, and then the driver will see if any other tasks have also asked for this higher speed, if so it will stay at this speed, otherwise it will drop the speed to the next highest speed requested.

Of course to do this requires the driver to remember what speed each task has requested, which could be as simple as an array of Task Handles / Requested Frequency that you search for, or you can get the Task Number of the task and use that as an index into an array.

Note, that this functionality actually has little to do with FreeRTOS itself and is more of a broad application-level functionality, that may happen to use some information about tasks.

Hello Richard,
That’s a great response. I will get back on your explanation.I respect your time and help. I am looking t integrate C and C++ code in Visual studio. I am launching windows Simulator of freeRTOS in Visual studio. I need to integrate C source code of FrEETOS with my C++ source code, I have tried all the possibilities of using.

  1. For header file.
    #ifdef __cplusplus // defined at least by G++, I don’t know for other compilers
    extern “C” {
    #endif
    #ifdef __cplusplus
    }
    #endif

  2. C++ source code
    extern “C” {
    }

For both the try I end up with error
C2016: Syntax error: identifier “class name”

C-file:
#include <stdio.h>
#include <conio.h>
#include “test1.h”

int main()
{
code();
return 0;
}

C++ header file:

#pragma once
#ifndef TEST1_H
#define TEST1_H
#ifdef __cplusplus // defined at least by G++, I don’t know for other compilers
extern “C” {
#endif

class test1 {
test1();
~test1();
public:
int code();
};
// … function definitions here
#endif //TEST1_H
#ifdef __cplusplus
}
#endif

C++ source file:
#include <stdarg.h>
#include “test1.h”

int test1::code(void)
{
return 0;
}

I end up with error
C2016: Syntax error: identifier “class name”

I am not able to call a Class member function in C source code.
Please help me resolve this problem.

Hello FreeRTOS team , Richard has been helping me out with some queries which is great.
I would like any response from your side, since I have not received any response from FreeRTOS team in this thread , But I am thankful to Richard.

Please do the needful.

Thanks and Regards
Abhijith N.M.

This has nothing to do with FreeRTOS. It’s basic C/C++ programming.
I afraid it’s your turn to sort this out :wink:

A simple answer is that C doesn’t have classes, so extern “C” doesn’t make the contents valid C code, so no calling class member functions.

For a C++ function to be callable by C code (by name) it needs to be an ordinary function, and the prototype for that function should exist within an extern "C block that the C++ compiler sees before the actual definition, so that it is created as a callable by C function.