Callback from a different FreeRTOS task
Posted: Tue Jun 07, 2022 3:57 pm
I'm working on an ESP32 project in which FreeRTOS is used. I have a communications system which isn't integrated into Micropython and this generates messages on a different task than then one Micropython runs on. I am trying to handle an inbound message (which is asynchronous to Micropython operation).
My code does this:
void messageCallback(const uint8_t* pCallbackData, uint32_t callbackLen) {
mp_obj_t arg = mp_obj_new_str(callbackData, callbackLen);
mp_sched_schedule(callbackObj, arg);
}
In the past (Micropython <= v1.16) this seemed to work ok - although I'm not sure why as I assume mp_obj_new_str() isn't thread safe.
Anyhow, currently (Micropython v.1.8) it crashes.
Is there a way to create a string object (that is garbage collected later) on this alternate task (thread) and then pass this to mp_sched_schedule() ?
Or should I try to find a workaround?
I noticed in the extint.c file that this passes (as the arg to mp_sched_schedule) a pointer to a static called pyb_extint_callback_arg and there is a comment there about "no need to scan by GC". So maybe this approach is possible although I have a string of variable length.
Another option I guess would be to pass a pointer to self - it seems that machine_i2c.c and machine_timer.c do this. In this case I guess I could add the message to a FreeRTOS queue (or similar) and the callback could act as a hint that the python code should go and fetch from the queue?
Any suggestions warmly received.
My code does this:
void messageCallback(const uint8_t* pCallbackData, uint32_t callbackLen) {
mp_obj_t arg = mp_obj_new_str(callbackData, callbackLen);
mp_sched_schedule(callbackObj, arg);
}
In the past (Micropython <= v1.16) this seemed to work ok - although I'm not sure why as I assume mp_obj_new_str() isn't thread safe.
Anyhow, currently (Micropython v.1.8) it crashes.
Is there a way to create a string object (that is garbage collected later) on this alternate task (thread) and then pass this to mp_sched_schedule() ?
Or should I try to find a workaround?
I noticed in the extint.c file that this passes (as the arg to mp_sched_schedule) a pointer to a static called pyb_extint_callback_arg and there is a comment there about "no need to scan by GC". So maybe this approach is possible although I have a string of variable length.
Another option I guess would be to pass a pointer to self - it seems that machine_i2c.c and machine_timer.c do this. In this case I guess I could add the message to a FreeRTOS queue (or similar) and the callback could act as a hint that the python code should go and fetch from the queue?
Any suggestions warmly received.