Run the “desktop” software on the microcontroller



Embox participated in the TechTrain IT festival. Like the first time, we brought the pieces of iron and showed our achievements live. We already wrote about some on the HabrĂ©, but you never know who :). VoIP phone , Qt and OpenCV were shown, all based on STM32F7-Discovery. In addition to the stand, we made three reports. I already described ideas of the first about the open project on a habr In this article I want to tell the ideas of another of our report, which was called “Launching“ desktop ”software on microcontrollers” . Well, taking this opportunity, I’ll tell you a little about my feelings about the festival.



TechTrain



First, let me tell you a little about the festival itself. Anyone who is not interested can immediately rewind to the section “Launching“ Desktop ”software on the microcontroller”



The first thing that caught my eye at the festival was a lot of people. We are tired of standing at the stand, speaking, conducting a quest, and communicating. On the other hand, it was very fun and interesting, and most importantly we received a lot of positive emotions, for example, several people told us that we have a good blog on the hub :). The participants of the festival were from various fields of activity, and were not even from IT, because the festival’s slogan is “a great festival for developers, engineers and sympathizers.” There were many children, one boy at the end of the festival dragged a robot machine from the stand and played with it. Neither the boy nor the robot was hurt.



To make the breadth of the audience more understandable, I will illustrate it differently. A lot of activities were carried out at the festival, one of them was a quest. Participants in the quest had to go up to stands, complete invented tasks, and receive a seal. For this, someone even got a whole quadrocopter. But now is not about that. We were also one of the places where you could get a seal. We had prepared tasks, including questions. The questions were simple, regarding open source software and embedded systems. But the contingent was so different that some resented the simplicity of the question, while others entered into a stupor. The last we naturally helped, told (or suggested google) about such things as: “who are the creators of the C language”, “what is famous for Linus Torvalds”, “what is the difference between a microprocessor and a microcontroller” or “what is a cross-compiler”. As a result, no one left without printing. By the way, it was a little strange, but suspiciously many easily deciphered “GNU - GNU is not Unix”. Probably prepared for Stallman. :)



The second follows from the first. There were entertainment for a very wide audience. For example, Habr brought a sandbox. True, it was not a local sandbox where you can get an invite for an article, but it was possible to wallow in it fame. About invites in the sandbox, I did not think to ask, maybe they gave as in a real sandbox. There was a club of old computers , you could play on them, which many did. There were slot machines from the Museum of Soviet slot machines . Sberbank, offered a relaxation zone, it was also possible to play in it already in the play station. There were traditional yes IT kickers and many other amusements. Unfortunately, we had to work, so I’m only talking about what was visible from our booth.



The third and probably the main thing was that the organizers managed to gather representatives of virtually all of its areas from web development and distributed systems to embedded. From developers and other techies, to human resources and just interested in it, for example, gamers (after all, it was possible to hack into Romero himself) or DIY enthusiasts. There were QA from COMAQA there were team leads from Burning Lead . There were communities of different cities (Rostov, Tver, Krasnodar). As a result, reports and stands were very different in topic. For example, our booth was located between the community of team leaders Burning Lead and the booth of the St. Petersburg Club of IT Directors .



As a result, people with very different backgrounds approached our booth, there were web developers and dotnet cheeks, there were JS and pluses. Someone recalled that he was doing something on the AVR while still at the university, someone was saying that he was having fun with RaPi or Arduino. There were amazingly many DotNet-chiks, many were wondering if we had a .NET Micro Framework . We even thought hard not to drag him to us. Expectedly there were a lot of questions about Rust. In short, in my opinion, the organizers managed to quite well implement the idea of ​​the festival “Find out what others live!”. After all, as at any festival, the main thing here is to see others and show themselves :).



Yes, photos are not given here. A few pictures , including with a live boy Volodya, can be viewed in our group. Well, of course, in the techtrain group, a bunch of high-quality photos, they promised to post more.



Launch Desktop Software on a Microcontroller



My report was a kind of summing up the results of several of our work on launching desktop software ( Qt , OpenCV , pjsip ). The report is aimed at the general public. For a habr I will try to reduce many details, and for necessary things I will give the link or keywords for search.



To begin with, I’ll give a kind of agenda. In fact, there will be 3 parts. Firstly, I will explain the difficulties of transferring desktop software to microcontrollers, and along the way I will explain the difference between the microcontroller and the microprocessor. I’ll also answer the key question, for example, goat bayan , why run desktop software on a microcontroller, and finally, I’ll tell you how we were able to greatly reduce the cost of transferring this “desktop” software to microcontrollers.



What is the difference between microcontrollers and microprocessors



So one of our questions on the quest sounded and maybe under the influence of this an article appeared on a Typical programmer telling how they differ. I agree with everything in the article, but we will try to explain in our own words and make several other accents.



What is a microcontroller (micro controller unit, mcu)? If you look at Wikipedia , you can understand that a microcontroller is a system on a chip, that is, a processor and peripherals are located in one chip.



The term system-on-a-chip (System-on-a-Chip, SoC) is a broader concept than a microcontroller. This is such an electronic circuit integrated into one chip, which contains a microprocessor (CPU) and peripherals. In any mobile phone there is a chip in which, in addition to several processor cores, there is a bunch of peripherals. Even x86 processors now include the functions of bridges and even gpu. But I want to focus not on a clear classification, but on the features. Everyone knows that systems on a chip:



Not everyone knows that they are even more reliable by reducing the number of contacts and may have a larger temperature range.



The microcontroller has even greater integration and also includes memory (RAM and ROM), which is also referred to the periphery. Accordingly, the properties are preserved, consume even less, cost even less, even less productive, even more reliable.



I want to talk more about “even less productive". The fact is that on the one hand it is true to take any modern general-purpose CPU or SoC and see significantly greater performance in the so-called DMIPS . But if you think about why such a performance? As an example, I’ll give the Quake and Doom games (the creator of which John Romero was at the festival), they worked great on the Pentium MMX. But the performance of systems based on this CPU is already blocked by top-end microcontrollers. I mean STM32F7 or even STM32H7 . That is, based on these MCUs, it is quite possible to run Doom and Quake level applications, which you agree is not bad at all.



Let's talk about another feature of the MCU - internal memory. She, as I said, is located directly on the chip, and therefore very fast. But her, of course, is not enough. But how little? The STM32F769 microcontroller contains 2 mb flash (ROM) and 512 + 16 + 4 kB SRAM (RAM). In addition, for example, the STM32F769i-disco board also has external memory (128-Mbit SDRAM and 512-Mbit Quad-SPI Flash memory), which is addressed directly. I'm not even talking about the ability to connect an sd card or usb. Well, if you recall the famous phrase “640 kb should be enough for everyone”, then the reasonable question arises - why is modern desktop software so gluttonous? For example, a calculator in windows 10 in idle mode consumes as much as 16 MB of memory.



Let's move on to the next feature of the MCU, the presence of ROM. In fact, systems with a CPU also contain a ROM; execution starts from it and the BIOS starts, but the rest of the software is loaded into RAM and executed from it. In embedded systems, it is customary to execute code directly, which is called eXecute-In-Place (XIP), Linux, by the way, also supports this mode, but the main thing is to load everything into the main memory and even then execute it. The ability to execute directly from the ROM is important for two reasons: the first is the Harvard architecture, the separation of the command bus and the data bus allows you to increase the speed of program execution; the second - well, naturally, less memory is wasted, because at least a code segment does not need to be copied anywhere else.



Another key feature of the MCU is the lack of a module such as MMU , that is, microcontrollers do not have hardware support for virtual memory management . In the MCU, even not all have MPUs , and the code runs in the same address space, so it’s difficult to organize a full fork () . But fork () itself, as we wrote in the article “fork () vs. vfork () ”is a rather difficult system call, and in terms of functionality it can, in many cases, be replaced by vfork () or posix_spawn ().



Summing up, how microcontrollers differ from microprocessors, we can say the following:

In terms of hardware:





In terms of consumer properties:





Why run desktop software on a microcontroller



So, MCUs are less productive and were originally intended (and intended) for control tasks. So why run software designed for another class of systems on them? Well, even if their power is already enough for such tasks, but these tasks can be solved in the traditional way for microcontrollers. That is, to use various small RTOS or write code from scratch, what is called bare-metal. There is an obvious answer, initially the management tasks were very simple, now the requirements for functionality are growing rapidly. Even from some seedy light bulb they expect control over the Internet, I'm not talking about the kettle, they will probably soon select the composition of tea according to the mood of the owner and heat the optimum amount of water to the optimum temperature in order to improve the environmental situation. :)



Therefore, it is not surprising that attempts are constantly being made to use the already developed software in microcontrollers. For example, one of the first and widely known attempts to run Linux on small platforms is ucLinux, now it is NOMMU mode mainly Linux. The idea was to run Linux on hardware platforms without an MMU, but we found out that this is one of the key differences between the CPU and the MCU.



Briefly, the advantages of using ready-made software:





How we do it at Embox



Here you understand, you can talk for a very long time, but again I will try to outline the main points in thesis and in an accessible way.



The first one. Embox uses the Mybuild build system with its own language for describing modules and systems, we wrote about it in the article “Mybuild - a build system for modular applications” . This assembly system allows you to not include anything superfluous in the image, or rather include only what is required. You can really set the system characteristics very finely, for example, to run PJSIP on STM32F7, we limited the number of ethernet packets to 16 by simply writing a line in the configuration file for the system

include embox.net.skbuff(amount_skb=16)
      
      





Mybuild analyzes dependencies and generates various artifacts, including header files, linker scripts and Makefiles. All this allows to significantly reduce the amount of memory required and not to include those parts that are not used at all.



The second one. Obviously, Embox must support POSIX and it is supported. Moreover, we have implemented our own standard library, because it should also be rebuilt depending on the configuration of the system.



The third. Static linking. In order to avoid problems with a single address space, reduce the image size and include only the required parts, we use a static link. At different stages, different parts of the system are assembled, but finally everything (the kernel, all subsystems, libraries and applications) is linked into a single image. It has all the information about all the characters in the image. And although at first glance we have many applications with standard int main (..) entry points, but in the resulting image, these will be different characters that will be available on the system from the command line.



Fourth. We had to implement a runtime for C ++, since many popular applications and libraries use advantages.



Fifth. We added the ability to use the build system

  ./configure; make; make install
      
      





After downloading the source from somewhere and applying the necessary patches.



Conclusion



Today, the functional requirements for systems that were previously made on microcontrollers have grown significantly. Control systems are expected to be compatible with universal systems. The characteristics of microcontrollers have also grown significantly, which allows you to run software on them with the corresponding functionality. Historically, software for microcontrollers for optimization purposes was written for a specific task, but with increasing functional requirements it is necessary to apply approaches from the world of universal systems that can greatly increase the reliability of software, namely, reuse of modules, distributed development. Universal software, although it does not take into account the features of embedded systems, but has significantly greater and well-functioning functionality. Therefore, the use of desktop software on microcontrollers gives a gain. Indeed, on the one hand, you can use a cheaper, more reliable and less consuming hardware platform, and on the other hand, the cost of developing such systems is significantly reduced.



All Articles