2774 views|0 replies

103

Posts

0

Resources
The OP
 

【RT-Thread Reading Notes】——The harder you work, the luckier you will be [Copy link]

 On April 13, the long-awaited RT-Thread Kernel Implementation and Application Development Practical Guide based on STM32 arrived. The reason for the expectation is that this book is a book I got by participating in the evaluation activity of the Electronic Engineering World. I will show you the books I received. My fate with Wildfire is more than that. Wildfire has helped me a lot in studying STM32 for a year and a half. I will show you the books I refer to. I really like reading the first chapter of Wildfire's "STM32 Library Development Practical Guide": Why learn STM32? The second section of the first chapter describes the growth path of embedded engineers. A person has more skills and can do more things, which has always encouraged me to keep learning. In the same way, in the second chapter of "RT-Thread Kernel Implementation and Application Development Practical Guide": Why learn RTOS, there is a sentence in it that encourages everyone: As a qualified embedded software engineer, learning is something that can never stop, and you must always prepare for the future. I regret not having enough knowledge when I need it. I hope you don't feel that way when the opportunity comes. I came into contact with STM32 at the end of 2017, but I had little experience in the electronics industry. During the summer vacation of my sophomore year in college, I participated in an electronic design competition. My self-study ability was relatively poor, and I had little understanding of the application of microcontrollers. That experience only made me feel deeply frustrated. Six years have passed in a flash. I didn't do technology at the beginning of my work until I was unhappy with my work and felt depressed. Two years ago, I insisted on choosing technology. Although I have not done real-time operating system projects at present, based on my experience in the projects I have done, embedded applications are more extensive. To make a long story short, I would like to say that I learned the programming ideas of RT-Thread through the book "RT-Thread Kernel Implementation and Application Development Practical Guide". In the book, Chapter 5 talks about the difference between bare metal systems and multi-threaded systems. Bare metal systems are divided into polling systems and foreground and background systems. The polling system is to initialize the relevant hardware first when programming bare metal, and then let the main program loop continuously in an infinite loop to do various things in sequence. The polling system is a very simple software structure, which is usually only suitable for things that can be completed by sequentially executing code and do not need external events to drive. The polling system is only suitable for functional codes that are executed sequentially. When there are external events driving, the real-time performance will be reduced. Compared with the polling system, the foreground and background systems add interrupts on the basis of polling. The response to external events is completed in the interrupt, and the event processing is still completed in the polling system. Here we call the interrupt the foreground, and the infinite loop in the main function is called the background. Multi-threaded system: Compared with the foreground and background systems, the event response of the multi-threaded system is also completed in the interrupt, but the event processing is completed in the thread. In a multi-threaded system, threads have priorities like interrupts, and threads with high priorities will be executed first. When an urgent event is marked in the interrupt, if the priority of the thread corresponding to the event is high enough, it will be responded immediately. Compared with the front-end and back-end systems, the real-time performance of multi-threading is further improved. In a multi-threaded system, according to the function of the program, we divide the main body of the program into independent, infinitely looping and non-returning small programs, which we call threads. Each thread is independent, does not interfere with each other, and has its own priority, which is scheduled and managed by the operating system. With the addition of an operating system, our programming becomes simpler. The additional overhead of the entire system is the tiny bit of FLASH and RAM occupied by the operating system. Chapter 6 introduces the definition of threads and the implementation of thread switching. What is a thread? In a bare metal system, the main body of the system is the infinite loop in the main function that is executed sequentially. In this infinite loop, the CPU completes various things in sequence. In a multi-threaded system, we divide the entire system into independent and non-returnable functions according to different functions. We call this function a thread. The difference between a thread and a bare metal system is that in a bare metal system, if there are global variables, sub-function calls, and interrupts, then when the system is running, we don't need to worry about where the global variables are placed, where the local variables are placed when the sub-function is called, and where the function return address is placed when the interrupt occurs. When writing an RTOS, we must figure out how these various environmental parameters are stored. In a bare metal system, they are all placed in a place called a stack. The stack is a continuous memory space in the RAM of the microcontroller. The size of the stack is generally specified in the startup file or link script, and finally initialized by the C library function main. However, in a multi-threaded system, each thread is independent and does not interfere with each other, so an independent space must be allocated for each thread. This stack space is usually a pre-defined array or a dynamically allocated space, but they all exist in RAM. In a multi-threaded system, as many thread stacks as there are threads need to be defined. Features of thread functions: A thread is an independent function, and the function body loops infinitely and cannot return. Definition of thread control block: In a bare metal system, the main body of the program is executed sequentially by the CPU. In a multi-threaded system, the execution of threads is scheduled by the system. In order to smoothly schedule threads, the system defines an additional thread control block for each thread. This thread control block is equivalent to the identity card of the thread, which contains all the information of the thread, such as the stack pointer of the thread, the name of the thread, the formal parameters of the thread, etc. With this thread control block, it is equivalent to a thread. In the future, all operations of the system on the thread can be implemented through this thread control block. Defining a thread control block requires a new data type, which is declared in the header file rtdef.h. It can be used to define a thread control block entity for each thread. Implementing the thread creation function: The stack of the thread, the function entity of the thread, and the control block of the thread ultimately need to be linked together in order to be uniformly scheduled by the system. We can write an initialization function to implement this, which contains the initialization thread control block pointer. Initialize the thread list node, the entry function of the thread, the thread parameters, which are used to point to the starting address of the thread stack and the size of the thread stack. After initializing the thread linked list node, we insert the thread into various linked lists in the future, which is achieved through this node. It is like a hook in the thread control block, which can hang the thread control block in various linked lists. Before initialization, we need to add a thread linked list node in the thread control block. There are two node pointers of the same type, next and prev, in the node of the linked list node data type, which are used to point to the next node and the previous node in the linked list respectively. There is also how to initialize the linked list node, insert a node after the head of the bidirectional linked list, insert a node before the head of the bidirectional linked list, and delete a node from the bidirectional linked list.
As of today, I only know these. I will use the next time to continue reading this book to share with you. My reading process is relatively slow and my method is not very good. Please forgive me!
This content is originally created by EEWORLD forum user Shen Tingting. If you want to reprint or use it for commercial purposes, you must obtain the author's consent and indicate the source


微信图片_20190430165518.jpg (140.36 KB, downloads: 0)

微信图片_20190430165518.jpg
Personal signature坚持自己的坚持,终究会有拨开云雾的一天
 

Just looking around
Find a datasheet?

EEWorld Datasheet Technical Support

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京B2-20211791 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号
快速回复 返回顶部 Return list