Skip to content

Commit b5b6498

Browse files
authored
Merge pull request #6902 from MicroDev1/coproc
Add `coproc` module
2 parents 7015b73 + 5232e3f commit b5b6498

19 files changed

+815
-12
lines changed

locale/circuitpython.pot

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -507,7 +507,8 @@ msgstr ""
507507
msgid "Array must contain halfwords (type 'H')"
508508
msgstr ""
509509

510-
#: shared-bindings/alarm/SleepMemory.c shared-bindings/nvm/ByteArray.c
510+
#: shared-bindings/alarm/SleepMemory.c shared-bindings/coproc/CoprocMemory.c
511+
#: shared-bindings/nvm/ByteArray.c
511512
msgid "Array values should be single bytes."
512513
msgstr ""
513514

@@ -913,6 +914,7 @@ msgstr ""
913914

914915
#: py/enum.c shared-bindings/_bleio/__init__.c shared-bindings/aesio/aes.c
915916
#: shared-bindings/alarm/__init__.c shared-bindings/busio/SPI.c
917+
#: shared-bindings/coproc/Coproc.c shared-bindings/coproc/__init__.c
916918
#: shared-bindings/microcontroller/Pin.c
917919
#: shared-bindings/neopixel_write/__init__.c
918920
msgid "Expected a %q"
@@ -1012,6 +1014,7 @@ msgstr ""
10121014
msgid "Firmware is invalid"
10131015
msgstr ""
10141016

1017+
#: ports/espressif/common-hal/coproc/__init__.c
10151018
#: ports/espressif/common-hal/dualbank/__init__.c
10161019
msgid "Firmware is too big"
10171020
msgstr ""
@@ -1915,7 +1918,8 @@ msgstr ""
19151918
msgid "Sleep Memory not available"
19161919
msgstr ""
19171920

1918-
#: shared-bindings/alarm/SleepMemory.c shared-bindings/nvm/ByteArray.c
1921+
#: shared-bindings/alarm/SleepMemory.c shared-bindings/coproc/CoprocMemory.c
1922+
#: shared-bindings/nvm/ByteArray.c
19191923
msgid "Slice and value different lengths."
19201924
msgstr ""
19211925

@@ -2159,6 +2163,10 @@ msgstr ""
21592163
msgid "Unable to start mDNS query"
21602164
msgstr ""
21612165

2166+
#: shared-bindings/coproc/CoprocMemory.c
2167+
msgid "Unable to write"
2168+
msgstr ""
2169+
21622170
#: shared-bindings/nvm/ByteArray.c
21632171
msgid "Unable to write to nvm."
21642172
msgstr ""
@@ -2407,7 +2415,7 @@ msgid "array has too many dimensions"
24072415
msgstr ""
24082416

24092417
#: py/objarray.c shared-bindings/alarm/SleepMemory.c
2410-
#: shared-bindings/nvm/ByteArray.c
2418+
#: shared-bindings/coproc/CoprocMemory.c shared-bindings/nvm/ByteArray.c
24112419
msgid "array/bytes required on right side"
24122420
msgstr ""
24132421

@@ -3682,7 +3690,8 @@ msgid "only sample_rate=16000 is supported"
36823690
msgstr ""
36833691

36843692
#: py/objarray.c py/objstr.c py/objstrunicode.c py/objtuple.c
3685-
#: shared-bindings/alarm/SleepMemory.c shared-bindings/nvm/ByteArray.c
3693+
#: shared-bindings/alarm/SleepMemory.c shared-bindings/coproc/CoprocMemory.c
3694+
#: shared-bindings/nvm/ByteArray.c
36863695
msgid "only slices with step=1 (aka None) are supported"
36873696
msgstr ""
36883697

ports/espressif/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ set(ENV{IDF_PATH} ${CMAKE_SOURCE_DIR}/esp-idf)
66

77
# The component list here determines what options we get in menuconfig and what the ninja file
88
# can build.
9-
set(COMPONENTS esptool_py soc driver log main esp-tls mbedtls mdns esp_event esp_adc_cal esp_netif esp_wifi lwip wpa_supplicant freertos bt usb)
9+
set(COMPONENTS esptool_py soc driver log main esp-tls mbedtls mdns esp_event esp_adc_cal esp_netif esp_wifi lwip ulp wpa_supplicant freertos bt usb)
1010

1111
if("${CIRCUITPY_ESP32_CAMERA}")
1212
message("Including esp32-camera")

ports/espressif/Makefile

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,8 @@ INC += \
135135
-isystem esp-idf/components/soc/include \
136136
-isystem esp-idf/components/soc/$(IDF_TARGET)/include \
137137
-isystem esp-idf/components/spi_flash/include \
138+
-isystem esp-idf/components/ulp/include \
139+
-isystem esp-idf/components/ulp/ulp_riscv/include \
138140
-isystem esp-idf/components/$(IDF_TARGET_ARCH)/include \
139141
-isystem esp-idf/components/$(IDF_TARGET_ARCH)/$(IDF_TARGET)/include
140142

