ADC LowPowerFrequencyMode = Enable

Post here first, or if you can't find a relevant section!
Post Reply
timdelta
Posts: 5
Joined: Thu Jan 16, 2025 4:04 pm

ADC LowPowerFrequencyMode = Enable

Post by timdelta »

Hi all

Forgive the relative noob question, and go easy!
I have an existing custom board that was programmed previously with CubeIDE (of which I have almost no experience) by a retired engineer, that I have successfully re-written a simplified functional version of in Arduino IDE (due to a needed modification), but I'm struggling to get the STM32LO518KT's current draw down enough - its a constant sampling sensor application, so going to sleep isn't really an option as I see it.

I've managed to cobble together a revised PeripheralPins.c (needed pullups on the i2c pins) and generic_clock.c (to alter the closck speeds) and put it in the variant folder (based on what I can see in the Cube versions main.c), and got the overall circuit current down from 13mA to 5mA by altering the clock speeds, but I need it to be under 4mA (like the Cube version achieves). It uses one i2c, two ADC's and one PWM output. I have reprogrammed an original board (that previously used the cube code and achieved <4mA) so it's nothing hardware based limiting me.

I've noticed the Cube version sets things in the hadc.Init such as LowPowerFrequencyMode = Enable etc -
- can anyone help by advising how can i access these settings in my setup() part of my sketch? (assuming that's the correct place to do it??)
- Is that even possible when using analogRead(xxx)??? - a small example would be incredibly helpful

I'm happy to share the whole or sections of either/both code if that helps...

Thanks!
by GonzoG » Thu Jan 23, 2025 11:27 am
You can use HAL libraries (like in CubeIDE) in stm32duino, so you can use all the adc code from cubeIDE in arduinoIDE just with some modifications.
You don't need to edit generic_clock.c or PeripheralPins.c, you can put your clock config and changed pin maps in your sketch.

As to current draw, it might not be possible to achieve same level. stm32duino is designed to be compatible with arduino and this means that there's a lot of bloatware. Also means that all standard hardware is enabled by default, so it draws some power, but most can be disabled:
https://github.com/stm32duino/Arduino_C ... figuration
Go to full post
GonzoG
Posts: 481
Joined: Wed Jan 15, 2020 11:30 am
Answers: 36
Location: Prudnik, Poland

Re: ADC LowPowerFrequencyMode = Enable

Post by GonzoG »

You can use HAL libraries (like in CubeIDE) in stm32duino, so you can use all the adc code from cubeIDE in arduinoIDE just with some modifications.
You don't need to edit generic_clock.c or PeripheralPins.c, you can put your clock config and changed pin maps in your sketch.

As to current draw, it might not be possible to achieve same level. stm32duino is designed to be compatible with arduino and this means that there's a lot of bloatware. Also means that all standard hardware is enabled by default, so it draws some power, but most can be disabled:
https://github.com/stm32duino/Arduino_C ... figuration
ag123
Posts: 1881
Joined: Thu Dec 19, 2019 5:30 am
Answers: 30

Re: ADC LowPowerFrequencyMode = Enable

Post by ag123 »

I'm not a best person to comment, but that the 'standard Hardware' enabled e.g. for analogRead() is most likely for the ADC rather than LPADC.
the standard GPIO banks like GPIOA, GPIOB etc are also likely enabled to support digitalWrite(), digitalRead() etc. for that matter, I'm not sure about where it the clocks is initialised for this.

in your generic_clock.c, void SystemClock_Config(void), normally after configuring the PLL to run on the external crystal, there is normally one part it initializes the peripheral clocks. you could check that list, to see that no 'unwanted' peripheral clocks are enabled by default, e.g.
https://github.com/stm32duino/Arduino_C ... lock.c#L57

e.g. for stm32f103c8 'bluepill', it'd seem ADC, USB is clocked.
https://github.com/stm32duino/Arduino_C ... x.cpp#L125

beyond that, I'm not too sure where the individual peripheral blocks are initialized. They could be initialized in subsequent code blocks or possibly in calls like pinMode() etc. I'd leave that to anyone more knowledgable to comment. Note that a lot of the individual peripheral modules could call things like __HAL_RCC_XXYY_CLK_ENABLE(); which could turn on clocks to those peripherals, this could happen say in libraries like SPI which would make such calls to clock the SPI peripheral, etc.
timdelta
Posts: 5
Joined: Thu Jan 16, 2025 4:04 pm

Re: ADC LowPowerFrequencyMode = Enable

Post by timdelta »

Thanks to you both for responding - That has set me on the right track and I've figured that out, now pulling about 3.2mA, Job done!! :D

I would still like to understand how to set the PeripheralPins and Clock settings in the sketch too, haven't figured that out yet, but after you set me on the right track on the other bits I'm pretty sure some playing around should get it ;)
GonzoG
Posts: 481
Joined: Wed Jan 15, 2020 11:30 am
Answers: 36
Location: Prudnik, Poland

Re: ADC LowPowerFrequencyMode = Enable

Post by GonzoG »

To change pins you just put an array with pin setup in your sketch. Copy one from PheripheralPins.h, paste it in your sketch, remove "WEAK", edit pin setup.

Same thing with clock setup. Just copy and paste SystemClock_Config(void) from generic_clock.c, remove "WEAK", edit.
timdelta
Posts: 5
Joined: Thu Jan 16, 2025 4:04 pm

Re: ADC LowPowerFrequencyMode = Enable

Post by timdelta »

Thanks so much :)
Post Reply

Return to “General discussion”