Skip to content

rg_input: use the unit parameter instead of RG_BATTERY_ADC_UNIT in the legacy ADC path#342

Open
koalaauto wants to merge 1 commit into
ducalex:devfrom
koalaauto:fix/rg_input-legacy-adc-unit
Open

rg_input: use the unit parameter instead of RG_BATTERY_ADC_UNIT in the legacy ADC path#342
koalaauto wants to merge 1 commit into
ducalex:devfrom
koalaauto:fix/rg_input-legacy-adc-unit

Conversation

@koalaauto

Copy link
Copy Markdown

Summary

_adc_setup_channel() takes an adc_unit_t unit parameter, but its legacy branch tests the battery macro RG_BATTERY_ADC_UNIT instead of that parameter. As a result any target that does not define RG_BATTERY_ADC_UNIT fails to compile.

Details

components/retro-go/rg_input.c:

static inline bool _adc_setup_channel(adc_unit_t unit, adc_channel_t channel, adc_atten_t atten, bool calibrate)
{
    RG_ASSERT(unit == ADC_UNIT_1 || unit == ADC_UNIT_2, "Invalid ADC unit");
    esp_err_t err = ESP_FAIL;
#ifdef USE_ADC_DRIVER_NG
    ...                                     // uses `unit` correctly
#else
    if (RG_BATTERY_ADC_UNIT == ADC_UNIT_1)  // <-- should be `unit`
    {
        adc1_config_width(ADC_WIDTH_MAX - 1);
        err = adc1_config_channel_atten(channel, atten);
    }
    else if (RG_BATTERY_ADC_UNIT == ADC_UNIT_2)  // <-- should be `unit`
    {
        err = adc2_config_channel_atten(channel, atten);
    }
#endif
    if (err != ESP_OK)
    {
        RG_LOGE("Failed to configure ADC_UNIT_%d channel:%d atten:%d error:0x%02X",
                (int)unit, ...);            // already uses `unit`

Two things make this bite:

  1. USE_ADC_DRIVER_NG is commented out at the top of the file (// #define USE_ADC_DRIVER_NG), so the #else branch is the one actually compiled.
  2. The function is static inline, so its body is compiled even when nothing calls it.

Consequently a target with, say, RG_GAMEPAD_ADC_MAP but no battery ADC — or simply no RG_BATTERY_* defines at all — dies with:

rg_input.c:71:9: error: 'RG_BATTERY_ADC_UNIT' undeclared (first use in this function);
                        did you mean 'RG_BATTERY_CALC_VOLTAGE'?

The intent is clearly to use the parameter: the very next RG_LOGE() already prints (int)unit, and the RG_ASSERT at the top validates unit.

Fix

Use unit in the legacy branch, matching the USE_ADC_DRIVER_NG branch and the assert/log around it.

Testing

Found while porting a new 160x128 ESP32 target that has no battery ADC. Before the change it would not compile on dev; after it builds and runs (ESP-IDF v5.5.2, release build) with both the gamepad and battery paths behaving as before on targets that do define RG_BATTERY_ADC_UNIT (for those, unit is exactly what was being passed in anyway).

_adc_setup_channel() takes an `adc_unit_t unit` parameter, but its legacy branch
tested the battery macro RG_BATTERY_ADC_UNIT instead. Any target that does not
define RG_BATTERY_ADC_UNIT therefore fails to compile:

  rg_input.c:71:9: error: 'RG_BATTERY_ADC_UNIT' undeclared (first use in this
                   function); did you mean 'RG_BATTERY_CALC_VOLTAGE'?

Two things make this bite: USE_ADC_DRIVER_NG is commented out at the top of the
file, so the #else branch is the one actually compiled; and the function is
`static inline`, so its body is compiled even when nothing calls it.

The intent is clearly the parameter -- the RG_ASSERT above validates `unit`, and
the RG_LOGE just below already prints (int)unit. For targets that do define
RG_BATTERY_ADC_UNIT this is a no-op, since that is exactly what was passed in.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant