1010*/
1111#include " adapter_gpio.hpp"
1212#include < driver/gpio.h>
13+
14+ #if defined(M5_UNIT_UNIFIED_USING_RMT_V2)
15+ #pragma message "Using RMT v2,Oneshot"
16+ #include < esp_adc/adc_oneshot.h>
17+ #else
18+ #pragma message "Using RMT v1"
1319#include < driver/adc.h>
20+ #endif
1421
1522#if defined(SOC_DAC_SUPPORTED) && SOC_DAC_SUPPORTED
1623#pragma message "DAC supported"
@@ -259,6 +266,8 @@ constexpr int8_t gpio_to_adc_table[] = {
259266#error Invalid target
260267#endif
261268
269+ // 0-9: ADC1 10-:ADC2 (ESP-IDF 4.x)
270+ // 0-9, 10- : ADC (ESP-IDF 5.x)
262271int8_t gpio_to_adc_channel (const int8_t pin)
263272{
264273 if (pin < 0 || pin >= m5::stl::size (gpio_to_adc_table)) {
@@ -329,7 +338,7 @@ m5::hal::error::error_t AdapterGPIOBase::GPIOImpl::write_digital(const gpio_num_
329338
330339m5::hal::error::error_t AdapterGPIOBase::GPIOImpl::read_digital (const gpio_num_t pin, bool & high)
331340{
332- high = false ;
341+ high = true ;
333342 high = gpio_get_level (pin);
334343 return m5::hal::error::error_t ::OK;
335344}
@@ -360,9 +369,47 @@ m5::hal::error::error_t AdapterGPIOBase::GPIOImpl::read_analog(uint16_t& value,
360369 if (ch < 0 ) {
361370 return m5::hal::error::error_t ::INVALID_ARGUMENT;
362371 }
372+ #if !defined(SOC_ADC_PERIPH_NUM) || SOC_ADC_PERIPH_NUM <= 1
373+ if (ch >= 10 ) {
374+ M5_LIB_LOGE (" Not support ADC2" );
375+ return m5::hal::error::error_t ::NOT_IMPLEMENTED;
376+ }
377+ #endif
378+
379+ #if defined(M5_UNIT_UNIFIED_USING_ADC_ONESHOT)
380+ // ESP-IDF 5.x
381+
382+ adc_unit_t unit = (ch < 10 ) ? ADC_UNIT_1 : ADC_UNIT_2;
383+ adc_channel_t channel = static_cast <adc_channel_t >((ch < 10 ) ? ch : (ch - 10 ));
384+
385+ adc_oneshot_unit_handle_t adc_handle{};
386+ adc_oneshot_unit_init_cfg_t init_config = {
387+ .unit_id = unit, .clk_src = ADC_RTC_CLK_SRC_DEFAULT, .ulp_mode = ADC_ULP_MODE_DISABLE};
363388
389+ if (adc_oneshot_new_unit (&init_config, &adc_handle) != ESP_OK) {
390+ return m5::hal::error::error_t ::UNKNOWN_ERROR;
391+ }
392+
393+ adc_oneshot_chan_cfg_t chan_config = {
394+ .atten = ADC_ATTEN_DB_12, // 0~3.3V
395+ .bitwidth = ADC_BITWIDTH_DEFAULT // 12bit
396+ };
397+
398+ auto ret = m5::hal::error::error_t ::UNKNOWN_ERROR;
399+ if (adc_oneshot_config_channel (adc_handle, channel, &chan_config) == ESP_OK) {
400+ int raw{};
401+ if (adc_oneshot_read (adc_handle, channel, &raw) == ESP_OK) {
402+ value = static_cast <uint16_t >(raw);
403+ ret = m5::hal::error::error_t ::OK;
404+ }
405+ }
406+ adc_oneshot_del_unit (adc_handle);
407+ return ret;
408+
409+ #else
410+ // ESP-IDF 4.x
411+ // ADC2
364412 if (ch >= 10 ) {
365- // ADC2
366413#if SOC_ADC_SUPPORTED && SOC_ADC_PERIPH_NUM > 1
367414 adc2_channel_t channel = static_cast <adc2_channel_t >(ch - 10 );
368415 int v = 0 ;
@@ -371,9 +418,6 @@ m5::hal::error::error_t AdapterGPIOBase::GPIOImpl::read_analog(uint16_t& value,
371418 }
372419 value = static_cast <uint16_t >(v);
373420 return m5::hal::error::error_t ::OK;
374- #else
375- M5_LIB_LOGE (" Not support ADC2" );
376- return m5::hal::error::error_t ::NOT_IMPLEMENTED;
377421#endif
378422 }
379423 // ADC1
@@ -382,6 +426,7 @@ m5::hal::error::error_t AdapterGPIOBase::GPIOImpl::read_analog(uint16_t& value,
382426 adc1_config_channel_atten (channel, ADC_ATTEN_DB_12);
383427 value = static_cast <uint16_t >(adc1_get_raw (channel));
384428 return m5::hal::error::error_t ::OK;
429+ #endif
385430}
386431
387432m5::hal::error::error_t AdapterGPIOBase::GPIOImpl::pulse_in (uint32_t & duration, const gpio_num_t pin, const int state,
0 commit comments