8000 ESP32: New feature: EspError exception, Pulse counter(PCNT), Quadrature encoder(QUAD) by IhorNehrutsa · Pull Request #6626 · micropython/micropython · GitHub
[go: up one dir, main page]

Skip to content

ESP32: New feature: EspError exception, Pulse counter(PCNT), Quadrature encoder(QUAD) #6626

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
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
7 changes: 7 additions & 0 deletions ports/esp32/cmodules/esp_err/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
**EspError Exception**

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
157 changes: 157 additions & 0 deletions ports/esp32/cmodules/esp_err/esp_err.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,157 @@
/*
* 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) // you may relocate this line to the mpconfigport.h
#if MODULE_ESP_ERR_ENABLED

// Include required definitions first
#include "py/obj.h"
//#include "py/objstr.h"
//#include "py/objtuple.h"
//#include "py/runtime.h"
//#include "py/builtin.h"

#include "py/objexcept.h"

//#include "esp_error.h"

/*
// Example how to raise ESP exception from C code
esp_err_t err = pcnt_event_disable(self->unit, evt_type);
if (err != ESP_OK)
raise_esp_error(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
}
*/

// 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) },
};
STATIC MP_DEFINE_CONST_DICT(esp_err_EspError_locals_dict, esp_err_EspError_locals_dict_table);

// Create the class-object itself
#if 1
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,
};
#else
#error The same ?, but raise Guru Meditation Error
MP_DEFINE_EXCEPTION(EspError, Exception)
#endif

// module stuff
// Set up the module properties
STATIC const mp_rom_map_elem_t esp_err_globals_table[] = {
{ MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR_esp_err) },
{ MP_ROM_QSTR(MP_QSTR_EspError), MP_ROM_PTR(&mp_type_EspError) },
};
STATIC MP_DEFINE_CONST_DICT(esp_err_globals, esp_err_globals_table);

// Define the module object
const mp_obj_module_t esp_err_cmodule = {
.base = { &mp_type_module },
.globals = (mp_obj_dict_t*)&esp_err_globals,
};
// Register the module
MP_REGISTER_MODULE(MP_QSTR_esp_err, esp_err_cmodule, MODULE_ESP_ERR_ENABLED);

/*
// 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
4 changes: 4 additions & 0 deletions ports/esp32/cmodules/esp_err/esp_error.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
const mp_obj_type_t mp_type_EspError;

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

8 changes: 8 additions & 0 deletions ports/esp32/cmodules/esp_err/micropython.mk
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
MOD_DIR := $(USERMOD_DIR)

# Add all C files to SRC_USERMOD.
SRC_USERMOD += $(MOD_DIR)/esp_err.c

# We can add our module folder to include paths if needed
# This is not actually needed in this example.
CFLAGS_USERMOD += -I$(MOD_DIR)
19 changes: 19 additions & 0 deletions ports/esp32/cmodules/pcnt/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
**ESP32 Pulse Counter**
https://docs.espressif.com/projects/esp-idf/en/latest/esp32/api-reference/peripherals/pcnt.html

Wrapped around
https://github.com/espressif/esp-idf/blob/master/components/driver/include/driver/pcnt.h
https://github.com/espressif/esp-idf/blob/master/components/hal/include/hal/pcnt_types.h
https://github.com/espressif/esp-idf/blob/m 6D40 aster/components/driver/pcnt.c

See also
https://github.com/espressif/esp-idf/tree/master/examples/peripherals/pcnt/pulse_count_event


**ESP32 Quadrature Encoder based on Pulse Counter(PCNT)**
Based on
https://github.com/madhephaestus/ESP32Encoder
https://github.com/bboser/MicroPython_ESP32_psRAM_LoBo/blob/quad_decoder/MicroPython_BUILD/components/micropython/esp32/machine_dec.c

See also
https://github.com/espressif/esp-idf/tree/master/examples/peripherals/pcnt/rotary_encoder
8 changes: 8 additions & 0 deletions ports/esp32/cmodules/pcnt/micropython.mk
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
MOD_DIR := $(USERMOD_DIR)

# Add all C files to SRC_USERMOD.
SRC_USERMOD += $(MOD_DIR)/pcnt.c

# We can add our module folder to include paths if needed
# This is not actually needed in this example.
CFLAGS_USERMOD += -I$(MOD_DIR)
Loading
0