cancel
Showing results for 
Search instead for 
Did you mean: 

Why STM uses HAL?

Agde.1
Associate III

Hello!

Can anybody explain why STM use HAL and what is the difference and hierarchy between low layer API, CMSIS, HAL, SPL ?

I always worked with just header files, but now all distributes API and I cant construct these API and their necessary in my head.

Please help!

5 REPLIES 5
Nikita91
Lead II

SPL is out of date. The HAL ancestor.

STM offers the HAL in order to allow basic users to use the resources of the MCU. Like any Hardware Abstraction Level library it tries to be generic and therefore it is complex and inefficient. In addition, you must adhere to the architecture chosen by ST, which may be very different from what you would have done.

For complex peripherals (USB, Ethernet...) this is the best way to use them proposed by ST.

There are also LL (Low Level) libraries. It is a set of declarations and utility functions.

Useful declarations concern for example the bit fields in the registers, which are missing from the .h files provided by ST (they only declare the bits individually).

There are very interesting LL functions, for example to find many system frequencies. This can be done by tracing the system clock source, the PLL coefficients, then the divisors of the various buses. But it already exists and it's easy to use.

For peripherals like I2C the LL makes it easy to configure the CCR register. It is also easy to configure a UART, which is quite complicated in a recent MCU where each UART can have several clock sources (or only one!).

There are also failures, such as EXTI management.

For me the LL is a toolbox in which you can find interesting things to write your own drivers. The use of constants and LL functions makes it possible to self-document the code because the names are very explicit. And most of the time the LL is efficient.

The big question is why the HAL is not LL-based?

It's allowed to mix HAL and LL, then you get twice the same features!

The best way to debug software is to use it, and ST avoids doing that!

CMSIS are files related to the CORTEX-M core: https://developer.arm.com/tools-and-software/embedded/cmsis

Rest assured, many people develop themselves the API they need: it is more suited to their specific needs and more efficient. Reading the LL or HAL code often makes it possible to clear up obscure points in the documentation.

Probably because remembering all the register level minutia across all STM32 chips/families is a fools errand.. Certainly makes my head hurt and fill with junk. The HAL offers a means to provide portability across designs with some functional discontinuities.

There are still include files with register level structures if you code that way, but these aren't 8051's, and there are multiple peripheral instances, and these are best represented with C structures, and not a million #defines

The thing with software where you have the source is that you can use the bits you like/want, dispense with the crap you don't and code something more application appropriate where needed.

Do what allows you to work most efficiently, and don't get hung up on how other people and other platforms solve this. There are many paths to success for those who can think their own way through the problem space..

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..

>>SPL is out of date. 

True, but I've got no plans to refactor functional code, on functional products, to chase a newer and bug ridden abstraction layer.

The SPL was/is a very solid platform from which to build something, still today.

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
Agde.1
Associate III

Yes, I agree, more complex devices requires at least some driver. But I still dont understand why do not use standartizied access struct like in CMSIS uses, for example

And why SPL is solid platform?

I don't know what do you exactly mean by "standardized access struct like in CMSIS", but nothing prevents you to use direct register access, and the CMSIS-mandated device header provides you with most of the symbols needed to avoid using "magic numbers".

ST does not produce examples using that approach, because it costs money (time, other resources) and Cube/HAL (plus CubeMX, to which Cube/HAL is a vehicle) is a better way to sell chips, as it impresses managers (who actually buy chips) more. Also, users, especially newcomers, for some reason I don't understand, require "libraries" to access everything. I suspect flawed education, but have no proof for that.

I hear the same argument in favour of Cube/HAL Clive used above, and I don't dismiss it entirely, but IMO it's not a compelling argument enough.

JW