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.
stmhal/adc.c: Fixing Issue #2243 - ADC not working for STM32L4.
Fixing the mentioned Issue. Main problem was:
HAL_ADC_GetState(adcHandle)may return other bits set (not onlyHAL_ADC_STATE_EOC_REG) when called - so I AND-ed it out as proposed by mattbrejza in Issue #2243.- ADC Pin has to be configured as
GPIO_MODE_ANALOG_ADC_CONTROLnot onlyGPIO_MODE_ANALOG. - Resolved ADC resolution L4 specific (Use L4 define
ADC_RESOLUTION_12B). - Changed setting of
Init.EOCSelectiontoADC_EOC_SINGLE_CONVfor L4. - Added call to
ADC_MultiModeTypeDefas this is done on a STM32Cube generated project too. - Introduction of proper ADC Pin definitions in stm32l476_af.csv
- Clean up: Configuration of ADC is done only in ONE function not the same is done in two functions.
Test is done on PA5 pin of STM32L4Discovery-Kit which is connected to the DOWN button.
Thanks to mattbrejza for discovering the bug.