Skip to main content
Visitor II
April 22, 2024
Question

STM32 VScode undefined reference HAL_FLASH_Unlock

  • April 22, 2024
  • 3 replies
  • 3387 views

Hello everyone,

I am currently taking my first steps with the STM32VScode extension. Previously I worked exclusively with the STM32cubeIDE.

I created a Cmake project via STM32cubeMX, which I then imported via the STM32 VScode extension. Everything worked fine. The build process afterwards was successful and I was able to run the program on my Nucleo-C31C6 board.

Now I want to access the flash memory. But as soon as I insert the function HAL_FLASH_Unlock() into my code, the following error occurs during the build process: main.c:109: undefined reference to `HAL_FLASH_Unlock'.

How do I have to include the HAL drivers correctly so that I can use the function?

 

maxl95

    This topic has been closed for replies.

    3 replies

    Super User
    April 22, 2024

    So do you have a definition for HAL_FLASH_Unlock anywhere in your project?

     


    @maxl95 wrote:

    How do I have to include the HAL drivers correctly so that I can use the function?


    As with any C code:

    1. You need to #include the appropriate header(s) in the file(s) where you call the function;
    2. You need to have the source file(s) which define the function - ie, contain its executable body - in your Project.
    maxl95Author
    Visitor II
    April 22, 2024

    Hi Andrew, thanks for your quick replay,

    1. I've #include "stm32c0xx_hal_flash.h" in my main.c
    2. The source file is in the defaul directory --> ./Drivers/STM32C0xx_HAL_Driver/Src

    I have also successfully used other functions (e.g. LL_USART) where the problem did not occur.

    Super User
    April 22, 2024

    @maxl95 wrote:
    1.  
    2. The source file is in the defaul directory --> ./Drivers/STM32C0xx_HAL_Driver/Src

    And is that file actually getting built?

    Technical Moderator
    April 25, 2024

    Hello @maxl95 

    Actually Flash drivers are by default integrated to your project

    If you are adding HAL_FLASH_Unlock()  to a custom file you can include main.h or stm32c0xx_hal_conf.h to get flash functions available in your custom file

    Visitor II
    March 13, 2025

    Hello @maxl95,

    I've just encountered this problem in my project but with STM32F4, and I'm also building with CMake. 

    I've added the stm32f4xx_hal_flash.c path into my main CMakeLists.txt to PROJECT_SOURCES list along with files from Core/Src. And that works for me.