source code for a basic Preemptive RTOS

Thread Starter

bug13

Joined Feb 13, 2012
1,791
Hi guys

Just wondering if anyone came across a very basic source code for Preemptive RTOS (for PIC ideally, but any will do), for learning purpose. I don't need any fancy features. It doesn't need to be fast, it doesn't need to be small in size (flash or data ram).

All I need is it works, and easy to understand. So I can learn how it works in a real example.

Thanks guys
 

Thread Starter

bug13

Joined Feb 13, 2012
1,791
I did have a look a long long long time ago, thing may have changed now. What I found at the time was there were simply too much features there for my learning purpose.

I hoping to find someone have done something very basic, maybe something like only have task switching. I want to know how they actually switch task by interrupt the current task (timer interrupt??), then switch the context of the stack to anther task.

That's properly all I want to know. The above is my understand on how it may work, it very possible that it could be wrong.
 

jfitter

Joined Mar 14, 2019
14
There is no such thing as a "basic" preemptive RTOS. If you are happy with non-preemptive you can do it yourself and learn a lot; just write a scheduler and define some macros. Preemptive RTOS is a whole level above this, and if your micro does not provide it, you will need to provide a system tick timer.
Free RTOS is a good place to start. I do find CHIBIOS even easier to follow so this may suit you better.
Learn the basics, write some code and get it to work, THEN delve into the bowels of the RTOS to see how it works.
Just a suggestion, but worked for me....
 

Thread Starter

bug13

Joined Feb 13, 2012
1,791
Why do you need a RTOS?
To be honest, I don't think I need a RTOS. If I have to give a couple reasons, they could be:
  • I am simply want to learn how it may work, and write my own RTOS, just for fun.
  • And I just saw a job ad I like today that they want someone with RTOS experience (this promote me want to learn about RTOS, again:)).
 

Thread Starter

bug13

Joined Feb 13, 2012
1,791
There is no such thing as a "basic" preemptive RTOS. If you are happy with non-preemptive you can do it yourself and learn a lot; just write a scheduler and define some macros. Preemptive RTOS is a whole level above this, and if your micro does not provide it, you will need to provide a system tick timer.
Free RTOS is a good place to start. I do find CHIBIOS even easier to follow so this may suit you better.
Learn the basics, write some code and get it to work, THEN delve into the bowels of the RTOS to see how it works.
Just a suggestion, but worked for me....
Cool, I will take a look at CHIBIOS.

I am writing my own task scheduler in most of my code. Some time I wish my tasks can be a bit more deterministic, not a problem so far tho.
 

jfitter

