Skip to content

General concepts

In general it is not feasible to develop programs directly on the microcontroller since normally it is not powerful and big enough to host a compiler system including editors shells, linkers,...

Moreover, the CPU architecture of a microcontroller differs significantly from the CPU on the PC where development takes places (PC or Mac). Hence, it is not possible to develop a program on the PC, compile it with the standard toolchain of the PC (i.e. with the compiler and linker to make executable programs for the PC) and then download the compiled binary to the microcontroller.

This is why programs for microcontrollers are developed with so-called cross-compilers which are included in "software development kits" (SDK) for the microcontroller.

A cross-compiler is a compiler which runs on a development system (e.g. a PC/laptop) but compiles code into a binary format for a different system (i.e. the microcontroller). The compiler is the heart of the SDK. The SDK contains the essential libraries to generate programs for the microcontroller using the specific hardware resources (i.e. libraries to access i2c ports or GPIO (General Pupose IOs) or ADCs etc. but also basic libraries like the standard C library "libc"). In addition the system contains everything which is necessary to communicate with the "target device" (i.e. the microcontroller) so that the compiled application can be downloaded and executed and possibly debugged on the target system. Usually this is done by communicating via a serial line (UART) over a terminal.

The most widely used computing language for microcontrollers is "C" or "C++". To write efficient (i.e. fast running) programs it is necessary to write code in one of these languages. Many microcontrollers can also be programmed in assembler code (i.e. code which uses directly the instruction set of the microprocessor), which can result in very fast and highly optimised code taking the minimum amount of space. However, programming a microcontroller in assembler becomes complicated for large projects since the programmer has to take care of everything which happens in the program. For example there is no concept of variables in assembler code. Every bit of data which has to be stored for later usage has to be written to some memory location and that location the programmer has to memorise for later usage. Assembler code can be mixed with C code so that critical parts of the program which need the highest degree of optimisation can be written in assembler, whereas for the the rest of the programme a higher level language like C or C++ can be used.

Modern controllers sometimes can also be programmed in even higher level languages like python. For the microcontroller used in this course (ESP32) a "micropython" system exists which allows to write programs in python. These programs are then interpreted and executed on the microcontroller. Of course the micropython interpreter has to be installed on the microcontroller for this to work. This way of operation is much less efficient (=fast) then C programs but with the power of today's microcontrollers it is largely sufficient for many applications (notably IOT applications where speed is usually not an issue). The micropython system comes with python modules to easily access the peripherals of the microcontroller so that many features of the controller hardware can be easily used. However, note that usually some features which are accessible with the native SDK system, might not be available in micropython. (Examples of these missing features for the ESP32 microcontroller: The I2S interface (for audio data streams) cannot be configured to use PDM (pulse density modulation). (In PDM the density of a series of pulses represents the analogue value of a voltage. Since the frequency of the pulses is very high a very accurate representation of the analogue signal can be achieved. It is trivial to turn a PDM signal into a analogue signal which can be fed into an amplifier or a headphone: you just need to connect a low pass filter to filter out the high frequency component of the base pulse frequency which is much higher than the audible frequency range (usually in the Mhz range). But even connecting a headphone directly to a PDM stream results in a reasonable quality audible audio signal). Another missing feature of the micropython port for the ESP32 is the support for the hardware pulse count controllers of the ESP32 (see chapter 17 of the technical reference manual).

Since most of you probably know python but not all of you know how to program in C or C++, this course will use the micropython environment of the ESP32 during the exercises.

Arduino eco-system

A very famous SDK for many different microcontrollers is the Arduino system. Different types of microcontrollers can be targeted with this system. The Arduino SDK offers you to choose from a huge database of development boards (a development board is a small circuit board which contains the microcontroller and some essential electronics around to provide power, interface via a USB cable to the host computer, may be some buttons for reset or entering a special "boot mode" at power up, one or more LEDs...) If a board is chosen by the user, the Arduino system magically downloads and installs the SDK for the board together with some software libraries. Programs written on the Arduino system for one type of microcontroller can be very easily ported to another microcontroller (provided that it has similar hardware features). Arduino provides a huge set of libraries to use the peripherals of the microcontrollers and the function calls (called the API = Application Programmer Interface) of these libraries are identical for many different microcontroller types. Under the hood the Arduino libraries call the SDK functions provided by the different microcontroller manufacturers, since these libraries "know" how to deal with the specific hardware features of the microcontroller.

The Arduino system is easy to use and very well documented. From a didactic point of view it is a bit "too good" in the sense that it hides a lot of complexity and a lot of typical procedures when using an SDK. Everything happens with some magic clicks on some few buttons. For this reason we will not use the Arduino system in this course, however, if you are motivated to continue to "play" with microcontrollers at home it is definitely a system to look at seriously, also since you can exploit an enormous amount of code and libraries written by others.

References

[1] Technical Reference Manual of the ESP32