Understanding the RTOS Kernel: A Guide for Beginners

Follow+Star Public Account Number, don’t miss exciting content

Understanding the RTOS Kernel: A Guide for Beginners

Author | strongerHuang

WeChat Public Account | strongerHuang
I recently saw a discussion in a technical group about whether to read the RTOS kernel source code. From the perspective of someone who has been there, I would say: it is not necessary, I do not recommend reading the RTOS kernel source code.
When I was in college, I enjoyed studying the underlying technical principles, such as digital tubes, LCDs, 74 series logic ICs, etc., curious about how they work and how they are implemented, and I would write some code to test and verify the principles.
Later, I learned some protocol stacks and RTOS, and I was curious and would spend time studying the source code. It wasn’t until I deeply read and understood the RTOS kernel source code that I realized it was not an easy task.

My Experience Learning RTOS

Let me first share my experience learning RTOS.
I started learning RTOS in college, and like many others, I was clueless, following what the teacher taught and what those around me were learning, such as digital electronics, analog electronics, microcontroller programming, peripherals, etc.
After learning various resources and peripherals for bare-metal microcontrollers, I felt it was time to level up so I chose RTOS.
In 2011 and 2012, when I was studying, RTOS was not as popular as it is now (at that time it was still mainly “bare-metal”), and most online tutorials on RTOS were still based on μCOS, with relatively few resources on other RTOS like FreeRTOS and RT-Thread.
Because early μCOS releases came with companion books (see the story of µC/OS), many teaching materials chose μCOS, which is why I chose to learn μCOS.
(By the way, although early μCOS was a commercial operating system, the source code was available for free download.)
1. Choosing a Microcontroller Development Board
My early learning of microcontrollers was mainly with 51 (AT89C51, STC89C51, etc.), MSP430, etc., so I was relatively familiar with them.
So I started learning μCOS based on these, because everyone knows that 51 resources (Flash, RAM) are indeed too limited; although there are online resources for porting μCOS to 51 microcontrollers, after porting the system, there is basically nothing else that can be done.
So, I struggled for a while porting μCOS to 51 and then gave up, choosing MSP430 instead, which has more resources than 51 and allows for more flexibility.
If there are students who learned to port μCOS on MSP430 back then, they might have seen my early shared source code:
Understanding the RTOS Kernel: A Guide for Beginners
After MSP430, I used STM32xx, LPC17xx, and other 32-bit microcontrollers based on the ARM Cortex-M3 core. At that time, running μCOS on MSP430 felt quite challenging, so I later chose STM32 to run μCOS.
By the way, let me recall the STM32 development board:
Now the STM32 development board market is mainly dominated by three companies: Point Atom, Anfu Lai, and Wildfire. In earlier years, there was a development board from Shenzhou Development, which was the most popular online at that time, mainly due to its high cost-performance ratio.
Understanding the RTOS Kernel: A Guide for Beginners
At that time, a board like the Shenzhou III based on STM32F103ZE was a very resource-rich development board for just over a hundred. Although I was relatively poor at the time, I still couldn’t resist the temptation and bought Shenzhou I, and later bought Shenzhou III.
Unfortunately, the Shenzhou Development Board team stopped production after 2015, and I wonder how many people still remember this?
2. Reading the μCOS Kernel Source Code
I came into contact with the μCOS operating system in 2011, and at that time, I was also referencing various examples and porting code, observing various phenomena.
Although the system ran successfully, and the kernel resources were utilized, I still could not understand various principles and could not grasp the essence of real-time operating systems.
So, I began the journey of reading the source code, and I found it was not simple. Because the operating system contains various pointers, arrays, structures, etc., and at that time, the teacher had never taught us data structures, so I could only explore step by step on my own.
I spent about a year reading the μCOS operating system kernel source code and kernel resources, and only then did I deeply understand how RTOS works, and I also understood the scheduling principles and communication mechanisms of the μCOS kernel.
If there were students who learned μCOS early on, they might have seen my early shared source code based on Shenzhou III and the uCOS2.92 system, which included various examples such as multitasking, semaphores, mutexes, event flags, message mailboxes, message queues, and memory management:
Understanding the RTOS Kernel: A Guide for Beginners
This source code has Chinese comments, which I translated step by step when I was learning μCOS. At that time, I put in a lot of effort to learn μCOS.
I recorded a simple video about this source code:

Is it necessary to read the source code to learn RTOS?

After seeing my experience above, you might feel an urge to read the kernel source code.
In fact, I do not recommend reading the source code for many people, especially for two types of people:
  • Those with weak fundamentals
  • Those with no time
Before learning RTOS, I had tinkered with a lot of source code and participated in electronic design competitions, and I considered my fundamentals to be decent. The key was that during college, I had a lot of time.
If your fundamentals are weak, and you don’t have much free time, and you want to learn RTOS, reading the source code must be done with caution, caution, and more caution.
For the vast majority of readers, my suggestion is: directly refer to examples, and then tinker with the various APIs of the operating system, understanding their functions and principles through status lights and serial printing outputs.
For example: after creating a task and deleting it, observe whether the status light is still executing this task.
What are the benefits of reading and understanding the kernel source code?
You may ask: If I do not recommend reading the source code, does that mean reading the source code is useless?
Reading and understanding the source code can actually be very beneficial, such as further understanding various communication mechanisms of RTOS for later application programming, and it can also enhance your programming thinking. Many of my later projects borrowed patterns from some source code.
Finally, I want to emphasize that RTOS kernels have some relatively complex content. If your fundamentals are weak, you may give up after a few days of reading. Also, if you have no time, just fishing for three days and sunbathing for two days might yield no results.

So, I do not recommend reading the source code for the vast majority of people.

———— END ————
Understanding the RTOS Kernel: A Guide for Beginners
● Column “Embedded Tools”
● Column “Embedded Development”
● Column “Keil Tutorial”
● Selected Tutorials from the Embedded Column
Follow the public account reply “Join Group” to join the technical group according to the rules, reply “1024” to see more content.
Understanding the RTOS Kernel: A Guide for Beginners
Understanding the RTOS Kernel: A Guide for Beginners
Click “Read Original” to see more shares.

Leave a Comment

Your email address will not be published. Required fields are marked *