STM32. Weight loss practice or diet for the controller

What kind of nonsense, some readers will probably think when they see the title of the publication. Did the author make a mistake on the resource? Or was it planned to place it in the hub "Geek Health"? And they will be partly right, but only partly ... Under the cut, we will talk about a method for reducing and optimizing the energy consumption of electronic devices.
Some time ago, the X-NUCLEO-LPM01A board fell into my hands , which for the time being was idle until I decided to study the energy consumption of one designed device with autonomous power.

This board was developed by STMicroelectronics and is positioned as "Power shield, Nucleo expansion board for power consumption measurement". I did not find an explanatory Russian-language description on the network for working with this devkit and decided to figure it out myself, and at the same time share information with the respected habro-community.
If you highlight the main one, the expansion board is a programmable power supply from 1.8 to 3.3 volts with the ability to measure the load current in real time (in dynamic mode up to
50 mA) with a sampling frequency of up to 100 kHz. The device can operate both independently and as part of a hardware-software complex. To graphically display the measured values, the STM32Cube MonitorPower utility is used . This software also allows you to set all the necessary parameters and carry out processing upon the occurrence of an event (trigger launch). X-NUCLEO-LPM01ASharpened to work with Nucleo-32 (64, 144), Arduino Nano (Uno) boards, for this purpose the appropriate shields are placed on it, but there is the possibility of working with arbitrary boards. The photo below shows the connection of the device under study to one of the connectors on the board.

In addition to application software, X-NUCLEO-LPM01A supports command mode, for which any terminal program should work . (Here I think a few got excited, HTerm wound up with a half-kick, but with my favorite ZOC had little poshamanit). Moreover, judging by the description, through the use of the VCP driver, the device can pour data into the COM port with a speed of up to 3686400 baud. Such a high speed is ensured in particular by transmitting a binary data stream (for sampling frequencies above 50 KHz), for ASCII characters the speed is slightly lower.
More specifically, the task that I will try to solve using X-NUCLEO-LPM01A is:
To obtain information on the dynamic power consumption of the nodes of interest in the designed product using the trigger trigger X-NUCLEO-LPM01A in the studied places of the program code.
In the process of continuous operation of the device to organize the accumulation of the necessary fragments for subsequent generalized analysis.
The screenshot below shows the current log of the SD card during data recording.

STM32Cube MonitorPower allows not only qualitatively, but also quantitatively evaluate the energy consumption of the device under study. The minimum, maximum, average values of the consumed current and the integrated energy value are shown in the lower part of the picture. Such data is available both for the entire measurement and for the selected fragment.
In the User manual, it is noted that trigger triggering of the measurement is carried out by an impulse on the leg of the D7 arduino shield. As it turned out, only a single start of the process with subsequent continuous recording works. An attempt to organize a start-stop mode for recording current (on both fronts of the pulse) was unsuccessful. That is, once starting recording in the right place on the rising front, it is not possible to slow down the process with a falling front of the pulse. And this is not quite what I expected to get from this device.
command | description |
htc | host control capture |
volt 3300-03 | supply voltage on the measured channel 3300 mV |
acqtime 0 | continuous measurement without time limit |
freq 10000 | sampling frequency 10 kHz |
start | start of measurement |
... | metering (data is poured into the terminal) |
stop | end of measurement |
The problem is that this is a manual control that is not synchronized with the events in the target controller. Of course, you can try to form the indicated sequence of commands on the side of the device under study, not the host, but then the data output stream from X-NUCLEO-LPM01A will also sprinkle into the controller and it is not clear what to do with them, and the redirection of the output "somewhere_to_other_place_" is also not available, ( although there are UART Tx, Rx pins on the board, they appear as "reserved" in the manual and are probably not involved in the current firmware version). I got out of this situation as follows:
command | description |
htc | host control capture |
volt 3300-03 | supply voltage on the measured channel 3300 mV |
acqtime 1 | measurement duration |
trigsrc d7 | synchronization of the beginning of measurement with a rising edge of the |
freq 10000 | sampling frequency 10 kHz |
start | start of measurement |
... | metering (data starts on the edge of the pulse and stops |
Now the data will be captured in batches, rather than in a continuous stream. This approach allows you to explore the consumption of the circuit in different places of the program and at different time intervals, which are quite far from each other. That is, if there is a need to collect information on the energy consumption of a device that works an hour (day, week), you do not need to write a continuous stream, and even with a maximum sampling rate, and then understand this mess, but just provide the above algorithm of work. And if the terminal program writes time markers to its log, as shown in the picture below, then it will be quite simple to parse the event history from such a file.

It’s like a video recorder with motion detection and time stamps.
Let me give you a couple of screenshots obtained using X-NUCLEO-LPM01A . The first is the power consumption of the controller while reading data from the built-in ADC. 32 values are read by timer interrupt with a frequency of 4 KHz and are stored in memory.

On the second - similar reading and saving of data, but only the controller "allows itself to sleep" after each measurement, until the next timer interrupt occurs, using one of the available energy-saving modes.

A significant delta in current consumption during measurement, ~ 6 mA, is immediately noticeable. These two examples do not claim to optimize the power consumption of low-power devices, but only demonstrate the capabilities of the X-NUCLEO-LPM01A , which is capable of measuring currents from 1 nA.
В заключение скажу, что, по моему скромному мнению, этот девкит представляется вполне годным инструментом, который может занять достойное место рядом с осциллографом и логическим анализатором сигналов на столе эмбеддера. А использование описанного подхода позволит не только "умерить аппетиты контроллера" при разработке критичных по питанию девайсов, но и лучше понять динамику процессов, в многообразных режимах энергосбережения, активно используемых разработчиками современных электронных устройств.
Несколько омрачает радужность картинки стоимость платы — более 7000 руб, но разве такая, ничтожная по нынешним временам, сумма сможет остановить настоящего гика от возможности обладания этим чудесным девкитом?
PS Despite the indication in UM that UART is reserved for future applications, I still stuck on it and found that it lives as a second interface (I / O is duplicated from VCP) at a speed of 921600 8N1 and can be used as a command, forming start and stop for the beginning - the end of the measurement. The disadvantage of this approach will be the mandatory availability of free UART on the side of the controller under study, not to mention the fact that 921600 bauds are often impossible at low clock frequencies.
PPS Already after writing the article, I noticed a difference in the last pictures of ~ 2 mA at the entrance to the measurement and at the end of it. I probably didn’t turn off ADC clocking, or I left the timer to work, but this is, as Leonid Kanevsky likes to say: “a completely different story” ...