Skip to content

Commit 83b0620

Browse files
committed
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.
1 parent 664cadc commit 83b0620

File tree

17 files changed

+388
-106
lines changed

17 files changed

+388
-106
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 requried 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: 16 additions & 19 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

@@ -958,10 +958,6 @@ msgstr ""
958958
msgid "Failed to connect: timeout"
959959
msgstr ""
960960

961-
#: ports/espressif/common-hal/wifi/__init__.c
962-
msgid "Failed to init wifi"
963-
msgstr ""
964-
965961
#: shared-module/audiomp3/MP3Decoder.c
966962
msgid "Failed to parse MP3 file"
967963
msgstr ""
@@ -1023,7 +1019,7 @@ msgstr ""
10231019
msgid "GNSS init"
10241020
msgstr ""
10251021

1026-
#: ports/espressif/bindings/espidf/__init__.c ports/espressif/esp_error.c
1022+
#: ports/espressif/common-hal/espidf/__init__.c ports/espressif/esp_error.c
10271023
msgid "Generic Failure"
10281024
msgstr ""
10291025

@@ -1192,7 +1188,7 @@ msgstr ""
11921188
msgid "Invalid MAC address"
11931189
msgstr ""
11941190

1195-
#: ports/espressif/bindings/espidf/__init__.c ports/espressif/esp_error.c
1191+
#: ports/espressif/common-hal/espidf/__init__.c ports/espressif/esp_error.c
11961192
#: py/moduerrno.c
11971193
msgid "Invalid argument"
11981194
msgstr ""
@@ -1222,15 +1218,15 @@ msgstr ""
12221218
msgid "Invalid pins"
12231219
msgstr ""
12241220

1225-
#: ports/espressif/bindings/espidf/__init__.c ports/espressif/esp_error.c
1221+
#: ports/espressif/common-hal/espidf/__init__.c ports/espressif/esp_error.c
12261222
msgid "Invalid size"
12271223
msgstr ""
12281224

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

1233-
#: ports/espressif/bindings/espidf/__init__.c ports/espressif/esp_error.c
1229+
#: ports/espressif/common-hal/espidf/__init__.c ports/espressif/esp_error.c
12341230
msgid "Invalid state"
12351231
msgstr ""
12361232

@@ -1254,7 +1250,7 @@ msgstr ""
12541250
msgid "Layer must be a Group or TileGrid subclass"
12551251
msgstr ""
12561252

1257-
#: ports/espressif/bindings/espidf/__init__.c ports/espressif/esp_error.c
1253+
#: ports/espressif/common-hal/espidf/__init__.c ports/espressif/esp_error.c
12581254
msgid "MAC address was invalid"
12591255
msgstr ""
12601256

@@ -1548,7 +1544,7 @@ msgstr ""
15481544
msgid "Only IPv4 addresses supported"
15491545
msgstr ""
15501546

1551-
#: ports/espressif/common-hal/socketpool/SocketPool.c
1547+
#: ports/espressif/common-hal/socketpool/Socket.c
15521548
msgid "Only IPv4 sockets supported"
15531549
msgstr ""
15541550

@@ -1604,19 +1600,19 @@ msgstr ""
16041600
msgid "Operation not permitted"
16051601
msgstr ""
16061602

1607-
#: ports/espressif/bindings/espidf/__init__.c ports/espressif/esp_error.c
1603+
#: ports/espressif/common-hal/espidf/__init__.c ports/espressif/esp_error.c
16081604
msgid "Operation or feature not supported"
16091605
msgstr ""
16101606

1611-
#: ports/espressif/bindings/espidf/__init__.c ports/espressif/esp_error.c
1607+
#: ports/espressif/common-hal/espidf/__init__.c ports/espressif/esp_error.c
16121608
msgid "Operation timed out"
16131609
msgstr ""
16141610

1615-
#: ports/espressif/bindings/espidf/__init__.c ports/espressif/esp_error.c
1611+
#: ports/espressif/common-hal/espidf/__init__.c ports/espressif/esp_error.c
16161612
msgid "Out of memory"
16171613
msgstr ""
16181614

1619-
#: ports/espressif/common-hal/socketpool/SocketPool.c
1615+
#: ports/espressif/common-hal/socketpool/Socket.c
16201616
msgid "Out of sockets"
16211617
msgstr ""
16221618

@@ -1787,7 +1783,7 @@ msgstr ""
17871783
msgid "Read-only object"
17881784
msgstr ""
17891785

1790-
#: ports/espressif/bindings/espidf/__init__.c ports/espressif/esp_error.c
1786+
#: ports/espressif/common-hal/espidf/__init__.c ports/espressif/esp_error.c
17911787
msgid "Received response was invalid"
17921788
msgstr ""
17931789

@@ -1803,7 +1799,7 @@ msgstr ""
18031799
msgid "Requested AES mode is unsupported"
18041800
msgstr ""
18051801

1806-
#: ports/espressif/bindings/espidf/__init__.c ports/espressif/esp_error.c
1802+
#: ports/espressif/common-hal/espidf/__init__.c ports/espressif/esp_error.c
18071803
msgid "Requested resource not found"
18081804
msgstr ""
18091805

@@ -2225,7 +2221,7 @@ msgstr ""
22252221
msgid "Value length > max_length"
22262222
msgstr ""
22272223

2228-
#: ports/espressif/bindings/espidf/__init__.c ports/espressif/esp_error.c
2224+
#: ports/espressif/common-hal/espidf/__init__.c ports/espressif/esp_error.c
22292225
msgid "Version was invalid"
22302226
msgstr ""
22312227

@@ -3764,6 +3760,7 @@ msgid "pressing SW38 button at start up.\n"
37643760
msgstr ""
37653761

37663762
#: ports/espressif/boards/adafruit_qtpy_esp32c3/mpconfigboard.h
3763+
#: ports/espressif/boards/beetle-esp32-c3/mpconfigboard.h
37673764
#: ports/espressif/boards/lolin_c3_mini/mpconfigboard.h
37683765
#: supervisor/shared/safe_mode.c
37693766
msgid "pressing boot button at start up.\n"

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
@@ -279,6 +279,8 @@ ifneq ($(CIRCUITPY_BLEIO),0)
279279
SRC_C += common-hal/_bleio/ble_events.c
280280
endif
281281

282+
SRC_C += $(wildcard common-hal/espidf/*.c)
283+
282284
SRC_COMMON_HAL_EXPANDED = \
283285
$(addprefix shared-bindings/, $(SRC_COMMON_HAL)) \
284286
$(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)