Electronic – STM32 HAL drivers

cmsishal-librarystm32

I've been using STM32 ARM microcontrollers for more than two years, the best way of coding that I've found is using the CMSIS and ST std peripheral libraries, and I think it has no restrictions or any main disadvantages.

But now I want to try HAL drivers to compare with std libraries. It seems that ST has introduced a tool, "STM32CubeMX", that could graphically configure the microcontroller and generate code based on HAL drivers. My problem is that I don't want to use their graphical tool to generate my codes, but after many searches on the Internet I couldn't find any tutorial or example to use HAL drivers directly in my project.

However ST has an user manual that describes anything about HAL drivers, but unfortunately it is about 1300 pages and I think it takes me much time to read.

I really prefer a short instruction or maybe a simple example project for using GPIOs.

Best Answer

If you do not want to use the STM32CubeMX code generator tool but still need simple example projects then you should use the STM32Cubexx firmware examples that are available for all STM32 series.

Each link has an application note called "STM32Cube firmware examples for STM32xx Series" (example for F4) which is actually a huge table listing all the available examples for all the development boards.

Also there are the user manual for each series called " Description of STM32xxxx HAL drivers" (example for F4). Yes, this is the 900 - 1300 pages long document but you do not have to read through all of it, just open up the relevant part. There are a short step by step guide at the beginning of every chapter. In case of the GPIO Generic Driver:

27.2.2 How to use this driver

  1. Enable the GPIO AHB clock using the following function: __HAL_RCC_GPIOx_CLK_ENABLE().
  2. Configure the GPIO pin(s) using HAL_GPIO_Init().
    • Configure the IO mode using "Mode" member from GPIO_
    • InitTypeDef structure Activate Pull-up, Pull-down resistor using "Pull" member from GPIO_InitTypeDef structure.
    • In case of Output or alternate function mode selection: the speed is configured through "Speed" member from GPIO_InitTypeDef structure.
    • In alternate mode is selection, the alternate function connected to the IO is configured through "Alternate" member from GPIO_InitTypeDef structure.
    • Analog mode is required when a pin is to be used as ADC channel or DAC output.
    • In case of external interrupt/event selection the "Mode" member from GPIO_InitTypeDef structure select the type (interrupt or event) and the corresponding trigger event (rising or falling or both).
  3. In case of external interrupt/event mode selection, configure NVIC IRQ priority mapped to the EXTI line using HAL_NVIC_SetPriority() and enable it using HAL_NVIC_EnableIRQ().
  4. To get the level of a pin configured in input mode use HAL_GPIO_ReadPin().
  5. To set/reset the level of a pin configured in output mode use HAL_GPIO_WritePin()/HAL_GPIO_TogglePin().
  6. To lock pin configuration until next reset use HAL_GPIO_LockPin().
  7. During and just after reset, the alternate functions are not active and the GPIO pins are configured in input floating mode (except JTAG pins).
  8. The LSE oscillator pins OSC32_IN and OSC32_OUT can be used as general purpose (PC14 and PC15, respectively) when the LSE oscillator is off. The LSE has priority over the GPIO function.
  9. The HSE oscillator pins OSC_IN/OSC_OUT can be used as general purpose PH0 and PH1, respectively, when the HSE oscillator is off. The HSE has priority over the GPIO function.

Based on this you can look up the function descriptions as well in this document and if you really need an example then there are the already mentioned STM32Cubexx example packages and you did not have to use the STM32CubeMX tool at all.