BTSTack on ESP32 runloop

352 views
Skip to first unread message

Martin Slater

unread,
Oct 8, 2020, 3:17:42 AM10/8/20
to btstack-dev
Hi there,
I am integrating BTStack into an existing application running on an ESP32. Thiis app already has a main run loop and I am trying to get btstack to play nice and not block this main thread.  If I just do the initialisation and call btstack_run_loop_execute() then it behaves as expected and my test device is discoverable but this obviously blocks the calling thread. 

I have tried moving all the initialisation to another task and calling btstack_run_loop_execute() from there but then it does not work correctly.  I can see a lot of info around the freertos runloop but cannnot find any example of using it in a multithreaded or non-blocking way. Can someone point me to how to get this working on another task or how to solve this?

thanks

Martin

Martin Slater

unread,
Oct 8, 2020, 4:35:32 AM10/8/20
to btstack-dev
I have succeeded in getting it to run correctly within another task but weirdly only if  ENABLE_LOG_DEBUG is enabled in btstack_config.h which seems to suggest a synchronisation issue and the resulting printf are adding sync points that make it work. I will restructure the app to support the builtin runloop structuring and see what that does.

Martin

Matthias Ringwald

unread,
Oct 8, 2020, 7:53:01 AM10/8/20
to btsta...@googlegroups.com
Hi Martin

The BTstack examples should work if you create a new thread and call bststack_init, btstack_main, and btstack_run_loop_execute on that thread with/without debug output.

In general, BTstack isn't thread-safe, so you cannot call any function from a different thread. However, you can call btstack_run_loop_freertos_execute_code_on_main_thread(..) to request a callback from the BTstack thread, and e.g. use a queue or just some global flags to trigger your application Bluetooth logic.

Best
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/321d2415-a8f5-47e9-a2f5-5bd2fb69faf6n%40googlegroups.com.

martinsl...@gmail.com

unread,
Oct 10, 2020, 1:27:28 AM10/10/20
to btsta...@googlegroups.com
Thanks for the info Matthias, I will test that out.   

I have been trying to get simple pairing working first between the ESP32 and and Android phone based on one of the examples and what I have seems flaky at best. Sometimes it pairs,  but most of the time it doesn't. The phone correctly brings up a message to accept pairing then after clicking OK on that it waits a while then times out. I have attached the modified example and logs, can you see what I doing wrong?

On 8/10/2020 10:53:03 PM, Matthias Ringwald <matthias...@gmail.com> wrote:

Hi Martin

The BTstack examples should work if you create a new thread and call bststack_init, btstack_main, and btstack_run_loop_execute on that thread with/without debug output.

In general, BTstack isn't thread-safe, so you cannot call any function from a different thread. However, you can call btstack_run_loop_freertos_execute_code_on_main_thread(..) to request a callback from the BTstack thread, and e.g. use a queue or just some global flags to trigger your application Bluetooth logic.

Best
Matthias


> On 8 Oct 2020, at 10:35, Martin Slater wrote:
>
> I have succeeded in getting it to run correctly within another task but weirdly only if ENABLE_LOG_DEBUG is enabled in btstack_config.h which seems to suggest a synchronisation issue and the resulting printf are adding sync points that make it work. I will restructure the app to support the builtin runloop structuring and see what that does.
>
> Martin
> On Thursday, 8 October 2020 at 18:17:42 UTC+11 Martin Slater wrote:
> Hi there,
> I am integrating BTStack into an existing application running on an ESP32. Thiis app already has a main run loop and I am trying to get btstack to play nice and not block this main thread. If I just do the initialisation and call btstack_run_loop_execute() then it behaves as expected and my test device is discoverable but this obviously blocks the calling thread.
>
> I have tried moving all the initialisation to another task and calling btstack_run_loop_execute() from there but then it does not work correctly. I can see a lot of info around the freertos runloop but cannnot find any example of using it in a multithreaded or non-blocking way. Can someone point me to how to get this working on another task or how to solve this?
>
> thanks
>
> Martin
>
> --
> 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/321d2415-a8f5-47e9-a2f5-5bd2fb69faf6n%40googlegroups.com.

--
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/UnSkv6Bxxhs/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/5416A88F-4C74-4518-8B91-4B392E4CE382%40gmail.com.
spp_streamer_logs.txt
pair_only.c

Martin Slater

unread,
Oct 11, 2020, 7:06:19 AM10/11/20
to btstack-dev

The failing to pair problem looks like it is an issue with the version of esp-idf that is used by PlatformIO.  Building with the master branch of esp32-if with no platform IO works fine. 

Martin

Matthias Ringwald

unread,
Oct 12, 2020, 10:40:41 AM10/12/20
to btsta...@googlegroups.com
Hi Martin

The version of esp-idf should not be crucial. If you continue to use PlatformIO (which is quite interesting, although I didn't try it yet), please let us know if using a newer version of esp-idf with PlatformIO makes it work, or still causes issues. I would ask to show your app_main() in that case.

best
Matthias
> To view this discussion on the web visit https://groups.google.com/d/msgid/btstack-dev/88807f9f-9a5e-42a8-814a-00d56e0cdf18n%40googlegroups.com.

Reply all
Reply to author
Forward
0 new messages