Skip to content

Commit edcf0c3

Browse files
committed
ESPULP: add option to set wakeup period and entrypoint
1 parent bf21254 commit edcf0c3

File tree

4 files changed

+77
-37
lines changed

4 files changed

+77
-37
lines changed

locale/circuitpython.pot

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -912,10 +912,6 @@ msgstr ""
912912
msgid "Error in safemode.py."
913913
msgstr ""
914914

915-
#: shared-bindings/ssl/SSLSocket.c
916-
msgid "Error: Failure to bind"
917-
msgstr ""
918-
919915
#: shared-bindings/alarm/__init__.c
920916
msgid "Expected a kind of %q"
921917
msgstr ""
@@ -1238,6 +1234,10 @@ msgstr ""
12381234
msgid "Invalid multicast MAC address"
12391235
msgstr ""
12401236

1237+
#: ports/espressif/common-hal/espulp/ULP.c
1238+
msgid "Invalid parameters"
1239+
msgstr ""
1240+
12411241
#: ports/espressif/common-hal/espidf/__init__.c
12421242
msgid "Invalid size"
12431243
msgstr ""
@@ -1278,6 +1278,10 @@ msgstr ""
12781278
msgid "Layer must be a Group or TileGrid subclass"
12791279
msgstr ""
12801280

1281+
#: ports/espressif/common-hal/espulp/ULP.c
1282+
msgid "Load binary failed"
1283+
msgstr ""
1284+
12811285
#: ports/espressif/common-hal/espidf/__init__.c
12821286
msgid "MAC address was invalid"
12831287
msgstr ""
@@ -1823,6 +1827,10 @@ msgstr ""
18231827
msgid "Right format but not supported"
18241828
msgstr ""
18251829

1830+
#: ports/espressif/common-hal/espulp/ULP.c
1831+
msgid "Run binary failed"
1832+
msgstr ""
1833+
18261834
#: main.c
18271835
msgid "Running in safe mode! Not running saved code.\n"
18281836
msgstr ""

ports/espressif/bindings/espulp/ULP.c

Lines changed: 33 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -90,21 +90,40 @@ STATIC mp_obj_t espulp_ulp_obj___exit__(size_t n_args, const mp_obj_t *args) {
9090
}
9191
STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(espulp_ulp___exit___obj, 4, 4, espulp_ulp_obj___exit__);
9292

93+
//| def set_wakeup_period(self, period_index: int, period_us: int) -> None:
94+
//| """Sets the wakeup period for the ULP."""
95+
//| ...
96+
STATIC mp_obj_t espulp_ulp_set_wakeup_period(mp_obj_t self_in, mp_obj_t period_index, mp_obj_t period_us) {
97+
espulp_ulp_obj_t *self = MP_OBJ_TO_PTR(self_in);
98+
check_for_deinit(self);
99+
100+
// period_index should be between 0 and 4 but bounds checking happens in esp-idf, so no need to do that here
101+
common_hal_espulp_ulp_set_wakeup_period(self, mp_obj_get_int(period_index), mp_obj_get_int(period_us));
102+
103+
return mp_const_none;
104+
}
105+
STATIC MP_DEFINE_CONST_FUN_OBJ_3(espulp_ulp_set_wakeup_period_obj, espulp_ulp_set_wakeup_period);
106+
93107
//| def run(
94-
//| self, program: ReadableBuffer, *, pins: Sequence[microcontroller.Pin] = ()
108+
//| self,
109+
//| program: ReadableBuffer,
110+
//| *,
111+
//| entrypoint: int,
112+
//| pins: Sequence[microcontroller.Pin] = ()
95113
//| ) -> None:
96-
//| """Loads the program into ULP memory and then runs the program. The given pins are
97-
//| claimed and not reset until `halt()` is called.
114+
//| """Loads the program into ULP memory and then runs the program, starting at entry_point.
115+
//| The given pins are claimed and not reset until `halt()` is called.
98116
//|
99117
//| The program will continue to run even when the running Python is halted."""
100118
//| ...
101119
STATIC mp_obj_t espulp_ulp_run(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) {
102120
espulp_ulp_obj_t *self = MP_OBJ_TO_PTR(pos_args[0]);
103121
check_for_deinit(self);
104122

105-
enum { ARG_program, ARG_pins };
123+
enum { ARG_program, ARG_entrypoint, ARG_pins };
106124
static const mp_arg_t allowed_args[] = {
107125
{ MP_QSTR_program, MP_ARG_REQUIRED | MP_ARG_OBJ},
126+
{ MP_QSTR_entrypoint, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = 0}},
108127
{ MP_QSTR_pins, MP_ARG_KW_ONLY | MP_ARG_OBJ, {.u_obj = mp_const_empty_tuple} },
109128
};
110129

