Skip to content

[BUG] STM32 low-side current sense rejects ADC alternate pins (ALT1) #559

Description

@baronep

I am using a custom STM32G474-based board with SimpleFOC low-side current sensing. I need to use ADC2 alternate pins for the current-sense inputs:

#define MOTOR1_ADCA_PIN PF_1
#define MOTOR1_ADCB_PIN PC_0_ALT1
#define MOTOR1_ADCC_PIN PC_1_ALT1

The board variant defines these mappings in PeripheralPins.c:

{PC_0,       ADC1, ...}, // ADC1_IN6
{PC_0_ALT1,  ADC2, ...}, // ADC2_IN6
{PC_1,       ADC1, ...}, // ADC1_IN7
{PC_1_ALT1,  ADC2, ...}, // ADC2_IN7
{PF_1,       ADC2, ...}, // ADC2_IN10

This should be a valid configuration for a low-side current sense on ADC2.

However, the SimpleFOC STM32 backend rejects the ALT pins before ADC configuration. The failure I get is:

STM32-CS: ERR: Pin C does not belong to any ADC!
Current sense init failed

and in the debugging path the library appears to be converting the pin through analogInputToPinName(...), which strips the ALT bit before checking PinMap_ADC.

Expected behavior:
PC_0_ALT1 and PC_1_ALT1 should be treated as valid ADC2 pins and current sensing should initialize successfully.

Actual behavior:
The STM32 low-side current sense code does not correctly handle alternate ADC pin names. It effectively resolves:

PC_0_ALT1 -> PC_0
PC_1_ALT1 -> PC_1
before checking PinMap_ADC, which leads to ADC1 lookup instead of ADC2 and fails for low-side current sensing.

Environment:

Board: custom STM32G474 variant
MCU: STM32G474
Framework: PlatformIO + Arduino STM32 core
Library: SimpleFOC
Current-sense class: LowsideCurrentSense

Additional notes:
I confirmed that the alternate pin mappings are present in the board variant, so the issue is not in the board definition itself. The bug is in the STM32 SimpleFOC current-sense path, where the alternate pin bit is discarded before checking the ADC map.

Workaround used:
I patched the STM32 backend to preserve the raw PinName instead of converting through analogInputToPinName(...) before the PinMap_ADC lookup. After that change, the library selected ADC2 and initialization succeeded. This however breaks the "Arduino" HAL and forces the user to use the raw STM32 pins (e.g. PF_1 instead of PF1)

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions