How to pass micropython objects from arbitrary Freertos thread in C when calling a python function #12596
-
Hi, I have an arbitrary freertos task in C (on esp32, using esp-idf). This task has a handle to to a micropython callback, let's call it I've seen #10188 but there was no answer, only something along the lines of "you should research", which imo is never an answer, rather stating the obvious, like asking who does this raincoat belong to and someone answering: "to its owner". Asking a question in a forum IS research, so it would be nice if someone who knows would simply answer, rather than say "you should find the answer" (everybody knows that). I've looked into |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments 3 replies
-
All of the ISR handling rules apply here. So instead of |
Beta Was this translation helpful? Give feedback.
-
@moefear85 In #8945 I am working on a way for a RTOS task to synchronously call into MicroPython to execute a callback. We need this for BLE events, and currently there's a specific implementation of this hidden inside modbluetooth.c, so this PR makes it into a more generic utility. The PR is WIP though and still being tested so I wouldn't rely on it for now, but this is something we hope to merge soon. |
Beta Was this translation helpful? Give feedback.
-
ok. I'm not facing such difficulties as freertos provides queues to pass information across threads without issues. I just |
Beta Was this translation helpful? Give feedback.
my_callback
will need to be reachable by the GC somehow to avoid releasing the memory too early. This could be done by creating a new root pointer (MP_REGISTER_ROOT_POINTER()
) or by attaching it to some other Python object whose lifetime is synchronized with the rtos task.All of the ISR handling rules apply here. So instead of
mp_obj_new_int()
, you could useMP_OBJ_NEW_SMALL_INT()
(as long as you don't need all 32 bits) and for bytes, you would need to use a pre-allocatedbytearray
.