Summary of the first lecture on the programming of modern microcontrollers on the example of STM32 and the operating system RIOT.Lectures are given at the Institute of Information Technologies MIREA on Saturdays, from 12:50 in the auditorium on the 4th floor of Building D. The classes are given 1.5 hours for the lecture itself and 3 hours for practical training in the IoT laboratory of the Samsung Academy on the lecture topic.
Hi, Giktayms! As we promised, we begin the publication of lecture notes, which are now being read at the IT Institute of MIREA. According to the results of the first introductory lecture, we decided to slightly change the structure of the course - instead of the planned two streams of 5 classes there will be one stream of 7 classes. This will allow for a quieter pace to sort out a number of auxiliary issues, as well as articles with a summary will appear on the GT every week throughout March and April, and not a week later, as planned earlier.
Nevertheless, in seven lectures it is impossible to completely settle such an extensive topic, therefore in some places the presentation will be abstract - although to compensate for this, we will try to indicate in which direction to look to those who want to sort out this or that issue on their own. ')
The course is designed for second and third year students who are familiar with the C language and basic concepts of electronics and electrical engineering. Prior acquaintance with microcontrollers is not required.
The goal of the course is the development of skills that allow working freely with microcontrollers on the ARM Cortex-M core at the modern level and, if so desired, move in the direction of further deepening their knowledge.
Today’s lecture is the first, so common concepts will be understood: what is a microcontroller in general and why is it needed, what is firmware and how does it work out, why do we need an operating system, and finally how to work with git. The result of a practical lesson is your own GitHub repository with OS source codes, as well as a successfully configured build environment on the local computer.
Microcontroller
In short, the microcontroller is a classic example of a “system on a chip”, which includes both the processor core and a set of auxiliary and peripheral devices, allowing the microcontroller to be completely self-sufficient in many cases.
In a typical microprocessor, similar to what is in any PC or smartphone, almost all modules that can be attributed to auxiliary (power, clocking, even basic peripheral devices) are brought out of the chip itself, despite the fact that working without them is a microprocessor can not.
In the microcontroller, on the contrary, not only the subsystems necessary for its operation, but also the mass of peripheral devices, which may be required in various practical tasks, are implemented on the same chip as the core. Moreover, many manufacturers of microcontrollers do not compete with each other in terms of core performance or memory size, but in abundance and functions of peripheral devices.
Microcontrollers have been developing for a long time in parallel with microprocessors - the Intel 8051 architecture, which is still found in industrial products, was developed in 1980. At some point, their development lines begin to overlap with microprocessors - for example, older microcontroller models have interfaces for external RAM, and microprocessor manufacturers integrate more and more peripherals onto the chip (just remember that at the dawn of the "personal computer" even the cache memory was accumulated by external microcircuits) - but in any case, they remain two significantly different branches of development.
Actually, the purpose of creating microcontrollers was the possibility of reducing the cost and miniaturization of various devices that require some small computing power: using a single chip, for which it’s simple enough to apply power, makes it much easier to design and manufacture a PCB compared to a set of 4-5 individual chips .
Of course, the microcontroller has its limitations - it is technically impossible to pack in one crystal that in a large PC takes up half the rather big board.
Operating frequencies rarely exceed 200 MHz, and more often are in the region of tens of megahertz.
The amount of RAM is within a megabyte, and more often around tens of kilobytes.
The amount of memory programs - within a megabyte, and more often - in the region of tens to hundreds of kilobytes.
As part of the course, we will work with STM32L151CC microcontrollers with 32 KB of RAM, 256 KB of ROM and a maximum operating frequency of 32 MHz (on the Nucleo-L152RE boards there are slightly more serious chips - 80 KB of RAM and 512 KB of ROM).
Memory
In general, there can be four types of memory inside a microcontroller:
Permanent memory (flash memory) is used to store user programs and, sometimes, some settings of the microcontroller itself. If, when referring to the characteristics of the microcontroller, the amount of memory is written, without specifying which one - as a rule, this is about flash. The contents of the flash is not reset when power is lost, the storage period for information in it under normal conditions is usually at least 10 years.
RAM is used to execute a user program and store “momentary” data. RAM is always reset when you restart or turn off the power, and may also not be saved when entering certain sleep modes. Microcontrollers often do not have a clear separation between program memory and data memory — as a result, the term “run from RAM” can be found, which means that not only data but also the program itself are in RAM; however, these are quite exotic cases.
Eeprom Also applies to permanent memory, but differs significantly from flash memory with its characteristics. Flash has two big drawbacks, which make it very inconvenient for saving some current data from the program - firstly, the flash has a limited number of rewrites of the same cell, and secondly, you can often work with flash only whole pages, which hundreds of bytes in size, even if you only need to overwrite one byte. EEPROM of these shortcomings is deprived - its service life is usually ten times longer (from 100 thousand to 1 million overwrites), and you can work in it with each byte separately. For this reason, EEPROM is used for permanent storage of data generated by the program itself (measurement archives, program settings, etc.), its typical size is in units of kilobytes, but it is not in all controllers.
System memory Areas of permanent memory, inaccessible to the user for writing, and recorded in the production of the microcontroller. Usually they contain the executable loader code (described below), but it can also store any calibration constants, serial numbers or even auxiliary libraries for working with peripheral devices.
You can look at the organization of the memory of a particular controller in its datasheet. For example, here is the datasheet on the STM32L151CC , on page 51 of which a memory card of this family is presented.
It is not difficult to notice that all four types of memory about which we spoke occupy a very small piece of the card - and the list of all peripheral devices in the controller is located on the most part of the picture.
Registers
The fact is that everything - in general everything - communication with all peripheral devices of the microcontroller and all its settings is carried out using only two operations:
read the value at the given address
write the value to the specified address
Everything inside the microcontroller is necessarily mapped to some address. These addresses are called registers (do not confuse with registers of the processor - there are data in the registers of the processors on which the processor performs operations; in the registers we are talking about there are some special data that are displayed in a specific way on the state of the various hardware units of the microcontroller).
So, for example, if we want the third leg of the port A of the microcontroller (PA2, the numbering comes from zero) to appear “1”, we need to write “1” to the third bit of the register located at 0x4002014. And if this leg is configured as an input and we, on the contrary, want to know what value is on it, we need to read the third bit of the register at 0x40020010.
Yes, in order to indicate to the controller, this foot is the input or output - it is necessary to record the corresponding values ​​in the corresponding bits at 0x40020000.
This is an important point in understanding the work of the microcontroller: absolutely everything that is not computational operations, for which the processor core itself is responsible, is done by writing or reading one or another register. Whatever libraries are in the top of your program - in the end, it all comes down to registers.
Of course, working with numeric addresses is rather inconvenient, therefore, for each Cortex-M core microcontroller, there is a CMSIS (Cortex Microcontroller Software Interface Standard) library, the most important component of which for us is the header file that describes the relatively readable registers in the particular controller the names.
With CMSIS, the PA leg described above will look like this: