Hello Gerry,
Let’s explore the STM32 HAL (Hardware Abstraction Layer) with a bit more depth.
As a starting point it’s useful to contrast the STM32 HAL with the well-known Arduino environment. In both cases abstraction is given high priority. Here the term abstraction allows you to move across different physical hardware with the same family without changing your code. For example, in Arduino the digitalWrite(pin, value ) function will work on every single Arduino from the classic UNO to the newest 32-bit Arduino. Likewise the SMT32 HAL command HAL_GPIO_WritePin(port, pin, value) will be the same across members of the STM32 family.
This abstraction is highly desirable as it reduces the amount of work required to understand and then attempt to manipulate the chip specific peripherals.
Stepping back, we need to realize that the STM32 is no Arduino. The most powerful members of the STM32 family have a rich and complex set of peripherals and consequently require an equally complex HAL. As of this writing, the HAL user manual UM1884 is 2604 pages long. This massive document will tell you everything you want to know about configuring the uC peripherals. However, it’s a bit of a learning curve if you are just getting started.
May I recommend you start your exploration using the Code Configurator Tool located within the STM32CubeID. This is a graphical tool that allows you to set individual functions for your chosen uC. For example, here is the window for the ADC.
At this point you may be asking what this graphical display has to do with HAL.
It provides working examples!
When you tell the tool to save your configuration it will automatically generate code in the main.c function. This code is built on top of HAL. Here is an example of the computer generated code:
/** Configure Regular Channel
*/
sConfig.Channel = ADC_CHANNEL_6;
sConfig.Rank = ADC_REGULAR_RANK_1;
sConfig.SamplingTime = ADC_SAMPLETIME_2CYCLES_5;
sConfig.SingleDiff = ADC_SINGLE_ENDED;
sConfig.OffsetNumber = ADC_OFFSET_NONE;
sConfig.Offset = 0;
if (HAL_ADC_ConfigChannel(&hadc1, &sConfig) != HAL_OK) {
Error_Handler();
}
Observe that the HAL_ADC_ConfigChannel( ) function is passes a structure containing the appropriate configuration fields. This is a typical HAL operation. I would also argue it’s one of the impediments to getting started with the STM32 and HAL.
As we conclude, let me leave you with this code that I use from within main( ) to retrieve the ADC values:
Select_ADC_Channel(ADC_CHANNEL_6);
HAL_ADC_Start(&hadc1);// Blocking ADC conversion ***** 12-bit ADC
HAL_ADC_PollForConversion(&hadc1, 100);
int ADCVal6 = HAL_ADC_GetValue(&hadc1);
Select_ADC_Channel(ADC_CHANNEL_8);
HAL_ADC_Start(&hadc1);
HAL_ADC_PollForConversion(&hadc1, 100);
int ADCVal8 = HAL_ADC_GetValue(&hadc1);
Best wishes in your exploration of FreeRTOS. It’s an impressive piece of code.
Sincerely,
APDahlen