Skip to content

Commit 82be75a

Browse files
jeplerdhalbert
andcommitted
Add ability to reserve psram
.. the primary user of which will be the camera, since the framebuffers must be allocated via esp-idf allocation function and never from the gc heap. A board can have a default value, and the value can also be set in the /.env file using the key CIRCUITPY_RESERVED_PSRAM with the value being the reserved size in bytes. Co-authored-by: Dan Halbert <[email protected]>
1 parent d659a3b commit 82be75a

File tree

17 files changed

+385
-100
lines changed

17 files changed

+385
-100
lines changed

docs/environment.rst

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,12 @@ CIRCUITPY_BLE_NAME
3535
~~~~~~~~~~~~~~~~~~
3636
Default BLE name the board advertises as, including for the BLE workflow.
3737

38+
CIRCUITPY_RESERVED_PSRAM
39+
~~~~~~~~~~~~~~~~~~~~~~~~
40+
On boards with Espressif microcontrollers with PSRAM (also called SPIRAM), permanently reserve a portion of PSRAM for use by esp-idf.
41+
This storage is removed from the CircuitPython "heap" and is available for allocation by esp-idf routines in the core instead.
42+
Generally, only set this to a non-zero value when it is required by a specific core module.
43+
3844
CIRCUITPY_WEB_API_PASSWORD
3945
~~~~~~~~~~~~~~~~~~~~~~~~~~
4046
Password required to make modifications to the board from the Web Workflow.

locale/circuitpython.pot

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -209,7 +209,7 @@ msgstr ""
209209
msgid "%q=%q"
210210
msgstr ""
211211

212-
#: ports/espressif/bindings/espidf/__init__.c ports/espressif/esp_error.c
212+
#: ports/espressif/common-hal/espidf/__init__.c ports/espressif/esp_error.c
213213
#, c-format
214214
msgid "%s error 0x%x"
215215
msgstr ""
@@ -645,7 +645,7 @@ msgstr ""
645645
msgid "CIRCUITPY drive could not be found or created."
646646
msgstr ""
647647

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

@@ -1019,7 +1019,7 @@ msgstr ""
10191019
msgid "GNSS init"
10201020
msgstr ""
10211021

1022-
#: ports/espressif/bindings/espidf/__init__.c ports/espressif/esp_error.c
1022+
#: ports/espressif/common-hal/espidf/__init__.c ports/espressif/esp_error.c
10231023
msgid "Generic Failure"
10241024
msgstr ""
10251025

@@ -1188,7 +1188,7 @@ msgstr ""
11881188
msgid "Invalid MAC address"
11891189
msgstr ""
11901190

1191-
#: ports/espressif/bindings/espidf/__init__.c ports/espressif/esp_error.c
1191+
#: ports/espressif/common-hal/espidf/__init__.c ports/espressif/esp_error.c
11921192
#: py/moduerrno.c
11931193
msgid "Invalid argument"
11941194
msgstr ""
@@ -1218,15 +1218,15 @@ msgstr ""
12181218
msgid "Invalid pins"
12191219
msgstr ""
12201220

1221-
#: ports/espressif/bindings/espidf/__init__.c ports/espressif/esp_error.c
1221+
#: ports/espressif/common-hal/espidf/__init__.c ports/espressif/esp_error.c
12221222
msgid "Invalid size"
12231223
msgstr ""
12241224

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

1229-
#: ports/espressif/bindings/espidf/__init__.c ports/espressif/esp_error.c
1229+
#: ports/espressif/common-hal/espidf/__init__.c ports/espressif/esp_error.c
12301230
msgid "Invalid state"
12311231
msgstr ""
12321232

@@ -1250,7 +1250,7 @@ msgstr ""
12501250
msgid "Layer must be a Group or TileGrid subclass"
12511251
msgstr ""
12521252

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

@@ -1600,15 +1600,15 @@ msgstr ""
16001600
msgid "Operation not permitted"
16011601
msgstr ""
16021602

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

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

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

@@ -1783,7 +1783,7 @@ msgstr ""
17831783
msgid "Read-only object"
17841784
msgstr ""
17851785

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

@@ -1799,7 +1799,7 @@ msgstr ""
17991799
msgid "Requested AES mode is unsupported"
18001800
msgstr ""
18011801

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

@@ -2221,7 +2221,7 @@ msgstr ""
22212221
msgid "Value length > max_length"
22222222
msgstr ""
22232223

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

