Skip to content

Commit c1abbaa

Browse files
committed
Merge remote-tracking branch 'origin/main'
2 parents b3c9caf + 082f573 commit c1abbaa

File tree

8 files changed

+62
-22
lines changed

8 files changed

+62
-22
lines changed

locale/circuitpython.pot

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -193,6 +193,7 @@ msgstr ""
193193

194194
#: ports/atmel-samd/common-hal/pulseio/PulseIn.c
195195
#: ports/cxd56/common-hal/pulseio/PulseIn.c
196+
#: ports/espressif/common-hal/coproc/Coproc.c
196197
#: ports/nrf/common-hal/pulseio/PulseIn.c
197198
#: ports/raspberrypi/common-hal/rp2pio/StateMachine.c
198199
#: ports/stm/common-hal/pulseio/PulseIn.c py/argcheck.c
@@ -485,6 +486,7 @@ msgstr ""
485486
msgid "Already have all-matches listener"
486487
msgstr ""
487488

489+
#: ports/espressif/common-hal/coproc/__init__.c
488490
#: shared-module/memorymonitor/AllocationAlarm.c
489491
#: shared-module/memorymonitor/AllocationSize.c
490492
msgid "Already running"
@@ -1015,7 +1017,7 @@ msgstr ""
10151017
msgid "Firmware is invalid"
10161018
msgstr ""
10171019

1018-
#: ports/espressif/common-hal/coproc/__init__.c
1020+
#: ports/espressif/common-hal/coproc/Coproc.c
10191021
#: ports/espressif/common-hal/dualbank/__init__.c
10201022
msgid "Firmware is too big"
10211023
msgstr ""

ports/espressif/common-hal/alarm/coproc/CoprocAlarm.c

Lines changed: 26 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,16 @@
2727
#include "shared-bindings/alarm/coproc/CoprocAlarm.h"
2828
#include "shared-bindings/coproc/__init__.h"
2929

30+
#if CIRCUITPY_COPROC
31+
32+
#include "supervisor/port.h"
33+
34+
#include "driver/rtc_cntl.h"
35+
#include "soc/rtc_cntl_reg.h"
36+
3037
#include "esp_sleep.h"
3138

32-
#if CIRCUITPY_COPROC
39+
static volatile bool woke_up = false;
3340

