Task scheduler

Thread Starter

Pushkar1

Joined Apr 5, 2021
416
I am reading the official free pdf book and API documentation for FreeRTOS.

I have been reading following page
https://www.freertos.org/a00132.html

I don't understand how to use vTaskStartScheduler(). I can create the tasks but as I understand, Only creating task is not sufficient. Tasks will be executed only when the scheduler is started.

Example usage:

C:
void vAFunction( void )
{
     // Tasks can be created before or after starting the RTOS
     scheduler
     xTaskCreate( vTaskCode,
                  "NAME",
                  STACK_SIZE,
                  NULL,
                  tskIDLE_PRIORITY,
                  NULL );

     // Start the real time scheduler.
     vTaskStartScheduler();

     // Will not get here unless there is insufficient RAM.
}
 

Dave Lowther

Joined Sep 8, 2016
225
I've never used FreeRTOS but I think I can answer your question based on using other RTOS's over the years.
Your understanding is correct.
Creating tasks will just create some entry in a data structure used by the scheduler.
When your code calls vTaskStartScheduler() your code loses control and hands control of task execution to the scheduler.
This is confirmed by the comment "Will not get here unless there is insufficient RAM"
After calling vTaskStartScheduler() the scheduler will run your tasks based on priority, whether they are blocked, etc.
 

Thread Starter

Pushkar1

Joined Apr 5, 2021
416
After calling vTaskStartScheduler() the scheduler will run your tasks based on priority, whether they are blocked, etc.
Thank you! @Dave Lowther

Let's say task and priority is something like
MyTask1, MyTask2 and MyTask3 with priorities 1,2 and 3 respectively.

When i call function vTaskStartScheduler() after creating tasks scheduler run each task according priority

I have three task How to know which task is running for how long?

If i want to run the task like, first for 30 ms, second t for 60 ms and third for 90ms then how to do in FreeRTOS
 

Dave Lowther

Joined Sep 8, 2016
225
I have three task How to know which task is running for how long?
I don't know enough about FreeRTOS to give you detailed specific answers.
If you want to measure how long each task is running for you could perhaps hook into, or temporarily edit, the scheduler to outpupt something on some spare port pins. You could then observe those pin states on an oscilloscope or logic analyser.
If you want to define in your code how long each task runs for then that's a different topic and I think that would require more kowledge of the Free RTOS API than I have.
 

ericgibbs

Joined Jan 29, 2010
18,872
hi P,
On this video which deals with a practical example of RTOS in a ESP32 it explains the timing and priority.
Approx 12minutes in from the start it covers this aspect.
E
 

Thread Starter

Pushkar1

Joined Apr 5, 2021
416
hi P,
On this video which deals with a practical example of RTOS in a ESP32 it explains the timing and priority.
Approx 12minutes in from the start it covers this aspect.
i have seen the video. I came to the conclusion that the tasks are created and then the scheduler is started. When there is a different priority, the task scheduler executes the higher priority task first. My doubt is, when the task priority are same, how does the scheduler execute the tasks in FreeRTOS?
 

Papabravo

Joined Feb 24, 2006
21,228
In general any convenient strategy could work. You can avoid this if you have more priority levels than tasks. If not, you might have a queue for each priority level and always schedule the task on the top of the queue. This would be the task of a given priority that has waited the longest to run.
 

michael8

Joined Jan 11, 2015
415
when the task priority are same, how does the scheduler execute the tasks in FreeRTOS?

https://www.freertos.org/RTOS-task-priority.html

Any number of tasks can share the same priority. If configUSE_TIME_SLICING is not defined, or if configUSE_TIME_SLICING is set to 1, then Ready state tasks of equal priority will share the available processing time using a time sliced round robin scheduling scheme.
 

Thread Starter

Pushkar1

Joined Apr 5, 2021
416
when the task priority are same, how does the scheduler execute the tasks in FreeRTOS?

https://www.freertos.org/RTOS-task-priority.html
Thank you @michael8

I'm still trying to figure out some facts. Let's say I have created tasks and set tasks priority as follows

T1 runs once for 8ms with Priority 2
T2 runs twice for 5ms with Priority1
T3 runs three times for 10ms with Priority3

How can be achieved this type of behavior through FreeRTOS?
 
Last edited:

michael8

Joined Jan 11, 2015
415
I've never used FreeRTOS but I've written similar things several times.

