Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion CMake/Modules/FindSystem.Device.Adc.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ list(APPEND System.Device.Adc_INCLUDE_DIRS ${CMAKE_SOURCE_DIR}/src/System.Device

# source files
set(System.Device.Adc_SRCS

sys_dev_adc_native.cpp

sys_dev_adc_native_System_Device_Adc_AdcChannel.cpp
Expand Down
2 changes: 2 additions & 0 deletions CMake/binutils.ESP32.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -687,6 +687,7 @@ macro(nf_add_idf_as_library)
esp_netif
esp_eth
esp_psram
esp_adc
littlefs
)

Expand All @@ -702,6 +703,7 @@ macro(nf_add_idf_as_library)
idf::esp_netif
idf::esp_eth
idf::esp_psram
idf::esp_adc
idf::littlefs
)

Expand Down
2 changes: 1 addition & 1 deletion CMake/riscv-esp32c6.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
"SUPPORT_ANY_BASE_CONVERSION": "ON",
"API_System.Net": "ON",
"API_System.Math": "ON",
"API_System.Device.Adc": "OFF",
"API_System.Device.Adc": "ON",
"API_System.Device.Gpio": "ON",
"API_System.Device.I2c": "ON",
"API_System.Device.I2c.Slave": "ON",
Expand Down
2 changes: 1 addition & 1 deletion CMake/riscv-esp32h2.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
"SUPPORT_ANY_BASE_CONVERSION": "ON",
"API_System.Net": "ON",
"API_System.Math": "ON",
"API_System.Device.Adc": "OFF",
"API_System.Device.Adc": "ON",
"API_System.Device.Gpio": "ON",
"API_System.Device.I2c": "ON",
"API_System.Device.I2c.Slave": "ON",
Expand Down
22 changes: 11 additions & 11 deletions targets/ESP32/_common/ESP32_C6_DeviceMapping.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,7 @@ int8_t Esp32_SPI_DevicePinMap[MAX_SPI_DEVICES][Esp32SpiPin_Max] = {
// others assign as NONE because the default pins can be shared with serial flash and PSRAM
int8_t Esp32_SERIAL_DevicePinMap[UART_NUM_MAX][Esp32SerialPin_Max] = {
// COM 1 - pins 21, 20
{UART_NUM_0_TXD_DIRECT_GPIO_NUM,
UART_NUM_0_RXD_DIRECT_GPIO_NUM,
UART_PIN_NO_CHANGE,
UART_PIN_NO_CHANGE},
{UART_NUM_0_TXD_DIRECT_GPIO_NUM, UART_NUM_0_RXD_DIRECT_GPIO_NUM, UART_PIN_NO_CHANGE, UART_PIN_NO_CHANGE},

#if defined(UART_NUM_2)
// COM 2 - all set to UART_PIN_NO_CHANGE
Expand Down Expand Up @@ -56,15 +53,18 @@ int8_t Esp32_LED_DevicePinMap[6] = {
-1, // 6
};

// We use "ADC1" for 20 logical channels
// Mapped to ESP32 controllers
// We use "ADC1" for 7 logical channels
// Mapped to ESP32_C6 controllers
// ESP32 ADC1 channels 0 - 6
// " ADC1 channel 7 - Internal Temperature sensor ?
// " ADC1 channel 8 - vdd33 ?
// TODO review ADC channels for ESP32_C3 , Internal Temperature sensor (VP) & Vdd33
int8_t Esp32_ADC_DevicePinMap[7] = {
// 0 1 2 3 4 5
0, 1, 2, 3, 4, 5, 6};
// 0 1 2 3 4 5
0,
1,
2,
3,
4,
5,
6};

// I2S
// 1 device I2S1
Expand Down
21 changes: 6 additions & 15 deletions targets/ESP32/_common/ESP32_H2_DeviceMapping.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,7 @@ int8_t Esp32_SPI_DevicePinMap[MAX_SPI_DEVICES][Esp32SpiPin_Max] = {
// others assign as NONE because the default pins can be shared with serial flash and PSRAM
int8_t Esp32_SERIAL_DevicePinMap[UART_NUM_MAX][Esp32SerialPin_Max] = {
// COM 1 - pins 21, 20
{UART_NUM_0_TXD_DIRECT_GPIO_NUM,
UART_NUM_0_RXD_DIRECT_GPIO_NUM,
UART_PIN_NO_CHANGE,
UART_PIN_NO_CHANGE},
{UART_NUM_0_TXD_DIRECT_GPIO_NUM, UART_NUM_0_RXD_DIRECT_GPIO_NUM, UART_PIN_NO_CHANGE, UART_PIN_NO_CHANGE},

#if defined(UART_NUM_2)
// COM 2 - all set to UART_PIN_NO_CHANGE
Expand Down Expand Up @@ -56,18 +53,12 @@ int8_t Esp32_LED_DevicePinMap[TARGET_LED_NUM_PINS] = {
-1, // 6
-1, // 7
-1, // 8
};
};

// We use "ADC1" for 20 logical channels
// Mapped to ESP32 controllers
// ESP32 ADC1 channels 0 - 7
// " ADC1 channel 8 - Internal Temperature sensor (VP)
// " ADC1 channel 9 - Internal Hall Sensor (VN)
// " ADC2 channels 10 - 19
// TODO review ADC channels for ESP32_C3
int8_t Esp32_ADC_DevicePinMap[TARGET_ADC_NUM_PINS] = {
// 0 1 2 3 4 5
};
// We use "ADC1" for 5 logical channels
// Mapped to ESP32_H2 controllers
// ESP32_H2 ADC1 channels 1 - 5
int8_t Esp32_ADC_DevicePinMap[TARGET_ADC_NUM_PINS] = {1, 2, 3, 4, 5};

// I2S
// 1 device I2S1
Expand Down
26 changes: 18 additions & 8 deletions targets/ESP32/_common/ESP32_P4_DeviceMapping.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,7 @@ int8_t Esp32_SPI_DevicePinMap[MAX_SPI_DEVICES][Esp32SpiPin_Max] = {
// others assign as NONE because the default pins can be shared with serial flash and PSRAM
int8_t Esp32_SERIAL_DevicePinMap[UART_NUM_MAX][Esp32SerialPin_Max] = {
// COM 1 - pins 21, 20
{UART_NUM_0_TXD_DIRECT_GPIO_NUM,
UART_NUM_0_RXD_DIRECT_GPIO_NUM,
UART_PIN_NO_CHANGE,
UART_PIN_NO_CHANGE},
{UART_NUM_0_TXD_DIRECT_GPIO_NUM, UART_NUM_0_RXD_DIRECT_GPIO_NUM, UART_PIN_NO_CHANGE, UART_PIN_NO_CHANGE},

#if defined(UART_NUM_2)
// COM 2 - all set to UART_PIN_NO_CHANGE
Expand Down Expand Up @@ -59,15 +56,28 @@ int8_t Esp32_LED_DevicePinMap[8] = {
-1 // 8
};

// ESP32_P4 ADC1 with 8 channels (GPIO 16,17,18,19,20,21,22,23)
// plus 6 channels (GPIO 49,50,51,52,53,54)

// ESP32P4 ADC1 channels 0 - 9
// ADC2 channels 10 - 19
Comment on lines +59 to 63
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🟡 Minor

Correct the ADC channel documentation.

The comments contain inaccuracies:

  1. Lines 62-63 claim "ADC1 channels 0 - 9" and "ADC2 channels 10 - 19" (20 channels total), but the actual mapping has only 14 channels (8 ADC1 + 6 ADC2).
  2. Line 60's indentation makes it unclear whether it's describing ADC1 or ADC2.
  3. Inconsistent naming: "ESP32_P4" (line 59) vs "ESP32P4" (line 62).

Apply this diff to fix the documentation:

-// ESP32_P4 ADC1 with 8 channels (GPIO 16,17,18,19,20,21,22,23)
-//               plus 6 channels (GPIO 49,50,51,52,53,54)
-
-// ESP32P4 ADC1 channels 0 - 9
-//         ADC2 channels 10 - 19
+// ESP32_P4 ADC1 with 8 channels (GPIO 16,17,18,19,20,21,22,23)
+//          ADC2 with 6 channels (GPIO 49,50,51,52,53,54)
+
+// ESP32_P4 ADC1 channels 0 - 7
+//          ADC2 channels 8 - 13

Note: Adjust the channel numbering if the per-unit indexing (ADC1: 0-7, ADC2: 0-5) is intended instead of sequential indexing.

📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
// ESP32_P4 ADC1 with 8 channels (GPIO 16,17,18,19,20,21,22,23)
// plus 6 channels (GPIO 49,50,51,52,53,54)
// ESP32P4 ADC1 channels 0 - 9
// ADC2 channels 10 - 19
// ESP32_P4 ADC1 with 8 channels (GPIO 16,17,18,19,20,21,22,23)
// ADC2 with 6 channels (GPIO 49,50,51,52,53,54)
// ESP32_P4 ADC1 channels 0 - 7
// ADC2 channels 8 - 13
🤖 Prompt for AI Agents
In targets/ESP32/_common/ESP32_P4_DeviceMapping.cpp around lines 59 to 63,
correct the ADC comment block: unify the chipset name to "ESP32_P4", clarify
which lines describe ADC1 vs ADC2 by fixing indentation and wording, and update
channel counts and numbering to reflect 14 channels total (ADC1: 8 channels on
GPIO16-23, ADC2: 6 channels on GPIO49-54). Also choose and document the indexing
scheme consistently (either per-unit indexing ADC1: 0-7 and ADC2: 0-5, or a
sequential 0-13 mapping) and update the text accordingly so it accurately
describes channel counts, GPIO pins, and numbering.

int8_t Esp32_ADC_DevicePinMap[TARGET_ADC_NUM_PINS] = {
// ADC1
ADC1_CHANNEL_0_GPIO_NUM, ADC1_CHANNEL_1_GPIO_NUM, ADC1_CHANNEL_2_GPIO_NUM, ADC1_CHANNEL_3_GPIO_NUM,
ADC1_CHANNEL_4_GPIO_NUM, ADC1_CHANNEL_5_GPIO_NUM, ADC1_CHANNEL_6_GPIO_NUM, ADC1_CHANNEL_7_GPIO_NUM,
16,
17,
18,
19,
20,
21,
22,
23,
// ADC2
ADC2_CHANNEL_0_GPIO_NUM, ADC2_CHANNEL_1_GPIO_NUM, ADC2_CHANNEL_2_GPIO_NUM, ADC2_CHANNEL_3_GPIO_NUM,
ADC2_CHANNEL_4_GPIO_NUM, ADC2_CHANNEL_5_GPIO_NUM};
49,
50,
51,
52,
53,
54};

// I2S
// 1 device I2S1
Expand Down
7 changes: 6 additions & 1 deletion targets/ESP32/_include/esp32_idf.h
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,13 @@
#include <driver/uart.h>
#include <driver/ledc.h>

#if defined(CONFIG_IDF_TARGET_ESP32)
// Use legacy ADC driver for ESP32 for now as the new one also requires the new I2S driver due to dependency because of
// internal DAC other ESP32 variants don't have DAC so use the new ADC driver
#include <driver/adc.h>
// #include <esp_adc/adc_oneshot.h>
#else
#include <esp_adc/adc_oneshot.h>
#endif

#if defined(CONFIG_IDF_TARGET_ESP32) || defined(CONFIG_IDF_TARGET_ESP32S2)
#include <driver/dac_oneshot.h>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,12 @@
//

#include <sys_dev_adc_native_target.h>
#include <Esp32_DeviceMapping.h>

// The ESP32 still uses the legacy ADC driver for now as dependency between ADC and I2S due to internal DAC etc
#if defined(CONFIG_IDF_TARGET_ESP32)

#if defined(IDF_TARGET_ESP32)
extern "C" uint8_t temprature_sens_read();
#endif

HRESULT Library_sys_dev_adc_native_System_Device_Adc_AdcChannel::NativeReadValue___I4(CLR_RT_StackFrame &stack)
{
Expand All @@ -33,13 +35,14 @@ HRESULT Library_sys_dev_adc_native_System_Device_Adc_AdcChannel::NativeReadValue
switch (channelNumber)
{

#if defined(IDF_TARGET_ESP32)
#if defined(CONFIG_IDF_TARGET_ESP32)
case 8:
reading = temperature_sens_read();
reading = temprature_sens_read();
break;

case 9:
reading = hall_sensor_read();
// Hall sensor no longer available in IDF 5.x
NANOCLR_SET_AND_LEAVE(CLR_E_INVALID_PARAMETER);
break;
#endif

Expand All @@ -53,7 +56,8 @@ HRESULT Library_sys_dev_adc_native_System_Device_Adc_AdcChannel::NativeReadValue
{
// Adjust channel number for ADC2
channelNumber -= CONFIG_SOC_ADC_MAX_CHANNEL_NUM;
esp_err_t result = adc2_get_raw((adc2_channel_t)channelNumber, (adc_bits_width_t)SOC_ADC_RTC_MAX_BITWIDTH, &reading);
esp_err_t result =
adc2_get_raw((adc2_channel_t)channelNumber, (adc_bits_width_t)SOC_ADC_RTC_MAX_BITWIDTH, &reading);

if (result != ESP_OK)
{
Expand Down Expand Up @@ -81,3 +85,79 @@ HRESULT Library_sys_dev_adc_native_System_Device_Adc_AdcChannel::NativeDisposeCh

NANOCLR_NOCLEANUP_NOLABEL();
}

#else // !defined(CONFIG_IDF_TARGET_ESP32)

adc_oneshot_unit_handle_t GetAdcHandle(adc_unit_t unit);

HRESULT Library_sys_dev_adc_native_System_Device_Adc_AdcChannel::NativeReadValue___I4(CLR_RT_StackFrame &stack)
{
NANOCLR_HEADER();

int channelNumber;
adc_unit_t adcNumber;
int reading = 0;

// get a pointer to the managed object instance and check that it's not NULL
CLR_RT_HeapBlock *pThis = stack.This();
FAULT_ON_NULL(pThis);

// Get channel from _channelNumber field
channelNumber = pThis[FIELD___channelNumber].NumericByRef().s4;
if (channelNumber < 0 || channelNumber > TARGET_ADC_NUM_PINS)
{
NANOCLR_SET_AND_LEAVE(CLR_E_INVALID_PARAMETER);
}

// Calculate internal ADC number based on channel number, 0->(CONFIG_SOC_ADC_MAX_CHANNEL_NUM - 1)
adcNumber = channelNumber < CONFIG_SOC_ADC_MAX_CHANNEL_NUM ? ADC_UNIT_1 : ADC_UNIT_2;

#if defined(CONFIG_IDF_TARGET_ESP32)
// Handle internal channels for ESP32 only
if (adcNumber == ADC_UNIT_1 && (channelNumber == 8 || channelNumber == 9))
{
if (channelNumber == 8)
{
// reading = temperature_sens_read();
reading = 0;
}
else
{
// Hall sensor no longer available
NANOCLR_SET_AND_LEAVE(CLR_E_INVALID_PARAMETER);
}
}
else
#endif
{
if (adcNumber == ADC_UNIT_2)
{
// Adjust channel number for ADC2
channelNumber -= CONFIG_SOC_ADC_MAX_CHANNEL_NUM;
}

// Read the value
if (adc_oneshot_read(GetAdcHandle(adcNumber), (adc_channel_t)channelNumber, &reading) != ESP_OK)
{
NANOCLR_SET_AND_LEAVE(CLR_E_PIN_UNAVAILABLE);
}
}

// Return value to the managed application
stack.SetResult_I4(reading);

NANOCLR_NOCLEANUP();
}

HRESULT Library_sys_dev_adc_native_System_Device_Adc_AdcChannel::NativeDisposeChannel___VOID(CLR_RT_StackFrame &stack)
{
(void)stack;

NANOCLR_HEADER();

// left empty on purpose, nothing to do here

NANOCLR_NOCLEANUP_NOLABEL();
}

#endif // !defined(CONFIG_IDF_TARGET_ESP32)
Loading