|
| 1 | +/* This example demonstrates using an ESP32 esp-idf function |
| 2 | +*/ |
| 3 | + |
| 4 | +// Include the header file to get access to the MicroPython API |
| 5 | +#include "py/dynruntime.h" |
| 6 | + |
| 7 | +// ESP-IDF imports |
| 8 | +#include <driver/pcnt.h> |
| 9 | +#include <esp_err.h> |
| 10 | + |
| 11 | +// This is the function which will be called from Python as counter_init(x) |
| 12 | +STATIC mp_obj_t counter_init(mp_obj_t x_obj) { |
| 13 | + // Extract the integer from the MicroPython input object |
| 14 | + mp_int_t x = mp_obj_get_int(x_obj); |
| 15 | + // Call pcnt_unit_config |
| 16 | + pcnt_config_t cfg = { |
| 17 | + .unit = 0, |
| 18 | + .channel = 0, |
| 19 | + .pulse_gpio_num = x, |
| 20 | + .ctrl_gpio_num = -1, // not yet supported |
| 21 | + .counter_h_lim = 0, |
| 22 | + .counter_l_lim = 0, |
| 23 | + .pos_mode = PCNT_COUNT_DIS, // positive edge is no-op |
| 24 | + .neg_mode = PCNT_COUNT_DIS, // negative edge is no-op |
| 25 | + .lctrl_mode = PCNT_MODE_KEEP, // ctrl pin is no-op |
| 26 | + .hctrl_mode = PCNT_MODE_KEEP, // ctrl pin is no-op |
| 27 | + }; |
| 28 | + esp_err_t err = pcnt_unit_config(&cfg); |
| 29 | + // Convert the result to a MicroPython integer object and return it |
| 30 | + return mp_obj_new_int(err); |
| 31 | +} |
| 32 | +// Define a Python reference to the function above |
| 33 | +STATIC MP_DEFINE_CONST_FUN_OBJ_1(counter_init_obj, counter_init); |
| 34 | + |
| 35 | +STATIC mp_obj_t mem_info() { |
| 36 | + extern int mp_micropython_mem_info(int, int); |
| 37 | + mp_printf(&mp_plat_print, "mp_fun_table is at 0x%x\n", &mp_fun_table); |
| 38 | + mp_printf(&mp_plat_print, "plat_relo_tab is at 0x%x\n", mp_fun_table.plat_relo_tab); |
| 39 | + mp_printf(&mp_plat_print, "calling mp_micropython_mem_info\n"); |
| 40 | + int i = mp_micropython_mem_info(0, 0); |
| 41 | + mp_printf(&mp_plat_print, "mp_micropython_mem_info: %d/0x%x\n", i, i); |
| 42 | + return mp_const_none; |
| 43 | +} |
| 44 | +STATIC MP_DEFINE_CONST_FUN_OBJ_0(mem_info_obj, mem_info); |
| 45 | + |
| 46 | +// This is the entry point and is called when the module is imported |
| 47 | +mp_obj_t mpy_init(mp_obj_fun_bc_t *self, size_t n_args, size_t n_kw, mp_obj_t *args) { |
| 48 | + // This must be first, it sets up the globals dict and other things |
| 49 | + MP_DYNRUNTIME_INIT_ENTRY |
| 50 | + |
| 51 | + // Make the function available in the module's namespace |
| 52 | + mp_store_global(MP_QSTR_counter_init, MP_OBJ_FROM_PTR(&counter_init_obj)); |
| 53 | + mp_store_global(MP_QSTR_mem_info, MP_OBJ_FROM_PTR(&mem_info_obj)); |
| 54 | + |
| 55 | + // This must be last, it restores the globals dict |
| 56 | + MP_DYNRUNTIME_INIT_EXIT |
| 57 | +} |
0 commit comments