Skip to content

Commit cb48a7b

Browse files
committed
Merge branch 'feature/ulp_fsm_adc_s3' into 'master'
ulp-fsm: Update ulp-fsm ADC example with S3 support Closes IDFGH-6299 See merge request espressif/esp-idf!19924
2 parents 29830f6 + 77ba84e commit cb48a7b

File tree

11 files changed

+118
-53
lines changed

11 files changed

+118
-53
lines changed

components/ulp/CMakeLists.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@ set(includes "")
66
if(CONFIG_SOC_ULP_SUPPORTED OR CONFIG_SOC_RISCV_COPROC_SUPPORTED)
77

88
list(APPEND srcs
9-
"ulp_common/ulp_common.c")
9+
"ulp_common/ulp_common.c"
10+
"ulp_common/ulp_adc.c")
1011

1112
list(APPEND includes
1213
ulp_common/include
@@ -25,7 +26,6 @@ if(CONFIG_SOC_ULP_SUPPORTED OR CONFIG_SOC_RISCV_COPROC_SUPPORTED)
2526
list(APPEND srcs
2627
"ulp_riscv/ulp_riscv.c"
2728
"ulp_riscv/ulp_riscv_lock.c"
28-
"ulp_riscv/ulp_riscv_adc.c"
2929
"ulp_riscv/ulp_riscv_i2c.c")
3030

3131
list(APPEND includes
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
/*
2+
* SPDX-FileCopyrightText: 2022 Espressif Systems (Shanghai) CO LTD
3+
*
4+
* SPDX-License-Identifier: Apache-2.0
5+
*/
6+
7+
#pragma once
8+
9+
#include "hal/adc_types.h"
10+
#include "esp_err.h"
11+
12+
#ifdef __cplusplus
13+
extern "C" {
14+
#endif
15+
16+
typedef struct {
17+
adc_unit_t adc_n; // ADC Unit
18+
adc_channel_t channel; // ADC channel
19+
adc_atten_t atten; // ADC channel attenuation
20+
adc_bitwidth_t width; // ADC bit width, only used for ADC unit 1
21+
adc_ulp_mode_t ulp_mode; // ADC ULP Mode
22+
} ulp_adc_cfg_t; // ULP FSM ADC configuration parameters
23+
24+
/**
25+
* @brief Initialize and calibrate the ADC for use by ULP FSM
26+
*
27+
* @param cfg Configuration parameters
28+
* @return esp_err_t ESP_OK for successful.
29+
*/
30+
esp_err_t ulp_adc_init(const ulp_adc_cfg_t *cfg);
31+
32+
#ifdef __cplusplus
33+
}
34+
#endif

components/ulp/ulp_riscv/ulp_riscv_adc.c renamed to components/ulp/ulp_common/ulp_adc.c

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@
44
* SPDX-License-Identifier: Apache-2.0
55
*/
66

7-
#include "ulp_riscv_adc.h"
7+
#include "sdkconfig.h"
8+
#include "ulp_adc.h"
89
#include "esp_err.h"
910
#include "esp_check.h"
1011
#include "esp_log.h"
@@ -13,9 +14,9 @@
1314
#include "esp_private/esp_sleep_internal.h"
1415
#include "esp_private/adc_share_hw_ctrl.h"
1516

16-
static const char *TAG = "ulp_riscv_adc";
17+
static const char *TAG = "ulp_adc";
1718

18-
esp_err_t ulp_riscv_adc_init(const ulp_riscv_adc_cfg_t *cfg)
19+
esp_err_t ulp_adc_init(const ulp_adc_cfg_t *cfg)
1920
{
2021
esp_err_t ret = ESP_OK;
2122

@@ -24,10 +25,19 @@ esp_err_t ulp_riscv_adc_init(const ulp_riscv_adc_cfg_t *cfg)
2425

2526
//-------------ADC1 Init---------------//
2627
adc_oneshot_unit_handle_t adc1_handle;
28+
2729
adc_oneshot_unit_init_cfg_t init_config1 = {
2830
.unit_id = cfg->adc_n,
29-
.ulp_mode = ADC_ULP_MODE_RISCV,
31+
.ulp_mode = cfg->ulp_mode,
3032
};
33+
34+
if (init_config1.ulp_mode == ADC_ULP_MODE_DISABLE) {
35+
/* Default to RISCV for backward compatibility */
36+
ESP_LOGI(TAG, "No ulp mode specified in cfg struct, default to riscv");
37+
init_config1.ulp_mode = ADC_ULP_MODE_RISCV;
38+
}
39+
40+
3141
ESP_ERROR_CHECK(adc_oneshot_new_unit(&init_config1, &adc1_handle));
3242

3343
//-------------ADC1 Config---------------//
@@ -38,7 +48,10 @@ esp_err_t ulp_riscv_adc_init(const ulp_riscv_adc_cfg_t *cfg)
3848
ESP_ERROR_CHECK(adc_oneshot_config_channel(adc1_handle, cfg->channel, &config));
3949

4050
//Calibrate the ADC
51+
#if SOC_ADC_CALIBRATION_V1_SUPPORTED
4152
adc_set_hw_calibration_code(cfg->adc_n, cfg->atten);
53+
#endif
54+
4255
esp_sleep_enable_adc_tsens_monitor(true);
4356

4457
err:

components/ulp/ulp_riscv/include/ulp_riscv_adc.h

Lines changed: 5 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -9,24 +9,15 @@
99
#include "hal/adc_types.h"
1010
#include "esp_err.h"
1111

12+
#include "ulp_adc.h"
13+
1214
#ifdef __cplusplus
1315
extern "C" {
1416
#endif
1517

16-
typedef struct {
17-
adc_unit_t adc_n; // ADC Unit
18-
adc_channel_t channel; // ADC channel
19-
adc_atten_t atten; // ADC channel attenuation
20-
adc_bitwidth_t width; // ADC bit width, only used for ADC unit 1
21-
} ulp_riscv_adc_cfg_t; // ULP Riscv ADC configuration parameters
22-
23-
/**
24-
* @brief Initialize and calibrate the ADC for use by ULP RISCV
25-
*
26-
* @param cfg Configuration parameters
27-
* @return esp_err_t ESP_OK for successful.
28-
*/
29-
esp_err_t ulp_riscv_adc_init(const ulp_riscv_adc_cfg_t *cfg);
18+
/* Kept for backwards compatibilty */
19+
#define ulp_riscv_adc_cfg_t ulp_adc_cfg_t
20+
#define ulp_riscv_adc_init ulp_adc_init
3021

3122
#ifdef __cplusplus
3223
}

examples/system/.build-test-rules.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,7 @@ examples/system/ulp_fsm/ulp:
160160

161161
examples/system/ulp_fsm/ulp_adc:
162162
enable:
163-
- if: IDF_TARGET == "esp32"
163+
- if: IDF_TARGET in ["esp32", "esp32s3"]
164164
temporary: true
165165
reason: the other targets are not tested yet
166166

examples/system/ulp_fsm/ulp_adc/README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1-
| Supported Targets | ESP32 |
2-
| ----------------- | ----- |
1+
| Supported Targets | ESP32 | ESP32-S3 |
2+
| ----------------- | ----- | -------- |
33

44
# ULP ADC Example
55

66
This example demonstrates how to use the ULP coprocessor to poll ADC in deep sleep.
77

8-
ULP program periodically measures the input voltage on GPIO34. The voltage is compared to two thresholds. If the voltage is less than the low threshold, or higher than the high threshold, ULP wakes up the system.
8+
ULP program periodically measures the input voltage on ADC_CHANNEL_6 (GPIO34 on ESP32, GPIO7 on ESP32-S3). The voltage is compared to two thresholds. If the voltage is less than the low threshold, or higher than the high threshold, ULP wakes up the system.
99

1010
By default, thresholds are set to 1.35V and 1.75V, approximately.
1111

examples/system/ulp_fsm/ulp_adc/main/ulp/adc.S

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
/*
2+
* SPDX-FileCopyrightText: 2022 Espressif Systems (Shanghai) CO LTD
3+
*
4+
* SPDX-License-Identifier: Unlicense OR CC0-1.0
5+
*/
16
/* ULP Example: using ADC in deep sleep
27
38
This example code is in the Public Domain (or CC0 licensed, at your option.)
@@ -22,9 +27,9 @@
2227
*/
2328
#include "soc/rtc_cntl_reg.h"
2429
#include "soc/soc_ulp.h"
30+
#include "example_config.h"
2531

26-
/* ADC1 channel 6, GPIO34 */
27-
.set adc_channel, 6
32+
.set adc_channel, EXAMPLE_ADC_CHANNEL
2833

2934
/* Configure the number of ADC samples to average on each measurement.
3035
For convenience, make it a power of 2. */
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
/*
2+
* SPDX-FileCopyrightText: 2022 Espressif Systems (Shanghai) CO LTD
3+
*
4+
* SPDX-License-Identifier: Unlicense OR CC0-1.0
5+
*/
6+
#pragma once
7+
8+
/* Ints are used here to be able to include the file in assembly as well */
9+
#define EXAMPLE_ADC_CHANNEL 6 // ADC_CHANNEL_6, GPIO34 on ESP32, GPIO7 on ESP32-S3
10+
#define EXAMPLE_ADC_UNIT 0 // ADC_UNIT_1
11+
#define EXAMPLE_ADC_ATTEN 3 // ADC_ATTEN_DB_11
12+
#define EXAMPLE_ADC_WIDTH 0 // ADC_BITWIDTH_DEFAULT
13+
14+
/* Set low and high thresholds, approx. 1.35V - 1.75V*/
15+
#define EXAMPLE_ADC_LOW_TRESHOLD 1500
16+
#define EXAMPLE_ADC_HIGH_TRESHOLD 2000

examples/system/ulp_fsm/ulp_adc/main/ulp_adc_example_main.c

Lines changed: 24 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -10,16 +10,18 @@
1010
#include <stdio.h>
1111
#include <string.h>
1212
#include "esp_sleep.h"
13-
#include "nvs.h"
14-
#include "nvs_flash.h"
1513
#include "soc/rtc_cntl_reg.h"
1614
#include "soc/sens_reg.h"
1715
#include "driver/gpio.h"
1816
#include "driver/rtc_io.h"
19-
#include "driver/dac.h"
20-
#include "esp32/ulp.h"
17+
#include "ulp.h"
2118
#include "ulp_main.h"
2219
#include "esp_adc/adc_oneshot.h"
20+
#include "ulp/example_config.h"
21+
#include "ulp_adc.h"
22+
23+
#include "freertos/FreeRTOS.h"
24+
#include "freertos/task.h"
2325

2426
extern const uint8_t ulp_main_bin_start[] asm("_binary_ulp_main_bin_start");
2527
extern const uint8_t ulp_main_bin_end[] asm("_binary_ulp_main_bin_end");
@@ -51,6 +53,12 @@ void app_main(void)
5153
printf("Entering deep sleep\n\n");
5254
start_ulp_program();
5355
ESP_ERROR_CHECK( esp_sleep_enable_ulp_wakeup() );
56+
57+
#if !CONFIG_IDF_TARGET_ESP32
58+
/* RTC peripheral power domain needs to be kept on to keep SAR ADC related configs during sleep */
59+
esp_sleep_pd_config(ESP_PD_DOMAIN_RTC_PERIPH, ESP_PD_OPTION_ON);
60+
#endif
61+
5462
esp_deep_sleep_start();
5563
}
5664

@@ -60,35 +68,31 @@ static void init_ulp_program(void)
6068
(ulp_main_bin_end - ulp_main_bin_start) / sizeof(uint32_t));
6169
ESP_ERROR_CHECK(err);
6270

63-
//-------------ADC1 Init---------------//
64-
adc_oneshot_unit_handle_t adc1_handle;
65-
adc_oneshot_unit_init_cfg_t init_config1 = {
66-
.unit_id = ADC_UNIT_1,
71+
ulp_adc_cfg_t cfg = {
72+
.adc_n = EXAMPLE_ADC_UNIT,
73+
.channel = EXAMPLE_ADC_CHANNEL,
74+
.width = EXAMPLE_ADC_WIDTH,
75+
.atten = EXAMPLE_ADC_ATTEN,
6776
.ulp_mode = ADC_ULP_MODE_FSM,
6877
};
69-
ESP_ERROR_CHECK(adc_oneshot_new_unit(&init_config1, &adc1_handle));
7078

71-
//-------------ADC1 Channel Config---------------//
72-
// Note: when changing channel here, also change 'adc_channel' constant in adc.S
73-
adc_oneshot_chan_cfg_t config = {
74-
.bitwidth = ADC_BITWIDTH_DEFAULT,
75-
.atten = ADC_ATTEN_DB_11,
76-
};
77-
ESP_ERROR_CHECK(adc_oneshot_config_channel(adc1_handle, ADC_CHANNEL_6, &config));
79+
ESP_ERROR_CHECK(ulp_adc_init(&cfg));
7880

79-
/* Set low and high thresholds, approx. 1.35V - 1.75V*/
80-
ulp_low_thr = 1500;
81-
ulp_high_thr = 2000;
81+
ulp_low_thr = EXAMPLE_ADC_LOW_TRESHOLD;
82+
ulp_high_thr = EXAMPLE_ADC_HIGH_TRESHOLD;
8283

8384
/* Set ULP wake up period to 20ms */
8485
ulp_set_wakeup_period(0, 20000);
8586

87+
#if CONFIG_IDF_TARGET_ESP32
8688
/* Disconnect GPIO12 and GPIO15 to remove current drain through
87-
* pullup/pulldown resistors.
89+
* pullup/pulldown resistors on modules which have these (e.g. ESP32-WROVER)
8890
* GPIO12 may be pulled high to select flash voltage.
8991
*/
9092
rtc_gpio_isolate(GPIO_NUM_12);
9193
rtc_gpio_isolate(GPIO_NUM_15);
94+
#endif // CONFIG_IDF_TARGET_ESP32
95+
9296
esp_deep_sleep_disable_rom_logging(); // suppress boot messages
9397
}
9498

examples/system/ulp_fsm/ulp_adc/pytest_ulp_fsm_adc.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99

1010
@pytest.mark.esp32
11+
@pytest.mark.esp32s3
1112
@pytest.mark.generic
1213
def test_ulp_fsm_adc(dut: Dut) -> None:
1314

examples/system/ulp_riscv/adc/main/ulp_riscv_adc_example_main.c

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
#include <stdio.h>
1616
#include "esp_sleep.h"
1717
#include "ulp_riscv.h"
18-
#include "ulp_riscv_adc.h"
18+
#include "ulp_adc.h"
1919
#include "ulp_main.h"
2020
#include "ulp/example_config.h"
2121

@@ -56,14 +56,15 @@ void app_main(void)
5656

5757
static void init_ulp_program(void)
5858
{
59-
ulp_riscv_adc_cfg_t cfg = {
60-
.adc_n = EXAMPLE_ADC_UNIT,
61-
.channel = EXAMPLE_ADC_CHANNEL,
62-
.width = EXAMPLE_ADC_WIDTH,
63-
.atten = EXAMPLE_ADC_ATTEN,
59+
ulp_adc_cfg_t cfg = {
60+
.adc_n = EXAMPLE_ADC_UNIT,
61+
.channel = EXAMPLE_ADC_CHANNEL,
62+
.width = EXAMPLE_ADC_WIDTH,
63+
.atten = EXAMPLE_ADC_ATTEN,
64+
.ulp_mode = ADC_ULP_MODE_RISCV,
6465
};
6566

66-
ESP_ERROR_CHECK(ulp_riscv_adc_init(&cfg));
67+
ESP_ERROR_CHECK(ulp_adc_init(&cfg));
6768

6869
esp_err_t err = ulp_riscv_load_binary(ulp_main_bin_start, (ulp_main_bin_end - ulp_main_bin_start));
6970
ESP_ERROR_CHECK(err);

0 commit comments

Comments
 (0)