New library system built on STM32Fxxx HAL drivers

As you maybe saw, when STM32F7-Discovery board arrived, I switched to make libraries also for this board. And if you want easy libs for this boards, you have to switch to HAL drivers provided from STMicroelectronics.

HAL drivers are basically supported for all STM32 families, so I decided to make a library in a way, that will be very easily to used with any STM32 family, especially targeted to these 3 series:

  • STM32F0xx: For very basic and small project can be easily used with powerful peripheral set and with 48MHz is just series device,
  • STM32F4xx: For everything else what STM32F0xx can’t done. This family is very supported by users, so having support for this family is great advantage,
  • STM32F7xx: This should be only used if you need LCD, AUDIO, USB, TOUCH, FILTERING and some “heavy duty” stuff on single project.

New library system

My new library system fully depends on HAL drivers from ST. Currently I have support for F4 and F7 devices but will add support for F0 also in near future. So, to use my libs for HAL drivers, you must have these libraries on your computer:

  • STM32F4xx_HAL drivers included in STM32CubeF4 package in case you use F4xx series
  • STM32F7xx_HAL drivers included in STM32CubeF7 package in case you use F7xx series

Include files

You have to include all *.c files to your project to avoid any missing functions from my libs. I never checked where they are basically declared because I always add ALL libs for specific package in project and I don’t care where it is, or it is not included.

Second thing, all my libs includes at least 2 files:

  • stm32fxxx_hal.h: File, where defines for different family is set up. This file is included in my library section also and HAVE TO be used in any project. It should not be changed, or you may get into troubles. This file then includes F4 or F7 header files depending on define you have selected if family is F4 or F7
  • defines.h: Project file, where any change to my library should be done. This allows you to easily upgrade my library file without changing settings you made in library. This file is not included in library section, but every project has its own file where changes for library (if possible) are done. This file must be created by user and can be empty if no changes for library is made.
  • Other includes may be done in case that specific library needs any other libraries from me. For example, DISCO library (for leds and buttons) uses my GPIO library.

Project defines

In order, to get my libraries properly to work, you have to make some specific compiler defines (if it is possible, use these defines in options for target, global defines for your target):

  • Specify STM32 family you will use:
    • STM32F4xx for STM32F4 series
    • STM32F7xx for STM32F7xx series
    • And so on..
  • Specify STM32 family device you will use:
    • STM32F429xx for STM32F429-Discovery board
    • STM32F746xx for STM32F7-Discovery board
    • And so on..
  • Specify some other settings depending to different library you use. You should always check library for possible changes!

Available libraries

All available libraries built for HAL system can be seen here on Github. I will add them to this site when I had some more of them.

On Github account are also some examples, which can be used as reference how to use library in your project.

Let me know what you think about that down in comments. Have a nice coding 😉


Owner of this site. Also electronic enthusiasts, web developer, 3D printer fan, handball player and more. Big fan of STM32F4 devices. In anticipation of the new Discovery board for STM32F7 lines.

You may also like...

Read before commenting!

Before you make a new comment, make sure you agree with things listed below:

  • - Read post to make sure if it is already posted what you are asking for,
  • - Make sure you have the latest version of libraries used in your project,
  • - Make a clean and grammatically correct written message,
  • - Report as many details as possible, including what have you done so far,
  • - Do NOT post any code here. Use Pastebin,
  • - Do NOT post any error codes here. Use Pastebin,
  • - Specify STM32Fxxx family and used Discovery/EVAL/Nucleo or custom made board,
  • - Make sure your clock is set correct for PLL,
  • - If you are using my HAL drivers, please check this post how to start.
