Modern digital devices consist of dozens, or even hundreds, of electronic circuits. To ensure the switching of signals between them and to control their operation, microcontroller units (MCU) are used – logical electronic components that combine various hardware modules, such as computing chips, ROM, RAM, bridges, voltage regulators, timers, clock generators, and I/O port controllers. The most elementary option of using an MCU is turning an LED on when some condition is met. Of course, we can provide a more complicated example: a microcontroller can also be used to control a robotic manipulator. In this article, we discuss the basic issues of programming microcontrollers (MCU firmware creation).
Applications for MCU Firmware
The widespread availability of microcontrollers and the simplicity of working with them expand the scope of their application almost infinitely. In particular, MCUs can be used in embedded systems, toys, industrial machines, home appliances, and home automation projects – that is, where it is not pure processing power that is needed but rather a balance between price and functionality.
Such microcircuits are of particular interest for radio fans, due to the fact that they do not require soldering parts to change functionality, as is the case with logical microcircuits. Since the functionality of microcontrollers is determined by the firmware, these devices can be used repeatedly to implement tasks of various kinds: the same controller can control the lights in your apartment and the flight of a quadcopter.
Pros and Cons of MCU Firmware
In fact, MCU firmware is a program that consists of a series of simple instructions aimed at processing data. The vast majority of such firmware works on the principle of a simple reactive scheme: reading incoming information, monitoring the current state, developing a package of outgoing instructions, and sending those outgoing instructions. Sometimes, however, it is also required to process data in memory, change it, or in some form transfer it to another device – for example, an LCD display or a serial port.
Advantages of MCU firmware:
- The breadth of implementation options. Programs for microcontrollers can be written both in high-level (C, Java), and low-level (Assembly) programming languages. The choice is rich enough so that any enthusiast can choose the most convenient option.
- Low entry barriers. Even a beginning programmer having zero development experience can copy ready-made code from the internet and run their own automation solution. Further, as work on the device progresses, its functionality can be extended by changing the code line by line (in most cases, understanding it is not difficult at all).
- For home experiments, the cheapest controller from Aliexpress would suffice.
- We already pointed out above that microcontrollers allow you to make functional changes on the fly. All you have to do is rewrite the firmware code.
Disadvantages of MCU firmware:
- Unsuitability for tasks requiring high computing power. When planning to create an automation solution based on a microcontroller, you still need to be aware that it can perform only simpler tasks that do not require serious processing capacities. If your program code contains multiple cycles and a large number of conditions, the device will start to slow down and, thus, minimize the level of UX.
- Possible security vulnerabilities. Did you know that last year, vulnerabilities were discovered in Emerson’s ControlWave microcontrollers? This raises certain doubts about the reliability of the MCU concept as a whole. Lack of thought in the firmware sometimes allows attackers to gain control over the MCU (note that some of these attacks can be carried out without even knowing the firmware code and microcontroller model). One of the simplest in terms of implementation is a DoS attack on MSP In the case of the AVR architecture it is possible to loop the code, and the vulnerabilities contained in some industry-leading MSP microcontrollers, for example, make it possible to execute third-party code from RAM. Typically, attackers establish a wireless communication or use a universal bus, connecting directly to the input/output ports of the MCU.
Advice for Projects: Getting Ready to Develop MCU Firmware
Typically, MCU firmware is created using integrated development environments (IDEs) that contain specialized compilers (for example, PonyProg). After compilation, the executable module is loaded into the ROM of the microcontroller using an electronic programmer. These are electronic devices that perform the task of transferring the adapted code from the computer to the microcontroller via a USB, LPT, or COM port.
USB port programmers are quite difficult to implement and operate, and the LPT port is almost completely obsolete. For this reason, programmers with a COM port are the most common on the market. USB–COM transceivers are used to connect them to modern computers, most of which are not equipped with COM ports.
Accordingly, before you start writing MCU firmware, you have to decide on two things: the choice of controller and the corresponding programmer.
The Nuances of Choosing a Microcontroller
This is perhaps the easiest step in preparing for the creation of MCU firmware. Due to the wide selection of offers on the market, we advise you to pay attention to the following factors:
- affordable price;
- positive reviews;
- the presence of detailed documentation and ready-made firmware options for solving various problems.
In particular, look at the wide model line of Microchip Technology microcontrollers – they are characterized by low power consumption and high quality, and are popular among home automation experts.
The Nuances of Choosing a Programmer
Most beginners opt for Atmel AVR microcontrollers. They are inexpensive, versatile, use the ISP (In-System Programming) interface, and, importantly, do not require drivers in most modern desktop operating systems. Note that the main advantage of choosing a programmer with an ISP interface is that you can physically connect the parts first, and only then write the firmware (and you will not need to remove the microcontroller from the already assembled circuit).
Coding MCU Firmware
Experienced programmers recommend using the Linux OS for development (there are a lot of reasons for this, but the main ones are the low entry barriers and high security). Additionally, you would need:
- Avrude, a console program for reading, modifying, and writing the contents of the AVR architecture microcontrollers’ memory using in-circuit programming technology;
- A compiler/IDE for your preferred language.
Separately, we must note that the first program may require increased access rights to the programmer. For these purposes, you will need to specify udev rights, which may vary for each individual version of the OS. You may also want to install the AVR Eclipse environment to allow you to perform all the manipulations from a convenient visual interface.
As for the choice of a specific language, here each developer must make their own choice based on their personal preferences. Some like the wide selection of methods and the abstractness of the C language; some get more benefit from using the AVR assembler, which manipulates the registers and blocks of the microcontroller directly.
By the way, we strongly recommend visiting the AVR Tutorials site (if you decide to use AVR microcontrollers) in order to clarify the situation with writing code – at least you will have an accurate idea of what, how, and why you need to write.
The internet is full of stories about ordinary users successfully creating MCU solutions for home automation. If you are interested in a professional approach to creating a project based on a microcontroller, contact us! Our experts have tremendous expertise in this industry, so they can easily solve even the most difficult task.