summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAngus Gratton <angus@redyak.com.au>2025-11-19 17:59:25 +1100
committerDamien George <damien@micropython.org>2025-11-25 11:55:00 +1100
commitde2ace72e943c8b38d77b7d68e8fe73e48459f76 (patch)
tree4c2c9840734d560b0f51d3edadf9c71ed5575f90
parentbe8d5fc27b30ade25bdd272368fd8b91e4d4b338 (diff)
esp32/modespnow: Fix espnow rate setting.
Only set the rate on interfaces that are active. It seems ESP-IDF 5.4.x or so added checks that the interface is enabled, whereas previous versions silently did nothing. This work was funded through GitHub Sponsors. Signed-off-by: Angus Gratton <angus@redyak.com.au>
-rw-r--r--ports/esp32/modespnow.c21
1 files changed, 17 insertions, 4 deletions
diff --git a/ports/esp32/modespnow.c b/ports/esp32/modespnow.c
index ab50032ff..5d048f6b2 100644
--- a/ports/esp32/modespnow.c
+++ b/ports/esp32/modespnow.c
@@ -187,6 +187,16 @@ static void send_cb(const esp_now_send_info_t *tx_info, esp_now_send_status_t st
static void recv_cb(const esp_now_recv_info_t *recv_info, const uint8_t *msg, int msg_len);
+// Return the current wifi mode, or raise ValueError
+static wifi_mode_t get_wifi_mode(void) {
+ wifi_mode_t mode;
+ if (esp_wifi_get_mode(&mode) != ESP_OK || mode == WIFI_MODE_NULL) {
+ // network.WLAN STA or AP must be set active(True) before ESP-NOW can be used
+ mp_raise_OSError(MP_ENOENT);
+ }
+ return mode;
+}
+
// ESPNow.init(): Initialise the data buffers and ESP-NOW functions.
// Initialise the Espressif ESPNOW software stack, register callbacks and
// allocate the recv data buffers.
@@ -197,7 +207,6 @@ static mp_obj_t espnow_init(mp_obj_t _) {
self->recv_buffer = m_new_obj(ringbuf_t);
ringbuf_alloc(self->recv_buffer, self->recv_buffer_size);
- esp_initialise_wifi(); // Call the wifi init code in network_wlan.c
check_esp_err(esp_now_init());
check_esp_err(esp_now_register_recv_cb(recv_cb));
check_esp_err(esp_now_register_send_cb(send_cb));
@@ -260,9 +269,13 @@ static mp_obj_t espnow_config(size_t n_args, const mp_obj_t *pos_args, mp_map_t
self->recv_timeout_ms = args[ARG_timeout_ms].u_int;
}
if (args[ARG_rate].u_int >= 0) {
- esp_initialise_wifi(); // Call the wifi init code in network_wlan.c
- check_esp_err(esp_wifi_config_espnow_rate(ESP_IF_WIFI_STA, args[ARG_rate].u_int));
- check_esp_err(esp_wifi_config_espnow_rate(ESP_IF_WIFI_AP, args[ARG_rate].u_int));
+ wifi_mode_t mode = get_wifi_mode();
+ if (mode == WIFI_MODE_STA || mode == WIFI_MODE_APSTA) {
+ check_esp_err(esp_wifi_config_espnow_rate(ESP_IF_WIFI_STA, args[ARG_rate].u_int));
+ }
+ if (mode == WIFI_MODE_AP || mode == WIFI_MODE_APSTA) {
+ check_esp_err(esp_wifi_config_espnow_rate(ESP_IF_WIFI_AP, args[ARG_rate].u_int));
+ }
}
if (args[ARG_get].u_obj == MP_OBJ_NULL) {
return mp_const_none;