Skip to content

Commit a76ec58

Browse files
committed
Rename low-side ADC path labels from legacy to ADC_READ.
Wrap ESP32 digi ADC sources in ARDUINO_ARCH_ESP32 guards so AVR CI builds do not pull sdkconfig.h. Select DMA backend per chip (I2S / SPI3 / GDMA) via internal.h so classic ESP32 and S2 do not compile GDMA. Include esp32_mcu.h before MCPWM guard in lowside.cpp.
1 parent b4aaeb0 commit a76ec58

9 files changed

Lines changed: 73 additions & 42 deletions

src/current_sense/hardware_specific/esp32/esp32_adc_digi_driver.c

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@
1010
* - ETM: ESP32-S3 and newer only; see esp32_adc_etm.c.
1111
*/
1212

13-
#include "sdkconfig.h"
13+
#if defined(ARDUINO_ARCH_ESP32)
14+
1415
#include "esp32_adc_digi_internal.h"
1516

1617
#if SIMPLEFOC_ESP32_ADC_DIGI_SUPPORTED
@@ -118,13 +119,25 @@ static void esp32_adc_apply_max_clock(esp32_adc_digi_ctx_t *adc)
118119
adc_ll_digi_set_clk_div(1);
119120
adc_ll_set_sample_cycle(ADC_LL_SAMPLE_CYCLE_DEFAULT);
120121
#else
122+
#if defined(ADC_DIGI_CLK_SRC_PLL_F80M)
121123
const soc_module_clk_t fast_src = SOC_MOD_CLK_PLL_F80M;
124+
const soc_periph_adc_digi_clk_src_t adc_clk = ADC_DIGI_CLK_SRC_PLL_F80M;
125+
#elif defined(ADC_DIGI_CLK_SRC_PLL_F96M)
126+
const soc_module_clk_t fast_src = SOC_MOD_CLK_PLL_F96M;
127+
const soc_periph_adc_digi_clk_src_t adc_clk = ADC_DIGI_CLK_SRC_PLL_F96M;
128+
#elif defined(ADC_DIGI_CLK_SRC_APB)
129+
const soc_module_clk_t fast_src = SOC_MOD_CLK_APB;
130+
const soc_periph_adc_digi_clk_src_t adc_clk = ADC_DIGI_CLK_SRC_APB;
131+
#else
132+
const soc_module_clk_t fast_src = (soc_module_clk_t)ADC_DIGI_CLK_SRC_DEFAULT;
133+
const soc_periph_adc_digi_clk_src_t adc_clk = ADC_DIGI_CLK_SRC_DEFAULT;
134+
#endif
122135
ESP_ERROR_CHECK(esp_clk_tree_enable_src(fast_src, true));
123136
uint32_t clk_hz = 0;
124137
ESP_ERROR_CHECK(esp_clk_tree_src_get_freq_hz(fast_src, ESP_CLK_TREE_SRC_FREQ_PRECISION_CACHED, &clk_hz));
125-
adc->hal_cfg.clk_src = ADC_DIGI_CLK_SRC_PLL_F80M;
138+
adc->hal_cfg.clk_src = adc_clk;
126139
adc->hal_cfg.clk_src_freq_hz = clk_hz;
127-
adc_ll_digi_clk_sel(ADC_DIGI_CLK_SRC_PLL_F80M);
140+
adc_ll_digi_clk_sel(adc_clk);
128141
adc_ll_digi_controller_clk_div(0, 1, 0);
129142
adc_ll_digi_set_clk_div(1);
130143
adc_ll_set_sample_cycle(ADC_LL_SAMPLE_CYCLE_DEFAULT);
@@ -443,4 +456,5 @@ esp_err_t esp32_adc_digi_trigger_software(void)
443456
return ESP_OK;
444457
}
445458

446-
#endif
459+
#endif /* SIMPLEFOC_ESP32_ADC_DIGI_SUPPORTED */
460+
#endif /* ARDUINO_ARCH_ESP32 */

src/current_sense/hardware_specific/esp32/esp32_adc_digi_driver.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@
88
*/
99
#pragma once
1010

11-
#include "sdkconfig.h"
11+
#if defined(ARDUINO_ARCH_ESP32)
12+
1213
#include "esp_err.h"
1314
#include "hal/adc_types.h"
1415
#include "esp32_adc_digi_internal.h"
@@ -52,3 +53,4 @@ bool esp32_adc_digi_etm_supported(void);
5253
#endif
5354

