Skip to content

Commit 3ed1b3c

Browse files
committed
Fix I2C pin lookup field name across chip generations
1 parent 908e721 commit 3ed1b3c

1 file changed

Lines changed: 18 additions & 15 deletions

File tree

src/m5_unit_component/adapter_i2c.cpp

Lines changed: 18 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -25,26 +25,29 @@
2525
#if defined(ARDUINO)
2626

2727
namespace {
28+
29+
// The input-signal selection field of gpio_func_in_sel_cfg_reg_t differs by chip generation
30+
// (independent of the ESP-IDF version):
31+
// Older chips (ESP32/S2/S3/C3): func_sel
32+
// Newer chips (C6/H2/P4 and later): in_sel
33+
// Detect which member exists at compile time and read whichever is present (no chip defines needed).
34+
template <class T>
35+
auto read_func_in_sel(const volatile T& reg, int) -> decltype(+reg.in_sel)
36+
{
37+
return reg.in_sel;
38+
}
39+
template <class T>
40+
auto read_func_in_sel(const volatile T& reg, long) -> decltype(+reg.func_sel)
41+
{
42+
return reg.func_sel;
43+
}
44+
2845
int16_t search_pin_number(const int peripheral_sig)
2946
{
3047
int16_t no{-1};
31-
#if defined(CONFIG_IDF_TARGET_ESP32C6)
32-
// C6
3348
no = (peripheral_sig >= 0 && peripheral_sig < m5::stl::size(GPIO.func_in_sel_cfg))
34-
? GPIO.func_in_sel_cfg[peripheral_sig].in_sel
49+
? static_cast<int16_t>(read_func_in_sel(GPIO.func_in_sel_cfg[peripheral_sig], 0))
3550
: -1;
36-
#elif defined(CONFIG_IDF_TARGET_ESP32P4)
37-
// P4
38-
no = (peripheral_sig >= 0 && peripheral_sig < m5::stl::size(GPIO.func_in_sel_cfg))
39-
? GPIO.func_in_sel_cfg[peripheral_sig].in_sel
40-
: -1;
41-
#else
42-
// Others
43-
no = (peripheral_sig >= 0 && peripheral_sig < m5::stl::size(GPIO.func_in_sel_cfg))
44-
? GPIO.func_in_sel_cfg[peripheral_sig].func_sel
45-
: -1;
46-
#endif
47-
4851
return (no < GPIO_NUM_MAX) ? no : -1;
4952
}
5053

0 commit comments

Comments
 (0)