3441
mp_obj_t alarm_coproc_coprocalarm_find_triggered_alarm(const size_t n_alarms, const mp_obj_t *alarms) {
3542
for (size_t i = 0; i < n_alarms; i++) {
@@ -47,6 +54,13 @@ mp_obj_t alarm_coproc_coprocalarm_create_wakeup_alarm(void) {
4754
return alarm;
4855
}
4956

57+
// This is used to wake the main CircuitPython task.
58+
STATIC void coproc_interrupt(void *arg) {
59+
(void)arg;
60+
woke_up = true;
61+
port_wake_main_task_from_isr();
62+
}
63+
5064
void alarm_coproc_coprocalarm_set_alarm(const bool deep_sleep, const size_t n_alarms, const mp_obj_t *alarms) {
5165
bool coproc_alarm_set = false;
5266
alarm_coproc_coprocalarm_obj_t *coproc_alarm = MP_OBJ_NULL;
@@ -65,24 +79,30 @@ void alarm_coproc_coprocalarm_set_alarm(const bool deep_sleep, const size_t n_al
6579
return;
6680
}
6781

68-
// load coproc program
69-
common_hal_coproc_load(coproc_alarm->coproc);
82+
// enable coproc interrupt
83+
rtc_isr_register(&coproc_interrupt, NULL, RTC_CNTL_COCPU_INT_ST);
84+
REG_SET_BIT(RTC_CNTL_INT_ENA_REG, RTC_CNTL_COCPU_INT_ENA);
85+
86+
// start coproc program
87+
common_hal_coproc_run(coproc_alarm->coproc);
7088
}
7189

7290
void alarm_coproc_coprocalarm_prepare_for_deep_sleep(void) {
73-
// start coproc program
74-
common_hal_coproc_run(NULL);
91+
// disbale coproc interrupt
92+
rtc_isr_deregister(&coproc_interrupt, NULL);
93+
REG_CLR_BIT(RTC_CNTL_INT_ENA_REG, RTC_CNTL_COCPU_INT_ENA);
7594

7695
// enable coproc wakeup
7796
esp_sleep_enable_ulp_wakeup();
7897
esp_sleep_pd_config(ESP_PD_DOMAIN_RTC_SLOW_MEM, ESP_PD_OPTION_ON);
7998
}
8099

81100
bool alarm_coproc_coprocalarm_woke_this_cycle(void) {
82-
return false;
101+
return woke_up;
83102
}
84103

85104
void alarm_coproc_coprocalarm_reset(void) {
105+
woke_up = false;
86106
}
87107

88108
#else // CIRCUITPY_COPROC

ports/espressif/common-hal/coproc/Coproc.c

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,12 +27,33 @@
2727
#include "shared-bindings/coproc/Coproc.h"
2828
#include "shared-bindings/coproc/CoprocMemory.h"
2929

30+
#include "py/runtime.h"
31+
32+
#if defined(CONFIG_IDF_TARGET_ESP32S2)
33+
#include "esp32s2/ulp.h"
34+
#define ULP_COPROC_RESERVE_MEM (CONFIG_ESP32S2_ULP_COPROC_RESERVE_MEM)
35+
#elif defined(CONFIG_IDF_TARGET_ESP32S3)
36+
#include "esp32s3/ulp.h"
37+
#define ULP_COPROC_RESERVE_MEM (CONFIG_ESP32S3_ULP_COPROC_RESERVE_MEM)
38+
#endif
39+
40+
#define RTC_SLOW_MEM_END ((uint32_t)RTC_SLOW_MEM + ULP_COPROC_RESERVE_MEM)
41+
3042
void common_hal_coproc_coproc_construct(coproc_coproc_obj_t *self,
3143
const uint8_t *buf, const size_t buf_len, coproc_memory_obj_t *coproc_memory) {
3244
// set CoprocMemory object
45+
if (coproc_memory != NULL) {
46+
if (coproc_memory->address < ((uint32_t)RTC_SLOW_MEM + buf_len) ||
47+
coproc_memory->address > (RTC_SLOW_MEM_END - coproc_memory->len)) {
48+
mp_raise_ValueError_varg(translate("%q out of range"), MP_QSTR_CoprocMemory);
49+
}
50+
}
3351
self->coproc_memory = coproc_memory;
3452

3553
// load buffer
54+
if (buf_len > ULP_COPROC_RESERVE_MEM) {
55+
mp_raise_RuntimeError(translate("Firmware is too big"));
56+
}
3657
self->buf_len = buf_len;
3758
self->buf = (uint8_t *)m_malloc(self->buf_len, false);
3859
memcpy(self->buf, buf, self->buf_len);

ports/espressif/common-hal/coproc/__init__.c

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,14 @@
4040
#include "soc/rtc_cntl_reg.h"
4141

4242
void common_hal_coproc_run(coproc_coproc_obj_t *self) {
43+
if (GET_PERI_REG_MASK(RTC_CNTL_ULP_CP_TIMER_REG, RTC_CNTL_ULP_CP_SLP_TIMER_EN)) {
44+
mp_raise_RuntimeError(translate("Already running"));
45+
}
46+
47+
ulp_riscv_load_binary(self->buf, self->buf_len);
48+
m_free(self->buf);
49+
self->buf = (uint8_t *)RTC_SLOW_MEM;
50+
4351
ulp_riscv_run();
4452
}
4553

@@ -55,18 +63,10 @@ void common_hal_coproc_halt(coproc_coproc_obj_t *self) {
5563
CLEAR_PERI_REG_MASK(RTC_CNTL_ULP_CP_TIMER_REG, RTC_CNTL_ULP_CP_SLP_TIMER_EN);
5664
// suspends the ulp operation
5765
SET_PERI_REG_MASK(RTC_CNTL_COCPU_CTRL_REG, RTC_CNTL_COCPU_DONE);
58-
// Resets the processor
66+
// resets the processor
5967
SET_PERI_REG_MASK(RTC_CNTL_COCPU_CTRL_REG, RTC_CNTL_COCPU_SHUT_RESET_EN);
6068
}
6169

62-
void common_hal_coproc_load(coproc_coproc_obj_t *self) {
63-
if (ulp_riscv_load_binary(self->buf, self->buf_len) != ESP_OK) {
64-
mp_raise_RuntimeError(translate("Firmware is too big"));
65-
}
66-
m_free(self->buf);
67-
self->buf = (uint8_t *)RTC_SLOW_MEM;
68-
}
69-
7070
mp_obj_t common_hal_coproc_memory(coproc_coproc_obj_t *self) {
7171
return (self->coproc_memory) ? MP_OBJ_FROM_PTR(self->coproc_memory) : mp_const_none;
7272
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ CONFIG_ESP32S2_DATA_CACHE_LINE_32B=y
4242
# CONFIG_ESP32S2_TRAX is not set
4343
CONFIG_ESP32S2_TRACEMEM_RESERVE_DRAM=0x0
4444
CONFIG_ESP32S2_ULP_COPROC_ENABLED=y
45-
CONFIG_ESP32S2_ULP_COPROC_RESERVE_MEM=4096
45+
CONFIG_ESP32S2_ULP_COPROC_RESERVE_MEM=8176
4646
CONFIG_ESP32S2_ULP_COPROC_RISCV=y
4747
CONFIG_ESP32S2_DEBUG_OCDAWARE=y
4848
# CONFIG_ESP32S2_DEBUG_STUBS_ENABLE is not set

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ CONFIG_ESP32S3_DATA_CACHE_LINE_SIZE=32
6868
# CONFIG_ESP32S3_TRAX is not set
6969
CONFIG_ESP32S3_TRACEMEM_RESERVE_DRAM=0x0
7070
CONFIG_ESP32S3_ULP_COPROC_ENABLED=y
71-
CONFIG_ESP32S3_ULP_COPROC_RESERVE_MEM=4096
71+
CONFIG_ESP32S3_ULP_COPROC_RESERVE_MEM=8176
7272
CONFIG_ESP32S3_ULP_COPROC_RISCV=y
7373
CONFIG_ESP32S3_BROWNOUT_DET=y
7474
CONFIG_ESP32S3_BROWNOUT_DET_LVL_SEL_7=y

shared-bindings/coproc/__init__.c

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -68,9 +68,7 @@ STATIC coproc_coproc_obj_t *get_coproc_obj(mp_obj_t *self_in) {
6868
//| ...
6969
//|
7070
STATIC mp_obj_t coproc_run(mp_obj_t self_in) {
71-
coproc_coproc_obj_t *self = get_coproc_obj(&self_in);
72-
common_hal_coproc_load(self);
73-
common_hal_coproc_run(self);
71+
common_hal_coproc_run(get_coproc_obj(&self_in));
7472
return mp_const_none;
7573
}
7674
STATIC MP_DEFINE_CONST_FUN_OBJ_1(coproc_run_obj, coproc_run);

shared-bindings/coproc/__init__.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,6 @@
3232

3333
extern void common_hal_coproc_run(coproc_coproc_obj_t *self);
3434
extern void common_hal_coproc_halt(coproc_coproc_obj_t *self);
35-
extern void common_hal_coproc_load(coproc_coproc_obj_t *self);
3635
extern mp_obj_t common_hal_coproc_memory(coproc_coproc_obj_t *self);
3736

3837
#endif // MICROPY_INCLUDED_SHARED_BINDINGS_COPROC___INIT___H

0 commit comments

Comments
 (0)