@@ -114,6 +133,8 @@ STATIC mp_obj_t espulp_ulp_run(size_t n_args, const mp_obj_t *pos_args, mp_map_t
114133
mp_buffer_info_t bufinfo;
115134
mp_get_buffer_raise(args[ARG_program].u_obj, &bufinfo, MP_BUFFER_READ);
116135

136+
mp_uint_t entrypoint = args[ARG_entrypoint].u_int;
137+
117138
mp_obj_t pins_in = args[ARG_pins].u_obj;
118139
const size_t num_pins = (size_t)MP_OBJ_SMALL_INT_VALUE(mp_obj_len(pins_in));
119140

@@ -131,7 +152,7 @@ STATIC mp_obj_t espulp_ulp_run(size_t n_args, const mp_obj_t *pos_args, mp_map_t
131152
pin_mask |= 1 << pin->number;
132153
}
133154

134-
common_hal_espulp_ulp_run(self, bufinfo.buf, bufinfo.len, pin_mask);
155+
common_hal_espulp_ulp_run(self, bufinfo.buf, bufinfo.len, entrypoint, pin_mask);
135156
return mp_const_none;
136157
}
137158
STATIC MP_DEFINE_CONST_FUN_OBJ_KW(espulp_ulp_run_obj, 2, espulp_ulp_run);
@@ -163,12 +184,13 @@ MP_PROPERTY_GETTER(espulp_ulp_arch_obj,
163184
(mp_obj_t)&espulp_ulp_get_arch_obj);
164185

165186
STATIC const mp_rom_map_elem_t espulp_ulp_locals_table[] = {
166-
{ MP_ROM_QSTR(MP_QSTR_deinit), MP_ROM_PTR(&espulp_ulp_deinit_obj) },
167-
{ MP_ROM_QSTR(MP_QSTR___enter__), MP_ROM_PTR(&mp_identity_obj) },
168-
{ MP_ROM_QSTR(MP_QSTR___exit__), MP_ROM_PTR(&espulp_ulp___exit___obj) },
169-
{ MP_ROM_QSTR(MP_QSTR_run), MP_ROM_PTR(&espulp_ulp_run_obj) },
170-
{ MP_ROM_QSTR(MP_QSTR_halt), MP_ROM_PTR(&espulp_ulp_halt_obj) },
171-
{ MP_ROM_QSTR(MP_QSTR_arch), MP_ROM_PTR(&espulp_ulp_arch_obj) },
187+
{ MP_ROM_QSTR(MP_QSTR_deinit), MP_ROM_PTR(&espulp_ulp_deinit_obj) },
188+
{ MP_ROM_QSTR(MP_QSTR___enter__), MP_ROM_PTR(&mp_identity_obj) },
189+
{ MP_ROM_QSTR(MP_QSTR___exit__), MP_ROM_PTR(&espulp_ulp___exit___obj) },
190+
{ MP_ROM_QSTR(MP_QSTR_set_wakeup_period), MP_ROM_PTR(&espulp_ulp_set_wakeup_period_obj)},
191+
{ MP_ROM_QSTR(MP_QSTR_run), MP_ROM_PTR(&espulp_ulp_run_obj) },
192+
{ MP_ROM_QSTR(MP_QSTR_halt), MP_ROM_PTR(&espulp_ulp_halt_obj) },
193+
{ MP_ROM_QSTR(MP_QSTR_arch), MP_ROM_PTR(&espulp_ulp_arch_obj) },
172194
};
173195
STATIC MP_DEFINE_CONST_DICT(espulp_ulp_locals_dict, espulp_ulp_locals_table);
174196

