Skip to content

Commit

Permalink
Merge remote-tracking branch 'upstream/master' into check_esp_err_FUN…
Browse files Browse the repository at this point in the history
…C_LINE_FILE
  • Loading branch information
IhorNehrutsa committed Jul 18, 2023
2 parents c54f070 + 7d66ae6 commit 6b677a3
Show file tree
Hide file tree
Showing 34 changed files with 387 additions and 134 deletions.
6 changes: 6 additions & 0 deletions docs/esp32/quickref.rst
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,12 @@ These are working configurations for LAN interfaces of popular boards::
lan = network.LAN(mdc=machine.Pin(23), mdio=machine.Pin(18),
phy_type=network.PHY_LAN8720, phy_addr=1, power=None)

# Wireless-Tag's WT32-ETH01 v1.4

lan = network.LAN(mdc=machine.Pin(23), mdio=machine.Pin(18),
phy_type=network.PHY_LAN8720, phy_addr=1,
power=machine.Pin(16))

# Espressif ESP32-Ethernet-Kit_A_V1.2

lan = network.LAN(id=0, mdc=Pin(23), mdio=Pin(18), power=Pin(5),
Expand Down
3 changes: 3 additions & 0 deletions ports/esp32/boards/GENERIC_OTA/sdkconfig.board
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
CONFIG_BOOTLOADER_APP_ROLLBACK_ENABLE=y
CONFIG_PARTITION_TABLE_CUSTOM=y
CONFIG_PARTITION_TABLE_CUSTOM_FILENAME="partitions-ota.csv"

# Reduce firmware size to fit in the OTA partition.
CONFIG_COMPILER_OPTIMIZATION_CHECKS_SILENT=y
1 change: 0 additions & 1 deletion ports/esp32/boards/sdkconfig.base
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,5 @@ CONFIG_UART_ISR_IN_IRAM=y

# IDF 5 deprecated
CONFIG_ADC_SUPPRESS_DEPRECATE_WARN=y
CONFIG_GPTIMER_SUPPRESS_DEPRECATE_WARN=y
CONFIG_RMT_SUPPRESS_DEPRECATE_WARN=y
CONFIG_I2S_SUPPRESS_DEPRECATE_WARN=y
97 changes: 52 additions & 45 deletions ports/esp32/machine_timer.c
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,10 @@
#include "modmachine.h"
#include "mphalport.h"

#include "driver/timer.h"
#include "hal/timer_hal.h"
#include "hal/timer_ll.h"
#include "soc/timer_periph.h"

#define TIMER_INTR_SEL TIMER_INTR_LEVEL
#define TIMER_DIVIDER 8

// TIMER_BASE_CLK is normally 80MHz. TIMER_DIVIDER ought to divide this exactly
Expand All @@ -48,6 +48,8 @@

typedef struct _machine_timer_obj_t {
mp_obj_base_t base;

timer_hal_context_t hal_context;
mp_uint_t group;
mp_uint_t index;

Expand Down Expand Up @@ -80,15 +82,9 @@ void machine_timer_deinit_all(void) {

STATIC void machine_timer_print(const mp_print_t *print, mp_obj_t self_in, mp_print_kind_t kind) {
machine_timer_obj_t *self = self_in;

timer_config_t config;
mp_printf(print, "Timer(%p; ", self);

timer_get_config(self->group, self->index, &config);

mp_printf(print, "alarm_en=%d, ", config.alarm_en);
mp_printf(print, "auto_reload=%d, ", config.auto_reload);
mp_printf(print, "counter_en=%d)", config.counter_en);
qstr mode = self->repeat ? MP_QSTR_PERIODIC : MP_QSTR_ONE_SHOT;
uint64_t period = self->period / (TIMER_SCALE / 1000); // convert to ms
mp_printf(print, "Timer(%u, mode=%q, period=%lu)", (self->group << 1) | self->index, mode, period);
}

STATIC mp_obj_t machine_timer_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_kw, const mp_obj_t *args) {
Expand Down Expand Up @@ -126,8 +122,14 @@ STATIC mp_obj_t machine_timer_make_new(const mp_obj_type_t *type, size_t n_args,
}

STATIC void machine_timer_disable(machine_timer_obj_t *self) {
if (self->hal_context.dev != NULL) {
// Disable the counter and alarm.
timer_ll_enable_counter(self->hal_context.dev, self->index, false);
timer_ll_enable_alarm(self->hal_context.dev, self->index, false);
}

if (self->handle) {
timer_pause(self->group, self->index);
// Free the interrupt handler.
esp_intr_free(self->handle);
self->handle = NULL;
}
Expand All @@ -138,39 +140,47 @@ STATIC void machine_timer_disable(machine_timer_obj_t *self) {

STATIC void machine_timer_isr(void *self_in) {
machine_timer_obj_t *self = self_in;
timg_dev_t *device = self->group ? &(TIMERG1) : &(TIMERG0);

#if CONFIG_IDF_TARGET_ESP32S3
device->hw_timer[self->index].update.tn_update = 1;
#else
device->hw_timer[self->index].update.tx_update = 1;
#endif
uint32_t intr_status = timer_ll_get_intr_status(self->hal_context.dev);

timer_ll_clear_intr_status(device, self->index);
timer_ll_set_alarm_value(device, self->index, self->repeat);

mp_sched_schedule(self->callback, self);
mp_hal_wake_main_task_from_isr();
if (intr_status & TIMER_LL_EVENT_ALARM(self->index)) {
timer_ll_clear_intr_status(self->hal_context.dev, TIMER_LL_EVENT_ALARM(self->index));
if (self->repeat) {
timer_ll_enable_alarm(self->hal_context.dev, self->index, true);
}
mp_sched_schedule(self->callback, self);
mp_hal_wake_main_task_from_isr();
}
}

STATIC void machine_timer_enable(machine_timer_obj_t *self) {
timer_config_t config;
config.alarm_en = TIMER_ALARM_EN;
config.auto_reload = self->repeat;
config.counter_dir = TIMER_COUNT_UP;
config.divider = TIMER_DIVIDER;
config.intr_type = TIMER_INTR_LEVEL;
config.counter_en = TIMER_PAUSE;
#if SOC_TIMER_GROUP_SUPPORT_XTAL
config.clk_src = TIMER_SRC_CLK_APB;
#endif

check_esp_err(timer_init(self->group, self->index, &config));
check_esp_err(timer_set_counter_value(self->group, self->index, 0x00000000));
check_esp_err(timer_set_alarm_value(self->group, self->index, self->period));
check_esp_err(timer_enable_intr(self->group, self->index));
check_esp_err(timer_isr_register(self->group, self->index, machine_timer_isr, (void *)self, TIMER_FLAGS, &self->handle));
check_esp_err(timer_start(self->group, self->index));
// 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_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);

// Allocate and enable the alarm interrupt.
timer_ll_enable_intr(self->hal_context.dev, TIMER_LL_EVENT_ALARM(self->index), false);
timer_ll_clear_intr_status(self->hal_context.dev, TIMER_LL_EVENT_ALARM(self->index));
ESP_ERROR_CHECK(
esp_intr_alloc(timer_group_periph_signals.groups[self->group].timer_irq_id[self->index],
TIMER_FLAGS, machine_timer_isr, self, &self->handle)
);
timer_ll_enable_intr(self->hal_context.dev, TIMER_LL_EVENT_ALARM(self->index), true);

// Enable the alarm to trigger at the given period.
timer_ll_set_alarm_value(self->hal_context.dev, self->index, self->period);
timer_ll_enable_alarm(self->hal_context.dev, self->index, true);

// Set the counter to reload at 0 if it's in repeat mode.
timer_ll_set_reload_value(self->hal_context.dev, self->index, 0);
timer_ll_enable_auto_reload(self->hal_context.dev, self->index, self->repeat);

// Enable the counter.
timer_ll_enable_counter(self->hal_context.dev, self->index, true);
}

STATIC mp_obj_t machine_timer_init_helper(machine_timer_obj_t *self, mp_uint_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) {
Expand Down Expand Up @@ -234,11 +244,8 @@ STATIC MP_DEFINE_CONST_FUN_OBJ_KW(machine_timer_init_obj, 1, machine_timer_init)

STATIC mp_obj_t machine_timer_value(mp_obj_t self_in) {
machine_timer_obj_t *self = self_in;
double result;

timer_get_counter_time_sec(self->group, self->index, &result);

return MP_OBJ_NEW_SMALL_INT((mp_uint_t)(result * 1000)); // value in ms
uint64_t result = timer_ll_get_counter_value(self->hal_context.dev, self->index);
return MP_OBJ_NEW_SMALL_INT((mp_uint_t)(result / (TIMER_SCALE / 1000))); // value in ms
}
STATIC MP_DEFINE_CONST_FUN_OBJ_1(machine_timer_value_obj, machine_timer_value);

Expand Down
5 changes: 5 additions & 0 deletions ports/esp32/main_esp32/idf_component.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
## IDF Component Manager Manifest File
dependencies:
espressif/mdns: "~1.1.0"
idf:
version: ">=5.0.2"
5 changes: 5 additions & 0 deletions ports/esp32/main_esp32c3/idf_component.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
## IDF Component Manager Manifest File
dependencies:
espressif/mdns: "~1.1.0"
idf:
version: ">=5.0.2"
3 changes: 2 additions & 1 deletion ports/esp32/main_esp32s2/idf_component.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
## IDF Component Manager Manifest File
dependencies:
espressif/mdns: "~1.1.0"
espressif/esp_tinyusb: "~1.0.0"
idf:
version: ">=5.0.2"
espressif/esp_tinyusb: "~1.0.0"
3 changes: 2 additions & 1 deletion ports/esp32/main_esp32s3/idf_component.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
## IDF Component Manager Manifest File
dependencies:
espressif/mdns: "~1.1.0"
espressif/esp_tinyusb: "~1.0.0"
idf:
version: ">=5.0.2"
espressif/esp_tinyusb: "~1.0.0"
1 change: 1 addition & 0 deletions ports/esp32/modnetwork.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ typedef struct _base_if_obj_t {
mp_obj_base_t base;
esp_interface_t if_id;
esp_netif_t *netif;
volatile bool active;
} base_if_obj_t;

extern const mp_obj_type_t esp_network_wlan_type;
Expand Down
4 changes: 4 additions & 0 deletions ports/esp32/modsocket.c
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,10 @@
#define MDNS_QUERY_TIMEOUT_MS (5000)
#define MDNS_LOCAL_SUFFIX ".local"

#ifndef NO_QSTR
#include "mdns.h"
#endif

enum {
SOCKET_STATE_NEW,
SOCKET_STATE_CONNECTED,
Expand Down
4 changes: 2 additions & 2 deletions ports/esp32/mpconfigport.h
Original file line number Diff line number Diff line change
Expand Up @@ -221,11 +221,11 @@ typedef long mp_off_t;
#endif

#ifndef MICROPY_HW_ENABLE_MDNS_QUERIES
#define MICROPY_HW_ENABLE_MDNS_QUERIES (0)
#define MICROPY_HW_ENABLE_MDNS_QUERIES (1)
#endif

#ifndef MICROPY_HW_ENABLE_MDNS_RESPONDER
#define MICROPY_HW_ENABLE_MDNS_RESPONDER (0)
#define MICROPY_HW_ENABLE_MDNS_RESPONDER (1)
#endif

#ifndef MICROPY_BOARD_STARTUP
Expand Down
15 changes: 7 additions & 8 deletions ports/esp32/network_lan.c
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,6 @@
typedef struct _lan_if_obj_t {
base_if_obj_t base;
bool initialized;
bool active;
int8_t mdc_pin;
int8_t mdio_pin;
int8_t phy_power_pin;
Expand Down Expand Up @@ -295,7 +294,7 @@ STATIC mp_obj_t get_lan(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_ar

esp_err_t esp_err = esp_eth_driver_install(&config, &self->eth_handle);
if (esp_err == ESP_OK) {
self->active = false;
self->base.active = false;
self->initialized = true;
} else {
if (esp_err == ESP_ERR_INVALID_ARG) {
Expand All @@ -322,19 +321,19 @@ STATIC mp_obj_t lan_active(size_t n_args, const mp_obj_t *args) {

if (n_args > 1) {
if (mp_obj_is_true(args[1])) {
self->active = (esp_eth_start(self->eth_handle) == ESP_OK);
if (!self->active) {
self->base.active = (esp_eth_start(self->eth_handle) == ESP_OK);
if (!self->base.active) {
mp_raise_msg(&mp_type_OSError, MP_ERROR_TEXT("ethernet enable failed"));
}
} else {
self->active = !(esp_eth_stop(self->eth_handle) == ESP_OK);
if (self->active) {
self->base.active = !(esp_eth_stop(self->eth_handle) == ESP_OK);
if (self->base.active) {
mp_raise_msg(&mp_type_OSError, MP_ERROR_TEXT("ethernet disable failed"));
}
}
}

return mp_obj_new_bool(self->active);
return mp_obj_new_bool(self->base.active);
}
STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(lan_active_obj, 1, 2, lan_active);

Expand All @@ -345,7 +344,7 @@ STATIC MP_DEFINE_CONST_FUN_OBJ_1(lan_status_obj, lan_status);

STATIC mp_obj_t lan_isconnected(mp_obj_t self_in) {
lan_if_obj_t *self = MP_OBJ_TO_PTR(self_in);
return self->active ? mp_obj_new_bool(self->phy->get_link(self->phy) == ETH_LINK_UP) : mp_const_false;
return self->base.active ? mp_obj_new_bool(self->phy->get_link(self->phy) == ETH_LINK_UP) : mp_const_false;
}
STATIC MP_DEFINE_CONST_FUN_OBJ_1(lan_isconnected_obj, lan_isconnected);

Expand Down
33 changes: 26 additions & 7 deletions ports/esp32/network_wlan.c
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,10 @@
#include "esp_wifi.h"
#include "esp_log.h"

#ifndef NO_QSTR
#include "mdns.h"
#endif

#if MICROPY_PY_NETWORK_WLAN

#if (WIFI_MODE_STA & WIFI_MODE_AP != WIFI_MODE_NULL || WIFI_MODE_STA | WIFI_MODE_AP != WIFI_MODE_APSTA)
Expand Down Expand Up @@ -72,17 +76,22 @@ static bool mdns_initialised = false;
#endif

static uint8_t conf_wifi_sta_reconnects = 0;
static volatile uint8_t wifi_sta_reconnects;
static uint8_t wifi_sta_reconnects;

// This function is called by the system-event task and so runs in a different
// thread to the main MicroPython task. It must not raise any Python exceptions.
static void network_wlan_wifi_event_handler(void *event_handler_arg, esp_event_base_t event_base, int32_t event_id, void *event_data) {
switch (event_id) {
case WIFI_EVENT_STA_START:
ESP_LOGI("wifi", "STA_START");
wlan_sta_obj.active = true;
wifi_sta_reconnects = 0;
break;

case WIFI_EVENT_STA_STOP:
wlan_sta_obj.active = false;
break;

case WIFI_EVENT_STA_CONNECTED:
ESP_LOGI("network", "CONNECTED");
break;
Expand Down Expand Up @@ -136,6 +145,15 @@ static void network_wlan_wifi_event_handler(void *event_handler_arg, esp_event_b
}
break;
}

case WIFI_EVENT_AP_START:
wlan_ap_obj.active = true;
break;

case WIFI_EVENT_AP_STOP:
wlan_ap_obj.active = false;
break;

default:
break;
}
Expand Down Expand Up @@ -180,10 +198,12 @@ void esp_initialise_wifi(void) {
wlan_sta_obj.base.type = &esp_network_wlan_type;
wlan_sta_obj.if_id = ESP_IF_WIFI_STA;
wlan_sta_obj.netif = esp_netif_create_default_wifi_sta();
wlan_sta_obj.active = false;

wlan_ap_obj.base.type = &esp_network_wlan_type;
wlan_ap_obj.if_id = ESP_IF_WIFI_AP;
wlan_ap_obj.netif = esp_netif_create_default_wifi_ap();
wlan_ap_obj.active = false;

wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT();
ESP_LOGD("modnetwork", "Initializing WiFi");
Expand Down Expand Up @@ -233,16 +253,15 @@ STATIC mp_obj_t network_wlan_active(size_t n_args, const mp_obj_t *args) {
} else {
esp_exceptions(esp_wifi_set_mode(mode));
if (!wifi_started) {
// WIFI_EVENT_STA_START must be received before esp_wifi_connect() can be called.
// Use the `wifi_sta_reconnects` variable to detect that event.
wifi_sta_reconnects = 1;
esp_exceptions(esp_wifi_start());
wifi_started = true;
while (wifi_sta_reconnects != 0) {
MICROPY_EVENT_POLL_HOOK;
}
}
}

// Wait for the interface to be in the correct state.
while (self->active != active) {
MICROPY_EVENT_POLL_HOOK;
}
}

return (mode & bit) ? mp_const_true : mp_const_false;
Expand Down
1 change: 0 additions & 1 deletion ports/renesas-ra/mpconfigport.h
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,6 @@

#if MICROPY_PY_MACHINE
#define MACHINE_BUILTIN_MODULE_CONSTANTS \
{ MP_ROM_QSTR(MP_QSTR_machine), MP_ROM_PTR(&mp_module_machine) }, \
{ MP_ROM_QSTR(MP_QSTR_machine), MP_ROM_PTR(&mp_module_machine) },
#else
#define MACHINE_BUILTIN_MODULE_CONSTANTS
Expand Down
2 changes: 1 addition & 1 deletion ports/stm32/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -404,7 +404,7 @@ HAL_SRC_C += $(addprefix $(STM32LIB_HAL_BASE)/Src/stm32$(MCU_SERIES)xx_,\
ll_utils.c \
)

ifeq ($(MCU_SERIES),$(filter $(MCU_SERIES),f4 f7 g0 g4 h5 h7 l0 l4 wb))
ifeq ($(MCU_SERIES),$(filter $(MCU_SERIES),f4 f7 g0 g4 h5 h7 l0 l1 l4 wb))
HAL_SRC_C += $(addprefix $(STM32LIB_HAL_BASE)/Src/stm32$(MCU_SERIES)xx_,\
hal_pcd.c \
hal_pcd_ex.c \
Expand Down
Loading

0 comments on commit 6b677a3

Please sign in to comment.