All STM32 HAL libraries

In this section, I will write all libraries based on HAL drivers from ST.

If you see this first time, make sure you read this post first about how structure of my libs looks like!


All my softwares are published under MIT license. This means that you are allow to modify, share and use my source and other stuff in personal or commercial use. If you modify source code, it has to stay under GNU GPL v3 license too. I reserve a right to shut down this website at any time.

If you want, you can make a donation to my Paypal account and support my work in future. Paypal link is below.



All examples for Keil uVision can be downloaded from my Github repository.

Libraries API

I’ve added doxygen documentation on my site for HAL based libraries. It is available on this link where you can find descriptions for used functions in my libraires.

All libraries

Libraries listed below are currently tested on F7 and F4 series and works OK. If you find any bug, please report it to me.

I’m not responsible for any damage you will do using my libraries. You at your own risk!

RCC For setting up clock control for your board, supporting internal or external clock, support for enabling I and D cache for F7 series.
GPIO For controlling GPIO pins.
DELAY For delay functions using Systick and DWT counter
EXTI For external interrupts in GPIO pins
DISCO For leds and buttons on STM32Fxxx boards
ONEWIRE For onewire based devices like DS18B20
DS18B20 For controlling/reading DS18B20 temperature sensor
USART For communication with UART protocol
SPI For SPI communication
GENERAL General library for different stuff
CRC CRC calculation unit
SDRAM Fir SDRAN communication on F429-Disco, F439-Eval and F7-Disco boards
LCD Single library for F7-Disco, F439-Eval and F429-Disco LCD boards using LTDC and DMA2D
BUTTON For controlling multiple buttons
FFT For FFT calculations
HD44780 For LCD display
I2C For I2C communication
IWDG For independent watchdog timer
RNG For true random number generator
CPU LOAD For measuring CPU load using sleep mode and DWT counter
FATFS For interfacing SDCARD with SDIO/SDMMC or SPI communication interface
USB USB Host and Device library
BOR For setting brown-out detection levels
TOUCH For controlling touch on F7-Disco and F439-Eval boards
RTC For internal Real-Time-Clock timer
NRF24L01+ For 2.4GHz NRF24L01+ transceiver
PVD Power voltage detector for STM32Fxxx
IDENTIFICATION Detect used STM32Fxxx device where program is running
DAC Output analog voltage
ADC Read analog voltage
MPU6050 Operate with MPU6050 accelerometer and gyroscope
DMA Operate with MPU6050 accelerometer and gyroscope
USART DMA Operate with MPU6050 accelerometer and gyroscope
SPI DMA Operate with MPU6050 accelerometer and gyroscope
DSP FILTERS For DSP signal processing
GPS For reading GPS module
AHRS IMU For calculating roll, pitch and yaw values from motion sensor
STRING For creating strings
ROTARY ENCODER For controlling up to 16 rotary encoders using external interrupts
AM2301 For temperature and humidity sensor (DHT-21)
SSD1306 For controlling SSD1306 LCD driver via I2C

Download libraries

Download is available in a single pack for all libraries. There will be no single downloads.

TM STM32 Libraries

