Runloop with existing loop logic on Pico W

45 views
Skip to first unread message

WHowe China

unread,
Jan 26, 2024, 3:44:31 AMJan 26
to btstack-dev
Hi firends,
I've already have a loop logic in my existing project. All functions are handled on each "tick". Something like this.

while (1) {
   inputs();
   logic();
   tusb_update();
   other_things();
}

Now I need to integrate the BLE report capability. But the runloop execute() takes over the thread, bypassing my existing tick based loop. I can't find a btstack_runloop_tick() like things.
Is there a decent way to integrate the btstack under this situation?

Thank you for your advices.


Matthias Ringwald

unread,
Jan 28, 2024, 7:00:58 AMJan 28
to btsta...@googlegroups.com
Hi WHowe

In general, if BTstack uses a while(1) { ... } loop on bare metal, you could register a data source with the BTstack run loop and enable DATA_SOURCE_CALLBACK_POLL, which would then be called on every iteration.
See https://bluekitchen-gmbh.com/btstack/#architecture/

This might not work on the Pico W if a run mode other than POLL is uses. On the Pico, BTstack makes use of the async_context interface of the Pico SDK.
See https://github.com/raspberrypi/pico-sdk/blob/master/src/rp2_common/pico_async_context/include/pico/async_context.h

I guess you need to sort your "tasks" into periodic ones that are called every x ms and those that result from lower-layer stuff, e.g. interrupts.
If you integrate your code with this async_context, it should be run fine in parallel with BTstack.

I haven't used this async_context directly. For further questions, please ask on the Pico SDK forum.

Hope this helps,
Matthias
> --
> You received this message because you are subscribed to the Google Groups "btstack-dev" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to btstack-dev...@googlegroups.com.
> To view this discussion on the web visit https://groups.google.com/d/msgid/btstack-dev/5c424a1f-2837-4345-aefb-5dfa410b41fdn%40googlegroups.com.

WHowe China

unread,
Jan 28, 2024, 9:17:39 AMJan 28
to btstack-dev

Hi Matt,
Thank you for the explanation, I'll try to make my existing "tasks" be a fake datasource of BTstack, and let BTstack be the scheduler.

Ricardo Quesada

unread,
Mar 22, 2024, 1:04:24 AMMar 22
to btstack-dev
WHowe,

Wondering if you managed to solve the issue ? What approach did you take ? 

I'm trying to do something similar: Pico W + tinyUSB Host + BTstack as Host as well.
I could put tinyUSB main loop in one core, BTstack run-loop in the other core... but ideally I'd like to put tinyUSB and BTstack in one core since I need the other for other tasks.

Matthias Ringwald

unread,
Mar 22, 2024, 1:36:17 PMMar 22
to btsta...@googlegroups.com
Hi Ricardo

I didn't have a look at tinyUSB but as explained earlier, BTstack uses the pico async interface. Ideally, tinyUSB would use that as well (it might already, don't know), then both just need to register with the pico async run loop and can run peacefully next to each other.

Best
Matthias
> To view this discussion on the web visit https://groups.google.com/d/msgid/btstack-dev/bf4bef64-f375-434a-a732-1c0e27d31779n%40googlegroups.com.

Ricardo Quesada

unread,
Mar 22, 2024, 1:37:54 PMMar 22
to btsta...@googlegroups.com
Many thanks Matthias,

I'll look into it. ty!

You received this message because you are subscribed to a topic in the Google Groups "btstack-dev" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/btstack-dev/0xDlfhEDpMg/unsubscribe.
To unsubscribe from this group and all its topics, send an email to btstack-dev...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/btstack-dev/E9E2FCF4-BF2C-4831-ADE2-46A249D324F4%40gmail.com.
Reply all
Reply to author
Forward
0 new messages