From d9c88c6306f1ba122f5861465292669d9a29a2d8 Mon Sep 17 00:00:00 2001 From: Peter Harper <77111776+peterharperuk@users.noreply.github.com> Date: Mon, 27 Feb 2023 17:12:20 +0000 Subject: [PATCH] Make sure BT device address is set. (#1284) * Make sure BT device address is set. * Change cyw43_hal_generate_laa_mac to match MicroPython --- .../pico_cyw43_driver/CMakeLists.txt | 1 + .../pico_cyw43_driver/btstack_chipset_cyw43.c | 26 +++++++++++++++++++ .../pico_cyw43_driver/btstack_cyw43.c | 2 +- .../btstack_hci_transport_cyw43.c | 10 +++++++ .../pico_cyw43_driver/cyw43_driver.c | 15 ++++++----- .../include/pico/btstack_chipset_cyw43.h | 18 +++++++++++++ 6 files changed, 65 insertions(+), 7 deletions(-) create mode 100644 src/rp2_common/pico_cyw43_driver/btstack_chipset_cyw43.c create mode 100644 src/rp2_common/pico_cyw43_driver/include/pico/btstack_chipset_cyw43.h diff --git a/src/rp2_common/pico_cyw43_driver/CMakeLists.txt b/src/rp2_common/pico_cyw43_driver/CMakeLists.txt index bc0663e88..4cdf95b33 100644 --- a/src/rp2_common/pico_cyw43_driver/CMakeLists.txt +++ b/src/rp2_common/pico_cyw43_driver/CMakeLists.txt @@ -61,6 +61,7 @@ if (EXISTS ${PICO_CYW43_DRIVER_PATH}/${CYW43_DRIVER_TEST_FILE}) pico_add_library(pico_btstack_hci_transport_cyw43 NOFLAG) target_sources(pico_btstack_hci_transport_cyw43 INTERFACE ${CMAKE_CURRENT_LIST_DIR}/btstack_hci_transport_cyw43.c + ${CMAKE_CURRENT_LIST_DIR}/btstack_chipset_cyw43.c ) target_include_directories(pico_btstack_hci_transport_cyw43_headers INTERFACE ${CMAKE_CURRENT_LIST_DIR}/include diff --git a/src/rp2_common/pico_cyw43_driver/btstack_chipset_cyw43.c b/src/rp2_common/pico_cyw43_driver/btstack_chipset_cyw43.c new file mode 100644 index 000000000..2c481ed2b --- /dev/null +++ b/src/rp2_common/pico_cyw43_driver/btstack_chipset_cyw43.c @@ -0,0 +1,26 @@ +/* + * Copyright (c) 2023 Raspberry Pi (Trading) Ltd. + * + * SPDX-License-Identifier: BSD-3-Clause + */ + +#include "pico/btstack_chipset_cyw43.h" + +static void chipset_set_bd_addr_command(bd_addr_t addr, uint8_t *hci_cmd_buffer) { + hci_cmd_buffer[0] = 0x01; + hci_cmd_buffer[1] = 0xfc; + hci_cmd_buffer[2] = 0x06; + reverse_bd_addr(addr, &hci_cmd_buffer[3]); +} + +static const btstack_chipset_t btstack_chipset_cyw43 = { + .name = "CYW43", + .init = NULL, + .next_command = NULL, + .set_baudrate_command = NULL, + .set_bd_addr_command = chipset_set_bd_addr_command, +}; + +const btstack_chipset_t * btstack_chipset_cyw43_instance(void) { + return &btstack_chipset_cyw43; +} diff --git a/src/rp2_common/pico_cyw43_driver/btstack_cyw43.c b/src/rp2_common/pico_cyw43_driver/btstack_cyw43.c index 751b134bc..75061f167 100644 --- a/src/rp2_common/pico_cyw43_driver/btstack_cyw43.c +++ b/src/rp2_common/pico_cyw43_driver/btstack_cyw43.c @@ -59,7 +59,7 @@ bool btstack_cyw43_init(async_context_t *context) { #endif #endif - hci_init(hci_transport_cyw43_instance(), NULL); + hci_init(hci_transport_cyw43_instance(), NULL); // setup TLV storage setup_tlv(); diff --git a/src/rp2_common/pico_cyw43_driver/btstack_hci_transport_cyw43.c b/src/rp2_common/pico_cyw43_driver/btstack_hci_transport_cyw43.c index e335c6751..c0210b651 100644 --- a/src/rp2_common/pico_cyw43_driver/btstack_hci_transport_cyw43.c +++ b/src/rp2_common/pico_cyw43_driver/btstack_hci_transport_cyw43.c @@ -9,6 +9,7 @@ #include "hci_transport.h" #include "hci.h" #include "pico/btstack_hci_transport_cyw43.h" +#include "pico/btstack_chipset_cyw43.h" // assert outgoing pre-buffer for cyw43 header is available #if !defined(HCI_OUTGOING_PRE_BUFFER_SIZE) || (HCI_OUTGOING_PRE_BUFFER_SIZE < 4) @@ -59,6 +60,15 @@ static int hci_transport_cyw43_open(void) { return err; } + // OTP should be set in which case BT gets an address of wifi mac + 1 + // If OTP is not set for some reason BT gets set to 43:43:A2:12:1F:AC. + // So for safety, set the bluetooth device address here. + bd_addr_t addr; + cyw43_hal_get_mac(0, (uint8_t*)&addr); + addr[BD_ADDR_LEN - 1]++; + hci_set_chipset(btstack_chipset_cyw43_instance()); + hci_set_bd_addr(addr); + btstack_run_loop_set_data_source_handler(&transport_data_source, &hci_transport_data_source_process); btstack_run_loop_enable_data_source_callbacks(&transport_data_source, DATA_SOURCE_CALLBACK_POLL); btstack_run_loop_add_data_source(&transport_data_source); diff --git a/src/rp2_common/pico_cyw43_driver/cyw43_driver.c b/src/rp2_common/pico_cyw43_driver/cyw43_driver.c index 6a3d01ce0..e63a9fafb 100644 --- a/src/rp2_common/pico_cyw43_driver/cyw43_driver.c +++ b/src/rp2_common/pico_cyw43_driver/cyw43_driver.c @@ -129,13 +129,16 @@ uint32_t storage_read_blocks(__unused uint8_t *dest, __unused uint32_t block_num } // Generate a mac address if one is not set in otp -void __attribute__((weak)) cyw43_hal_generate_laa_mac(__unused int idx, uint8_t buf[6]) { +void __attribute__((weak)) cyw43_hal_generate_laa_mac(int idx, uint8_t buf[6]) { CYW43_DEBUG("Warning. No mac in otp. Generating mac from board id\n"); - pico_unique_board_id_t board_id; - pico_get_unique_board_id(&board_id); - memcpy(buf, &board_id.id[2], 6); - buf[0] &= (uint8_t)~0x1; // unicast - buf[0] |= 0x2; // locally administered + pico_unique_board_id_t pid; + pico_get_unique_board_id(&pid); + buf[0] = 0x02; // LAA range + buf[1] = (pid.id[7] << 4) | (pid.id[6] & 0xf); + buf[2] = (pid.id[5] << 4) | (pid.id[4] & 0xf); + buf[3] = (pid.id[3] << 4) | (pid.id[2] & 0xf); + buf[4] = pid.id[1]; + buf[5] = (pid.id[0] << 2) | idx; } // Return mac address diff --git a/src/rp2_common/pico_cyw43_driver/include/pico/btstack_chipset_cyw43.h b/src/rp2_common/pico_cyw43_driver/include/pico/btstack_chipset_cyw43.h new file mode 100644 index 000000000..037b68ddf --- /dev/null +++ b/src/rp2_common/pico_cyw43_driver/include/pico/btstack_chipset_cyw43.h @@ -0,0 +1,18 @@ +/* + * Copyright (c) 2023 Raspberry Pi (Trading) Ltd. + * + * SPDX-License-Identifier: BSD-3-Clause + */ + +#ifndef _PICO_BTSTACK_CHIPSET_CYW43_H +#define _PICO_BTSTACK_CHIPSET_CYW43_H + +#include "btstack_chipset.h" + +/** + * \brief Return the singleton BTstack chipset CY43 API instance + * \ingroup pico_btstack + */ +const btstack_chipset_t * btstack_chipset_cyw43_instance(void); + +#endif