5455
#endif /* SIMPLEFOC_ESP32_ADC_DIGI_SUPPORTED */
56+
#endif /* ARDUINO_ARCH_ESP32 */

src/current_sense/hardware_specific/esp32/esp32_adc_digi_internal.h

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,13 @@
55
*/
66
#pragma once
77

8+
#if !defined(ARDUINO_ARCH_ESP32)
9+
10+
#define SIMPLEFOC_ESP32_ADC_DIGI_SUPPORTED 0
11+
#define SIMPLEFOC_ESP32_ADC_ETM_SUPPORTED 0
12+
13+
#else
14+
815
#include <stdbool.h>
916
#include <stdint.h>
1017
#include "sdkconfig.h"
@@ -29,36 +36,29 @@
2936
* - ESP32-S3+: GDMA (esp32_adc_dma_gdma.c)
3037
*/
3138

32-
#if defined(ARDUINO_ARCH_ESP32)
33-
3439
#define SIMPLEFOC_ESP32_ADC_DIGI_SUPPORTED 1
3540

36-
/* ETM is not available on original ESP32 or ESP32-S2. */
37-
#if SOC_ETM_SUPPORTED && !CONFIG_IDF_TARGET_ESP32 && !CONFIG_IDF_TARGET_ESP32S2
38-
#define SIMPLEFOC_ESP32_ADC_ETM_SUPPORTED 1
39-
#else
40-
#define SIMPLEFOC_ESP32_ADC_ETM_SUPPORTED 0
41+
#if CONFIG_IDF_TARGET_ESP32
42+
#define SIMPLEFOC_ESP32_ADC_USE_I2S_DMA 1
43+
#elif CONFIG_IDF_TARGET_ESP32S2
44+
#define SIMPLEFOC_ESP32_ADC_USE_SPI3_DMA 1
45+
#elif SOC_GDMA_SUPPORTED
46+
#define SIMPLEFOC_ESP32_ADC_USE_GDMA_DMA 1
4147
#endif
4248

49+
#if SOC_ETM_SUPPORTED && defined(SIMPLEFOC_ESP32_ADC_USE_GDMA_DMA)
50+
#define SIMPLEFOC_ESP32_ADC_ETM_SUPPORTED 1
4351
#else
44-
45-
#define SIMPLEFOC_ESP32_ADC_DIGI_SUPPORTED 0
4652
#define SIMPLEFOC_ESP32_ADC_ETM_SUPPORTED 0
47-
4853
#endif
4954

50-
/* Pattern / DMA sizing — kept aligned with espFoC isensor_adc_internal.h */
5155
#define SIMPLEFOC_ESP32_ADC_PATTERN_HZ 80000
5256
#define SIMPLEFOC_ESP32_ADC_NUM_CHANNELS 2
5357
#define SIMPLEFOC_ESP32_ADC_CONVERT_LIMIT 2
5458

5559
#define ESP32_ADC_DIGI_FRAME_BYTES \
5660
(SIMPLEFOC_ESP32_ADC_NUM_CHANNELS * SOC_ADC_DIGI_RESULT_BYTES)
5761

58-
/*
59-
* DMA writes adc_digi_output_data_t[N] into adc_buffer (zero-copy).
60-
* int adc_buffer[3] must be DMA-capable and >= ESP32_ADC_DIGI_FRAME_BYTES used bytes.
61-
*/
6262
#if CONFIG_IDF_TARGET_ESP32 || CONFIG_IDF_TARGET_ESP32S2
6363
#define ESP32_ADC_DIGI_SAMPLE_RAW(p) ((int32_t)((p)->type1.data))
6464
#else
@@ -106,3 +106,5 @@ esp_err_t esp32_adc_digi_etm_enable(bool enable);
106106
void esp32_adc_digi_etm_disconnect(void);
107107

108108
#endif /* SIMPLEFOC_ESP32_ADC_ETM_SUPPORTED */
109+
110+
#endif /* ARDUINO_ARCH_ESP32 */

src/current_sense/hardware_specific/esp32/esp32_adc_digi_lowside.cpp

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
1-
#include "esp32_adc_digi_lowside.h"
1+
#include "esp32_mcu.h"
2+
3+
#if defined(ESP_H) && defined(ARDUINO_ARCH_ESP32) && defined(SOC_MCPWM_SUPPORTED) \
4+
&& !defined(SIMPLEFOC_ESP32_USELEDC)
25

3-
#if SIMPLEFOC_ESP32_ADC_DIGI_SUPPORTED && defined(SOC_MCPWM_SUPPORTED) && !defined(SIMPLEFOC_ESP32_USELEDC)
6+
#include "esp32_adc_digi_lowside.h"
47

