Skip to content

ESP32: New feature: EspError exception #6638

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Closed
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
148 changes: 148 additions & 0 deletions ports/esp32/esp_error.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,148 @@
/*
* This file was generated by micropython-extmod-generator https://github.com/prusnak/micropython-extmod-generator
* from Python stab file esp_err.py
*
* The MIT License (MIT)
*
* Copyright (c) 2020 Ihor Nehrutsa
*
* 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.
*/

/*
Error Codes and Helper Functions
https://docs.espressif.com/projects/esp-idf/en/latest/esp32/api-reference/system/esp_err.html

Wrapped around
https://github.com/espressif/esp-idf/blob/master/components/esp_common/include/esp_err.h
*/

#define MODULE_ESP_ERR_ENABLED (1)
#if MODULE_ESP_ERR_ENABLED

// Include required definitions first
#include "py/obj.h"
#include "py/objexcept.h"


/*
// Example how to raise ESP exception from C code

#include "esp_error.h"

ESP_EXCEPTIONS(pcnt_event_disable(self->unit, evt_type))

// is equivalent to

esp_err_t err = pcnt_event_disable(self->unit, evt_type);
if (err != ESP_OK)
mp_raise_EspError(err);
*/

// Defining classes
// class EspError(Exception):
typedef struct _mp_obj_esp_err_EspError_t {
mp_obj_base_t base;
} mp_obj_esp_err_EspError_t;

const mp_obj_type_t mp_type_EspError;
/*
// Defining EspError methods
// def EspError.__init__(self, error_code: int=0, error_msg: str='')
STATIC mp_obj_t esp_err_EspError_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_kw, const mp_obj_t *args) {
mp_arg_check_num(n_args, n_kw, 0, 2, false);
self->base.type = &mp_type_EspError;

mp_obj_esp_err_EspError_t *self = MP_OBJ_TO_PTR(args[0]);
mp_int_t error_code = mp_obj_get_int(args[1]);
const char* error_msg = mp_obj_str_get_str(args[2]);

//TODO: Your code here
return MP_OBJ_FROM_PTR(self);
}
*/
/*
STATIC mp_obj_t esp_err_EspError_print(const mp_print_t *print, mp_obj_t self_obj, mp_print_kind_t kind) {
esp_err_EspError_obj_t *self = MP_OBJ_TO_PTR(self_obj);
mp_printf(print, "EspError()");

//TODO: Your code here
}
*/

STATIC mp_obj_t esp_err_EspError_esp_err_to_name(mp_obj_t self_obj, mp_obj_t err_obj) {
// mp_obj_esp_err_EspError_t *self = MP_OBJ_TO_PTR(self_obj);
mp_int_t err = mp_obj_get_int(err_obj);

const char *s = esp_err_to_name(err);

return mp_obj_new_str(s, strlen(s));
}
STATIC MP_DEFINE_CONST_FUN_OBJ_2(esp_err_EspError_esp_err_to_name_obj, esp_err_EspError_esp_err_to_name);

// EspError stuff
// Register class methods
STATIC const mp_rom_map_elem_t esp_err_EspError_locals_dict_table[] = {
// { MP_ROM_QSTR(MP_QSTR_ESP_ERR_FLASH_BASE), MP_ROM_INT(ESP_ERR_FLASH_BASE) },
{ MP_ROM_QSTR(MP_QSTR_ESP_ERR_INVALID_ARG), MP_ROM_INT(ESP_ERR_INVALID_ARG) },
{ MP_ROM_QSTR(MP_QSTR_ESP_ERR_INVALID_CRC), MP_ROM_INT(ESP_ERR_INVALID_CRC) },
{ MP_ROM_QSTR(MP_QSTR_ESP_ERR_INVALID_MAC), MP_ROM_INT(ESP_ERR_INVALID_MAC) },
{ MP_ROM_QSTR(MP_QSTR_ESP_ERR_INVALID_RESPONSE), MP_ROM_INT(ESP_ERR_INVALID_RESPONSE) },
{ MP_ROM_QSTR(MP_QSTR_ESP_ERR_INVALID_SIZE), MP_ROM_INT(ESP_ERR_INVALID_SIZE) },
{ MP_ROM_QSTR(MP_QSTR_ESP_ERR_INVALID_STATE), MP_ROM_INT(ESP_ERR_INVALID_STATE) },
{ MP_ROM_QSTR(MP_QSTR_ESP_ERR_INVALID_VERSION), MP_ROM_INT(ESP_ERR_INVALID_VERSION) },
{ MP_ROM_QSTR(MP_QSTR_ESP_ERR_MESH_BASE), MP_ROM_INT(ESP_ERR_MESH_BASE) },
{ MP_ROM_QSTR(MP_QSTR_ESP_ERR_NOT_FOUND), MP_ROM_INT(ESP_ERR_NOT_FOUND) },
{ MP_ROM_QSTR(MP_QSTR_ESP_ERR_NOT_SUPPORTED), MP_ROM_INT(ESP_ERR_NOT_SUPPORTED) },
{ MP_ROM_QSTR(MP_QSTR_ESP_ERR_NO_MEM), MP_ROM_INT(ESP_ERR_NO_MEM) },
{ MP_ROM_QSTR(MP_QSTR_ESP_ERR_TIMEOUT), MP_ROM_INT(ESP_ERR_TIMEOUT) },
{ MP_ROM_QSTR(MP_QSTR_ESP_ERR_WIFI_BASE), MP_ROM_INT(ESP_ERR_WIFI_BASE) },
{ MP_ROM_QSTR(MP_QSTR_ESP_FAIL), MP_ROM_INT(ESP_FAIL) },
{ MP_ROM_QSTR(MP_QSTR_ESP_OK), MP_ROM_INT(ESP_OK) },
Comment on lines +103 to +117
Copy link
Contributor Author

@IhorNehrutsa IhorNehrutsa Nov 20, 2020

Choose a reason for hiding this comment

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

I'm not sure if all(or part) ESP-IDF codes should be pulled(Q-stringed) as constants into MicroPython.
The complete list is very long and not in the "micro" paradigm.

Error Codes Reference
https://docs.espressif.com/projects/esp-idf/en/latest/esp32/api-reference/error-codes.html

{ MP_ROM_QSTR(MP_QSTR_err_to_name), MP_ROM_PTR(&esp_err_EspError_esp_err_to_name_obj) }, \
};
STATIC MP_DEFINE_CONST_DICT(esp_err_EspError_locals_dict, esp_err_EspError_locals_dict_table);

