You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: documentation/basic/basic.md
+21-30Lines changed: 21 additions & 30 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,4 +1,4 @@
1
-
#Kernel Basics
1
+
@page kernel_basics Kernel Basics
2
2
3
3
This chapter gives a brief introduction to the software architecture of the RT-Thread kernel, beginning with its composition and implementation. While also introducing RT-Thread kernel-related concepts for beginners.
4
4
After understanding this chapter, readers will have an elementary understanding of the RT Thread kernel and will be able to answer questions such as -
@@ -10,15 +10,7 @@ After understanding this chapter, readers will have an elementary understanding
10
10
11
11
In the nutshell, this is only a brief introduction to software architecture decomposition and implementation of the real-time kernel. This will give understanding and concepts of how RT-Thread kernel works togther. After learning from this chapter, readers will have basic knowledge of each kernel components, system booting up proccesses, memory allocation and distrubtion, and methods of kernel configuration.
12
12
13
-
## **Table of Contents**
14
-
15
-
1.[Introduction to RT-Thread Kernel](#introduction-to-rt-thread-kernel)
Kernel is the most basic and fundenmental part of an Operating System. Kernel service library and RT-Thread kernel libraries are interfacing between hardware and components/service layer. This includes the implementation of real-time kernel service library (rtservice.h/kservice.c) and other RT-Thread kernel libraries such as object management, thread management and scheduler, inter-thread communication management, clock management and memory management respectively. Below diagram is the core architecture diagram of the core kernel.
24
16
@@ -30,7 +22,7 @@ Implementation of core kernel libraries are similar to a small set of standard C
30
22
The built of the Kernel will be vary depending on the complier. For example, using GNU GCC compiler, it will use more implementation from the standard C library. Last but not least, the minimum resource requirements of the Kernel is 3KB ROM and 1.2KB RAM.
31
23
32
24
33
-
###Thread Scheduling
25
+
## Thread Scheduling
34
26
35
27
Thread is the smallest scheduling unit in the RT-Thread operating system. The thread scheduling algorithm is a **Priority-based Full Preemptive Multi-Thread** scheduling algorithm.
36
28
The system can support up to 256(0 - 255) thread priorities. For systems with tight resources, configurations with 8 or 32 thread priorities can be chosen(For example, STM32 has 32 thread priorities as per the default configuration). Lower numbers have a higher priority where 0 represents the highest priority furthermore the lowest priority(highest number) is reserved for idle threads.
@@ -39,7 +31,7 @@ The number of threads is bounded by the memory of the hardware platform and not
39
31
40
32
Thread management will be covered in detail in the "Thread Management" chapter.
41
33
42
-
###Clock Management
34
+
## Clock Management
43
35
44
36
RT-Thread's Clock management is based upon a **clock beat**, which is the smallest clock unit in the RT-Thread operating system.
45
37
The RT-Thread timer provides two types of timer mechanisms:
@@ -52,7 +44,7 @@ The timer service is concluded using a timer timing callback i.e. a timeout func
52
44
53
45
Timer will be explained further in the "Clock Management" chapter.
54
46
55
-
###Synchronization between Threads
47
+
## Synchronization between Threads
56
48
57
49
RT-Thread uses thread semaphores, mutexes, and event sets to achieve inter-thread synchronization.
58
50
Thread synchronizations happen through the acquisition and release of semaphore and mutexes.
@@ -61,13 +53,13 @@ Event sets are primarily used for synchronization between threads, they can achi
61
53
62
54
The concepts of semaphores, mutexes, and event sets are detailed in the "Inter-Thread Synchronization" chapter.
63
55
64
-
###Inter-Thread Communication
56
+
## Inter-Thread Communication
65
57
66
58
RT-Thread supports communication mechanisms such as mailbox, message queue, etc. The mailbox's message length is fixed to 4 bytes. Whereas, message queue can receive messages in variable size and cache the messages in its own memory space.
67
59
Compared to a message queue, a mailbox is more efficient. The sending action of the mailbox and message queue can be safely used in an ISR (Interrupt Service Routine). The communication mechanism allows threads to wait by priority or to acquire by the First In First Out (FIFO) method.
68
60
The concept of mailbox and message queue will be explained in detail in the "Inter-Thread Communication" chapter.
69
61
70
-
###Memory Management
62
+
## Memory Management
71
63
72
64
RT-Thread allows:
73
65
1. Static Memory Pool
@@ -82,13 +74,13 @@ There is also a dynamic memory heap management called memheap, suitable for memo
82
74
83
75
The concept of memory management will be explained in the "Memory Management" chapter.
84
76
85
-
###I/O Device Management
77
+
## I/O Device Management
86
78
87
79
RT-Thread uses I2C, SPI, USB, UART, etc., as peripheral devices and is uniformly registered through the device. It realized a device management subsystem accessed by the name, and it can access hardware devices according to a unified API interface. On the device driver interface, depending on the characteristics of the embedded system, corresponding events can be attached to different devices. The driver notifies the upper application program when the device event is triggered.
88
80
89
81
The concept of I/O device management will be explained in the "Device Model" and "General Equipment" chapters.
90
82
91
-
##RT-Thread Startup Process
83
+
# RT-Thread Startup Process
92
84
93
85
The understanding of most codes usually starts from learning the startup process. We will firstly look for the source of the startup. Taking MDK-ARM as an example, the user program entry for MDK-ARM is the main() function located in the main.c file. The launching of the system starts from the assembly code startup_stm32f103xe.s, jumps to the C code, initializes the RT-Thread system function, and finally enters the user program entry main().
94
86
@@ -172,7 +164,7 @@ int main(void)
172
164
}
173
165
```
174
166
175
-
##RT-Thread Program Memory Distribution
167
+
# RT-Thread Program Memory Distribution
176
168
177
169
The general MCU contains storage space that includes the on-chip Flash and the on-chip RAM. RAM is equivalent to memory, and Flash is comparable to a hard disk. The compiler classifies a program into several parts stored in different memory areas of the MCU.
178
170
@@ -239,7 +231,7 @@ void sensor_init()
239
231
```
240
232
The `sensor_value` is stored in the ZI segment and is automatically initialized to zero after system startup (some library functions provided by the user program or compiler are initialized to zero). The sensor_inited variable is stored in the RW segment, and the sensor_enable is stored in the RO segment.
241
233
242
-
##RT-Thread Automatic Initialization Mechanism
234
+
# RT-Thread Automatic Initialization Mechanism
243
235
244
236
The automatic initialization mechanism means that the initialization function does not need to be called by explicit function. It only needs to be declared by macro definition at the function definition, and it will be executed during system startup.
245
237
@@ -288,10 +280,9 @@ The macro interface definitions used to implement the automatic initialization f
288
280
289
281
Initialization function actively declares through these macro interfaces, such as INIT_BOARD_EXPORT (rt_hw_usart_init), the linker will automatically collect all the declared initialization functions, placed in the RTI symbol segment, the symbol segment is located in the RO segment of the memory distribution. All functions in this RTI symbol segment are automatically called when the system is initialized.
290
282
291
-
RT-Thread Kernel Object Model
292
-
---------------------
283
+
# RT-Thread Kernel Object Model
293
284
294
-
### Static and Dynamic Objects
285
+
## Static and Dynamic Objects
295
286
296
287
The RT-Thread kernel is designed with object-oriented method. The system-level infrastructures are all kernel objects such as threads, semaphores, mutexes, timers, and more. Kernel objects fall into two categories: static kernel objects and dynamic kernel objects. Static kernel objects are usually placed in RW and ZI segments, initialized in the program after system startup; dynamic kernel objects are created from the memory heap and then manually initialized.
297
288
@@ -372,7 +363,7 @@ In this example, thread1 is a static thread object and thread2 is a dynamic thre
372
363
373
364
Static objects take up RAM space and is not depend on the memory heap manager. When allocating static objects, the time needed is determined. Dynamic objects depend on the memory heap manager. It requests RAM space while running. When the object is deleted, the occupied RAM space is released. These two methods have their own advantages and disadvantages, and can be selected according to actual needs.
374
365
375
-
###Kernel Object Management Structure
366
+
## Kernel Object Management Structure
376
367
377
368
RT-Thread uses the kernel object management system to access/manage all kernel objects. Kernel objects contain most of the facilities in the kernel. These kernel objects can be statically allocated static objects and dynamic objects allocated from the system memory heap. .
378
369
@@ -396,7 +387,7 @@ The advantages of this design approach are:
396
387
397
388
Derivations from object control block rt_object in the above figure includes: thread object, memory pool object, timer object, device object and IPC object (IPC: Inter-Process Communication. In RT-Thread real-time operating system, IPC objects is used for synchronization and communicate between threads); derivations from IPC objects includes: semaphores, mutexes, events, mailboxes, message queues, signals, etc.
398
389
399
-
###Object Control Block
390
+
## Object Control Block
400
391
401
392
Data structure of kernel object control block:
402
393
@@ -452,7 +443,7 @@ enum rt_object_class_type
452
443
453
444
From the above type specification, we can see that if it is a static object, the highest bit of the object type will be 1 (which is the OR operation of RT_Object_Class_Static and other object types and operations). Otherwise it will be dynamic object, and the maximum number of object classes that the system can accommodate is 127.
454
445
455
-
###Kernel Object Management
446
+
## Kernel Object Management
456
447
457
448
Data structure of kernel object container:
458
449
@@ -470,7 +461,7 @@ struct rt_object_information
470
461
471
462
A class of objects is managed by an rt_object_information structure, and each practical instance of such type of object is mounted to the object_list in the form of a linked list. The memory block size of this type of object is identified by object_size (the memory block each practical instance of each type of object is the same size).
472
463
473
-
####Initialization Object
464
+
### Initialization Object
474
465
475
466
An uninitialized static object must be initialized before it can be used. The initialization object uses the following interfaces:
476
467
@@ -489,7 +480,7 @@ When this function is called to initialize the object, the system will place the
489
480
| type | The type of the object must be a enumeration type listed in rt_object_class_type, RT_Object_Class_Static excluded. (For static objects, or objects initialized with the rt_object_init interface, the system identifies it as an RT_Object_Class_Static type) |
490
481
| name | Name of the object. Each object can be set to a name, and the maximum length for the name is specified by RT_NAME_MAX. The system does not care if it uses ’`\0`’as a terminal symbol. |
491
482
492
-
#### Detach Object
483
+
### Detach Object
493
484
494
485
Detach an object from the kernel object manager. The following interfaces are used to detach objects:
Calling this interface makes a static kernel object to be detached from the kernel object container, meaning the corresponding object node is deleted from the kernel object container linked list. After the object is detached, the memory occupied by the object will not be released.
501
492
502
-
####Allocate object
493
+
### Allocate object
503
494
504
495
The above descriptions are interfaces of objects initialization and detachment, both of which are under circumstances that object-oriented memory blocks already exist. But dynamic objects can be requested when needed. The memory space is freed for other applications when not needed. To request assigning new objects, you can use the following interfaces:
505
496
@@ -519,7 +510,7 @@ When calling the above interface, the system first needs to obtain object inform
For a dynamic object, when it is no longer used, you can call the following interface to delete the object and release the corresponding system resources:
525
516
@@ -534,7 +525,7 @@ When the above interface is called, the object is first detached from the object
534
525
|----------|------------|
535
526
| object | object handle |
536
527
537
-
####Identify objects
528
+
### Identify objects
538
529
539
530
Identify whether the specified object is a system object (static kernel object). The following interface is used to identify the object:
0 commit comments