Skip to content

Add ability to reserve psram #6667

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

Merged
merged 1 commit into from
Aug 5, 2022
Merged
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
6 changes: 6 additions & 0 deletions docs/environment.rst
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,12 @@ CIRCUITPY_BLE_NAME
~~~~~~~~~~~~~~~~~~
Default BLE name the board advertises as, including for the BLE workflow.

CIRCUITPY_RESERVED_PSRAM
~~~~~~~~~~~~~~~~~~~~~~~~
On boards with Espressif microcontrollers with PSRAM (also called SPIRAM), permanently reserve a portion of PSRAM for use by esp-idf.
This storage is removed from the CircuitPython "heap" and is available for allocation by esp-idf routines in the core instead.
Generally, only set this to a non-zero value when it is required by a specific core module.

CIRCUITPY_WEB_API_PASSWORD
~~~~~~~~~~~~~~~~~~~~~~~~~~
Password required to make modifications to the board from the Web Workflow.
Expand Down
26 changes: 13 additions & 13 deletions locale/circuitpython.pot
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,7 @@ msgstr ""
msgid "%q=%q"
msgstr ""

#: ports/espressif/bindings/espidf/__init__.c ports/espressif/esp_error.c
#: ports/espressif/common-hal/espidf/__init__.c ports/espressif/esp_error.c
#, c-format
msgid "%s error 0x%x"
msgstr ""
Expand Down Expand Up @@ -645,7 +645,7 @@ msgstr ""
msgid "CIRCUITPY drive could not be found or created."
msgstr ""

#: ports/espressif/bindings/espidf/__init__.c ports/espressif/esp_error.c
#: ports/espressif/common-hal/espidf/__init__.c ports/espressif/esp_error.c
msgid "CRC or checksum was invalid"
msgstr ""

Expand Down Expand Up @@ -1019,7 +1019,7 @@ msgstr ""
msgid "GNSS init"
msgstr ""

#: ports/espressif/bindings/espidf/__init__.c ports/espressif/esp_error.c
#: ports/espressif/common-hal/espidf/__init__.c ports/espressif/esp_error.c
msgid "Generic Failure"
msgstr ""

Expand Down Expand Up @@ -1188,7 +1188,7 @@ msgstr ""
msgid "Invalid MAC address"
msgstr ""

#: ports/espressif/bindings/espidf/__init__.c ports/espressif/esp_error.c
#: ports/espressif/common-hal/espidf/__init__.c ports/espressif/esp_error.c
#: py/moduerrno.c
msgid "Invalid argument"
msgstr ""
Expand Down Expand Up @@ -1218,15 +1218,15 @@ msgstr ""
msgid "Invalid pins"
msgstr ""

#: ports/espressif/bindings/espidf/__init__.c ports/espressif/esp_error.c
#: ports/espressif/common-hal/espidf/__init__.c ports/espressif/esp_error.c
msgid "Invalid size"
msgstr ""

#: ports/espressif/common-hal/ssl/SSLContext.c
msgid "Invalid socket for TLS"
msgstr ""

#: ports/espressif/bindings/espidf/__init__.c ports/espressif/esp_error.c
#: ports/espressif/common-hal/espidf/__init__.c ports/espressif/esp_error.c
msgid "Invalid state"
msgstr ""

Expand All @@ -1250,7 +1250,7 @@ msgstr ""
msgid "Layer must be a Group or TileGrid subclass"
msgstr ""

#: ports/espressif/bindings/espidf/__init__.c ports/espressif/esp_error.c
#: ports/espressif/common-hal/espidf/__init__.c ports/espressif/esp_error.c
msgid "MAC address was invalid"
msgstr ""

Expand Down Expand Up @@ -1600,15 +1600,15 @@ msgstr ""
msgid "Operation not permitted"
msgstr ""

#: ports/espressif/bindings/espidf/__init__.c ports/espressif/esp_error.c
#: ports/espressif/common-hal/espidf/__init__.c ports/espressif/esp_error.c
msgid "Operation or feature not supported"
msgstr ""

#: ports/espressif/bindings/espidf/__init__.c ports/espressif/esp_error.c
#: ports/espressif/common-hal/espidf/__init__.c ports/espressif/esp_error.c
msgid "Operation timed out"
msgstr ""

#: ports/espressif/bindings/espidf/__init__.c ports/espressif/esp_error.c
#: ports/espressif/common-hal/espidf/__init__.c ports/espressif/esp_error.c
msgid "Out of memory"
msgstr ""

Expand Down Expand Up @@ -1783,7 +1783,7 @@ msgstr ""
msgid "Read-only object"
msgstr ""

#: ports/espressif/bindings/espidf/__init__.c ports/espressif/esp_error.c
#: ports/espressif/common-hal/espidf/__init__.c ports/espressif/esp_error.c
msgid "Received response was invalid"
msgstr ""

Expand All @@ -1799,7 +1799,7 @@ msgstr ""
msgid "Requested AES mode is unsupported"
msgstr ""

#: ports/espressif/bindings/espidf/__init__.c ports/espressif/esp_error.c
#: ports/espressif/common-hal/espidf/__init__.c ports/espressif/esp_error.c
msgid "Requested resource not found"
msgstr ""

Expand Down Expand Up @@ -2221,7 +2221,7 @@ msgstr ""
msgid "Value length > max_length"
msgstr ""

#: ports/espressif/bindings/espidf/__init__.c ports/espressif/esp_error.c
#: ports/espressif/common-hal/espidf/__init__.c ports/espressif/esp_error.c
msgid "Version was invalid"
msgstr ""

Expand Down
4 changes: 4 additions & 0 deletions main.c
Original file line number Diff line number Diff line change
Expand Up @@ -792,8 +792,12 @@ STATIC void __attribute__ ((noinline)) run_boot_py(safe_mode_t safe_mode) {
usb_get_boot_py_data(usb_boot_py_data, size);
#endif

port_post_boot_py(true);

cleanup_after_vm(heap, result.exception);

port_post_boot_py(false);

#if CIRCUITPY_USB
// Now give back the data we saved from the heap going away.
usb_return_boot_py_data(usb_boot_py_data, size);
Expand Down
2 changes: 2 additions & 0 deletions ports/espressif/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -281,6 +281,8 @@ ifneq ($(CIRCUITPY_BLEIO),0)
SRC_C += common-hal/_bleio/ble_events.c
endif

SRC_C += $(wildcard common-hal/espidf/*.c)

SRC_COMMON_HAL_EXPANDED = \
$(addprefix shared-bindings/, $(SRC_COMMON_HAL)) \
$(addprefix shared-bindings/, $(SRC_BINDINGS_ENUMS)) \
Expand Down
79 changes: 16 additions & 63 deletions ports/espressif/bindings/espidf/__init__.c
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,20 @@ const mp_obj_type_t mp_type_espidf_MemoryError = {
.parent = &mp_type_MemoryError,
};

//| def get_total_psram() -> int:
//| """Returns the number of bytes of psram detected, or 0 if psram is not present or not configured"""
STATIC mp_obj_t espidf_get_total_psram(void) {
return MP_OBJ_NEW_SMALL_INT(common_hal_espidf_get_total_psram());
}
MP_DEFINE_CONST_FUN_OBJ_0(espidf_get_total_psram_obj, espidf_get_total_psram);

//| def get_reserved_psram() -> int:
//| """Returns number of bytes of psram reserved for use by esp-idf, either a board-specific default value or the value defined in ``/.env``."""
STATIC mp_obj_t espidf_get_reserved_psram(void) {
return MP_OBJ_NEW_SMALL_INT(common_hal_espidf_get_reserved_psram());
}
MP_DEFINE_CONST_FUN_OBJ_0(espidf_get_reserved_psram_obj, espidf_get_reserved_psram);

STATIC const mp_rom_map_elem_t espidf_module_globals_table[] = {
{ MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR_espidf) },

Expand All @@ -129,6 +143,8 @@ STATIC const mp_rom_map_elem_t espidf_module_globals_table[] = {

{ MP_ROM_QSTR(MP_QSTR_erase_nvs), MP_ROM_PTR(&espidf_erase_nvs_obj)},

{ MP_ROM_QSTR(MP_QSTR_get_total_psram), MP_ROM_PTR(&espidf_get_total_psram_obj)},
{ MP_ROM_QSTR(MP_QSTR_get_reserved_psram), MP_ROM_PTR(&espidf_get_reserved_psram_obj)},
{ MP_ROM_QSTR(MP_QSTR_IDFError), MP_ROM_PTR(&mp_type_espidf_IDFError) },
{ MP_ROM_QSTR(MP_QSTR_MemoryError), MP_ROM_PTR(&mp_type_espidf_MemoryError) },
};
Expand All @@ -139,66 +155,3 @@ const mp_obj_module_t espidf_module = {
.base = { &mp_type_module },
.globals = (mp_obj_dict_t *)&espidf_module_globals,
};

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);
}

MP_REGISTER_MODULE(MP_QSTR_espidf, espidf_module, CIRCUITPY_ESPIDF);
7 changes: 7 additions & 0 deletions ports/espressif/bindings/espidf/__init__.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,4 +39,11 @@ NORETURN void mp_raise_espidf_MemoryError(void);
void raise_esp_error(esp_err_t err) NORETURN;
#define CHECK_ESP_RESULT(x) do { int res = (x); if (res != ESP_OK) raise_esp_error(res); } while (0)

void common_hal_espidf_reserve_psram(void);
bool common_hal_espidf_set_reserved_psram(size_t amount);
size_t common_hal_espidf_get_reserved_psram(void);
size_t common_hal_espidf_get_total_psram(void);
intptr_t common_hal_espidf_get_psram_start(void);
intptr_t common_hal_espidf_get_psram_end(void);

#endif // MICROPY_INCLUDED_ESPRESSIF_BINDINGS_ESPIDF___INIT___H
Loading