main.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -792,8 +792,12 @@ STATIC void __attribute__ ((noinline)) run_boot_py(safe_mode_t safe_mode) {
792792
usb_get_boot_py_data(usb_boot_py_data, size);
793793
#endif
794794

795+
port_post_boot_py(true);
796+
795797
cleanup_after_vm(heap, result.exception);
796798

799+
port_post_boot_py(false);
800+
797801
#if CIRCUITPY_USB
798802
// Now give back the data we saved from the heap going away.
799803
usb_return_boot_py_data(usb_boot_py_data, size);

ports/espressif/Makefile

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -281,6 +281,8 @@ ifneq ($(CIRCUITPY_BLEIO),0)
281281
SRC_C += common-hal/_bleio/ble_events.c
282282
endif
283283

284+
SRC_C += $(wildcard common-hal/espidf/*.c)
285+
284286
SRC_COMMON_HAL_EXPANDED = \
285287
$(addprefix shared-bindings/, $(SRC_COMMON_HAL)) \
286288
$(addprefix shared-bindings/, $(SRC_BINDINGS_ENUMS)) \

ports/espressif/bindings/espidf/__init__.c

Lines changed: 16 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,20 @@ const mp_obj_type_t mp_type_espidf_MemoryError = {
120120
.parent = &mp_type_MemoryError,
121121
};
122122

123+
//| def get_total_psram() -> int:
124+
//| """Returns the number of bytes of psram detected, or 0 if psram is not present or not configured"""
125+
STATIC mp_obj_t espidf_get_total_psram(void) {
126+
return MP_OBJ_NEW_SMALL_INT(common_hal_espidf_get_total_psram());
127+
}
128+
MP_DEFINE_CONST_FUN_OBJ_0(espidf_get_total_psram_obj, espidf_get_total_psram);
129+
130+
//| def get_reserved_psram() -> int:
131+
//| """Returns number of bytes of psram reserved for use by esp-idf, either a board-specific default value or the value defined in ``/.env``."""
132+
STATIC mp_obj_t espidf_get_reserved_psram(void) {
133+
return MP_OBJ_NEW_SMALL_INT(common_hal_espidf_get_reserved_psram());
134+
}
135+
MP_DEFINE_CONST_FUN_OBJ_0(espidf_get_reserved_psram_obj, espidf_get_reserved_psram);
136+
123137
STATIC const mp_rom_map_elem_t espidf_module_globals_table[] = {
124138
{ MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR_espidf) },
125139

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

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

146+
{ MP_ROM_QSTR(MP_QSTR_get_total_psram), MP_ROM_PTR(&espidf_get_total_psram_obj)},
147+
{ MP_ROM_QSTR(MP_QSTR_get_reserved_psram), MP_ROM_PTR(&espidf_get_reserved_psram_obj)},
132148
{ MP_ROM_QSTR(MP_QSTR_IDFError), MP_ROM_PTR(&mp_type_espidf_IDFError) },
133149
{ MP_ROM_QSTR(MP_QSTR_MemoryError), MP_ROM_PTR(&mp_type_espidf_MemoryError) },
134150
};
@@ -139,66 +155,3 @@ const mp_obj_module_t espidf_module = {
139155
.base = { &mp_type_module },
140156
.globals = (mp_obj_dict_t *)&espidf_module_globals,
141157
};
142-
143-
void raise_esp_error(esp_err_t err) {
144-
const compressed_string_t *msg = NULL;
145-
const mp_obj_type_t *exception_type = &mp_type_espidf_IDFError;
146-
switch (err) {
147-
case ESP_FAIL:
148-
msg = translate("Generic Failure");
149-
break;
150-
case ESP_ERR_NO_MEM:
151-
exception_type = &mp_type_espidf_MemoryError;
152-
msg = translate("Out of memory");
153-
break;
154-
case ESP_ERR_INVALID_ARG:
155-
msg = translate("Invalid argument");
156-
break;
157-
case ESP_ERR_INVALID_STATE:
158-
msg = translate("Invalid state");
159-
break;
160-
case ESP_ERR_INVALID_SIZE:
161-
msg = translate("Invalid size");
162-
break;
163-
case ESP_ERR_NOT_FOUND:
164-
msg = translate("Requested resource not found");
165-
break;
166-
case ESP_ERR_NOT_SUPPORTED:
167-
msg = translate("Operation or feature not supported");
168-
break;
169-
case ESP_ERR_TIMEOUT:
170-
msg = translate("Operation timed out");
171-
break;
172-
case ESP_ERR_INVALID_RESPONSE:
173-
msg = translate("Received response was invalid");
174-
break;
175-
case ESP_ERR_INVALID_CRC:
176-
msg = translate("CRC or checksum was invalid");
177-
break;
178-
case ESP_ERR_INVALID_VERSION:
179-
msg = translate("Version was invalid");
180-
break;
181-
case ESP_ERR_INVALID_MAC:
182-
msg = translate("MAC address was invalid");
183-
break;
184-
}
185-
if (msg) {
186-
mp_raise_msg(exception_type, msg);
187-
}
188-
189-
const char *group = "ESP-IDF";
190-
191-
// tests must be in descending order
192-
MP_STATIC_ASSERT(ESP_ERR_FLASH_BASE > ESP_ERR_MESH_BASE);
193-
MP_STATIC_ASSERT(ESP_ERR_MESH_BASE > ESP_ERR_WIFI_BASE);
194-
if (err >= ESP_ERR_FLASH_BASE) {
195-
group = "Flash";
196-
} else if (err >= ESP_ERR_MESH_BASE) {
197-
group = "Mesh";
198-
} else if (err >= ESP_ERR_WIFI_BASE) {
199-
group = "WiFi";
200-
}
201-
mp_raise_msg_varg(exception_type, translate("%s error 0x%x"), group, err);
202-
}
203-
204-
MP_REGISTER_MODULE(MP_QSTR_espidf, espidf_module, CIRCUITPY_ESPIDF);

ports/espressif/bindings/espidf/__init__.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,4 +39,11 @@ NORETURN void mp_raise_espidf_MemoryError(void);
3939
void raise_esp_error(esp_err_t err) NORETURN;
4040
#define CHECK_ESP_RESULT(x) do { int res = (x); if (res != ESP_OK) raise_esp_error(res); } while (0)
4141

42+
void common_hal_espidf_reserve_psram(void);
43+
bool common_hal_espidf_set_reserved_psram(size_t amount);
44+
size_t common_hal_espidf_get_reserved_psram(void);
45+
size_t common_hal_espidf_get_total_psram(void);
46+
intptr_t common_hal_espidf_get_psram_start(void);
47+
intptr_t common_hal_espidf_get_psram_end(void);
48+
4249
#endif // MICROPY_INCLUDED_ESPRESSIF_BINDINGS_ESPIDF___INIT___H

0 commit comments

Comments
 (0)