Looking at the FreeRTOS documentation the concept of a task seems to be that it generally exists forever. The concept
of "run for 8 ms" doesn't really exist in FreeRTOS.

What are you really trying to do? How much experience with C and assembler do you have?

At least to me, the concept that the RTOS just runs the highest priority ready task says it all. It implies
(as the doc says) that if a higher priority task becomes ready the RTOS will switch which task is running.
 

Papabravo

Joined Feb 24, 2006
21,228
In a preemptive scheduler you cannot guarantee that a scheduled task will be allocated a fixed amount of uninterrupted runtime. However, you can elevate a task's priority so that it will always be the highest priority task for some period of time. After that happens you can return the task to it's ordinary priority level. It may or may not be possible to do this in FreeRTOS. Doing this has serious implications for meeting the systems real time requirements and must be done with care. IMHO you are trying to run a marathon before learning to crawl and walk.
 

Thread Starter

Pushkar1

Joined Apr 5, 2021
416
What are you really trying to do? How much experience with C and assembler do you have?
I want to learn how to write code for an embedded system with real time operating system technique. I am aware that I do not really need a RTOS to develop embedded system, but I want to increase my skills. I don't think there is any harm in learning RTOS programming. I am good at c programming and use only traditional c method for my embedded project. I don't have experience with assembly.

That's why I am playing with FreeRTOS and ESP32. So far I have completed the LED flashing task. I am studying the reference manual and sample codes
 

nsaspook

Joined Aug 27, 2009
13,315
I want to learn how to write code for an embedded system with real time operating system technique. I am aware that I do not really need a RTOS to develop embedded system, but I want to increase my skills. I don't think there is any harm in learning RTOS programming. I am good at c programming and use only traditional c method for my embedded project. I don't have experience with assembly.

That's why I am playing with FreeRTOS and ESP32. So far I have completed the LED flashing task. I am studying the reference manual and sample codes
Good. I would suggest you concentrate reading about RTOS and general OS fundamentals for a while longer instead of jumping too much into sample codes.


1631378896761.png
 

michael8

Joined Jan 11, 2015
415
I don't have experience with assembly.

I think you at least need to understand some parts of assembly and the hardware to understand an operating system.

Consider you have something running on a typical micro which has a stack for subroutine calls and interrupts.
How do you save it's current registers and instruction counter and run a different piece of code with a different
stack? This is way the OS does along with deciding which to run and more.
 

Thread Starter

Pushkar1

Joined Apr 5, 2021
416
I think you at least need to understand some parts of assembly and the hardware to understand an operating system.
Yes, I have heard that assembly code is faster than c code. That's why programmers prefer assembly language to design real time operating systems. I started learning assembly language few months ago I wrote few basic programs in assembly and since then I am working in the c language. I am finding assembly very difficult then c language.
 

michael8

Joined Jan 11, 2015
415
You need to know some of the assembler not because it's faster, but because it can include operations you can't do from C.

Like switch the stack to a different stack or issue some special instructions which C doesn't have (enable/disable interrupts,
hardware interlocked memory access, hardware sleep/wait, more...)

So it's not really learning assembler but rather what the OS is doing to enable it to work (which isn't include in
general C capabilities). And knowing your hardware and what it can do...

It's possible to write an OS in C with some special assembly functions to do the non-C stuff (linux, FreeRTOS).

https://en.wikipedia.org/wiki/ESP32 says:

The ESP32 series employs either a Tensilica Xtensa LX6 microprocessor in both dual-core and single-core variations, Xtensa LX7 dual-core microprocessor or a single-core RISC-V microprocessor.

Which ESP32 processor are you using?
 

click_here

Joined Sep 22, 2020
548
The Linux kernal is written in C (with GNU extentions) with *some* assembly code.

When looking at Scheduling there are lots of ways you can do it.


I suggest that you start with a simple FIFO task scheduler using a queue as a starting point. There is no reason that you can't do that entirely in C.

If you haven't already - Before making a task scheduler I would suggest that you start with learning about abstract data types in C first. Things like:
  • Single-linked Lists
  • Double-linked Lists
  • Circular Lists
  • Stacks
  • Queues
  • Priority Queues
  • Deques
  • Heterogeneous Structures and Object Trees
  • Binary Trees
  • Balanced Binary Trees
  • Tries
  • Hash Tables
  • Sparse Matrix

... And write at least one program for each one.
 
Top