use esp error for espnow

This commit is contained in:
MicroDev 2023-02-03 15:42:44 +05:30
parent cac90a6969
commit a1644f15ea
No known key found for this signature in database
GPG Key ID: 2C0867BE60967730
6 changed files with 46 additions and 138 deletions

View File

@ -222,7 +222,7 @@ msgstr ""
msgid "%q=%q" msgid "%q=%q"
msgstr "" msgstr ""
#: ports/espressif/common-hal/espidf/__init__.c ports/espressif/esp_error.c #: ports/espressif/common-hal/espidf/__init__.c
#, c-format #, c-format
msgid "%s error 0x%x" msgid "%s error 0x%x"
msgstr "" msgstr ""
@ -680,7 +680,7 @@ msgstr ""
msgid "CIRCUITPY drive could not be found or created." msgid "CIRCUITPY drive could not be found or created."
msgstr "" msgstr ""
#: ports/espressif/common-hal/espidf/__init__.c ports/espressif/esp_error.c #: ports/espressif/common-hal/espidf/__init__.c
msgid "CRC or checksum was invalid" msgid "CRC or checksum was invalid"
msgstr "" msgstr ""
@ -1067,7 +1067,7 @@ msgstr ""
msgid "GNSS init" msgid "GNSS init"
msgstr "" msgstr ""
#: ports/espressif/common-hal/espidf/__init__.c ports/espressif/esp_error.c #: ports/espressif/common-hal/espidf/__init__.c
msgid "Generic Failure" msgid "Generic Failure"
msgstr "" msgstr ""
@ -1235,8 +1235,7 @@ msgstr ""
msgid "Invalid MAC address" msgid "Invalid MAC address"
msgstr "" msgstr ""
#: ports/espressif/common-hal/espidf/__init__.c ports/espressif/esp_error.c #: ports/espressif/common-hal/espidf/__init__.c py/moduerrno.c
#: py/moduerrno.c
msgid "Invalid argument" msgid "Invalid argument"
msgstr "" msgstr ""
@ -1266,7 +1265,7 @@ msgstr ""
msgid "Invalid multicast MAC address" msgid "Invalid multicast MAC address"
msgstr "" msgstr ""
#: ports/espressif/common-hal/espidf/__init__.c ports/espressif/esp_error.c #: ports/espressif/common-hal/espidf/__init__.c
msgid "Invalid size" msgid "Invalid size"
msgstr "" msgstr ""
@ -1275,7 +1274,7 @@ msgstr ""
msgid "Invalid socket for TLS" msgid "Invalid socket for TLS"
msgstr "" msgstr ""
#: ports/espressif/common-hal/espidf/__init__.c ports/espressif/esp_error.c #: ports/espressif/common-hal/espidf/__init__.c
msgid "Invalid state" msgid "Invalid state"
msgstr "" msgstr ""
@ -1307,7 +1306,7 @@ msgstr ""
msgid "Layer must be a Group or TileGrid subclass" msgid "Layer must be a Group or TileGrid subclass"
msgstr "" msgstr ""
#: ports/espressif/common-hal/espidf/__init__.c ports/espressif/esp_error.c #: ports/espressif/common-hal/espidf/__init__.c
msgid "MAC address was invalid" msgid "MAC address was invalid"
msgstr "" msgstr ""
@ -1667,11 +1666,11 @@ msgstr ""
msgid "Operation not permitted" msgid "Operation not permitted"
msgstr "" msgstr ""
#: ports/espressif/common-hal/espidf/__init__.c ports/espressif/esp_error.c #: ports/espressif/common-hal/espidf/__init__.c
msgid "Operation or feature not supported" msgid "Operation or feature not supported"
msgstr "" msgstr ""
#: ports/espressif/common-hal/espidf/__init__.c ports/espressif/esp_error.c #: ports/espressif/common-hal/espidf/__init__.c
msgid "Operation timed out" msgid "Operation timed out"
msgstr "" msgstr ""
@ -1679,7 +1678,7 @@ msgstr ""
msgid "Out of MDNS service slots" msgid "Out of MDNS service slots"
msgstr "" msgstr ""
#: ports/espressif/common-hal/espidf/__init__.c ports/espressif/esp_error.c #: ports/espressif/common-hal/espidf/__init__.c
msgid "Out of memory" msgid "Out of memory"
msgstr "" msgstr ""
@ -1855,7 +1854,7 @@ msgstr ""
msgid "Read-only filesystem" msgid "Read-only filesystem"
msgstr "" msgstr ""
#: ports/espressif/common-hal/espidf/__init__.c ports/espressif/esp_error.c #: ports/espressif/common-hal/espidf/__init__.c
msgid "Received response was invalid" msgid "Received response was invalid"
msgstr "" msgstr ""
@ -1875,7 +1874,7 @@ msgstr ""
msgid "Requested AES mode is unsupported" msgid "Requested AES mode is unsupported"
msgstr "" msgstr ""
#: ports/espressif/common-hal/espidf/__init__.c ports/espressif/esp_error.c #: ports/espressif/common-hal/espidf/__init__.c
msgid "Requested resource not found" msgid "Requested resource not found"
msgstr "" msgstr ""
@ -2341,7 +2340,7 @@ msgstr ""
msgid "Value length > max_length" msgid "Value length > max_length"
msgstr "" msgstr ""
#: ports/espressif/common-hal/espidf/__init__.c ports/espressif/esp_error.c #: ports/espressif/common-hal/espidf/__init__.c
msgid "Version was invalid" msgid "Version was invalid"
msgstr "" msgstr ""
@ -2431,11 +2430,6 @@ msgstr ""
msgid "addresses is empty" msgid "addresses is empty"
msgstr "" msgstr ""
#: ports/espressif/bindings/espnow/Peers.c
#: ports/espressif/common-hal/espnow/ESPNow.c
msgid "an error occured"
msgstr ""
#: py/compile.c #: py/compile.c
msgid "annotation must be an identifier" msgid "annotation must be an identifier"
msgstr "" msgstr ""

View File

@ -218,7 +218,6 @@ endif
SRC_C += \ SRC_C += \
background.c \ background.c \
mphalport.c \ mphalport.c \
bindings/espidf/__init__.c \
boards/$(BOARD)/board.c \ boards/$(BOARD)/board.c \
boards/$(BOARD)/pins.c \ boards/$(BOARD)/pins.c \
shared/netutils/netutils.c \ shared/netutils/netutils.c \
@ -252,8 +251,6 @@ ifneq ($(CIRCUITPY_BLEIO),0)
SRC_C += common-hal/_bleio/ble_events.c SRC_C += common-hal/_bleio/ble_events.c
endif endif
SRC_C += $(wildcard common-hal/espidf/*.c)
ifneq ($(CIRCUITPY_ESPCAMERA),0) ifneq ($(CIRCUITPY_ESPCAMERA),0)
SRC_CAMERA := \ SRC_CAMERA := \
$(wildcard common-hal/espcamera/*.c) \ $(wildcard common-hal/espcamera/*.c) \
@ -263,6 +260,13 @@ CFLAGS += -isystem esp32-camera/driver/include
CFLAGS += -isystem esp32-camera/conversions/include CFLAGS += -isystem esp32-camera/conversions/include
endif endif
ifneq ($(CIRCUITPY_ESPIDF),0)
SRC_ESPIDF := \
$(wildcard common-hal/espidf/*.c) \
$(wildcard bindings/espidf/*.c)
SRC_C += $(SRC_ESPIDF)
endif
ifneq ($(CIRCUITPY_ESPNOW),0) ifneq ($(CIRCUITPY_ESPNOW),0)
SRC_ESPNOW := \ SRC_ESPNOW := \
$(wildcard common-hal/espnow/*.c) \ $(wildcard common-hal/espnow/*.c) \

View File

@ -28,17 +28,13 @@
#include "py/objlist.h" #include "py/objlist.h"
#include "py/runtime.h" #include "py/runtime.h"
#include "bindings/espidf/__init__.h"
#include "bindings/espnow/Peer.h" #include "bindings/espnow/Peer.h"
#include "bindings/espnow/Peers.h" #include "bindings/espnow/Peers.h"
#include "esp_now.h" #include "esp_now.h"
static void check_esp_err(esp_err_t status) {
if (status != ESP_OK) {
mp_raise_RuntimeError(translate("an error occured"));
}
}
//| class Peers: //| class Peers:
//| """A class that provides peer managment functions. Sequence[Peer].""" //| """A class that provides peer managment functions. Sequence[Peer]."""
//| //|
@ -51,7 +47,7 @@ static void check_esp_err(esp_err_t status) {
//| ... //| ...
STATIC mp_obj_t espnow_peers_append(mp_obj_t self_in, mp_obj_t arg) { STATIC mp_obj_t espnow_peers_append(mp_obj_t self_in, mp_obj_t arg) {
espnow_peer_obj_t *peer = MP_OBJ_TO_PTR(mp_arg_validate_type(arg, &espnow_peer_type, MP_QSTR_Peer)); espnow_peer_obj_t *peer = MP_OBJ_TO_PTR(mp_arg_validate_type(arg, &espnow_peer_type, MP_QSTR_Peer));
check_esp_err(esp_now_add_peer(&peer->peer_info)); CHECK_ESP_RESULT(esp_now_add_peer(&peer->peer_info));
espnow_peers_obj_t *self = MP_OBJ_TO_PTR(self_in); espnow_peers_obj_t *self = MP_OBJ_TO_PTR(self_in);
return mp_obj_list_append(self->list, arg); return mp_obj_list_append(self->list, arg);
} }
@ -66,7 +62,7 @@ STATIC MP_DEFINE_CONST_FUN_OBJ_2(espnow_peers_append_obj, espnow_peers_append);
//| //|
STATIC mp_obj_t espnow_peers_remove(mp_obj_t self_in, mp_obj_t arg) { STATIC mp_obj_t espnow_peers_remove(mp_obj_t self_in, mp_obj_t arg) {
espnow_peer_obj_t *peer = MP_OBJ_TO_PTR(mp_arg_validate_type(arg, &espnow_peer_type, MP_QSTR_Peer)); espnow_peer_obj_t *peer = MP_OBJ_TO_PTR(mp_arg_validate_type(arg, &espnow_peer_type, MP_QSTR_Peer));
check_esp_err(esp_now_del_peer(peer->peer_info.peer_addr)); CHECK_ESP_RESULT(esp_now_del_peer(peer->peer_info.peer_addr));
espnow_peers_obj_t *self = MP_OBJ_TO_PTR(self_in); espnow_peers_obj_t *self = MP_OBJ_TO_PTR(self_in);
return mp_obj_list_remove(self->list, arg); return mp_obj_list_remove(self->list, arg);
} }

View File

@ -29,7 +29,9 @@
#include "supervisor/memory.h" #include "supervisor/memory.h"
#include "py/runtime.h" #include "py/runtime.h"
#include "esp_now.h"
#include "esp_log.h" #include "esp_log.h"
#define TAG "espidf" #define TAG "espidf"
#ifdef CONFIG_SPIRAM #ifdef CONFIG_SPIRAM
@ -180,14 +182,19 @@ void raise_esp_error(esp_err_t err) {
// tests must be in descending order // tests must be in descending order
MP_STATIC_ASSERT(ESP_ERR_FLASH_BASE > ESP_ERR_MESH_BASE); MP_STATIC_ASSERT(ESP_ERR_FLASH_BASE > ESP_ERR_MESH_BASE);
MP_STATIC_ASSERT(ESP_ERR_MESH_BASE > ESP_ERR_WIFI_BASE); MP_STATIC_ASSERT(ESP_ERR_MESH_BASE > ESP_ERR_ESPNOW_BASE);
MP_STATIC_ASSERT(ESP_ERR_ESPNOW_BASE > ESP_ERR_WIFI_BASE);
if (err >= ESP_ERR_FLASH_BASE) { if (err >= ESP_ERR_FLASH_BASE) {
group = "Flash"; group = "Flash";
} else if (err >= ESP_ERR_MESH_BASE) { } else if (err >= ESP_ERR_MESH_BASE) {
group = "Mesh"; group = "Mesh";
} else if (err >= ESP_ERR_ESPNOW_BASE) {
group = "ESP-NOW";
} else if (err >= ESP_ERR_WIFI_BASE) { } else if (err >= ESP_ERR_WIFI_BASE) {
group = "WiFi"; group = "WiFi";
} }
mp_raise_msg_varg(exception_type, translate("%s error 0x%x"), group, err); mp_raise_msg_varg(exception_type, translate("%s error 0x%x"), group, err);
} }

View File

@ -30,7 +30,9 @@
#include "py/mperrno.h" #include "py/mperrno.h"
#include "py/runtime.h" #include "py/runtime.h"
#include "bindings/espidf/__init__.h"
#include "bindings/espnow/ESPNowPacket.h" #include "bindings/espnow/ESPNowPacket.h"
#include "shared-bindings/wifi/__init__.h" #include "shared-bindings/wifi/__init__.h"
#include "common-hal/espnow/ESPNow.h" #include "common-hal/espnow/ESPNow.h"
@ -41,6 +43,8 @@
#define ESPNOW_MAGIC 0x99 #define ESPNOW_MAGIC 0x99
// TODO: deinit wifi?
// The maximum length of an espnow packet (bytes) // The maximum length of an espnow packet (bytes)
#define MAX_PACKET_LEN (sizeof(espnow_packet_t) + ESP_NOW_MAX_DATA_LEN) #define MAX_PACKET_LEN (sizeof(espnow_packet_t) + ESP_NOW_MAX_DATA_LEN)
@ -66,12 +70,6 @@ typedef struct {
uint8_t msg[0]; // Message is up to 250 bytes uint8_t msg[0]; // Message is up to 250 bytes
} __attribute__((packed)) espnow_packet_t; } __attribute__((packed)) espnow_packet_t;
static void check_esp_err(esp_err_t status) {
if (status != ESP_OK) {
mp_raise_RuntimeError(translate("an error occured"));
}
}
// Return a pointer to the ESPNow module singleton // Return a pointer to the ESPNow module singleton
static espnow_obj_t *_get_singleton(void) { static espnow_obj_t *_get_singleton(void) {
return MP_STATE_PORT(espnow_singleton); return MP_STATE_PORT(espnow_singleton);
@ -152,12 +150,12 @@ void common_hal_espnow_init(espnow_obj_t *self) {
common_hal_wifi_radio_set_enabled(&common_hal_wifi_radio_obj, true); common_hal_wifi_radio_set_enabled(&common_hal_wifi_radio_obj, true);
} }
check_esp_err(esp_wifi_config_espnow_rate(ESP_IF_WIFI_STA, self->phy_rate)); CHECK_ESP_RESULT(esp_wifi_config_espnow_rate(ESP_IF_WIFI_STA, self->phy_rate));
check_esp_err(esp_wifi_config_espnow_rate(ESP_IF_WIFI_AP, self->phy_rate)); CHECK_ESP_RESULT(esp_wifi_config_espnow_rate(ESP_IF_WIFI_AP, self->phy_rate));
check_esp_err(esp_now_init()); CHECK_ESP_RESULT(esp_now_init());
check_esp_err(esp_now_register_send_cb(send_cb)); CHECK_ESP_RESULT(esp_now_register_send_cb(send_cb));
check_esp_err(esp_now_register_recv_cb(recv_cb)); CHECK_ESP_RESULT(esp_now_register_recv_cb(recv_cb));
} }
// De-initialize the ESP-NOW software stack, // De-initialize the ESP-NOW software stack,
@ -167,9 +165,9 @@ void common_hal_espnow_deinit(espnow_obj_t *self) {
return; return;
} }
check_esp_err(esp_now_unregister_send_cb()); CHECK_ESP_RESULT(esp_now_unregister_send_cb());
check_esp_err(esp_now_unregister_recv_cb()); CHECK_ESP_RESULT(esp_now_unregister_recv_cb());
check_esp_err(esp_now_deinit()); CHECK_ESP_RESULT(esp_now_deinit());
self->recv_buffer->buf = NULL; self->recv_buffer->buf = NULL;
self->recv_buffer = NULL; self->recv_buffer = NULL;
@ -191,7 +189,7 @@ void common_hal_espnow_set_phy_rate(espnow_obj_t *self, mp_int_t value) {
}; };
void common_hal_espnow_set_pmk(espnow_obj_t *self, const uint8_t *key) { void common_hal_espnow_set_pmk(espnow_obj_t *self, const uint8_t *key) {
check_esp_err(esp_now_set_pmk(key)); CHECK_ESP_RESULT(esp_now_set_pmk(key));
} }
// --- Send and Receive ESP-NOW data --- // --- Send and Receive ESP-NOW data ---
@ -206,7 +204,7 @@ mp_obj_t common_hal_espnow_send(espnow_obj_t *self, const uint8_t *mac, const mp
(mp_hal_ticks_ms() - start) <= DEFAULT_SEND_TIMEOUT_MS) { (mp_hal_ticks_ms() - start) <= DEFAULT_SEND_TIMEOUT_MS) {
RUN_BACKGROUND_TASKS; RUN_BACKGROUND_TASKS;
} }
check_esp_err(err); CHECK_ESP_RESULT(err);
// Increment the sent packet count. // Increment the sent packet count.
// If mac == NULL msg will be sent to all peers EXCEPT any broadcast or multicast addresses. // If mac == NULL msg will be sent to all peers EXCEPT any broadcast or multicast addresses.

View File

@ -1,91 +0,0 @@
/*
* This file is part of the MicroPython project, http://micropython.org/
*
* The MIT License (MIT)
*
* Copyright (c) 2020 Jeff Epler for Adafruit Industries
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
#include "esp_error.h"
#include "py/runtime.h"
#include "bindings/espidf/__init__.h"
void raise_esp_error(esp_err_t err) {
const compressed_string_t *msg = NULL;
const mp_obj_type_t *exception_type = &mp_type_espidf_IDFError;
switch (err) {
case ESP_FAIL:
msg = translate("Generic Failure");
break;
case ESP_ERR_NO_MEM:
exception_type = &mp_type_espidf_MemoryError;
msg = translate("Out of memory");
break;
case ESP_ERR_INVALID_ARG:
msg = translate("Invalid argument");
break;
case ESP_ERR_INVALID_STATE:
msg = translate("Invalid state");
break;
case ESP_ERR_INVALID_SIZE:
msg = translate("Invalid size");
break;
case ESP_ERR_NOT_FOUND:
msg = translate("Requested resource not found");
break;
case ESP_ERR_NOT_SUPPORTED:
msg = translate("Operation or feature not supported");
break;
case ESP_ERR_TIMEOUT:
msg = translate("Operation timed out");
break;
case ESP_ERR_INVALID_RESPONSE:
msg = translate("Received response was invalid");
break;
case ESP_ERR_INVALID_CRC:
msg = translate("CRC or checksum was invalid");
break;
case ESP_ERR_INVALID_VERSION:
msg = translate("Version was invalid");
break;
case ESP_ERR_INVALID_MAC:
msg = translate("MAC address was invalid");
break;
}
if (msg) {
mp_raise_msg(exception_type, msg);
}
const char *group = "ESP-IDF";
// tests must be in descending order
MP_STATIC_ASSERT(ESP_ERR_FLASH_BASE > ESP_ERR_MESH_BASE);
MP_STATIC_ASSERT(ESP_ERR_MESH_BASE > ESP_ERR_WIFI_BASE);
if (err >= ESP_ERR_FLASH_BASE) {
group = "Flash";
} else if (err >= ESP_ERR_MESH_BASE) {
group = "Mesh";
} else if (err >= ESP_ERR_WIFI_BASE) {
group = "WiFi";
}
mp_raise_msg_varg(exception_type, translate("%s error 0x%x"), group, err);
}