@@ -399,6 +401,9 @@ ifneq ($(CIRCUITPY_BLEIO),0)
399401
BINARY_BLOBS += esp-idf/components/esp_phy/lib/$(IDF_TARGET)/libbtbb.a \
400402
esp-idf/components/bt/controller/lib_esp32c3_family/$(IDF_TARGET)/libbtdm_app.a
401403
endif
404+
ifneq ($(CIRCUITPY_COPROC),0)
405+
ESP_IDF_COMPONENTS_LINK += ulp
406+
endif
402407

403408
ESP_IDF_COMPONENTS_EXPANDED = $(foreach component, $(ESP_IDF_COMPONENTS_LINK), $(BUILD)/esp-idf/esp-idf/$(component)/lib$(component).a)
404409

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
/*
2+
* This file is part of the Micro Python project, http://micropython.org/
3+
*
4+
* The MIT License (MIT)
5+
*
6+
* Copyright (c) 2022 microDev
7+
*
8+
* Permission is hereby granted, free of charge, to any person obtaining a copy
9+
* of this software and associated documentation files (the "Software"), to deal
10+
* in the Software without restriction, including without limitation the rights
11+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12+
* copies of the Software, and to permit persons to whom the Software is
13+
* furnished to do so, subject to the following conditions:
14+
*
15+
* The above copyright notice and this permission notice shall be included in
16+
* all copies or substantial portions of the Software.
17+
*
18+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
24+
* THE SOFTWARE.
25+
*/
26+
27+
#include "shared-bindings/coproc/Coproc.h"
28+
#include "shared-bindings/coproc/CoprocMemory.h"
29+
30+
void common_hal_coproc_coproc_construct(coproc_coproc_obj_t *self,
31+
const uint8_t *buf, const size_t buf_len, coproc_memory_obj_t *coproc_memory) {
32+
// set CoprocMemory object
33+
self->coproc_memory = coproc_memory;
34+
35+
// load buffer
36+
self->buf_len = buf_len;
37+
self->buf = (uint8_t *)m_malloc(self->buf_len, false);
38+
memcpy(self->buf, buf, self->buf_len);
39+
}
40+
41+
bool common_hal_coproc_coproc_deinited(coproc_coproc_obj_t *self) {
42+
return self->buf == NULL;
43+
}
44+
45+
void common_hal_coproc_coproc_deinit(coproc_coproc_obj_t *self) {
46+
if (common_hal_coproc_coproc_deinited(self)) {
47+
return;
48+
}
49+
m_free(self->buf);
50+
self->buf = NULL;
51+
self->coproc_memory = NULL;
52+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/*
2+
* This file is part of the Micro Python project, http://micropython.org/
3+
*
4+
* The MIT License (MIT)
5+
*
6+
* Copyright (c) 2022 microDev
7+
*
8+
* Permission is hereby granted, free of charge, to any person obtaining a copy
9+
* of this software and associated documentation files (the "Software"), to deal
10+
* in the Software without restriction, including without limitation the rights
11+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12+
* copies of the Software, and to permit persons to whom the Software is
13+
* furnished to do so, subject to the following conditions:
14+
*
15+
* The above copyright notice and this permission notice shall be included in
16+
* all copies or substantial portions of the Software.
17+
*
18+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
24+
* THE SOFTWARE.
25+
*/
26+
27+
#ifndef MICROPY_INCLUDED_ESPRESSIF_COMMON_HAL_COPROC_COPROC_H
28+
#define MICROPY_INCLUDED_ESPRESSIF_COMMON_HAL_COPROC_COPROC_H
29+
30+
#include "py/obj.h"
31+
#include "common-hal/coproc/CoprocMemory.h"
32+
33+
typedef struct {
34+
mp_obj_base_t base;
35+
uint8_t *buf;
36+
size_t buf_len;
37+
coproc_memory_obj_t *coproc_memory;
38+
} coproc_coproc_obj_t;
39+
40+
#endif // MICROPY_INCLUDED_ESPRESSIF_COMMON_HAL_COPROC_COPROC_H
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
/*
2+
* This file is part of the Micro Python project, http://micropython.org/
3+
*
4+
* The MIT License (MIT)
5+
*
6+
* Copyright (c) 2022 microDev
7+
*
8+
* Permission is hereby granted, free of charge, to any person obtaining a copy
9+
* of this software and associated documentation files (the "Software"), to deal
10+
* in the Software without restriction, including without limitation the rights
11+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12+
* copies of the Software, and to permit persons to whom the Software is
13+
* furnished to do so, subject to the following conditions:
14+
*
15+
* The above copyright notice and this permission notice shall be included in
16+
* all copies or substantial portions of the Software.
17+
*
18+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
24+
* THE SOFTWARE.
25+
*/
26+
27+
#include "shared-bindings/coproc/CoprocMemory.h"
28+
29+
uint32_t common_hal_coproc_memory_get_length(coproc_memory_obj_t *self) {
30+
return self->len;
31+
}
32+
33+
bool common_hal_coproc_memory_set_bytes(coproc_memory_obj_t *self,
34+
uint32_t start_index, const uint8_t *values, uint32_t len) {
35+
36+
if (start_index + len > self->len) {
37+
return false;
38+
}
39+
40+
memcpy((uint8_t *)(self->address + start_index), values, len);
41+
return true;
42+
}
43+
44+
void common_hal_coproc_memory_get_bytes(coproc_memory_obj_t *self,
45+
uint32_t start_index, uint8_t *values, uint32_t len) {
46+
47+
if (start_index + len > self->len) {
48+
return;
49+
}
50+
51+
memcpy(values, (uint8_t *)(self->address + start_index), len);
52+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/*
2+
* This file is part of the Micro Python project, http://micropython.org/
3+
*
4+
* The MIT License (MIT)
5+
*
6+
* Copyright (c) 2022 microDev
7+
*
8+
* Permission is hereby granted, free of charge, to any person obtaining a copy
9+
* of this software and associated documentation files (the "Software"), to deal
10+
* in the Software without restriction, including without limitation the rights
11+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12+
* copies of the Software, and to permit persons to whom the Software is
13+
* furnished to do so, subject to the following conditions:
14+
*
15+
* The above copyright notice and this permission notice shall be included in
16+
* all copies or substantial portions of the Software.
17+
*
18+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
24+
* THE SOFTWARE.
25+
*/
26+
27+
#ifndef MICROPY_INCLUDED_ESPRESSIF_COMMON_HAL_COPROC_COPROCMEMORY_H
28+
#define MICROPY_INCLUDED_ESPRESSIF_COMMON_HAL_COPROC_COPROCMEMORY_H
29+
30+
#include "py/obj.h"
31+
32+
typedef struct {
33+
mp_obj_base_t base;
34+
uint32_t address;
35+
uint16_t len;
36+
} coproc_memory_obj_t;
37+
38+
#endif // MICROPY_INCLUDED_ESPRESSIF_COMMON_HAL_COPROC_COPROCMEMORY_H
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
/*
2+
* This file is part of the Micro Python project, http://micropython.org/
3+
*
4+
* The MIT License (MIT)
5+
*
6+
* Copyright (c) 2022 microDev
7+
*
8+
* Permission is hereby granted, free of charge, to any person obtaining a copy
9+
* of this software and associated documentation files (the "Software"), to deal
10+
* in the Software without restriction, including without limitation the rights
11+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12+
* copies of the Software, and to permit persons to whom the Software is
13+
* furnished to do so, subject to the following conditions:
14+
*
15+
* The above copyright notice and this permission notice shall be included in
16+
* all copies or substantial portions of the Software.
17+
*
18+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
24+
* THE SOFTWARE.
25+
*/
26+
27+
#include "shared-bindings/coproc/__init__.h"
28+
29+
#include "py/runtime.h"
30+
31+
#if defined(CONFIG_IDF_TARGET_ESP32S2)
32+
#include "esp32s2/ulp.h"
33+
#include "esp32s2/ulp_riscv.h"
34+
#elif defined(CONFIG_IDF_TARGET_ESP32S3)
35+
#include "esp32s3/ulp.h"
36+
#include "esp32s3/ulp_riscv.h"
37+
#endif
38+
39+
// To-do idf v5.0: remove following include
40+
#include "soc/rtc_cntl_reg.h"
41+
42+
void common_hal_coproc_run(coproc_coproc_obj_t *self) {
43+
if (ulp_riscv_load_binary(self->buf, self->buf_len) != ESP_OK) {
44+
mp_raise_RuntimeError(translate("Firmware is too big"));
45+
}
46+
m_free(self->buf);
47+
self->buf = (uint8_t *)RTC_SLOW_MEM;
48+
ulp_riscv_run();
49+
}
50+
51+
void common_hal_coproc_halt(coproc_coproc_obj_t *self) {
52+
self->buf = (uint8_t *)m_malloc(self->buf_len, false);
53+
memcpy(self->buf, (uint8_t *)RTC_SLOW_MEM, self->buf_len);
54+
55+
// To-do idf v5.0: use following functions
56+
// ulp_riscv_timer_stop();
57+
// ulp_riscv_halt();
58+
59+
// stop the ulp timer
60+
CLEAR_PERI_REG_MASK(RTC_CNTL_ULP_CP_TIMER_REG, RTC_CNTL_ULP_CP_SLP_TIMER_EN);
61+
// suspends the ulp operation
62+
SET_PERI_REG_MASK(RTC_CNTL_COCPU_CTRL_REG, RTC_CNTL_COCPU_DONE);
63+
// Resets the processor
64+
SET_PERI_REG_MASK(RTC_CNTL_COCPU_CTRL_REG, RTC_CNTL_COCPU_SHUT_RESET_EN);
65+
}
66+
67+
mp_obj_t common_hal_coproc_memory(coproc_coproc_obj_t *self) {
68+
return (self->coproc_memory) ? MP_OBJ_FROM_PTR(self->coproc_memory) : mp_const_none;
69+
}

ports/espressif/esp-idf-config/sdkconfig-esp32s2.defaults

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,9 @@ CONFIG_ESP32S2_DATA_CACHE_LINE_32B=y
4141

4242
# CONFIG_ESP32S2_TRAX is not set
4343
CONFIG_ESP32S2_TRACEMEM_RESERVE_DRAM=0x0
44-
# CONFIG_ESP32S2_ULP_COPROC_ENABLED is not set
45-
CONFIG_ESP32S2_ULP_COPROC_RESERVE_MEM=0
44+
CONFIG_ESP32S2_ULP_COPROC_ENABLED=y
45+
CONFIG_ESP32S2_ULP_COPROC_RESERVE_MEM=4096
46+
CONFIG_ESP32S2_ULP_COPROC_RISCV=y
4647
CONFIG_ESP32S2_DEBUG_OCDAWARE=y
4748
# CONFIG_ESP32S2_DEBUG_STUBS_ENABLE is not set
4849
CONFIG_ESP32S2_BROWNOUT_DET=y
@@ -65,7 +66,7 @@ CONFIG_ESP32S2_RTC_CLK_SRC_INT_RC=y
6566
CONFIG_ESP32S2_RTC_CLK_CAL_CYCLES=576
6667
# CONFIG_ESP32S2_NO_BLOBS is not set
6768
# CONFIG_ESP32S2_KEEP_USB_ALIVE is not set
68-
# CONFIG_ESP32S2_RTCDATA_IN_FAST_MEM is not set
69+
CONFIG_ESP32S2_RTCDATA_IN_FAST_MEM=y
6970
# CONFIG_ESP32S2_USE_FIXED_STATIC_RAM_SIZE is not set
7071
#
7172
# MAC Config

ports/espressif/esp-idf-config/sdkconfig-esp32s3.defaults

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -67,8 +67,9 @@ CONFIG_ESP32S3_DATA_CACHE_LINE_SIZE=32
6767

6868
# CONFIG_ESP32S3_TRAX is not set
6969
CONFIG_ESP32S3_TRACEMEM_RESERVE_DRAM=0x0
70-
# CONFIG_ESP32S3_ULP_COPROC_ENABLED is not set
71-
CONFIG_ESP32S3_ULP_COPROC_RESERVE_MEM=0
70+
CONFIG_ESP32S3_ULP_COPROC_ENABLED=y
71+
CONFIG_ESP32S3_ULP_COPROC_RESERVE_MEM=4096
72+
CONFIG_ESP32S3_ULP_COPROC_RISCV=y
7273
CONFIG_ESP32S3_BROWNOUT_DET=y
7374
CONFIG_ESP32S3_BROWNOUT_DET_LVL_SEL_7=y
7475
# CONFIG_ESP32S3_BROWNOUT_DET_LVL_SEL_6 is not set
@@ -88,7 +89,7 @@ CONFIG_ESP32S3_RTC_CLK_SRC_INT_RC=y
8889
# CONFIG_ESP32S3_RTC_CLK_SRC_INT_8MD256 is not set
8990
CONFIG_ESP32S3_RTC_CLK_CAL_CYCLES=1024
9091
CONFIG_ESP32S3_DEEP_SLEEP_WAKEUP_DELAY=2000
91-
# CONFIG_ESP32S3_RTCDATA_IN_FAST_MEM is not set
92+
CONFIG_ESP32S3_RTCDATA_IN_FAST_MEM=y
9293
# CONFIG_ESP32S3_USE_FIXED_STATIC_RAM_SIZE is not set
9394
# end of ESP32S3-Specific
9495

@@ -218,4 +219,3 @@ CONFIG_CAMERA_DMA_BUFFER_SIZE_MAX=32768
218219
# CONFIG_CAMERA_CONVERTER_ENABLED is not set
219220
# end of Camera configuration
220221
# end of Component config
221-

0 commit comments

Comments
 (0)