Skip to content
Merged
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
5 changes: 4 additions & 1 deletion ports/esp32/machine_adc.c
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@

#include "driver/gpio.h"
#include "driver/adc.h"
#include "esp_adc_cal.h"

#include "py/runtime.h"
#include "py/mphal.h"
Expand Down Expand Up @@ -121,11 +122,13 @@ STATIC void madc_print(const mp_print_t *print, mp_obj_t self_in, mp_print_kind_
}

STATIC void madc_atten_helper(const madc_obj_t *self, mp_int_t atten) {
esp_err_t err;
esp_err_t err = ESP_OK;
if (self->block->unit_id == ADC_UNIT_1) {
err = adc1_config_channel_atten(self->channel_id, atten);
} else {
#if (SOC_ADC_PERIPH_NUM >= 2)
err = adc2_config_channel_atten(self->channel_id, atten);
#endif
}
if (err != ESP_OK) {
mp_raise_ValueError(MP_ERROR_TEXT("invalid atten"));
Expand Down
10 changes: 9 additions & 1 deletion ports/esp32/machine_adcblock.c
Original file line number Diff line number Diff line change
Expand Up @@ -85,11 +85,13 @@ void madcblock_bits_helper(madcblock_obj_t *self, mp_int_t bits) {
if (self->unit_id == ADC_UNIT_1) {
adc1_config_width(self->width);
}
#if HAS_ADC_CAL
for (adc_atten_t atten = ADC_ATTEN_DB_0; atten < ADC_ATTEN_MAX; atten++) {
if (self->characteristics[atten] != NULL) {
esp_adc_cal_characterize(self->unit_id, atten, self->width, DEFAULT_VREF, self->characteristics[atten]);
}
}
#endif
}

STATIC void madcblock_init_helper(madcblock_obj_t *self, size_t n_pos_args, const mp_obj_t *pos_args, mp_map_t *kw_args) {
Expand Down Expand Up @@ -167,16 +169,19 @@ STATIC mp_obj_t madcblock_connect(size_t n_pos_args, const mp_obj_t *pos_args, m
STATIC MP_DEFINE_CONST_FUN_OBJ_KW(madcblock_connect_obj, 2, madcblock_connect);

mp_int_t madcblock_read_helper(madcblock_obj_t *self, adc_channel_t channel_id) {
int raw;
int raw = 0;
if (self->unit_id == ADC_UNIT_1) {
raw = adc1_get_raw(channel_id);
} else {
#if (SOC_ADC_PERIPH_NUM >= 2)
check_esp_err(adc2_get_raw(channel_id, self->width, &raw));
#endif
}
return raw;
}

mp_int_t madcblock_read_uv_helper(madcblock_obj_t *self, adc_channel_t channel_id, adc_atten_t atten) {
#ifdef HAS_ADC_CAL
int raw = madcblock_read_helper(self, channel_id);
esp_adc_cal_characteristics_t *adc_chars = self->characteristics[atten];
if (adc_chars == NULL) {
Expand All @@ -186,6 +191,9 @@ mp_int_t madcblock_read_uv_helper(madcblock_obj_t *self, adc_channel_t channel_i
}
mp_int_t uv = esp_adc_cal_raw_to_voltage(raw, adc_chars) * 1000;
return uv;
#else
return 0;
#endif
}

STATIC const mp_rom_map_elem_t madcblock_locals_dict_table[] = {
Expand Down
11 changes: 11 additions & 0 deletions ports/esp32/machine_adcblock.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,13 @@
#define MICROPY_INCLUDED_MACHINE_ADCBLOCK_H

#include "esp_adc_cal.h"
#include "esp_adc/adc_cali.h"
#include "esp_adc/adc_cali_scheme.h"

#if CONFIG_IDF_TARGET_ESP32 || CONFIG_IDF_TARGET_ESP32S2 || CONFIG_IDF_TARGET_ESP32C3 || CONFIG_IDF_TARGET_ESP32S3
// See #if above in "esp_adc_cal_types_legacy.h"
#define HAS_ADC_CAL
#endif

#define ADC_ATTEN_MAX SOC_ADC_ATTEN_NUM

Expand All @@ -10,7 +17,11 @@ typedef struct _madcblock_obj_t {
adc_unit_t unit_id;
mp_int_t bits;
adc_bits_width_t width;
#ifdef HAS_ADC_CAL
esp_adc_cal_characteristics_t *characteristics[ADC_ATTEN_MAX];
#else
int *characteristics[1];
#endif
} madcblock_obj_t;

extern madcblock_obj_t madcblock_obj[];
Expand Down
2 changes: 1 addition & 1 deletion ports/esp32/machine_timer.c
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ STATIC void machine_timer_enable(machine_timer_obj_t *self) {
// Initialise the timer.
timer_hal_init(&self->hal_context, self->group, self->index);
timer_ll_enable_counter(self->hal_context.dev, self->index, false);
timer_ll_set_clock_source(self->hal_context.dev, self->index, GPTIMER_CLK_SRC_APB);
timer_ll_set_clock_source(self->hal_context.dev, self->index, GPTIMER_CLK_SRC_DEFAULT);
timer_ll_set_clock_prescale(self->hal_context.dev, self->index, TIMER_DIVIDER);
timer_hal_set_counter_value(&self->hal_context, 0);
timer_ll_set_count_direction(self->hal_context.dev, self->index, GPTIMER_COUNT_UP);
Expand Down
4 changes: 4 additions & 0 deletions ports/esp32/modesp32.c
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,7 @@ STATIC mp_obj_t esp32_wake_on_ulp(const mp_obj_t wake) {
}
STATIC MP_DEFINE_CONST_FUN_OBJ_1(esp32_wake_on_ulp_obj, esp32_wake_on_ulp);

#if !SOC_GPIO_SUPPORT_HOLD_SINGLE_IO_IN_DSLP
STATIC mp_obj_t esp32_gpio_deep_sleep_hold(const mp_obj_t enable) {
if (mp_obj_is_true(enable)) {
gpio_deep_sleep_hold_en();
Expand All @@ -147,6 +148,7 @@ STATIC mp_obj_t esp32_gpio_deep_sleep_hold(const mp_obj_t enable) {
return mp_const_none;
}
STATIC MP_DEFINE_CONST_FUN_OBJ_1(esp32_gpio_deep_sleep_hold_obj, esp32_gpio_deep_sleep_hold);
#endif

#if CONFIG_IDF_TARGET_ESP32

Expand Down Expand Up @@ -199,7 +201,9 @@ STATIC const mp_rom_map_elem_t esp32_module_globals_table[] = {
{ MP_ROM_QSTR(MP_QSTR_wake_on_ext0), MP_ROM_PTR(&esp32_wake_on_ext0_obj) },
{ MP_ROM_QSTR(MP_QSTR_wake_on_ext1), MP_ROM_PTR(&esp32_wake_on_ext1_obj) },
{ MP_ROM_QSTR(MP_QSTR_wake_on_ulp), MP_ROM_PTR(&esp32_wake_on_ulp_obj) },
#if !SOC_GPIO_SUPPORT_HOLD_SINGLE_IO_IN_DSLP
{ MP_ROM_QSTR(MP_QSTR_gpio_deep_sleep_hold), MP_ROM_PTR(&esp32_gpio_deep_sleep_hold_obj) },
#endif
#if CONFIG_IDF_TARGET_ESP32
{ MP_ROM_QSTR(MP_QSTR_raw_temperature), MP_ROM_PTR(&esp32_raw_temperature_obj) },
#endif
Expand Down
6 changes: 6 additions & 0 deletions ports/esp32/modnetwork_globals.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,12 @@
{ MP_ROM_QSTR(MP_QSTR_AUTH_WPA2_WPA3_PSK), MP_ROM_INT(WIFI_AUTH_WPA2_WPA3_PSK) },
{ MP_ROM_QSTR(MP_QSTR_AUTH_WAPI_PSK), MP_ROM_INT(WIFI_AUTH_WAPI_PSK) },
{ MP_ROM_QSTR(MP_QSTR_AUTH_OWE), MP_ROM_INT(WIFI_AUTH_OWE) },
#if ESP_IDF_VERSION > ESP_IDF_VERSION_VAL(5, 1, 1)
{ MP_ROM_QSTR(MP_QSTR_AUTH_WPA3_ENT_192), MP_ROM_INT(WIFI_AUTH_WPA3_ENT_192) },
#endif
#if ESP_IDF_VERSION >= ESP_IDF_VERSION_VAL(5, 2, 0)
{ MP_ROM_QSTR(MP_QSTR_AUTH_WPA3_EXT_PSK), MP_ROM_INT(WIFI_AUTH_WPA3_EXT_PSK) },
#endif
{ MP_ROM_QSTR(MP_QSTR_AUTH_MAX), MP_ROM_INT(WIFI_AUTH_MAX) },
#endif

Expand Down
2 changes: 1 addition & 1 deletion ports/esp32/network_common.c
Original file line number Diff line number Diff line change
Expand Up @@ -158,4 +158,4 @@ STATIC mp_obj_t esp_phy_mode(size_t n_args, const mp_obj_t *args) {
}
MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(esp_network_phy_mode_obj, 0, 1, esp_phy_mode);

_Static_assert(WIFI_AUTH_MAX == 10, "Synchronize WIFI_AUTH_XXX constants with the ESP-IDF. Look at esp-idf/components/esp_wifi/include/esp_wifi_types.h");
_Static_assert(WIFI_AUTH_MAX == 11, "Synchronize WIFI_AUTH_XXX constants with the ESP-IDF. Look at esp-idf/components/esp_wifi/include/esp_wifi_types.h");
1 change: 1 addition & 0 deletions ports/esp32/uart.c
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@

#include <stdio.h>

#include "soc/uart_periph.h"
#include "hal/uart_hal.h"

#include "py/runtime.h"
Expand Down