Microcontroller Hardware Abstraction Layer (HAL) in Education

Why are microcontrollers so difficult to program?

We could argue that C programming is easy, it’s the Special Function Registers (SFRs) that are complex. The microcontroller is difficult to program as the associated hardware is increasingly complex. This requires the programmer to first understand the hardware’s function and then control the hardware.
Recall that microcontroller hardware is controlled by many SFRs, many of which have a subtle and non-intuitive impact on the hardware. For example, to use an Analog Digital Converter (ADC) the programmer must set or clear SFRs for the reference voltage, conversion clock, channel select, and justification (left or right). An advanced programmer may also configure SFRs triggering an interrupt when the ADC conversion is complete. For high performance microcontrollers, the programmer may configure a Direct Memory Access (DMA) mechanism to handle the ADC result independent of the main CPU.

This SFR complexity can be measured by the heft of the datasheet. As an example, consider the new 8-bit Microchip PIC16F13145T-I/SO. The datasheet is 604 pages long with 46 pages dedicated to the ADC. Contrast this with the Renesas R7FA6M5BH2CBG#AC0 with its 2276 page hardware user manual of which 85 pages are dedicated to the ADC. To be sure, these two examples are on opposite ends of the microcontroller spectrum. However, the point is clear. Microcontroller hardware is complex. We know that the datasheet material is dense and it’s very easy to miss a SFR thereby disabling the module or reducing its performance. The hardware abstraction layer is designed to mitigate this complexity.

Why has Arduino been so successful?

Arduino is perhaps the best-known microcontroller Hardware Abstraction layer (HAL). As we review the Arduino Language Reference we encounter the Arduino HAL categorized as Digital I/O, Analog I/O, Advanced I/O, Time, Interrupts, and Communication. These well know functions simplify the development process as the program is isolated from the underlying SFRs. Also, the Arduino HAL agent takes care of all members of the Arduino family. Consequently, the smallest ATtiny and the largest System On Module (SOM) such as the PORTENTA Pro H7 can be programmed using the same functions. This cross compatibility and attractiveness of HAL are even found in devices outside the Arduino umbrella including industrial grade Programmable Logic Controllers (PLC). You will find that the lines between Arduino, Raspberry Pi, and industrial controllers are blurred with a bias toward program simplification and a reduction of development time.

Tech Tip: The ATtiny and RA6M5 demonstrate the commonality in the Arduino HAL implementation. They also show the limitations. The ATtiny is one step below the “standard Arduino.” For example, your ATtiny may not have the requisite hardware to run the serial and SPI interfaces. Software techniques may be necessary to implement these features. On the other hand, the RA6M5 powerhouse has considerably more hardware and features than the “standard” Arduino. An example is this recent article that unlocks some of the PWM capabilities on the PORTENTA Pro C33.

The takeaway message is that the programmer must be vigilant and have a good understanding of the hardware and capabilities of a given microcontroller but not necessarily down to the SFR level. This also suggests an education path where all engineers, technologists, and technicians should deeply explore at least one microcontroller at the SFR level. A deep understanding of one microcontroller will lead to a greater understanding of all microcontrollers. In this article I advocate for using devices on the lesser end of microcontroller spectrum with classic devices such as the 8051, MSP430, Z8, AVR, and PIC.

What trends do we see in microcontroller software development?

Without question we see a proliferation of HAL. This may take the form graphical hardware configurators such as those featured in MPLAB and Simplicity Studio. Arduino and Arduino like tools will continue to be used. There are HAL and low-level drivers such as this comprehensive offering for ST Microelectronics’ STM32 devices. The STM document shows that there are many ways to construct a HAL. The STM32 is close to the SFRs. Personally, I find it useful to explore this document side by side with the datasheet for the given STM32 processor. With the understanding that the code is still valid if we swap any given microcontroller for a more capable device.

Tech Tip: When it comes to education, we need to understand the difference between learners and experts. There is a strong temptation to teach the learner to use the same tools as the expert. Today and in the future, the HAL will dominate as the tools are reliable and reduce development time. At the same time, we need to ask how the experts became experts in the first place. Chances are very high that every microcontroller expert has a deep understanding of at least one microcontroller. They will tell you a story about the long night spent troubleshooting that one errant SFR and the adrenalin rush of finally discovering the problem at 3 AM.

Conclusion

A balanced approach to education is necessary. Perhaps this is why I occasionally encounter professors with such strong feelings for and against the HAL exclusive approach. In both cases they swing too far missing the necessary and nonlinear transition between learner and expert.

Best wishes,

APDahlen

Please follow this link for related Arduino education content.

About the Author

Aaron Dahlen, LCDR USCG (Ret.), serves as an application engineer at DigiKey. He has a unique electronics and automation foundation built over a 27-year military career as a technician and engineer which was further enhanced by 12 years of teaching (interwoven). With an MSEE degree from Minnesota State University, Mankato, Dahlen has taught in an ABET accredited EE program, served as the program coordinator for an EET program, and taught component-level repair to military electronics technicians. Dahlen has returned to his Northern Minnesota home and thoroughly enjoys researching and writing articles such as this. LinkedIn | Aaron Dahlen - Application Engineer - DigiKey