// Create the class-object itself
const mp_obj_type_t mp_type_EspError = {
{ &mp_type_type },
.name = MP_QSTR_EspError,

// .make_new = esp_err_EspError_make_new,
// .print = esp_err_EspError_print,
.locals_dict = (mp_obj_dict_t *)&esp_err_EspError_locals_dict,

.print = mp_obj_exception_print,
.make_new = mp_obj_exception_make_new,
.attr = mp_obj_exception_attr,
.parent = &mp_type_Exception,
};

/*
// To call from C modules use like:
mp_raise_msg_varg(&mp_type_EspError, MP_ERROR_TEXT("An error message %d"), val);
*/
/*
// Function to call from C modules
NORETURN void mp_raise_EspError(mp_rom_error_text_t msg) {
mp_raise_msg(&mp_type_EspError, msg);
}
*/

#endif // MODULE_ESP_ERR_ENABLED
39 changes: 39 additions & 0 deletions ports/esp32/esp_error.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/*
* This file is part of the MicroPython project, http://micropython.org/
*
* The MIT License (MIT)
*
* Copyright (c) 2020 Ihor Nehrutsa
*
* 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.
*/
#ifndef MICROPY_INCLUDED_ESP_ERROR_H
#define MICROPY_INCLUDED_ESP_ERROR_H

#define mp_raise_EspError(e) mp_raise_msg_varg(&mp_type_EspError, MP_ERROR_TEXT("%d(0x%X) - %s"), e, e, esp_err_to_name(e));

static inline void esp_exceptions(esp_err_t e) {
if (e != ESP_OK) {
mp_raise_EspError(e)
}
}

#define ESP_EXCEPTIONS(e) do { esp_exceptions(e); } while (0);

#endif // MICROPY_INCLUDED_ESP_ERROR_H
1 change: 1 addition & 0 deletions ports/esp32/mpconfigport.h
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,7 @@

// extra built in names to add to the global namespace
#define MICROPY_PORT_BUILTINS \
{ MP_ROM_QSTR(MP_QSTR_EspError), MP_ROM_PTR(&mp_type_EspError) }, \
{ MP_OBJ_NEW_QSTR(MP_QSTR_input), (mp_obj_t)&mp_builtin_input_obj }, \
{ MP_OBJ_NEW_QSTR(MP_QSTR_open), (mp_obj_t)&mp_builtin_open_obj },

Expand Down
1 change: 1 addition & 0 deletions py/obj.h
Original file line number Diff line number Diff line change
Expand Up @@ -666,6 +666,7 @@ extern const mp_obj_type_t mp_type_UnicodeError;
extern const mp_obj_type_t mp_type_ValueError;
extern const mp_obj_type_t mp_type_ViperTypeError;
extern const mp_obj_type_t mp_type_ZeroDivisionError;
extern const mp_obj_type_t mp_type_EspError; // it used in ESP32 port only, not visible in other ports // ESP8266-???

// Constant objects, globally accessible: None, False, True
// These should always be accessed via the below macros.
Expand Down