Portenta H7: read chip temperature, board voltage

I think I saw a thread about "how to read the chip temperature?"

Here is a piece of code how to do:
it is NOT an Arduino Sketch code: it is STM32_HAL based (potentially possible to take to a Sketch, just modify some lines, e.g. my print_log(UART_OUT, ...); and the includes needed).

Code files attached below.
You need two additional lines in the clock configuration (extend or insert at the right place, no idea how to do on an Arduino Sketch):

PeriphClkInitStruct.PeriphClockSelection = RCC_PERIPHCLK_RTC | RCC_PERIPHCLK_USB | RCC_PERIPHCLK_SPI2 | RCC_PERIPHCLK_QSPI | RCC_PERIPHCLK_ADC;

and:

PeriphClkInitStruct.AdcClockSelection = RCC_ADCCLKSOURCE_PLL2;

All the other code, how to read temperature sensor in chip (and how to calibrate) in code files.

Remark:

  • on-chip temperature sensor is ADC3, channel 18, VBat ADC3, channel 17 etc.
  • it configures also ADC_A2 (PC2_C) as INP0 and ADC_A3 (PC3_C) as INP1 for voltage measurements (analog inputs).

ADC3_temp.h (388 Bytes)
ADC3_temp.c (4.3 KB)

Just strange
When I provide a voltage on INP0 (e.g. the 3V1 taken from board) - the INP1 shows me also high voltage (e.g. I provide the 3.1V to INP0 and INP1 shows me 2.xV).
The opposite (INP1 with 3.1V) is fine: INP0 remains low (floating).

If no voltage provided: it gets the noise, pretty huge, in range of 0.2V. Tie the input down to ground and it seems to be "calm" (0.0V).

So, this example code:

  • reads the internal chip temperature sensor (and calibrates the result - necessary to do)
  • reads the VBat (indication for board voltage, main power supply: with my PMIC config: 3.3V)
  • reads the VRef (needed for ADC, also indication about board voltage, 3.1V (not power voltage))
  • and reads INP0 and INP1 (as voltage measurement pins on Portenta H7)