STM32 libraries based on STM32Fxxx HAL drivers.


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!
  • John Doe

    Hi, WHat is the difference between this download link for your hal libraries and what is on github, which is a much bigger archive? Am I misunderstanding something?

    • Here are only my lib files. On github are also examples.

  • Артем Тихонович

    Hi! Thank you very much! Excelent work! Do you have some plans to make libs for STM32F1 series? Or may be it works in current variant? I will test it with 32f1.

    • No, I don’t have that in mind. I don’t see an option when F1 should be used.

      • LB IN HB

        The stm32f107 is interesting as a less expensive option with Ethernet. On mouser here in the US, it is about $4 cheaper than the stm32f407.

        • Only for that, I’m not ready to do libs availability for old series F1.

  • Murtaz Naqvi

    Hi! Thanks you very much! These are very useful! Dude i have a problem i am making an E-reader using stm32f4 for which i am using a 256mb sdram. I am confused about DMA or FMC can you help me out which one to use regarding to my project.

    • Hello,

      for start, you definitelly need FMC for SDRAM controlling. After that, you can use DMA for memory to memory conversion (reading/writing process).
      Remember, only DMA2 can do memory to memory conversion.

      • Murtaz Naqvi

        Thanks you very much! I really appreciate it.

  • fmc159

    Hello, I wonder if you could advise someone very new to the STM32 family! I have happily compiled and flashed some of your examples, but others (eg. no. 9 the GPIO example) won’t build in KEIL as they go over the free version size limit. I don’t suppose there is anything I can do to reduce project size? I am just surprised that such simple examples would require so much space, but I am guessing it’s the cost of choosing the larger HAL library.

    • Hi,

      Try to remove all libs fron project which are not needed and try again.
      Another option is to use free gcc compiler.

      • fmc159

        Yes, I think I’ll look to move to Coocox and GCC. Huge thanks for the quick reply!

  • Rafael Dias

    I’m studying the Standard Peripheral Library and the HAL from STMicroelectronics. These are good libraries but, sometimes, a little confusing about how to use the functionalities.
    I found your project and I liked it a lot.

    My question is about the motivation of write your own Hardware Abstraction Layer. I think that the main motivation maybe is because there are some bugs in the StdPeripheral Library (like a bug that I founded in the USART library), but I would to know what do you think about the ST libraries.

    Not to mention that implementing your own library, you can learn a lot about the SMT32F architecture.

    • Hi, creating own hal drivers is great idea, but there is a lot of mcus and you wont use always the same one for which you have drivers.

      Let me know which bug you have found in usart lib for std periph.

      • Rafael Dias

        I found two bugs: one was related to the Baud Rate configuration. In the 1.0.0 version of the STD Peripheral Library from STMicroelectronics I was putting a invalid value on the baud rate generator register. I identified this problem when I did a test project to write something on the serial port. I created the project with the keil IDE, put the STD Peripheral library and wrote the source code. After removing of all the errors and warnings, by measuring the output baudrate I had a value totally different from the configured value. After a day reading the source code from STD Library and looking at the datasheet I found the error.
        The another problem that I had was related to the creation of two instances of USART interface. In the same project of the previous item, when I created the second instance (each instance of a different USART link, i.e, COM1 and COM2), the hardware issued a Fault exception. In the same time I had to change the microcontroller family from STM32 to a TivaWare from Texas Instruments. This was a management decision for questions of cost and team expertise.

        So, because the changing os microcontroller family I could not fix this last problem.

        Nowadays I’ll work again with the STM32F4 family and the question that started this is because I will not be happy if I start to use the std peripheral library and have problems with code errors

        • Ouuh, don’t worry. You used that then about 2 or 3 years ago?
          Now is version 1.5.1 out for these drivers and there is no bug.

          HAL drivers are buggy. I don’t like them, but if I want to be “in time” with ST and their software, I have to use them. I hope they will make stable versions soon. Now is about 1.5 year of HAL drivers for F4 series and there is still no fully stable version out.

          But for F7 lines and L4 new lines, there is no std drivers so I have to use HAL.

  • apastdei

    Hello my friend! This is an amazing effort and resource. Could you please advise on setting up a coocox project using your HAL libraries? I think I have problem with my compiler defines. All my thanks!

    • Hello,

      please send me your entire project you have for now to my email which can be found on my site, and I will take a look at your project.

      • apastdei

        For now I am just trying to run one of your HAL KEIL examples in Coocox, that’s all I need advice on. thanks!

        • Ok, but:
          1. I have to download coocox again,
          2. Which board you are using for testing so I can make project template for you.
          3. I hope I will do it today.

          • apastdei

            I am using STM32F429 Discovery. I have just come back to STM programming after some break, I used to work with standard periph libraries, but want to start with HAL/your libraries.

            You are amazing, I will be always grateful!

          • apastdei

            Hello again my friend, I have made simple button interrupt in Coocox to turn on onboard LEDs (just using HAL drivers, NOT your libraries), but it is not working. I can blink LEDs using HAL Write or Toggle functions, but cannot get button to toggle LED using interrupt. If I send project can you take a look? I know it must be something simple..

          • Do it.

          • apastdei


  • Mohammad Kamel

    i searched a lot on internet to find hal library how can i get them
    thanks for your help

  • Hello Majerle, first of all, this hal library that you prepared is another level compared to your previous amazing stdperiph library which I spent a lot of time playing with it. Best thing that I see in hal library is that we can combine different stm32 processors in one keil project without dealing with changing our codes every time for every processor. I can build same project for stm32f030 and stm32f405 (I use most of the time in my projects) at the same time. This change made me save a lot of time of migrating from one processor to another. Your libraries helped me shortening my code and made my code more readable. Another leveling up is that you put all of your work to github.

    Thank you very much to your great effort for spending time on this libraries.

    However, where is “tm_stm32_adc.h” and “tm_stm32_adc.h”. 😀 Do you plan migrating adc library from stdperiph to hal ? If you don’t do it soon, please inform me then i will try 😀

    • You are right. Libs are only done to speed up development process by not initializing everything from start always.

      ADC lib will be done, but right now I just don’t find free time to do it. 🙂

  • mukhe

    Hey MJ,
    Just letting you know that your I2C and USART for HAL libraries are very good. Thrown everything at it but still working without issues.

    I do wonder if you will ever have a DCMI library though. Just being greedy though.
    Anyways, thanks for all the libraries!

  • Георгий

    Please, give me the default project with HAL libraries. Thank you.

  • Jeremy Vance

    Could I suggest another library or perhaps you have already dabbled in this? Have you tried to emulate EEPROM writing to the user configurable flash on the chip? It would be great to save small bits of data without a backup battery or external EEPROM or SD card.

    • You must reserve one sector for eeprom data (I suggest second sector). First sector for bootloader program and third an more for yoir program.

      To write data, read sector, change value, erase sector and write data.

      • Jeremy Vance

        Do you have any code that shows this as an example? I’ve been searching the internet for HAL based examples but I can’t find anything complete or working. A lot of unanswered questions on the ST forums.

        • Not for HAL. Search for examples for flash how to program it and erase. Thats all you need.

  • JL

    Hello. Thanks for your libraries, I have been using the STD versions quite a bit.
    I tried using the HAL NRF transmit (no IRQ) example and it doesn’t compile for me, there are errors.

    .TargetsSTM32F4_DISCOVERYproject.axf: Error: L6218E: Undefined symbol TM_NRF24L01_Read_Interrupts (referred from main.o).

    ..0-STM32_LIBRARIEStm_stm32_disco.c(89): warning: #61-D: integer operation result is out of range

    I fixed those and the code compiles and runs but gets stuck on SPI_WAIT when run the code on my discovery with the NRF on the default SPI pins.

    The exact same setup works fine with the STD version of the example, no problems. Am I missing something?

    • This function exists in library.
      So I have no idea what is going wrong here.

      I checked again and also on github under hal drivers.

      • JL

        Here is my build log after downloading the example code from github and compiling without changes:

        • I see, I forgot to set to void or return something from it. Will fix that.

      • JL

        It is complaining about this function in tm_stm32_nrf24l01.c

      • JL

        I tried fixing it by replacing all occurences of “NRF24L01_CLEAR_INTTERRUPTS” with


        This allowed it to compile but I am not sure if it is correct

      • JL

        Please disregard my original comment above, you are right, that function exists

      • divum

        Hi Tilen,

        Partial display update library is available in HAL Libraries?

  • Guiem

    Hi Majerle,

    First of all fantastic work with your libraries, they are very useful!

    I’ve seen that you haven’t done the HAL PWM library. Are you planning to do it? I’m interested in this library and if you’re not going to do it soon I’m going to do it by my self.

    Thank You.

    • Making pwm lib is not necessary with hal since it is very easy to do it.

  • macgeorge

    Hello and thank you for all the libraries!
    Do you intend to port your ethernet library for HAL (STM32F7 Disco)? Thanks!

  • Artur Rybus

    I have pretty complicated problem with your library. I am trying to make my hd 44780 working but nothing works. I defined what i should and it seems to compiling without any problems. I started to checking your libraries with files generated by stm32CubeMX. After this i noticed first problem. Your delay library works perfect only in debugger mode. That means it doesn’t work after uploading program to microcontroler. Do you know what can be reason of my problem? I work on STM32F411RETx
    Greetings from Poland.

    • Library works ok always. Check your pll settings.

      • Artur Rybus

        I know what i did wrong. I have forgotten to init libraries in main . Thanks for great libraries

  • yosa

    I am using STM32F407 Discovery board with AC6 systemworkbench. I tried to copy only the GPIO and DISCO libraries and integrating with my project.
    I am not able to find the stm32f4xx.h stm32f4xx_hal.h files which are defined in stm32f4xxx.h. Please help.

    • You must read about my HAL drivers. File stm32fxxx_hal.h is also included in package of libraries you downloaded from this site. stm32f4xx.h file is file from Cube package you get with HAL drivers.

      • yosa

        Thank you for your prompt reply!
        where do we get the cube package?

        • Google for more info.
          It can be downloaded from

          • yosa

            Able to download only PDF from check with other sites. Thanks for your help!

          • Stm32cubef4

  • Elkin

    Hi Tilen, I’m stating with an STM32F401 Discovery board I just bought. I want to learn to program it using HALs. Somewhere in your site you mention that STM’s HAL libraries are not good. What is wrong with them? Why you made new libraries since STM already released theirs? It’s confusing for me.

    • I have wrappers built on hal because f7 series does not have other than HAL.

      HAL has some problems and still does not work properly.

  • david

    how can I include this library in current project in Keil uVision created using CubeMX?

    I have created new map in project and import all your files into this map bit I got many of errors after I tried to compile project: “browse information of one or more files is not available”

    • I made those libs to not to use cubemx 😉 but anyway you can use it. Add files to project. From that error I cant help you much.

      • david

        What’s about define.h file, cant find it because uvision said that can’t find this file, although it including in onewire.h file

        • Actually reading this post gives you this:
          “If you see this first time, make sure you read this post first about how structure of my libs looks like!”

  • Mare

    Can I use part of your code in my project, which will be under “unlicense” ( ) ? I will keep the reference to your site.

  • otijus

    Hi Tilen,
    First, I would like to congratulate you for the excellent jobs of making the whole confusing aspects of STM32Fxxx work greatly. You are genius, really.
    Secondly, I am a true beginner in the world of microcontroller / STM32Fxxx and have been in the learning process to work with it. This is the information about my equipment : STM32F429I-DISC1 & Keil uVision 5 Lite Edition; Windows 7 64-bits.
    And here is my question regarding I2C program :
    1. I followed your instructions by downloading your libraries and worked with Keil IDE.
    2. I built and loaded your STMFxxx_EXTI software with my new installation : everything worked okay.
    3. I then built and loaded your STMFxxx_I2C with MPU-6050 of Sparkfun. So, my purpose is to learn how the I2C protocole work, since this is a new subject for me. As a result, no errors shown once I have loaded the program. However, the RED and GREEN leds are toggling / blinking once the program runs. And as I understand from your comment, this means that there is something wrong with my actions. Everything should be the same as your equipment and setups; except that there are 2 external pull-up resistors on the MPU-6050 board.
    So, could I ask you to help debugging this problem ?
    I thank you in advance and enjoy your evening.

    • From my perspective right now it is hard to detemine what the actual problem is.
      Can you give me a link from example you tested and didn’t work?

      But probably you have some problems with:
      1. Wiring
      2. Power supply for device or device actually doesn’t work.

      • otijus

        Thank you for your quick reply.
        This is the link :
        As for the wiring :
        1. VCC 3V : pins 3V of STM32F429
        2. SCL : pin PA8
        3 .SDA : pin PC9
        4. GND 0V : pin GND of STM32F429
        Before writing you the answer, I re-did the complete build and debug process again, and the result is the same. Meaning : both GREEN and RED leds are blinking. As for further info, I do right-clic on the “&read” variable to activate the watch option. It shows “0” during the run process.
        Please feel free to say if you would need further info.
        Thank you,

        • How is the A0 pin on MPU device set? With this pin, you can set I2C address.
          If after setting this pin high or low (both tests), you can run I2C scan loop to check if any device is recognized on I2C bus.

          • otijus

            Before using the MPU-6050 of Sparkfun, I checked using the multimeter that the A0 of the address byte is soldered to 0V or GND. So, A0 is always “0”.
            How should I do to check that the MPU device is recognized on I2C bus ?

          • On link below is and example for Standard Peripheral Library for my I2C lib.
            Using HAL lib, it is the same in main, so check the for loop to do the scanning.
            Also, check what TM_I2C_isDeviceConnected returns to properly detect if it is detected.


          • otijus

            Great, I will do this.
            It will probably take me some times to do so. So, I will report the result as soon as I can.
            In the meantime, here is the info about the MPU device :

          • otijus

            So I have downloaded your STD libraries, picked up the STM32F429_I2C_SEARCH_DEVICES program and build it with Keil uVision 5. I have got this error message : .TargetsSTM32F429_Discoveryproject.axf: Error: L6218E: Undefined symbol TM_DMA2DGRAPHIC_CopyBuffer (referred from tm_stm32f4_ili9341_ltdc.o).
            Any tips on how to fix it ?
            Also, do you think that the program size is less than 32K (I am using Keil Lite free version) ?

          • otijus

            Further info :
            I added the files “tm_stm32f4_dma2d_graphic.c” and “…h” to TM folder. Also the files “stm32f4xx_dma2d.c” and “…h” to STD_Periph_Drivers folder. Then I built and debugged the program : it works properly.
            Here is the info : the display shows “Device: 82” and the function TM_I2C_IsDeviceConnected() gives “0x08002472”.
            Do you have any tips / procedures on how to proceed further to fix my problem ? And thank you in advance for your useful help.

          • 0x82 is address of touch controller connected to PA8 PC9 pins as you mentioned you have setup in F429-Discovery board. So this means your device is not responsible for some reason.

          • otijus

            If the device is correctly connected and recognized by the bus, the remaining question is that : did I do some mistakes in manipulating your program example for I2C ? Or do I miss some steps in doing so ?

          • If scanner has found one device, then this means that it works (scanner code works). So it is problem with your hardware (MPU..)

          • otijus

            Thank you for your reply.
            This hardware problem really does not help in my learning process. The MPU-6050 is brand new and I really took care when I soldered the connector. Which MPU board have you been using in your example ?
            I think that I will order the MPU boards and redo the whole process again. And I will let you know how it goes.
            Cheers and enjoy your weekend, 🙂

  • otijus

    Hey Tilen,
    Hopefully that you have been doing okay.
    Feedback : so I have received the MPU-6050 from Olimex and have tested the sensor using your HAL I2C libraries. Great results : your job has been fantastic, and I thank you for this. This phase has enabled me to understand better the protocole.
    Present issue : I will need to implement the I2C protocle using a Honeywell HIH8120 sensor. Link :
    And I need your expertise to answer the following questions :
    1. Can I modify and tailor the function HAL_I2C_Master_Transmit() to correspond to the Measurement Request of the sensor (page 1, figure 2 of the datasheet) ?
    2. Can I use your function TM_I2C_ReadMultiNoRegister() to fetch and to read the datas from the sensor (page 2, figure 4 of the datasheet) ?
    Any advice would be greatly appreciated. 🙂
    Thank you in advance,

  • Matt

    Hi Tilen,

    I’m hoping you can help me with a question regarding the STM32F4 HAL driver callbacks.

    I am developing for an STM32F4. I’m using STM32CubeMX and System Workbench for STM32. I’m using FreeRTOS. I would like to know how I should, and should not, be using the STM32F4 HAL Driver callbacks.

    As an example, I’m using HAL_CAN_RxCpltCallback. Within this call back, I decode and validate the CAN frame, and then generate a number of different events (i.e. using xQueueSendFromISR). Or in some cases, I populate a CAN frame in response, and send it within the callback. Currently I have 300 lines of code in this specific callback.

    Ideally, I would make the callback easier to read, by calling out to additional functions; i.e. if the CAN frame/message is a request for date, then call handleRequestForData(), and so on.

    However, I can see that HAL_CAN_RxCpltCallback is called from HAL_CAN_IRQHandler. So I take it that HAL_CAN_RxCpltCallback is also an IRQ. And its my understanding that I should keep all IRQ’s short, and not to call out to other functions (due to stack size limitations).

    So my question is, should I minimize the amount of code in my callbacks. And in this case, simply push the CAN frame into another queue for another task to handle it?


    • Absolutelly try to maoe your isr as short as possible. If you just copy 20bytes, this is probably not a problem, but if you filter data for example, then do not use irq for that.

  • Алексей Kiss

    Dear Sir! Do you can to show us, what you will be do in future. So also I want ask you, do you can to make tutorial, how read datasheet and write one program from (I2C or SPI).
    Thank you, you make great work!

  • Shail

    Hi all,
    we are facing problem in RNDIS USB Class implementation for STM32 Nucleo 144pin. We have reference code but still not able to use USB OTG Library for RNIDS Class. Kindly explain function flow and necessary files required for that.
    Thanks and Regards,
    Shail Shah.

  • frank

    Hi, I am trying to compile your hal libs. Got the error below. Scan of all files does not show any definition of I2C_CR2_TXDMAEN. Any suggestions? Thanks.

    ‘I2C_CR2_TXDMAEN’ undeclared (first use in this function) tm_stm32_i2c_dma.c

  • frank

    Please see . Trying to compile HAL libs with including Fatfs, and cannot figure out duplicate symbol error. Any suggestions?

  • John Doe

    Hello. What version of HAL drivers does your library support?

    • In general with latest version should work.

      • John Doe

        Nope. the HAL files you have on github seem to be old – 2015. In stm32f4xx_hal_sd.h -HAL_SD_TransferStateTypedef exists. In newer HAL version, that is no longer there. Looks like it exists in version 1.12 and earlier.

        • There were changes in BSP for SD card. I’ve updated the HAL and SDcard BSP package but I didn’t tested now for all projects. SD card read/write worked after modification.

          • John Doe

            Cool ! thanks

  • dava

    check this please.I receive an error code about :

    #include “defines.h”
    #include “tm_stm32f4_ili9341.h”
    #include “tm_stm32f4_stmpe811.h”

    • Võ Kiệt

      download library defines.h in this web