Created
March 21, 2019 13:38
-
-
Save shimarin/3f13c67b05b41c8931a566918591f5a6 to your computer and use it in GitHub Desktop.
native_memory module for MicroPython ESP32
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
diff --git a/ports/esp32/Makefile b/ports/esp32/Makefile | |
index 36ee4b3b0..3af707a13 100644 | |
--- a/ports/esp32/Makefile | |
+++ b/ports/esp32/Makefile | |
@@ -178,6 +178,7 @@ SRC_C = \ | |
machine_dac.c \ | |
machine_pwm.c \ | |
machine_uart.c \ | |
+ native_memory.c \ | |
modmachine.c \ | |
modnetwork.c \ | |
network_lan.c \ | |
diff --git a/ports/esp32/boards/sdkconfig.spiram b/ports/esp32/boards/sdkconfig.spiram | |
index b34781a0d..f8f42c3e5 100644 | |
--- a/ports/esp32/boards/sdkconfig.spiram | |
+++ b/ports/esp32/boards/sdkconfig.spiram | |
@@ -11,12 +11,15 @@ CONFIG_APP_EXCLUDE_PROJECT_NAME_VAR=y | |
CONFIG_LOG_BOOTLOADER_LEVEL_WARN=y | |
# ESP32-specific | |
+CONFIG_ESPTOOLPY_FLASHFREQ_80M=y | |
CONFIG_ESP32_DEFAULT_CPU_FREQ_240=y | |
CONFIG_SPIRAM_SUPPORT=y | |
CONFIG_SPIRAM_IGNORE_NOTFOUND=y | |
CONFIG_SPIRAM_USE_MEMMAP=y | |
CONFIG_TASK_WDT_CHECK_IDLE_TASK_CPU0=n | |
CONFIG_ESP32_XTAL_FREQ_AUTO=y | |
+CONFIG_SPIRAM_SPEED_80M=y | |
+CONFIG_SPIRAM_OCCUPY_HSPI_HOST=y | |
# Power Management | |
CONFIG_PM_ENABLE=y | |
diff --git a/ports/esp32/mpconfigport.h b/ports/esp32/mpconfigport.h | |
index 7b9b40025..639664b6b 100644 | |
--- a/ports/esp32/mpconfigport.h | |
+++ b/ports/esp32/mpconfigport.h | |
@@ -176,6 +176,7 @@ extern const struct _mp_obj_module_t utime_module; | |
extern const struct _mp_obj_module_t uos_module; | |
extern const struct _mp_obj_module_t mp_module_usocket; | |
extern const struct _mp_obj_module_t mp_module_machine; | |
+extern const struct _mp_obj_module_t mp_module_native_memory; | |
extern const struct _mp_obj_module_t mp_module_network; | |
extern const struct _mp_obj_module_t mp_module_onewire; | |
@@ -186,6 +187,7 @@ extern const struct _mp_obj_module_t mp_module_onewire; | |
{ MP_OBJ_NEW_QSTR(MP_QSTR_uos), (mp_obj_t)&uos_module }, \ | |
{ MP_OBJ_NEW_QSTR(MP_QSTR_usocket), (mp_obj_t)&mp_module_usocket }, \ | |
{ MP_OBJ_NEW_QSTR(MP_QSTR_machine), (mp_obj_t)&mp_module_machine }, \ | |
+ { MP_OBJ_NEW_QSTR(MP_QSTR_native_memory), (mp_obj_t)&mp_module_native_memory }, \ | |
{ MP_OBJ_NEW_QSTR(MP_QSTR_network), (mp_obj_t)&mp_module_network }, \ | |
{ MP_OBJ_NEW_QSTR(MP_QSTR__onewire), (mp_obj_t)&mp_module_onewire }, \ | |
{ MP_OBJ_NEW_QSTR(MP_QSTR_uhashlib), (mp_obj_t)&mp_module_uhashlib }, \ | |
diff --git a/ports/esp32/native_memory.c b/ports/esp32/native_memory.c | |
new file mode 100644 | |
index 000000000..fc7f742d4 | |
--- /dev/null | |
+++ b/ports/esp32/native_memory.c | |
@@ -0,0 +1,58 @@ | |
+#include "py/runtime.h" | |
+ | |
+#define SLOT_NUM 3 | |
+ | |
+typedef struct _native_memory_slot_t { | |
+ void* ptr; | |
+ size_t size; | |
+} native_memory_slot_t; | |
+ | |
+STATIC native_memory_slot_t native_memory_slot[SLOT_NUM] = { | |
+ { NULL, 0 }, | |
+ { NULL, 0 }, | |
+ { NULL, 0 } | |
+}; | |
+ | |
+STATIC mp_obj_t native_memory_alloc(mp_obj_t _slot, mp_obj_t _size) { | |
+ int slot = (int)mp_obj_int_get_truncated(_slot); | |
+ size_t size = (size_t)mp_obj_int_get_truncated(_size); | |
+ void* new_ptr = NULL; | |
+ if (slot < 0 || slot >= SLOT_NUM) { | |
+ mp_raise_msg(&mp_type_IndexError, "slot index out of range"); | |
+ return mp_const_none; | |
+ } | |
+ // else | |
+ if (size == 0) { | |
+ if (native_memory_slot[slot].ptr != NULL) heap_caps_free(native_memory_slot[slot].ptr); | |
+ new_ptr = 0; | |
+ } else if (native_memory_slot[slot].ptr != NULL) { | |
+ if (native_memory_slot[slot].size >= size) new_ptr = native_memory_slot[slot].ptr; | |
+ else { | |
+ new_ptr = heap_caps_realloc(native_memory_slot[slot].ptr, size, MALLOC_CAP_8BIT|MALLOC_CAP_INTERNAL); | |
+ } | |
+ } else { | |
+ new_ptr = heap_caps_malloc(size, MALLOC_CAP_8BIT|MALLOC_CAP_INTERNAL); | |
+ } | |
+ | |
+ if (new_ptr == NULL && size > 0) { | |
+ mp_raise_msg(&mp_type_MemoryError, "memory allocation failed"); | |
+ return mp_const_none; | |
+ } | |
+ // else | |
+ native_memory_slot[slot].ptr = new_ptr; | |
+ native_memory_slot[slot].size = size; | |
+ return mp_obj_new_int((mp_int_t)(uintptr_t)new_ptr); | |
+} | |
+MP_DEFINE_CONST_FUN_OBJ_2(native_memory_alloc_obj, native_memory_alloc); | |
+ | |
+STATIC const mp_rom_map_elem_t mp_module_native_memory_globals_table[] = { | |
+ { MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR_native_memory) }, | |
+ { MP_ROM_QSTR(MP_QSTR_alloc), MP_ROM_PTR(&native_memory_alloc_obj) } | |
+}; | |
+ | |
+STATIC MP_DEFINE_CONST_DICT(mp_module_native_memory_globals, mp_module_native_memory_globals_table); | |
+ | |
+const mp_obj_module_t mp_module_native_memory = { | |
+ .base = { &mp_type_module }, | |
+ .globals = (mp_obj_dict_t*)&mp_module_native_memory_globals | |
+}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment