In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-31 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/01 Report--
How the helloworld program is called, I believe that many inexperienced people do not know what to do, so this paper summarizes the causes of the problem and solutions, through this article I hope you can solve this problem.
Analyze how helloworld programs are called and what SYS_RUN does
I believe everyone has implemented their first helloworld program on the Hongmeng system.
The code is very simple, after compiling and burning, we can see that the serial port has printed [DEMO] Hello world.
But when was the HelloWorld function called? What does SYS_RUN do?
Let's take a look.
1. Start the process
First, we need to analyze the startup process of Hi3861. At present, Hi3861 uses the liteOS-M kernel, which is not provided by the relevant source vendors, but it does not hinder us. After some search, I can see that after hi3861 starts, the first entry function is the app_main function.
(vendor\ hisi\ hi3861\ hi3861\ app\ wifiiot_app\ src\ app_main.c)
You can open it and see the contents of the app_main function, as follows. Of course, I only have a simplified version here. I have deleted a lot of initialization functions, leaving only the final ones.
Hi_void app_main (hi_void) {/ / print sdk version const hi_char* sdk_ver = hi_get_sdk_version (); printf ("sdk ver:%s\ r\ n", sdk_ver); / / Serial port, IO initialization and other peripheral_init (); / / wifi initialization ret = hi_wifi_init (APP_INIT_VAP_NUM, APP_INIT_USR_NUM); / / Hongmeng system initialization HOS_SystemInit ();}
We can see that app_main actually does a lot of work after startup, including io initialization, wifi initialization, and finally call HOS_SystemInit (); the final initialization of the Hongmeng system.
Let's take a look at what HOS_SystemInit () has done.
Open the source code base\ startup\ services\ bootstrap_lite\ source\ system_init.c
You can see that the function is as follows:
Void HOS_SystemInit (void) {MODULE_INIT (bsp); MODULE_INIT (device); MODULE_INIT (core); SYS_INIT (service); SYS_INIT (feature); MODULE_INIT (run); SAMGR_Bootstrap ();}
It looks as if some modules are being called. If you look closely, one of them is MODULE_INIT (run);. As the name implies, it seems to initialize or call a run module. What is the run module? Let's take a look at the SYS_RUN (HelloWorld) of the title. Is it possible to guess that MODULE_INIT (run) is actually calling HelloWorld?
Ha, ha. Actually, it is. If you add print information, you can see the following print.
.. /.. / base/startup/services/bootstrap_lite/source/system_init.c 38.. /.. / applications/sample/wifi-iot/app/my_first_app/hello_world.c 9 [DEMO] Hello world.../../base/startup/services/bootstrap_lite/source/system_init.c 40
Looking carefully at the print statement I added, I did execute MODULE_INIT (run) on line 38 before printing [DEMO] Hello world.
So it's just as we guessed. Of course not. We have to analyze why this is the case.
two。 Link
Let's take a look at what MODULE_INIT (run) has done. In fact, it's just a macro.
# define MODULE_INIT (name)\ do {\ MODULE_CALL (name, 0);\} while (0)
And MODULE_CALL (name, 0); can be expanded again: of course, I added the print of the if statement in it.
We can see that it actually defines an InitCall pointer, and the pointer is this:
(MODULE_BEGIN (name, step))
The MODULE_BEGIN macro is actually expanded as follows:
# define MODULE_NAME (name, step) ".zinit call." >
In fact, ".zinit call." # name # step ".init" ends with .zinitcall.run2.init.
It is actually a way of writing, that is, when our code is compiled, there is a special address in the code. Its name is .zinitcall.run2.init, that is, the InitCall pointer points to the address of the .zinitcall.run2.init code segment.
Draw a picture:
The green one is the .zinitcall.run2.init code snippet, which contains function pointers.
All right, we should all understand here, continue to look at this diagram, in fact, this is just to take out all the function pointers in this code segment, and then execute the function pointed to by the function pointer.
At this point, there is only one last question: how to point it to the HelloWorld function.
This is actually the credit of SYS_RUN.
Let's also take a look at what SYS_RUN has done, and the other is also a macro. The expansion process is as follows:
We can see that in fact, the end result of SYS_RUN (HelloWorld) is:
Static const InitCall USED_ATTR _ _ zinitcall_##layer##_##func\ _ _ attribute__ ((section (".zit call." >
It looks very complicated, and we have no lack of dismantling it:
Let's not look at the red font, then the result is:
Static const InitCall = HelloWorld
Is not very simple, in fact, it defines a global variable (function pointer), which points to HelloWorld.
What's the red font for? It actually tells the compiler that my variable (static const InitCall variable) is very special and is compiled in the .zinitcall.run2.init section when compiling.
3. Advice
Here are two pieces of advice:
1. Please do not write while (1) directly in the entry function defined by SYS_RUN ()
This is easy to understand, because after the system starts, app_main calls the entry function defined by SYS_RUN (), such as HelloWorld. If we write while (1) in the HelloWorld function, it will cause the subsequent code of app_main not to be executed, there must be a problem.
2. For threads created by the entry function defined by SYS_RUN (), you must have a sleep action.
To solve the first problem, we naturally thought that we could create a thread in the entry function defined by SYS_RUN (), so that we could while (1). Haha, in fact, there is also a problem, because app_main itself is also a task, if we create a task with a particularly high priority, it will cause the app_main task not to be executed, or there will be problems. So there should be sleep to ensure that the subsequent code of app_main can be executed smoothly.
After reading the above, have you mastered how the helloworld program is called? If you want to learn more skills or want to know more about it, you are welcome to follow the industry information channel, thank you for reading!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.