ports/espressif/bindings/espulp/ULP.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,5 +35,6 @@ void common_hal_espulp_ulp_construct(espulp_ulp_obj_t *self, espulp_architecture
3535
bool common_hal_espulp_ulp_deinited(espulp_ulp_obj_t *self);
3636
void common_hal_espulp_ulp_deinit(espulp_ulp_obj_t *self);
3737

38-
void common_hal_espulp_ulp_run(espulp_ulp_obj_t *self, uint32_t *program, size_t length, uint32_t pin_mask);
38+
void common_hal_espulp_ulp_set_wakeup_period(espulp_ulp_obj_t *self, size_t period_index, uint32_t period_us);
39+
void common_hal_espulp_ulp_run(espulp_ulp_obj_t *self, uint32_t *program, size_t length, uint32_t entry_point, uint32_t pin_mask);
3940
void common_hal_espulp_ulp_halt(espulp_ulp_obj_t *self);

ports/espressif/common-hal/espulp/ULP.c

Lines changed: 30 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,13 @@
4646
// To-do idf v5.0: remove following include
4747
#include "soc/rtc_cntl_reg.h"
4848

49+
#ifndef CONFIG_ULP_COPROC_TYPE_FSM
50+
#warning "Have no FSM"
51+
#endif
52+
// #ifndef CONFIG_ULP_COPROC_TYPE_RISCV
53+
// #warning "Have no RISCV"
54+
// #endif
55+
4956
STATIC bool ulp_used = false;
5057
STATIC uint32_t pins_used = 0;
5158

@@ -54,7 +61,14 @@ void espulp_reset(void) {
5461
ulp_used = false;
5562
}
5663

57-
void common_hal_espulp_ulp_run(espulp_ulp_obj_t *self, uint32_t *program, size_t length, uint32_t pin_mask) {
64+
void common_hal_espulp_ulp_set_wakeup_period(espulp_ulp_obj_t *self, size_t period_index, uint32_t period_us) {
65+
int _errno = ulp_set_wakeup_period(period_index, period_us);
66+
if (_errno != ESP_OK) {
67+
mp_raise_ValueError(MP_ERROR_TEXT("Invalid parameters"));
68+
}
69+
}
70+
71+
void common_hal_espulp_ulp_run(espulp_ulp_obj_t *self, uint32_t *program, size_t length, uint32_t entry_point, uint32_t pin_mask) {
5872
if (length > CONFIG_ULP_COPROC_RESERVE_MEM) {
5973
mp_raise_ValueError(MP_ERROR_TEXT("Program too long"));
6074
}
@@ -87,13 +101,18 @@ void common_hal_espulp_ulp_run(espulp_ulp_obj_t *self, uint32_t *program, size_t
87101
}
88102
pins_used = pin_mask;
89103

90-
ulp_set_wakeup_period(0, 20000);
91-
104+
int _errno;
92105
switch (self->arch) {
93106
#ifdef CONFIG_ULP_COPROC_TYPE_FSM
94107
case FSM:
95-
ulp_load_binary(0, (const uint8_t *)program, length);
96-
ulp_run(0);
108+
_errno = ulp_load_binary(0, (const uint8_t *)program, length / sizeof(uint32_t));
109+
if (_errno != ESP_OK) {
110+
mp_raise_RuntimeError(MP_ERROR_TEXT("Load binary failed"));
111+
}
112+
_errno = ulp_run(entry_point / sizeof(uint32_t));
113+
if (_errno != ESP_OK) {
114+
mp_raise_RuntimeError(MP_ERROR_TEXT("Run binary failed"));
115+
}
97116
break;
98117
#endif
99118
#ifdef CONFIG_ULP_COPROC_TYPE_RISCV
@@ -109,23 +128,13 @@ void common_hal_espulp_ulp_run(espulp_ulp_obj_t *self, uint32_t *program, size_t
109128
}
110129

111130
void common_hal_espulp_ulp_halt(espulp_ulp_obj_t *self) {
112-
switch (self->arch) {
113-
/*
114-
#ifdef CONFIG_ULP_COPROC_TYPE_FSM
115-
case FSM:
116-
break;
117-
#endif
118-
*/
119-
#ifdef CONFIG_ULP_COPROC_TYPE_RISCV
120-
case RISCV:
121-
ulp_riscv_timer_stop();
122-
ulp_riscv_halt();
123-
break;
124-
#endif
125-
default:
126-
mp_raise_NotImplementedError(NULL);
127-
break;
131+
#ifdef CONFIG_ULP_COPROC_TYPE_RISCV
132+
if (self->arch == RISCV) {
133+
ulp_riscv_timer_stop();
134+
ulp_riscv_halt();
128135
}
136+
#endif
137+
CLEAR_PERI_REG_MASK(RTC_CNTL_ULP_CP_TIMER_REG, RTC_CNTL_ULP_CP_SLP_TIMER_EN);
129138

130139
// Release pins we were using.
131140
for (uint8_t i = 0; i < 32; i++) {

0 commit comments

Comments
 (0)