Mixing Mbed with STM HAL for granular ADC control

I’m attempting to set up a simple project where I include the STM HAL within mbed to allow granular control of an ADC (ADC1) within an STM32 chip, while leaving everything else in the OS alone. I am trying to be minimally invasive on the Mbed OS while still getting the control of the ADC I need.

I have found a post from some time ago explaining some steps to take in order to do this. Using STM32 HAL with mbedOS | Mbed

I’ve done the following:

  1. Create a cubemx project (very simple, basically 1 adc channel and a uart)
  2. Auto generate the code
  3. Verify it compiles in CubeIDE, flashes to board, and am able to debug all the way the main()
  4. Take the clock configs into my Mbed project from the CubeIDE project
  5. Take the MX_ADC1_Init() function from the CubeIDE into MBed

My issue is that the MX_ADC1_Init() function fails in Mbed (can’t complete), but runs fine in the bare metal when run from cubeIDE. I’m not really sure why. When I debug and step through the Init() function, I see something about an internal regulator being off, causing an error. I can only assume that Mbed is doing some device configuration behind the scenes that is making this Init() routine fail.

Does anyone have any experience in bringing low-level adc control into Mbed OS by using the STM HAL? Maybe there is something else I have to do to get this to work?

Thanks.

Hello John,

This is what forked for me:

  • When you create a new program in STM32CubeIDE you plan to port to Mbed OS make sure you setup the same system clock configuration as used by Mbed OS for the given target. You can find it in system_clock.c file of the selected STM target. For example, for the NUCLEO_H743ZI2 it is located in the mbed-os/targets/TARGET_STM/TARGET_STM32H7/TARGET_STM32H743xI/TARGET_NUCLEO_H743ZI2 folder.

  • After the program is ported to Mbed let Mbed to configure the HAL and System Clock:
    In the main.cpp comment out:

...
    //    HAL_Init();
...
    //    SystemClock_Config();
...

You can find an example here. It’s quite old but might help.
Here is a more recent one but it doesn’t use ADC.

Best regards, Zoltan

I figured it out. It wasn’t enough to configure the clocks correctly only. I actually had to bring in the STM32g4xx_hal_msp.c file from the cubeIDE project into the mbed project.

Once this file was brought over, I was able to debug and get through ADC Init() without a HAL error.

I am currently reading the ADC and seeing the values I expect.

1 Like