8000 ports/esp32/esp32_rmt.c: ISR-driven pulse generator in RMT. · micropython/micropython@207e296 · GitHub < 10000 body class="logged-out env-production page-responsive" style="word-wrap: break-word;">
Skip to content

Commit 207e296

Browse files
ports/esp32/esp32_rmt.c: ISR-driven pulse generator in RMT.
See #7015. This adds 2 new functions to ports/esp32/esp32_rmt.c: // RMT.store_pulses(self, item_list:list[int]) -> None // RMT.issue_pulses(self, tx_ready_func:callable, item_index:int, item_count:int, clock_div:int) -> None
1 parent 89b3207 commit 207e296

File tree

1 file changed

+71
-0
lines changed

1 file changed

+71
-0
lines changed

ports/esp32/esp32_rmt.c

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -337,6 +337,75 @@ STATIC mp_obj_t esp32_rmt_write_pulses(size_t n_args, const mp_obj_t *args) {
337337
}
338338
STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(esp32_rmt_write_pulses_obj, 2, 3, esp32_rmt_write_pulses);
339339

340+
// RMT.store_pulses(self, item_list:list[int32]) -> None
341+
STATIC mp_obj_t esp32_rmt_store_pulses(size_t n_args, const mp_obj_t *args) {
342+
esp32_rmt_obj_t *self = MP_OBJ_TO_PTR(args[0]);
343+
mp_obj_t item_list_obj = args[1];
344+
345+
size_t num_items = 0;
346+
mp_obj_t *item_list_ptr = NULL;
347+
mp_obj_get_array(item_list_obj, &num_items, &item_list_ptr);
348+
349+
if (num_items > self->num_items) {
350+
self->items = (rmt_item32_t *)m_realloc(self->items, num_items * sizeof(rmt_item32_t *));
351+
self->num_items = num_items;
352+
}
353+
354+
for (mp_uint_t item_index = 0; item_index < num_items; item_index++) {
355+
self->items[item_index].val = mp_obj_get_int(item_list_ptr[item_index]);
356+
}
357+
358+
return mp_const_none;
359+
}
360+
STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(esp32_rmt_store_pulses_obj, 2, 2, esp32_rmt_store_pulses);
361+
362+
#include "py/gc.h"
363+
#include "py/mpthread.h"
364+
#include "py/stackctrl.h"
365+
366+
// called from esp32 RMT system ISR provided by rmt_driver_install()
367+
STATIC void esp32_rmt_private_tx_end_callback(rmt_channel_t channel, void *arg) {
368+
369+
void *state_past = mp_thread_get_state();
370+
mp_state_thread_t state_next;
371+
mp_thread_set_state(&state_next);
372+
373+
mp_stack_set_top(&state_next + 1);
374+
mp_stack_set_limit(1024);
375+
376+
mp_locals_set(mp_state_ctx.thread.dict_locals);
377+
mp_globals_set(mp_state_ctx.thread.dict_globals);
378+
379+
mp_sched_lock();
380+
gc_lock();
381+
382+
mp_obj_t *tx_ready_fn = (mp_obj_t *)arg;
383+
mp_call_function_0(tx_ready_fn);
384+
385+
gc_unlock();
386+
mp_sched_unlock();
387+
mp_thread_set_state(state_past);
388+
389+
}
390+
391+
// RMT.issue_pulses(self, tx_ready_func:callable, item_index:int, item_count:int, clock_div:int) -> None
392+
STATIC mp_obj_t esp32_rmt_issue_pulses(size_t n_args, const mp_obj_t *args) {
393+
esp32_rmt_obj_t *self = MP_OBJ_TO_PTR(args[0]);
394+
mp_obj_t *tx_ready_fn = MP_OBJ_TO_PTR(args[1]);
395+
mp_uint_t item_index = mp_obj_get_int(args[2]);
396+
mp_uint_t item_count = mp_obj_get_int(args[3]);
397+
self->clock_div = mp_obj_get_int(args[4]);
398+
399+
check_esp_err(rmt_set_clk_div(self->channel_id, self->clock_div));
400+
401+
rmt_register_tx_end_callback(esp32_rmt_private_tx_end_callback, tx_ready_fn);
402+
403+
check_esp_err(rmt_write_items(self->channel_id, self->items + item_index, item_count, false));
404+
405+
return mp_const_none;
406+
}
407+
STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(esp32_rmt_issue_pulses_obj, 5, 5, esp32_rmt_issue_pulses);
408+
340409
STATIC mp_obj_t esp32_rmt_bitstream_channel(size_t n_args, const mp_obj_t *args) {
341410
if (n_args > 0) {
342411
if (args[0] == mp_const_none) {
@@ -366,6 +435,8 @@ STATIC const mp_rom_map_elem_t esp32_rmt_locals_dict_table[] = {
366435
{ MP_ROM_QSTR(MP_QSTR_wait_done), MP_ROM_PTR(&esp32_rmt_wait_done_obj) },
367436
{ MP_ROM_QSTR(MP_QSTR_loop), MP_ROM_PTR(&esp32_rmt_loop_obj) },
368437
{ MP_ROM_QSTR(MP_QSTR_write_pulses), MP_ROM_PTR(&esp32_rmt_write_pulses_obj) },
438+
{ MP_ROM_QSTR(MP_QSTR_store_pulses), MP_ROM_PTR(&esp32_rmt_store_pulses_obj) },
439+
{ MP_ROM_QSTR(MP_QSTR_issue_pulses), MP_ROM_PTR(&esp32_rmt_issue_pulses_obj) },
369440

370441
// Static methods
371442
{ MP_ROM_QSTR(MP_QSTR_bitstream_channel), MP_ROM_PTR(&esp32_rmt_bitstream_channel_obj) },

0 commit comments

Comments
 (0)
0