58
#include "esp32_adc_digi_driver.h"
69
#include "../../../drivers/hardware_specific/esp32/esp32_driver_mcpwm.h"
@@ -47,12 +50,12 @@ static esp_err_t esp32_adc_digi_bind_params(ESP32CurrentSenseParams *params)
4750
ESP32AdcLowsidePath esp32_adc_lowside_configure(ESP32CurrentSenseParams *params)
4851
{
4952
if (params == NULL || !esp32_adc_digi_supported()) {
50-
return ESP32_ADC_LOWSIDE_LEGACY;
53+
return ESP32_ADC_LOWSIDE_ADC_READ;
5154
}
5255

5356
if (esp32_adc_digi_bind_params(params) != ESP_OK) {
54-
SIMPLEFOC_ESP32_CS_DEBUG("WARN: ADC digi+DMA init failed, using legacy adcRead path");
55-
return ESP32_ADC_LOWSIDE_LEGACY;
57+
SIMPLEFOC_ESP32_CS_DEBUG("WARN: ADC digi+DMA init failed, using ADC_READ (adcRead) path");
58+
return ESP32_ADC_LOWSIDE_ADC_READ;
5659
}
5760

5861
params->adc_lowside_path = ESP32_ADC_LOWSIDE_DIGI_SW;
@@ -191,7 +194,7 @@ void *esp32_adc_lowside_sync_mcpwm(void *driver_params, ESP32CurrentSenseParams
191194
}
192195

193196
#if SIMPLEFOC_ESP32_ADC_ETM_SUPPORTED
194-
if (esp32_adc_digi_etm_supported() && cs->adc_lowside_path != ESP32_ADC_LOWSIDE_LEGACY) {
197+
if (esp32_adc_digi_etm_supported() && cs->adc_lowside_path != ESP32_ADC_LOWSIDE_ADC_READ) {
195198
return esp32_adc_lowside_sync_etm(driver_params, cs);
196199
}
197200
#endif

src/current_sense/hardware_specific/esp32/esp32_adc_digi_lowside.h

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,12 @@
77
*/
88
#pragma once
99

10+
#if defined(ESP_H) && defined(ARDUINO_ARCH_ESP32) && defined(SOC_MCPWM_SUPPORTED) \
11+
&& !defined(SIMPLEFOC_ESP32_USELEDC)
12+
1013
#include "esp32_adc_digi_internal.h"
1114

12-
#if SIMPLEFOC_ESP32_ADC_DIGI_SUPPORTED && defined(SOC_MCPWM_SUPPORTED) && !defined(SIMPLEFOC_ESP32_USELEDC)
15+
#if SIMPLEFOC_ESP32_ADC_DIGI_SUPPORTED
1316

1417
#include "esp32_mcu.h"
1518

@@ -18,4 +21,5 @@ bool esp32_adc_lowside_uses_mcpwm_isr(const ESP32CurrentSenseParams *params);
1821
void *esp32_adc_lowside_sync_mcpwm(void *driver_params, ESP32CurrentSenseParams *cs);
1922
void esp32_adc_lowside_start_conversion(void);
2023

24+
#endif /* SIMPLEFOC_ESP32_ADC_DIGI_SUPPORTED */
2125
#endif

src/current_sense/hardware_specific/esp32/esp32_adc_dma_esp32.c

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,11 @@
66
* I2S DMA backend for ADC digi on ESP32 (i2s_ll only).
77
*/
88

9-
#include "sdkconfig.h"
9+
#if defined(ARDUINO_ARCH_ESP32)
1010

11-
#if CONFIG_IDF_TARGET_ESP32
11+
#include "esp32_adc_digi_internal.h"
12+
13+
#if defined(SIMPLEFOC_ESP32_ADC_USE_I2S_DMA) && SIMPLEFOC_ESP32_ADC_USE_I2S_DMA
1214

1315
#include "esp_err.h"
1416
#include "esp_intr_alloc.h"
@@ -17,7 +19,6 @@
1719
#include "hal/adc_hal.h"
1820
#include "soc/i2s_periph.h"
1921
#include "esp_private/i2s_platform.h"
20-
#include "esp32_adc_digi_internal.h"
2122

2223
#define ADC_DMA_I2S_HOST ADC_HAL_DMA_I2S_HOST
2324
#define ADC_DMA_INTR_MASK BIT(9)
@@ -128,4 +129,5 @@ esp_err_t esp32_adc_digi_dma_reset(esp32_adc_digi_dma_ctx_t *ctx)
128129
return ESP_OK;
129130
}
130131

131-
#endif
132+
#endif /* SIMPLEFOC_ESP32_ADC_USE_I2S_DMA */
133+
#endif /* ARDUINO_ARCH_ESP32 */

src/current_sense/hardware_specific/esp32/esp32_adc_dma_esp32s2.c

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,17 @@
66
* SPI3 DMA backend for ADC digi on ESP32-S2 (spi_ll + spicommon).
77
*/
88

9-
#include "sdkconfig.h"
9+
#if defined(ARDUINO_ARCH_ESP32)
1010

11-
#if CONFIG_IDF_TARGET_ESP32S2
11+
#include "esp32_adc_digi_internal.h"
12+
13+
#if defined(SIMPLEFOC_ESP32_ADC_USE_SPI3_DMA) && SIMPLEFOC_ESP32_ADC_USE_SPI3_DMA
1214

1315
#include "esp_err.h"
1416
#include "esp_intr_alloc.h"
1517
#include "esp_attr.h"
1618
#include "hal/spi_ll.h"
1719
#include "esp_private/spi_common_internal.h"
18-
#include "esp32_adc_digi_internal.h"
1920

2021
#define ADC_DMA_SPI_HOST SPI3_HOST
2122
#define ADC_DMA_INTR_MASK SPI_LL_INTR_IN_SUC_EOF
@@ -133,4 +134,5 @@ esp_err_t esp32_adc_digi_dma_reset(esp32_adc_digi_dma_ctx_t *ctx)
133134
return ESP_OK;
134135
}
135136

136-
#endif
137+
#endif /* SIMPLEFOC_ESP32_ADC_USE_SPI3_DMA */
138+
#endif /* ARDUINO_ARCH_ESP32 */

src/current_sense/hardware_specific/esp32/esp32_adc_dma_gdma.c

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,11 @@
66
* GDMA RX backend for ADC digi (ESP32-S3, C3, C6, …) using gdma_hal + gdma_ll only.
77
*/
88

9-
#include "sdkconfig.h"
9+
#if defined(ARDUINO_ARCH_ESP32)
1010

11-
#if !CONFIG_IDF_TARGET_ESP32 && !CONFIG_IDF_TARGET_ESP32S2
11+
#include "esp32_adc_digi_internal.h"
12+
13+
#if defined(SIMPLEFOC_ESP32_ADC_USE_GDMA_DMA) && SIMPLEFOC_ESP32_ADC_USE_GDMA_DMA
1214

1315
#include "esp_err.h"
1416
#include "esp_intr_alloc.h"
@@ -18,7 +20,6 @@
1820
#include "hal/gdma_ll.h"
1921
#include "hal/gdma_types.h"
2022
#include "soc/gdma_periph.h"
21-
#include "esp32_adc_digi_internal.h"
2223

2324
#define ESP32_ADC_GDMA_GROUP 0
2425
#define ESP32_ADC_GDMA_PAIR 2
@@ -143,4 +144,5 @@ esp_err_t esp32_adc_digi_dma_reset(esp32_adc_digi_dma_ctx_t *ctx)
143144
return ESP_OK;
144145
}
145146

146-
#endif
147+
#endif /* SIMPLEFOC_ESP32_ADC_USE_GDMA_DMA */
148+
#endif /* ARDUINO_ARCH_ESP32 */

src/current_sense/hardware_specific/esp32/esp32_adc_etm.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@
66
* ETM wiring: MCPWM timer event -> ADC digi START task.
77
*/
88

9-
#include "sdkconfig.h"
9+
#if defined(ARDUINO_ARCH_ESP32)
10+
1011
#include "esp32_adc_digi_internal.h"
1112

1213
#if SIMPLEFOC_ESP32_ADC_ETM_SUPPORTED
@@ -19,8 +20,6 @@
1920
#include "esp_private/etm_interface.h"
2021
#include "soc/soc_caps.h"
2122
#include "soc/soc_etm_source.h"
22-
#include "esp32_adc_digi_internal.h"
23-
2423
static const char *TAG = "esp32_adc_etm";
2524

2625
typedef struct {
@@ -157,3 +156,4 @@ void esp32_adc_digi_etm_disconnect(void)
157156
}
158157

159158
#endif /* SIMPLEFOC_ESP32_ADC_ETM_SUPPORTED */
159+
#endif /* ARDUINO_ARCH_ESP32 */

0 commit comments

Comments
 (0)