This book is written by the core author of the self-developed open source embedded real-time operating system RT-Thread. The professionalism is beyond doubt. It systematically analyzes the core design and implementation of embedded systems and grasps the essence of the Internet of Things operating system. Qiu Yi, co-founder of Ruiside Technology. He has participated in the development of the RT-Thread open source operating system since 2006 when he was in school. Together with outstanding engineers in China, he developed RT-Thread from scratch. RT-Thread is widely used in many industries such as energy, automotive, medical, and consumer electronics, becoming an open source embedded operating system independently developed by Chinese people, mature and stable, and with a large installed base. Xiong Puxiang, founder and CEO of Ruiside Technology, founded the RT-Thread open source operating system in 2006, and in the form of an open source community, led outstanding engineers in China to develop the RT-Thread autonomous operating system kernel from scratch, integrating real-time object-oriented design, and a large number of peripheral mature and stable software components, such as file systems, command lines, graphical user interfaces, etc. Zhu Tianlong (Armink), technical director of Ruiside Technology, is responsible for the research and development of RT-Thread Internet of Things components, cutting-edge technology exploration and team management. He once worked in a well-known domestic medical equipment company as a manager of the R&D department, led the R&D implementation and mass production of multiple projects, and obtained more than 10 national invention patents. He has nearly 10 years of experience in RT-Thread development. He is also an embedded open source geek, sharing several highly active open source software such as EasyLogger, EasyFlash, and CmBacktrace. Preface Part 1 Kernel Chapter 1 Embedded Real-Time Operating System 2 1.1 Embedded System 3 1.2 Real-Time System 4 1.3 Embedded Real-Time Operating System 6 1.3.1 Mainstream Embedded Real-Time Operating System 7 1.3.2 Development Trend 8 1.4 Summary of this Chapter 8 Chapter 2 Understanding and Quickly Getting Started with RT-Thread 9 2.1 RT-Thread Overview 9 2.2 RT-Thread Architecture 10 2.3 RT-Thread Acquisition 11 2.4 RT-Thread Quick Start 12 2.4.1 Prepare the Environment 13 2.4.2 Introduction to RT-Thread 16 2.4.3 Marquee Example 20 2.5 Summary of this Chapter 21 Chapter 3 Kernel Basics 22 3.1 Introduction to RT-Thread Kernel 22 3.2 RT-Thread Startup Process 24 3.3 RT-Thread Program Memory Distribution 26 3.4 RT-Thread Automatic Initialization Mechanism 28 3.5 RT-Thread Kernel Object Model 29 3.5.1 Static Objects and Dynamic Objects 29 3.5.2 Kernel Object Management Architecture 31 3.5.3 Object Control Block 33 3.5.4 Kernel Object Management Method 34 3.6 RT-Thread Kernel Configuration Example 36 3.7 Common Macro Definitions 38 3.8 Summary of This Chapter 39 Chapter 4 Thread Management 40 4.1 Functional Characteristics of Thread Management 40 4.2 Working Mechanism of Threads 41 4.2.1 Thread Control Block 41 4.2.2 Important Attributes of Threads 42 4.2.3 Thread State Switching 45 4.2.4 System Threads 46 4.3 Thread Management Methods 46 4.3.1 Creating and Deleting Threads 47 4.3.2 Initializing and Leaving Threads 48 4.3.3 Starting Threads 49 4.3.4 Obtaining the Current Thread 50 4.3.5 Making a Thread Give Up Processor Resources 50 4.3.6 Making a Thread Sleep 50 4.3.7 Suspending and Resuming Threads 51 4.3.8 Controlling Threads 52 4.3.9 Setting and Deleting Idle Hooks 52 4.3.10 Setting Scheduler Hooks 53 4.4 Thread Application Examples 53 4.4.1 Creating Threads Example 54 4.4.2 Thread Time Slice Round Robin Scheduling Example 56 4.4.3 Thread Scheduler Hook Example 57 4.5 Summary of This Chapter 59 Chapter 5 Clock Management 60 5.1 Clock Tick 60 5.1.1 How to Implement the Clock Tick 60 5.1.2 How to Get the Clock Tick 61 5.2 Timer Management 62 5.2.1 Introduction to RT-Thread Timers 62 5.2.2 How Timers Work 63 5.2.3 How to Manage Timers 65 5.3 Timer Application Examples 69 5.4 High-Precision Delay 72 5.5 Summary of This Chapter 73 Chapter 6 Inter-Thread Synchronization 74 6.1 Semaphores 75 6.1.1 How Semaphores Work 75 6.1.2 Semaphore Control Block 75 6.1.3 How to manage semaphores 76 6.1.4 Examples of semaphore applications 79 6.1.5 Where to use semaphores 85 6.2 Mutexes 87 6.2.1 How mutexes work 87 6.2.2 Mutex control blocks 89 6.2.3 How to manage mutexes 89 6.2.4 Examples of mutex applications 92 6.2.5 Where to use mutexes 97 6.3 Event sets 97 6.3.1 How event sets work 97 6.3.2 Event set control blocks 98 6.3.3 How to manage event sets 99 6.3.4 Examples of event set applications 101 6.3.5 Where to use event sets 104 6.4 Summary of this chapter 104 Chapter 7 Inter-thread communication 105 7.1 Mailboxes 105 7.1.1 How mailboxes work 105 7.1.2 Mailbox control blocks 106 7.1.3 How to manage mailboxes 106 7.1.4 Mailbox Usage Examples 110 7.1.5 Mailbox Usage Scenarios 112 7.2 Message Queues 113 7.2.1 Message Queue Working Mechanism 113 7.2.2 Message Queue Control Block 114 7.2.3 Message Queue Management Method 115 7.2.4 Message Queue Application Examples 118 7.2.5 Message Queue Usage Scenarios 121 7.3 Signals 123 7.3.1 Signal Working Mechanism 123 7.3.2 Signal Management Method 124 7.3.3 Signal Application Examples 126 7.4 Sections in This Chapter 128 Chapter 8 Memory Management 129 8.1 Memory Management Features 129 8.2 Memory Heap Management 130 8.2.1 Small Memory Management Algorithm 131 8.2.2 Slab Management Algorithm 132 8.2.3 Memheap Management Algorithm 133 8.2.4 Memory Heap Configuration and Initialization 134 8.2.5 Memory heap management method 134 8.2.6 Memory heap management application example 136 8.3 Memory pool 138 8.3.1 Memory pool working mechanism 139 8.3.2 Memory pool management method 140 8.3.3 Memory pool application example 143 8.4 Summary of this chapter 145 Chapter 9 Interrupt Management 146 9.1 Cortex-MCPU Architecture Basics 146 9.1.1 Register Introduction 147 9.1.2 Operation Mode and Privilege Level 148 9.1.3 Nested Vector Interrupt Controller 148 9.1.4 PendSV System Call 149 9.2 RT-Thread Interrupt Working Mechanism 149 9.2.1 Interrupt Vector Table 149 9.2.2 Interrupt Processing Process 151 9.2.3 Interrupt Nesting 153 9.2.4 Interrupt Stack 154 9.2.5 Bottom Half Processing of Interrupts 154 9.3 RT-Thread Interrupt Management Interface 156 9.3.1 Interrupt Service Routine Hook 157 9.3.2 Interrupt Source Management 158 9.3.3 Global Interrupt Switch 158 9.3.4 Interrupt Notification 160 9.4 Interrupt and Polling 161 9.5 Example of Using the Global Interrupt Switch 162 9.6 Summary of this Chapter 164 Chapter 10 Kernel Porting 165 10.1 CPU Architecture Porting 165 10.1.1 Implementing the Global Interrupt Switch 166 10.1.2 Implementing Thread Stack Initialization 167 10.1.3 Implementing Context Switching 168 10.1.4 Implementing the Clock Beat 174 10.2 BSP Porting 175 10.3 Kernel Porting Example 175 10.3.1 Preparing a Bare Metal Project 176 10.3.2 Creating an RT-Thread Project 177 10.3.3 Implementing Clock Management 179 10.3.4 Implementing console output 180 10.3.5 Implementing dynamic heap memory management 181 10.3.6 Migrating to more development boards 183 10.4 Summary of this chapter 184 Part II Components Chapter 11 Env auxiliary development environment 186 11.1 Introduction to Env 186 11.2 Features of Env 187 11.3 Example of building an Env project 189 11.4 Build more MDK projects 196 11.4.1 Create a peripheral example project 196 11.4.2 Create a file system example project 198 11.4.3 Create a network example project 202 11.5 Summary of this chapter 206 Chapter 12 FinSH console 207 12.1 Introduction to FinSH 207 12.2 FinSH Built-in Commands 209 12.2.1 Display Thread Status 210 12.2.2 Display Semaphore Status 210 12.2.3 Display Event Status 210 12.2.4 Display Mutex Status 210 12.2.5 Display Mailbox Status 211 12.2.6 Display Message Queue Status 211 12.2.7 Display Memory Pool Status 211 12.2.8 Display Timer Status 212 12.2.9 Display Device Status 212 12.2.10 Display Dynamic Memory Status 212 12.3 Custom FinSH Commands 213 12.3.1 Custom msh Commands 213 12.3.2 Custom C-Style Commands and Variables 213 12.3.3 Custom Command Renaming 214 12.4 FinSH Function Configuration 214 12.5 FinSH Application Examples 216 12.5.1 Custom msh Command Example 216 12.5.2 msh Command Example with Parameters 217 12.6 Chapter Summary 218 Chapter 13 I/O Device Management 219 13.1 Introduction to I/O Devices 219 13.1.1 I/O Device Management Framework 219 13.1.2 I/O Device Model 221 13.1.3 I/O Device Types 222 13.2 Creating and Registering I/O Devices 223 13.3 Accessing I/O Devices 226 13.3.1 Finding Devices 226 13.3.2 Initializing Devices 227 13.3.3 Opening and Closing Devices 227 13.3.4 Controlling Devices 228 13.3.5 Reading and Writing Devices 229 13.3.6 Data Sending and Receiving Callbacks 229 13.3.7 Device Access Examples 230 13.4 Chapter Summary 231 Chapter 14 General Peripheral Interface 232 14.1 UART Serial Port 232 14.1.1 Serial Port Device Management 233 14.1.2 Create and Register Serial Port Device 233 14.1.3 Access Serial Port Device 235 14.1.4 Serial Port Device Usage Examples 235 14.2 GPIO 237 14.2.1 PIN Device Management 238 14.2.2 Create and Register PIN Device 238 14.2.3 Access PIN Device 239 14.2.4 PIN Device Usage Examples 242 14.3 SPI Bus 243 14.3.1 SPI Device Management 244 14.3.2 Create and Register SPI Bus Device 246 14.3.3 Create and Mount SPI Slave Device 247 14.3.4 Access SPI Slave Device 249 14.3.5 Special Usage Scenarios 254 14.3.6 SPI Device Usage Examples 255 14.4 I2C Bus 256 14.4.1 I2C Device Management 258 14.4.2 Creating and Registering I2C Bus Devices 258 14.4.3 Accessing I2C Devices 259 14.4.4 I2C Device Application Examples 260 14.5 Running Device Application Examples 263 14.5.1 Running PIN Device Example 264 14.5.2 Running SPI Device Example 265 14.5.3 Running I2C Device Example 266 14.5.4 Running Serial Port Device Example 266 14.6 Summary of This Chapter 267 Chapter 15 Virtual File System 268 15.1 Introduction to DFS 268 15.1.1 DFS Architecture 269 15.1.2 POSIX Interface Layer 269 15.1.3 Virtual File System Layer 270 15.1.4 Device Abstraction Layer 270 15.2 File System Mount Management 271 15.2.1 Initializing DFS Components 271 15.2.2 Registering a File System 271 15.2.3 Registering a Storage Device as a Block Device 271 15.2.4 Formatting a File System 272 15.2.5 Mounting a File System 273 15.2.6 Unmounting a File System 273 15.3 File Management 273 15.3.1 Opening and Closing Files 273 15.3.2 Reading and Writing Data 274 15.3.3 Renaming 275 15.3.4 Obtaining Status 275 15.3.5 Deleting Files 275 15.3.6 Synchronizing File Data to Storage Devices 276 15.3.7 Querying File System Related Information 276 15.3.8 Monitoring I/O Device Status 276 15.4 Directory Management 277 15.4.1 Creating and Deleting Directories 277 15.4.2 Opening and Closing Directories 277 15.4.3 Read Directory 278 15.4.4 Get the Read Position of Directory Stream 278 15.4.5 Set the Next Read Position of Directory 278 15.4.6 Reset the Read Position of Directory to the Beginning 279 15.5 DFS Function Configuration 279 15.6 DFS Application Example 279 15.6.1 Preparation 280 15.6.2 Read and Write File Example 283 15.6.3 Change File Name Example 284 15.6.4 Get File Status Example 285 15.6.5 Create Directory Example 286 15.6.6 Read Directory Example 286 15.6.7 Set the Read Directory Position Example 287 15.7 Summary of This Chapter 289 Chapter 16 Network Framework 290 16.1 Introduction to TCP/IP Network Protocol 290 16.1.1 OSI Reference Model 290 16.1.2 TCP/IP Reference Model 291 16.1.3 The difference between TCP/IP reference model and OSI reference model 291 16.1.4 IP address 292 16.1.5 Subnet mask 292 16.1.6 MAC address 292 16.2 Introduction to RT-Thread network framework 292 16.3 Network framework workflow 294 16.3.1 Network protocol cluster registration 294 16.3.2 Network data receiving process 295 16.3.3 Network data sending process 296 16.4 Network socket programming 296 16.4.1 TCP socket communication process 296 16.4.2 UDP socket communication process 297 16.4.3 Create a socket 298 16.4.4 Bind a socket 298 16.4.5 Establish a TCP connection 299 16.4.6 Data transmission 300 16.4.7 Close the network connection 301 16.5 Network function configuration 302 16.6 Network Application Example 303 16.6.1 Preparation 303 16.6.2 TCP Client Example 306 16.6.3 UDP Client Example 310 16.7 Chapter Summary 312 Appendix Amenuconfig Configuration Options 313 Appendix BSCONS Build System 3171 SPI Device Management 244 14.3.2 Creating and Registering SPI Bus Devices 246 14.3.3 Creating and Mounting SPI Slave Devices 247 14.3.4 Accessing SPI Slave Devices 249 14.3.5 Special Usage Scenarios 254 14.3.6 SPI Device Usage Examples 255 14.4 I2C Bus 256 14.4.1 I2C Device Management 258 14.4.2 Creating and Registering I2C Bus Devices 258 14.4.3 Accessing I2C Devices 259 14.4.4 I2C Device Application Examples 260 14.5 Running Device Application Examples 263 14.5.1 Running PIN Device Example 264 14.5.2 Running SPI Device Example 265 14.5.3 Running I2C Device Example 266 14.5.4 Running Serial Port Device Example 266 14.6 Chapter Summary 267 Chapter 15 Virtual File System 268 15.1 Introduction to DFS 268 15.1.1 DFS Architecture 269 15.1.2 POSIX Interface Layer 269 15.1.3 Virtual File System Layer 270 15.1.4 Device Abstraction Layer 270 15.2 File System Mount Management 271 15.2.1 DFS Component Initialization 271 15.2.2 Registering a File System 271 15.2.3 Registering a Storage Device as a Block Device 271 15.2.4 Formatting a File System 272 15.2.5 Mounting a File System 273 15.2.6 Unmounting a File System 273 15.3 File Management 273 15.3.1 Opening and Closing Files 273 15.3.2 Reading and Writing Data 274 15.3.3 Renaming 275 15.3.4 Getting Status 275 15.3.5 Deleting Files 275 15.3.6 Synchronizing File Data to Storage Devices 276 15.3.7 Querying File System Related Information 276 15.3.8 Monitoring I/O Device Status 276 15.4 Directory Management 277 15.4.1 Creating and Deleting Directories 277 15.4.2 Opening and Closing Directories 277 15.4.3 Reading Directories 278 15.4.4 Getting the Read Position of a Directory Stream 278 15.4.5 Setting the Next Read Position of a Directory 278 15.4.6 Resetting the Read Position of a Directory to the Beginning 279 15.5 DFS Function Configuration 279 15.6 DFS Application Examples 279 15.6.1 Preparations 280 15.6.2 Reading and Writing Files Example 283 15.6.3 Changing File Name Example 284 15.6.4 Getting File Status Example 285 15.6.5 Creating a Directory Example 286 15.6.6 Example of reading a directory 286 15.6.7 Example of setting the read directory location 287 15.7 Summary of this chapter 289 Chapter 16 Network Framework 290 16.1 Introduction to TCP/IP Network Protocol 290 16.1.1 OSI Reference Model 290 16.1.2 TCP/IP Reference Model 291 16.1.3 Differences between TCP/IP Reference Model and OSI Reference Model 291 16.1.4 IP Address 292 16.1.5 Subnet Mask 292 16.1.6 MAC Address 292 16.2 Introduction to RT-Thread Network Framework 292 16.3 Network Framework Workflow 294 16.3.1 Network Protocol Cluster Registration 294 16.3.2 Network Data Receiving Process 295 16.3.3 Network Data Sending Process 296 16.4 Network Socket Programming 296 16.4.1 TCP socket communication process 296 16.4.2 UDP socket communication process 297 16.4.3 Create a socket 298 16.4.4 Bind a socket 298 16.4.5 Establish a TCP connection 299 16.4.6 Data transmission 300 16.4.7 Close the network connection 301 16.5 Network function configuration 302 16.6 Network application example 303 16.6.1 Preparation 303 16.6.2 TCP client example 306 16.6.3 UDP client example 310 16.7 Chapter summary 312 Appendix Amenuconfig configuration options 313 Appendix BCSons build system 3171 SPI Device Management 244 14.3.2 Creating and Registering SPI Bus Devices 246 14.3.3 Creating and Mounting SPI Slave Devices 247 14.3.4 Accessing SPI Slave Devices 249 14.3.5 Special Usage Scenarios 254 14.3.6 SPI Device Usage Examples 255 14.4 I2C Bus 256 14.4.1 I2C Device Management 258 14.4.2 Creating and Registering I2C Bus Devices 258 14.4.3 Accessing I2C Devices 259 14.4.4 I2C Device Application Examples 260 14.5 Running Device Application Examples 263 14.5.1 Running PIN Device Example 264 14.5.2 Running SPI Device Example 265 14.5.3 Running I2C Device Example 266 14.5.4 Running Serial Port Device Example 266 14.6 Chapter Summary 267 Chapter 15 Virtual File System 268 15.1 Introduction to DFS 268 15.1.1 DFS Architecture 269 15.1.2 POSIX Interface Layer 269 15.1.3 Virtual File System Layer 270 15.1.4 Device Abstraction Layer 270 15.2 File System Mount Management 271 15.2.1 DFS Component Initialization 271 15.2.2 Registering a File System 271 15.2.3 Registering a Storage Device as a Block Device 271 15.2.4 Formatting a File System 272 15.2.5 Mounting a File System 273 15.2.6 Unmounting a File System 273 15.3 File Management 273 15.3.1 Opening and Closing Files 273 15.3.2 Reading and Writing Data 274 15.3.3 Renaming 275 15.3.4 Getting Status 275 15.3.5 Deleting Files 275 15.3.6 Synchronizing File Data to Storage Devices 276 15.3.7 Querying File System Related Information 276 15.3.8 Monitoring I/O Device Status 276 15.4 Directory Management 277 15.4.1 Creating and Deleting Directories 277 15.4.2 Opening and Closing Directories 277 15.4.3 Reading Directories 278 15.4.4 Getting the Read Position of a Directory Stream 278 15.4.5 Setting the Next Read Position of a Directory 278 15.4.6 Resetting the Read Position of a Directory to the Beginning 279 15.5 DFS Function Configuration 279 15.6 DFS Application Examples 279 15.6.1 Preparations 280 15.6.2 Reading and Writing Files Example 283 15.6.3 Changing File Name Example 284 15.6.4 Getting File Status Example 285 15.6.5 Creating a Directory Example 286 15.6.6 Example of reading a directory 286 15.6.7 Example of setting the read directory location 287 15.7 Summary of this chapter 289 Chapter 16 Network Framework 290 16.1 Introduction to TCP/IP Network Protocol 290 16.1.1 OSI Reference Model 290 16.1.2 TCP/IP Reference Model 291 16.1.3 Differences between TCP/IP Reference Model and OSI Reference Model 291 16.1.4 IP Address 292 16.1.5 Subnet Mask 292 16.1.6 MAC Address 292 16.2 Introduction to RT-Thread Network Framework 292 16.3 Network Framework Workflow 294 16.3.1 Network Protocol Cluster Registration 294 16.3.2 Network Data Receiving Process 295 16.3.3 Network Data Sending Process 296 16.4 Network Socket Programming 296 16.4.1 TCP socket communication process 296 16.4.2 UDP socket communication process 297 16.4.3 Create a socket 298 16.4.4 Bind a socket 298 16.4.5 Establish a TCP connection 299 16.4.6 Data transmission 300 16.4.7 Close the network connection 301 16.5 Network function configuration 302 16.6 Network application example 303 16.6.1 Preparation 303 16.6.2 TCP client example 306 16.6.3 UDP client example 310 16.7 Chapter summary 312 Appendix Amenuconfig configuration options 313 Appendix BCSons build system 317
You Might Like
Recommended ContentMore
Open source project More
Popular Components
Searched by Users
Just Take a LookMore
Trending Downloads
Trending ArticlesMore