Comment will be deleted on breaking these rules without notification!
  • Neder

    hi Tilen Majerle i need a program detect the edge rising of two different square wave the first square wave connected on PC10 the other on PC11 on stm32f429 dico , and after that calculate the time between the two rising edge (DT) like that can u help me bro plz thank u

    • Hello,
      well this is not related to HAL drivers, but anyway. This looks like classical handling for rotary encoders.
      You can use built-in timer feature for this, OR use 2 EXTI irqs to handle interrupts when edges happen.

      • Neder

        bro i m new can u help me i do it like that but it does not work :
        if (GPIO_ReadInputDataBit(GPIOC, GPIO_Pin_10)==1)

        { timerValue = TIM_GetCounter(TIM2);

        while(GPIO_ReadInputDataBit(GPIOC, GPIO_Pin_11)!=1) ;

        sprintf(ch,”%lf seconde”, (double)timerValue);

        • Sure it does not work. When you detect pin high, save current timer value.
          When you detect PC11 pin high, save value to another variable.

          Then substract both times and you will get what you want.

          • Neder

            tks u bro for reply…… hight means 1 and low means 0 or it is false to use 1 and 0 as edge detector ????? other question i do it like that is that correct ???

            uint8_t ch[10000];
            double timerValue1,timerValue2 ,delta;

            { timerValue1 = TIM_GetCounter(TIM2);

            while(GPIO_ReadInputDataBit(GPIOC, GPIO_Pin_10)!=1) ;
            { timerValue2 = TIM_GetCounter(TIM4);

            while(GPIO_ReadInputDataBit(GPIOC, GPIO_Pin_10)!=1) ;
            delta= timerValue2- timerValue1;

            sprintf(ch,”%lf seconde”, (double)delta);

          • And what is delta? Btw..this is not related to thus post!!

          • Neder

            so sorry bro i know it is not related u said to me substract both times that is why i use delta

  • Jan

    I believe it’s about time that a version of your libraries based on HAL are made! Great job there! I am eagerly waiting for tutorials about using these HAL-based libraries in the future (if ever there will be)! Again, great job!

    • You should know that there is only one advantage of HAL drivers. They are for all series the same functions which makes easily to port also for my libs.
      But for critical applications, HAL drivers are not good choice.

      • Jan

        I totally agree. For critical applications, using ones own custom libs that are optimized for that applications is often a better choice.

  • Pingback: All STM32 HAL libraries - STM32F4 Discovery()

  • John Doe

    are the example programs from your older, TM* libraries usable with the HAL libraries?

    • No, they are not. Which lib would you need from there?

  • Pingback: HAL library 2- LEDS and BUTTON for STM32 boards - STM32F4 Discovery()

  • Pingback: HAL library 4- EXTI for STM32Fxxx - STM32F4 Discovery()

  • Pingback: HAL library 01- RCC for STM32Fxxx - STM32F4 Discovery()

  • Pingback: HAL library 1.5- GPIO for STM32Fxxx - STM32F4 Discovery()

  • Pingback: HAL library 05- OneWire for STM32Fxxx - STM32F4 Discovery()

  • Pingback: HAL library 06- DS18B20 for STM32Fxxx - STM32F4 Discovery()

  • Pingback: HAL library 08- SPI for STM32Fxxx - STM32F4 Discovery()

  • Pingback: HAL library 07- USART for STM32Fxxx - STM32F4 Discovery()

  • Pingback: HAL library 09- GENERAL for STM32Fxxx - STM32F4 Discovery()

  • Pingback: HAL library 10- CRC for STM32Fxxx - STM32F4 Discovery()

  • Pingback: HAL library 11- SDRAM for STM32Fxxx - STM32F4 Discovery()

  • Pingback: HAL library 12- LCD for STM32Fxxx - STM32F4 Discovery()

  • Pingback: HAL Library 13- Buttons for STM32Fxxx - STM32F4 Discovery()

  • Pingback: HAL Library 14- Fast Fourier Transform for STM32Fxxx - STM32F4 Discovery()

  • Pingback: HAL library 15- HD44780 for STM32Fxxx - STM32F4 Discovery()

  • Pingback: HAL Library 16- I2C for STM32Fxxx devices - STM32F4 Discovery()

  • Pingback: HAL Library 17- IWDG for STM32Fxxx - STM32F4 Discovery()

  • Pingback: HAL Library 18- RNG for STM32Fxxx - STM32F4 Discovery()

  • Pingback: HAL Library 19- CPU LOAD monitor for STM32Fxxx - STM32F4 Discovery()

  • Pingback: HAL Library 20- FATFS for STM32Fxxx - STM32F4 Discovery()

  • Pingback: HAL Library 22- BOR for STM32Fxxx - STM32F4 Discovery()

  • Pingback: HAL Library 23- TOUCH for STM32Fxxx - STM32F4 Discovery()

  • Pingback: HAL Library 21- Multi purpose USB library for STM32Fxxx - STM32F4 Discovery()

  • h.mehrabian

    First of all, tnx for ur support.
    Second, where can we find recent update of HAL Library, specially MCD Application Team(direct site)?

  • Pingback: HAL Library 25- NRF24L01+ for STM32Fxxx - STM32F4 Discovery()

  • Pingback: HAL Library 26- Power Voltage Detector (PVD) for STM32Fxxx - STM32F4 Discovery()

  • Pingback: HAL Library 28- Digital to Analog converter for STM32Fxxx - STM32F4 Discovery()

  • Pingback: HAL Library 29- Analog to Digital converter for STM32Fxxx - STM32F4 Discovery()

  • Pingback: HAL Library 30 - MPU6050 for STM32Fxxx - STM32F4 Discovery()

  • John Doe

    Hi you have a typo in tm_stm32_esp8266.c – #include “tm_stm32f4_esp8266.h”. There is no such file in 00-STM32_LIBRARIES. Should be #include “tm_stm32_esp8266.h”, no?

  • Kris

    super lib but can you show how start it with TrueStudio. It is now without limit. That’s good environment.
    Will be best if you show example project it in tree structure.

    • I have original license from them, but because of some strange project organizations on their site I can’t make a good and practical examples with it.

      I had already a conversation with their support and now I’m waiting for them to return me a email message.

      So, I will make an example when I got it useful in working state.

      • Kris

        Ok I understand.
        I and probably many other peaple will be wait.


  • Dmitry Kudriavcev

    Hello! i`m trying to start lcd display with ur library. could u write small guide for berinners how to add libraries to clean project. couse i have a lot of errors. like reference not found. =.
    i try use Cubemx for conf SPI and generate code. import to SW4STM(everything ok) then add all libs from a did rirght way?
    and u have #include “stm32f4xx_rcc.h” instead of #include “stm32f4xx_hal_rcc.h” from drivers. with version i should use?
    than a lot!

    • Find LCD library for HAL and start from there. HAL librariy does not include file you said for sure 🙂 can project sample on my Github so I don’t see a problem using it. Direct Keil opening. Check what there is and use the same. Simple I think.

      • Dmitry Kudriavcev

        i try to run examples with HAL, but i have 411 not 429…
        can i change smth in this project for running my ILI9341 display?

        • Project is not for HAL.

          Any yes, project on link uses SPI for interface, you can use it with F411 too.

          • Dmitry Kudriavcev

            but HAL library 12- LCD for STM32Fxxx

          • Is not for SPI interface, but for RGB which is not available on your board.

          • Dmitry Kudriavcev

            could u tell best way to run ili9341+f411 with HAL.
            my brain gonna destroy…

          • Build SPI based library.

          • Dmitry Kudriavcev


          • Do it yourself was my thought, I guess.

          • Dmitry Kudriavcev

            im beginer,..
            can i use tm_stm32_lcd.c for this?

          • Second time Dmitry. You CAN’T.

          • Dmitry Kudriavcev

            thanx. last question: any kind of library here can be useful for me?

          • You can:

            1. use library 08 for standard peripheral drivers
            2. download SPI and GPIO libraries for HAL
            3. edit library 08 with libraries for HAL

            or use STD drivers always and not HAL.

          • Dmitry Kudriavcev

            yeah. i`m waiting this answer from the beginning. tnx!

          • I thought you are able to figure this by yourself.

  • bob

    Hi Majerle,
    I programmed into my STM32F4_Discovery board the hex file…
    but Windows 7 (32bit) failed to “enumerate”.
    The STMicroelectronics Virtual-com-port driver IS ALREADY installed and works with my STM32F103xx devices.
    What else do I need to do?
    Green LEDS; LD4 and LD7 both ON when I connect CN5 to Windows USB.

  • Pingback: HAL Library 31- DMA STM32Fxxx devices - STM32F4 Discovery()

  • Mahmud Ibrahim

    Hi Tilen,
    Your HAL Library is good. Do you have any plan for STM32F1xx series?? Because you see there are a lot of small sized development board are present in the market with STM32F103C8 chip. I think it would be great if you create a section of STM32F1xx series. Apart from that, nice and well organized tutorial. Thumps Up!!!! I have a question. I want to add STM32F051 Discovery board on HAL Driver library of yours. Every thing works fine. But when the keil compiler building the hex, its choose stm32f091_startup.s file as a startup function instead of stm32f051_startup.s. How i can solve this problem??

    • Hi Mahmud,

      I don’t have in mind these devices right now since I’m too busy for starting this. Maybe later, I can do for start with basic libs like GPIO, SPI, USART, I2C and similar. There are differences in libs like DMA, FLASH, and some others.

      For this chip and keil problem. How do you work with F051? Do you have my project template or how did you build project?

      • Mahmud Ibrahim

        Yes. I do have your project template. I am using it.Please take a look on this picture. Here, you can see that I am using STM32F051 Discovery board. Already add STM32F051 in file extension bar of keil. While compiling the code, you see “startup_stm32f091xc.s” (red marked) is assembling instead of “startup_stm32f051x8.s”(black marked). I have changed the target name and also the C/C++ definitions of this project(red marked). Will you please help me to find out where I am making mistake.

        • Now I see. You did that target by yourself, but you did not enable F051 file to compile in that target and ignore F091. You must do it manually.

          • Mahmud Ibrahim

            Will you please guide me how to do that??

          • Right click on file,and then options. There you have some checkboxes. For one file, you have to exclude from build, for second file you must check it to build.

          • Mahmud Ibrahim

            Thank you so much for your support.

          • Mahmud Ibrahim

            Thanks Tilen. I solved it. 😀

  • Сергей

    Hi Tilen, are you sure everything OK here?

    uint32_t TM_BUFFER_ReadString(TM_BUFFER_t* Buffer, char* buff, uint32_t buffsize) {
    	uint32_t i = 0;
    	uint8_t ch;
    	uint32_t freeMem;
    	/* Check value buffer */
    	if (Buffer == NULL) {
    		return 0;
    	/* Get free */
    	freeMem = TM_BUFFER_GetFree(Buffer);
    	/* Check for any data on USART */
    	if (
    		freeMem == 0 ||                                                   /*!StringDelimiter) < 0 && /*!< String delimiter is not in buffer */
    			freeMem != 0 &&                                               /*!< Buffer is not full */
    			TM_BUFFER_GetFull(Buffer) < buffsize                          /*!< User buffer size is larger than number of elements in buffer */
    	) {
    		/* Return 0 */
    		return 0;
  • Сергей

    Hi Tilen, are you sure that everything OK here? I’m talking about check conditions in if statement.

    • Am i on exam or what? Tell what you wanna tell.

      • Сергей

        Why it have to fail if there is no free space in buffer? And comment is inconsistent with code.

        freeMem == 0 ||                                     /*!< Buffer empty */
      • Сергей

        So, what you will say?

        • I think you are so good, I don’t have even time to see email and think before answer to you, sir.
          As always, you were right this time too.

          • Сергей

            Why be rude? I’m trying to help improve the library. Yes, sometimes I have pointed out on irrelevant bugs. That is why I ask suggestive questions. Because I’m not sure whether this is a bug or it should be.
            If it hurts you, then I think I’ll no longer write anything.

          • I don’t have in mind to be rude, but I don’t like when people ignore comment rules and later after they make post, they continuosly span “and?” “where are you?” and so on. I will respond to message when I will think about and have time.

            Anyway, thanks for pointing this out.

            You are welcome to show any bugs you see. You can use Github instead.

  • Victor

    Hi! I am trying to use my new STM32F7 Discovery board with your libraries but I really don’t know how to change the target. I have erased the define in c/c++ label and i made the following changes (image) but it still looks for some f4 libraries. What else do i need to do?

    • Just follow instructions in the main.c file on the top comments. Targets are predefined from me. Select STM32F7_DISCOVERY and you are done.

      • Victor

        Thank you so much! Now it works fine

  • Yannis

    Hi Tilen! I am trying to integrate only crc and dma libraries to my existing project with coocox libraries. Will they work out of the box? If the answer is already out there and i failed to identify it I apologize.
    Furthermore, my ultimate goal is to run the Cryptoaccelerator and the CRC with DMA. I am working on an STM32F417 board. How should I approach that in your opinion?

    • CRC yes, DMA too, both should work. CRC has some problems if data is not 32-bits aligned arrays. (must fix lib again)

      It could work DMA + CRC 🙂

      • Yannis

        It basically works in case you download as you mention the HAL libraries. However this is basically the whole project. I cannot migrate to HAL very easily at the moment. Why HAL?

  • Dave

    I cant find the file where “TM_SPI_PinsPack_1” is defined

    • With the fact that is related to SPI, do you have in mind to check SPI library?

      • Dave

        Thank you very much, there were some missing includes of HAL Libraries!

  • Dimas Setiaji

    ok.. now i confuse…. on
    standard peripheral drivers “Lib”
    HAL “Lib”….
    tell me which one gave me fastest response, minimum code output. to gave me bare bone FASTest for simple task like PWM,Timer,External Interrupt..
    if i ever want any OS for realtime etc,
    Many USB + Many LAN + Many UART + RAM + SDCARD + all in the same chip…..
    i would get RaspberryPi 3B rather than STM32