Joined Mar 14, 2019
14
You say you are writing your own task scheduler. Is this preemptive or non-preemptive? The difference is huge.
In the dark old days I used to write my own schedulers, until I got wise and figured that people do just this for a living and their schedulers are better than mine. I was writing PIC code at the time so invested in SALVO which worked very well for me for quite a few commercial jobs. It was so small and lean you could put it on almost any PIC. It is non-preemptive and is essentially a large library of macros and a scheduler core.
These days I use ARMs, mostly STM32s for motor control, and either FreeRTOS or CHIBIOS with occaisional foray into NUTTX. For many jobs this is blatant overkill, but who cares? The chips are as cheap as 8 bitters, the development system is open source, they perform better, and for many jobs you can pretend you are writing code for your PC: unlimited resources (so there are 4500 pages of documentation, but you don't have to read it all at once) :)
 

djsfantasi

Joined Apr 11, 2010
5,956

Thread Starter

bug13

Joined Feb 13, 2012
1,791
You say you are writing your own task scheduler. Is this preemptive or non-preemptive? The difference is huge.
In the dark old days I used to write my own schedulers, until I got wise and figured that people do just this for a living and their schedulers are better than mine. I was writing PIC code at the time so invested in SALVO which worked very well for me for quite a few commercial jobs. It was so small and lean you could put it on almost any PIC. It is non-preemptive and is essentially a large library of macros and a scheduler core.
These days I use ARMs, mostly STM32s for motor control, and either FreeRTOS or CHIBIOS with occaisional foray into NUTTX. For many jobs this is blatant overkill, but who cares? The chips are as cheap as 8 bitters, the development system is open source, they perform better, and for many jobs you can pretend you are writing code for your PC: unlimited resources (so there are 4500 pages of documentation, but you don't have to read it all at once) :)
My schedulers are very simple, they are non-preemptive. Very similar to the proto-threading linked above. But I do add the feature that I can add a task, and/or remove a task from the scheduler as required etc...

I do see using a RTOS can save a lot of time. Which STM32 do you usually use with RTOS? Are STM32 well supported by FreeRTOS? or do you need to port the FreeRTOS to the stm32 you use?
 

jfitter

Joined Mar 14, 2019
14
FreeRTOS does support STM32 as does CHIBIOS. The suppport is easy to implement and there are several ways. You can do it the way of the pros and use STM's tools and libraries. It is really well supported by STM and once you set it up it is easy. I use Eclipse IDE and there are many internet resources to get you started. For quick and nasty development you can buy the Blue Pill boards for a couple of bucks each - like an Arduino but with an STM32F103, making it an Arduino on steroids. An Aussie guy has done a FreeRTOS v9 port to these boards. All you need to do is load a new bootloader which makes them look like a LeafLabs board. Everything is on the web - docco, bootloader, instructions, etc. This is a seriously FAST way to get into FreeRTOS development.
FreeRTOS can be preemptive or non-preemptive, but if you choose the correct memory management model you are unlikely to get into strife with preemptive tasking, so there is no good reason to make compromises. With respect to application complexity, I hear regularly the comment that the application is too simple for an RTOS so let's roll our own. Bad decision!!!
Really smart guys write really good RTOSs. I say let them. I will focus my efforts on my application.
Unless I intend to make a million of my product, I am not really too concerned about choosing the processor that just barely is capable of running my application. I make low volume, high value, specialized products. I will specify a processor big enough, with plenty of grunt, and lots of resources, to make my programming task comfortable and as bug free as possible, and without having to resort to sneaky tricks to get stuff done.
Plenty of resources, a good RTOS, and plain vanilla programming = robust product.
 

Thread Starter

bug13

Joined Feb 13, 2012
1,791
@jfitter I am interested in what you use CHIBIOS for and what you FreeRTOS for.

Do you use STM32 (STM32F0??) with CHIBIOS mainly to replace 8-BIT MCU, and you use FreeRTOS for more complicated job?
 

jfitter

Joined Mar 14, 2019
14
@jfitter I am interested in what you use CHIBIOS for and what you FreeRTOS for.

Do you use STM32 (STM32F0??) with CHIBIOS mainly to replace 8-BIT MCU, and you use FreeRTOS for more complicated job?
Well I pretty much gave up on 8 bitters years ago except where I have a really good reason to use them. The newer 32b processors do more, faster, and have more resources, all for the same price. Once you get over the learning curve you won't go back. And there are better development tools.

I use an RTOS whenever possible, even for quite trivial jobs. If I just want to flash a LED, use an RTOS. Why not? I have resources to spare so why get greedy. The code I have to write is simpler, easier to understand, and more likely to be bug free. How hard is it?

Make a loop, make a counter, keep track of the counter, switch the LED, etc.
vs Toggle the LED, wait a while

The electrical design is not particularly more difficult - a chip, a clock, a reset - not much more than an 8b cpu. Most of the chips have lots more pins but you are not obliged to use them all.

As for CHIBIOS vs FreeRTOS - I think both are equally capable. I do find that CHIBIOS allows for code that is easier to read, at least for me it is. FreeRTOS I find a bit harder to follow. Either is just as easy to link with your code. I use Eclipse and MSVS with both RTOSs and, usually, Gnu C++.

For some insight into my thinking. A customer wanted an "egg turner". It a set of rollers driven by a dc motor and a belt, the whole shebang enclosed in an incubator. You put eggs on the rollers and every so often the motor starts and turns the eggs over, half a turn. It does this for a number of weeks, after which presumably the chickens hatch. Timing does not need to be accurate but you do need to count to rather large numbers. An 8pin 8bit PIC and a MOSFET did the job perfectly, and cheaply. Score 1 for the 8 bitters.

My current job is an in-house development of a dynamic balancing machine for small jet turbines. The hardware has a lot of stuff to do and the UI BT'ed to a PC or Android app. I started with an 8 bit cpu but there was too much maths to do and too many tasks to do to sensibly keep track of what was going on. The extra maths meant that the Android had to do it, which was asking a lot of it (glorified telephone), and the comms bandwidth went too high as well. Now it is an STM32 and FreeRTOS. All the tasks are now under control and easy to manage. The maths is done too, blindingly fast, and so only the answers need to be sent to the host. The PC and the Android are just a GUI and don't have to do much thinking. All this for the same hardware cost as an 8bit solution.

We may make a few thousand of these. If so my new address is somewhere in the Bahamas.
If I planned to make a million of them my design solutions would be quite different.
 

BR-549

Joined Sep 22, 2013
4,938
I am not a programmer, but with the cheap sophistication, low power and memory that can be stamped out, jfitter's programming strategy will be very common in the future.
 

nsaspook

Joined Aug 27, 2009
6,720
I am not a programmer, but with the cheap sophistication, low power and memory that can be stamped out, jfitter's programming strategy will be very common in the future.
Viva La Future. That's exactly what every manufacturer of 32-bit controllers wants.
 

BR-549

Joined Sep 22, 2013
4,938
It's what the market wants. Especially with 5G and the IoT coming.

8 bit will be like tubes. A biplane. A boat anchor.

Just a guess.
 
Top