Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Raspberry Pico W can't store link key #498

Open
wasdwasd0105 opened this issue Jul 11, 2023 · 10 comments
Open

Raspberry Pico W can't store link key #498

wasdwasd0105 opened this issue Jul 11, 2023 · 10 comments

Comments

@wasdwasd0105
Copy link

Describe the bug
Hello,
I am writing a BTstack project with Pico W and want to save paired info. However, it seems BTstack may not utilize Pico's storage to save link key. gap_store_link_key_for_bd_addr was called to save link key. However, I get nothing when I use list_link_keys() to get the key

To Reproduce

Here is my project: https://github.com/wasdwasd0105/PicoW-usb2bt-audio

Screenshot 2023-07-10 at 8 29 41 PM

Expected behavior

A clear and concise description of what you expected to happen.

Additional context
Add any other context about the problem here.

@mringwal
Copy link
Member

Hi. That's weird. BTstack uses it's btstack_tlv.h for persistent storage. On the Pico W, this is implemented in btstack_flash_bank.c, which provides pico_flash_bank_instance().

If you run the a2dp_sink_demo and connect from a mobile phone. Can you connect again after the pairing?

I would assume yes - I've played with the Cosmic Unicorn and BTstack a while ago and don't remember the need to pair every time.

@peterharperuk the list_link_keys() functions uses the link key iterator provided by our btstack_link_key_db_tlv.c, which only requires that btstack_tlv is properly set-up. The iteration uses the same logic as the regular look-up. (just to get you into the loop, no action item yet :)

@peterharperuk
Copy link
Contributor

Yes, it should work. Pico W uses the last two blocks in flash to store the TLV stuff. It's possible to overwrite those, but it doesn't seem likely. Where your code that calls list_link_keys?

@mringwal
Copy link
Member

@peterharperuk if you like, please copy list_link_keys from https://github.com/bluekitchen/btstack/blob/master/example/gap_link_keys.c#L64
into one of the classic examples and call it after pairing (or on start after a reboot..)

@wasdwasd0105
Copy link
Author

Screenshot 2023-07-14 at 11 03 34 PM

I think I got the reason: the storage system works well if I compile directly from https://github.com/raspberrypi/pico-examples/ and it will fail if I use a third-party temple such as https://github.com/MrGreensWorkshop/RasPiPicoSDK_BT_Classic_SPP. So what should I do to initiate the storage/DB part properly?

@wasdwasd0105
Copy link
Author

wasdwasd0105 commented Jul 15, 2023

And I found if I use a temple project, I can't store data. The issue is flash_safe_exectue will return PICO_ERROR_NOT_PREMITTED(-4). I remove the safe check func and directly write flash, it can save data to storage

Screenshot 2023-07-15 at 1 00 33 AM Screenshot 2023-07-15 at 1 00 11 AM Screenshot 2023-07-15 at 12 59 45 AM

@mringwal
Copy link
Member

Interesting find. We've only checked our examples on the Pico W.
BTstack's TLV is initialized from cyw43_arch_init() which is also called from the main.c in MrGreensWorkshop.

Could you start your app development from one of them? For SPP, please have a look at spp_counter or spp_streamer - the first one sends a counter, the second tries to send as fast as possible.

@sunnyqeen
Copy link

@wasdwasd0105
bool flash_safe_execute_core_init(void) is needed for multi-core
see pico-sdk\src\rp2_common\pico_flash\include\pico\flash.h
I got similar problem.

@wasdwasd0105
Copy link
Author

Yes! I solve the issue. I tried flash_safe_execute_core_init(void) on core 1 but still returned error -4. But I defined PICO_FLASH_ASSUME_CORE1_SAFE = 1 and it works. I use core 1 only to handle USB data so I don't use core 1 to write storage. Thanks!

@mringwal
Copy link
Member

@peterharperuk Can you follow the problem and the solution/workaround? Is there a general fix that could be applied?

@wasdwasd0105 wasdwasd0105 reopened this Jul 29, 2023
@wasdwasd0105
Copy link
Author

I will leave the issue open

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants