stmhal: ADC class only uses ADC1 hardware
This issue appears not to be very minor for the pyboard or any board using 64- or 100-pin versions of the STM32F405, so it could be low-priority or 'won't fix'. However, I encountered this running MicroPython on an Olimex STM32-E407 dev board, which uses a 144-pin STM32F407. On this board, much of the GPIO from ports A, B, and C are connected to / utilized by on-board peripherals (like an Ethernet PHY). Most of the ADC input pins that are broken out to headers are on Port F, but those ADC pins are only connected internally to ADC3, so not accessible via the current ADC class:
>>> pf6 = pyb.ADC('PF6')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: pin F6 does not have ADC capabilities
>>>
Since I am already customizing the firmware a bit to get it running on the Olimex board, I think that I can solve my immediate problem by hacking a few lines in adc.c to use ADC3 instead of ADC1. I don't have a more generic solution yet, so I just wanted to raise the issue before I forgot about it.
The biggest potential benefit that I could see in enabling the ADC2 and ADC3 hardware for lower pin-count chips would be to enable high-speed interleaved ADC reads on those pins connected to all three instances, but it would take some serious recoding to allow this, for an application that may see relatively little use.
stm32/adc: Add STM32H5 support.
Description
Add STM32H5 ADC support to pyb.ADC by correcting existing code.
Changes are:
- Run ADC on PCLK/16
- Use STM32 ADC library channel literals (__HAL_ADC_DECIMAL_NB_TO_CHANNEL)
- Use correct temperature conversion for H5 (30°C, 130°C calibration points)
- Add support for ADC2 input channels
- Optimize sample time for external inputs
Thanks to @yn386 for discussions.
Environment
- NUCLEO-H563ZI
- Using STM32H573I-DK board with minor modifications (input clock, on-chip flash)
- First ADC Input (PF12) connected externally to GPIO PF14
MicroPython v1.20.0-283-g7d66ae603-dirty on 2023-07-29; STM32H573I-DK with STM32H573IIK3Q
Tests
from pyb import Pin, ADC, ADCAll
io = Pin('F14', Pin.OUT_PP)
io.high()
adc=ADC(Pin('F12'))
adc.read()
# Result: 4095 -> Ok, 3.30 V
io.low()
adc.read()
# Result: 4 --> Ok, ~2 mV
adc.read()
# Result: 2 --> Ok, ~1 mV
# Pin F13 is only available on ADC2, for test connect ADC Input PF13 to GPIO PF14
adc2=ADC(Pin('F13'))
print(adc2)
<ADC2 on Pin(Pin.cpu.F13, mode=Pin.ANALOG) channel=2>
io.high()
adc2.read()
# Result: 4087 --> Ok
io.low()
adc2.read()
# Result: 2 --> Ok
all=ADCAll(12,0x70000)
all.read_core_temp()
# Result: 28.2205 -> Ok
all.read_core_vref()
# Result: 1.207985 -> Ok, nominal 1.21 V